-
Notifications
You must be signed in to change notification settings - Fork 377
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 spec for Alt, Plus, Alternative #187
Comments
Alt
, Plus
and Alternative
?Alt
, Plus
and Alternative
?
And maybe also add -- | The `MonadZero` type class has no members of its own; it just specifies
-- | that the type has both `Monad` and `Alternative` instances.
-- |
-- | Types which have `MonadZero` instances should also satisfy the following
-- | laws:
-- |
-- | - Annihilation: `empty >>= f = empty`
class (Monad m, Alternative m) <= MonadZero m
-- | The `MonadPlus` type class has no members of its own but extends
-- | `MonadZero` with an additional law:
-- |
-- | - Distributivity: `(x <|> y) >>= f == (x >>= f) <|> (y >>= f)`
class MonadZero m <= MonadPlus m |
Alt looks like fantasy-land's Semigroup with additional distributivity law:
And Plus looks like Monoid with additional law:
Am I reading this right? How would they fit in the dependency tree then? Alt would have dependency on Semigroup and Functor, right? But I'm confused with dependencies of Plus, should it be Alt and Functor, or Monoid and Functor, or all three? |
Or maybe I've misunderstood all this, and Semigroup shouldn't be connected with Alt, neither Monoid with Plus. In other words |
Distinction between typeclasses MonadPlus, Alternative, and Monoid? So for example Maybe could be Semigroup if value in it is also Semigroup, but Plus and Alt should not "look" at values inside |
we can make Task instance of Semigroup if values in Task are also Semigroups. Task.of([a]).concat(Task.of([b])) = Task.of([a,b]) we can make Task MonadPlus Task.rejected(err).alt(Task.of(b)) = Task.of(b) we can make TaskAp Alternative Task.empty/*never*/.alt(Task.of(b)) = Task.of(b) But we would have collision with Monoid's empty and Plus' empty. in PureScirpt Monoid's empty is called
|
I see, so the idea is that they are not connected and Monoid for example can do something different than Plus. My first impression is that I'd rather had them connected, so we always talk about the same operation but may make less or more stronger claim. But I really don't feel educated and experienced enough to discuss new type classes, though 😄 Very curious what others think! |
😄 just came to all that Alternative staff today from #186 (I want this specs to make Free lawful and concurrent) :P |
@rpominov / @safareli your intuition seems pretty spot on - As an example of how this manifests, take instance semigroupMaybe :: Semigroup a => Semigroup (Maybe a) where
append Nothing y = y
append x Nothing = x
append (Just x) (Just y) = Just (x <> y)
instance altMaybe :: Alt Maybe where
alt Nothing r = r
alt l _ = l So compare As for the empty values... I can't think of an example where |
Thanks , @garyb , I understand it better now.
In fantasy-land we would probably have to do the same for the same reason. We also start from |
Yeah, I kinda wish we'd done something different there too! |
it already discussed here: #117 some related issue about the laws: purescript/purescript-control#6 |
It good to have this on Fantasy Land i think. Kudos for bringing it again @safareli |
some resources:
There is diagrinment in haskell comunity on monadPlus laws and this proposal suggests to split it in MonadZero, MonadPlus and MonadOr. Currently purescript has MonadZero and MonadPlus, but no -- | The `MonadOr` type class has no members of its own but extends
-- | `MonadZero` with an additional law:
-- |
-- | - Left catch: `(return a) <|> b = return a`
class MonadZero m <= MonadOr m Currently I don't know if there is a plan to add it, maybe @garyb knows it. |
I'm adding @robotlolita @Avaq as Task/Future will be effected with this specs. For Applicative versions of Task/Future:
For Monadic version of Task/Future:
|
@SimonRichardson can you take a look this issue? if it's ok I would create PR for update:
in here I was first thinking that maybe we could define |
Sure, I can look tomorrow 😀 |
We probably could define I'm trying to think of examples of things which have I suppose And for the latter, we could write a variant of Perhaps it is worth adding. |
@paf31 And if we have |
I think that's asking too much of |
@joneshf correct ! (I had a mistake in graph) |
I'd be more than happy to see a PR @safareli - awesome work already :) |
Alt
, Plus
and Alternative
?
What others think on changing |
#197 is ready for review <3 |
I think it would be nice if we also define algebras for
Alt
,Plus
andAlternative
Snippets from purescript-control
It will be useful to
Future/Task
implementations (alt
will berace
).and with also define
Parallel
algebra then in concurrent applicative could implementAlternative
and we would have therace
as well (MonadRace
is removed).The text was updated successfully, but these errors were encountered: