Skip to content

Commit 779f2b7

Browse files
committed
WIP
1 parent 158400a commit 779f2b7

File tree

8 files changed

+61
-62
lines changed

8 files changed

+61
-62
lines changed

parser-typechecker/src/Unison/Typechecker/Context.hs

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ data InfoNote v loc
367367
-- job to use the binding with the smallest containing scope so as to respect variable
368368
-- shadowing.
369369
-- This is used in the LSP.
370-
VarBinding v (Type v loc)
370+
VarBinding v loc (Type v loc)
371371
| -- | The usage of a particular variable. We report the variable and its location so we can match a given source location with a specific symbol later in the LSP.
372372
VarMention v loc
373373
deriving (Show)
@@ -394,7 +394,7 @@ substituteSolved ::
394394
InfoNote v loc
395395
substituteSolved ctx = \case
396396
(SolvedBlank b v t) -> SolvedBlank b v (applyCtx ctx t)
397-
VarBinding v t -> VarBinding v (applyCtx ctx t)
397+
VarBinding v loc t -> VarBinding v loc (applyCtx ctx t)
398398
i -> i
399399

400400

@@ -815,7 +815,7 @@ extend' e c@(Context ctx) = Context . (: ctx) . (e,) <$> i'
815815
extend :: (Var v) => Element v loc -> Context v loc -> M v loc (Context v loc)
816816
extend e c = do
817817
case e of
818-
Ann v _loc t -> noteVarBinding v t
818+
Ann v loc t -> noteVarBinding v loc t
819819
_ -> pure ()
820820
either compilerCrash pure $ extend' e c
821821

@@ -1122,8 +1122,8 @@ noteTopLevelType e binding typ = case binding of
11221122
-- | Take note of the types and locations of all bindings, including let bindings, letrec
11231123
-- bindings, lambda argument bindings and top-level bindings.
11241124
-- This information is used to provide information to the LSP after typechecking.
1125-
noteVarBinding :: (Var v) => v -> Type v loc -> M v loc ()
1126-
noteVarBinding v t = btw $ VarBinding v t
1125+
noteVarBinding :: (Var v) => v -> loc -> Type v loc -> M v loc ()
1126+
noteVarBinding v loc t = btw $ VarBinding v loc t
11271127

