@@ -6,6 +6,7 @@ import Idris.AbsSyntax
6import Idris.Error
6import Idris.Error
7import Idris.Delaborate
7import Idris.Delaborate
8import Idris.Imports
8import Idris.Imports
9import Paths_idris
10import Paths_idris
11import Core.TT
12import Core.TT
@@ -19,22 +20,15 @@ import Control.Monad.State
19import Data.List
20import Data.List
20import Debug.Trace
21import Debug.Trace
22−-- Data to pass to recursively called elaborators; e.g. for where blocks,
23−-- paramaterised declarations, etc.
25−data ElabInfo = EInfo { params :: [(Name, PTerm)],
24+recheckC ctxt fc env t
26− inblock :: Ctxt [Name], -- names in the block, and their params
25+ = do (tm, ty, cs) <- tclift $ recheck ctxt env t
27− liftname :: Name -> Name }
26+ addConstraints fc cs
29−toplevel = EInfo [] emptyContext id
31−recheckC ctxt env t = do (tm, ty, cs) <- tclift $ recheck ctxt env t
35−checkDef ns = do ctxt <- getContext
29+checkDef fc ns = do ctxt <- getContext
36− mapM (\(n, t) -> do (t', _) <- recheckC ctxt [] t
30+ mapM (\(n, t) -> do (t', _) <- recheckC ctxt fc [] t
37− return (n, t')) ns
31+ return (n, t')) ns
39elabType :: ElabInfo -> SyntaxInfo -> FC -> Name -> PTerm -> Idris ()
33elabType :: ElabInfo -> SyntaxInfo -> FC -> Name -> PTerm -> Idris ()
40elabType info syn fc n ty' = {- let ty' = piBind (params info) ty_in
34elabType info syn fc n ty' = {- let ty' = piBind (params info) ty_in
@@ -47,10 +41,10 @@ elabType info syn fc n ty' = {- let ty' = piBind (params info) ty_in
47 logLvl 2 $ show n ++ " type " ++ showImp True ty
41 logLvl 2 $ show n ++ " type " ++ showImp True ty
48 ((ty', defer), log) <- tclift $ elaborate ctxt n (Set (UVal 0))
42 ((ty', defer), log) <- tclift $ elaborate ctxt n (Set (UVal 0))
49 (erun fc (build i info False ty))
43 (erun fc (build i info False ty))
50− (cty, _) <- recheckC ctxt [] ty'
44+ (cty, _) <- recheckC ctxt fc [] ty'
51 logLvl 2 $ "---> " ++ show cty
45 logLvl 2 $ "---> " ++ show cty
52 let nty = normalise ctxt [] cty
46 let nty = normalise ctxt [] cty
53− ds <- checkDef ((n, nty):defer)
47+ ds <- checkDef fc ((n, nty):defer)
54 addDeferred ds
48 addDeferred ds
56elabData :: ElabInfo -> SyntaxInfo -> FC -> PData -> Idris ()
50elabData :: ElabInfo -> SyntaxInfo -> FC -> PData -> Idris ()
@@ -63,9 +57,9 @@ elabData info syn fc (PDatadecl n t_in dcons)
63 let t = addImpl i t_in
57 let t = addImpl i t_in
64 ((t', defer), log) <- tclift $ elaborate ctxt n (Set (UVal 0))
58 ((t', defer), log) <- tclift $ elaborate ctxt n (Set (UVal 0))
65 (erun fc (build i info False t))
59 (erun fc (build i info False t))
66− def' <- checkDef defer
60+ def' <- checkDef fc defer
67 addDeferred def'
61 addDeferred def'
68− (cty, _) <- recheckC ctxt [] t'
62+ (cty, _) <- recheckC ctxt fc [] t'
69 logLvl 2 $ "---> " ++ show cty
63 logLvl 2 $ "---> " ++ show cty
70 updateContext (addTyDecl n cty) -- temporary, to check cons
64 updateContext (addTyDecl n cty) -- temporary, to check cons
71 cons <- mapM (elabCon info syn) dcons
65 cons <- mapM (elabCon info syn) dcons
@@ -83,10 +77,10 @@ elabCon info syn (n, t_in, fc)
83 ((t', defer), log) <- tclift $ elaborate ctxt n (Set (UVal 0))
77 ((t', defer), log) <- tclift $ elaborate ctxt n (Set (UVal 0))
84 (erun fc (build i info False t))
78 (erun fc (build i info False t))
85 logLvl 2 $ "Rechecking " ++ show t'
79 logLvl 2 $ "Rechecking " ++ show t'
86− def' <- checkDef defer
80+ def' <- checkDef fc defer
87 addDeferred def'
81 addDeferred def'
88 ctxt <- getContext
82 ctxt <- getContext
89− (cty, _) <- recheckC ctxt [] t'
83+ (cty, _) <- recheckC ctxt fc [] t'
90 logLvl 2 $ "---> " ++ show n ++ " : " ++ show cty
84 logLvl 2 $ "---> " ++ show n ++ " : " ++ show cty
91 return (n, cty)
85 return (n, cty)
@@ -122,7 +116,7 @@ elabVal info aspat tm_in
122 logLvl 3 ("Value: " ++ show tm')
116 logLvl 3 ("Value: " ++ show tm')
123 let vtm = getInferTerm tm'
117 let vtm = getInferTerm tm'
124 logLvl 2 (show vtm)
118 logLvl 2 (show vtm)
125− recheckC ctxt [] vtm
119+ recheckC ctxt (FC "prompt" 0) [] vtm
127elabClause :: ElabInfo -> FC -> PClause -> Idris (Term, Term)
121elabClause :: ElabInfo -> FC -> PClause -> Idris (Term, Term)
128elabClause info fc (PClause fname lhs_in withs rhs_in whereblock)
122elabClause info fc (PClause fname lhs_in withs rhs_in whereblock)
@@ -137,7 +131,7 @@ elabClause info fc (PClause fname lhs_in withs rhs_in whereblock)
137 let lhs_tm = orderPats (getInferTerm lhs')
131 let lhs_tm = orderPats (getInferTerm lhs')
138 let lhs_ty = getInferType lhs'
132 let lhs_ty = getInferType lhs'
139 logLvl 3 (show lhs_tm)
133 logLvl 3 (show lhs_tm)
140− (clhs, clhsty) <- recheckC ctxt [] lhs_tm
134+ (clhs, clhsty) <- recheckC ctxt fc [] lhs_tm
141 logLvl 5 ("Checked " ++ show clhs)
135 logLvl 5 ("Checked " ++ show clhs)
142 -- Elaborate where block
136 -- Elaborate where block
143 ist <- getIState
137 ist <- getIState
@@ -164,10 +158,10 @@ elabClause info fc (PClause fname lhs_in withs rhs_in whereblock)
164 return $ runState (collectDeferred tt) [])
158 return $ runState (collectDeferred tt) [])
165 logLvl 2 $ "---> " ++ show rhs'
159 logLvl 2 $ "---> " ++ show rhs'
166 when (not (null defer)) $ iLOG $ "DEFERRED " ++ show defer
160 when (not (null defer)) $ iLOG $ "DEFERRED " ++ show defer
167− def' <- checkDef defer
161+ def' <- checkDef fc defer
168 addDeferred def'
162 addDeferred def'
169 ctxt <- getContext
163 ctxt <- getContext
170− (crhs, crhsty) <- recheckC ctxt [] rhs'
164+ (crhs, crhsty) <- recheckC ctxt fc [] rhs'
171 return (clhs, crhs)
165 return (clhs, crhs)
173 decorate x = UN (show fname ++ "#" ++ show x)
167 decorate x = UN (show fname ++ "#" ++ show x)
@@ -197,7 +191,7 @@ elabClause info fc (PWith fname lhs_in withs wval_in withblock)
197 let lhs_ty = getInferType lhs'
191 let lhs_ty = getInferType lhs'
198 let ret_ty = getRetTy lhs_ty
192 let ret_ty = getRetTy lhs_ty
199 logLvl 3 (show lhs_tm)
193 logLvl 3 (show lhs_tm)
200− (clhs, clhsty) <- recheckC ctxt [] lhs_tm
194+ (clhs, clhsty) <- recheckC ctxt fc [] lhs_tm
201 logLvl 5 ("Checked " ++ show clhs)
195 logLvl 5 ("Checked " ++ show clhs)
202 let bargs = getPBtys lhs_tm
196 let bargs = getPBtys lhs_tm
203 let wval = addImpl i wval_in
197 let wval = addImpl i wval_in
@@ -212,9 +206,9 @@ elabClause info fc (PWith fname lhs_in withs wval_in withblock)
212 psolve lhs_tm
206 psolve lhs_tm
213 tt <- get_term
207 tt <- get_term
214 return (tt, d))
208 return (tt, d))
215− def' <- checkDef defer
209+ def' <- checkDef fc defer
216 addDeferred def'
210 addDeferred def'
217− (cwval, cwvalty) <- recheckC ctxt [] (getInferTerm wval')
211+ (cwval, cwvalty) <- recheckC ctxt fc [] (getInferTerm wval')
218 logLvl 3 ("With type " ++ show cwvalty ++ "\nRet type " ++ show ret_ty)
212 logLvl 3 ("With type " ++ show cwvalty ++ "\nRet type " ++ show ret_ty)
219 windex <- getName
213 windex <- getName
220 -- build a type declaration for the new function:
214 -- build a type declaration for the new function:
@@ -224,7 +218,7 @@ elabClause info fc (PWith fname lhs_in withs wval_in withblock)
224 let wname = MN windex (show fname)
218 let wname = MN windex (show fname)
225 let imps = getImps wtype -- add to implicits context
219 let imps = getImps wtype -- add to implicits context
226 put (i { idris_implicits = addDef wname imps (idris_implicits i) })
220 put (i { idris_implicits = addDef wname imps (idris_implicits i) })
227− def' <- checkDef [(wname, wtype)]
221+ def' <- checkDef fc [(wname, wtype)]
228 addDeferred def'
222 addDeferred def'
230 -- in the subdecls, lhs becomes:
224 -- in the subdecls, lhs becomes:
@@ -247,9 +241,9 @@ elabClause info fc (PWith fname lhs_in withs wval_in withblock)
247 psolve lhs_tm
241 psolve lhs_tm
248 tt <- get_term
242 tt <- get_term
249 return (tt, d))
243 return (tt, d))
250− def' <- checkDef defer
244+ def' <- checkDef fc defer
251 addDeferred def'
245 addDeferred def'
252− (crhs, crhsty) <- recheckC ctxt [] rhs'
246+ (crhs, crhsty) <- recheckC ctxt fc [] rhs'
253 return (clhs, crhs)
247 return (clhs, crhs)
255 getImps (Bind n (Pi _) t) = pexp Placeholder : getImps t
249 getImps (Bind n (Pi _) t) = pexp Placeholder : getImps t
@@ -485,304 +479,3 @@ elabDecl' info (PDirective i) = i
485-- elabDecl' info (PImport i) = loadModule i
479-- elabDecl' info (PImport i) = loadModule i
488−-- Using the elaborator, convert a term in raw syntax to a fully
489−-- elaborated, typechecked term.
491−-- If building a pattern match, we convert undeclared variables from
492−-- holes to pattern bindings.
494−-- Also find deferred names in the term and their types
496−build :: IState -> ElabInfo -> Bool -> PTerm -> Elab (Term, [(Name, Type)])
497−build ist info pattern tm
498− = do elab ist info pattern tm
500− return $ runState (collectDeferred tt) []
502−elab :: IState -> ElabInfo -> Bool -> PTerm -> Elab ()
503−elab ist info pattern tm
505− when pattern -- convert remaining holes to pattern vars
508− isph arg = case getTm arg of
512− toElab arg = case getTm arg of
513− Placeholder -> Nothing
514− v -> Just (priority arg, elabE v)
516− mkPat = do hs <- get_holes
518− (h: hs) -> do patvar h; mkPat
521− elabE t = {- do g <- goal
523− trace ("Elaborating " ++ show t ++ " : " ++ show g ++ "\n\tin " ++ show tm)
526− local f = do e <- get_env
527− return (f `elem` map fst e)
529− elab' PSet = do fill RSet; solve
530− elab' (PConstant c) = do apply (RConstant c) []; solve
531− elab' (PQuote r) = do fill r; solve
532− elab' (PTrue fc) = try (elab' (PRef fc unitCon))
533− (elab' (PRef fc unitTy))
534− elab' (PFalse fc) = elab' (PRef fc falseTy)
535− elab' (PResolveTC (FC "HACK" _))
537− elab' (PResolveTC fc) = do c <- unique_hole (MN 0 "c")
539− elab' (PRefl fc) = elab' (PApp fc (PRef fc eqCon) [pimp (MN 0 "a") Placeholder,
540− pimp (MN 0 "x") Placeholder])
541− elab' (PEq fc l r) = elab' (PApp fc (PRef fc eqTy) [pimp (MN 0 "a") Placeholder,
542− pimp (MN 0 "b") Placeholder,
544− elab' (PPair fc l r) = try (elabE (PApp fc (PRef fc pairTy)
546− (elabE (PApp fc (PRef fc pairCon)
547− [pimp (MN 0 "A") Placeholder,
548− pimp (MN 0 "B") Placeholder,
550− elab' (PDPair fc l@(PRef _ n) t r)
552− Placeholder -> try asType asValue
554− where asType = elab' (PApp fc (PRef fc sigmaTy)
556− pexp (PLam n Placeholder r)])
557− asValue = elab' (PApp fc (PRef fc existsCon)
559− pimp (MN 0 "P") Placeholder,
561− elab' (PDPair fc l t r) = elab' (PApp fc (PRef fc existsCon)
563− pimp (MN 0 "P") Placeholder,
565− elab' (PRef fc n) | pattern && not (inparamBlock n)
566− = do ctxt <- get_context
567− let sc = case lookupTy n ctxt of
572− try (do apply (Var n) []; solve)
574− else do apply (Var n) []; solve
575− where inparamBlock n = case lookupCtxt n (inblock info) of
578− elab' (PRef fc n) = erun fc $ do apply (Var n) []; solve
579− elab' (PLam n Placeholder sc)
580− = do attack; intro (Just n); elabE sc; solve
581− elab' (PLam n ty sc)
582− = do tyn <- unique_hole (MN 0 "lamty")
585− introTy (Var tyn) (Just n)
591− elab' (PPi _ n Placeholder sc)
592− = do attack; arg n (MN 0 "ty"); elabE sc; solve
593− elab' (PPi _ n ty sc)
594− = do attack; tyn <- unique_hole (MN 0 "ty")
597− MN _ _ -> unique_hole n
604− elab' (PLet n ty val sc)
606− tyn <- unique_hole (MN 0 "letty")
608− valn <- unique_hole (MN 0 "letval")
609− claim valn (Var tyn)
610− letbind n (Var tyn) (Var valn)
612− Placeholder -> return ()
619− elab' (PApp fc (PRef _ f) args')
620− = do let args = {- case lookupCtxt f (inblock info) of
621− Just ps -> (map (pexp . (PRef fc)) ps ++ args')
623− ivs <- get_instances
624− try (do ns <- apply (Var f) (map isph args)
628− sortBy (\(_,x) (_,y) -> compare (priority x) (priority y))
630− elabArgs [] False ns' (map (\x -> (lazyarg x, getTm x)) eargs))
631− (do apply_elab f (map toElab args)
633− ivs' <- get_instances
634− when (not pattern) $
635− mapM_ (\n -> do focus n
636− resolveTC 10 ist) (ivs' \\ ivs)
637−-- ivs <- get_instances
638−-- when (not (null ivs)) $
640−-- trace (show ivs ++ "\n" ++ show t) $
641−-- mapM_ (\n -> do focus n
642−-- resolveTC ist) ivs
643− where tcArg (n, PConstraint _ _ Placeholder) = True
646− elab' (PApp fc f [arg])
648− do simple_app (elabE f) (elabE (getTm arg))
650− elab' Placeholder = do (h : hs) <- get_holes
652− elab' (PMetavar n) = do attack; defer n; solve
653− elab' (PProof ts) = do mapM_ (runTac True ist) ts
654− elab' (PTactics ts) = do mapM_ (runTac False ist) ts
655− elab' (PElabError e) = fail e
656− elab' x = fail $ "Not implemented " ++ show x
658− elabArgs failed retry [] _
659− | retry = let (ns, ts) = unzip (reverse failed) in
660− elabArgs [] False ns ts
661− | otherwise = return ()
662− elabArgs failed r (n:ns) ((_, Placeholder) : args)
663− = elabArgs failed r ns args
664− elabArgs failed r (n:ns) ((lazy, t) : args)
665− | lazy && not pattern
666− = do elabArg n (PApp bi (PRef bi (UN "lazy"))
667− [pimp (UN "a") Placeholder,
669− | otherwise = elabArg n t
670− where elabArg n t = if r
671− then try (do focus n; elabE t; elabArgs failed r ns args)
672− (elabArgs ((n,(lazy, t)):failed) r ns args)
673− else do focus n; elabE t; elabArgs failed r ns args
675−trivial :: IState -> Elab ()
676−trivial ist = try (elab ist toplevel False (PRefl (FC "prf" 0)))
678− tryAll (map fst env))
680− tryAll [] = fail "No trivial solution"
681− tryAll (x:xs) = try (elab ist toplevel False (PRef (FC "prf" 0) x))
684−resolveTC :: Int -> IState -> Elab ()
689− (blunderbuss t (map fst (ctxtAlist (tt_ctxt ist))))
691− blunderbuss t [] = fail $ "Can't resolve type class " ++ show t
692− blunderbuss t (n:ns) | tcname n = try (resolve n depth)
694− | otherwise = blunderbuss t ns
695− tcname (UN ('@':_)) = True
696− tcname (NS _ n) = tcname n
700− | depth == 0 = fail $ "Can't resolve type class"
703− let imps = case lookupCtxt n (idris_implicits ist) of
705− Just args -> map isImp args
706− args <- apply (Var n) imps
707− mapM_ (\ (_,n) -> do focus n
708− resolveTC (depth - 1) ist)
709− (filter (\ (x, y) -> not x) (zip imps args))
711− where isImp (PImp _ _ _ _) = True
714−collectDeferred :: Term -> State [(Name, Type)] Term
715−collectDeferred (Bind n (GHole t) app) =
719−collectDeferred (Bind n b t) = do b' <- cdb b
720− t' <- collectDeferred t
721− return (Bind n b' t')
723− cdb (Let t v) = liftM2 Let (collectDeferred t) (collectDeferred v)
724− cdb (Guess t v) = liftM2 Guess (collectDeferred t) (collectDeferred v)
725− cdb b = do ty' <- collectDeferred (binderTy b)
726− return (b { binderTy = ty' })
727−collectDeferred (App f a) = liftM2 App (collectDeferred f) (collectDeferred a)
728−collectDeferred t = return t
730−-- Running tactics directly
732−runTac :: Bool -> IState -> PTactic -> Elab ()
733−runTac autoSolve ist tac = runT (fmap (addImpl ist) tac) where
734− runT (Intro []) = do g <- goal
735− attack; intro (bname g)
737− bname (Bind n _ _) = Just n
739− runT (Intro xs) = mapM_ (\x -> do attack; intro (Just x)) xs
740− runT (Exact tm) = do elab ist toplevel False tm
741− when autoSolve solveAll
742− runT (Refine fn []) = do imps <- case lookupCtxt fn (idris_implicits ist) of
743− Nothing -> envArgs fn
744− Just args -> return $ map isImp args
745− ns <- apply (Var fn) imps
746− when autoSolve solveAll
747− where isImp (PImp _ _ _ _) = True
749− envArgs n = do e <- get_env
751− Just t -> return $ map (const False)
752− (getArgTys (binderTy t))
754− runT (Refine fn imps) = do ns <- apply (Var fn) imps
755− when autoSolve solveAll
756− runT (Rewrite tm) -- to elaborate tm, let bind it, then rewrite by that
757− = do attack; -- (h:_) <- get_holes
758− tyn <- unique_hole (MN 0 "rty")
761− valn <- unique_hole (MN 0 "rval")
762− claim valn (Var tyn)
763− letn <- unique_hole (MN 0 "rewrite_rule")
764− letbind letn (Var tyn) (Var valn)
766− elab ist toplevel False tm
768− when autoSolve solveAll
771− tyn <- unique_hole (MN 0 "letty")
773− valn <- unique_hole (MN 0 "letval")
774− claim valn (Var tyn)
775− letn <- unique_hole n
776− letbind letn (Var tyn) (Var valn)
778− elab ist toplevel False tm
779− when autoSolve solveAll
780− runT Compute = compute
781− runT Trivial = do trivial ist; when autoSolve solveAll
782− runT (Focus n) = focus n
784− runT (Try l r) = do try (runT l) (runT r)
785− runT (TSeq l r) = do runT l; runT r
786− runT x = fail $ "Not implemented " ++ show x
788−solveAll = try (do solve; solveAll) (return ())