1module IRTS.Simplified where
3import IRTS.Defunctionalise
5import Idris.Core.Typecheck
7import Control.Monad.State
11-- Simplified expressions, where functions/constructors can only be applied
15 | SApp Bool Name [LVar]
18 | SCon Int Name [LVar]
20 | SChkCase LVar [SAlt]
23 | SForeign FLang FType String [(FType, LVar)]
25 | SNothing -- erased value, will never be inspected
29data SAlt = SConCase Int Int Name [Name] SExp
30 | SConstCase Const SExp
34data SDecl = SFun Name [Name] Int SExp
37hvar :: State (DDefs, Int) Int
38hvar = do (l, h) <- get
42ldefs :: State (DDefs, Int) DDefs
43ldefs = do (l, h) <- get
46simplify :: Bool -> DExp -> State (DDefs, Int) SExp
47simplify tl (DV (Loc i)) = return (SV (Loc i))
48simplify tl (DV (Glob x))
50 case lookupCtxtExact x ctxt of
51 Just (DConstructor _ t 0) -> return $ SCon t x []
52 _ -> return $ SV (Glob x)
53simplify tl (DApp tc n args) = do args' <- mapM sVar args
54 mkapp (SApp (tl || tc) n) args'
55simplify tl (DForeign lang ty fn args)
56 = do args' <- mapM sVar (map snd args)
57 let fargs = zip (map fst args) args'
58 mkfapp (SForeign lang ty fn) fargs
59simplify tl (DLet n v e) = do v' <- simplify False v
61 return (SLet (Glob n) v' e')
62simplify tl (DUpdate n e) = do e' <- simplify False e
63 return (SUpdate (Glob n) e')
64simplify tl (DC i n args) = do args' <- mapM sVar args
65 mkapp (SCon i n) args'
66simplify tl (DProj t i) = do v <- sVar t
68 (x, Nothing) -> return (SProj x i)
70 return (SLet (Glob x) e (SProj (Glob x) i))
71simplify tl (DCase e alts) = do v <- sVar e
72 alts' <- mapM (sAlt tl) alts
74 (x, Nothing) -> return (SCase x alts')
76 return (SLet (Glob x) e (SCase (Glob x) alts'))
77simplify tl (DChkCase e alts)
79 alts' <- mapM (sAlt tl) alts
81 (x, Nothing) -> return (SChkCase x alts')
83 return (SLet (Glob x) e (SChkCase (Glob x) alts'))
84simplify tl (DConst c) = return (SConst c)
85simplify tl (DOp p args) = do args' <- mapM sVar args
87simplify tl DNothing = return SNothing
88simplify tl (DError str) = return $ SError str
92 case lookupCtxtExact x ctxt of
93 Just (DConstructor _ t 0) -> sVar (DC t x [])
94 _ -> return (Glob x, Nothing)
95sVar (DV x) = return (x, Nothing)
96sVar e = do e' <- simplify False e
98 return (Glob (sMN i "R"), Just e')
100mkapp f args = mkapp' f args [] where
101 mkapp' f [] args = return $ f (reverse args)
102 mkapp' f ((x, Nothing) : xs) args = mkapp' f xs (x : args)
103 mkapp' f ((x, Just e) : xs) args
104 = do sc <- mkapp' f xs (x : args)
107mkfapp f args = mkapp' f args [] where
108 mkapp' f [] args = return $ f (reverse args)
109 mkapp' f ((ty, (x, Nothing)) : xs) args = mkapp' f xs ((ty, x) : args)
110 mkapp' f ((ty, (x, Just e)) : xs) args
111 = do sc <- mkapp' f xs ((ty, x) : args)
114sAlt tl (DConCase i n args e) = do e' <- simplify tl e
115 return (SConCase (-1) i n args e')
116sAlt tl (DConstCase c e) = do e' <- simplify tl e
117 return (SConstCase c e')
118sAlt tl (DDefaultCase e) = do e' <- simplify tl e
119 return (SDefaultCase e')
121checkDefs :: DDefs -> [(Name, DDecl)] -> TC [(Name, SDecl)]
122checkDefs ctxt [] = return []
123checkDefs ctxt (con@(n, DConstructor _ _ _) : xs)
124 = do xs' <- checkDefs ctxt xs
126checkDefs ctxt ((n, DFun n' args exp) : xs)
127 = do let sexp = evalState (simplify True exp) (ctxt, 0)
128 (exp', locs) <- runStateT (scopecheck n ctxt (zip args [0..]) sexp) (length args)
129 xs' <- checkDefs ctxt xs
130 return ((n, SFun n' args ((locs + 1) - length args) exp') : xs')
135scopecheck :: Name -> DDefs -> [(Name, Int)] -> SExp -> StateT Int TC SExp
136scopecheck fn ctxt envTop tm = sc envTop tm where
137 failsc err = fail $ "Codegen error in " ++ show fn ++ ":" ++ err
139 sc env (SV (Glob n)) =
140 case lookup n (reverse env) of -- most recent first
141 Just i -> do lvar i; return (SV (Loc i))
142 Nothing -> case lookupCtxtExact n ctxt of
143 Just (DConstructor _ i ar) ->
145 then return (SCon i n [])
146 else failsc $ "Constructor " ++ show n ++
147 " has arity " ++ show ar
148 Just _ -> return (SV (Glob n))
149 Nothing -> failsc $ "No such variable " ++ show n
150 sc env (SApp tc f args)
151 = do args' <- mapM (scVar env) args
152 case lookupCtxtExact f ctxt of
153 Just (DConstructor n tag ar) ->
154 if True -- (ar == length args)
155 then return $ SCon tag n args'
156 else failsc $ "Constructor " ++ show f ++
157 " has arity " ++ show ar
158 Just _ -> return $ SApp tc f args'
159 Nothing -> failsc $ "No such variable " ++ show f
160 sc env (SForeign l ty f args)
161 = do args' <- mapM (\ (t, a) -> do a' <- scVar env a
163 return $ SForeign l ty f args'
164 sc env (SCon tag f args)
165 = do args' <- mapM (scVar env) args
166 case lookupCtxtExact f ctxt of
167 Just (DConstructor n tag ar) ->
168 if True -- (ar == length args)
169 then return $ SCon tag n args'
170 else failsc $ "Constructor " ++ show f ++
171 " has arity " ++ show ar
172 _ -> failsc $ "No such constructor " ++ show f
174 = do e' <- scVar env e
176 sc env (SCase e alts)
177 = do e' <- scVar env e
178 alts' <- mapM (scalt env) alts
179 return (SCase e' alts')
180 sc env (SChkCase e alts)
181 = do e' <- scVar env e
182 alts' <- mapM (scalt env) alts
183 return (SChkCase e' alts')
184 sc env (SLet (Glob n) v e)
185 = do let env' = env ++ [(n, length env)]
187 n' <- scVar env' (Glob n)
189 return (SLet n' v' e')
190 sc env (SUpdate (Glob n) e)
191 = do -- n already in env
193 n' <- scVar env (Glob n)
194 return (SUpdate n' e')
195 sc env (SOp prim args)
196 = do args' <- mapM (scVar env) args
197 return (SOp prim args')
201 case lookup n (reverse env) of -- most recent first
202 Just i -> do lvar i; return (Loc i)
203 Nothing -> case lookupCtxtExact n ctxt of
204 Just (DConstructor _ i ar) ->
205 failsc $ "can't pass constructor here"
206 Just _ -> return (Glob n)
207 Nothing -> failsc $ "No such variable " ++ show n ++
208 " in " ++ show tm ++ " " ++ show envTop
211 scalt env (SConCase _ i n args e)
212 = do let env' = env ++ zip args [length env..]
213 tag <- case lookupCtxtExact n ctxt of
214 Just (DConstructor _ i ar) ->
215 if True -- (length args == ar)
217 else failsc $ "Constructor " ++ show n ++
218 " has arity " ++ show ar
219 _ -> failsc $ "No constructor " ++ show n
221 return (SConCase (length env) tag n args e')
222 scalt env (SConstCase c e) = do e' <- sc env e
223 return (SConstCase c e')
224 scalt env (SDefaultCase e) = do e' <- sc env e
225 return (SDefaultCase e')
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