Skip to content

Commit 235ef53

Browse files
committed
Use a canonical ordering when destructing terms
1 parent f7ea9e5 commit 235ef53

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

plugins/hls-tactics-plugin/src/Wingman/Judgements.hs

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import Development.IDE.Spans.LocalBindings
1616
import OccName
1717
import SrcLoc
1818
import Type
19+
import Wingman.GHC (algebraicTyCon)
1920
import Wingman.Types
2021

2122

@@ -149,7 +150,10 @@ findPositionVal :: Judgement' a -> OccName -> Int -> Maybe OccName
149150
findPositionVal jdg defn pos = listToMaybe $ do
150151
-- It's important to inspect the entire hypothesis here, as we need to trace
151152
-- ancstry through potentially disallowed terms in the hypothesis.
152-
(name, hi) <- M.toList $ M.map (overProvenance expandDisallowed) $ hyByName $ jEntireHypothesis jdg
153+
(name, hi) <- M.toList
154+
$ M.map (overProvenance expandDisallowed)
155+
$ hyByName
156+
$ jEntireHypothesis jdg
153157
case hi_provenance hi of
154158
TopLevelArgPrv defn' pos' _
155159
| defn == defn'
@@ -238,12 +242,13 @@ patternHypothesis scrutinee dc jdg
238242
= introduceHypothesis $ \_ pos ->
239243
PatternMatchPrv $
240244
PatVal
241-
scrutinee
242-
(maybe mempty
243-
(\scrut -> S.singleton scrut <> getAncestry jdg scrut)
244-
scrutinee)
245-
(Uniquely dc)
246-
pos
245+
scrutinee
246+
(maybe
247+
mempty
248+
(\scrut -> S.singleton scrut <> getAncestry jdg scrut)
249+
scrutinee)
250+
(Uniquely dc)
251+
pos
247252

248253

249254
------------------------------------------------------------------------------
@@ -285,6 +290,21 @@ jLocalHypothesis
285290
. jHypothesis
286291

287292

293+
------------------------------------------------------------------------------
294+
-- | Given a judgment, return the hypotheses that are acceptable to destruct.
295+
--
296+
-- We use the ordering of the hypothesis for this purpose. Since new bindings
297+
-- are always inserted at the beginning, we can impose a canonical ordering on
298+
-- which order to try destructs by what order they are introduced --- stopping
299+
-- at the first one we've already destructed.
300+
jAcceptableDestructTargets :: Judgement' CType -> [HyInfo CType]
301+
jAcceptableDestructTargets
302+
= filter (isJust . algebraicTyCon . unCType . hi_type)
303+
. takeWhile (not . isAlreadyDestructed . hi_provenance)
304+
. unHypothesis
305+
. jEntireHypothesis
306+
307+
288308
------------------------------------------------------------------------------
289309
-- | If we're in a top hole, the name of the defining function.
290310
isTopHole :: Context -> Judgement' a -> Maybe OccName
@@ -391,6 +411,12 @@ isDisallowed :: Provenance -> Bool
391411
isDisallowed DisallowedPrv{} = True
392412
isDisallowed _ = False
393413

414+
------------------------------------------------------------------------------
415+
-- | Has this term already been disallowed?
416+
isAlreadyDestructed :: Provenance -> Bool
417+
isAlreadyDestructed (DisallowedPrv AlreadyDestructed _) = True
418+
isAlreadyDestructed _ = False
419+
394420

395421
------------------------------------------------------------------------------
396422
-- | Eliminates 'DisallowedPrv' provenances.

plugins/hls-tactics-plugin/src/Wingman/Tactics.hs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -358,9 +358,7 @@ overFunctions =
358358

359359
overAlgebraicTerms :: (HyInfo CType -> TacticsM ()) -> TacticsM ()
360360
overAlgebraicTerms =
361-
attemptOn $ filter (isJust . algebraicTyCon . unCType . hi_type)
362-
. unHypothesis
363-
. jHypothesis
361+
attemptOn jAcceptableDestructTargets
364362

365363

366364
allNames :: Judgement -> Set OccName

0 commit comments

Comments
 (0)