Skip to content

Commit a56067a

Browse files
committed
Properly scope GADT equality evidence in the judgment
Fixes haskell#1937
1 parent 3f1a7df commit a56067a

File tree

8 files changed

+21
-28
lines changed

8 files changed

+21
-28
lines changed

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,6 @@ destructMatches use_field_puns f scrut t jdg = do
6868
-- #syn_scoped
6969
method_hy = foldMap evidenceToHypothesis ev
7070
args = conLikeInstOrigArgTys' con apps
71-
modify $ evidenceToSubst ev
72-
subst <- gets ts_unifier
7371
ctx <- ask
7472

7573
let names_in_scope = hyNamesInScope hy
@@ -80,7 +78,7 @@ destructMatches use_field_puns f scrut t jdg = do
8078
let hy' = patternHypothesis scrut con jdg
8179
$ zip names'
8280
$ coerce args
83-
j = fmap (CType . substTyAddInScope subst . unCType)
81+
j = withNewCoercions (evidenceToCoercions ev)
8482
$ introduce ctx hy'
8583
$ introduce ctx method_hy
8684
$ withNewGoal g jdg

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ withNewGoal :: a -> Judgement' a -> Judgement' a
6969
withNewGoal t = field @"_jGoal" .~ t
7070

7171

72+
withNewCoercions :: [(a, a)] -> Judgement' a -> Judgement' a
73+
withNewCoercions ev = field @"j_coercion" <>~ ev
74+
75+
7276
normalizeHypothesis :: Functor f => Context -> f CType -> f CType
7377
normalizeHypothesis = fmap . coerce . normalizeType
7478

@@ -414,6 +418,7 @@ mkFirstJudgement ctx hy top goal =
414418
, _jWhitelistSplit = True
415419
, _jIsTopHole = top
416420
, _jGoal = CType goal
421+
, j_coercion = mempty
417422
}
418423

419424

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@ module Wingman.Judgements.Theta
55
( Evidence
66
, getEvidenceAtHole
77
, mkEvidence
8+
, evidenceToCoercions
89
, evidenceToSubst
910
, evidenceToHypothesis
1011
, evidenceToThetaType
12+
, allEvidenceToSubst
1113
) where
1214

1315
import Class (classTyVars)
1416
import Control.Applicative (empty)
1517
import Control.Lens (preview)
18+
import Data.Coerce (coerce)
1619
import Data.Maybe (fromMaybe, mapMaybe, maybeToList)
1720
import Data.Generics.Sum (_Ctor)
1821
import Data.Set (Set)
@@ -109,6 +112,11 @@ allEvidenceToSubst skolems ((a, b) : evs) =
109112
$ allEvidenceToSubst skolems
110113
$ fmap (substPair subst) evs
111114

115+
------------------------------------------------------------------------------
116+
-- | Update our knowledge of which types are equal.
117+
evidenceToCoercions :: [Evidence] -> [(CType, CType)]
118+
evidenceToCoercions = coerce . mapMaybe (preview $ _Ctor @"EqualityOfTypes")
119+
112120
------------------------------------------------------------------------------
113121
-- | Update our knowledge of which types are equal.
114122
evidenceToSubst :: [Evidence] -> TacticState -> TacticState

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import Type (tyCoVarsOfTypeWellScoped)
3232
import Wingman.Context (getInstance)
3333
import Wingman.GHC (tryUnifyUnivarsButNotSkolems, updateSubst)
3434
import Wingman.Judgements
35+
import Wingman.Judgements.Theta (allEvidenceToSubst)
3536
import Wingman.Simplify (simplify)
3637
import Wingman.Types
3738

@@ -48,10 +49,12 @@ newSubgoal
4849
-> Rule
4950
newSubgoal j = do
5051
ctx <- ask
52+
skolems <- gets ts_skolems
53+
let coercions = allEvidenceToSubst skolems $ coerce $ j_coercion j
5154
unifier <- gets ts_unifier
5255
subgoal
5356
$ normalizeJudgement ctx
54-
$ substJdg unifier
57+
$ substJdg (unionTCvSubst unifier coercions)
5558
$ unsetIsTopHole
5659
$ normalizeJudgement ctx j
5760

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ data Judgement' a = Judgement
297297
, _jWhitelistSplit :: !Bool
298298
, _jIsTopHole :: !Bool
299299
, _jGoal :: !a
300+
, j_coercion :: [(a, a)]
300301
}
301302
deriving stock (Eq, Generic, Functor, Show)
302303

plugins/hls-tactics-plugin/test/CodeAction/RunMetaprogramSpec.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ spec = do
3333
metaTest 11 11 "MetaUseMethod"
3434
metaTest 9 38 "MetaCataCollapse"
3535
metaTest 7 16 "MetaCataCollapseUnary"
36-
metaTest 21 32 "MetaCataAST"
36+
metaTest 10 32 "MetaCataAST"
3737
metaTest 6 46 "MetaPointwise"
3838
metaTest 4 28 "MetaUseSymbol"

plugins/hls-tactics-plugin/test/golden/MetaCataAST.expected.hs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,8 @@ data AST a where
77
Equal :: AST a -> AST a -> AST Bool
88

99
eval :: AST a -> a
10-
-- NOTE(sandy): There is an unrelated bug that is shown off in this test
11-
-- namely, that
12-
--
13-
-- @eval (IntLit n) = _@
14-
--
15-
-- but should be
16-
--
17-
-- @eval (IntLit n) = n@
18-
--
19-
-- https://github.com/haskell/haskell-language-server/issues/1937
20-
2110
eval (BoolLit b) = b
22-
eval (IntLit n) = _
11+
eval (IntLit n) = n
2312
eval (If ast ast' ast_a)
2413
= let
2514
ast_c = eval ast

plugins/hls-tactics-plugin/test/golden/MetaCataAST.hs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,5 @@ data AST a where
77
Equal :: AST a -> AST a -> AST Bool
88

99
eval :: AST a -> a
10-
-- NOTE(sandy): There is an unrelated bug that is shown off in this test
11-
-- namely, that
12-
--
13-
-- @eval (IntLit n) = _@
14-
--
15-
-- but should be
16-
--
17-
-- @eval (IntLit n) = n@
18-
--
19-
-- https://github.com/haskell/haskell-language-server/issues/1937
20-
2110
eval = [wingman| intros x, cata x; collapse |]
2211

0 commit comments

Comments
 (0)