1{-# LANGUAGE PatternGuards #-}
3module IRTS.Defunctionalise(module IRTS.Defunctionalise,
4 module IRTS.Lang) where
13import Control.Monad.State
16 | DApp Bool Name [DExp] -- True = tail call
17 | DLet Name DExp DExp -- name just for pretty printing
18 | DUpdate Name DExp -- eval expression, then update var with it
22 | DChkCase DExp [DAlt] -- a case where the type is unknown (for EVAL/APPLY)
24 | DForeign FLang FType String [(FType, DExp)]
26 | DNothing -- erased value, can be compiled to anything since it'll never
31data DAlt = DConCase Int Name [Name] DExp
32 | DConstCase Const DExp
36data DDecl = DFun Name [Name] DExp -- name, arg names, definition
37 | DConstructor Name Int Int -- constructor name, tag, arity
40type DDefs = Ctxt DDecl
42defunctionalise :: Int -> LDefs -> DDefs
43defunctionalise nexttag defs
44 = let all = toAlist defs
45 -- sort newcons so that EVAL and APPLY cons get sequential tags
46 (allD, (enames, anames)) = runState (mapM (addApps defs) all) ([], [])
47 anames' = sort (nub anames)
49 newecons = sortBy conord $ concatMap (toCons enames') (getFn all)
50 newacons = sortBy conord $ concatMap (toConsA anames') (getFn all)
51 eval = mkEval newecons
52 app = mkApply newacons
53 condecls = declare nexttag (newecons ++ newacons) in
54 addAlist (eval : app : condecls ++ allD) emptyContext
55 where conord (n, _, _) (n', _, _) = compare n n'
57getFn :: [(Name, LDecl)] -> [(Name, Int)]
58getFn xs = mapMaybe fnData xs
59 where fnData (n, LFun _ _ args _) = Just (n, length args)
64-- 1 Create a data constructor for each function
65-- 2 Create a data constructor for each underapplication of a function
66-- 3 Convert underapplications to their corresponding constructors
67-- 4 Create an EVAL function which calls the appropriate function for data constructors
68-- created as part of step 1
69-- 5 Create an APPLY function which adds an argument to each underapplication (or calls
70-- APPLY again for an exact application)
71-- 6 Wrap overapplications in chains of APPLY
72-- 7 Wrap unknown applications (i.e. applications of local variables) in chains of APPLY
73-- 8 Add explicit EVAL to case, primitives, and foreign calls
75addApps :: LDefs -> (Name, LDecl) -> State ([Name], [(Name, Int)]) (Name, DDecl)
76addApps defs o@(n, LConstructor _ t a)
77 = return (n, DConstructor n t a)
78addApps defs (n, LFun _ _ args e)
80 return (n, DFun n args e')
82 aa :: [Name] -> LExp -> State ([Name], [(Name, Int)]) DExp
83 aa env (LV (Glob n)) | n `elem` env = return $ DV (Glob n)
84 | otherwise = aa env (LApp False (LV (Glob n)) [])
85 aa env (LApp tc (LV (Glob n)) args)
86 = do args' <- mapM (aa env) args
87 case lookupCtxtExact n defs of
88 Just (LConstructor _ i ar) -> return $ DApp tc n args'
89 Just (LFun _ _ as _) -> let arity = length as in
90 fixApply tc n args' arity
91 Nothing -> return $ chainAPPLY (DV (Glob n)) args'
92 aa env (LLazyApp n args)
93 = do args' <- mapM (aa env) args
94 case lookupCtxtExact n defs of
95 Just (LConstructor _ i ar) -> return $ DApp False n args'
96 Just (LFun _ _ as _) -> let arity = length as in
97 fixLazyApply n args' arity
98 Nothing -> return $ chainAPPLY (DV (Glob n)) args'
99 aa env (LForce (LLazyApp n args)) = aa env (LApp False (LV (Glob n)) args)
100 aa env (LForce e) = liftM eEVAL (aa env e)
101 aa env (LLet n v sc) = liftM2 (DLet n) (aa env v) (aa (n : env) sc)
102 aa env (LCon i n args) = liftM (DC i n) (mapM (aa env) args)
103 aa env (LProj t@(LV (Glob n)) i)
104 | n `elem` env = do t' <- aa env t
105 return $ DProj (DUpdate n t') i
106 aa env (LProj t i) = do t' <- aa env t
108 aa env (LCase e alts) = do e' <- aa env e
109 alts' <- mapM (aaAlt env) alts
110 return $ DCase e' alts'
111 aa env (LConst c) = return $ DConst c
112 aa env (LForeign l t n args) = liftM (DForeign l t n) (mapM (aaF env) args)
113 aa env (LOp LFork args) = liftM (DOp LFork) (mapM (aa env) args)
114 aa env (LOp f args) = do args' <- mapM (aa env) args
116 aa env LNothing = return DNothing
117 aa env (LError e) = return $ DError e
119 aaF env (t, e) = do e' <- aa env e
122 aaAlt env (LConCase i n args e)
123 = liftM (DConCase i n args) (aa (args ++ env) e)
124 aaAlt env (LConstCase c e) = liftM (DConstCase c) (aa env e)
125 aaAlt env (LDefaultCase e) = liftM DDefaultCase (aa env e)
127 fixApply tc n args ar
129 = return $ DApp tc n args
131 = do (ens, ans) <- get
132 let alln = map (\x -> (n, x)) [length args .. ar]
133 put (ens, alln ++ ans)
134 return $ DApp tc (mkUnderCon n (ar - length args)) args
136 = return $ chainAPPLY (DApp tc n (take ar args)) (drop ar args)
138 fixLazyApply n args ar
140 = do (ens, ans) <- get
142 return $ DApp False (mkFnCon n) args
144 = do (ens, ans) <- get
145 let alln = map (\x -> (n, x)) [length args .. ar]
146 put (ens, alln ++ ans)
147 return $ DApp False (mkUnderCon n (ar - length args)) args
149 = return $ chainAPPLY (DApp False n (take ar args)) (drop ar args)
152 chainAPPLY f (a : as) = chainAPPLY (DApp False (sMN 0 "APPLY") [f, a]) as
154 -- if anything in the DExp is projected from, we'll need to evaluate it,
155 -- but we only want to do it once, rather than every time we project.
159 | needsEval x t = DLet x (DV (Glob x)) (preEval xs t)
160 | otherwise = preEval xs t
162 needsEval x (DApp _ _ args) = or (map (needsEval x) args)
163 needsEval x (DC _ _ args) = or (map (needsEval x) args)
164 needsEval x (DCase e alts) = needsEval x e || or (map nec alts)
165 where nec (DConCase _ _ _ e) = needsEval x e
166 nec (DConstCase _ e) = needsEval x e
167 nec (DDefaultCase e) = needsEval x e
168 needsEval x (DChkCase e alts) = needsEval x e || or (map nec alts)
169 where nec (DConCase _ _ _ e) = needsEval x e
170 nec (DConstCase _ e) = needsEval x e
171 nec (DDefaultCase e) = needsEval x e
172 needsEval x (DLet n v e)
173 | x == n = needsEval x v
174 | otherwise = needsEval x v || needsEval x e
175 needsEval x (DForeign _ _ _ args) = or (map (needsEval x) (map snd args))
176 needsEval x (DOp op args) = or (map (needsEval x) args)
177 needsEval x (DProj (DV (Glob x')) _) = x == x'
178 needsEval x _ = False
180eEVAL x = DApp False (sMN 0 "EVAL") [x]
182data EvalApply a = EvalCase (Name -> a)
185-- For a function name, generate a list of
186-- data constuctors, and whether to handle them in EVAL or APPLY
188toCons :: [Name] -> (Name, Int) -> [(Name, Int, EvalApply DAlt)]
193 (DConCase (-1) (mkFnCon n) (take i (genArgs 0))
195 (DApp False n (map (DV . Glob) (take i (genArgs 0))))))))
196 : [] -- mkApplyCase n 0 i
198 where dupdate tlarg x = DUpdate tlarg x
200toConsA :: [(Name, Int)] -> (Name, Int) -> [(Name, Int, EvalApply DAlt)]
202 | Just ar <- lookup n ns
204-- EvalCase (\tlarg ->
205-- (DConCase (-1) (mkFnCon n) (take i (genArgs 0))
207-- (DApp False n (map (DV . Glob) (take i (genArgs 0))))))))
210 where dupdate tlarg x = x
212mkApplyCase fname n ar | n == ar = []
213mkApplyCase fname n ar
214 = let nm = mkUnderCon fname (ar - n) in
215 (nm, n, ApplyCase (DConCase (-1) nm (take n (genArgs 0))
216 (DApp False (mkUnderCon fname (ar - (n + 1)))
217 (map (DV . Glob) (take n (genArgs 0) ++
219 : mkApplyCase fname (n + 1) ar
221mkEval :: [(Name, Int, EvalApply DAlt)] -> (Name, DDecl)
222mkEval xs = (sMN 0 "EVAL", DFun (sMN 0 "EVAL") [sMN 0 "arg"]
223 (mkBigCase (sMN 0 "EVAL") 256 (DV (Glob (sMN 0 "arg")))
224 (mapMaybe evalCase xs ++
225 [DDefaultCase (DV (Glob (sMN 0 "arg")))])))
227 evalCase (n, t, EvalCase x) = Just (x (sMN 0 "arg"))
230mkApply :: [(Name, Int, EvalApply DAlt)] -> (Name, DDecl)
231mkApply xs = (sMN 0 "APPLY", DFun (sMN 0 "APPLY") [sMN 0 "fn", sMN 0 "arg"]
232 (case mapMaybe applyCase xs of
235 mkBigCase (sMN 0 "APPLY") 256
236 (DV (Glob (sMN 0 "fn")))
238 [DDefaultCase DNothing])))
240 applyCase (n, t, ApplyCase x) = Just x
241 applyCase _ = Nothing
244declare :: Int -> [(Name, Int, EvalApply DAlt)] -> [(Name, DDecl)]
245declare t xs = dec' t xs [] where
246 dec' t [] acc = reverse acc
247 dec' t ((n, ar, _) : xs) acc = dec' (t + 1) xs ((n, DConstructor n t ar) : acc)
250genArgs i = sMN i "P_c" : genArgs (i + 1)
252mkFnCon n = sMN 0 ("P_" ++ show n)
254mkUnderCon n missing = sMN missing ("U_" ++ show n)
256instance Show DExp where
257 show e = show' [] e where
258 show' env (DV (Loc i)) = "var " ++ env!!i
259 show' env (DV (Glob n)) = show n
260 show' env (DApp _ e args) = show e ++ "(" ++
261 showSep ", " (map (show' env) args) ++")"
262 show' env (DLet n v e) = "let " ++ show n ++ " = " ++ show' env v ++ " in " ++
263 show' (env ++ [show n]) e
264 show' env (DUpdate n e) = "!update " ++ show n ++ "(" ++ show' env e ++ ")"
265 show' env (DC i n args) = show n ++ ")" ++ showSep ", " (map (show' env) args) ++ ")"
266 show' env (DProj t i) = show t ++ "!" ++ show i
267 show' env (DCase e alts) = "case " ++ show' env e ++ " of {\n\t" ++
268 showSep "\n\t| " (map (showAlt env) alts)
269 show' env (DChkCase e alts) = "case' " ++ show' env e ++ " of {\n\t" ++
270 showSep "\n\t| " (map (showAlt env) alts)
271 show' env (DConst c) = show c
272 show' env (DForeign lang ty n args)
273 = "foreign " ++ n ++ "(" ++ showSep ", " (map (show' env) (map snd args)) ++ ")"
274 show' env (DOp f args) = show f ++ "(" ++ showSep ", " (map (show' env) args) ++ ")"
275 show' env (DError str) = "error " ++ show str
276 show' env DNothing = "____"
278 showAlt env (DConCase _ n args e)
279 = show n ++ "(" ++ showSep ", " (map show args) ++ ") => "
281 showAlt env (DConstCase c e) = show c ++ " => " ++ show' env e
282 showAlt env (DDefaultCase e) = "_ => " ++ show' env e
284-- Divide up a large case expression so that each has a maximum of
287mkBigCase cn max arg branches
288 | length branches <= max = DChkCase arg branches
289 | otherwise = -- DChkCase arg branches -- until I think of something...
290 -- divide the branches into groups of at most max (by tag),
291 -- generate a new case and shrink, recursively
292 let bs = sortBy tagOrd branches
293 (all, def) = case (last bs) of
294 DDefaultCase t -> (init all, Just (DDefaultCase t))
296 bss = groupsOf max all
297 cs = map mkCase bss in
298 DChkCase arg branches
300 where mkCase bs = DChkCase arg bs
302 tagOrd (DConCase t _ _ _) (DConCase t' _ _ _) = compare t t'
303 tagOrd (DConstCase c _) (DConstCase c' _) = compare c c'
304 tagOrd (DDefaultCase _) (DDefaultCase _) = EQ
306 tagOrd (DConCase _ _ _ _) (DDefaultCase _) = LT
307 tagOrd (DConCase _ _ _ _) (DConstCase _ _) = LT
308 tagOrd (DConstCase _ _) (DDefaultCase _) = LT
310 tagOrd (DDefaultCase _) (DConCase _ _ _ _) = GT
311 tagOrd (DConstCase _ _) (DConCase _ _ _ _) = GT
312 tagOrd (DDefaultCase _) (DConstCase _ _) = GT
314groupsOf :: Int -> [DAlt] -> [[DAlt]]
316groupsOf x xs = let (batch, rest) = span (tagLT (x + tagHead xs)) xs in
317 batch : groupsOf x rest
318 where tagHead (DConstCase (I i) _ : _) = i
319 tagHead (DConCase t _ _ _ : _) = t
320 tagHead (DDefaultCase _ : _) = -1 -- must be the end
322 tagLT i (DConstCase (I j) _) = i < j
323 tagLT i (DConCase j _ _ _) = i < j
324 tagLT i (DDefaultCase _) = False
326dumpDefuns :: DDefs -> String
327dumpDefuns ds = showSep "\n" $ map showDef (toAlist ds)
328 where showDef (x, DFun fn args exp)
329 = show fn ++ "(" ++ showSep ", " (map show args) ++ ") = \n\t" ++
331 showDef (x, DConstructor n t a) = "Constructor " ++ show n ++ " " ++ show t
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list