@@ -30,17 +30,21 @@ toplevel = EInfo [] emptyContext id
30elabType :: ElabInfo -> Name -> PTerm -> Idris ()
30elabType :: ElabInfo -> Name -> PTerm -> Idris ()
31elabType info n_in ty_in = let ty = piBind (params info) ty_in
31elabType info n_in ty_in = let ty = piBind (params info) ty_in
32 n = liftname info n_in in
32 n = liftname info n_in in
33− do ctxt <- getContext
34+ ctxt <- getContext
34 logLvl 2 $ "Type " ++ showImp True ty
35 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)
36 (cty, _) <- tclift $ recheck ctxt [] ty'
37 (cty, _) <- tclift $ recheck ctxt [] ty'
37 logLvl 2 $ "---> " ++ show cty
38 logLvl 2 $ "---> " ++ show cty
38− updateContext (addTyDecl n (normalise ctxt [] cty))
39+ let nty = normalise ctxt [] cty
40+ addDeferred ((n, nty):defer)
40elabData :: ElabInfo -> PData -> Idris ()
42elabData :: ElabInfo -> PData -> Idris ()
41elabData info (PDatadecl n t dcons)
43elabData info (PDatadecl n t dcons)
44+ = do checkUndefined n
42− = do ctxt <- getContext
45+ ctxt <- getContext
43− (t', log) <- tclift $ elaborate ctxt n (Set 0) (build info False t)
46+ ((t', defer), log) <- tclift $ elaborate ctxt n (Set 0) (build info False t)
44 (cty, _) <- tclift $ recheck ctxt [] t'
48 (cty, _) <- tclift $ recheck ctxt [] t'
45 logLvl 2 $ "---> " ++ show cty
49 logLvl 2 $ "---> " ++ show cty
46 updateContext (addTyDecl n cty) -- temporary, to check cons
50 updateContext (addTyDecl n cty) -- temporary, to check cons
@@ -49,17 +53,20 @@ elabData info (PDatadecl n t dcons)
50elabCon :: ElabInfo -> (Name, PTerm) -> Idris (Name, Type)
54elabCon :: ElabInfo -> (Name, PTerm) -> Idris (Name, Type)
51elabCon info (n, t)
55elabCon info (n, t)
56+ = do checkUndefined n
52− = do ctxt <- getContext
57+ ctxt <- getContext
53 logLvl 2 $ "Constructor " ++ show n ++ " : " ++ showImp True t
58 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)
55-- logLvl 2 $ "Rechecking " ++ show t'
60-- logLvl 2 $ "Rechecking " ++ show t'
56 (cty, _) <- tclift $ recheck ctxt [] t'
62 (cty, _) <- tclift $ recheck ctxt [] t'
57 logLvl 2 $ "---> " ++ show n ++ " : " ++ show cty
63 logLvl 2 $ "---> " ++ show n ++ " : " ++ show cty
58 return (n, cty)
64 return (n, cty)
60elabClauses :: ElabInfo -> Name -> [PClause] -> Idris ()
66elabClauses :: ElabInfo -> Name -> [PClause] -> Idris ()
61elabClauses info n_in cs = let n = liftname info n_in in
67elabClauses info n_in cs = let n = liftname info n_in in
62− do pats <- mapM (elabClause info) cs
69+ pats <- mapM (elabClause info) cs
63 logLvl 3 (showSep "\n" (map (\ (l,r) ->
70 logLvl 3 (showSep "\n" (map (\ (l,r) ->
64 show l ++ " = " ++
71 show l ++ " = " ++
65 show r) pats))
72 show r) pats))
@@ -78,8 +85,8 @@ elabVal :: ElabInfo -> PTerm -> Idris (Term, Type)
78elabVal info tm
85elabVal info tm
79 = do ctxt <- getContext
86 = do ctxt <- getContext
80 logLvl 10 (show tm)
87 logLvl 10 (show tm)
81− (tm', _) <- tclift $ elaborate ctxt (MN 0 "val") infP
88+ ((tm', defer), _) <- tclift $ elaborate ctxt (MN 0 "val") infP
82− (build info False (infTerm tm))
89+ (build info False (infTerm tm))
83 logLvl 3 ("Value: " ++ show tm')
90 logLvl 3 ("Value: " ++ show tm')
84 let vtm = getInferTerm tm'
91 let vtm = getInferTerm tm'
85 logLvl 2 (show vtm)
92 logLvl 2 (show vtm)
@@ -90,8 +97,8 @@ elabClause info (PClause fname lhs rhs whereblock)
90 = do ctxt <- getContext
97 = do ctxt <- getContext
91 -- Build the LHS as an "Infer", and pull out its type and
98 -- Build the LHS as an "Infer", and pull out its type and
92 -- pattern bindings
99 -- pattern bindings
93− (lhs', _) <- tclift $ elaborate ctxt (MN 0 "patLHS") infP
100+ ((lhs', dlhs), _) <- tclift $ elaborate ctxt (MN 0 "patLHS") infP
94− (build info True (infTerm lhs))
101+ (build info True (infTerm lhs))
95 let lhs_tm = getInferTerm lhs'
102 let lhs_tm = getInferTerm lhs'
96 let lhs_ty = getInferType lhs'
103 let lhs_ty = getInferType lhs'
97 logLvl 3 (show lhs_tm)
104 logLvl 3 (show lhs_tm)
@@ -104,12 +111,15 @@ elabClause info (PClause fname lhs rhs whereblock)
104 -- Now build the RHS, using the type of the LHS as the goal.
111 -- Now build the RHS, using the type of the LHS as the goal.
105 logLvl 2 (showImp True rhs)
112 logLvl 2 (showImp True rhs)
106 ctxt <- getContext -- new context with where block added
113 ctxt <- getContext -- new context with where block added
107− (rhs', _) <- tclift $ elaborate ctxt (MN 0 "patRHS") clhsty
114+ ((rhs', defer), _) <- tclift $ elaborate ctxt (MN 0 "patRHS") clhsty
108− (do pbinds lhs_tm
115+ (do pbinds lhs_tm
109− build winfo False rhs
116+ (_, d) <- build winfo False rhs
110− psolve lhs_tm
117+ psolve lhs_tm
111− get_term)
118+ tt <- get_term
112 logLvl 2 $ "---> " ++ show rhs'
120 logLvl 2 $ "---> " ++ show rhs'
121+ when (not (null defer)) $ iLOG $ "DEFERRED " ++ show defer
113 (crhs, crhsty) <- tclift $ recheck ctxt [] rhs'
123 (crhs, crhsty) <- tclift $ recheck ctxt [] rhs'
114 return (clhs, crhs)
124 return (clhs, crhs)
@@ -165,9 +175,12 @@ elabDecl' info (PParams ns ps) = mapM_ (elabDecl' pinfo) ps
165-- If building a pattern match, we convert undeclared variables from
175-- If building a pattern match, we convert undeclared variables from
166-- holes to pattern bindings.
176-- holes to pattern bindings.
178+-- Also find deferred names in the term and their types
168−build :: ElabInfo -> Bool -> PTerm -> Elab Term
180+build :: ElabInfo -> Bool -> PTerm -> Elab (Term, [(Name, Type)])
169build info pattern tm = do elab info pattern tm
181build info pattern tm = do elab info pattern tm
170− get_term
182+ tt <- get_term
183+ return $ runState (collectDeferred tt) []
172elab :: ElabInfo -> Bool -> PTerm -> Elab ()
185elab :: ElabInfo -> Bool -> PTerm -> Elab ()
173elab info pattern tm = do elab' tm
186elab info pattern tm = do elab' tm
@@ -235,12 +248,28 @@ elab info pattern tm = do elab' tm
235 = do simple_app (elab' f) (elab' (getTm arg))
248 = do simple_app (elab' f) (elab' (getTm arg))
237 elab' Placeholder = fail $ "Can't deal with a placeholder here"
250 elab' Placeholder = fail $ "Can't deal with a placeholder here"
251+ elab' (PMetavar n) = do attack; defer n; solve
238 elab' (PElabError e) = fail e
252 elab' (PElabError e) = fail e
239 elab' x = fail $ "Not implemented " ++ show x
253 elab' x = fail $ "Not implemented " ++ show x
241 elabArgs [] _ = return ()
255 elabArgs [] _ = return ()
242 elabArgs (n:ns) (Placeholder : args) = elabArgs ns args
256 elabArgs (n:ns) (Placeholder : args) = elabArgs ns args
243 elabArgs (n:ns) (t : args) = do focus n; elab' t; elabArgs ns args
257 elabArgs (n:ns) (t : args) = do focus n; elab' t; elabArgs ns args
259+collectDeferred :: Term -> State [(Name, Type)] Term
260+collectDeferred (Bind n (GHole t) app) =
264+collectDeferred (Bind n b t) = do b' <- cdb b
265+ t' <- collectDeferred t
266+ return (Bind n b' t')
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