这是我讲义中的一个简单代码。我不太明白。谁能向我解释“case (eval e1, eval e2) of”的含义。据我了解,这个命令应该适用于 const Int。没有关于 eval e1->bool 的讨论。
-- Simple expressions
--
data Expr = Const Int
| Add Expr Expr -- arguments must be Ints
| Equal Expr Expr -- arguments must be of same type
| If Expr Expr Expr -- 1st argument must be a Bool
-- Evaluation results
--
data Value = IntV Int
| BoolV Bool
deriving Show
-- Evaluate a simple expression.
--
eval :: Expr -> Value
eval (Const i) = IntV i
eval (Add e1 e2) =
case (eval e1, eval e2) of
(IntV i1, IntV i2) -> IntV $ i1 + i2
_ -> error "Add: Int expected"
eval (Equal e1 e2) =
case (eval e1, eval e2) of
(IntV i1, IntV i2) -> BoolV $ i1 == i2
(BoolV b1, BoolV b2) -> BoolV $ b1 == b2
_ -> error "Equal: same types expected"
eval (If ec et ee) =
case eval ec of
BoolV flag
| flag -> eval et
| otherwise -> eval ee
_ -> error "If: conditional must be Bool"