1{-# LANGUAGE PatternGuards #-}
3module Idris.ProofSearch(trivial, proofSearch) where
5import Idris.Core.Elaborate hiding (Tactic(..))
7import Idris.Core.Evaluate
8import Idris.Core.CaseTree
9import Idris.Core.Typecheck
12import Idris.Delaborate
15import Control.Applicative ((<$>))
17import Control.Monad.State.Strict
21-- Pass in a term elaborator to avoid a cyclic dependency with ElabTerm
23trivial :: (PTerm -> ElabD ()) -> IState -> ElabD ()
24trivial elab ist = try' (do elab (PRefl (fileFC "prf") Placeholder)
31 tryAll [] = fail "No trivial solution"
33 = do -- if type of x has any holes in it, move on
36 if all (\n -> not (n `elem` hs)) (freeNames (binderTy b))
37 then try' (elab (PRef (fileFC "prf") x))
41cantSolveGoal :: ElabD ()
42cantSolveGoal = do g <- goal
45 CantSolveGoal g (map (\(n,b) -> (n, binderTy b)) env)
48 Bool -> -- invoked from a tactic proof. If so, making
49 -- new metavariables is meaningless, and there shoudl
50 -- be an error reported instead.
51 Int -> -- maximum depth
52 (PTerm -> ElabD ()) -> Maybe Name -> Name -> [Name] ->
54proofSearch False fromProver depth elab _ nroot [fn] ist
55 = do -- get all possible versions of the name, take the first one that
57 let all_imps = lookupCtxtName fn (idris_implicits ist)
60 -- if nothing worked, make a new metavariable
61 tryAllFns [] | fromProver = cantSolveGoal
62 tryAllFns [] = do attack; defer [] nroot; solve
63 tryAllFns (f : fs) = try' (tryFn f) (tryAllFns fs) True
65 tryFn (f, args) = do let imps = map isImp args
68 args <- map snd <$> try' (apply (Var f) imps)
69 (match_apply (Var f) imps) True
71-- when (length ps < length ps') $ fail "Can't apply constructor"
72 -- Make metavariables for new holes
75 if fromProver then cantSolveGoal
77 mapM_ (\ h -> do focus h
78 attack; defer [] nroot; solve)
80-- (filter (\ (x, y) -> not x) (zip (map fst imps) args))
83 isImp (PImp p _ _ _ _) = (True, p)
84 isImp arg = (True, priority arg) -- try to get all of them by unification
85proofSearch rec fromProver maxDepth elab fn nroot hints ist
86 = case lookupCtxt nroot (idris_tyinfodata ist) of
87 [TISolution ts] -> findInferredTy ts
88 _ -> psRec rec maxDepth
90 findInferredTy (t : _) = elab (delab ist (toUN t))
92 toUN t@(P nt (MN i n) ty)
93 | ('_':xs) <- str n = t
94 | otherwise = P nt (UN n) ty
95 toUN (App f a) = App (toUN f) (toUN a)
98 psRec _ 0 | fromProver = cantSolveGoal
99 psRec rec 0 = do attack; defer [] nroot; solve --fail "Maximum depth reached"
100 psRec False d = tryCons d hints
101 psRec True d = try' (trivial elab ist)
102 (try' (try' (resolveByCon (d - 1)) (resolveByLocals (d - 1))
104 -- if all else fails, make a new metavariable
107 else do attack; defer [] nroot; solve) True) True
110 getFn d (Just f) | d < maxDepth-1 = [f]
115 let (f, _) = unApply t
117 P _ n _ -> case lookupCtxt n (idris_datatypes ist) of
118 [t] -> tryCons d (hints ++ con_names t ++
120 _ -> fail "Not a data type"
121 _ -> fail "Not a data type"
123 -- if there are local variables which have a function type, try
129 tryLocals d [] = fail "Locals failed"
130 tryLocals d ((x, t) : xs) = try' (tryLocal d x t) (tryLocals d xs) True
132 tryCons d [] = fail "Constructors failed"
133 tryCons d (c : cs) = try' (tryCon d c) (tryCons d cs) True
135 tryLocal d n t = do let a = getPArity (delab ist (binderTy t))
138 tryLocalArg d n 0 = elab (PRef (fileFC "prf") n)
139 tryLocalArg d n i = simple_app (tryLocalArg d n (i - 1))
140 (psRec True d) "proof search local apply"
142 -- Like type class resolution, but searching with constructors
144 do let imps = case lookupCtxtName n (idris_implicits ist) of
146 [args] -> map isImp (snd args)
147 _ -> fail "Ambiguous name"
150 args <- map snd <$> try' (apply (Var n) imps)
151 (match_apply (Var n) imps) True
154 when (length ps < length ps') $ fail "Can't apply constructor"
155 mapM_ (\ (_, h) -> do focus h
157 (filter (\ (x, y) -> not x) (zip (map fst imps) args))
160 isImp (PImp p _ _ _ _) = (True, p)
161 isImp arg = (False, priority arg)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list