@@ -34,12 +34,12 @@ import Data.List
34import Data.Maybe
34import Data.Maybe
35import Debug.Trace
35import Debug.Trace
37−data DExp = DV LVar
37+data DExp = DV Name
38 | DApp Bool Name [DExp] -- True = tail call
38 | DApp Bool Name [DExp] -- True = tail call
39 | DLet Name DExp DExp -- name just for pretty printing
39 | DLet Name DExp DExp -- name just for pretty printing
40 | DUpdate Name DExp -- eval expression, then update var with it
40 | DUpdate Name DExp -- eval expression, then update var with it
41 | DProj DExp Int
41 | DProj DExp Int
42− | DC (Maybe LVar) Int Name [DExp]
42+ | DC (Maybe Name) Int Name [DExp]
43 | DCase CaseType DExp [DAlt]
43 | DCase CaseType DExp [DAlt]
44 | DChkCase DExp [DAlt] -- a case where the type is unknown (for EVAL/APPLY)
44 | DChkCase DExp [DAlt] -- a case where the type is unknown (for EVAL/APPLY)
45 | DConst Const
45 | DConst Const
@@ -90,27 +90,27 @@ addApps defs (n, LFun _ _ args e)
90 return (n, DFun n args e')
90 return (n, DFun n args e')
92 aa :: [Name] -> LExp -> State ([Name], [(Name, Int)]) DExp
92 aa :: [Name] -> LExp -> State ([Name], [(Name, Int)]) DExp
93− aa env (LV (Glob n)) | n `elem` env = return $ DV (Glob n)
93+ aa env (LV n) | n `elem` env = return $ DV n
94− | otherwise = aa env (LApp False (LV (Glob n)) [])
94+ | otherwise = aa env (LApp False (LV n) [])
95− aa env (LApp tc (LV (Glob n)) args)
95+ aa env (LApp tc (LV n) args)
96 = do args' <- mapM (aa env) args
96 = do args' <- mapM (aa env) args
97 case lookupCtxtExact n defs of
97 case lookupCtxtExact n defs of
98 Just (LConstructor _ i ar) -> return $ DApp tc n args'
98 Just (LConstructor _ i ar) -> return $ DApp tc n args'
99 Just (LFun _ _ as _) -> let arity = length as in
99 Just (LFun _ _ as _) -> let arity = length as in
100 fixApply tc n args' arity
100 fixApply tc n args' arity
101− Nothing -> return $ chainAPPLY (DV (Glob n)) args'
101+ Nothing -> return $ chainAPPLY (DV n) args'
102 aa env (LLazyApp n args)
102 aa env (LLazyApp n args)
103 = do args' <- mapM (aa env) args
103 = do args' <- mapM (aa env) args
104 case lookupCtxtExact n defs of
104 case lookupCtxtExact n defs of
105 Just (LConstructor _ i ar) -> return $ DApp False n args'
105 Just (LConstructor _ i ar) -> return $ DApp False n args'
106 Just (LFun _ _ as _) -> let arity = length as in
106 Just (LFun _ _ as _) -> let arity = length as in
107 fixLazyApply n args' arity
107 fixLazyApply n args' arity
108− Nothing -> return $ chainAPPLY (DV (Glob n)) args'
108+ Nothing -> return $ chainAPPLY (DV n) args'
109− aa env (LForce (LLazyApp n args)) = aa env (LApp False (LV (Glob n)) args)
109+ aa env (LForce (LLazyApp n args)) = aa env (LApp False (LV n) args)
110 aa env (LForce e) = liftM eEVAL (aa env e)
110 aa env (LForce e) = liftM eEVAL (aa env e)
111 aa env (LLet n v sc) = liftM2 (DLet n) (aa env v) (aa (n : env) sc)
111 aa env (LLet n v sc) = liftM2 (DLet n) (aa env v) (aa (n : env) sc)
112 aa env (LCon loc i n args) = liftM (DC loc i n) (mapM (aa env) args)
112 aa env (LCon loc i n args) = liftM (DC loc i n) (mapM (aa env) args)
113− aa env (LProj t@(LV (Glob n)) i)
113+ aa env (LProj t@(LV n) i)
114 | n `elem` env = do t' <- aa env t
114 | n `elem` env = do t' <- aa env t
115 return $ DProj (DUpdate n t') i
115 return $ DProj (DUpdate n t') i
116 aa env (LProj t i) = do t' <- aa env t
116 aa env (LProj t i) = do t' <- aa env t
@@ -170,7 +170,7 @@ addApps defs (n, LFun _ _ args e)
171 preEval [] t = t
171 preEval [] t = t
172 preEval (x : xs) t
172 preEval (x : xs) t
173− | needsEval x t = DLet x (DV (Glob x)) (preEval xs t)
173+ | needsEval x t = DLet x (DV x) (preEval xs t)
174 | otherwise = preEval xs t
174 | otherwise = preEval xs t
176 needsEval x (DApp _ _ args) = any (needsEval x) args
176 needsEval x (DApp _ _ args) = any (needsEval x) args
@@ -188,7 +188,7 @@ addApps defs (n, LFun _ _ args e)
188 | otherwise = needsEval x v || needsEval x e
188 | otherwise = needsEval x v || needsEval x e
189 needsEval x (DForeign _ _ args) = any (needsEval x) (map snd args)
189 needsEval x (DForeign _ _ args) = any (needsEval x) (map snd args)
190 needsEval x (DOp op args) = any (needsEval x) args
190 needsEval x (DOp op args) = any (needsEval x) args
191− needsEval x (DProj (DV (Glob x')) _) = x == x'
191+ needsEval x (DProj (DV x') _) = x == x'
192 needsEval x _ = False
192 needsEval x _ = False
194eEVAL x = DApp False (sMN 0 "EVAL") [x]
194eEVAL x = DApp False (sMN 0 "EVAL") [x]
@@ -207,7 +207,7 @@ toCons ns (n, i)
207 EvalCase (\tlarg ->
207 EvalCase (\tlarg ->
208 (DConCase (-1) (mkFnCon n) (take i (genArgs 0))
208 (DConCase (-1) (mkFnCon n) (take i (genArgs 0))
209 (dupdate tlarg
209 (dupdate tlarg
210− (DApp False n (map (DV . Glob) (take i (genArgs 0))))))))
210+ (DApp False n (map DV (take i (genArgs 0))))))))
211 : [] -- mkApplyCase n 0 i
211 : [] -- mkApplyCase n 0 i
212 | otherwise = []
212 | otherwise = []
213 where dupdate tlarg x = DUpdate tlarg x
213 where dupdate tlarg x = DUpdate tlarg x
@@ -219,7 +219,7 @@ toConsA ns (n, i)
219-- EvalCase (\tlarg ->
219-- EvalCase (\tlarg ->
220-- (DConCase (-1) (mkFnCon n) (take i (genArgs 0))
220-- (DConCase (-1) (mkFnCon n) (take i (genArgs 0))
221-- (dupdate tlarg
221-- (dupdate tlarg
222−-- (DApp False n (map (DV . Glob) (take i (genArgs 0))))))))
222+-- (DApp False n (map DV (take i (genArgs 0))))))))
223 = mkApplyCase n ar i
223 = mkApplyCase n ar i
224 | otherwise = []
224 | otherwise = []
225 where dupdate tlarg x = x
225 where dupdate tlarg x = x
@@ -229,13 +229,13 @@ mkApplyCase fname n ar
229 = let nm = mkUnderCon fname (ar - n) in
229 = let nm = mkUnderCon fname (ar - n) in
230 (nm, n, ApplyCase (DConCase (-1) nm (take n (genArgs 0))
230 (nm, n, ApplyCase (DConCase (-1) nm (take n (genArgs 0))
231 (DApp False (mkUnderCon fname (ar - (n + 1)))
231 (DApp False (mkUnderCon fname (ar - (n + 1)))
232− (map (DV . Glob) (take n (genArgs 0) ++
232+ (map DV (take n (genArgs 0) ++
233 [sMN 0 "arg"])))))
233 [sMN 0 "arg"])))))
235 if (ar - (n + 2) >=0 )
235 if (ar - (n + 2) >=0 )
236 then (nm, n, Apply2Case (DConCase (-1) nm (take n (genArgs 0))
236 then (nm, n, Apply2Case (DConCase (-1) nm (take n (genArgs 0))
237 (DApp False (mkUnderCon fname (ar - (n + 2)))
237 (DApp False (mkUnderCon fname (ar - (n + 2)))
238− (map (DV . Glob) (take n (genArgs 0) ++
238+ (map DV (take n (genArgs 0) ++
239 [sMN 0 "arg0", sMN 0 "arg1"])))))
239 [sMN 0 "arg0", sMN 0 "arg1"])))))
241 mkApplyCase fname (n + 1) ar
241 mkApplyCase fname (n + 1) ar
@@ -243,9 +243,9 @@ mkApplyCase fname n ar
244mkEval :: [(Name, Int, EvalApply DAlt)] -> (Name, DDecl)
244mkEval :: [(Name, Int, EvalApply DAlt)] -> (Name, DDecl)
245mkEval xs = (sMN 0 "EVAL", DFun (sMN 0 "EVAL") [sMN 0 "arg"]
245mkEval xs = (sMN 0 "EVAL", DFun (sMN 0 "EVAL") [sMN 0 "arg"]
246− (mkBigCase (sMN 0 "EVAL") 256 (DV (Glob (sMN 0 "arg")))
246+ (mkBigCase (sMN 0 "EVAL") 256 (DV (sMN 0 "arg"))
247 (mapMaybe evalCase xs ++
247 (mapMaybe evalCase xs ++
248− [DDefaultCase (DV (Glob (sMN 0 "arg")))])))
248+ [DDefaultCase (DV (sMN 0 "arg"))])))
250 evalCase (n, t, EvalCase x) = Just (x (sMN 0 "arg"))
250 evalCase (n, t, EvalCase x) = Just (x (sMN 0 "arg"))
251 evalCase _ = Nothing
251 evalCase _ = Nothing
@@ -256,7 +256,7 @@ mkApply xs = (sMN 0 "APPLY", DFun (sMN 0 "APPLY") [sMN 0 "fn", sMN 0 "arg"]
256 [] -> DNothing
256 [] -> DNothing
258 mkBigCase (sMN 0 "APPLY") 256
258 mkBigCase (sMN 0 "APPLY") 256
259− (DV (Glob (sMN 0 "fn")))
259+ (DV (sMN 0 "fn"))
260 (cases ++
260 (cases ++
261 [DDefaultCase DNothing])))
261 [DDefaultCase DNothing])))
@@ -269,14 +269,14 @@ mkApply2 xs = (sMN 0 "APPLY2", DFun (sMN 0 "APPLY2") [sMN 0 "fn", sMN 0 "arg0",
269 [] -> DNothing
269 [] -> DNothing
271 mkBigCase (sMN 0 "APPLY") 256
271 mkBigCase (sMN 0 "APPLY") 256
272− (DV (Glob (sMN 0 "fn")))
272+ (DV (sMN 0 "fn"))
273 (cases ++
273 (cases ++
274 [DDefaultCase
274 [DDefaultCase
275 (DApp False (sMN 0 "APPLY")
275 (DApp False (sMN 0 "APPLY")
276 [DApp False (sMN 0 "APPLY")
276 [DApp False (sMN 0 "APPLY")
277− [DV (Glob (sMN 0 "fn")),
277+ [DV (sMN 0 "fn"),
278− DV (Glob (sMN 0 "arg0"))],
278+ DV (sMN 0 "arg0")],
279− DV (Glob (sMN 0 "arg1"))])
279+ DV (sMN 0 "arg1")])
282 applyCase (n, t, Apply2Case x) = Just x
282 applyCase (n, t, Apply2Case x) = Just x
@@ -297,8 +297,7 @@ mkUnderCon n missing = sMN missing ("U_" ++ show n)
298instance Show DExp where
298instance Show DExp where
299 show e = show' [] e where
299 show e = show' [] e where
300− show' env (DV (Loc i)) = "var " ++ env!!i
300+ show' env (DV n) = show n
301− show' env (DV (Glob n)) = "GLOB " ++ show n
302 show' env (DApp _ e args) = show e ++ "(" ++
301 show' env (DApp _ e args) = show e ++ "(" ++
303 showSep ", " (map (show' env) args) ++")"
302 showSep ", " (map (show' env) args) ++")"
304 show' env (DLet n v e) = "let " ++ show n ++ " = " ++ show' env v ++ " in " ++
303 show' env (DLet n v e) = "let " ++ show n ++ " = " ++ show' env v ++ " in " ++