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. Re:  How do I map a String and a IO String? (Francesco Ariis)
   2.  special polymorphic default of typeclass depending on other
      typeclasses? (Silent Leaf)
   3. Re:  special polymorphic default of typeclass depending on
      other typeclasses? (Jean Lopes)
   4. Re:  special polymorphic default of typeclass depending on
      other typeclasses? (Sylvain Henry)
   5. Re:  special polymorphic default of typeclass depending on
      other typeclasses? (David McBride)
   6. Re:  special polymorphic default of typeclass depending on
      other typeclasses? (Silent Leaf)


----------------------------------------------------------------------

Message: 1
Date: Thu, 29 Jun 2017 16:48:46 +0200
From: Francesco Ariis <fa...@ariis.it>
To: beginners@haskell.org
Subject: Re: [Haskell-beginners] How do I map a String and a IO
        String?
Message-ID: <20170629144846.3pwqpxytpwcrg...@x60s.casa>
Content-Type: text/plain; charset=utf-8

On Thu, Jun 29, 2017 at 04:15:16PM +0200, Jona Ekenberg wrote:
> Thank you for your help Francesco!
> 
> I tried writing it like this:
> 
> > lineOrIo :: String -> IO String
> > lineOrIo cs | (isPrefixOf "./" cs) = readFile cs
> >             | otherwise            = return cs
> >
> > printLines path = do
> >   file <- readFile path
> >   lines <- map lineOrIo (lines file)
> >   print lines

You are using `map`, which has signature

    λ> :t map
    map :: (a -> b) -> [a] -> [b]

But lineOrIo hasn't signature `a -> b` but `a -> m b` (where m is a monad)!
mapM will fit the bill:

    mapM :: Monad m => (a -> m b) -> [a] -> m [b]

and that should do it!


------------------------------

Message: 2
Date: Thu, 29 Jun 2017 19:15:26 +0200
From: Silent Leaf <silent.le...@gmail.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: [Haskell-beginners] special polymorphic default of typeclass
        depending on other typeclasses?
Message-ID:
        <CAGFccjNStH-WCrOQ0X=NnTuKWL3c-Ai27wHCcXTMSR=n3dGp=a...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

hi,

say i have the following typeclass:

class Foo a where
  bar :: a  -> String

looks a lot like the Read typeclass, right? (at least i think it should?)
well say it's a different meaning (in other terms i can't or do not want to
use Read, but i'd like to implement a default version of bar for those
instances that also implement Read. is there a way to do so?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20170629/a1fa9673/attachment-0001.html>

------------------------------

Message: 3
Date: Thu, 29 Jun 2017 14:27:21 -0300
From: Jean Lopes <hawu....@gmail.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] special polymorphic default of
        typeclass depending on other typeclasses?
Message-ID:
        <CAKeoKsh2RogDHp7h50hf6Lf11fn_iDnNT+1yyGOqfJb=k1n...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

It appear more like Show, you can add a  constraint to the generic instance

instance Show a => Foo a where
    bar x = show x




Em 29 de jun de 2017 2:16 PM, "Silent Leaf" <silent.le...@gmail.com>
escreveu:

hi,

say i have the following typeclass:

class Foo a where
  bar :: a  -> String

looks a lot like the Read typeclass, right? (at least i think it should?)
well say it's a different meaning (in other terms i can't or do not want to
use Read, but i'd like to implement a default version of bar for those
instances that also implement Read. is there a way to do so?

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20170629/74ad9392/attachment-0001.html>

------------------------------

Message: 4
Date: Thu, 29 Jun 2017 19:34:55 +0200
From: Sylvain Henry <sylv...@haskus.fr>
To: beginners@haskell.org
Subject: Re: [Haskell-beginners] special polymorphic default of
        typeclass depending on other typeclasses?
Message-ID: <4103a59a-956e-042f-70f4-1e330b19f...@haskus.fr>
Content-Type: text/plain; charset="utf-8"; Format="flowed"

Constraints aren't considered when instance selection is performed. I.e. 
you can't have both:
instance Foo a where ...
instance Show a => Foo a where ....

But you can use default method signatures: 
https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/glasgow_exts.html#default-method-signatures


On 29/06/2017 19:27, Jean Lopes wrote:
> It appear more like Show, you can add a  constraint to the generic 
> instance
>
> instance Show a => Foo a where
>     bar x = show x
>
>
>
>
> Em 29 de jun de 2017 2:16 PM, "Silent Leaf" <silent.le...@gmail.com 
> <mailto:silent.le...@gmail.com>> escreveu:
>
>     hi,
>
>     say i have the following typeclass:
>
>     class Foo a where
>       bar :: a  -> String
>
>     looks a lot like the Read typeclass, right? (at least i think it
>     should?)
>     well say it's a different meaning (in other terms i can't or do
>     not want to use Read, but i'd like to implement a default version
>     of bar for those instances that also implement Read. is there a
>     way to do so?
>
>     _______________________________________________
>     Beginners mailing list
>     Beginners@haskell.org <mailto:Beginners@haskell.org>
>     http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>     <http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners>
>
>
>
>
> _______________________________________________
> Beginners mailing list
> Beginners@haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20170629/29946a4d/attachment-0001.html>

------------------------------

