Skip to content

Commit

Permalink
Merge pull request #4557 from unorsk/unorsk/unique-as-default-type-mo…
Browse files Browse the repository at this point in the history
…difier

Unorsk/unique as default type modifier
  • Loading branch information
mergify[bot] authored Jan 6, 2024
2 parents a2c4a5a + 70eee30 commit 2ea7756
Show file tree
Hide file tree
Showing 71 changed files with 402 additions and 440 deletions.
34 changes: 27 additions & 7 deletions parser-typechecker/src/Unison/Syntax/DeclParser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,18 @@ resolveUnresolvedModifier unresolvedModifier var =
UnresolvedModifier'Structural -> pure (DD.Structural <$ unresolvedModifier)
UnresolvedModifier'UniqueWithGuid guid -> pure (DD.Unique guid <$ unresolvedModifier)
UnresolvedModifier'UniqueWithoutGuid guid0 -> do
ParsingEnv {uniqueTypeGuid} <- ask
guid <- fromMaybe guid0 <$> lift (lift (uniqueTypeGuid (Name.unsafeFromVar var)))
pure (DD.Unique guid <$ unresolvedModifier)
unique <- resolveUniqueModifier var guid0
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.unsafeFromVar var)))
pure $ DD.Unique guid

defaultUniqueModifier :: (Monad m, Var v) => v -> P v m DD.Modifier
defaultUniqueModifier var =
uniqueName 32 >>= resolveUniqueModifier var

-- unique[someguid] type Blah = ...
modifier :: (Monad m, Var v) => P v m (Maybe (L.Token UnresolvedModifier))
Expand Down Expand Up @@ -132,7 +141,7 @@ dataDeclaration ::
Maybe (L.Token UnresolvedModifier) ->
P v m (v, DataDeclaration v Ann, Accessors v)
dataDeclaration maybeUnresolvedModifier = do
keywordTok <- fmap void (reserved "type") <|> openBlockWith "type"
_ <- fmap void (reserved "type") <|> openBlockWith "type"
(name, typeArgs) <-
(,)
<$> TermParser.verifyRelativeVarName prefixDefinitionName
Expand Down Expand Up @@ -181,7 +190,13 @@ dataDeclaration maybeUnresolvedModifier = do
closingAnn :: Ann
closingAnn = last (ann eq : ((\(_, _, t) -> ann t) <$> constructors))
case maybeUnresolvedModifier of
Nothing -> P.customFailure $ MissingTypeModifier ("type" <$ keywordTok) name
Nothing -> do
modifier <- defaultUniqueModifier (L.payload name)
pure
( L.payload name,
DD.mkDataDecl' modifier closingAnn typeArgVs constructors,
accessors
)
Just unresolvedModifier -> do
modifier <- resolveUnresolvedModifier unresolvedModifier (L.payload name)
pure
Expand All @@ -196,7 +211,7 @@ effectDeclaration ::
Maybe (L.Token UnresolvedModifier) ->
P v m (v, EffectDeclaration v Ann)
effectDeclaration maybeUnresolvedModifier = do
keywordTok <- fmap void (reserved "ability") <|> openBlockWith "ability"
_ <- fmap void (reserved "ability") <|> openBlockWith "ability"
name <- TermParser.verifyRelativeVarName prefixDefinitionName
typeArgs <- many (TermParser.verifyRelativeVarName prefixDefinitionName)
let typeArgVs = L.payload <$> typeArgs
Expand All @@ -208,7 +223,12 @@ effectDeclaration maybeUnresolvedModifier = do
last $ ann blockStart : ((\(_, _, t) -> ann t) <$> constructors)

case maybeUnresolvedModifier of
Nothing -> P.customFailure $ MissingTypeModifier ("ability" <$ keywordTok) name
Nothing -> do
modifier <- defaultUniqueModifier (L.payload name)
pure
( L.payload name,
DD.mkEffectDecl' modifier closingAnn typeArgVs constructors
)
Just unresolvedModifier -> do
modifier <- resolveUnresolvedModifier unresolvedModifier (L.payload name)
pure
Expand Down
5 changes: 3 additions & 2 deletions parser-typechecker/src/Unison/Syntax/DeclPrinter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,9 @@ fieldNames env r name dd = do

prettyModifier :: DD.Modifier -> Pretty SyntaxText
prettyModifier DD.Structural = fmt S.DataTypeModifier "structural"
prettyModifier (DD.Unique _uid) =
fmt S.DataTypeModifier "unique" -- <> ("[" <> P.text uid <> "] ")
prettyModifier (DD.Unique _uid) = mempty -- don't print anything since 'unique' is the default
-- leaving this comment for the historical record so the syntax for uid is not forgotten
-- fmt S.DataTypeModifier "unique" -- <> ("[" <> P.text uid <> "] ")

