@@ -16,6 +16,7 @@ import Core.CaseTree
1616
1717import Control.Monad
1818import Control.Monad.State
19+import Data.List
1920import Debug.Trace
2021
2122-- Data to pass to recursively called elaborators; e.g. for where blocks,
@@ -104,14 +105,14 @@ elabClauses info fc n_in cs = let n = liftname info n_in in
104105 depat (Bind n (PVar t) sc) = depat (instantiate (P Bound n t) sc)
105106 depat x = x
106107
107−elabVal :: ElabInfo -> PTerm -> Idris (Term, Type)
108−elabVal info tm_in
108+elabVal :: ElabInfo -> Bool -> PTerm -> Idris (Term, Type)
109+elabVal info aspat tm_in
109110 = do ctxt <- getContext
110111 i <- get
111112 let tm = addImpl i tm_in
112113 logLvl 10 (showImp True tm)
113114 ((tm', defer), _) <- tclift $ elaborate ctxt (MN 0 "val") infP
114− (build i info True (infTerm tm))
115+ (build i info aspat (infTerm tm))
115116 logLvl 3 ("Value: " ++ show tm')
116117 let vtm = getInferTerm tm'
117118 logLvl 2 (show vtm)
@@ -360,27 +361,6 @@ elab ist info pattern tm
360361 (h: hs) -> do patvar h; mkPat
361362 [] -> return ()
362363
363− -- Expand implict arguments here, then move on to main elaborator
364−-- elabE (PRef fc n)
365−-- | Just ps <- lookupCtxt n (inblock info)
366−-- = -- let exp = (PApp fc (PRef fc n) (map (pexp . (PRef fc)) ps))
367−-- elabE (PApp fc (PRef fc n) [])
368−-- elabE f@(PApp fc fn args) = trace ("Elaborating " ++ show f) $ elab' f
369−-- elabE (PRef fc f)
370−-- = do l <- local f
371−-- elabN f l
372−-- where elabN f False = let afn = aiFn ist fc f [] in
373−-- -- trace ("Ref expanded to " ++ showImp True afn) $
374−-- elab' afn
375−-- elabN f True = elab' (PRef fc f)
376−-- elabE (PApp fc fn@(PRef _ f) args)
377−-- = do l <- local f
378−-- elabApp f args l -- no implicits
379−-- where elabApp f args False = let afn = aiFn ist fc f args in
380−-- -- trace ("Expanded to " ++ showImp True afn) $
381−-- elab' afn
382−-- elabApp f args True = elab' (PApp fc fn args)
383−-- elabE (PApp fc f as) = elab' (mkPApp fc 1 f as)
384364 elabE t = elab' t
385365
386366 local f = do e <- get_env
@@ -392,6 +372,8 @@ elab ist info pattern tm
392372 elab' (PTrue fc) = try (elab' (PRef fc unitCon))
393373 (elab' (PRef fc unitTy))
394374 elab' (PFalse fc) = elab' (PRef fc falseTy)
375+ elab' (PResolveTC fc) = do c <- unique_hole (MN 0 "c")
376+ instanceArg c
395377 elab' (PRefl fc) = elab' (PApp fc (PRef fc eqCon) [pimp (MN 0 "a") Placeholder,
396378 pimp (MN 0 "x") Placeholder])
397379 elab' (PEq fc l r) = elab' (PApp fc (PRef fc eqTy) [pimp (MN 0 "a") Placeholder,
@@ -452,11 +434,25 @@ elab ist info pattern tm
452434 = do let args = {- case lookupCtxt f (inblock info) of
453435 Just ps -> (map (pexp . (PRef fc)) ps ++ args')
454436 _ ->-} args'
437+ ivs <- get_instances
455438 try (do ns <- apply (Var f) (map isph args)
456439 solve
457440 elabArgs ns (map (\x -> (lazyarg x, getTm x)) args))
458441 (do apply_elab f (map toElab args)
459442 solve)
443+ ivs' <- get_instances
444+ when (not pattern) $
445+ mapM_ (\n -> do focus n
446+ resolveTC ist) (ivs' \\ ivs)
447+-- ivs <- get_instances
448+-- when (not (null ivs)) $
449+-- do t <- get_term
450+-- trace (show ivs ++ "\n" ++ show t) $
451+-- mapM_ (\n -> do focus n
452+-- resolveTC ist) ivs
453+ where tcArg (n, PConstraint _ Placeholder) = True
454+ tcArg _ = False
455+
460456 elab' (PApp fc f [arg])
461457 = erun fc $
462458 do simple_app (elabE f) (elabE (getTm arg))
@@ -478,7 +474,40 @@ elab ist info pattern tm
478474 elabArgs ns args
479475 | otherwise
480476 = do focus n; elabE t; elabArgs ns args
481−
477+
478+trivial :: IState -> Elab ()
479+trivial ist = try (elab ist toplevel False (PRefl (FC "prf" 0)))
480+ (do env <- get_env
481+ tryAll (map fst env))
482+ where
483+ tryAll [] = fail "No trivial solution"
484+ tryAll (x:xs) = try (elab ist toplevel False (PRef (FC "prf" 0) x))
485+ (tryAll xs)
486+
487+resolveTC :: IState -> Elab ()
488+resolveTC ist = do t <- goal
489+ tm <- get_term
490+ try (trivial ist)
491+ (blunderbuss t (map fst (toAlist (tt_ctxt ist))))
492+ where
493+ blunderbuss t [] = fail $ "Can't resolve type class " ++ show t
494+ blunderbuss t (n:ns) | tcname n = try (resolve n)
495+ (blunderbuss t ns)
496+ | otherwise = blunderbuss t ns
497+ tcname (UN (('@':_) : _)) = True
498+ tcname _ = False
499+
500+ resolve n = do t <- goal
501+ let imps = case lookupCtxt n (idris_implicits ist) of
502+ Nothing -> []
503+ Just args -> map isImp args
504+ args <- apply (Var n) imps
505+ mapM_ (\ (_,n) -> do focus n
506+ resolveTC ist)
507+ (filter (\ (x, y) -> not x) (zip imps args))
508+ solve
509+ where isImp (PImp _ _ _) = True
510+ isImp _ = False
482511
483512collectDeferred :: Term -> State [(Name, Type)] Term
484513collectDeferred (Bind n (GHole t) app) =
@@ -531,15 +560,7 @@ runTac autoSolve ist tac = runT (fmap (addImpl ist) tac) where
531560 rewrite (Var letn)
532561 when autoSolve solveAll
533562 runT Compute = compute
534− runT Trivial = try (do elab ist toplevel False (PRefl (FC "prf" 0))
535− when autoSolve solveAll)
536− (do env <- get_env
537− tryAll (map fst env))
538− where
539− tryAll [] = fail "No trivial solution"
540− tryAll (x:xs) = try (do elab ist toplevel False (PRef (FC "prf" 0) x)
541− when autoSolve solveAll)
542− (tryAll xs)
563+ runT Trivial = do trivial ist; when autoSolve solveAll
543564 runT (Focus n) = focus n
544565 runT Solve = solve
545566 runT x = fail $ "Not implemented " ++ show x