执行IO操作,但返回其他类型

时间:2016-05-30 18:55:05

标签: haskell io

对于一个项目,我们被赋予了编写命令式语言并通过Haskell执行它的任务。解析器(这里省略)和部分评估完成。现在只剩下编码效果了。操纵一个小机器人。

给出以下代码:

data Env = Env [Binding]
instance Show Env where
  show (Env (x:xs)) = show x ++ ", " ++ show (Env xs)
  show (Env []) = ""

data Binding = Binding (String,Int)
instance Show Binding where
  show (Binding x) = fst x ++ " : " ++ show (snd x)

lookup' :: String -> Env -> Int
lookup' zoek (Env env) = case elemIndex zoek [fst x | Binding x <- env] of
  Just y ->  y
  Nothing -> error "Not found"


eval :: Stmt -> Env ->  Env
eval (Seq s) env = foldl (flip eval) env s
eval (Assign varName aexpr) env = evalAssign varName aexpr env
eval (If bool stmt1 stmt2) env = evalIf bool stmt1 stmt2 env
eval (While bool stmt) env = undefined
eval (MotorInstruct string aExpr) env = undefined
eval (SensorRead string) env = undefined
eval Skip env = env

evalAExpr :: AExpr -> Env -> Int
evalAExpr (IntConst int) _  = fromInteger int
evalAExpr (Neg a) env = - evalAExpr a env
evalAExpr (ABinary Add a b) env = evalAExpr a env + evalAExpr b env
evalAExpr (ABinary Subtract a b) env = evalAExpr a env - evalAExpr b env
evalAExpr (ABinary Multiply a b) env = evalAExpr a env * evalAExpr b env
evalAExpr (ABinary Divide a b) env = evalAExpr a env `div` evalAExpr b env
evalAExpr (Var x) env = getElementAtEnv env (lookup' x env)
  where
    getElementAtEnv (Env env) index = getSndFromBinding (env !! index)
    getSndFromBinding (Binding (_,t)) = t


evalBExpr :: BExpr -> Env -> Bool
evalBExpr (BoolConst bool) _ = bool
evalBExpr (Not expr) env = not $ evalBExpr expr env
-- Boolean operators
evalBExpr (BBinary And a b) env = evalBExpr a env && evalBExpr b env
evalBExpr (BBinary Or a b) env = evalBExpr a env || evalBExpr b env
-- Relational operators
evalBExpr (RBinary Greater a b) env = evalAExpr a env > evalAExpr b env
evalBExpr (RBinary Less a b) env = evalAExpr a env < evalAExpr b env
evalBExpr (RBinary Equal a b) env = evalAExpr a env == evalAExpr b env


evalIf ::  BExpr -> Stmt -> Stmt -> Env -> Env
evalIf expr s1 s2 env = if evalBExpr expr env
  then
    eval s1 env
  else
    eval s2 env


evalAssign :: String -> AExpr -> Env -> Env
evalAssign term s (Env env)= if term `elem` transform
  then
    Env ( take (lookup' term (Env env)) env ++ [Binding (term, evalAExpr s (Env env))]++ drop (lookup' term (Env env) + 1) env)
  else
    Env (env ++ [Binding (term, evalAExpr s (Env env))])
  where transform = [ fst ele | Binding ele <- env]


zoekMotor :: String -> Int
zoekMotor "left" = 0x9
zoekMotor "right" = 0xa
zoekMotor _ = error "No such motor"


sendToMotor :: String -> Int -> IO()
sendToMotor m s = do
  bot <- openMBot
  sendCommand bot $ setMotor (zoekMotor m) s s
  closeMBot bot

evalMotorInstruct :: String -> AExpr -> Env -> Env
evalMotorInstruct welke waarde env = do
  sendToMotor welke (evalAExpr waarde env)
  return env

我如何在我的评估函数sendToMotor中执行函数IO()(返回evalMotorInstruct),该函数应返回Env?我有点不知道如何执行我的'动作' - 函数,只让我的Env退出评估函数。

请注意evalMotorInstruct的当前代码不正确。该函数应返回Env,但实际上返回IO Env

谢谢

1 个答案:

答案 0 :(得分:2)

您的AExpr和BExpr类型代表您的语言的纯计算 - 不仅它们不执行任何IO,而且它们也不会修改环境。因此,您不必修改其评估函数。

所以你只需要修改class Event { int Priority; }; bool operator<(const Event& lhs, const Event& rhs) {return (lhs.Priority < rhs.Priority);} 值的评估。类型签名将更改为:

Stmt

eval :: Stmt -> Env -> IO Env 将如何更改的示例:

eval Seq

请注意,eval (Seq []) env = return env eval (Seq (s:ss)) env = do env' <- eval s env -- eval the first statement eval (Seq ss) env' -- eval the rest 无需更改:

eval If
如果您将其签名更改为:

eval (If bool stmt1 stmt2) env = if evalBExpr bool env then eval stmt1 env else eval stmt2 env 将编译

evalMotorInstruct

我会把剩下的留给你。

重构时,只需注释掉无法编译的代码。然后逐行逐步添加行,在添加另一行之前将每行重新编译。如果需要,请使用evalMotorInstruct :: String -> AExpr -> Env -> IO Env 。回来后再填这些。