1{-# LANGUAGE ScopedTypeVariables #-}
3module Idris.TypeSearch (
4 searchByType, searchPred, defaultScoreFunction
7import Control.Applicative (Applicative (..), (<$>), (<*>), (<|>))
8import Control.Arrow (first, second, (&&&), (***))
9import Control.Monad (guard)
11import Data.List (find, partition, (\\))
13import qualified Data.Map as M
14import Data.Maybe (catMaybes, fromMaybe, isJust, maybeToList, mapMaybe)
15import Data.Monoid (Monoid (mempty, mappend))
16import Data.Ord (comparing)
17import qualified Data.PriorityQueue.FingerTree as Q
19import qualified Data.Set as S
20import qualified Data.Text as T (pack, isPrefixOf)
21import Data.Traversable (traverse)
23import Idris.AbsSyntax (addUsingConstraints, addImpl, getIState, putIState, implicit)
24import Idris.AbsSyntaxTree (class_instances, ClassInfo, defaultSyntax, eqTy, Idris,
25 IState (idris_classes, idris_docstrings, tt_ctxt, idris_outputmode),
26 implicitAllowed, OutputMode(..), PTerm, toplevel)
27import Idris.Core.Evaluate (Context (definitions), Def (Function, TyDecl, CaseOp), normaliseC)
28import Idris.Core.TT hiding (score)
29import Idris.Core.Unify (match_unify)
30import Idris.Delaborate (delabTy)
31import Idris.Docstrings (noDocs, overview)
32import Idris.Elab.Type (elabType)
33import Idris.Output (iRenderOutput, iPrintResult, iRenderResult, prettyDocumentedIst)
35import Prelude hiding (pred)
37import Util.Pretty (text, char, vsep, (<>), Doc, annotate)
39searchByType :: PTerm -> Idris ()
40searchByType pterm = do
41 pterm' <- addUsingConstraints syn emptyFC pterm
42 pterm'' <- implicit toplevel syn name pterm'
44 let pterm''' = addImpl i pterm''
45 ty <- elabType toplevel syn (fst noDocs) (snd noDocs) emptyFC [] name pterm'
46 putIState i -- don't actually make any changes
47 let names = searchUsing searchPred i ty
48 let names' = take numLimit names
50 [ let docInfo = (n, delabTy i n, fmap (overview . fst) (lookupCtxtExact n (idris_docstrings i))) in
51 displayScore theScore <> char ' ' <> prettyDocumentedIst i docInfo
52 | (n, theScore) <- names']
53 case idris_outputmode i of
54 RawOutput _ -> do mapM_ iRenderOutput docs
56 IdeSlave _ _ -> iRenderResult (vsep docs)
59 syn = defaultSyntax { implicitAllowed = True } -- syntax
60 name = sMN 0 "searchType" -- name
62-- | Conduct a type-directed search using a given match predicate
63searchUsing :: (IState -> Type -> [(Name, Type)] -> [(Name, a)])
64 -> IState -> Type -> [(Name, a)]
65searchUsing pred istate ty = pred istate nty . concat . M.elems $
66 M.mapWithKey (\key -> M.toAscList . M.mapMaybe (f key)) (definitions ctxt)
68 nty = normaliseC ctxt [] ty
71 guard $ not (special k)
72 type2 <- typeFromDef x
73 return $ normaliseC ctxt [] type2
74 special :: Name -> Bool
75 special (NS n _) = special n
77 special (UN n) = T.pack "default#" `T.isPrefixOf` n
78 || n `elem` map T.pack ["believe_me", "really_believe_me"]
81-- Our default search predicate.
82searchPred :: IState -> Type -> [(Name, Type)] -> [(Name, Score)]
83searchPred istate ty1 = matcher where
85 matcher = matchTypesBulk istate maxScore ty1
88typeFromDef :: (Def, b, c, d) -> Maybe Type
89typeFromDef (def, _, _, _) = get def where
90 get :: Def -> Maybe Type
91 get (Function ty _) = Just ty
92 get (TyDecl _ ty) = Just ty
93 -- get (Operator ty _ _) = Just ty
94 get (CaseOp _ ty _ _ _ _) = Just ty
98-- | reverse the edges for a directed acyclic graph
99reverseDag :: Ord k => [((k, a), Set k)] -> [((k, a), Set k)]
100reverseDag xs = map f xs where
101 f ((k, v), _) = ((k, v), S.fromList . map (fst . fst) $ filter (S.member k . snd) xs)
104-- | Compute a directed acyclic graph corresponding to the
105-- arguments of a function.
106-- returns [(the name and type of the bound variable
107-- the names in the type of the bound variable)]
109 => (TT n -> Bool) -- ^ filter to remove some arguments
111 -> ([((n, TT n), Set n)], [(n, TT n)], TT n)
112computeDagP removePred t = (reverse (map f arguments), reverse theRemovedArgs , retTy) where
113 f (n, ty) = ((n, ty), M.keysSet (usedVars ty))
115 (arguments, theRemovedArgs, retTy) = go [] [] t
117 -- NOTE : args are in reverse order
118 go args removedArgs (Bind n (Pi ty _) sc) = let arg = (n, ty) in
120 then go args (arg : removedArgs) sc
121 else go (arg : args) removedArgs sc
122 go args removedArgs sc = (args, removedArgs, sc)
124-- | Collect the names and types of all the free variables
125-- The Boolean indicates those variables which are determined due to injectivity
126-- I have not given nearly enough thought to know whether this is correct
127usedVars :: Ord n => TT n -> Map n (TT n, Bool)
128usedVars = f True where
129 f b (P Bound n t) = M.singleton n (t, b) `M.union` f b t
130 f b (Bind n binder t2) = (M.delete n (f b t2) `M.union`) $ case binder of
131 Let t v -> f b t `M.union` f b v
132 Guess t v -> f b t `M.union` f b v
133 bind -> f b (binderTy bind)
134 f b (App t1 t2) = f b t1 `M.union` f (b && isInjective t1) t2
135 f b (Proj t _) = f b t
136 f _ (V _) = error "unexpected! run vToP first"
139-- | Remove a node from a directed acyclic graph
140deleteFromDag :: Ord n => n -> [((n, TT n), (a, Set n))] -> [((n, TT n), (a, Set n))]
141deleteFromDag name [] = []
142deleteFromDag name (((name2, ty), (ix, set)) : xs) = (if name == name2
144 else (((name2, ty) , (ix, S.delete name set)) :) ) (deleteFromDag name xs)
146deleteFromArgList :: Ord n => n -> [(n, TT n)] -> [(n, TT n)]
147deleteFromArgList n = filter ((/= n) . fst)
149-- | Asymmetric modifications to keep track of
152 , typeClassApp :: !Int
153 , typeClassIntro :: !Int
154 } deriving (Eq, Show)
157-- | Homogenous tuples
161 } deriving (Eq, Show)
163sided :: (a -> a -> b) -> Sided a -> b
164sided f (Sided l r) = f l r
166-- | Could be a functor instance, but let's avoid name overloading
167both :: (a -> b) -> Sided a -> Sided b
168both f (Sided l r) = Sided (f l) (f r)
171 { transposition :: !Int
172 , equalityFlips :: !Int
173 , asymMods :: !(Sided AsymMods)
174 } deriving (Eq, Show)
176displayScore :: Score -> Doc OutputAnnotation
177displayScore score = case both noMods (asymMods score) of
178 Sided True True -> annotated EQ "=" -- types are isomorphic
179 Sided True False -> annotated LT "<" -- found type is more general than searched type
180 Sided False True -> annotated GT ">" -- searched type is more general than found type
181 Sided False False -> text "_"
183 annotated ordr = annotate (AnnSearchResult ordr) . text
184 noMods (Mods app tcApp tcIntro) = app + tcApp + tcIntro == 0
186scoreCriterion :: Score -> Bool
187scoreCriterion (Score _ _ amods) = not
188 ( sided (&&) (both ((> 0) . argApp) amods)
189 || sided (+) (both argApp amods) > 4
190 || sided (||) (both (\(Mods _ tcApp tcIntro) -> tcApp > 3 || tcIntro > 3) amods)
193defaultScoreFunction :: Score -> Int
194defaultScoreFunction (Score trans eqFlip amods) =
195 trans + eqFlip + linearPenalty + upAndDowncastPenalty
197 -- prefer finding a more general type to a less general type
198 linearPenalty = (\(Sided l r) -> 3 * l + r)
199 (both (\(Mods app tcApp tcIntro) -> 3 * app + 4 * tcApp + 2 * tcIntro) amods)
200 -- it's very bad to have *both* upcasting and downcasting
201 upAndDowncastPenalty = 100 *
202 sided (*) (both (\(Mods app tcApp tcIntro) -> 2 * app + tcApp + tcIntro) amods)
204instance Monoid a => Monoid (Sided a) where
205 mempty = Sided mempty mempty
206 (Sided l1 r1) `mappend` (Sided l2 r2) = Sided (l1 `mappend` l2) (r1 `mappend` r2)
208instance Monoid AsymMods where
210 (Mods a b c) `mappend` (Mods a' b' c') = Mods (a + a') (b + b') (c + c')
212instance Monoid Score where
213 mempty = Score 0 0 mempty
214 (Score t e mods) `mappend` (Score t' e' mods') = Score (t + t') (e + e') (mods `mappend` mods')
216-- | A directed acyclic graph representing the arguments to a function
217-- The 'Int' represents the position of the argument (1st argument, 2nd, etc.)
218type ArgsDAG = [((Name, Type), (Int, Set Name))]
220-- | A list of typeclass constraints
221type Classes = [(Name, Type)]
223-- | The state corresponding to an attempted match of two types.
225 { holes :: ![(Name, Type)] -- ^ names which have yet to be resolved
226 , argsAndClasses :: !(Sided (ArgsDAG, Classes))
227 -- ^ arguments and typeclass constraints for each type which have yet to be resolved
228 , score :: !Score -- ^ the score so far
229 , usedNames :: ![Name] -- ^ all names that have been used
232modifyTypes :: (Type -> Type) -> (ArgsDAG, Classes) -> (ArgsDAG, Classes)
233modifyTypes f = modifyDag *** modifyList
235 modifyDag = map (first (second f))
236 modifyList = map (second f)
238findNameInArgsDAG :: Name -> ArgsDAG -> Maybe (Type, Maybe Int)
239findNameInArgsDAG name = fmap ((snd . fst) &&& (Just . fst . snd)) . find ((name ==) . fst . fst)
241findName :: Name -> (ArgsDAG, Classes) -> Maybe (Type, Maybe Int)
242findName n (args, classes) = findNameInArgsDAG n args <|> ((,) <$> lookup n classes <*> Nothing)
244deleteName :: Name -> (ArgsDAG, Classes) -> (ArgsDAG, Classes)
245deleteName n (args, classes) = (deleteFromDag n args, filter ((/= n) . fst) classes)
247tcToMaybe :: TC' e a -> Maybe a
248tcToMaybe (OK x) = Just x
249tcToMaybe (Error _) = Nothing
251inArgTys :: (Type -> Type) -> ArgsDAG -> ArgsDAG
252inArgTys = map . first . second
255typeclassUnify :: Ctxt ClassInfo -> Context -> Type -> Type -> Maybe [(Name, Type)]
256typeclassUnify classInfo ctxt ty tyTry = do
257 res <- tcToMaybe $ match_unify ctxt [] ty retTy [] theHoles []
258 guard $ null (theHoles \\ map fst res)
259 let argTys' = map (second $ foldr (.) id [ subst n t | (n, t) <- res ]) tcArgs
263 theHoles = map fst nonTcArgs
264 retTy = getRetTy tyTry'
265 (tcArgs, nonTcArgs) = partition (isTypeClassArg classInfo . snd) $ getArgTys tyTry'
267isTypeClassArg :: Ctxt ClassInfo -> Type -> Bool
268isTypeClassArg classInfo ty = not (null (getClassName clss >>= flip lookupCtxt classInfo)) where
269 (clss, args) = unApply ty
270 getClassName (P (TCon _ _) className _) = [className]
274instance Ord Score where
275 compare = comparing defaultScoreFunction
277-- | Compute the power set
278subsets :: [a] -> [[a]]
280subsets (x : xs) = let ss = subsets xs in map (x :) ss ++ ss
283-- not recursive (i.e., doesn't flip iterated identities) at the moment
284-- recalls the number of flips that have been made
285flipEqualities :: Type -> [(Int, Type)]
286flipEqualities t = case t of
287 eq1@(App (App (App (App eq@(P _ eqty _) tyL) tyR) valL) valR) | eqty == eqTy ->
288 [(0, eq1), (1, App (App (App (App eq tyR) tyL) valR) valL)]
289 Bind n binder sc -> (\bind' (j, sc') -> (fst (binderTy bind') + j, Bind n (fmap snd bind') sc'))
290 <$> traverse flipEqualities binder <*> flipEqualities sc
291 App f x -> (\(i, f') (j, x') -> (i + j, App f' x'))
292 <$> flipEqualities f <*> flipEqualities x
295--DONT run vToP first!
296-- | Try to match two types together in a unification-like procedure.
297-- Returns a list of types and their minimum scores, sorted in order
298-- of increasing score.
299matchTypesBulk :: forall info. IState -> Int -> Type -> [(info, Type)] -> [(info, Score)]
300matchTypesBulk istate maxScore type1 types = getAllResults startQueueOfQueues where
301 getStartQueues :: (info, Type) -> Maybe (Score, (info, Q.PQueue Score State))
302 getStartQueues nty@(info, type2) = case mapMaybe startStates ty2s of
304 xs -> Just (minimum (map fst xs), (info, Q.fromList xs))
306 ty2s = (\(i, dag) (j, retTy) -> (i + j, dag, retTy))
307 <$> flipEqualitiesDag dag2 <*> flipEqualities retTy2
308 flipEqualitiesDag dag = case dag of
310 ((n, ty), (pos, deps)) : xs ->
311 (\(i, ty') (j, xs') -> (i + j , ((n, ty'), (pos, deps)) : xs'))
312 <$> flipEqualities ty <*> flipEqualitiesDag xs
313 startStates (numEqFlips, sndDag, sndRetTy) = do
314 state <- unifyQueue (State startingHoles
315 (Sided (dag1, typeClassArgs1) (sndDag, typeClassArgs2))
316 (mempty { equalityFlips = numEqFlips }) usedns) [(retTy1, sndRetTy)]
317 return (score state, state)
320 (dag2, typeClassArgs2, retTy2) = makeDag (uniqueBinders (map fst argNames1) type2)
321 argNames2 = map fst dag2
322 usedns = map fst startingHoles
323 startingHoles = argNames1 ++ argNames2
325 startingTypes = [(retTy1, retTy2)]
328 startQueueOfQueues :: Q.PQueue Score (info, Q.PQueue Score State)
329 startQueueOfQueues = Q.fromList $ mapMaybe getStartQueues types
331 getAllResults :: Q.PQueue Score (info, Q.PQueue Score State) -> [(info, Score)]
332 getAllResults q = case Q.minViewWithKey q of
334 Just ((nextScore, (info, stateQ)), q') ->
335 if defaultScoreFunction nextScore <= maxScore
336 then case nextStepsQueue stateQ of
337 Nothing -> getAllResults q'
338 Just (Left stateQ') -> case Q.minViewWithKey stateQ' of
339 Nothing -> getAllResults q'
340 Just ((newQscore,_), _) -> getAllResults (Q.add newQscore (info, stateQ') q')
341 Just (Right theScore) -> (info, theScore) : getAllResults q'
345 ctxt = tt_ctxt istate
346 classInfo = idris_classes istate
348 (dag1, typeClassArgs1, retTy1) = makeDag type1
349 argNames1 = map fst dag1
350 makeDag :: Type -> (ArgsDAG, Classes, Type)
351 makeDag = first3 (zipWith (\i (ty, deps) -> (ty, (i, deps))) [0..] . reverseDag) .
352 computeDagP (isTypeClassArg classInfo) . vToP
353 first3 f (a,b,c) = (f a, b, c)
355 -- update our state with the unification resolutions
356 resolveUnis :: [(Name, Type)] -> State -> Maybe (State, [(Type, Type)])
357 resolveUnis [] state = Just (state, [])
358 resolveUnis ((name, term@(P Bound name2 _)) : xs)
359 state | isJust findArgs = do
360 ((ty1, ix1), (ty2, ix2)) <- findArgs
362 (state'', queue) <- resolveUnis xs state'
363 let transScore = fromMaybe 0 (abs <$> ((-) <$> ix1 <*> ix2))
364 return (inScore (\s -> s { transposition = transposition s + transScore }) state'', (ty1, ty2) : queue)
366 unresolved = argsAndClasses state
367 inScore f stat = stat { score = f (score stat) }
368 findArgs = ((,) <$> findName name (left unresolved) <*> findName name2 (right unresolved)) <|>
369 ((,) <$> findName name2 (left unresolved) <*> findName name (right unresolved))
370 matchnames = [name, name2]
371 deleteArgs = deleteName name . deleteName name2
372 state' = state { holes = filter (not . (`elem` matchnames) . fst) (holes state)
373 , argsAndClasses = both (modifyTypes (subst name term) . deleteArgs) unresolved}
375 resolveUnis ((name, term) : xs)
376 state@(State hs unresolved _ _) = case both (findName name) unresolved of
377 Sided Nothing Nothing -> Nothing
378 Sided (Just _) (Just _) -> error "Idris internal error: TypeSearch.resolveUnis"
379 oneOfEach -> first (addScore (both scoreFor oneOfEach)) <$> nextStep
381 scoreFor (Just _) = mempty { argApp = 1 }
382 scoreFor Nothing = mempty { argApp = otherApplied }
383 -- find variables which are determined uniquely by the type
384 -- due to injectivity
385 matchedVarMap = usedVars term
386 bothT f (x, y) = (f x, f y)
387 (injUsedVars, notInjUsedVars) = bothT M.keys . M.partition id . M.filterWithKey (\k _-> k `elem` map fst hs) $ M.map snd matchedVarMap
388 varsInTy = injUsedVars ++ notInjUsedVars
389 toDelete = name : varsInTy
390 deleteMany = foldr (.) id (map deleteName toDelete)
392 otherApplied = length notInjUsedVars
394 addScore additions theState = theState { score = let s = score theState in
395 s { asymMods = asymMods s `mappend` additions } }
396 state' = state { holes = filter (not . (`elem` toDelete) . fst) hs
397 , argsAndClasses = both (modifyTypes (subst name term) . deleteMany) (argsAndClasses state) }
398 nextStep = resolveUnis xs state'
401 -- | resolve a queue of unification constraints
402 unifyQueue :: State -> [(Type, Type)] -> Maybe State
403 unifyQueue state [] = return state
404 unifyQueue state ((ty1, ty2) : queue) = do
405 --trace ("go: \n" ++ show state) True `seq` return ()
406 res <- tcToMaybe $ match_unify ctxt [ (n, Pi ty (TType (UVar 0))) | (n, ty) <- holes state] ty1 ty2 [] (map fst $ holes state) []
407 (state', queueAdditions) <- resolveUnis res state
408 guard $ scoreCriterion (score state')
409 unifyQueue state' (queue ++ queueAdditions)
411 possClassInstances :: [Name] -> Type -> [Type]
412 possClassInstances usedns ty = do
413 className <- getClassName clss
414 classDef <- lookupCtxt className classInfo
415 n <- class_instances classDef
416 def <- lookupCtxt n (definitions ctxt)
417 nty <- normaliseC ctxt [] <$> (case typeFromDef def of Just x -> [x]; Nothing -> [])
418 let ty' = vToP (uniqueBinders usedns nty)
421 (clss, _) = unApply ty
422 getClassName (P (TCon _ _) className _) = [className]
425 -- Just if the computation hasn't totally failed yet, Nothing if it has
426 -- Left if we haven't found a terminal state, Right if we have
427 nextStepsQueue :: Q.PQueue Score State -> Maybe (Either (Q.PQueue Score State) Score)
428 nextStepsQueue queue = do
429 ((nextScore, next), rest) <- Q.minViewWithKey queue
431 then Just $ Right nextScore
432 else let additions = if scoreCriterion nextScore
433 then Q.fromList [ (score state, state) | state <- nextSteps next ]
435 Just $ Left (Q.union rest additions)
437 isFinal (State [] (Sided ([], []) ([], [])) _ _) = True
440 -- | Find all possible matches starting from a given state.
441 -- We go in stages rather than concatenating all three results in hopes of narrowing
442 -- the search tree. Once we advance in a phase, there should be no going back.
443 nextSteps :: State -> [State]
445 -- Stage 3 - match typeclasses
446 nextSteps (State [] unresolved@(Sided ([], c1) ([], c2)) scoreAcc usedns) =
447 if null results3 then results4 else results3
449 -- try to match a typeclass argument from the left with a typeclass argument from the right
451 catMaybes [ unifyQueue (State []
452 (Sided ([], deleteFromArgList n1 c1)
453 ([], map (second subst2for1) (deleteFromArgList n2 c2)))
454 scoreAcc usedns) [(ty1, ty2)]
455 | (n1, ty1) <- c1, (n2, ty2) <- c2, let subst2for1 = psubst n2 (P Bound n1 ty1)]
457 -- try to hunt match a typeclass constraint by replacing it with an instance
458 results4 = [ State [] (both (\(cs, _, _) -> ([], cs)) sds)
459 (scoreAcc `mappend` Score 0 0 (both (\(_, amods, _) -> amods) sds))
460 (usedns ++ sided (++) (both (\(_, _, hs) -> hs) sds))
463 allMods = parallel defMod mods
464 mods :: Sided [( Classes, AsymMods, [Name] )]
465 mods = both (instanceMods . snd) unresolved
466 defMod :: Sided (Classes, AsymMods, [Name])
467 defMod = both (\(_, cs) -> (cs, mempty , [])) unresolved
468 parallel :: Sided a -> Sided [a] -> [Sided a]
469 parallel (Sided l r) (Sided ls rs) = map (flip Sided r) ls ++ map (Sided l) rs
470 instanceMods :: Classes -> [( Classes , AsymMods, [Name] )]
471 instanceMods classes = [ ( newClassArgs, mempty { typeClassApp = 1 }, newHoles )
473 , inst <- possClassInstances usedns ty
474 , newClassArgs <- maybeToList $ typeclassUnify classInfo ctxt ty inst
475 , let newHoles = map fst newClassArgs ]
478 -- Stage 1 - match arguments
479 nextSteps (State hs (Sided (dagL, c1) (dagR, c2)) scoreAcc usedns) = results where
481 results = concatMap takeSomeClasses results1
483 -- we only try to match arguments whose names don't appear in the types
484 -- of any other arguments
485 canBeFirst :: ArgsDAG -> [(Name, Type)]
486 canBeFirst = map fst . filter (S.null . snd . snd)
488 -- try to match an argument from the left with an argument from the right
489 results1 = catMaybes [ unifyQueue (State (filter (not . (`elem` [n1,n2]) . fst) hs)
490 (Sided (deleteFromDag n1 dagL, c1)
491 (inArgTys subst2for1 $ deleteFromDag n2 dagR, map (second subst2for1) c2))
492 scoreAcc usedns) [(ty1, ty2)]
493 | (n1, ty1) <- canBeFirst dagL, (n2, ty2) <- canBeFirst dagR
494 , let subst2for1 = psubst n2 (P Bound n1 ty1)]
498 -- Stage 2 - simply introduce a subset of the typeclasses
499 -- we've finished, so take some classes
500 takeSomeClasses (State [] unresolved@(Sided ([], _) ([], _)) scoreAcc usedns) =
501 map statesFromMods . prod $ both (classMods . snd) unresolved
503 swap (Sided l r) = Sided r l
504 statesFromMods :: Sided (Classes, AsymMods) -> State
505 statesFromMods sides = let classes = both (\(c, _) -> ([], c)) sides
506 mods = swap (both snd sides) in
507 State [] classes (scoreAcc `mappend` (mempty { asymMods = mods })) usedns
508 classMods :: Classes -> [(Classes, AsymMods)]
509 classMods cs = let lcs = length cs in
510 [ (cs', mempty { typeClassIntro = lcs - length cs' }) | cs' <- subsets cs ]
511 prod :: Sided [a] -> [Sided a]
512 prod (Sided ls rs) = [Sided l r | l <- ls, r <- rs]
513 -- still have arguments to match, so just be the identity
514 takeSomeClasses s = [s]
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