@@ -19,15 +19,17 @@ import Debug.Trace
1919-- paramaterised declarations, etc.
2020
2121data ElabInfo = EInfo { params :: [(Name, PTerm)],
22+ inblock :: Ctxt [Name], -- names in the block, and their params
2223 liftname :: Name -> Name }
2324
24−toplevel = EInfo [] id
25+toplevel = EInfo [] emptyContext id
2526
2627elabType :: ElabInfo -> Name -> PTerm -> Idris ()
27−elabType info n ty
28− = do ctxt <- getContext
28+elabType info n_in ty_in = let ty = piBind (params info) ty_in
29+ n = liftname info n_in in
30+ do ctxt <- getContext
2931 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)
3133 (cty, _) <- tclift $ recheck ctxt [] ty'
3234 logLvl 2 $ "---> " ++ show cty
3335 updateContext (addTyDecl n (normalise ctxt [] cty))
@@ -35,7 +37,7 @@ elabType info n ty
3537elabData :: ElabInfo -> PData -> Idris ()
3638elabData info (PDatadecl n t dcons)
3739 = 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)
3941 (cty, _) <- tclift $ recheck ctxt [] t'
4042 logLvl 2 $ "---> " ++ show cty
4143 updateContext (addTyDecl n cty) -- temporary, to check cons
@@ -46,15 +48,15 @@ elabCon :: ElabInfo -> (Name, PTerm) -> Idris (Name, Type)
4648elabCon info (n, t)
4749 = do ctxt <- getContext
4850 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)
5052-- iLOG $ "Rechecking " ++ show t'
5153 (cty, _) <- tclift $ recheck ctxt [] t'
5254 logLvl 2 $ "---> " ++ show n ++ " : " ++ show cty
5355 return (n, cty)
5456
5557elabClauses :: ElabInfo -> Name -> [PClause] -> Idris ()
56−elabClauses info n cs
57− = do pats <- mapM (elabClause info) cs
58+elabClauses info n_in cs = let n = liftname info n_in in
59+ do pats <- mapM (elabClause info) cs
5860 logLvl 3 (showSep "\n" (map (\ (l,r) ->
5961 show l ++ " = " ++
6062 show r) pats))
@@ -74,34 +76,34 @@ elabVal info tm
7476 = do ctxt <- getContext
7577 logLvl 10 (show tm)
7678 (tm', _) <- tclift $ elaborate ctxt (MN 0 "val") infP
77− (build True (infTerm tm))
79+ (build info True (infTerm tm))
7880 logLvl 3 ("Value: " ++ show tm')
7981 let vtm = getInferTerm tm'
8082 iLOG (show vtm)
8183 tclift $ recheck ctxt [] vtm
8284
8385elabClause :: ElabInfo -> PClause -> Idris (Term, Term)
84−elabClause info (PClause _ lhs rhs whereblock)
86+elabClause info (PClause fname lhs rhs whereblock)
8587 = do ctxt <- getContext
8688 -- Build the LHS as an "Infer", and pull out its type and
8789 -- pattern bindings
8890 (lhs', _) <- tclift $ elaborate ctxt (MN 0 "patLHS") infP
89− (build True (infTerm lhs))
91+ (build info True (infTerm lhs))
9092 let lhs_tm = getInferTerm lhs'
9193 let lhs_ty = getInferType lhs'
9294 logLvl 3 (show lhs_tm)
9395 (clhs, clhsty) <- tclift $ recheck ctxt [] lhs_tm
94− -- TODO: elaborate where block here.
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−
96+ -- Elaborate where block
97+ ist <- getIState
98+ windex <- getName
99+ let winfo = pinfo (pvars ist lhs_tm) whereblock windex
100+ mapM_ (elabDecl' winfo) whereblock
100101 -- Now build the RHS, using the type of the LHS as the goal.
101102 iLOG (showImp True rhs)
103+ ctxt <- getContext -- new context with where block added
102104 (rhs', _) <- tclift $ elaborate ctxt (MN 0 "patRHS") clhsty
103105 (do pbinds lhs_tm
104− build False rhs
106+ build winfo False rhs
105107 psolve lhs_tm
106108 get_term)
107109 logLvl 2 $ "---> " ++ show rhs'
@@ -115,8 +117,21 @@ elabClause info (PClause _ lhs rhs whereblock)
115117 psolve (Bind n (PVar t) sc) = do solve; psolve sc
116118 psolve tm = return ()
117119
118− pvars (Bind n (PVar t) sc) = (n, t) : pvars sc
119− pvars _ = []
120+ pvars ist (Bind n (PVar t) sc) = (n, delab ist t) : pvars ist sc
121+ pvars ist _ = []
122+
123+ pinfo ns ps i
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,
130+ inblock = newb,
131+ liftname = (\n -> case lookupCtxt n newb of
132+ Nothing -> n
133+ _ -> MN i (show n)) . l
134+ }
120135
121136elabDecl :: ElabInfo -> PDecl -> Idris ()
122137elabDecl 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
126141 elabType info n ty
127142elabDecl' info (PData d) = do iLOG $ "Elaborating " ++ show d
128143 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
130145 elabClauses info n ps
146+elabDecl' info (PParams ns ps) = do iLOG $ "Params block " ++ show ns
147+ mapM_ (elabDecl' pinfo) ps
148+ where
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,
154+ inblock = newb }
131155
132156{- not finished...
133157parameterise :: [(Name, Type)] -> (Name -> Name) -> [PDecl] -> Idris [PDecl]
@@ -169,14 +193,14 @@ addNs = undefined
169193-- If building a pattern match, we convert undeclared variables from
170194-- holes to pattern bindings.
171195
172−build :: Bool -> PTerm -> Elab Term
173−build pattern tm = do elab pattern tm
174− get_term
196+build :: ElabInfo -> Bool -> PTerm -> Elab Term
197+build info pattern tm = do elab info pattern tm
198+ get_term
175199
176−elab :: Bool -> PTerm -> Elab ()
177−elab pattern tm = do elab' tm
178− when pattern -- convert remaining holes to pattern vars
179− mkPat
200+elab :: ElabInfo -> Bool -> PTerm -> Elab ()
201+elab info pattern tm = do elab' tm
202+ when pattern -- convert remaining holes to pattern vars
203+ mkPat
180204 where
181205 isph arg = case getTm arg of
182206 Placeholder -> True
@@ -194,10 +218,16 @@ elab pattern tm = do elab' tm
194218 elab' PSet = do fill (RSet 0); solve
195219 elab' (PConstant c) = do fill (RConstant c); solve
196220 elab' (PQuote r) = do fill r; solve
197− elab' (PRef n) | pattern
221+ elab' (PRef n) | pattern && not (inparamBlock n)
198222 = try (do apply (Var n) []; solve)
199223 (patvar n)
200− elab' (PRef n) = do apply (Var n) []; solve
224+ where inparamBlock n = case lookupCtxt n (inblock info) of
225+ Nothing -> False
226+ _ -> True
227+ elab' (PRef n)
228+ | Just ps <- lookupCtxt n (inblock info)
229+ = elab' (PApp (PRef n) [])
230+ | otherwise = do apply (Var n) []; solve
201231 elab' (PLam n Placeholder sc)
202232 = do attack; intro n; elab' sc; solve
203233 elab' (PPi _ n Placeholder sc)
@@ -211,11 +241,15 @@ elab pattern tm = do elab' tm
211241 elab' sc
212242 solve
213243 elab' (PApp (PRef f) args)
214− = try (do ns <- apply (Var f) (map isph args)
215− solve
216− elabArgs ns (map getTm args))
217− (do apply_elab f (map toElab args)
218− solve)
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
248+ = try (do ns <- apply (Var f) (map isph args)
249+ solve
250+ elabArgs ns (map getTm args))
251+ (do apply_elab f (map toElab args)
252+ solve)
219253 elab' (PApp f [arg])
220254 = do simple_app (elab' f) (elab' (getTm arg))
221255 solve