Message: 5
Date: Thu, 29 Jun 2017 13:45:14 -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] special polymorphic default of
        typeclass depending on other typeclasses?
Message-ID:
        <CAN+Tr403yniYUf_z1AAzU97Kn=ho4OrH+Yzs=zwk7-fukch...@mail.gmail.com>
Content-Type: text/plain; charset="UTF-8"

This is a common mistake that people who try to use type classes run
into.  I remember banging my head against it pretty hard when I first
started out.

There's this temptation that  you should be able to write the following:

class Foo a where
  bar :: a  -> String

instance Read a => Foo a where
  bar a = read a

instance Foo () where
  bar _ = "bar"

But the problem with that is that now () applies to two conflicting
classes.  It is both a Read and a Foo. So when you go bar (), which
instance should fire?  The Foo instance or the Read () => Foo
instance?

There are a multitude of ways you could try to resolve this.  Let's
say obviously the Read constrainted instance is more specific, we
should use that.  But then what if the user of your library happens to
have a instance Ord a => Foo a in his library, now which one of those
is more specific?  Read or Ord?

Because of all these ambiguities during type checking ghc doesn't even
look at the constraint.  It would see instance Foo a, and instance Foo
(), and then say oh! those are overlapping instances because () could
apply to either class before you consider what constraints apply.

There's actually several very in depth answers on stackoverflow for
this questions like this, such as this one:
https://stackoverflow.com/a/3216937/1500583  It might give you some
ideas on what to do about this.

On Thu, Jun 29, 2017 at 1:15 PM, Silent Leaf <silent.le...@gmail.com> wrote:
> hi,
>
> say i have the following typeclass:
>
> class Foo a where
>   bar :: a  -> String
>
> looks a lot like the Read typeclass, right? (at least i think it should?)
> well say it's a different meaning (in other terms i can't or do not want to
> use Read, but i'd like to implement a default version of bar for those
> instances that also implement Read. is there a way to do so?
>
> _______________________________________________
> Beginners mailing list
> Beginners@haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>


------------------------------

Message: 6
Date: Thu, 29 Jun 2017 20:08:08 +0200
From: Silent Leaf <silent.le...@gmail.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] special polymorphic default of
        typeclass depending on other typeclasses?
Message-ID:
        <cagfccjpfb_buanum_g5pwswr5wje99cgptxrmzpvb79tkmh...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

you're right my example is more like Show, it's because i change two times
the order of input and output, and did not check at the end if it was what
i wanted...

Actually i did not mean constraints on instances, especially thereafter
ad-hoc polymorphic instances.
I rather meant what i said, as in default implementation (as in, right
inside the body of the class).
like that:

class Foo a where
  bar :: a -> String
  bar :: Show a => a -> String
  bar = show -- default implementation: in other terms, if you define an
instance without defining this method

the idea would be then that if you have Foo (), you can either implement a
special version, or leave it to show. mind you i'm not even sure we can
define an instance without any specific implementation of method?

--that would be allowed:
instance Show => Foo () where
  -- nothing, if it's even legal by default

-- and of course that would be allowed to if someone wanted a special
function bar :: Foo () => () -> String
instance Foo () where
  bar = ....

obviously, i do not mean *both* instances of Foo (), just, one or the
other. it would merely be a way to implement ad-hoc polymorphism onto
*default implementations of methods*, that is, those inside the body of the
class.



2017-06-29 19:45 GMT+02:00 David McBride <toa...@gmail.com>:

> This is a common mistake that people who try to use type classes run
> into.  I remember banging my head against it pretty hard when I first
> started out.
>
> There's this temptation that  you should be able to write the following:
>
> class Foo a where
>   bar :: a  -> String
>
> instance Read a => Foo a where
>   bar a = read a
>
> instance Foo () where
>   bar _ = "bar"
>
> But the problem with that is that now () applies to two conflicting
> classes.  It is both a Read and a Foo. So when you go bar (), which
> instance should fire?  The Foo instance or the Read () => Foo
> instance?
>
> There are a multitude of ways you could try to resolve this.  Let's
> say obviously the Read constrainted instance is more specific, we
> should use that.  But then what if the user of your library happens to
> have a instance Ord a => Foo a in his library, now which one of those
> is more specific?  Read or Ord?
>
> Because of all these ambiguities during type checking ghc doesn't even
> look at the constraint.  It would see instance Foo a, and instance Foo
> (), and then say oh! those are overlapping instances because () could
> apply to either class before you consider what constraints apply.
>
> There's actually several very in depth answers on stackoverflow for
> this questions like this, such as this one:
> https://stackoverflow.com/a/3216937/1500583  It might give you some
> ideas on what to do about this.
>
> On Thu, Jun 29, 2017 at 1:15 PM, Silent Leaf <silent.le...@gmail.com>
> wrote:
> > hi,
> >
> > say i have the following typeclass:
> >
> > class Foo a where
> >   bar :: a  -> String
> >
> > looks a lot like the Read typeclass, right? (at least i think it should?)
> > well say it's a different meaning (in other terms i can't or do not want
> to
> > use Read, but i'd like to implement a default version of bar for those
> > instances that also implement Read. is there a way to do so?
> >
> > _______________________________________________
> > 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20170629/ae54d807/attachment.html>

------------------------------

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 24
******************************************

Reply via email to