@@ -31,43 +31,52 @@ checkDef ns = do ctxt <- getContext
3131 mapM (\(n, t) -> do (t', _) <- tclift $ recheck ctxt [] t
3232 return (n, t')) ns
3333
34−elabType :: ElabInfo -> FC -> Name -> PTerm -> Idris ()
35−elabType info fc n_in ty_in = let ty = piBind (params info) ty_in
36− n = liftname info n_in in
34+elabType :: ElabInfo -> SyntaxInfo -> FC -> Name -> PTerm -> Idris ()
35+elabType info syn fc n ty' = {- let ty' = piBind (params info) ty_in
36+ n = liftname info n_in in -}
3737 do checkUndefined fc n
3838 ctxt <- getContext
39− logLvl 2 $ "Type " ++ showImp True ty
39+ i <- get
40+ ty' <- implicit syn n ty'
41+ let ty = addImpl i ty'
42+ logLvl 2 $ show n ++ " type " ++ showImp True ty
4043 ((ty', defer), log) <- tclift $ elaborate ctxt n (Set 0)
41− (erun fc (build info False ty))
44+ (erun fc (build i info False ty))
4245 (cty, _) <- tclift $ recheck ctxt [] ty'
4346 logLvl 2 $ "---> " ++ show cty
4447 let nty = normalise ctxt [] cty
4548 ds <- checkDef ((n, nty):defer)
4649 addDeferred ds
4750
48−elabData :: ElabInfo -> FC -> PData -> Idris ()
49−elabData info fc (PDatadecl n t dcons)
51+elabData :: ElabInfo -> SyntaxInfo -> FC -> PData -> Idris ()
52+elabData info syn fc (PDatadecl n t_in dcons)
5053 = do iLOG (show fc)
5154 checkUndefined fc n
5255 ctxt <- getContext
56+ i <- get
57+ t_in <- implicit syn n t_in
58+ let t = addImpl i t_in
5359 ((t', defer), log) <- tclift $ elaborate ctxt n (Set 0)
54− (erun fc (build info False t))
60+ (erun fc (build i info False t))
5561 def' <- checkDef defer
5662 addDeferred def'
5763 (cty, _) <- tclift $ recheck ctxt [] t'
5864 logLvl 2 $ "---> " ++ show cty
5965 updateContext (addTyDecl n cty) -- temporary, to check cons
60− cons <- mapM (elabCon info) dcons
66+ cons <- mapM (elabCon info syn) dcons
6167 ttag <- getName
6268 setContext (addDatatype (Data n ttag cty cons) ctxt)
6369
64−elabCon :: ElabInfo -> (Name, PTerm, FC) -> Idris (Name, Type)
65−elabCon info (n, t, fc)
70+elabCon :: ElabInfo -> SyntaxInfo -> (Name, PTerm, FC) -> Idris (Name, Type)
71+elabCon info syn (n, t_in, fc)
6672 = do checkUndefined fc n
6773 ctxt <- getContext
74+ i <- get
75+ t_in <- implicit syn n t_in
76+ let t = addImpl i t_in
6877 logLvl 2 $ show fc ++ ":Constructor " ++ show n ++ " : " ++ showImp True t
6978 ((t', defer), log) <- tclift $ elaborate ctxt n (Set 0)
70− (erun fc (build info False t))
79+ (erun fc (build i info False t))
7180 logLvl 2 $ "Rechecking " ++ show t'
7281 def' <- checkDef defer
7382 addDeferred def'
@@ -96,23 +105,28 @@ elabClauses info fc n_in cs = let n = liftname info n_in in
96105 depat x = x
97106
98107elabVal :: ElabInfo -> PTerm -> Idris (Term, Type)
99−elabVal info tm
108+elabVal info tm_in
100109 = do ctxt <- getContext
110+ i <- get
111+ let tm = addImpl i tm_in
101112 logLvl 10 (showImp True tm)
102113 ((tm', defer), _) <- tclift $ elaborate ctxt (MN 0 "val") infP
103− (build info True (infTerm tm))
114+ (build i info True (infTerm tm))
104115 logLvl 3 ("Value: " ++ show tm')
105116 let vtm = getInferTerm tm'
106117 logLvl 2 (show vtm)
107118 tclift $ recheck ctxt [] vtm
108119
109120elabClause :: ElabInfo -> FC -> PClause -> Idris (Term, Term)
110−elabClause info fc (PClause fname lhs withs rhs whereblock)
121+elabClause info fc (PClause fname lhs_in withs rhs_in whereblock)
111122 = do ctxt <- getContext
112123 -- Build the LHS as an "Infer", and pull out its type and
113124 -- pattern bindings
125+ i <- get
126+ let lhs = addImpl i lhs_in
127+ logLvl 5 ("LHS: " ++ showImp True lhs)
114128 ((lhs', dlhs), _) <- tclift $ elaborate ctxt (MN 0 "patLHS") infP
115− (erun fc (build info True (infTerm lhs)))
129+ (erun fc (build i info True (infTerm lhs)))
116130 let lhs_tm = getInferTerm lhs'
117131 let lhs_ty = getInferType lhs'
118132 logLvl 3 (show lhs_tm)
@@ -122,14 +136,22 @@ elabClause info fc (PClause fname lhs withs rhs whereblock)
122136 ist <- getIState
123137 windex <- getName
124138 let winfo = pinfo (pvars ist lhs_tm) whereblock windex
125− mapM_ (elabDecl' winfo) whereblock
139+ let decls = concatMap declared whereblock
140+ let newargs = pvars ist lhs_tm
141+ let wb = map (expandParamsD decorate newargs decls) whereblock
142+ logLvl 5 $ show wb
143+ mapM_ (elabDecl' info) wb
126144 -- Now build the RHS, using the type of the LHS as the goal.
145+ i <- get -- new implicits from where block
146+ logLvl 5 (showImp True (expandParams decorate newargs decls rhs_in))
147+ let rhs = addImpl i (expandParams decorate newargs decls rhs_in)
148+ -- TODO: but don't do names in scope
127149 logLvl 2 (showImp True rhs)
128150 ctxt <- getContext -- new context with where block added
129151 ((rhs', defer), _) <-
130152 tclift $ elaborate ctxt (MN 0 "patRHS") clhsty
131153 (do pbinds lhs_tm
132− (_, _) <- erun fc (build winfo False rhs)
154+ (_, _) <- erun fc (build i info False rhs)
133155 psolve lhs_tm
134156 tt <- get_term
135157 return $ runState (collectDeferred tt) [])
@@ -141,6 +163,7 @@ elabClause info fc (PClause fname lhs withs rhs whereblock)
141163 (crhs, crhsty) <- tclift $ recheck ctxt [] rhs'
142164 return (clhs, crhs)
143165 where
166+ decorate x = UN [show fname ++ "#" ++ show x]
144167 pinfo ns ps i
145168 = let ds = concatMap declared ps
146169 newps = params info ++ ns
@@ -149,32 +172,36 @@ elabClause info fc (PClause fname lhs withs rhs whereblock)
149172 l = liftname info in
150173 info { params = newps,
151174 inblock = newb,
152− liftname = (\n -> case lookupCtxt n newb of
153− Nothing -> n
154− _ -> MN i (show n)) . l
175+ liftname = id -- (\n -> case lookupCtxt n newb of
176+ -- Nothing -> n
177+ -- _ -> MN i (show n)) . l
155178 }
156179
157−elabClause info fc (PWith fname lhs withs wval withblock)
180+elabClause info fc (PWith fname lhs_in withs wval_in withblock)
158181 = do ctxt <- getContext
159182 -- Build the LHS as an "Infer", and pull out its type and
160183 -- pattern bindings
184+ i <- get
185+ let lhs = addImpl i lhs_in
186+ logLvl 5 ("LHS: " ++ showImp True lhs)
161187 ((lhs', dlhs), _) <- tclift $ elaborate ctxt (MN 0 "patLHS") infP
162− (erun fc (build info True (infTerm lhs)))
188+ (erun fc (build i info True (infTerm lhs)))
163189 let lhs_tm = getInferTerm lhs'
164190 let lhs_ty = getInferType lhs'
165191 let ret_ty = getRetTy lhs_ty
166192 logLvl 3 (show lhs_tm)
167193 (clhs, clhsty) <- tclift $ recheck ctxt [] lhs_tm
168194 logLvl 5 ("Checked " ++ show clhs)
169− logLvl 5 ("Checking " ++ show wval)
170195 let bargs = getPBtys lhs_tm
196+ let wval = addImpl i wval_in
197+ logLvl 5 ("Checking " ++ showImp True wval)
171198 -- Elaborate wval in this context
172199 ((wval', defer), _) <-
173200 tclift $ elaborate ctxt (MN 0 "withRHS")
174201 (bindTyArgs PVTy bargs infP)
175202 (do pbinds lhs_tm
176203 -- TODO: may want where here - see winfo abpve
177− (_', d) <- erun fc (build info False (infTerm wval))
204+ (_', d) <- erun fc (build i info False (infTerm wval))
178205 psolve lhs_tm
179206 tt <- get_term
180207 return (tt, d))
@@ -188,6 +215,8 @@ elabClause info fc (PWith fname lhs withs wval withblock)
188215 let wtype = bindTyArgs Pi (bargs ++ [(MN 0 "warg", getRetTy cwvalty)]) ret_ty
189216 logLvl 3 ("New function type " ++ show wtype)
190217 let wname = MN windex (show fname)
218+ let imps = getImps wtype -- add to implicits context
219+ put (i { idris_implicits = addDef wname imps (idris_implicits i) })
191220 def' <- checkDef [(wname, wtype)]
192221 addDeferred def'
193222
@@ -203,10 +232,11 @@ elabClause info fc (PWith fname lhs withs wval withblock)
203232 [pexp wval])
204233 logLvl 3 ("New RHS " ++ show rhs)
205234 ctxt <- getContext -- New context with block added
235+ i <- get
206236 ((rhs', defer), _) <-
207237 tclift $ elaborate ctxt (MN 0 "wpatRHS") clhsty
208238 (do pbinds lhs_tm
209− (_, d) <- erun fc (build info False rhs)
239+ (_, d) <- erun fc (build i info False rhs)
210240 psolve lhs_tm
211241 tt <- get_term
212242 return (tt, d))
@@ -215,21 +245,30 @@ elabClause info fc (PWith fname lhs withs wval withblock)
215245 (crhs, crhsty) <- tclift $ recheck ctxt [] rhs'
216246 return (clhs, crhs)
217247 where
248+ getImps (Bind n (Pi _) t) = pexp Placeholder : getImps t
249+ getImps _ = []
250+
218251 mkAuxC wname lhs ns (PClauses fc n cs)
219252 | n == fname = do cs' <- mapM (mkAux wname lhs ns) cs
220253 return $ PClauses fc wname cs'
221254 | otherwise = fail $ "with clause uses wrong function name " ++ show n
222255 mkAuxC wname lhs ns d = return $ d
223256
224− mkAux wname toplhs ns (PClause n tm (w:ws) rhs wheres)
225− = do logLvl 2 ("Matching " ++ show tm ++ " against " ++ show toplhs)
257+ mkAux wname toplhs ns (PClause n tm_in (w:ws) rhs wheres)
258+ = do i <- get
259+ let tm = addImpl i tm_in
260+ logLvl 2 ("Matching " ++ showImp True tm ++ " against " ++
261+ showImp True toplhs)
226262 case matchClause toplhs tm of
227263 Nothing -> fail "with clause does not match top level"
228264 Just mvars -> do logLvl 3 ("Match vars : " ++ show mvars)
229265 lhs <- updateLHS n wname mvars ns tm w
230266 return $ PClause wname lhs ws rhs wheres
231− mkAux wname toplhs ns (PWith n tm (w:ws) wval wheres)
232− = do logLvl 2 ("Matching " ++ show tm ++ " against " ++ show toplhs)
267+ mkAux wname toplhs ns (PWith n tm_in (w:ws) wval wheres)
268+ = do i <- get
269+ let tm = addImpl i tm_in
270+ logLvl 2 ("Matching " ++ showImp True tm ++ " against " ++
271+ showImp True toplhs)
233272 case matchClause toplhs tm of
234273 Nothing -> fail "with clause does not match top level"
235274 Just mvars -> do lhs <- updateLHS n wname mvars ns tm w
@@ -268,10 +307,10 @@ elabDecl info d = idrisCatch (elabDecl' info d) (\e -> iputStrLn (report e))
268307
269308elabDecl' info (PFix _ _ _) = return () -- nothing to elaborate
270309elabDecl' info (PSyntax _ p) = return () -- nothing to elaborate
271−elabDecl' info (PTy f n ty) = do iLOG $ "Elaborating type decl " ++ show n
272− elabType info f n ty
273−elabDecl' info (PData f d) = do iLOG $ "Elaborating " ++ show (d_name d)
274− elabData info f d
310+elabDecl' info (PTy s f n ty) = do iLOG $ "Elaborating type decl " ++ show n
311+ elabType info s f n ty
312+elabDecl' info (PData s f d) = do iLOG $ "Elaborating " ++ show (d_name d)
313+ elabData info s f d
275314elabDecl' info d@(PClauses f n ps) = do iLOG $ "Elaborating clause " ++ show n
276315 elabClauses info f n ps
277316elabDecl' info (PParams f ns ps) = mapM_ (elabDecl' pinfo) ps
@@ -293,15 +332,17 @@ elabDecl' info (PParams f ns ps) = mapM_ (elabDecl' pinfo) ps
293332
294333-- Also find deferred names in the term and their types
295334
296−build :: ElabInfo -> Bool -> PTerm -> Elab (Term, [(Name, Type)])
297−build info pattern tm = do elab info pattern tm
298− tt <- get_term
299− return $ runState (collectDeferred tt) []
300−
301−elab :: ElabInfo -> Bool -> PTerm -> Elab ()
302−elab info pattern tm = do elab' tm
303− when pattern -- convert remaining holes to pattern vars
304− mkPat
335+build :: IState -> ElabInfo -> Bool -> PTerm -> Elab (Term, [(Name, Type)])
336+build ist info pattern tm
337+ = do elab ist info pattern tm
338+ tt <- get_term
339+ return $ runState (collectDeferred tt) []
340+
341+elab :: IState -> ElabInfo -> Bool -> PTerm -> Elab ()
342+elab ist info pattern tm
343+ = do elabE tm
344+ when pattern -- convert remaining holes to pattern vars
345+ mkPat
305346 where
306347 isph arg = case getTm arg of
307348 Placeholder -> True
@@ -309,13 +350,39 @@ elab info pattern tm = do elab' tm
309350
310351 toElab arg = case getTm arg of
311352 Placeholder -> Nothing
312− v -> Just (elab' v)
353+ v -> Just (elabE v)
313354
314355 mkPat = do hs <- get_holes
315356 case hs of
316357 (h: hs) -> do patvar h; mkPat
317358 [] -> return ()
318359
360+ -- Expand implict arguments here, then move on to main elaborator
361+-- elabE (PRef fc n)
362+-- | Just ps <- lookupCtxt n (inblock info)
363+-- = -- let exp = (PApp fc (PRef fc n) (map (pexp . (PRef fc)) ps))
364+-- elabE (PApp fc (PRef fc n) [])
365+-- elabE f@(PApp fc fn args) = trace ("Elaborating " ++ show f) $ elab' f
366+-- elabE (PRef fc f)
367+-- = do l <- local f
368+-- elabN f l
369+-- where elabN f False = let afn = aiFn ist fc f [] in
370+-- -- trace ("Ref expanded to " ++ showImp True afn) $
371+-- elab' afn
372+-- elabN f True = elab' (PRef fc f)
373+-- elabE (PApp fc fn@(PRef _ f) args)
374+-- = do l <- local f
375+-- elabApp f args l -- no implicits
376+-- where elabApp f args False = let afn = aiFn ist fc f args in
377+-- -- trace ("Expanded to " ++ showImp True afn) $
378+-- elab' afn
379+-- elabApp f args True = elab' (PApp fc fn args)
380+-- elabE (PApp fc f as) = elab' (mkPApp fc 1 f as)
381+ elabE t = elab' t
382+
383+ local f = do e <- get_env
384+ return (f `elem` map fst e)
385+
319386 elab' PSet = do fill (RSet 0); solve
320387 elab' (PConstant c) = do apply (RConstant c) []; solve
321388 elab' (PQuote r) = do fill r; solve
@@ -327,9 +394,9 @@ elab info pattern tm = do elab' tm
327394 elab' (PEq fc l r) = elab' (PApp fc (PRef fc eqTy) [pimp (MN 0 "a") Placeholder,
328395 pimp (MN 0 "b") Placeholder,
329396 pexp l, pexp r])
330− elab' (PPair fc l r) = try (elab' (PApp fc (PRef fc pairTy)
397+ elab' (PPair fc l r) = try (elabE (PApp fc (PRef fc pairTy)
331398 [pexp l,pexp r]))
332− (elab' (PApp fc (PRef fc pairCon)
399+ (elabE (PApp fc (PRef fc pairCon)
333400 [pimp (MN 0 "A") Placeholder,
334401 pimp (MN 0 "B") Placeholder,
335402 pexp l, pexp r]))
@@ -352,21 +419,18 @@ elab info pattern tm = do elab' tm
352419 where inparamBlock n = case lookupCtxt n (inblock info) of
353420 Nothing -> False
354421 _ -> True
355− elab' (PRef fc n)
356− | Just ps <- lookupCtxt n (inblock info)
357− = elab' (PApp fc (PRef fc n) [])
358− | otherwise = erun fc $ do apply (Var n) []; solve
422+ elab' (PRef fc n) = erun fc $ do apply (Var n) []; solve
359423 elab' (PLam n Placeholder sc)
360− = do attack; intro (Just n); elab' sc; solve
424+ = do attack; intro (Just n); elabE sc; solve
361425 elab' (PPi _ n Placeholder sc)
362− = do attack; arg n (MN 0 "ty"); elab' sc; solve
426+ = do attack; arg n (MN 0 "ty"); elabE sc; solve
363427 elab' (PPi _ n ty sc)
364428 = do attack; tyn <- unique_hole (MN 0 "ty")
365429 claim tyn (RSet 0)
366430 forall n (Var tyn)
367431 focus tyn
368− elab' ty
369− elab' sc
432+ elabE ty
433+ elabE sc
370434 solve
371435 elab' (PLet n Placeholder val sc)
372436 = do attack; -- (h:_) <- get_holes
@@ -377,29 +441,27 @@ elab info pattern tm = do elab' tm
377441 claim valn (Var tyn)
378442 letbind n (Var tyn) (Var valn)
379443 focus valn
380− elab' val
381− elab' sc
444+ elabE val
445+ elabE sc
382446 -- end_unify
383447 solve
384− elab' (PApp fc (PRef _ f) args)
385− | Just ps <- lookupCtxt f (inblock info)
386− = erun fc $
387− elabApp (liftname info f) (map (pexp . (PRef fc)) ps ++ args)
388− | otherwise = erun fc $ elabApp f args
389− where elabApp f args
390− = try (do ns <- apply (Var f) (map isph args)
391− solve
392− elabArgs ns (map (\x -> (lazyarg x, getTm x)) args))
393− (do apply_elab f (map toElab args)
394− solve)
448+ elab' (PApp fc (PRef _ f) args')
449+ = do let args = {- case lookupCtxt f (inblock info) of
450+ Just ps -> (map (pexp . (PRef fc)) ps ++ args')
451+ _ ->-} args'
452+ try (do ns <- apply (Var f) (map isph args)
453+ solve
454+ elabArgs ns (map (\x -> (lazyarg x, getTm x)) args))
455+ (do apply_elab f (map toElab args)
456+ solve)
395457 elab' (PApp fc f [arg])
396458 = erun fc $
397− do simple_app (elab' f) (elab' (getTm arg))
459+ do simple_app (elabE f) (elabE (getTm arg))
398460 solve
399461 elab' Placeholder = do (h : hs) <- get_holes
400462 movelast h
401463 elab' (PMetavar n) = do attack; defer n; solve
402− elab' (PProof ts) = do mapM_ (runTac True) ts
464+ elab' (PProof ts) = do mapM_ (runTac True ist) ts
403465 elab' (PElabError e) = fail e
404466 elab' x = fail $ "Not implemented " ++ show x
405467
@@ -412,7 +474,7 @@ elab info pattern tm = do elab' tm
412474 pexp t]);
413475 elabArgs ns args
414476 | otherwise
415− = do focus n; elab' t; elabArgs ns args
477+ = do focus n; elabE t; elabArgs ns args
416478
417479
418480collectDeferred :: Term -> State [(Name, Type)] Term
@@ -433,16 +495,23 @@ collectDeferred t = return t
433495
434496-- Running tactics directly
435497
436−runTac :: Bool -> PTactic -> Elab ()
437−runTac autoSolve = runT where
498+runTac :: Bool -> IState -> PTactic -> Elab ()
499+runTac autoSolve ist tac = runT (fmap (addImpl ist) tac) where
438500 runT (Intro []) = do g <- goal
439501 attack; intro (bname g)
440502 where
441503 bname (Bind n _ _) = Just n
442504 bname _ = Nothing
443505 runT (Intro xs) = mapM_ (\x -> do attack; intro (Just x)) xs
444− runT (Exact tm) = do elab toplevel False tm
506+ runT (Exact tm) = do elab ist toplevel False tm
445507 when autoSolve solveAll
508+ runT (Refine fn []) = do let imps = case lookupCtxt fn (idris_implicits ist) of
509+ Nothing -> []
510+ Just args -> map isImp args
511+ ns <- apply (Var fn) imps
512+ when autoSolve solveAll
513+ where isImp (PImp _ _ _) = True
514+ isImp _ = False
446515 runT (Refine fn imps) = do ns <- apply (Var fn) imps
447516 when autoSolve solveAll
448517 runT (Rewrite tm) -- to elaborate tm, let bind it, then rewrite by that
@@ -455,17 +524,17 @@ runTac autoSolve = runT where
455524 letn <- unique_hole (MN 0 "rewrite_rule")
456525 letbind letn (Var tyn) (Var valn)
457526 focus valn
458− elab toplevel False tm
527+ elab ist toplevel False tm
459528 rewrite (Var letn)
460529 when autoSolve solveAll
461530 runT Compute = compute
462− runT Trivial = try (do elab toplevel False (PRefl (FC "prf" 0))
531+ runT Trivial = try (do elab ist toplevel False (PRefl (FC "prf" 0))
463532 when autoSolve solveAll)
464533 (do env <- get_env
465534 tryAll (map fst env))
466535 where
467536 tryAll [] = fail "No trivial solution"
468− tryAll (x:xs) = try (do elab toplevel False (PRef (FC "prf" 0) x)
537+ tryAll (x:xs) = try (do elab ist toplevel False (PRef (FC "prf" 0) x)
469538 when autoSolve solveAll)
470539 (tryAll xs)
471540 runT (Focus n) = focus n