1{-# LANGUAGE PatternGuards #-}
3module IRTS.Defunctionalise(module IRTS.Defunctionalise,
4 module IRTS.Lang) where
8import Idris.Core.CaseTree
14import Control.Monad.State
17 | DApp Bool Name [DExp] -- True = tail call
18 | DLet Name DExp DExp -- name just for pretty printing
19 | DUpdate Name DExp -- eval expression, then update var with it
21 | DC (Maybe LVar) Int Name [DExp]
22 | DCase CaseType DExp [DAlt]
23 | DChkCase DExp [DAlt] -- a case where the type is unknown (for EVAL/APPLY)
25 | DForeign FLang FType String [(FType, DExp)]
27 | DNothing -- erased value, can be compiled to anything since it'll never
32data DAlt = DConCase Int Name [Name] DExp
33 | DConstCase Const DExp
37data DDecl = DFun Name [Name] DExp -- name, arg names, definition
38 | DConstructor Name Int Int -- constructor name, tag, arity
41type DDefs = Ctxt DDecl
43defunctionalise :: Int -> LDefs -> DDefs
44defunctionalise nexttag defs
45 = let all = toAlist defs
46 -- sort newcons so that EVAL and APPLY cons get sequential tags
47 (allD, (enames, anames)) = runState (mapM (addApps defs) all) ([], [])
48 anames' = sort (nub anames)
50 newecons = sortBy conord $ concatMap (toCons enames') (getFn all)
51 newacons = sortBy conord $ concatMap (toConsA anames') (getFn all)
52 eval = mkEval newecons
53 app = mkApply newacons
54 condecls = declare nexttag (newecons ++ newacons) in
55 addAlist (eval : app : condecls ++ allD) emptyContext
56 where conord (n, _, _) (n', _, _) = compare n n'
58getFn :: [(Name, LDecl)] -> [(Name, Int)]
59getFn xs = mapMaybe fnData xs
60 where fnData (n, LFun _ _ args _) = Just (n, length args)
65-- 1 Create a data constructor for each function
66-- 2 Create a data constructor for each underapplication of a function
67-- 3 Convert underapplications to their corresponding constructors
68-- 4 Create an EVAL function which calls the appropriate function for data constructors
69-- created as part of step 1
70-- 5 Create an APPLY function which adds an argument to each underapplication (or calls
71-- APPLY again for an exact application)
72-- 6 Wrap overapplications in chains of APPLY
73-- 7 Wrap unknown applications (i.e. applications of local variables) in chains of APPLY
74-- 8 Add explicit EVAL to case, primitives, and foreign calls
76addApps :: LDefs -> (Name, LDecl) -> State ([Name], [(Name, Int)]) (Name, DDecl)
77addApps defs o@(n, LConstructor _ t a)
78 = return (n, DConstructor n t a)
79addApps defs (n, LFun _ _ args e)
81 return (n, DFun n args e')
83 aa :: [Name] -> LExp -> State ([Name], [(Name, Int)]) DExp
84 aa env (LV (Glob n)) | n `elem` env = return $ DV (Glob n)
85 | otherwise = aa env (LApp False (LV (Glob n)) [])
86 aa env (LApp tc (LV (Glob n)) args)
87 = do args' <- mapM (aa env) args
88 case lookupCtxtExact n defs of
89 Just (LConstructor _ i ar) -> return $ DApp tc n args'
90 Just (LFun _ _ as _) -> let arity = length as in
91 fixApply tc n args' arity
92 Nothing -> return $ chainAPPLY (DV (Glob n)) args'
93 aa env (LLazyApp n args)
94 = do args' <- mapM (aa env) args
95 case lookupCtxtExact n defs of
96 Just (LConstructor _ i ar) -> return $ DApp False n args'
97 Just (LFun _ _ as _) -> let arity = length as in
98 fixLazyApply n args' arity
99 Nothing -> return $ chainAPPLY (DV (Glob n)) args'
100 aa env (LForce (LLazyApp n args)) = aa env (LApp False (LV (Glob n)) args)
101 aa env (LForce e) = liftM eEVAL (aa env e)
102 aa env (LLet n v sc) = liftM2 (DLet n) (aa env v) (aa (n : env) sc)
103 aa env (LCon loc i n args) = liftM (DC loc i n) (mapM (aa env) args)
104 aa env (LProj t@(LV (Glob n)) i)
105 | n `elem` env = do t' <- aa env t
106 return $ DProj (DUpdate n t') i
107 aa env (LProj t i) = do t' <- aa env t
109 aa env (LCase up e alts) = do e' <- aa env e
110 alts' <- mapM (aaAlt env) alts
111 return $ DCase up e' alts'
112 aa env (LConst c) = return $ DConst c
113 aa env (LForeign l t n args) = liftM (DForeign l t n) (mapM (aaF env) args)
114 aa env (LOp LFork args) = liftM (DOp LFork) (mapM (aa env) args)
115 aa env (LOp f args) = do args' <- mapM (aa env) args
117 aa env LNothing = return DNothing
118 aa env (LError e) = return $ DError e
120 aaF env (t, e) = do e' <- aa env e
123 aaAlt env (LConCase i n args e)
124 = liftM (DConCase i n args) (aa (args ++ env) e)
125 aaAlt env (LConstCase c e) = liftM (DConstCase c) (aa env e)
126 aaAlt env (LDefaultCase e) = liftM DDefaultCase (aa env e)
128 fixApply tc n args ar
130 = return $ DApp tc n args
132 = do (ens, ans) <- get
133 let alln = map (\x -> (n, x)) [length args .. ar]
134 put (ens, alln ++ ans)
135 return $ DApp tc (mkUnderCon n (ar - length args)) args
137 = return $ chainAPPLY (DApp tc n (take ar args)) (drop ar args)
139 fixLazyApply n args ar
141 = do (ens, ans) <- get
143 return $ DApp False (mkFnCon n) args
145 = do (ens, ans) <- get
146 let alln = map (\x -> (n, x)) [length args .. ar]
147 put (ens, alln ++ ans)
148 return $ DApp False (mkUnderCon n (ar - length args)) args
150 = return $ chainAPPLY (DApp False n (take ar args)) (drop ar args)
153 chainAPPLY f (a : as) = chainAPPLY (DApp False (sMN 0 "APPLY") [f, a]) as
155 -- if anything in the DExp is projected from, we'll need to evaluate it,
156 -- but we only want to do it once, rather than every time we project.
160 | needsEval x t = DLet x (DV (Glob x)) (preEval xs t)
161 | otherwise = preEval xs t
163 needsEval x (DApp _ _ args) = or (map (needsEval x) args)
164 needsEval x (DC _ _ _ args) = or (map (needsEval x) args)
165 needsEval x (DCase up e alts) = needsEval x e || or (map nec alts)
166 where nec (DConCase _ _ _ e) = needsEval x e
167 nec (DConstCase _ e) = needsEval x e
168 nec (DDefaultCase e) = needsEval x e
169 needsEval x (DChkCase e alts) = needsEval x e || or (map nec alts)
170 where nec (DConCase _ _ _ e) = needsEval x e
171 nec (DConstCase _ e) = needsEval x e
172 nec (DDefaultCase e) = needsEval x e
173 needsEval x (DLet n v e)
174 | x == n = needsEval x v
175 | otherwise = needsEval x v || needsEval x e
176 needsEval x (DForeign _ _ _ args) = or (map (needsEval x) (map snd args))
177 needsEval x (DOp op args) = or (map (needsEval x) args)
178 needsEval x (DProj (DV (Glob x')) _) = x == x'
179 needsEval x _ = False
181eEVAL x = DApp False (sMN 0 "EVAL") [x]
183data EvalApply a = EvalCase (Name -> a)
186-- For a function name, generate a list of
187-- data constuctors, and whether to handle them in EVAL or APPLY
189toCons :: [Name] -> (Name, Int) -> [(Name, Int, EvalApply DAlt)]
194 (DConCase (-1) (mkFnCon n) (take i (genArgs 0))
196 (DApp False n (map (DV . Glob) (take i (genArgs 0))))))))
197 : [] -- mkApplyCase n 0 i
199 where dupdate tlarg x = DUpdate tlarg x
201toConsA :: [(Name, Int)] -> (Name, Int) -> [(Name, Int, EvalApply DAlt)]
203 | Just ar <- lookup n ns
205-- EvalCase (\tlarg ->
206-- (DConCase (-1) (mkFnCon n) (take i (genArgs 0))
208-- (DApp False n (map (DV . Glob) (take i (genArgs 0))))))))
211 where dupdate tlarg x = x
213mkApplyCase fname n ar | n == ar = []
214mkApplyCase fname n ar
215 = let nm = mkUnderCon fname (ar - n) in
216 (nm, n, ApplyCase (DConCase (-1) nm (take n (genArgs 0))
217 (DApp False (mkUnderCon fname (ar - (n + 1)))
218 (map (DV . Glob) (take n (genArgs 0) ++
220 : mkApplyCase fname (n + 1) ar
222mkEval :: [(Name, Int, EvalApply DAlt)] -> (Name, DDecl)
223mkEval xs = (sMN 0 "EVAL", DFun (sMN 0 "EVAL") [sMN 0 "arg"]
224 (mkBigCase (sMN 0 "EVAL") 256 (DV (Glob (sMN 0 "arg")))
225 (mapMaybe evalCase xs ++
226 [DDefaultCase (DV (Glob (sMN 0 "arg")))])))
228 evalCase (n, t, EvalCase x) = Just (x (sMN 0 "arg"))
231mkApply :: [(Name, Int, EvalApply DAlt)] -> (Name, DDecl)
232mkApply xs = (sMN 0 "APPLY", DFun (sMN 0 "APPLY") [sMN 0 "fn", sMN 0 "arg"]
233 (case mapMaybe applyCase xs of
236 mkBigCase (sMN 0 "APPLY") 256
237 (DV (Glob (sMN 0 "fn")))
239 [DDefaultCase DNothing])))
241 applyCase (n, t, ApplyCase x) = Just x
242 applyCase _ = Nothing
245declare :: Int -> [(Name, Int, EvalApply DAlt)] -> [(Name, DDecl)]
246declare t xs = dec' t xs [] where
247 dec' t [] acc = reverse acc
248 dec' t ((n, ar, _) : xs) acc = dec' (t + 1) xs ((n, DConstructor n t ar) : acc)
251genArgs i = sMN i "P_c" : genArgs (i + 1)
253mkFnCon n = sMN 0 ("P_" ++ show n)
255mkUnderCon n missing = sMN missing ("U_" ++ show n)
257instance Show DExp where
258 show e = show' [] e where
259 show' env (DV (Loc i)) = "var " ++ env!!i
260 show' env (DV (Glob n)) = show n
261 show' env (DApp _ e args) = show e ++ "(" ++
262 showSep ", " (map (show' env) args) ++")"
263 show' env (DLet n v e) = "let " ++ show n ++ " = " ++ show' env v ++ " in " ++
264 show' (env ++ [show n]) e
265 show' env (DUpdate n e) = "!update " ++ show n ++ "(" ++ show' env e ++ ")"
266 show' env (DC loc i n args) = atloc loc ++ show n ++ "(" ++ showSep ", " (map (show' env) args) ++ ")"
267 where atloc Nothing = ""
268 atloc (Just l) = "@" ++ show (LV l) ++ ":"
269 show' env (DProj t i) = show t ++ "!" ++ show i
270 show' env (DCase up e alts) = "case" ++ update ++ show' env e ++ " of {\n\t" ++
271 showSep "\n\t| " (map (showAlt env) alts)
272 where update = case up of
275 show' env (DChkCase e alts) = "case' " ++ show' env e ++ " of {\n\t" ++
276 showSep "\n\t| " (map (showAlt env) alts)
277 show' env (DConst c) = show c
278 show' env (DForeign lang ty n args)
279 = "foreign " ++ n ++ "(" ++ showSep ", " (map (show' env) (map snd args)) ++ ")"
280 show' env (DOp f args) = show f ++ "(" ++ showSep ", " (map (show' env) args) ++ ")"
281 show' env (DError str) = "error " ++ show str
282 show' env DNothing = "____"
284 showAlt env (DConCase _ n args e)
285 = show n ++ "(" ++ showSep ", " (map show args) ++ ") => "
287 showAlt env (DConstCase c e) = show c ++ " => " ++ show' env e
288 showAlt env (DDefaultCase e) = "_ => " ++ show' env e
290-- Divide up a large case expression so that each has a maximum of
293mkBigCase cn max arg branches
294 | length branches <= max = DChkCase arg branches
295 | otherwise = -- DChkCase arg branches -- until I think of something...
296 -- divide the branches into groups of at most max (by tag),
297 -- generate a new case and shrink, recursively
298 let bs = sortBy tagOrd branches
299 (all, def) = case (last bs) of
300 DDefaultCase t -> (init all, Just (DDefaultCase t))
302 bss = groupsOf max all
303 cs = map mkCase bss in
304 DChkCase arg branches
306 where mkCase bs = DChkCase arg bs
308 tagOrd (DConCase t _ _ _) (DConCase t' _ _ _) = compare t t'
309 tagOrd (DConstCase c _) (DConstCase c' _) = compare c c'
310 tagOrd (DDefaultCase _) (DDefaultCase _) = EQ
312 tagOrd (DConCase _ _ _ _) (DDefaultCase _) = LT
313 tagOrd (DConCase _ _ _ _) (DConstCase _ _) = LT
314 tagOrd (DConstCase _ _) (DDefaultCase _) = LT
316 tagOrd (DDefaultCase _) (DConCase _ _ _ _) = GT
317 tagOrd (DConstCase _ _) (DConCase _ _ _ _) = GT
318 tagOrd (DDefaultCase _) (DConstCase _ _) = GT
320groupsOf :: Int -> [DAlt] -> [[DAlt]]
322groupsOf x xs = let (batch, rest) = span (tagLT (x + tagHead xs)) xs in
323 batch : groupsOf x rest
324 where tagHead (DConstCase (I i) _ : _) = i
325 tagHead (DConCase t _ _ _ : _) = t
326 tagHead (DDefaultCase _ : _) = -1 -- must be the end
328 tagLT i (DConstCase (I j) _) = i < j
329 tagLT i (DConCase j _ _ _) = i < j
330 tagLT i (DDefaultCase _) = False
332dumpDefuns :: DDefs -> String
333dumpDefuns ds = showSep "\n" $ map showDef (toAlist ds)
334 where showDef (x, DFun fn args exp)
335 = show fn ++ "(" ++ showSep ", " (map show args) ++ ") = \n\t" ++
337 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