Skip to content

Commit

Permalink
generate unique type guids only when necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellwrosen committed Dec 9, 2024
1 parent cece289 commit 995e8e4
Show file tree
Hide file tree
Showing 20 changed files with 79 additions and 93 deletions.
43 changes: 14 additions & 29 deletions parser-typechecker/src/Unison/Syntax/DeclParser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -75,38 +75,25 @@ declarations = do
data UnresolvedModifier
= UnresolvedModifier'Structural
| UnresolvedModifier'UniqueWithGuid !Text
| -- The Text here is a random GUID that we *may not end up using*, as in the case when we instead have a GUID to
-- reuse (which we will discover soon, once we parse this unique type's name and pass it into the `uniqueTypeGuid`
-- function in the parser environment).
--
-- However, we generate this GUID anyway for backwards-compatibility with *transcripts*. Since the GUID we assign
-- is a function of the current source location in the parser state, if we generate it later (after moving a few
-- tokens ahead to the type's name), then we'll get a different value.
--
-- This is only done to make the transcript diff smaller and easier to review, as the PR that adds this GUID-reuse
-- feature ought not to change any hashes. However, at any point after it lands in trunk, this Text could be
-- removed from this constructor, the generation of these GUIDs could be delayed until we actually need them, and
-- the transcripts could all be re-generated.
UnresolvedModifier'UniqueWithoutGuid !Text
| UnresolvedModifier'UniqueWithoutGuid

resolveUnresolvedModifier :: (Monad m, Var v) => L.Token UnresolvedModifier -> v -> P v m (L.Token DD.Modifier)
resolveUnresolvedModifier unresolvedModifier var =
case L.payload unresolvedModifier of
UnresolvedModifier'Structural -> pure (DD.Structural <$ unresolvedModifier)
UnresolvedModifier'UniqueWithGuid guid -> pure (DD.Unique guid <$ unresolvedModifier)
UnresolvedModifier'UniqueWithoutGuid guid0 -> do
unique <- resolveUniqueModifier var guid0
UnresolvedModifier'UniqueWithoutGuid -> do
unique <- resolveUniqueModifier var
pure $ unique <$ unresolvedModifier

resolveUniqueModifier :: (Monad m, Var v) => v -> Text -> P v m DD.Modifier
resolveUniqueModifier var guid0 = do
ParsingEnv {uniqueTypeGuid} <- ask
guid <- fromMaybe guid0 <$> lift (lift (uniqueTypeGuid (Name.unsafeParseVar var)))
pure $ DD.Unique guid

defaultUniqueModifier :: (Monad m, Var v) => v -> P v m DD.Modifier
defaultUniqueModifier var =
uniqueName 32 >>= resolveUniqueModifier var
resolveUniqueModifier :: (Monad m, Var v) => v -> P v m DD.Modifier
resolveUniqueModifier var = do
env <- ask
guid <-
lift (lift (env.uniqueTypeGuid (Name.unsafeParseVar var))) >>= \case
Nothing -> uniqueName 32
Just guid -> pure guid
pure (DD.Unique guid)

-- unique[someguid] type Blah = ...
modifier :: (Monad m, Var v) => P v m (Maybe (L.Token UnresolvedModifier))
Expand All @@ -116,9 +103,7 @@ modifier = do
unique = do
tok <- openBlockWith "unique"
optional (openBlockWith "[" *> importWordyId <* closeBlock) >>= \case
Nothing -> do
guid <- uniqueName 32
pure (UnresolvedModifier'UniqueWithoutGuid guid <$ tok)
Nothing -> pure (UnresolvedModifier'UniqueWithoutGuid <$ tok)
Just guid -> pure (UnresolvedModifier'UniqueWithGuid (Name.toText (L.payload guid)) <$ tok)
structural = do
tok <- openBlockWith "structural"
Expand Down Expand Up @@ -196,7 +181,7 @@ dataDeclaration maybeUnresolvedModifier = do
_ <- closeBlock
case maybeUnresolvedModifier of
Nothing -> do
modifier <- defaultUniqueModifier (L.payload name)
modifier <- resolveUniqueModifier (L.payload name)
-- ann spanning the whole Decl.
let declSpanAnn = ann typeToken <> closingAnn
pure
Expand Down Expand Up @@ -234,7 +219,7 @@ effectDeclaration maybeUnresolvedModifier = do

case maybeUnresolvedModifier of
Nothing -> do
modifier <- defaultUniqueModifier (L.payload name)
modifier <- resolveUniqueModifier (L.payload name)
-- ann spanning the whole ability declaration.
let abilitySpanAnn = ann abilityToken <> closingAnn
pure
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ scratch/main> add
scratch/main> names term1
Term
Hash: #8hum58rlih
Hash: #42m1ui9g56
Names: term1 term2
```
4 changes: 2 additions & 2 deletions unison-src/transcripts/idempotent/find-by-type.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ scratch/main> find : Text
I couldn't find exact type matches, resorting to fuzzy
matching...
1. bar : Text -> A
2. baz : A -> Text
1. baz : A -> Text
2. bar : Text -> A
3. A.A : Text -> A
```
10 changes: 5 additions & 5 deletions unison-src/transcripts/idempotent/fix2254.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,15 @@ scratch/a2> update
scratch/a2> view A NeedsA f f2 f3 g
type A a b c d
= A a
| D d
| E a d
= E a d
| B b
| A a
| D d
| C c
structural type NeedsA a b
= NeedsA (A a b Nat Nat)
| Zoink Text
= Zoink Text
| NeedsA (A a b Nat Nat)
f : A Nat Nat Nat Nat -> Nat
f = cases
Expand Down
5 changes: 3 additions & 2 deletions unison-src/transcripts/idempotent/kind-inference.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ unique type T a
Loading changes detected in scratch.u.
Kind mismatch arising from
3 | | StarStar (a Nat)
2 | = Star a
a doesn't expect an argument; however, it is applied to Nat.
The arrow type (->) expects arguments of kind Type; however,
it is applied to a which has kind: Type -> Type.
```

## Kinds are inferred by decl component
Expand Down
4 changes: 2 additions & 2 deletions unison-src/transcripts/idempotent/move-all.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ scratch/main> history Bar
Note: The most recent namespace hash is immediately below this
message.
⊙ 1. #o7vuviel4c
⊙ 1. #hk3a3lsc2e
+ Adds / updates:
Expand All @@ -106,7 +106,7 @@ scratch/main> history Bar
T.T
□ 2. #c5cggiaumo (start of history)
□ 2. #vqc50q3b3v (start of history)
```

## Happy Path - Just term
Expand Down
4 changes: 2 additions & 2 deletions unison-src/transcripts/idempotent/move-namespace.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ scratch/happy> history b
Note: The most recent namespace hash is immediately below this
message.
⊙ 1. #rkvfe5p8fu
⊙ 1. #ugqniosnp0
+ Adds / updates:
Expand All @@ -195,7 +195,7 @@ scratch/happy> history b
T.T
□ 2. #avlnmh0erc (start of history)
□ 2. #a7r726o5ut (start of history)
```

## Namespace history
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1286,5 +1286,5 @@ result f =
ability GiveA a
ability GiveB a
result : '{e, GiveA V, GiveB V} r ->{e} r
result : '{e, GiveB V, GiveA V} r ->{e} r
```
6 changes: 3 additions & 3 deletions unison-src/transcripts/idempotent/propagate.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ scratch/main> add
scratch/main> find.verbose
1. -- #uj8oalgadr2f52qloufah6t8vsvbc76oqijkotek87vooih7aqu44k20hrs34kartusapghp4jmfv6g1409peklv3r6a527qpk52soo
1. -- #j743idicb1sf7udts85812agaml4rkfi3iss6lstvmvgufibd40blq5qtmoh9ndrtkvkaqkurn7npgc61ob8j2louj04j8slkppsl90
type Foo
2. -- #uj8oalgadr2f52qloufah6t8vsvbc76oqijkotek87vooih7aqu44k20hrs34kartusapghp4jmfv6g1409peklv3r6a527qpk52soo#0
2. -- #j743idicb1sf7udts85812agaml4rkfi3iss6lstvmvgufibd40blq5qtmoh9ndrtkvkaqkurn7npgc61ob8j2louj04j8slkppsl90#0
Foo.Foo : Foo
3. -- #j6hbm1gc2ak4f46b6705q90ld4bmhoi8etq2q45j081i9jgn95fvk3p6tjg67e7sm0021035i8qikmk4p6k845l5d00u26cos5731to
3. -- #sd7apvqbpk3vl2aassq4gcckovohqrs05ne1g9ol0fb6gd227bp388osj7bg40kttt2o9f1kit9avlb94ep8q1ho3g284ursrplb4l0
fooToInt : Foo -> Int
Expand Down
12 changes: 6 additions & 6 deletions unison-src/transcripts/idempotent/unique-type-churn.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ If the name stays the same, the churn is even prevented if the type is updated a
scratch/main> names A
Type
Hash: #uj8oalgadr
Hash: #j743idicb1
Names: A
Term
Hash: #uj8oalgadr#0
Hash: #j743idicb1#0
Names: A.A
```

Expand Down Expand Up @@ -88,11 +88,11 @@ scratch/main> update
scratch/main> names A
Type
Hash: #ufo5tuc7ho
Hash: #186m0i6upt
Names: A
Term
Hash: #ufo5tuc7ho#0
Hash: #186m0i6upt#0
Names: A.A
```

Expand Down Expand Up @@ -126,10 +126,10 @@ scratch/main> update
scratch/main> names A
Type
Hash: #uj8oalgadr
Hash: #j743idicb1
Names: A
Term
Hash: #uj8oalgadr#0
Hash: #j743idicb1#0
Names: A.A
```
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,17 @@ scratch/main> update
scratch/main> view Foo
type Foo = Bar Nat | Baz Nat Nat
type Foo = Baz Nat Nat | Bar Nat
scratch/main> find.verbose
1. -- #2sffq4apsq1cts53njcunj63fa8ohov4eqn77q14s77ajicajh4g28sq5s5ai33f2k6oh6o67aarnlpu7u7s4la07ag2er33epalsog
1. -- #id3p6do8f7ssln9npa3gs3c2i8uors25ffts92pr4nsh9k9bn3no50e4d1b053c2d0vei64pbtcpdld9gk6drsvptnpfqr6tp8v4qh0
type Foo
2. -- #2sffq4apsq1cts53njcunj63fa8ohov4eqn77q14s77ajicajh4g28sq5s5ai33f2k6oh6o67aarnlpu7u7s4la07ag2er33epalsog#0
2. -- #id3p6do8f7ssln9npa3gs3c2i8uors25ffts92pr4nsh9k9bn3no50e4d1b053c2d0vei64pbtcpdld9gk6drsvptnpfqr6tp8v4qh0#1
Foo.Bar : Nat -> Foo
3. -- #2sffq4apsq1cts53njcunj63fa8ohov4eqn77q14s77ajicajh4g28sq5s5ai33f2k6oh6o67aarnlpu7u7s4la07ag2er33epalsog#1
3. -- #id3p6do8f7ssln9npa3gs3c2i8uors25ffts92pr4nsh9k9bn3no50e4d1b053c2d0vei64pbtcpdld9gk6drsvptnpfqr6tp8v4qh0#0
Foo.Baz : Nat -> Nat -> Foo
```
4 changes: 2 additions & 2 deletions unison-src/transcripts/idempotent/update-type-add-field.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ scratch/main> view Foo
scratch/main> find.verbose
1. -- #8fk6k0j208th1ia4vnjtoc5fomd6le540prec255svg71bcfga9dofrvoq1d7v6010d6b6em4q51p8st5c5juhrev72cnnel8ko3o1g
1. -- #hlhjq1lf1cvfevkvb9d441kkubn0f6s43gvrd4gcff0r739vomehjnov4b3qe8506fb5bm8m5ba0sol9mbljgkk3gb2qt2u02v6i2vo
type Foo
2. -- #8fk6k0j208th1ia4vnjtoc5fomd6le540prec255svg71bcfga9dofrvoq1d7v6010d6b6em4q51p8st5c5juhrev72cnnel8ko3o1g#0
2. -- #hlhjq1lf1cvfevkvb9d441kkubn0f6s43gvrd4gcff0r739vomehjnov4b3qe8506fb5bm8m5ba0sol9mbljgkk3gb2qt2u02v6i2vo#0
Foo.Bar : Nat -> Nat -> Foo
```
Original file line number Diff line number Diff line change
Expand Up @@ -72,28 +72,28 @@ scratch/main> view Foo
scratch/main> find.verbose
1. -- #05gh1dur4778dauh9slaofprc5356n47qpove0c1jl0birt2fcu301js8auu5vfr5bjfga9j8ikuk07ll9fu1gj3ehrp3basguhsd58
1. -- #m0tpa159pbsdld5ea0marnq9614dnmjjc72n1evi4bsk45a1hl84qprt6vdvejuuiuc3f5o23olc1t19tk1dt8mjobmr0chqc3svij8
type Foo
2. -- #77mi33dv8ac2s90852khi35km5gsamhnpada8mai0k36obbttgg17qld719ospcs1ht9ctolg3pjsqs6qjnl3hfmu493rgsher73sc0
2. -- #16o21u2lli7rd8f3h4epnblpfk3h68gag99d5gicihcpk15dkv4m9601picg37ncsbg2e8j63tu7ebs40jrcoifs7f6nqrus3qnfgv0
Foo.bar : Foo -> Nat
3. -- #7m1n2178r5u12jdnb6crcmanu2gm961kdvbjul5m6hta1s57avibsvk6p5g9efut8sennpgstbb8kf97eujbbuiplsoloa4cael7t90
3. -- #64v4pv4rvmnts82gbsb1u2dvgdu4eqq8leq37anqjrkq8s9c7ogrjahdotc36nrodva6ok1rs4ah5k09i7sb0clvcap2773k1t7thb8
Foo.bar.modify : (Nat ->{g} Nat) -> Foo ->{g} Foo
4. -- #ghuqoel4pao6v8e7un238i3e86vv7a7pnvgaq8m9s32edm1upgv35gri2iu32ipn9r4poli56r5kr3vtjfrltem696grfl75al4jkgg
4. -- #bkfjhnu5jqv2m49hosd8g6m4e5u9ju4b1du90cji8s8hnaendvnep2a5cd085ejtu27c4lm3u7slamk52p86rubp211jc5c0qcut2l0
Foo.bar.set : Nat -> Foo -> Foo
5. -- #p8emkm2s09n3nsd8ne5f6fro0vsldk8pn7n6rcf417anuvvun43qrk1ioofs6pdq4537eosao17c7ibvktktr3lfqglmj26gmbulmj0
5. -- #u394ule3vr1ab8q5nit6miktgpki9gj4nft5jfjsho6cflg94kf953mdhjj8s18e9j1525iv8l5ebjhebnuc01q51fl8ni5n9j0gs28
Foo.baz : Foo -> Int
6. -- #0il9pl29jpe3fh6vp3qeqai73915k3qffhf4bgttrgsj000b9fgs3bqoj8ugjop6kdr04acc34m1bj7lf417tslfeva7dmmoqdu5hug
6. -- #cbbi7mqcaqdlcl41uajb608b8fi5dfvc654rmd47mk9okpn1t3jltrf8psnn3g2tnr1ftctj753fjhco3ku1oapc664upo1h6eodfrg
Foo.baz.modify : (Int ->{g} Int) -> Foo ->{g} Foo
7. -- #87rjeqltvvd4adffsheqae62eefoge8p78pvnjdkc9q1stq20lhubvtpos0io4v3vhnol8nn2uollup97l4orq1fh2h12b0imeuuc58
7. -- #hhi45ik1245qq3g93586f998di8j5afvjamqr2m08auqq8ogqt4d01rejrse4qsl27381vnqnt8uffhgvnc0nk22o5uabimjhji4868
Foo.baz.set : Int -> Foo -> Foo
8. -- #05gh1dur4778dauh9slaofprc5356n47qpove0c1jl0birt2fcu301js8auu5vfr5bjfga9j8ikuk07ll9fu1gj3ehrp3basguhsd58#0
8. -- #m0tpa159pbsdld5ea0marnq9614dnmjjc72n1evi4bsk45a1hl84qprt6vdvejuuiuc3f5o23olc1t19tk1dt8mjobmr0chqc3svij8#0
Foo.Foo : Nat -> Int -> Foo
```
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ scratch/main> view Foo
scratch/main> find.verbose
1. -- #b509v3eg4kehsg29g6pvrogeb71ue32nm2fj9284n4i7lprsr7u9a7g6s695d09du0fsfti6rrsk1s62q5thpr1jjkqb3us3s0lrd60
1. -- #h88o5sirfn0a8f4o81sb012p2rha5h8r73n8bloc8qq94kqmltjq94iiep2e6dj7ppuulc8jce2f0vmddqp76nm0hqs9jh53s502v4g
type Foo
2. -- #b509v3eg4kehsg29g6pvrogeb71ue32nm2fj9284n4i7lprsr7u9a7g6s695d09du0fsfti6rrsk1s62q5thpr1jjkqb3us3s0lrd60#0
2. -- #h88o5sirfn0a8f4o81sb012p2rha5h8r73n8bloc8qq94kqmltjq94iiep2e6dj7ppuulc8jce2f0vmddqp76nm0hqs9jh53s502v4g#0
Foo.Bar : Nat -> Foo
```
Original file line number Diff line number Diff line change
Expand Up @@ -78,28 +78,28 @@ scratch/main> view Foo
scratch/main> find.verbose
1. -- #05gh1dur4778dauh9slaofprc5356n47qpove0c1jl0birt2fcu301js8auu5vfr5bjfga9j8ikuk07ll9fu1gj3ehrp3basguhsd58
1. -- #m0tpa159pbsdld5ea0marnq9614dnmjjc72n1evi4bsk45a1hl84qprt6vdvejuuiuc3f5o23olc1t19tk1dt8mjobmr0chqc3svij8
type Foo
2. -- #77mi33dv8ac2s90852khi35km5gsamhnpada8mai0k36obbttgg17qld719ospcs1ht9ctolg3pjsqs6qjnl3hfmu493rgsher73sc0
2. -- #16o21u2lli7rd8f3h4epnblpfk3h68gag99d5gicihcpk15dkv4m9601picg37ncsbg2e8j63tu7ebs40jrcoifs7f6nqrus3qnfgv0
Foo.bar : Foo -> Nat
3. -- #7m1n2178r5u12jdnb6crcmanu2gm961kdvbjul5m6hta1s57avibsvk6p5g9efut8sennpgstbb8kf97eujbbuiplsoloa4cael7t90
3. -- #64v4pv4rvmnts82gbsb1u2dvgdu4eqq8leq37anqjrkq8s9c7ogrjahdotc36nrodva6ok1rs4ah5k09i7sb0clvcap2773k1t7thb8
Foo.bar.modify : (Nat ->{g} Nat) -> Foo ->{g} Foo
4. -- #ghuqoel4pao6v8e7un238i3e86vv7a7pnvgaq8m9s32edm1upgv35gri2iu32ipn9r4poli56r5kr3vtjfrltem696grfl75al4jkgg
4. -- #bkfjhnu5jqv2m49hosd8g6m4e5u9ju4b1du90cji8s8hnaendvnep2a5cd085ejtu27c4lm3u7slamk52p86rubp211jc5c0qcut2l0
Foo.bar.set : Nat -> Foo -> Foo
5. -- #p8emkm2s09n3nsd8ne5f6fro0vsldk8pn7n6rcf417anuvvun43qrk1ioofs6pdq4537eosao17c7ibvktktr3lfqglmj26gmbulmj0
5. -- #u394ule3vr1ab8q5nit6miktgpki9gj4nft5jfjsho6cflg94kf953mdhjj8s18e9j1525iv8l5ebjhebnuc01q51fl8ni5n9j0gs28
Foo.baz : Foo -> Int
6. -- #0il9pl29jpe3fh6vp3qeqai73915k3qffhf4bgttrgsj000b9fgs3bqoj8ugjop6kdr04acc34m1bj7lf417tslfeva7dmmoqdu5hug
6. -- #cbbi7mqcaqdlcl41uajb608b8fi5dfvc654rmd47mk9okpn1t3jltrf8psnn3g2tnr1ftctj753fjhco3ku1oapc664upo1h6eodfrg
Foo.baz.modify : (Int ->{g} Int) -> Foo ->{g} Foo
7. -- #87rjeqltvvd4adffsheqae62eefoge8p78pvnjdkc9q1stq20lhubvtpos0io4v3vhnol8nn2uollup97l4orq1fh2h12b0imeuuc58
7. -- #hhi45ik1245qq3g93586f998di8j5afvjamqr2m08auqq8ogqt4d01rejrse4qsl27381vnqnt8uffhgvnc0nk22o5uabimjhji4868
Foo.baz.set : Int -> Foo -> Foo
8. -- #05gh1dur4778dauh9slaofprc5356n47qpove0c1jl0birt2fcu301js8auu5vfr5bjfga9j8ikuk07ll9fu1gj3ehrp3basguhsd58#0
8. -- #m0tpa159pbsdld5ea0marnq9614dnmjjc72n1evi4bsk45a1hl84qprt6vdvejuuiuc3f5o23olc1t19tk1dt8mjobmr0chqc3svij8#0
Foo.Foo : Nat -> Int -> Foo
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ unique type Foo = Bar Nat Nat
``` ucm :error
scratch/main> view Foo
type Foo = #b509v3eg4k#0 Nat
type Foo = #5mod0n8ps2#0 Nat
scratch/main> update
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,16 @@ scratch/main> view Foo
scratch/main> find.verbose
1. -- #b509v3eg4kehsg29g6pvrogeb71ue32nm2fj9284n4i7lprsr7u9a7g6s695d09du0fsfti6rrsk1s62q5thpr1jjkqb3us3s0lrd60
1. -- #oebc8v8v9lob5bnq7go1pjhfjbtnh8dmfhontua90t3mji0cl91t1dqaece9quofrk1vsbq6g0ukfigoi0vmvc01v8roceppejlgbs8
type Foo
2. -- #36rn6jqt1k5jccb3c7vagp3jam74dngr92kgcntqhs6dbkua54verfert2i6hsku6uitt9s2jvt1msric0tgemal52d5apav6akn25o
2. -- #gl18p1lnbeari67ohdt9n46usnvsl59a6up1lhd9r808pqb7tt5edsf65o98bqcvb529mfm7q631ciuv2t5nqnde1i7b9t5mlu1drto
Foo.Bar : Nat -> Foo
3. -- #b509v3eg4kehsg29g6pvrogeb71ue32nm2fj9284n4i7lprsr7u9a7g6s695d09du0fsfti6rrsk1s62q5thpr1jjkqb3us3s0lrd60#0
3. -- #oebc8v8v9lob5bnq7go1pjhfjbtnh8dmfhontua90t3mji0cl91t1dqaece9quofrk1vsbq6g0ukfigoi0vmvc01v8roceppejlgbs8#0
Foo.internal.Bar : Nat -> Foo
4. -- #204frdcl0iid1ujkkfbkc6b3v7cgqp56h1q3duc46i5md6qb4m6am1fqbceb335u87l05gkdnaa7fjn4alj1diukgme63e41lh072l8
4. -- #td96hudai64mf0qgtusc70ehv98krs10jghdipjluc6cp4j8ac65msrt3tji18enpm2tm8d8h2qcf3parke19g7s17ipkd925m3061g
makeFoo : Nat -> Foo
```
Loading

0 comments on commit 995e8e4

Please sign in to comment.