@@ -4,6 +4,7 @@ module Idris.ElabDecls where
5import Idris.AbsSyntax
5import Idris.AbsSyntax
6import Idris.Error
6import Idris.Error
7+import Idris.Delaborate
8import Core.TT
9import Core.TT
9import Core.Elaborate
10import Core.Elaborate
@@ -14,8 +15,16 @@ import Core.CaseTree
14import Control.Monad
15import Control.Monad
15import Debug.Trace
16import Debug.Trace
18+-- Data to pass to recursively called elaborators; e.g. for where blocks,
19+-- paramaterised declarations, etc.
21+data ElabInfo = EInfo { params :: [(Name, PTerm)],
22+ liftname :: Name -> Name }
24+toplevel = EInfo [] id
17−elabType :: Name -> PTerm -> Idris ()
26+elabType :: ElabInfo -> Name -> PTerm -> Idris ()
18−elabType n ty
27+elabType info n ty
19 = do ctxt <- getContext
28 = do ctxt <- getContext
20 logLvl 2 $ "Type " ++ showImp True ty
29 logLvl 2 $ "Type " ++ showImp True ty
21 (ty', log) <- tclift $ elaborate ctxt n (Set 0) (build False ty)
30 (ty', log) <- tclift $ elaborate ctxt n (Set 0) (build False ty)
@@ -23,18 +32,18 @@ elabType n ty
23 logLvl 2 $ "---> " ++ show cty
32 logLvl 2 $ "---> " ++ show cty
24 updateContext (addTyDecl n (normalise ctxt [] cty))
33 updateContext (addTyDecl n (normalise ctxt [] cty))
26−elabData :: PData -> Idris ()
35+elabData :: ElabInfo -> PData -> Idris ()
27−elabData (PDatadecl n t dcons)
36+elabData info (PDatadecl n t dcons)
28 = do ctxt <- getContext
37 = do ctxt <- getContext
29 (t', log) <- tclift $ elaborate ctxt n (Set 0) (build False t)
38 (t', log) <- tclift $ elaborate ctxt n (Set 0) (build False t)
30 (cty, _) <- tclift $ recheck ctxt [] t'
39 (cty, _) <- tclift $ recheck ctxt [] t'
31 logLvl 2 $ "---> " ++ show cty
40 logLvl 2 $ "---> " ++ show cty
32 updateContext (addTyDecl n cty) -- temporary, to check cons
41 updateContext (addTyDecl n cty) -- temporary, to check cons
33− cons <- mapM elabCon dcons
42+ cons <- mapM (elabCon info) dcons
34 setContext (addDatatype (Data n cty cons) ctxt)
43 setContext (addDatatype (Data n cty cons) ctxt)
36−elabCon :: (Name, PTerm) -> Idris (Name, Type)
45+elabCon :: ElabInfo -> (Name, PTerm) -> Idris (Name, Type)
37−elabCon (n, t)
46+elabCon info (n, t)
38 = do ctxt <- getContext
47 = do ctxt <- getContext
39 iLOG $ "Constructor " ++ show n ++ " : " ++ showImp True t
48 iLOG $ "Constructor " ++ show n ++ " : " ++ showImp True t
40 (t', log) <- tclift $ elaborate ctxt n (Set 0) (build False t)
49 (t', log) <- tclift $ elaborate ctxt n (Set 0) (build False t)
@@ -43,9 +52,9 @@ elabCon (n, t)
43 logLvl 2 $ "---> " ++ show n ++ " : " ++ show cty
52 logLvl 2 $ "---> " ++ show n ++ " : " ++ show cty
44 return (n, cty)
53 return (n, cty)
46−elabClauses :: Name -> [PClause] -> Idris ()
55+elabClauses :: ElabInfo -> Name -> [PClause] -> Idris ()
47−elabClauses n cs
56+elabClauses info n cs
48− = do pats <- mapM elabClause cs
57+ = do pats <- mapM (elabClause info) cs
49 logLvl 3 (showSep "\n" (map (\ (l,r) ->
58 logLvl 3 (showSep "\n" (map (\ (l,r) ->
50 show l ++ " = " ++
59 show l ++ " = " ++
51 show r) pats))
60 show r) pats))
@@ -60,8 +69,8 @@ elabClauses n cs
60 depat (Bind n (PVar t) sc) = depat (instantiate (P Bound n t) sc)
69 depat (Bind n (PVar t) sc) = depat (instantiate (P Bound n t) sc)
61 depat x = x
70 depat x = x
63−elabVal :: PTerm -> Idris (Term, Type)
72+elabVal :: ElabInfo -> PTerm -> Idris (Term, Type)
64−elabVal tm
73+elabVal info tm
65 = do ctxt <- getContext
74 = do ctxt <- getContext
66 logLvl 10 (show tm)
75 logLvl 10 (show tm)
67 (tm', _) <- tclift $ elaborate ctxt (MN 0 "val") infP
76 (tm', _) <- tclift $ elaborate ctxt (MN 0 "val") infP
@@ -71,8 +80,8 @@ elabVal tm
71 iLOG (show vtm)
80 iLOG (show vtm)
72 tclift $ recheck ctxt [] vtm
81 tclift $ recheck ctxt [] vtm
74−elabClause :: PClause -> Idris (Term, Term)
83+elabClause :: ElabInfo -> PClause -> Idris (Term, Term)
75−elabClause (PClause _ lhs rhs whereblock)
84+elabClause info (PClause _ lhs rhs whereblock)
76 = do ctxt <- getContext
85 = do ctxt <- getContext
77 -- Build the LHS as an "Infer", and pull out its type and
86 -- Build the LHS as an "Infer", and pull out its type and
78 -- pattern bindings
87 -- pattern bindings
@@ -83,10 +92,11 @@ elabClause (PClause _ lhs rhs whereblock)
83 logLvl 3 (show lhs_tm)
92 logLvl 3 (show lhs_tm)
84 (clhs, clhsty) <- tclift $ recheck ctxt [] lhs_tm
93 (clhs, clhsty) <- tclift $ recheck ctxt [] lhs_tm
85 -- TODO: elaborate where block here.
94 -- TODO: elaborate where block here.
86− -- probably the way to check the where clauses is to
95+ -- probably the way to check the where clauses is to recursively
87− -- treat them as in a params block.
96+ -- call the elaborator with a list of variables assumed by the
88− -- Will need to know what the params are and add them while
97+ -- where block (i.e. add those variables to all types and clauses
89− -- elaborating.
98+ -- which are elaborated).
90 -- Now build the RHS, using the type of the LHS as the goal.
100 -- Now build the RHS, using the type of the LHS as the goal.
91 iLOG (showImp True rhs)
101 iLOG (showImp True rhs)
92 (rhs', _) <- tclift $ elaborate ctxt (MN 0 "patRHS") clhsty
102 (rhs', _) <- tclift $ elaborate ctxt (MN 0 "patRHS") clhsty
@@ -105,16 +115,53 @@ elabClause (PClause _ lhs rhs whereblock)
105 psolve (Bind n (PVar t) sc) = do solve; psolve sc
115 psolve (Bind n (PVar t) sc) = do solve; psolve sc
106 psolve tm = return ()
116 psolve tm = return ()
118+ pvars (Bind n (PVar t) sc) = (n, t) : pvars sc
108−elabDecl :: PDecl -> Idris ()
121+elabDecl :: ElabInfo -> PDecl -> Idris ()
109−elabDecl d = idrisCatch (elabDecl' d) (\e -> iputStrLn (report e))
122+elabDecl info d = idrisCatch (elabDecl' info d) (\e -> iputStrLn (report e))
124+elabDecl' info (PFix _ _) = return () -- nothing to elaborate
125+elabDecl' info (PTy n ty) = do iLOG $ "Elaborating type decl " ++ show n
127+elabDecl' info (PData d) = do iLOG $ "Elaborating " ++ show d
129+elabDecl' info d@(PClauses n ps) = do iLOG $ "Elaborating " ++ show n
130+ elabClauses info n ps
133+parameterise :: [(Name, Type)] -> (Name -> Name) -> [PDecl] -> Idris [PDecl]
134+parameterise ns hide ds = do
136+ let pns = map (\ (n, t) -> (n, delab i t)) ns
137+ mapM (paramDecl (concatMap declared ds) hide pns) ds
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')
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'))
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
111−elabDecl' (PFix _ _) = return () -- nothing to elaborate
163+addNs :: [Name] -> (Name -> Name) -> [Name] -> PTerm -> PTerm
112−elabDecl' (PTy n ty) = do iLOG $ "Elaborating type decl " ++ show n
164+addNs = undefined
114−elabDecl' (PData d) = do iLOG $ "Elaborating " ++ show d
116−elabDecl' d@(PClauses n ps) = do iLOG $ "Elaborating " ++ show n
119-- Using the elaborator, convert a term in raw syntax to a fully
166-- Using the elaborator, convert a term in raw syntax to a fully
120-- elaborated, typechecked term.
167-- elaborated, typechecked term.