prettyDataHeader ::
(Var v) => HQ.HashQualified Name -> DD.DataDeclaration v a -> Pretty SyntaxText
Expand Down
80 changes: 40 additions & 40 deletions unison-src/transcripts-using-base/all-base-hashes.output.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ testABunchOfNats _ =
⍟ These new definitions are ok to `add`:
unique type EncDec
type EncDec
BE16 : EncDec
BE32 : EncDec
BE64 : EncDec
Expand All @@ -81,7 +81,7 @@ testABunchOfNats _ =
⍟ I've added these definitions:
unique type EncDec
type EncDec
BE16 : EncDec
BE32 : EncDec
BE64 : EncDec
Expand Down
11 changes: 2 additions & 9 deletions unison-src/transcripts-using-base/doc.output.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ unique type time.DayOfWeek = Sun | Mon | Tue | Wed | Thu | Fri | Sat
⍟ These new definitions are ok to `add`:
unique type time.DayOfWeek
type time.DayOfWeek
ImportantConstant : Nat
ImportantConstant.doc : Doc2
d1 : Doc2
Expand All @@ -63,14 +63,7 @@ You can preview what docs will look like when rendered to the console using the
The 7 days of the week, defined as:
unique type DayOfWeek
= Sun
| Mon
| Tue
| Wed
| Thu
| Fri
| Sat
type DayOfWeek = Sun | Mon | Tue | Wed | Thu | Fri | Sat
```
The `docs ImportantConstant` command will look for `ImportantConstant.doc` in the file or codebase. You can do this instead of explicitly linking docs to definitions.
Expand Down
8 changes: 4 additions & 4 deletions unison-src/transcripts-using-base/fix2297.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ This tests a case where a function was somehow discarding abilities.


```unison:error
ability Trivial where
structural ability Trivial where
trivial : ()
-- This handler SHOULD leave any additional effects alone and unhandled
handleTrivial : '{e, Trivial} a -> {e} a
handleTrivial action =
handleTrivial action =
h : Request {Trivial} a -> a
h = cases
{trivial -> resume} -> handle !resume with h
{a} -> a
handle !action with h
testAction : '{Exception, IO, Trivial} ()
testAction _ =
printText "hi!"
testAction = do
printLine "hi!"
trivial
wat : ()
Expand Down
16 changes: 6 additions & 10 deletions unison-src/transcripts-using-base/fix2297.output.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ This tests a case where a function was somehow discarding abilities.


```unison
ability Trivial where
structural ability Trivial where
trivial : ()
-- This handler SHOULD leave any additional effects alone and unhandled
handleTrivial : '{e, Trivial} a -> {e} a
handleTrivial action =
handleTrivial action =
h : Request {Trivial} a -> a
h = cases
{trivial -> resume} -> handle !resume with h
{a} -> a
handle !action with h
testAction : '{Exception, IO, Trivial} ()
testAction _ =
printText "hi!"
testAction = do
printLine "hi!"
trivial
wat : ()
Expand All @@ -29,13 +29,9 @@ wat = handleTrivial testAction -- Somehow this completely forgets about Excepti
Loading changes detected in scratch.u.
I expected to see `structural` or `unique` at the start of
this line:
The expression in red needs the {Exception} ability, but this location does not have access to any abilities.
1 | ability Trivial where
19 | wat = handleTrivial testAction -- Somehow this completely forgets about Exception and IO
Learn more about when to use `structural` vs `unique` in the
Unison Docs:
https://www.unison-lang.org/learn/language-reference/unique-types/
```
4 changes: 2 additions & 2 deletions unison-src/transcripts/abilities.output.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ ha = cases
⍟ These new definitions are ok to `add`:
unique ability A
ability A
ha : Request {A} r -> r
```
Expand All @@ -36,7 +36,7 @@ ha = cases
⍟ I've added these definitions:
unique ability A
ability A
ha : Request {A} r -> r
```
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ term2 _ = ()
⍟ These new definitions are ok to `add`:
unique ability Bar
unique ability Foo
ability Bar
ability Foo
term1 : '{Bar, Foo} ()
term2 : '{Bar, Foo} ()
Expand All @@ -35,8 +35,8 @@ term2 _ = ()
⍟ I've added these definitions:
unique ability Bar
unique ability Foo
ability Bar
ability Foo
term1 : '{Bar, Foo} ()
term2 : '{Bar, Foo} ()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ unique ability Channels where
⍟ These new definitions are ok to `add`:
unique ability Channels
ability Channels
```
```ucm
Expand All @@ -30,7 +30,7 @@ unique ability Channels where
⍟ I've added these definitions:
unique ability Channels
ability Channels
```
Now we update the ability, changing the name of the constructor, _but_, we simultaneously
Expand Down Expand Up @@ -64,7 +64,7 @@ thing _ = send 1
⍟ These names already exist. You can `update` them to your
new definition:
unique ability Channels
ability Channels
```
These should fail with a term/ctor conflict since we exclude the ability from the update.
Expand All @@ -88,7 +88,7 @@ These should fail with a term/ctor conflict since we exclude the ability from th
⍟ I've updated these names to your new definition:
unique ability Channels
ability Channels
```
If however, `Channels.send` and `thing` _depend_ on `Channels`, updating them should succeed since it pulls in the ability as a dependency.
Expand Down
Loading

0 comments on commit 2ea7756

Please sign in to comment.