@@ -1,4 +1,4 @@
1−module IRTS.Simplified where
1+module IRTS.Simplified(simplifyDefs, SDecl(..), SExp(..), SAlt(..)) where
22
33import IRTS.Defunctionalise
44import Idris.Core.TT
@@ -16,7 +16,8 @@ data SExp = SV LVar
1616 | SApp Bool Name [LVar]
1717 | SLet LVar SExp SExp
1818 | SUpdate LVar SExp
19− | SCon Int Name [LVar]
19+ | SCon (Maybe LVar) -- location to reallocate, if available
20+ Int Name [LVar]
2021 | SCase CaseType LVar [SAlt]
2122 | SChkCase LVar [SAlt]
2223 | SProj LVar Int
@@ -49,7 +50,7 @@ simplify tl (DV (Loc i)) = return (SV (Loc i))
4950simplify tl (DV (Glob x))
5051 = do ctxt <- ldefs
5152 case lookupCtxtExact x ctxt of
52− Just (DConstructor _ t 0) -> return $ SCon t x []
53+ Just (DConstructor _ t 0) -> return $ SCon Nothing t x []
5354 _ -> return $ SV (Glob x)
5455simplify tl (DApp tc n args) = do args' <- mapM sVar args
5556 mkapp (SApp (tl || tc) n) args'
@@ -62,8 +63,8 @@ simplify tl (DLet n v e) = do v' <- simplify False v
6263 return (SLet (Glob n) v' e')
6364simplify tl (DUpdate n e) = do e' <- simplify False e
6465 return (SUpdate (Glob n) e')
65−simplify tl (DC i n args) = do args' <- mapM sVar args
66− mkapp (SCon i n) args'
66+simplify tl (DC loc i n args) = do args' <- mapM sVar args
67+ mkapp (SCon loc i n) args'
6768simplify tl (DProj t i) = do v <- sVar t
6869 case v of
6970 (x, Nothing) -> return (SProj x i)
@@ -91,7 +92,7 @@ simplify tl (DError str) = return $ SError str
9192sVar (DV (Glob x))
9293 = do ctxt <- ldefs
9394 case lookupCtxtExact x ctxt of
94− Just (DConstructor _ t 0) -> sVar (DC t x [])
95+ Just (DConstructor _ t 0) -> sVar (DC Nothing t x [])
9596 _ -> return (Glob x, Nothing)
9697sVar (DV x) = return (x, Nothing)
9798sVar e = do e' <- simplify False e
@@ -119,15 +120,15 @@ sAlt tl (DConstCase c e) = do e' <- simplify tl e
119120sAlt tl (DDefaultCase e) = do e' <- simplify tl e
120121 return (SDefaultCase e')
121122
122−checkDefs :: DDefs -> [(Name, DDecl)] -> TC [(Name, SDecl)]
123−checkDefs ctxt [] = return []
124−checkDefs ctxt (con@(n, DConstructor _ _ _) : xs)
125− = do xs' <- checkDefs ctxt xs
123+simplifyDefs :: DDefs -> [(Name, DDecl)] -> TC [(Name, SDecl)]
124+simplifyDefs ctxt [] = return []
125+simplifyDefs ctxt (con@(n, DConstructor _ _ _) : xs)
126+ = do xs' <- simplifyDefs ctxt xs
126127 return xs'
127−checkDefs ctxt ((n, DFun n' args exp) : xs)
128+simplifyDefs ctxt ((n, DFun n' args exp) : xs)
128129 = do let sexp = evalState (simplify True exp) (ctxt, 0)
129130 (exp', locs) <- runStateT (scopecheck n ctxt (zip args [0..]) sexp) (length args)
130− xs' <- checkDefs ctxt xs
131+ xs' <- simplifyDefs ctxt xs
131132 return ((n, SFun n' args ((locs + 1) - length args) exp') : xs')
132133
133134lvar v = do i <- get
@@ -143,7 +144,7 @@ scopecheck fn ctxt envTop tm = sc envTop tm where
143144 Nothing -> case lookupCtxtExact n ctxt of
144145 Just (DConstructor _ i ar) ->
145146 if True -- ar == 0
146− then return (SCon i n [])
147+ then return (SCon Nothing i n [])
147148 else failsc $ "Constructor " ++ show n ++
148149 " has arity " ++ show ar
149150 Just _ -> return (SV (Glob n))
@@ -153,7 +154,7 @@ scopecheck fn ctxt envTop tm = sc envTop tm where
153154 case lookupCtxtExact f ctxt of
154155 Just (DConstructor n tag ar) ->
155156 if True -- (ar == length args)
156− then return $ SCon tag n args'
157+ then return $ SCon Nothing tag n args'
157158 else failsc $ "Constructor " ++ show f ++
158159 " has arity " ++ show ar
159160 Just _ -> return $ SApp tc f args'
@@ -162,12 +163,16 @@ scopecheck fn ctxt envTop tm = sc envTop tm where
162163 = do args' <- mapM (\ (t, a) -> do a' <- scVar env a
163164 return (t, a')) args
164165 return $ SForeign l ty f args'
165− sc env (SCon tag f args)
166− = do args' <- mapM (scVar env) args
166+ sc env (SCon loc tag f args)
167+ = do loc' <- case loc of
168+ Nothing -> return Nothing
169+ Just l -> do l' <- scVar env l
170+ return (Just l')
171+ args' <- mapM (scVar env) args
167172 case lookupCtxtExact f ctxt of
168173 Just (DConstructor n tag ar) ->
169174 if True -- (ar == length args)
170− then return $ SCon tag n args'
175+ then return $ SCon loc' tag n args'
171176 else failsc $ "Constructor " ++ show f ++
172177 " has arity " ++ show ar
173178 _ -> failsc $ "No such constructor " ++ show f