@@ -30,17 +30,21 @@ toplevel = EInfo [] emptyContext id
3030elabType :: ElabInfo -> Name -> PTerm -> Idris ()
3131elabType info n_in ty_in = let ty = piBind (params info) ty_in
3232 n = liftname info n_in in
33− do ctxt <- getContext
33+ do checkUndefined n
34+ ctxt <- getContext
3435 logLvl 2 $ "Type " ++ showImp True ty
35− (ty', log) <- tclift $ elaborate ctxt n (Set 0) (build info False ty)
36+ ((ty', defer), log) <- tclift $ elaborate ctxt n (Set 0) (build info False ty)
3637 (cty, _) <- tclift $ recheck ctxt [] ty'
3738 logLvl 2 $ "---> " ++ show cty
38− updateContext (addTyDecl n (normalise ctxt [] cty))
39+ let nty = normalise ctxt [] cty
40+ addDeferred ((n, nty):defer)
3941
4042elabData :: ElabInfo -> PData -> Idris ()
4143elabData info (PDatadecl n t dcons)
42− = do ctxt <- getContext
43− (t', log) <- tclift $ elaborate ctxt n (Set 0) (build info False t)
44+ = do checkUndefined n
45+ ctxt <- getContext
46+ ((t', defer), log) <- tclift $ elaborate ctxt n (Set 0) (build info False t)
47+ addDeferred defer
4448 (cty, _) <- tclift $ recheck ctxt [] t'
4549 logLvl 2 $ "---> " ++ show cty
4650 updateContext (addTyDecl n cty) -- temporary, to check cons
@@ -49,17 +53,20 @@ elabData info (PDatadecl n t dcons)
4953
5054elabCon :: ElabInfo -> (Name, PTerm) -> Idris (Name, Type)
5155elabCon info (n, t)
52− = do ctxt <- getContext
56+ = do checkUndefined n
57+ ctxt <- getContext
5358 logLvl 2 $ "Constructor " ++ show n ++ " : " ++ showImp True t
54− (t', log) <- tclift $ elaborate ctxt n (Set 0) (build info False t)
59+ ((t', defer), log) <- tclift $ elaborate ctxt n (Set 0) (build info False t)
5560-- logLvl 2 $ "Rechecking " ++ show t'
61+ addDeferred defer
5662 (cty, _) <- tclift $ recheck ctxt [] t'
5763 logLvl 2 $ "---> " ++ show n ++ " : " ++ show cty
5864 return (n, cty)
5965
6066elabClauses :: ElabInfo -> Name -> [PClause] -> Idris ()
6167elabClauses info n_in cs = let n = liftname info n_in in
62− do pats <- mapM (elabClause info) cs
68+ do solveDeferred n
69+ pats <- mapM (elabClause info) cs
6370 logLvl 3 (showSep "\n" (map (\ (l,r) ->
6471 show l ++ " = " ++
6572 show r) pats))
@@ -78,8 +85,8 @@ elabVal :: ElabInfo -> PTerm -> Idris (Term, Type)
7885elabVal info tm
7986 = do ctxt <- getContext
8087 logLvl 10 (show tm)
81− (tm', _) <- tclift $ elaborate ctxt (MN 0 "val") infP
82− (build info False (infTerm tm))
88+ ((tm', defer), _) <- tclift $ elaborate ctxt (MN 0 "val") infP
89+ (build info False (infTerm tm))
8390 logLvl 3 ("Value: " ++ show tm')
8491 let vtm = getInferTerm tm'
8592 logLvl 2 (show vtm)
@@ -90,8 +97,8 @@ elabClause info (PClause fname lhs rhs whereblock)
9097 = do ctxt <- getContext
9198 -- Build the LHS as an "Infer", and pull out its type and
9299 -- pattern bindings
93− (lhs', _) <- tclift $ elaborate ctxt (MN 0 "patLHS") infP
94− (build info True (infTerm lhs))
100+ ((lhs', dlhs), _) <- tclift $ elaborate ctxt (MN 0 "patLHS") infP
101+ (build info True (infTerm lhs))
95102 let lhs_tm = getInferTerm lhs'
96103 let lhs_ty = getInferType lhs'
97104 logLvl 3 (show lhs_tm)
@@ -104,12 +111,15 @@ elabClause info (PClause fname lhs rhs whereblock)
104111 -- Now build the RHS, using the type of the LHS as the goal.
105112 logLvl 2 (showImp True rhs)
106113 ctxt <- getContext -- new context with where block added
107− (rhs', _) <- tclift $ elaborate ctxt (MN 0 "patRHS") clhsty
108− (do pbinds lhs_tm
109− build winfo False rhs
110− psolve lhs_tm
111− get_term)
114+ ((rhs', defer), _) <- tclift $ elaborate ctxt (MN 0 "patRHS") clhsty
115+ (do pbinds lhs_tm
116+ (_, d) <- build winfo False rhs
117+ psolve lhs_tm
118+ tt <- get_term
119+ return (tt, d))
112120 logLvl 2 $ "---> " ++ show rhs'
121+ when (not (null defer)) $ iLOG $ "DEFERRED " ++ show defer
122+ addDeferred defer
113123 (crhs, crhsty) <- tclift $ recheck ctxt [] rhs'
114124 return (clhs, crhs)
115125 where
@@ -165,9 +175,12 @@ elabDecl' info (PParams ns ps) = mapM_ (elabDecl' pinfo) ps
165175-- If building a pattern match, we convert undeclared variables from
166176-- holes to pattern bindings.
167177
168−build :: ElabInfo -> Bool -> PTerm -> Elab Term
178+-- Also find deferred names in the term and their types
179+
180+build :: ElabInfo -> Bool -> PTerm -> Elab (Term, [(Name, Type)])
169181build info pattern tm = do elab info pattern tm
170− get_term
182+ tt <- get_term
183+ return $ runState (collectDeferred tt) []
171184
172185elab :: ElabInfo -> Bool -> PTerm -> Elab ()
173186elab info pattern tm = do elab' tm
@@ -235,12 +248,28 @@ elab info pattern tm = do elab' tm
235248 = do simple_app (elab' f) (elab' (getTm arg))
236249 solve
237250 elab' Placeholder = fail $ "Can't deal with a placeholder here"
251+ elab' (PMetavar n) = do attack; defer n; solve
238252 elab' (PElabError e) = fail e
239253 elab' x = fail $ "Not implemented " ++ show x
240254
241255 elabArgs [] _ = return ()
242256 elabArgs (n:ns) (Placeholder : args) = elabArgs ns args
243257 elabArgs (n:ns) (t : args) = do focus n; elab' t; elabArgs ns args
244−
258+
259+collectDeferred :: Term -> State [(Name, Type)] Term
260+collectDeferred (Bind n (GHole t) app) =
261+ do ds <- get
262+ put ((n, t) : ds)
263+ return app
264+collectDeferred (Bind n b t) = do b' <- cdb b
265+ t' <- collectDeferred t
266+ return (Bind n b' t')
267+ where
268+ cdb (Let t v) = liftM2 Let (collectDeferred t) (collectDeferred v)
269+ cdb (Guess t v) = liftM2 Guess (collectDeferred t) (collectDeferred v)
270+ cdb b = do ty' <- collectDeferred (binderTy b)
271+ return (b { binderTy = ty' })
272+collectDeferred (App f a) = liftM2 App (collectDeferred f) (collectDeferred a)
273+collectDeferred t = return t
245274
246275