Send Beginners mailing list submissions to beginners@haskell.org To subscribe or unsubscribe via the World Wide Web, visit http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners or, via email, send a message with subject or body 'help' to beginners-requ...@haskell.org
You can reach the person managing the list at beginners-ow...@haskell.org When replying, please edit your Subject line so it is more specific than "Re: Contents of Beginners digest..." Today's Topics: 1. Alternative instance w/ additional restriction (Baa) 2. Re: Alternative instance w/ additional restriction (David McBride) 3. Re: Alternative instance w/ additional restriction (Baa) 4. mempty and "No instance for (Monoid Int)" (Baa) 5. Re: Alternative instance w/ additional restriction (David McBride) ---------------------------------------------------------------------- Message: 1 Date: Wed, 7 Jun 2017 15:26:44 +0300 From: Baa <aqua...@gmail.com> To: beginners@haskell.org Subject: [Haskell-beginners] Alternative instance w/ additional restriction Message-ID: <20170607152644.1bc47bc8@Pavel> Content-Type: text/plain; charset=UTF-8 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 ------------------------------ Message: 2 Date: Wed, 7 Jun 2017 11:25:26 -0400 From: David McBride <toa...@gmail.com> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: Re: [Haskell-beginners] Alternative instance w/ additional restriction Message-ID: <can+tr40a-wcvhmvekjvlz_qhcxqp+moa9t-2nbrlvg+h5zv...@mail.gmail.com> Content-Type: text/plain; charset="UTF-8" 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 <aqua...@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@haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners ------------------------------ Message: 3 Date: Wed, 7 Jun 2017 19:06:25 +0300 From: Baa <aqua...@gmail.com> To: beginners@haskell.org Subject: Re: [Haskell-beginners] Alternative instance w/ additional restriction Message-ID: <20170607190625.29dfb935@Pavel> Content-Type: text/plain; charset=UTF-8 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 <toa...@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 <aqua...@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@haskell.org > > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners ------------------------------ Message: 4 Date: Wed, 7 Jun 2017 19:33:52 +0300 From: Baa <aqua...@gmail.com> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: [Haskell-beginners] mempty and "No instance for (Monoid Int)" Message-ID: <20170607193352.0085b85a@Pavel> Content-Type: text/plain; charset=UTF-8 Maybe a is the Monoid: instance Monoid a => Monoid (Maybe a) -- Defined in ‘GHC.Base’ so I can compare its values with empty value: mempty == Nothing => True But if I try: mempty == Just 4 I get: <interactive>:1:1: error: • Ambiguous type variable ‘a0’ arising from a use of ‘mempty’ prevents the constraint ‘(Monoid a0)’ from being solved. Probable fix: use a type annotation to specify what ‘a0’ should be. These potential instances exist: instance Monoid a => Monoid (IO a) -- Defined in ‘GHC.Base’ instance Monoid Ordering -- Defined in ‘GHC.Base’ instance Monoid a => Monoid (Maybe a) -- Defined in ‘GHC.Base’ ...plus 7 others (use -fprint-potential-instances to see them all) • In the first argument of ‘(==)’, namely ‘mempty’ In the expression: mempty == Just 4 In an equation for ‘it’: it = mempty == Just 4 OK, I try: mempty::Maybe Int and get: <interactive>:1:1: error: • No instance for (Monoid Int) arising from a use of ‘mempty’ • In the expression: mempty :: Maybe Int In an equation for ‘it’: it = mempty :: Maybe Int so, how is related Int to Monoid, why does ghc expect from mempty::Maybe Int, Int to be Monoid?! As I understand, this means only that I mean "mempty" from (Maybe Int) type, which is Monoid and exists sure. Interesting is, that: mempty::Maybe [Int] => Nothing but how is related "monoidality" of "Maybe a" with "monoidality of "a" ??? Initial idea was to make comparison: mempty :: Maybe Int == Just 4 => False /Best regards, Paul ------------------------------ Message: 5 Date: Wed, 7 Jun 2017 12:40:52 -0400 From: David McBride <toa...@gmail.com> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: Re: [Haskell-beginners] Alternative instance w/ additional restriction Message-ID: <can+tr42btjwv+spqb4z1zds4sf5xysxarbijrbwhm-1aurj...@mail.gmail.com> Content-Type: text/plain; charset="UTF-8" I honestly don't think so. But you can ask on haskell-cafe or on stackoverflow if you like. There is a reasonable chance that a class or library exists that already will do what you are looking for, just that I haven't heard of it. On Wed, Jun 7, 2017 at 12:06 PM, Baa <aqua...@gmail.com> wrote: > 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 <toa...@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 <aqua...@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@haskell.org >> > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners >> _______________________________________________ >> Beginners mailing list >> Beginners@haskell.org >> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners ------------------------------ Subject: Digest Footer _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners ------------------------------ End of Beginners Digest, Vol 108, Issue 3 *****************************************