-
Notifications
You must be signed in to change notification settings - Fork 28
Alternative & MonadPlus #8
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| module Control.Alt where | ||
|
|
||
| infixl 3 <|> | ||
|
|
||
| class (Functor f) <= Alt f where | ||
| (<|>) :: forall a. f a -> f a -> f a |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| module Control.Alternative where | ||
|
|
||
| import Control.Alt | ||
| import Control.Lazy | ||
| import Control.Plus | ||
|
|
||
| class (Applicative f, Plus f) <= Alternative f | ||
|
|
||
| some :: forall f a. (Alternative f, Lazy1 f) => f a -> f [a] | ||
| some v = (:) <$> v <*> defer1 (\_ -> many v) | ||
|
|
||
| many :: forall f a. (Alternative f, Lazy1 f) => f a -> f [a] | ||
| many v = some v <|> pure [] | ||
|
|
||
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| module Control.Lazy where | ||
|
|
||
| class Lazy l where | ||
| defer :: (Unit -> l) -> l | ||
|
|
||
| class Lazy1 l where | ||
| defer1 :: forall a. (Unit -> l a) -> l a | ||
|
|
||
| class Lazy2 l where | ||
| defer2 :: forall a b. (Unit -> l a b) -> l a b | ||
|
|
||
| fix :: forall l a. (Lazy l) => (l -> l) -> l | ||
| fix f = defer (\_ -> f (fix f)) | ||
|
|
||
| fix1 :: forall l a. (Lazy1 l) => (l a -> l a) -> l a | ||
| fix1 f = defer1 (\_ -> f (fix1 f)) | ||
|
|
||
| fix2 :: forall l a b. (Lazy2 l) => (l a b -> l a b) -> l a b | ||
| fix2 f = defer2 (\_ -> f (fix2 f)) |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| module Control.MonadPlus where | ||
|
|
||
| import Control.Alternative | ||
| import Control.Plus | ||
|
|
||
| class (Monad m, Alternative m) <= MonadPlus m | ||
|
|
||
| guard :: forall m. (MonadPlus m) => Boolean -> m Unit | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Finally! Now I can add this to the book under array comprehensions :) |
||
| guard true = return unit | ||
| guard false = empty | ||
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| module Control.Plus where | ||
|
|
||
| import Control.Alt | ||
|
|
||
| class (Alt f) <= Plus f where | ||
| empty :: forall a. f a |
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.
These look like they won't terminate.
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.
Oops, of course. I meant to come back to these.
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 would like to find a way to keep these though, because they're obviously very useful. In
parsing, I wrote a special combinator, but I think if we have a type class which captures laziness, we can do it.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.
Like this :
https://github.com/purescript-contrib/purescript-node-args/blob/master/src/Node/Args.purs.hs#L86
https://github.com/purescript-contrib/purescript-node-args/blob/master/src/Node/Args.purs.hs#L40
Maybe there's a better name.
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.
Perhaps something to add to
purescript-lazythen? I guess we'd prefercontrolto have no dependencies though, considering a lot of stuff is going to be depending on it...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 think it's fine in
control. It's not an implementation of laziness.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.
Control.Lazythen? That might be extremely useful actually.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.
Sure. I think there might also be use for a whole set of type classes along these lines:
Lazy,Lazy1, etc. but for now, just the two is probably enough :)