-
Notifications
You must be signed in to change notification settings - Fork 71
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
Use Natural Transformations wherever possible #207
Open
milesfrain
wants to merge
1
commit into
purescript:master
Choose a base branch
from
milesfrain:nat-trans
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if this is the most appropriate use, since the inner values are changing. Took inspiration from
List
library:https://github.com/purescript/purescript-lists/blob/62900a56f6864638c952575dfd211a1cc55be7da/src/Data/List/NonEmpty.purs#L149-L153
By the same logic, we could also convert many others, such as
head
andtail
ofArray
, as was done in thisList
PR:purescript/purescript-lists#101
I wonder if this will add to confusion for beginners.
There's also some potentially misleading info in our definition of natural transformation.
So maybe that first paragraph should be changed to:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You’re reading the docs in a different way from what is intended there, because it’s not actually possible to write a function
f ~> g
which does manipulate the inner values in the way that is meant there, the compiler won’t let you.In most cases, all that the “cannot manipulate the inner values” condition means is that every element of type
a
in the result also occurred in the input. Soreverse
,init
, and evenhead
are all perfectly valid natural transformations. An invalid one would be egmap (_ + 1)
, but of course that wouldn’t compile as you’d get something like “can’t match a0 with Int”.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For what it is worth, the fact that
head
andreverse
weren't defined via natural transformations confused this beginner. I was reading Jordan's reference and my first thought was "I betreverse
has a natural transformation in it's type signature". I wonder if we could use~>
in the code as an example of best practice (assuming that it is best practice) and put comments in such as "This type signature could be expressed as forall a. ...".There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it is a best practice, it's not really very significant whether you use
~>
or not. I just think we should avoid it in the core libraries because it's another thing to learn, it's harder to search for in Pursuit, and because it's awkward when you have cases which don't quite fit into natural transformations liketail :: forall a. List a -> Maybe (List a)
; if you wanted to write that with~>
you'd need to use a type liketail :: List ~> Compose Maybe List
which I think we'd all probably agree is not appropriate.