Skip to content

Add showConstructor to convert union value constructor to Text#1257

Merged
Gabriella439 merged 8 commits intodhall-lang:masterfrom
darichey:show-constructor
Jan 7, 2022
Merged

Add showConstructor to convert union value constructor to Text#1257
Gabriella439 merged 8 commits intodhall-lang:masterfrom
darichey:show-constructor

Conversation

@darichey
Copy link
Copy Markdown
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
Copy Markdown
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

Copy link
Copy Markdown
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.

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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