11281128
noteVarMention :: (Var v) => v -> loc -> M v loc ()
11291129
noteVarMention v loc = do
@@ -1237,20 +1237,13 @@ synthesizeWanted (Term.Constructor' r) =
12371237
synthesizeWanted tm@(Term.Request' r) =
12381238
fmap (wantRequest tm) . ungeneralize . Type.purifyArrows
12391239
=<< getEffectConstructorType r
1240-
synthesizeWanted tm@(Term.Let1Top' top binding e) = do
1240+
synthesizeWanted (Term.Let1Top' top binding boundVarAnn e) = do
12411241
(tbinding, wb) <- synthesizeBinding top binding
12421242
v' <- ABT.freshen e freshenVar
12431243
when (Var.isAction (ABT.variable e)) $
12441244
-- enforce that actions in a block have type ()
12451245
subtype tbinding (DDB.unitType (ABT.annotation binding))
1246-
case tm of
1247-
outer@(ABT.Tm' (Term.Let _ rhs abs@(ABT.Term _ _ (ABT.Abs v' body)))) -> do
1248-
-- let innerAnn = ABT.annotation inner
1249-
-- tbinding' = ABT.annotation binding
1250-
Debug.debugM Debug.Temp "synthesizeWanted (let binding)" (v', ("outer" :: Text, anythingToString $ ABT.annotation outer), ("rhs" :: Text, anythingToString $ ABT.annotation rhs), ("abs" :: Text, anythingToString $ ABT.annotation abs), ("body" :: Text, anythingToString $ ABT.annotation body))
1251-
_ -> pure ()
1252-
appendContext [Ann v' (error "Unset Ann loc: synthesizeWanted") tbinding]
1253-
-- Debug.debugM Debug.Temp "synthesizeWanted (missing annotation)" (v', anythingToString $ ABT.annotation tm, binding)
1246+
appendContext [Ann v' boundVarAnn tbinding]
12541247
(t, w) <- synthesize (ABT.bindInheritAnnotation e (Term.var () v'))
12551248
t <- applyM t
12561249
when top $ noteTopLevelType e binding tbinding
@@ -1338,7 +1331,7 @@ synthesizeWanted e
13381331

13391332
-- ->I=> (Full Damas Milner rule)
13401333
-- | Term.Lam' body <- e = do
1341-
| tm@(ABT.Tm' (Term.Lam (ABT.Abs' body))) <- e = do
1334+
| (ABT.Tm' (Term.Lam (ABT.Abs' boundVarAnn body))) <- e = do
13421335
-- arya: are there more meaningful locations we could put into and
13431336
-- pull out of the abschain?)
13441337
[arg, i, e, o] <-
@@ -1351,14 +1344,8 @@ synthesizeWanted e
13511344
let it = existential' l B.Blank i
13521345
ot = existential' l B.Blank o
13531346
et = existential' l B.Blank e
1354-
case tm of
1355-
ABT.Term _ tmAnn (ABT.Tm (Term.Lam (ABT.Term _ absAnn (ABT.Abs v body)))) -> do
1356-
Debug.debugM Debug.Temp "Lambda binding anns" (v, anythingToString tmAnn, anythingToString absAnn, anythingToString $ ABT.annotation body)
1357-
_ -> pure ()
1358-
let annLoc = error "Unset Ann loc: synthesizeWanted"
13591347
appendContext $
1360-
[existential i, existential e, existential o, Ann arg annLoc it]
1361-
Debug.debugM Debug.Temp "tm arg loc (missing annotation)" arg
1348+
[existential i, existential e, existential o, Ann arg boundVarAnn it]
13621349

13631350
when (Var.typeOf i == Var.Delay) $ do
13641351
-- '(1 + 1) turns into a lambda with an arg variable of type Var.Delay
@@ -1689,7 +1676,6 @@ checkPattern scrutineeType p =
16891676
v <- getAdvance p
16901677
v' <- lift $ freshenVar v
16911678
lift . appendContext $ [Ann v' loc scrutineeType]
1692-
Debug.debugM Debug.Temp "Pattern binding anns" (v, anythingToString loc)
16931679
pure [(v, v')]
16941680
-- Ex: [42, y, Foo z]
16951681
Pattern.SequenceLiteral loc ps -> do
@@ -1777,7 +1763,6 @@ checkPattern scrutineeType p =
17771763
v <- getAdvance p
17781764
v' <- lift $ freshenVar v
17791765
lift . appendContext $ [Ann v' loc scrutineeType]
1780-
Debug.debugM Debug.Temp "As Pattern anns" (v, anythingToString loc)
17811766
((v, v') :) <$> checkPattern scrutineeType p'
17821767
-- ex: { a } -> a
17831768
-- ex: { (x, 42) } -> a
@@ -1913,7 +1898,7 @@ annotateLetRecBindings isTop letrec =
19131898
pure (Term.ann (loc binding) e t2, t2, vloc)
19141899
-- If we're not using an annotation, we make one up. There's 2 cases:
19151900

1916-
lam@(Term.Lam' _) ->
1901+
lam@(Term.Lam' {}) ->
19171902
-- If `e` is a lambda of arity K, we immediately refine the
19181903
-- existential to `a1 ->{e1} a2 ... ->{eK} r`. This gives better
19191904
-- inference of the lambda's ability variables in conjunction with
@@ -2476,29 +2461,23 @@ checkWanted want m (Type.Forall' body) = do
24762461
ABT.bindInheritAnnotation body (universal' () x)
24772462
-- =>I
24782463
-- Lambdas are pure, so they add nothing to the wanted set
2479-
checkWanted want (Term.Lam' body) (Type.Arrow'' i es o) = do
2480-
let annLoc = error "checkWanted: missing annotation"
2464+
checkWanted want (Term.Lam' boundVarAnn body) (Type.Arrow'' i es o) = do
24812465
x <- ABT.freshen body freshenVar
24822466
markThenRetract0 x $ do
2483-
Debug.debugM Debug.Temp "checkWanted:lam (missing annotation)" x
2484-
extendContext (Ann x annLoc i)
2467+
Debug.debugM Debug.Temp "checkWanted:Lam" (x, anythingToString boundVarAnn)
2468+
extendContext (Ann x boundVarAnn i)
24852469
body <- pure $ ABT.bindInheritAnnotation body (Term.var () x)
24862470
checkWithAbilities es body o
24872471
pure want
2488-
checkWanted want tm@(Term.Let1Top' top binding m) t = do
2489-
case tm of
2490-
outer@(ABT.Tm' (Term.Let _ rhs abs@(ABT.Term _ _ (ABT.Abs v' body)))) -> do
2491-
Debug.debugM Debug.Temp "checkWanted:Let" (v', ("outer" :: Text, anythingToString $ ABT.annotation outer), ("rhs" :: Text, anythingToString $ ABT.annotation rhs), ("abs" :: Text, anythingToString $ ABT.annotation abs), ("body" :: Text, anythingToString $ ABT.annotation body))
2492-
_ -> pure ()
2493-
let annLoc = error "checkWanted: missing annotation"
2472+
checkWanted want (Term.Let1Top' top binding boundVarAnn m) t = do
24942473
(tbinding, wbinding) <- synthesizeBinding top binding
24952474
want <- coalesceWanted wbinding want
24962475
v <- ABT.freshen m freshenVar
24972476
markThenRetractWanted v $ do
24982477
when (Var.isAction (ABT.variable m)) $
24992478
-- enforce that actions in a block have type ()
25002479
subtype tbinding (DDB.unitType (ABT.annotation binding))
2501-
extendContext (Ann v annLoc tbinding)
2480+
extendContext (Ann v boundVarAnn tbinding)
25022481
checkWanted want (ABT.bindInheritAnnotation m (Term.var () v)) t
25032482
checkWanted want (Term.LetRecNamed' [] m) t =
25042483
checkWanted want m t

unison-cli/src/Unison/LSP/FileAnalysis.hs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,26 @@ checkFile doc = runMaybeT do
121121
& Foldable.toList
122122
& reverse -- Type notes that come later in typechecking have more information filled in.
123123
& foldMap \case
124-
Result.TypeInfo (Context.VarBinding v typ) -> Map.singleton v typ
124+
Result.TypeInfo (Context.VarBinding v _loc typ) -> Map.singleton v typ
125125
_ -> mempty
126126
& pure
127+
128+
let allVarBindings =
129+
typecheckingNotes
130+
& Foldable.toList
131+
& reverse -- Type notes that come later in typechecking have more information filled in.
132+
& foldMap \case
133+
Result.TypeInfo (Context.VarBinding v loc _typ) -> [(v, loc)]
134+
_ -> mempty
135+
Debug.debugM Debug.Temp "allVarBindings" allVarBindings
136+
127137
let allVarMentions =
128138
typecheckingNotes
129139
& Foldable.toList
130140
& reverse -- Type notes that come later in typechecking have more information filled in.
131141
& foldMap \case
132142
Result.TypeInfo (Context.VarMention v loc) -> [(v, loc)]
143+
Result.TypeInfo (Context.VarBinding v loc _) -> [(v, loc)]
133144
_ -> mempty
134145
Debug.debugM Debug.Temp "allVarMentions" allVarMentions
135146
Debug.debugM Debug.Temp "symbolTypes" symbolTypes

unison-cli/src/Unison/LSP/Hover.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,10 @@ hoverInfo uri pos =
128128
node <- LSPQ.nodeAtPosition uri pos
129129
Debug.debugM Debug.Temp "node" node
130130
case node of
131+
-- Var mentions
131132
LSPQ.TermNode (Term.Var' v) -> pure $ v
133+
-- Var bindings
134+
LSPQ.TermNode (ABT.Abs'' v _body) -> pure $ v
132135
LSPQ.TermNode {} -> empty
133136
LSPQ.TypeNode {} -> empty
134137
LSPQ.PatternNode _pat -> empty

unison-cli/src/Unison/LSP/Queries.hs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,12 @@ instance Functor SourceNode where
198198
-- children contain that position.
199199
findSmallestEnclosingNode :: Pos -> Term Symbol Ann -> Maybe (SourceNode Ann)
200200
findSmallestEnclosingNode pos term
201-
| -- Abs nodes annotate the location of the var being bound, not the body of the binding, so we just skip over them.
202-
ABT.Abs'' _ body <- term =
203-
findSmallestEnclosingNode pos body
201+
| ABT.Term _ absAnn (ABT.Abs _ body) <- term =
202+
-- Abs nodes annotate the location of the var being bound, not the body of the binding, so we either match on
203+
-- the binding, or skip over them to the body.
204+
if absAnn `Ann.contains` pos
205+
then Just (TermNode term)
206+
else findSmallestEnclosingNode pos body
204207
| annIsFilePosition ann && not (ann `Ann.contains` pos) = Nothing
205208
| Just r <- cleanImplicitUnit term = findSmallestEnclosingNode pos r
206209
| otherwise = do

unison-core/src/Unison/ABT.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,8 @@ pattern Cycle'' t <- Term _ _ (Cycle t)
230230
pattern Abs'' :: v -> Term f v a -> Term f v a
231231
pattern Abs'' v body <- Term _ _ (Abs v body)
232232

233-
pattern Abs' :: (Foldable f, Functor f, Var v) => Subst f v a -> Term f v a
234-
pattern Abs' subst <- (unabs1 -> Just subst)
233+
pattern Abs' :: (Foldable f, Functor f, Var v) => a -> Subst f v a -> Term f v a
234+
pattern Abs' absAnn subst <- (unabs1 -> Just (absAnn, subst))
235235

236236
pattern CycleA' :: a -> [(a, v)] -> Term f v a -> Term f v a
237237
pattern CycleA' a avs t <- Term _ a (Cycle (AbsNA' avs t))
@@ -469,8 +469,8 @@ data Subst f v a = Subst
469469
variable :: v
470470
}
471471

472-
unabs1 :: forall a f v. (Foldable f, Functor f, Var v) => Term f v a -> Maybe (Subst f v a)
473-
unabs1 (Term _ _ (Abs v body)) = Just (Subst freshen bind bindInheritAnnotation v)
472+
unabs1 :: forall a f v. (Foldable f, Functor f, Var v) => Term f v a -> Maybe (a, Subst f v a)
473+
unabs1 (Term _ absAnn (Abs v body)) = Just (absAnn, Subst freshen bind bindInheritAnnotation v)
474474
where
475475
freshen :: (v -> t) -> t
476476
freshen f = f v

unison-core/src/Unison/Term.hs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ pattern Abs' ::
469469
(Foldable f, Functor f, ABT.Var v) =>
470470
ABT.Subst f v a ->
471471
ABT.Term f v a
472-
pattern Abs' subst <- ABT.Abs' subst
472+
pattern Abs' subst <- ABT.Abs' _absAnn subst
473473

474474
pattern Int' :: Int64 -> ABT.Term (F typeVar typeAnn patternAnn) v a
475475
pattern Int' n <- (ABT.out -> ABT.Tm (Int n))
@@ -620,9 +620,10 @@ pattern List' xs <- (ABT.out -> ABT.Tm (List xs))
620620

621621
pattern Lam' ::
622622
(ABT.Var v) =>
623+
a ->
623624
ABT.Subst (F typeVar typeAnn patternAnn) v a ->
624625
ABT.Term (F typeVar typeAnn patternAnn) v a
625-
pattern Lam' subst <- ABT.Tm' (Lam (ABT.Abs' subst))
626+
pattern Lam' absAnn subst <- ABT.Tm' (Lam (ABT.Abs' absAnn subst))
626627

627628
pattern Delay' :: (Var v) => Term2 vt at ap v a -> Term2 vt at ap v a
628629
pattern Delay' body <- (unDelay -> Just body)
@@ -659,17 +660,19 @@ pattern LamsNamedOrDelay' vs body <- (unLamsUntilDelay' -> Just (vs, body))
659660
pattern Let1' ::
660661
(Var v) =>
661662
Term' vt v a ->
663+
a ->
662664
ABT.Subst (F vt a a) v a ->
663665
Term' vt v a
664-
pattern Let1' b subst <- (unLet1 -> Just (_, b, subst))
666+
pattern Let1' b bindNameAnn subst <- (unLet1 -> Just (_, b, bindNameAnn, subst))
665667

666668
pattern Let1Top' ::
667669
(Var v) =>
668670
IsTop ->
669671
Term' vt v a ->
672+
a ->
670673
ABT.Subst (F vt a a) v a ->
671674
Term' vt v a
672-
pattern Let1Top' top b subst <- (unLet1 -> Just (top, b, subst))
675+
pattern Let1Top' top b bindNameAnn subst <- (unLet1 -> Just (top, b, bindNameAnn, subst))
673676

674677
pattern Let1Named' ::
675678
v ->
@@ -1060,8 +1063,8 @@ singleLet isTop spanAnn absAnn (v, body) e = ABT.tm' spanAnn (Let isTop body (AB
10601063
unLet1 ::
10611064
(Var v) =>
10621065
Term' vt v a ->
1063-
Maybe (IsTop, Term' vt v a, ABT.Subst (F vt a a) v a)
1064-
unLet1 (ABT.Tm' (Let isTop b (ABT.Abs' subst))) = Just (isTop, b, subst)
1066+
Maybe (IsTop, Term' vt v a, a, ABT.Subst (F vt a a) v a)
1067+
unLet1 (ABT.Tm' (Let isTop b (ABT.Abs' absAnn subst))) = Just (isTop, b, absAnn, subst)
10651068
unLet1 _ = Nothing
10661069

10671070
-- | Satisfies `unLet (let' bs e) == Just (bs, e)`
@@ -1374,7 +1377,7 @@ updateDependencies termUpdates typeUpdates = ABT.rebuildUp go
13741377
-- | If the outermost term is a function application,
13751378
-- perform substitution of the argument into the body
13761379
betaReduce :: (Var v) => Term0 v -> Term0 v
1377-
betaReduce (App' (Lam' f) arg) = ABT.bind f arg
1380+
betaReduce (App' (Lam' _absAnn f) arg) = ABT.bind f arg
13781381
betaReduce e = e
13791382

13801383
betaNormalForm :: (Var v) => Term0 v -> Term0 v

unison-core/src/Unison/Type.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,10 @@ pattern Effect0' :: (Ord v) => [Type v a] -> Type v a -> Type v a
153153
pattern Effect0' es t <- (unEffect0 -> (es, t))
154154

155155
pattern Forall' :: (ABT.Var v) => ABT.Subst F v a -> ABT.Term F v a
156-
pattern Forall' subst <- ABT.Tm' (Forall (ABT.Abs' subst))
156+
pattern Forall' subst <- ABT.Tm' (Forall (ABT.Abs' _ subst))
157157

158158
pattern IntroOuter' :: (ABT.Var v) => ABT.Subst F v a -> ABT.Term F v a
159-
pattern IntroOuter' subst <- ABT.Tm' (IntroOuter (ABT.Abs' subst))
159+
pattern IntroOuter' subst <- ABT.Tm' (IntroOuter (ABT.Abs' _ subst))
160160

161161
pattern IntroOuterNamed' :: v -> ABT.Term F v a -> ABT.Term F v a
162162
pattern IntroOuterNamed' v body <- ABT.Tm' (IntroOuter (ABT.out -> ABT.Abs v body))
@@ -181,7 +181,7 @@ pattern Cycle' :: [v] -> f (ABT.Term f v a) -> ABT.Term f v a
181181
pattern Cycle' xs t <- ABT.Cycle' xs t
182182

183183
pattern Abs' :: (Foldable f, Functor f, ABT.Var v) => ABT.Subst f v a -> ABT.Term f v a
184-
pattern Abs' subst <- ABT.Abs' subst
184+
pattern Abs' subst <- ABT.Abs' _ subst
185185

186186
unPure :: (Ord v) => Type v a -> Maybe (Type v a)
187187
unPure (Effect'' [] t) = Just t

unison-runtime/src/Unison/Runtime/ANF.hs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -377,14 +377,14 @@ beta rec (Apps' l@(LamsNamed' vs body) as)
377377
beta _ _ = Nothing
378378

379379
isStructured :: (Var v) => Term v a -> Bool
380-
isStructured (Var' _) = False
381-
isStructured (Lam' _) = False
382-
isStructured (Nat' _) = False
383-
isStructured (Int' _) = False
384-
isStructured (Float' _) = False
385-
isStructured (Text' _) = False
386-
isStructured (Char' _) = False
387-
isStructured (Constructor' _) = False
380+
isStructured (Var' {}) = False
381+
isStructured (Lam' {}) = False
382+
isStructured (Nat' {}) = False
383+
isStructured (Int' {}) = False
384+
isStructured (Float' {}) = False
385+
isStructured (Text' {}) = False
386+
isStructured (Char' {}) = False
387+
isStructured (Constructor' {}) = False
388388
isStructured (Apps' Constructor' {} args) = any isStructured args
389389
isStructured (If' b t f) =
390390
isStructured b || isStructured t || isStructured f

0 commit comments

Comments
 (0)