@@ -1,6 +1,7 @@
1module IRTS.Lang where
1module IRTS.Lang where
3import Core.TT
3import Core.TT
4+import Control.Monad.State hiding(lift)
5data LVar = Loc Int | Glob Name
6data LVar = Loc Int | Glob Name
6 deriving Show
7 deriving Show
@@ -9,6 +10,7 @@ data LExp = LV LVar
9 | LApp Bool Name [LExp] -- True = tail call
10 | LApp Bool Name [LExp] -- True = tail call
10 | LLazyApp Name [LExp] -- True = tail call
11 | LLazyApp Name [LExp] -- True = tail call
11 | LLet Name LExp LExp -- name just for pretty printing
12 | LLet Name LExp LExp -- name just for pretty printing
13+ | LLam [Name] LExp -- lambda, lifted out before compiling
12 | LCon Int Name [LExp]
14 | LCon Int Name [LExp]
13 | LCase LExp [LAlt]
15 | LCase LExp [LAlt]
14 | LConst Const
16 | LConst Const
@@ -49,3 +51,68 @@ addTags i ds = tag i ds []
49 tag i (x : as) acc = tag i as (x : acc)
51 tag i (x : as) acc = tag i as (x : acc)
50 tag i [] acc = (i, reverse acc)
52 tag i [] acc = (i, reverse acc)
54+data LiftState = LS Name Int [(Name, LDecl)]
56+lname (NS n x) i = NS (lname n i) x
57+lname (UN n) i = MN i n
58+lname x i = MN i (show x)
60+liftAll :: [(Name, LDecl)] -> [(Name, LDecl)]
61+liftAll xs = concatMap (\ (x, d) -> lambdaLift x d) xs
63+lambdaLift :: Name -> LDecl -> [(Name, LDecl)]
64+lambdaLift n (LFun _ args e)
65+ = let (e', (LS _ _ decls)) = runState (lift args e) (LS n 0 []) in
66+ (n, LFun n args e') : decls
67+lambdaLift n x = [(n, x)]
69+getNextName :: State LiftState Name
70+getNextName = do LS n i ds <- get
71+ put (LS n (i + 1) ds)
74+addFn :: Name -> LDecl -> State LiftState ()
75+addFn fn d = do LS n i ds <- get
76+ put (LS n i ((fn, d) : ds))
78+lift :: [Name] -> LExp -> State LiftState LExp
79+lift env (LV v) = return (LV v)
80+lift env (LApp tc n args) = do args' <- mapM (lift env) args
81+ return (LApp tc n args')
82+lift env (LLazyApp n args) = do args' <- mapM (lift env) args
83+ return (LLazyApp n args')
84+lift env (LLet n v e) = do v' <- lift env v
85+ e' <- lift (env ++ [n]) e
86+ return (LLet n v' e')
87+lift env (LLam args e) = do e' <- lift (env ++ args) e
89+ addFn fn (LFun fn (env ++ args) e')
90+ return (LApp False fn (map (LV . Glob) env))
91+lift env (LCon i n args) = do args' <- mapM (lift env) args
92+ return (LCon i n args')
93+lift env (LCase e alts) = do alts' <- mapM liftA alts
95+ return (LCase e' alts')
97+ liftA (LConCase i n args e) = do e' <- lift (env ++ args) e
98+ return (LConCase i n args e')
99+ liftA (LConstCase c e) = do e' <- lift env e
100+ return (LConstCase c e')
101+ liftA (LDefaultCase e) = do e' <- lift env e
102+ return (LDefaultCase e')
103+lift env (LConst c) = return (LConst c)
104+lift env (LForeign l t s args) = do args' <- mapM (liftF env) args
105+ return (LForeign l t s args')
107+ liftF env (t, e) = do e' <- lift env e
109+lift env (LOp f args) = do args' <- mapM (lift env) args
110+ return (LOp f args')