Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add showConstructor to convert union value constructor to Text #1257

Merged
merged 8 commits into from
Jan 7, 2022

Conversation

darichey
Copy link
Contributor

@darichey darichey commented Dec 31, 2021

Fixes #1204

Adds a showConstructor keyword that can be used to convert a union value to Text by its constructor name. For example:

 :paste
-- Entering multi-line mode. Press <Ctrl-D> to finish.
| let Foo = < A | B : Natural | C : Natural -> Text >
| let a = showConstructor Foo.A
| let b = showConstructor (Foo.B 3)
| let c = showConstructor (Foo.C (\(n : Natural) -> ""))
| in
| "${a}${b}${c}"

"ABC"

It also works on Optional values:

 showConstructor (None Natural)

"None"

 showConstructor (Some 0)

"Some"

I have a draft implementation for dhall-haskell: darichey/dhall-haskell@64ef9df

Copy link
Contributor

@Gabriella439 Gabriella439 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! I think all this needs is tests

standard/dhall.abnf Outdated Show resolved Hide resolved
standard/type-inference.md Show resolved Hide resolved
Copy link
Collaborator

@MonoidMusician MonoidMusician left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, just this one detail.

standard/type-inference.md Outdated Show resolved Hide resolved
darichey and others added 2 commits January 1, 2022 18:12
* Fix grammar spacing
* Make typing rule less strict to work with abstract arguments

Co-authored-by: Gabriella Gonzalez <Gabriel439@gmail.com>
Co-authored-by: Verity Scheel <monoidmusician@gmail.com>
Copy link
Collaborator

@sjakobi sjakobi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a great feature! :)

I've been trying to think of some "interesting" type-inference failure tests. So far I've only come up with:

  • showConstructor < A : Bool >.A
  • showConstructor None

Please also add type-inference and normalization success tests for something like showConstructor (< A : Bool>.A False).

I also wonder whether showConstructor ought to work for Bools. I don't see why not. Our main reason for not allowing merge for Bools doesn't seem to apply in this case.

@darichey
Copy link
Contributor Author

darichey commented Jan 2, 2022

Thanks for the test suggestions! I've added those.

showConstructor for Bools sounds reasonable, though I wonder what the use case would be where you wouldn't just use Bool/show.

Copy link
Collaborator

@sjakobi sjakobi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

showConstructor for Bools sounds reasonable, though I wonder what the use case would be where you wouldn't just use Bool/show.

It might be just slightly more convenient than importing Bool/show from the Prelude. Not a very important addition though.

Co-authored-by: Simon Jakobi <simon.jakobi@gmail.com>
@Gabriella439 Gabriella439 merged commit 8a9be23 into dhall-lang:master Jan 7, 2022
Gabriella439 added a commit to dhall-lang/dhall-haskell that referenced this pull request Feb 16, 2022
… as standardized in dhall-lang/dhall-lang#1257

Co-authored-by: David Richey <darichey1@gmail.com>
@Gabriella439 Gabriella439 mentioned this pull request Oct 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Show for arbitrary enums
4 participants