Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1.  understanding type classes and class constraints (Robert Krahn)
   2. Re:  understanding type classes and class constraints (Dan Krol)
   3. Re:  understanding type classes and class constraints (Dan Krol)
   4. Re:  understanding type classes and class constraints
      (Brandon Allbery)
   5. Re:  understanding type classes and class constraints
      (Robert Krahn)


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

Message: 1
Date: Sun, 17 Nov 2013 18:47:58 -0600
From: Robert Krahn <[email protected]>
To: [email protected]
Subject: [Haskell-beginners] understanding type classes and class
        constraints
Message-ID:
        <CAJSDjYPc_p-UrU4a3SbVi-FLt28tpwB2L_tkYb=chgdyasw...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

Hi everyone,

I'm currently enjoying to learn Haskell and came upon a problem I don't
understand:

Motivated by the Learn you a Haskell / yes-no typeclass example I tried to
create a simple "Tester" type class:

class Tester a where
    test :: a -> Bool

I wasn't to define the rules when test returns True/False for various
types, e.g.

instance Tester Integer where
    test 0 = False
    test _ = True

For the Maybe instance I want to delegate to the value of Just and add a
class constraint:

instance (Tester m) => Tester (Maybe m) where
    test Nothing = False
    test (Just x) = test x

It compiles nicely and works for Just values
test (Just 3) -- True
test (Just 0) -- False

But
test Nothing

gives me
    No instance for (Tester a0) arising from a use of `test'
    The type variable `a0' is ambiguous
    Possible fix: add a type signature that fixes these type variable(s)
    Note: there are several potential instances:
      instance Tester m => Tester (Maybe m)
      instance Tester Integer
    In the expression: test Nothing

Could you please enlighten me what's going on and how to fix my code?

Thank you!
Robert
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20131117/7bc98c74/attachment-0001.html>

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

Message: 2
Date: Sun, 17 Nov 2013 16:54:45 -0800
From: Dan Krol <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] understanding type classes and class
        constraints
Message-ID:
        <caawrcs_n9cwkr9qpmvv4yvtwvr7obzqnmvpu61bkgjmkgd6...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

When you run "test Nothing", ghc doesn't know what type Nothing is. Just 0,
ghc can deduce is a Maybe Integer. Nothing can be Maybe Integer, Maybe
(Maybe Integer), Maybe Char, etc.

Try running:

test (Nothing :: Maybe Integer)


On Sun, Nov 17, 2013 at 4:47 PM, Robert Krahn <[email protected]>wrote:

> Hi everyone,
>
> I'm currently enjoying to learn Haskell and came upon a problem I don't
> understand:
>
> Motivated by the Learn you a Haskell / yes-no typeclass example I tried to
> create a simple "Tester" type class:
>
> class Tester a where
>     test :: a -> Bool
>
> I wasn't to define the rules when test returns True/False for various
> types, e.g.
>
> instance Tester Integer where
>     test 0 = False
>     test _ = True
>
> For the Maybe instance I want to delegate to the value of Just and add a
> class constraint:
>
> instance (Tester m) => Tester (Maybe m) where
>     test Nothing = False
>     test (Just x) = test x
>
> It compiles nicely and works for Just values
> test (Just 3) -- True
> test (Just 0) -- False
>
> But
> test Nothing
>
> gives me
>     No instance for (Tester a0) arising from a use of `test'
>     The type variable `a0' is ambiguous
>     Possible fix: add a type signature that fixes these type variable(s)
>     Note: there are several potential instances:
>       instance Tester m => Tester (Maybe m)
>       instance Tester Integer
>     In the expression: test Nothing
>
> Could you please enlighten me what's going on and how to fix my code?
>
> Thank you!
> Robert
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20131117/887c8469/attachment-0001.html>

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

Message: 3
Date: Sun, 17 Nov 2013 16:57:02 -0800
From: Dan Krol <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] understanding type classes and class
        constraints
Message-ID:
        <CAAWRcS_x=bzepx59y0r1oxhwcjsod9z9jbiu-ch8vxvn_yp...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

I guess I see the intuitive confusion. It shouldn't matter what type
Nothing is, it should always return False. But ghc doesn't like to give
results of a determined type (Bool) based on operations put on values of
undetermined type (Maybe a).


On Sun, Nov 17, 2013 at 4:54 PM, Dan Krol <[email protected]> wrote:

