@@ -4,6 +4,7 @@ module Idris.ElabDecls where
44
55import Idris.AbsSyntax
66import Idris.Error
7+import Idris.Delaborate
78
89import Core.TT
910import Core.Elaborate
@@ -14,8 +15,16 @@ import Core.CaseTree
1415import Control.Monad
1516import Debug.Trace
1617
17−elabType :: Name -> PTerm -> Idris ()
18−elabType n ty
18+-- Data to pass to recursively called elaborators; e.g. for where blocks,
19+-- paramaterised declarations, etc.
20+
21+data ElabInfo = EInfo { params :: [(Name, PTerm)],
22+ liftname :: Name -> Name }
23+
24+toplevel = EInfo [] id
25+
26+elabType :: ElabInfo -> Name -> PTerm -> Idris ()
27+elabType info n ty
1928 = do ctxt <- getContext
2029 logLvl 2 $ "Type " ++ showImp True ty
2130 (ty', log) <- tclift $ elaborate ctxt n (Set 0) (build False ty)
@@ -23,18 +32,18 @@ elabType n ty
2332 logLvl 2 $ "---> " ++ show cty
2433 updateContext (addTyDecl n (normalise ctxt [] cty))
2534
26−elabData :: PData -> Idris ()
27−elabData (PDatadecl n t dcons)
35+elabData :: ElabInfo -> PData -> Idris ()
36+elabData info (PDatadecl n t dcons)
2837 = do ctxt <- getContext
2938 (t', log) <- tclift $ elaborate ctxt n (Set 0) (build False t)
3039 (cty, _) <- tclift $ recheck ctxt [] t'
3140 logLvl 2 $ "---> " ++ show cty
3241 updateContext (addTyDecl n cty) -- temporary, to check cons
33− cons <- mapM elabCon dcons
42+ cons <- mapM (elabCon info) dcons
3443 setContext (addDatatype (Data n cty cons) ctxt)
3544
36−elabCon :: (Name, PTerm) -> Idris (Name, Type)
37−elabCon (n, t)
45+elabCon :: ElabInfo -> (Name, PTerm) -> Idris (Name, Type)
46+elabCon info (n, t)
3847 = do ctxt <- getContext
3948 iLOG $ "Constructor " ++ show n ++ " : " ++ showImp True t
4049 (t', log) <- tclift $ elaborate ctxt n (Set 0) (build False t)
@@ -43,9 +52,9 @@ elabCon (n, t)
4352 logLvl 2 $ "---> " ++ show n ++ " : " ++ show cty
4453 return (n, cty)
4554
46−elabClauses :: Name -> [PClause] -> Idris ()
47−elabClauses n cs
48− = do pats <- mapM elabClause cs
55+elabClauses :: ElabInfo -> Name -> [PClause] -> Idris ()
56+elabClauses info n cs
57+ = do pats <- mapM (elabClause info) cs
4958 logLvl 3 (showSep "\n" (map (\ (l,r) ->
5059 show l ++ " = " ++
5160 show r) pats))
@@ -60,8 +69,8 @@ elabClauses n cs
6069 depat (Bind n (PVar t) sc) = depat (instantiate (P Bound n t) sc)
6170 depat x = x
6271
63−elabVal :: PTerm -> Idris (Term, Type)
64−elabVal tm
72+elabVal :: ElabInfo -> PTerm -> Idris (Term, Type)
73+elabVal info tm
6574 = do ctxt <- getContext
6675 logLvl 10 (show tm)
6776 (tm', _) <- tclift $ elaborate ctxt (MN 0 "val") infP
@@ -71,8 +80,8 @@ elabVal tm
7180 iLOG (show vtm)
7281 tclift $ recheck ctxt [] vtm
7382
74−elabClause :: PClause -> Idris (Term, Term)
75−elabClause (PClause _ lhs rhs whereblock)
83+elabClause :: ElabInfo -> PClause -> Idris (Term, Term)
84+elabClause info (PClause _ lhs rhs whereblock)
7685 = do ctxt <- getContext
7786 -- Build the LHS as an "Infer", and pull out its type and
7887 -- pattern bindings
@@ -83,10 +92,11 @@ elabClause (PClause _ lhs rhs whereblock)
8392 logLvl 3 (show lhs_tm)
8493 (clhs, clhsty) <- tclift $ recheck ctxt [] lhs_tm
8594 -- TODO: elaborate where block here.
86− -- probably the way to check the where clauses is to
87− -- treat them as in a params block.
88− -- Will need to know what the params are and add them while
89− -- elaborating.
95+ -- probably the way to check the where clauses is to recursively
96+ -- call the elaborator with a list of variables assumed by the
97+ -- where block (i.e. add those variables to all types and clauses
98+ -- which are elaborated).
99+
90100 -- Now build the RHS, using the type of the LHS as the goal.
91101 iLOG (showImp True rhs)
92102 (rhs', _) <- tclift $ elaborate ctxt (MN 0 "patRHS") clhsty
@@ -105,16 +115,53 @@ elabClause (PClause _ lhs rhs whereblock)
105115 psolve (Bind n (PVar t) sc) = do solve; psolve sc
106116 psolve tm = return ()
107117
108−elabDecl :: PDecl -> Idris ()
109−elabDecl d = idrisCatch (elabDecl' d) (\e -> iputStrLn (report e))
118+ pvars (Bind n (PVar t) sc) = (n, t) : pvars sc
119+ pvars _ = []
120+
121+elabDecl :: ElabInfo -> PDecl -> Idris ()
122+elabDecl info d = idrisCatch (elabDecl' info d) (\e -> iputStrLn (report e))
123+
124+elabDecl' info (PFix _ _) = return () -- nothing to elaborate
125+elabDecl' info (PTy n ty) = do iLOG $ "Elaborating type decl " ++ show n
126+ elabType info n ty
127+elabDecl' info (PData d) = do iLOG $ "Elaborating " ++ show d
128+ elabData info d
129+elabDecl' info d@(PClauses n ps) = do iLOG $ "Elaborating " ++ show n
130+ elabClauses info n ps
131+
132+{- not finished...
133+parameterise :: [(Name, Type)] -> (Name -> Name) -> [PDecl] -> Idris [PDecl]
134+parameterise ns hide ds = do
135+ i <- getIState
136+ let pns = map (\ (n, t) -> (n, delab i t)) ns
137+ mapM (paramDecl (concatMap declared ds) hide pns) ds
138+
139+paramDecl :: [Name] -> -- Names defined in this block
140+ (Name -> Name) -> -- what to call the lifted functions
141+ [(Name, PTerm)] -> -- parameters to add
142+ PDecl -> Idris PDecl
143+paramDecl ns newname ps (PTy n ty)
144+ = do let ty' = piBind ps ty
145+ return (PTy (newname n) (addNs ns newname (map fst ps) ty'))
146+paramDecl ns newname ps (PClauses n cs)
147+ = do cs' <- mapM pclause cs
148+ return (PClauses n cs')
149+ where
150+ pclause (PClause n lhs rhs [])
151+ = return $ PClause n [] (addNs ns newname (map fst ps) lhs)
152+ (addNs ns newname (map fst ps) rhs)
153+paramDecl ns newname ps (PData (PDatadecl n ty cons))
154+ = do let ty' = piBind ps ty
155+ cons' <- mapM con cons
156+ return (PData (PDatadecl n ty' cons'))
157+ where
158+ con (n, ty) = do let ty' = piBind ps ty
159+ return (n, addNs ns newname (map fst ps) ty')
160+paramDecl ns newname ps d = return d
161+-}
110162
111−elabDecl' (PFix _ _) = return () -- nothing to elaborate
112−elabDecl' (PTy n ty) = do iLOG $ "Elaborating type decl " ++ show n
113− elabType n ty
114−elabDecl' (PData d) = do iLOG $ "Elaborating " ++ show d
115− elabData d
116−elabDecl' d@(PClauses n ps) = do iLOG $ "Elaborating " ++ show n
117− elabClauses n ps
163+addNs :: [Name] -> (Name -> Name) -> [Name] -> PTerm -> PTerm
164+addNs = undefined
118165
119166-- Using the elaborator, convert a term in raw syntax to a fully
120167-- elaborated, typechecked term.