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.  Ambiguous type variable (Jonathon Delgado)
   2. Re:  Ambiguous type variable (Francesco Ariis)
   3. Re:  Ambiguous type variable (Michael Snoyman)
   4. Re:  Ambiguous type variable (Jonathon Delgado)


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

Message: 1
Date: Thu, 17 Aug 2017 07:55:28 +0000
From: Jonathon Delgado <volderm...@hotmail.com>
To: "beginners@haskell.org" <beginners@haskell.org>
Subject: [Haskell-beginners] Ambiguous type variable
Message-ID:
        
<vi1p18901mb01447137ec64af6336fd4c62cd...@vi1p18901mb0144.eurp189.prod.outlook.com>
        
Content-Type: text/plain; charset="Windows-1252"

I'm trying to use
  catch (...) (\e -> putStrLn $ show e)
However, I get an error
  Ambiguous type variable ‘a0’ arising from a use of ‘show’ prevents the 
constraint ‘(Show a0)’ from being solved.
This goes away if I change the code to
  catch (...) (\e -> putStrLn $ show (e::IOException))

A couple of things I don't understand here:
- The signature for catch begins "Exception e", and exception it "class 
(Typeable e, Show e) => Exception e". So why isn't show automatically available?
- Why does the new code work at all? e is Exception, not IOException. What 
would happen if it caught a different Exception?


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

Message: 2
Date: Thu, 17 Aug 2017 10:15:18 +0200
From: Francesco Ariis <fa...@ariis.it>
To: beginners@haskell.org
Subject: Re: [Haskell-beginners] Ambiguous type variable
Message-ID: <20170817081518.ar27uotrgpqsb...@x60s.casa>
Content-Type: text/plain; charset=utf-8

On Thu, Aug 17, 2017 at 07:55:28AM +0000, Jonathon Delgado wrote:
> I'm trying to use
>   catch (...) (\e -> putStrLn $ show e)
> However, I get an error
>   Ambiguous type variable ‘a0’ arising from a use of ‘show’ prevents the 
> constraint ‘(Show a0)’ from being solved.
> This goes away if I change the code to
>   catch (...) (\e -> putStrLn $ show (e::IOException))
> 
> A couple of things I don't understand here:
> - The signature for catch begins "Exception e", and exception it "class 
> (Typeable e, Show e) => Exception e". So why isn't show automatically 
> available?
> - Why does the new code work at all? e is Exception, not IOException. What 
> would happen if it caught a different Exception?

IOException is a concrete type while Exception is a typeclass. In the end,
the compiler needs the former, the latter not being enough.

The code works as any other class-based function would

    someFunction :: Monoid a -> [a] -> a
    -- ^-- in the end `Monoid a` will become something concrete, like
    -- a String, a Sum, etc.

Does that make sense?


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

Message: 3
Date: Thu, 17 Aug 2017 11:58:31 +0300
From: Michael Snoyman <mich...@snoyman.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] Ambiguous type variable
Message-ID:
        <cakt9ecorgtrpt7q+f7vt6j+xv1ralnwwgljzpartpfq79oe...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

On Thu, Aug 17, 2017 at 10:55 AM, Jonathon Delgado <volderm...@hotmail.com>
wrote:

> I'm trying to use
>   catch (...) (\e -> putStrLn $ show e)
> However, I get an error
>   Ambiguous type variable ‘a0’ arising from a use of ‘show’ prevents the
> constraint ‘(Show a0)’ from being solved.
> This goes away if I change the code to
>   catch (...) (\e -> putStrLn $ show (e::IOException))
>
> A couple of things I don't understand here:
> - The signature for catch begins "Exception e", and exception it "class
> (Typeable e, Show e) => Exception e". So why isn't show automatically
> available?
> - Why does the new code work at all? e is Exception, not IOException. What
> would happen if it caught a different Exception?
>


In my experience, the most common thing people need is "catch all
synchronous exceptions." The word "synchronous" there is the source of a
lot of confusion, and relates to a complicated topic of asynchronous
exceptions. My recommendation is: don't worry about that right now, use the
safe-exceptions package, and switch from catch to catchAny. More details on
the package are available at:

https://haskell-lang.org/library/safe-exceptions
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20170817/d817bd7a/attachment-0001.html>

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

Message: 4
Date: Thu, 17 Aug 2017 12:24:07 +0000
From: Jonathon Delgado <volderm...@hotmail.com>
To: "beginners@haskell.org" <beginners@haskell.org>
Subject: Re: [Haskell-beginners] Ambiguous type variable
Message-ID:
        
<vi1p18901mb0144f9c67bacc7163d2c53bbcd...@vi1p18901mb0144.eurp189.prod.outlook.com>
        
Content-Type: text/plain; charset="Windows-1252"

I'm sure it makes sense! I'm not really following though.

I understood typeclasses to be analogous to OO interfaces. So if a variable 
implements the Exception interface, and Exception implements the Show 
interface, then it should automatically support show.

I take it this was wrong? How does the compiler use typeclasses if they're not 
interfaces?


Francesco Ariis  wrote: 

> I'm trying to use 
>   catch (...) (\e -> putStrLn $ show e) 
> However, I get an error 
>   Ambiguous type variable ‘a0’ arising from a use of ‘show’ prevents the 
> constraint ‘(Show a0)’ from being solved. 
> This goes away if I change the code to 
>   catch (...) (\e -> putStrLn $ show (e::IOException)) 
> 
> A couple of things I don't understand here: 
> - The signature for catch begins "Exception e", and exception it "class 
> (Typeable e, Show e) => Exception e". So why isn't show automatically 
> available? 
> - Why does the new code work at all? e is Exception, not IOException. What 
> would happen if it caught a different Exception? 

IOException is a concrete type while Exception is a typeclass. In the end, 
the compiler needs the former, the latter not being enough. 

The code works as any other class-based function would 

    someFunction :: Monoid a -> [a] -> a 
    -- ^-- in the end `Monoid a` will become something concrete, like 
    -- a String, a Sum, etc. 

Does that make sense? 

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

Subject: Digest Footer

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


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

End of Beginners Digest, Vol 110, Issue 16
******************************************

Reply via email to