> When you run "test Nothing", ghc doesn't know what type Nothing is. Just
> 0, ghc can deduce is a Maybe Integer. Nothing can be Maybe Integer, Maybe
> (Maybe Integer), Maybe Char, etc.
>
> Try running:
>
> test (Nothing :: Maybe Integer)
>
>
> On Sun, Nov 17, 2013 at 4:47 PM, Robert Krahn <[email protected]>wrote:
>
>> Hi everyone,
>>
>> I'm currently enjoying to learn Haskell and came upon a problem I don't
>> understand:
>>
>> Motivated by the Learn you a Haskell / yes-no typeclass example I tried
>> to create a simple "Tester" type class:
>>
>> class Tester a where
>>     test :: a -> Bool
>>
>> I wasn't to define the rules when test returns True/False for various
>> types, e.g.
>>
>> instance Tester Integer where
>>     test 0 = False
>>     test _ = True
>>
>> For the Maybe instance I want to delegate to the value of Just and add a
>> class constraint:
>>
>> instance (Tester m) => Tester (Maybe m) where
>>     test Nothing = False
>>     test (Just x) = test x
>>
>> It compiles nicely and works for Just values
>> test (Just 3) -- True
>> test (Just 0) -- False
>>
>> But
>> test Nothing
>>
>> gives me
>>     No instance for (Tester a0) arising from a use of `test'
>>     The type variable `a0' is ambiguous
>>     Possible fix: add a type signature that fixes these type variable(s)
>>     Note: there are several potential instances:
>>       instance Tester m => Tester (Maybe m)
>>       instance Tester Integer
>>     In the expression: test Nothing
>>
>> Could you please enlighten me what's going on and how to fix my code?
>>
>> Thank you!
>>  Robert
>>
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20131117/999fdad2/attachment-0001.html>

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

Message: 4
Date: Sun, 17 Nov 2013 19:57:27 -0500
From: Brandon Allbery <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] understanding type classes and class
        constraints
Message-ID:
        <CAKFCL4W8g=-rg9fkurelsg2hempoz27zmcu6-tsxliwvvya...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

On Sun, Nov 17, 2013 at 7:47 PM, Robert Krahn <[email protected]>wrote:

> instance Tester Integer where
>     test 0 = False
>     test _ = True
>
> For the Maybe instance I want to delegate to the value of Just and add a
> class constraint:
>
> instance (Tester m) => Tester (Maybe m) where
>     test Nothing = False
>     test (Just x) = test x
>
> It compiles nicely and works for Just values
> test (Just 3) -- True
> test (Just 0) -- False
>
> But
> test Nothing
>
> gives me
>     No instance for (Tester a0) arising from a use of `test'
>

Several things are happening here. First is that numbers undergo
defaulting; with nothing else to specify the type of `3` or `0`, they
default to Integer and all is well.

This cannot be said of `Nothing`; it needs to pick an instance, but
`Nothing` doesn't give it anything to work from.

"But I only have an instance for Integer!" Typeclasses work under the "open
world assumption": a typeclass could be added at any time, so it cannot
commit to a given typeclass instance simply because it only happens to know
of one valid instance. In particular, it cannot conclude that `Nothing` is
of type `Maybe Integer` solely because it happens to only be aware of a
`Tester Integer` instance.

(Instances are program global and cannot be hidden, so not making the open
world assumption could easily cause programs to completely change their
meaning with the addition of a single additional instance *anywhere* in
that program, even if not imported to that module.)

-- 
brandon s allbery kf8nh                               sine nomine associates
[email protected]                                  [email protected]
unix, openafs, kerberos, infrastructure, xmonad        http://sinenomine.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20131117/f931b38b/attachment-0001.html>

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

Message: 5
Date: Sun, 17 Nov 2013 20:52:32 -0600
From: Robert Krahn <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] understanding type classes and class
        constraints
Message-ID:
        <cajsdjymcmdivhm_okv1ue_ttqnotakax-t-vybcrsfx7xsj...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

Thanks a lot for the thorough and quick explanations!


On Sun, Nov 17, 2013 at 6:57 PM, Brandon Allbery <[email protected]>wrote:

> On Sun, Nov 17, 2013 at 7:47 PM, Robert Krahn <[email protected]>wrote:
>
>> instance Tester Integer where
>>     test 0 = False
>>     test _ = True
>>
>> For the Maybe instance I want to delegate to the value of Just and add a
>> class constraint:
>>
>> instance (Tester m) => Tester (Maybe m) where
>>     test Nothing = False
>>     test (Just x) = test x
>>
>> It compiles nicely and works for Just values
>> test (Just 3) -- True
>> test (Just 0) -- False
>>
>> But
>> test Nothing
>>
>> gives me
>>     No instance for (Tester a0) arising from a use of `test'
>>
>
> Several things are happening here. First is that numbers undergo
> defaulting; with nothing else to specify the type of `3` or `0`, they
> default to Integer and all is well.
>
> This cannot be said of `Nothing`; it needs to pick an instance, but
> `Nothing` doesn't give it anything to work from.
>
> "But I only have an instance for Integer!" Typeclasses work under the
> "open world assumption": a typeclass could be added at any time, so it
> cannot commit to a given typeclass instance simply because it only happens
> to know of one valid instance. In particular, it cannot conclude that
> `Nothing` is of type `Maybe Integer` solely because it happens to only be
> aware of a `Tester Integer` instance.
>
> (Instances are program global and cannot be hidden, so not making the open
> world assumption could easily cause programs to completely change their
> meaning with the addition of a single additional instance *anywhere* in
> that program, even if not imported to that module.)
>
> --
> brandon s allbery kf8nh                               sine nomine
> associates
> [email protected]
> [email protected]
> unix, openafs, kerberos, infrastructure, xmonad
> http://sinenomine.net
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20131117/28a977f3/attachment.html>

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

Subject: Digest Footer

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


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

End of Beginners Digest, Vol 65, Issue 8
****************************************

Reply via email to