@@ -16,6 +16,7 @@ import Development.IDE.Spans.LocalBindings
16
16
import OccName
17
17
import SrcLoc
18
18
import Type
19
+ import Wingman.GHC (algebraicTyCon )
19
20
import Wingman.Types
20
21
21
22
@@ -61,7 +62,10 @@ withNewGoal t = field @"_jGoal" .~ t
61
62
62
63
63
64
introduce :: Hypothesis a -> Judgement' a -> Judgement' a
64
- introduce hy = field @ " _jHypothesis" <>~ hy
65
+ -- NOTE(sandy): It's important that we put the new hypothesis terms first,
66
+ -- since 'jAcceptableDestructTargets' will never destruct a pattern that occurs
67
+ -- after a previously-destructed term.
68
+ introduce hy = field @ " _jHypothesis" %~ mappend hy
65
69
66
70
67
71
------------------------------------------------------------------------------
@@ -149,7 +153,10 @@ findPositionVal :: Judgement' a -> OccName -> Int -> Maybe OccName
149
153
findPositionVal jdg defn pos = listToMaybe $ do
150
154
-- It's important to inspect the entire hypothesis here, as we need to trace
151
155
-- ancstry through potentially disallowed terms in the hypothesis.
152
- (name, hi) <- M. toList $ M. map (overProvenance expandDisallowed) $ hyByName $ jEntireHypothesis jdg
156
+ (name, hi) <- M. toList
157
+ $ M. map (overProvenance expandDisallowed)
158
+ $ hyByName
159
+ $ jEntireHypothesis jdg
153
160
case hi_provenance hi of
154
161
TopLevelArgPrv defn' pos' _
155
162
| defn == defn'
@@ -238,12 +245,13 @@ patternHypothesis scrutinee dc jdg
238
245
= introduceHypothesis $ \ _ pos ->
239
246
PatternMatchPrv $
240
247
PatVal
241
- scrutinee
242
- (maybe mempty
243
- (\ scrut -> S. singleton scrut <> getAncestry jdg scrut)
244
- scrutinee)
245
- (Uniquely dc)
246
- pos
248
+ scrutinee
249
+ (maybe
250
+ mempty
251
+ (\ scrut -> S. singleton scrut <> getAncestry jdg scrut)
252
+ scrutinee)
253
+ (Uniquely dc)
254
+ pos
247
255
248
256
249
257
------------------------------------------------------------------------------
@@ -285,6 +293,21 @@ jLocalHypothesis
285
293
. jHypothesis
286
294
287
295
296
+ ------------------------------------------------------------------------------
297
+ -- | Given a judgment, return the hypotheses that are acceptable to destruct.
298
+ --
299
+ -- We use the ordering of the hypothesis for this purpose. Since new bindings
300
+ -- are always inserted at the beginning, we can impose a canonical ordering on
301
+ -- which order to try destructs by what order they are introduced --- stopping
302
+ -- at the first one we've already destructed.
303
+ jAcceptableDestructTargets :: Judgement' CType -> [HyInfo CType ]
304
+ jAcceptableDestructTargets
305
+ = filter (isJust . algebraicTyCon . unCType . hi_type)
306
+ . takeWhile (not . isAlreadyDestructed . hi_provenance)
307
+ . unHypothesis
308
+ . jEntireHypothesis
309
+
310
+
288
311
------------------------------------------------------------------------------
289
312
-- | If we're in a top hole, the name of the defining function.
290
313
isTopHole :: Context -> Judgement' a -> Maybe OccName
@@ -391,6 +414,12 @@ isDisallowed :: Provenance -> Bool
391
414
isDisallowed DisallowedPrv {} = True
392
415
isDisallowed _ = False
393
416
417
+ ------------------------------------------------------------------------------
418
+ -- | Has this term already been disallowed?
419
+ isAlreadyDestructed :: Provenance -> Bool
420
+ isAlreadyDestructed (DisallowedPrv AlreadyDestructed _) = True
421
+ isAlreadyDestructed _ = False
422
+
394
423
395
424
------------------------------------------------------------------------------
396
425
-- | Eliminates 'DisallowedPrv' provenances.
0 commit comments