@@ -1,4 +1,4 @@
1−module IRTS.Simplified where
1+module IRTS.Simplified(simplifyDefs, SDecl(..), SExp(..), SAlt(..)) where
3import IRTS.Defunctionalise
3import IRTS.Defunctionalise
4import Idris.Core.TT
4import Idris.Core.TT
@@ -16,7 +16,8 @@ data SExp = SV LVar
16 | SApp Bool Name [LVar]
16 | SApp Bool Name [LVar]
17 | SLet LVar SExp SExp
17 | SLet LVar SExp SExp
18 | SUpdate LVar SExp
18 | SUpdate LVar SExp
19+ | SCon (Maybe LVar) -- location to reallocate, if available
19− | SCon Int Name [LVar]
20+ Int Name [LVar]
20 | SCase CaseType LVar [SAlt]
21 | SCase CaseType LVar [SAlt]
21 | SChkCase LVar [SAlt]
22 | SChkCase LVar [SAlt]
22 | SProj LVar Int
23 | SProj LVar Int
@@ -49,7 +50,7 @@ simplify tl (DV (Loc i)) = return (SV (Loc i))
49simplify tl (DV (Glob x))
50simplify tl (DV (Glob x))
50 = do ctxt <- ldefs
51 = do ctxt <- ldefs
51 case lookupCtxtExact x ctxt of
52 case lookupCtxtExact x ctxt of
52− Just (DConstructor _ t 0) -> return $ SCon t x []
53+ Just (DConstructor _ t 0) -> return $ SCon Nothing t x []
53 _ -> return $ SV (Glob x)
54 _ -> return $ SV (Glob x)
54simplify tl (DApp tc n args) = do args' <- mapM sVar args
55simplify tl (DApp tc n args) = do args' <- mapM sVar args
55 mkapp (SApp (tl || tc) n) args'
56 mkapp (SApp (tl || tc) n) args'
@@ -62,8 +63,8 @@ simplify tl (DLet n v e) = do v' <- simplify False v
62 return (SLet (Glob n) v' e')
63 return (SLet (Glob n) v' e')
63simplify tl (DUpdate n e) = do e' <- simplify False e
64simplify tl (DUpdate n e) = do e' <- simplify False e
64 return (SUpdate (Glob n) e')
65 return (SUpdate (Glob n) e')
65−simplify tl (DC i n args) = do args' <- mapM sVar args
66+simplify tl (DC loc i n args) = do args' <- mapM sVar args
66− mkapp (SCon i n) args'
67+ mkapp (SCon loc i n) args'
67simplify tl (DProj t i) = do v <- sVar t
68simplify tl (DProj t i) = do v <- sVar t
69 (x, Nothing) -> return (SProj x i)
70 (x, Nothing) -> return (SProj x i)
@@ -91,7 +92,7 @@ simplify tl (DError str) = return $ SError str
91sVar (DV (Glob x))
92sVar (DV (Glob x))
92 = do ctxt <- ldefs
93 = do ctxt <- ldefs
93 case lookupCtxtExact x ctxt of
94 case lookupCtxtExact x ctxt of
94− Just (DConstructor _ t 0) -> sVar (DC t x [])
95+ Just (DConstructor _ t 0) -> sVar (DC Nothing t x [])
95 _ -> return (Glob x, Nothing)
96 _ -> return (Glob x, Nothing)
96sVar (DV x) = return (x, Nothing)
97sVar (DV x) = return (x, Nothing)
97sVar e = do e' <- simplify False e
98sVar e = do e' <- simplify False e
@@ -119,15 +120,15 @@ sAlt tl (DConstCase c e) = do e' <- simplify tl e
119sAlt tl (DDefaultCase e) = do e' <- simplify tl e
120sAlt tl (DDefaultCase e) = do e' <- simplify tl e
120 return (SDefaultCase e')
121 return (SDefaultCase e')
122−checkDefs :: DDefs -> [(Name, DDecl)] -> TC [(Name, SDecl)]
123+simplifyDefs :: DDefs -> [(Name, DDecl)] -> TC [(Name, SDecl)]
123−checkDefs ctxt [] = return []
124+simplifyDefs ctxt [] = return []
124−checkDefs ctxt (con@(n, DConstructor _ _ _) : xs)
125+simplifyDefs ctxt (con@(n, DConstructor _ _ _) : xs)
125− = do xs' <- checkDefs ctxt xs
126+ = do xs' <- simplifyDefs ctxt xs
126 return xs'
127 return xs'
127−checkDefs ctxt ((n, DFun n' args exp) : xs)
128+simplifyDefs ctxt ((n, DFun n' args exp) : xs)
128 = do let sexp = evalState (simplify True exp) (ctxt, 0)
129 = do let sexp = evalState (simplify True exp) (ctxt, 0)
129 (exp', locs) <- runStateT (scopecheck n ctxt (zip args [0..]) sexp) (length args)
130 (exp', locs) <- runStateT (scopecheck n ctxt (zip args [0..]) sexp) (length args)
130− xs' <- checkDefs ctxt xs
131+ xs' <- simplifyDefs ctxt xs
131 return ((n, SFun n' args ((locs + 1) - length args) exp') : xs')
132 return ((n, SFun n' args ((locs + 1) - length args) exp') : xs')
133lvar v = do i <- get
134lvar v = do i <- get
@@ -143,7 +144,7 @@ scopecheck fn ctxt envTop tm = sc envTop tm where
143 Nothing -> case lookupCtxtExact n ctxt of
144 Nothing -> case lookupCtxtExact n ctxt of
144 Just (DConstructor _ i ar) ->
145 Just (DConstructor _ i ar) ->
145 if True -- ar == 0
146 if True -- ar == 0
146− then return (SCon i n [])
147+ then return (SCon Nothing i n [])
147 else failsc $ "Constructor " ++ show n ++
148 else failsc $ "Constructor " ++ show n ++
148 " has arity " ++ show ar
149 " has arity " ++ show ar
149 Just _ -> return (SV (Glob n))
150 Just _ -> return (SV (Glob n))
@@ -153,7 +154,7 @@ scopecheck fn ctxt envTop tm = sc envTop tm where
153 case lookupCtxtExact f ctxt of
154 case lookupCtxtExact f ctxt of
154 Just (DConstructor n tag ar) ->
155 Just (DConstructor n tag ar) ->
155 if True -- (ar == length args)
156 if True -- (ar == length args)
156− then return $ SCon tag n args'
157+ then return $ SCon Nothing tag n args'
157 else failsc $ "Constructor " ++ show f ++
158 else failsc $ "Constructor " ++ show f ++
158 " has arity " ++ show ar
159 " has arity " ++ show ar
159 Just _ -> return $ SApp tc f args'
160 Just _ -> return $ SApp tc f args'
@@ -162,12 +163,16 @@ scopecheck fn ctxt envTop tm = sc envTop tm where
162 = do args' <- mapM (\ (t, a) -> do a' <- scVar env a
163 = do args' <- mapM (\ (t, a) -> do a' <- scVar env a
163 return (t, a')) args
164 return (t, a')) args
164 return $ SForeign l ty f args'
165 return $ SForeign l ty f args'
165− sc env (SCon tag f 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
166− = do args' <- mapM (scVar env) args
171+ args' <- mapM (scVar env) args
167 case lookupCtxtExact f ctxt of
172 case lookupCtxtExact f ctxt of
168 Just (DConstructor n tag ar) ->
173 Just (DConstructor n tag ar) ->
169 if True -- (ar == length args)
174 if True -- (ar == length args)
170− then return $ SCon tag n args'
175+ then return $ SCon loc' tag n args'
171 else failsc $ "Constructor " ++ show f ++
176 else failsc $ "Constructor " ++ show f ++
172 " has arity " ++ show ar
177 " has arity " ++ show ar
173 _ -> failsc $ "No such constructor " ++ show f
178 _ -> failsc $ "No such constructor " ++ show f