module RecCompiler where import Language.Haskell.Pretty import Target import RecType comp :: Expr -> Target comp (Const c) = TUnit c comp (Var x) = TUnit (TVar x) comp (Val (x, m) n) = comp m `TBind` TLambda1 x (comp n) comp (Let decls n) = TLet (map (\ (x, m) -> let TUnit c = comp m in (PVar x, c)) decls) (comp n) comp (App f x) = comp f `TBind` TLambda1 "_f" (comp x `TBind` TLambda1 "_x" (TApp1 (TVar "_f") (TVar "_x"))) comp (Lambda x m) = TUnit (if x=="_" then TLambda0 (comp m) else TLambda1 x (comp m)) comp (Delay m) = TUnit (comp m) comp (If e1 e2 e3) = comp e1 `TBind` TLambda1 "_b" (TIf (TVar "_b") (comp e2) (comp e3)) comp (While e1 e2) = TLet [(PVar "_while", body)] (TVar "_while") where body = comp e1 `TBind` TLambda1 "_b" (TIf (TVar "_b") (comp e2 `TBind` TLambda0 (TVar "_while")) (TUnit (TVar "()"))) comp (Begin [e]) = comp e comp (Begin (e:es)) = comp e `TBind` TLambda0 (comp (Begin es)) compDecls :: Decls -> [(Pattern, Target)] compDecls decls = map (\ (x, m) -> let TUnit c = comp m in (PVar x, c)) decls