@@ -19,15 +19,17 @@ import Debug.Trace
19-- paramaterised declarations, etc.
19-- paramaterised declarations, etc.
21data ElabInfo = EInfo { params :: [(Name, PTerm)],
21data ElabInfo = EInfo { params :: [(Name, PTerm)],
22+ inblock :: Ctxt [Name], -- names in the block, and their params
22 liftname :: Name -> Name }
23 liftname :: Name -> Name }
24−toplevel = EInfo [] id
25+toplevel = EInfo [] emptyContext id
26elabType :: ElabInfo -> Name -> PTerm -> Idris ()
27elabType :: ElabInfo -> Name -> PTerm -> Idris ()
27−elabType info n ty
28+elabType info n_in ty_in = let ty = piBind (params info) ty_in
29+ n = liftname info n_in in
28− = do ctxt <- getContext
30+ do ctxt <- getContext
29 logLvl 2 $ "Type " ++ showImp True ty
31 logLvl 2 $ "Type " ++ showImp True ty
30− (ty', log) <- tclift $ elaborate ctxt n (Set 0) (build False ty)
32+ (ty', log) <- tclift $ elaborate ctxt n (Set 0) (build info False ty)
31 (cty, _) <- tclift $ recheck ctxt [] ty'
33 (cty, _) <- tclift $ recheck ctxt [] ty'
32 logLvl 2 $ "---> " ++ show cty
34 logLvl 2 $ "---> " ++ show cty
33 updateContext (addTyDecl n (normalise ctxt [] cty))
35 updateContext (addTyDecl n (normalise ctxt [] cty))
@@ -35,7 +37,7 @@ elabType info n ty
35elabData :: ElabInfo -> PData -> Idris ()
37elabData :: ElabInfo -> PData -> Idris ()
36elabData info (PDatadecl n t dcons)
38elabData info (PDatadecl n t dcons)
37 = do ctxt <- getContext
39 = do ctxt <- getContext
38− (t', log) <- tclift $ elaborate ctxt n (Set 0) (build False t)
40+ (t', log) <- tclift $ elaborate ctxt n (Set 0) (build info False t)
39 (cty, _) <- tclift $ recheck ctxt [] t'
41 (cty, _) <- tclift $ recheck ctxt [] t'
40 logLvl 2 $ "---> " ++ show cty
42 logLvl 2 $ "---> " ++ show cty
41 updateContext (addTyDecl n cty) -- temporary, to check cons
43 updateContext (addTyDecl n cty) -- temporary, to check cons
@@ -46,15 +48,15 @@ elabCon :: ElabInfo -> (Name, PTerm) -> Idris (Name, Type)
46elabCon info (n, t)
48elabCon info (n, t)
47 = do ctxt <- getContext
49 = do ctxt <- getContext
48 iLOG $ "Constructor " ++ show n ++ " : " ++ showImp True t
50 iLOG $ "Constructor " ++ show n ++ " : " ++ showImp True t
49− (t', log) <- tclift $ elaborate ctxt n (Set 0) (build False t)
51+ (t', log) <- tclift $ elaborate ctxt n (Set 0) (build info False t)
50-- iLOG $ "Rechecking " ++ show t'
52-- iLOG $ "Rechecking " ++ show t'
51 (cty, _) <- tclift $ recheck ctxt [] t'
53 (cty, _) <- tclift $ recheck ctxt [] t'
52 logLvl 2 $ "---> " ++ show n ++ " : " ++ show cty
54 logLvl 2 $ "---> " ++ show n ++ " : " ++ show cty
53 return (n, cty)
55 return (n, cty)
55elabClauses :: ElabInfo -> Name -> [PClause] -> Idris ()
57elabClauses :: ElabInfo -> Name -> [PClause] -> Idris ()
56−elabClauses info n cs
58+elabClauses info n_in cs = let n = liftname info n_in in
57− = do pats <- mapM (elabClause info) cs
59+ do pats <- mapM (elabClause info) cs
58 logLvl 3 (showSep "\n" (map (\ (l,r) ->
60 logLvl 3 (showSep "\n" (map (\ (l,r) ->
59 show l ++ " = " ++
61 show l ++ " = " ++
60 show r) pats))
62 show r) pats))
@@ -74,34 +76,34 @@ elabVal info tm
74 = do ctxt <- getContext
76 = do ctxt <- getContext
75 logLvl 10 (show tm)
77 logLvl 10 (show tm)
76 (tm', _) <- tclift $ elaborate ctxt (MN 0 "val") infP
78 (tm', _) <- tclift $ elaborate ctxt (MN 0 "val") infP
77− (build True (infTerm tm))
79+ (build info True (infTerm tm))
78 logLvl 3 ("Value: " ++ show tm')
80 logLvl 3 ("Value: " ++ show tm')
79 let vtm = getInferTerm tm'
81 let vtm = getInferTerm tm'
80 iLOG (show vtm)
82 iLOG (show vtm)
81 tclift $ recheck ctxt [] vtm
83 tclift $ recheck ctxt [] vtm
83elabClause :: ElabInfo -> PClause -> Idris (Term, Term)
85elabClause :: ElabInfo -> PClause -> Idris (Term, Term)
84−elabClause info (PClause _ lhs rhs whereblock)
86+elabClause info (PClause fname lhs rhs whereblock)
85 = do ctxt <- getContext
87 = do ctxt <- getContext
86 -- Build the LHS as an "Infer", and pull out its type and
88 -- Build the LHS as an "Infer", and pull out its type and
87 -- pattern bindings
89 -- pattern bindings
88 (lhs', _) <- tclift $ elaborate ctxt (MN 0 "patLHS") infP
90 (lhs', _) <- tclift $ elaborate ctxt (MN 0 "patLHS") infP
89− (build True (infTerm lhs))
91+ (build info True (infTerm lhs))
90 let lhs_tm = getInferTerm lhs'
92 let lhs_tm = getInferTerm lhs'
91 let lhs_ty = getInferType lhs'
93 let lhs_ty = getInferType lhs'
92 logLvl 3 (show lhs_tm)
94 logLvl 3 (show lhs_tm)
93 (clhs, clhsty) <- tclift $ recheck ctxt [] lhs_tm
95 (clhs, clhsty) <- tclift $ recheck ctxt [] lhs_tm
94− -- TODO: elaborate where block here.
96+ -- Elaborate where block
95− -- probably the way to check the where clauses is to recursively
97+ ist <- getIState
96− -- call the elaborator with a list of variables assumed by the
98+ windex <- getName
97− -- where block (i.e. add those variables to all types and clauses
99+ let winfo = pinfo (pvars ist lhs_tm) whereblock windex
98− -- which are elaborated).
100+ mapM_ (elabDecl' winfo) whereblock
100 -- Now build the RHS, using the type of the LHS as the goal.
101 -- Now build the RHS, using the type of the LHS as the goal.
101 iLOG (showImp True rhs)
102 iLOG (showImp True rhs)
103+ ctxt <- getContext -- new context with where block added
102 (rhs', _) <- tclift $ elaborate ctxt (MN 0 "patRHS") clhsty
104 (rhs', _) <- tclift $ elaborate ctxt (MN 0 "patRHS") clhsty
103 (do pbinds lhs_tm
105 (do pbinds lhs_tm
104− build False rhs
106+ build winfo False rhs
105 psolve lhs_tm
107 psolve lhs_tm
106 get_term)
108 get_term)
107 logLvl 2 $ "---> " ++ show rhs'
109 logLvl 2 $ "---> " ++ show rhs'
@@ -115,8 +117,21 @@ elabClause info (PClause _ lhs rhs whereblock)
115 psolve (Bind n (PVar t) sc) = do solve; psolve sc
117 psolve (Bind n (PVar t) sc) = do solve; psolve sc
116 psolve tm = return ()
118 psolve tm = return ()
118− pvars (Bind n (PVar t) sc) = (n, t) : pvars sc
120+ pvars ist (Bind n (PVar t) sc) = (n, delab ist t) : pvars ist sc
119− pvars _ = []
121+ pvars ist _ = []
124+ = let ds = concatMap declared ps
125+ newps = params info ++ ns
126+ dsParams = map (\n -> (n, map fst newps)) ds
127+ newb = addAlist dsParams (inblock info)
128+ l = liftname info in
129+ info { params = newps,
131+ liftname = (\n -> case lookupCtxt n newb of
133+ _ -> MN i (show n)) . l
121elabDecl :: ElabInfo -> PDecl -> Idris ()
136elabDecl :: ElabInfo -> PDecl -> Idris ()
122elabDecl info d = idrisCatch (elabDecl' info d) (\e -> iputStrLn (report e))
137elabDecl info d = idrisCatch (elabDecl' info d) (\e -> iputStrLn (report e))
@@ -126,8 +141,17 @@ elabDecl' info (PTy n ty) = do iLOG $ "Elaborating type decl " ++ show n
126 elabType info n ty
141 elabType info n ty
127elabDecl' info (PData d) = do iLOG $ "Elaborating " ++ show d
142elabDecl' info (PData d) = do iLOG $ "Elaborating " ++ show d
128 elabData info d
143 elabData info d
129−elabDecl' info d@(PClauses n ps) = do iLOG $ "Elaborating " ++ show n
144+elabDecl' info d@(PClauses n ps) = do iLOG $ "Elaborating clause " ++ show n
130 elabClauses info n ps
145 elabClauses info n ps
146+elabDecl' info (PParams ns ps) = do iLOG $ "Params block " ++ show ns
147+ mapM_ (elabDecl' pinfo) ps
149+ pinfo = let ds = concatMap declared ps
150+ newps = params info ++ ns
151+ dsParams = map (\n -> (n, map fst newps)) ds
152+ newb = addAlist dsParams (inblock info) in
153+ info { params = newps,
132{- not finished...
156{- not finished...
133parameterise :: [(Name, Type)] -> (Name -> Name) -> [PDecl] -> Idris [PDecl]
157parameterise :: [(Name, Type)] -> (Name -> Name) -> [PDecl] -> Idris [PDecl]
@@ -169,14 +193,14 @@ addNs = undefined
169-- If building a pattern match, we convert undeclared variables from
193-- If building a pattern match, we convert undeclared variables from
170-- holes to pattern bindings.
194-- holes to pattern bindings.
172−build :: Bool -> PTerm -> Elab Term
196+build :: ElabInfo -> Bool -> PTerm -> Elab Term
173−build pattern tm = do elab pattern tm
197+build info pattern tm = do elab info pattern tm
174− get_term
198+ get_term
176−elab :: Bool -> PTerm -> Elab ()
200+elab :: ElabInfo -> Bool -> PTerm -> Elab ()
177−elab pattern tm = do elab' tm
201+elab info pattern tm = do elab' tm
178− when pattern -- convert remaining holes to pattern vars
202+ when pattern -- convert remaining holes to pattern vars
181 isph arg = case getTm arg of
205 isph arg = case getTm arg of
182 Placeholder -> True
206 Placeholder -> True
@@ -194,10 +218,16 @@ elab pattern tm = do elab' tm
194 elab' PSet = do fill (RSet 0); solve
218 elab' PSet = do fill (RSet 0); solve
195 elab' (PConstant c) = do fill (RConstant c); solve
219 elab' (PConstant c) = do fill (RConstant c); solve
196 elab' (PQuote r) = do fill r; solve
220 elab' (PQuote r) = do fill r; solve
197− elab' (PRef n) | pattern
221+ elab' (PRef n) | pattern && not (inparamBlock n)
198 = try (do apply (Var n) []; solve)
222 = try (do apply (Var n) []; solve)
199 (patvar n)
223 (patvar n)
224+ where inparamBlock n = case lookupCtxt n (inblock info) of
228+ | Just ps <- lookupCtxt n (inblock info)
229+ = elab' (PApp (PRef n) [])
200− elab' (PRef n) = do apply (Var n) []; solve
230+ | otherwise = do apply (Var n) []; solve
201 elab' (PLam n Placeholder sc)
231 elab' (PLam n Placeholder sc)
202 = do attack; intro n; elab' sc; solve
232 = do attack; intro n; elab' sc; solve
203 elab' (PPi _ n Placeholder sc)
233 elab' (PPi _ n Placeholder sc)
@@ -211,11 +241,15 @@ elab pattern tm = do elab' tm
213 elab' (PApp (PRef f) args)
243 elab' (PApp (PRef f) args)
244+ | Just ps <- lookupCtxt f (inblock info)
245+ = elabApp (liftname info f) (map (PExp . PRef) ps ++ args)
246+ | otherwise = elabApp f args
247+ where elabApp f args
214− = try (do ns <- apply (Var f) (map isph args)
248+ = try (do ns <- apply (Var f) (map isph args)
216− elabArgs ns (map getTm args))
250+ elabArgs ns (map getTm args))
217− (do apply_elab f (map toElab args)
251+ (do apply_elab f (map toElab args)
219 elab' (PApp f [arg])
253 elab' (PApp f [arg])
220 = do simple_app (elab' f) (elab' (getTm arg))
254 = do simple_app (elab' f) (elab' (getTm arg))