[Haskell-cafe] Eval of a syntax tree for reduction

Antti-Juhani Kaijanaho antti-juhani at kaijanaho.info
Tue Mar 28 00:42:01 EST 2006


Steve Downey wrote:
> It makes eval1 a bit more complicated, and not as straightforward 
> translation from the type system being described, though.
> e.g reducing If looks more like
> 
> eval1 (TmIfExpr t1 t2 t3) =
>     let t1' = eval1 t1
>     in  case t1' of
>             { Just t1'' -> Just $ TmIfExpr t1'' t2 t3
>             ; Nothing -> Nothing
>             }

You should use the fact that Maybe is a monad:

eval1 (TmIfExpr t1 t2 t3) =
   do t1' <- eval1 t1
      return $ TmIfExpr t1' t2 t3

> and eval then looks like
> eval t =
>     let t' = eval1 t
>     in case t' of
>          { Just t'' -> eval t''
>          ; Nothing -> t'
>          }

(In the above, I suspect you need Nothing -> t, no prime.)

BTW, there's no need to use let here:

eval t = case eval1 t of Just t' -> eval t'
                          Nothing -> t




More information about the Haskell-Cafe mailing list