[Haskell-beginners] Alternative instance w/ additional restriction
Baa
aquagnu at gmail.com
Wed Jun 7 16:06:25 UTC 2017
So, if I understood right, I'm trying to restrict instance more than
class allows. And only way to accomplish it - is to create own class
instead of Alternative which will not be based on
Functor :: (* -> *) -> *, like Alternative. No other workarounds?
В Wed, 7 Jun 2017 11:25:26 -0400
David McBride <toad3k at gmail.com> пишет:
> The Alternative class says nothing about the a in MyData a. It only
> represents code relevant to MyData.
>
> When you see class Applicative f => Alternative (f :: * -> *), that
> means all of its functions had to work on any f, where f takes any
> type and becomes some other type which could be anything.
>
> The reason it works for Monoid is that class Monoid a where implies
> that a is completely known by the time the instance is fulfilled,
> therefore the instance can look at what a ended up being and ensure
> whatever a is, it must have this constraint on it.
>
> You can tell the difference because mempty returns a type that
> mentions the a mentioned in the class, whereas empty returns an a that
> is not mentioned in the class, therefore it has to work for any a.
>
> On Wed, Jun 7, 2017 at 8:26 AM, Baa <aquagnu at gmail.com> wrote:
> > Hello all!
> >
> > If I try to write, for example:
> >
> > instance Alternative MyData where
> > empty = NoMyData
> > a <|> b = if a == b then ...
> >
> > I get error (see on bottom of the mail) sure, bcz I suppose
> > something like:
> >
> > Eq a => (MyData a)
> >
> > All my attempts to add something to instance's `pure` signature have
> > failed. How can I instantiate something with additional
> > restrictions, like in this case? Or are there another solutions for
> > such problem?
> >
> > Interesting is that MyData derives Eq itself! Which, I suppose, must
> > means that "Eq (MyData a)", and if it's true than "Eq a" is true,
> > because how "MyData a" can be Eq without to be "Eq a" (with
> > *automatically deriving* of Eq instance) ?!
> >
> > By the way, for Monoid (which is "* -> Constraint") I can add "Eq a"
> > constraint without problems:
> >
> > instance Eq a => Monoid (Origin a) where
> > mempty = NoMyData
> > mappend NoMyData a = a
> > mappend a NoMyData = a
> > mappend (MyData a) (MyData b)|a == b = MyData a
> > |otherwise = NoMyData
> >
> > but not for Alternative, which is "(* -> *) -> Constraint".
> >
> > *ORIGINAL ERROR DUMP*:
> > ======================
> > 42 16 error error:
> > • No instance for (Eq a) arising
> > from a use of ‘==’ Possible fix:
> > add (Eq a) to the context of
> > the type signature for:
> > (<|>) :: Origin a -> Origin
> > a -> Origin a • In the expression: a == b
> > In the expression: if a == b then
> > NoOrigin else NoOrigin In an equation for ‘<|>’:
> > a <|> b = if a == b then
> > NoOrigin else NoOrigin (intero)
> >
> >
> > /Best regards
> > Paul
> > _______________________________________________
> > Beginners mailing list
> > Beginners at haskell.org
> > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
More information about the Beginners
mailing list