Send Beginners mailing list submissions to
[email protected]
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
[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. Why is :t 42 ::Num a => a -> a ? (Olumide)
2. Re: Why is :t 42 ::Num a => a -> a ? (Sylvain Henry)
3. Exception/Error Handling Best Practices (Hillary Ryan)
4. Re: Exception/Error Handling Best Practices
(Theodore Lief Gannon)
----------------------------------------------------------------------
Message: 1
Date: Sat, 24 Sep 2016 14:54:08 +0100
From: Olumide <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: [Haskell-beginners] Why is :t 42 ::Num a => a -> a ?
Message-ID: <[email protected]>
Content-Type: text/plain; charset=utf-8; format=flowed
Is 42 a function? And if it is why does 42 + 24 work, if both are
functions. I know there's something I'm getting wrong here.
Also, is the expression abs -42 an error. I initially thought that abs
to the operator (-) first but there is no space between - and 42. Or is
there some associativity issue going on here.
- Olumide
------------------------------
Message: 2
Date: Sat, 24 Sep 2016 16:05:37 +0200
From: Sylvain Henry <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] Why is :t 42 ::Num a => a -> a ?
Message-ID: <[email protected]>
Content-Type: text/plain; charset=utf-8; format=flowed
Hi,
> :t 42
42 :: Num t => t
The parser has to make some choices to distinguish between negate and
subtraction. In ambiguous cases, subtraction is chosen:
> abs -42
<interactive>:2:1: error:
• Non type-variable argument in the constraint: Num (a -> a)
(Use FlexibleContexts to permit this)
• When checking the inferred type
it :: forall a. (Num (a -> a), Num a) => a -> a
> abs (-42)
42
> let abs=10
> abs-42
-32
> abs -42
-32
> abs - 42
-32
> abs (-42)
<interactive>:15:1: error:
• Non type-variable argument in the constraint: Num (t -> t1)
(Use FlexibleContexts to permit this)
• When checking the inferred type
it :: forall t t1. (Num (t -> t1), Num t) => t1
It leads to some confusion:
> fmap (+ 42) [1,2]
[43,44]
> fmap (- 42) [1,2]
<interactive>:12:1: error:
• Non type-variable argument in the constraint: Num (a -> b)
(Use FlexibleContexts to permit this)
• When checking the inferred type
it :: forall a b. (Num (a -> b), Num a) => [b]
> fmap (flip (-) 42) [1,2]
[-41,-40]
Cheers
Sylvain
On 24/09/2016 15:54, Olumide wrote:
> Is 42 a function? And if it is why does 42 + 24 work, if both are
> functions. I know there's something I'm getting wrong here.
>
> Also, is the expression abs -42 an error. I initially thought that abs
> to the operator (-) first but there is no space between - and 42. Or
> is there some associativity issue going on here.
>
> - Olumide
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
------------------------------
Message: 3
Date: Sat, 24 Sep 2016 23:49:48 -0500
From: Hillary Ryan <[email protected]>
To: [email protected]
Subject: [Haskell-beginners] Exception/Error Handling Best Practices
Message-ID:
<CADB=hlgyavlnwv_gtlxzd1viebmxf_f4ncu6bw9q4eqtbuh...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
Dear Haskellers,
I've been working on variants of the following code:
connect_from_file :: String -> IO Connection
connect_from_file fi = do
s <- BL.readFile fi
cred <- (decode s :: Maybe Db_cred)
c <- connect . reformat_cred $ cred
return c
What this code intends to do is read a config from a file in JSON, decode
the JSON, and then open a connection based the decoded info. This code
doesn't compile for several reasons, but I've left it in this form because
I believe it is the most convenient for approaching my question:
How should I handle errors here?
Both readFile (from Bytestring) and connect (from mysql.simple) throw
exceptions when things don't go their way, and decode uses maybe. I see
four ways of handling these errors:
0) *All typed errors: *I could "capture" all the exceptions in Either types
and use an EitherIO monad or transformer to automatically abort when a Left
value appears. Now my function looks like: String -> IO (Either MyErrorType
Connection)
1) *All exceptions: *I could throw an exception if Nothing appears during
decoding. The function looks the same: String -> IO Connection
2) *A mix*: I could leave the connect and readFile functions alone, but
return IO (Nothing) if the decoding doesn't work out. Now we have: String
-> IO (Maybe Connection)
3) Not 0)-2)
So what is the proper way to do things?
Many Thanks,
Hillary
PS For what it is worth, I have read:
0) https://www.schoolofhaskell.com/user/commercial/content/
exceptions-best-practices . I'd love to get feedback on how to apply this
article to my question if it is appropriate.
1) https://lexi-lambda.github.io/blog/2016/06/12/four-months-with-haskell/
. From this article and others, I get the sense that there is great
confusion in the community as to how to handle errors correctly, and worse
yet, that different approaches are not easily compatible.
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20160924/7286e4ba/attachment-0001.html>
------------------------------
Message: 4
Date: Sun, 25 Sep 2016 04:53:45 -0700
From: Theodore Lief Gannon <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Exception/Error Handling Best
Practices
Message-ID:
<cajopsucjjakzp52vdjpbbao2edknvyft340n2tc4+qxjoj+...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
I recently had to deal with a situation like this in a small web service
which acts as an on-demand scraper for other sites. It runs on Servant,
which uses ExceptT as of a few versions ago (EitherT before that), but for
the remote calls it uses Wreq, which throws HttpException values. I needed
to be able to report the details of remote errors rather than having them
become a generic unhandled exception, so I was stuck with your option 0 --
but with ExceptT rather than Either, and it turns out ExceptT is pretty
slick! Here's the resultant code:
https://github.com/tejon/Meeseeks/blob/master/src/bin/MeeseeksBox.hs#L42-L48
It's a one-route service so there's a bit of clutter in there that really
should be abstracted out and/or done better. (Let's just pretend logE
doesn't exist -- semantically, it doesn't!) But other than the bit I
documented with a comment, it's entirely straightforward; and IIRC omitting
that signature defaulted the type to IOException, which looks like what you
want anyway.
tl;dr 0.5) ExceptT.
On Sat, Sep 24, 2016 at 9:49 PM, Hillary Ryan <[email protected]>
wrote:
> Dear Haskellers,
>
> I've been working on variants of the following code:
>
> connect_from_file :: String -> IO Connection
> connect_from_file fi = do
> s <- BL.readFile fi
> cred <- (decode s :: Maybe Db_cred)
> c <- connect . reformat_cred $ cred
> return c
>
> What this code intends to do is read a config from a file in JSON, decode
> the JSON, and then open a connection based the decoded info. This code
> doesn't compile for several reasons, but I've left it in this form because
> I believe it is the most convenient for approaching my question:
>
> How should I handle errors here?
>
> Both readFile (from Bytestring) and connect (from mysql.simple) throw
> exceptions when things don't go their way, and decode uses maybe. I see
> four ways of handling these errors:
>
> 0) *All typed errors: *I could "capture" all the exceptions in Either
> types and use an EitherIO monad or transformer to automatically abort when
> a Left value appears. Now my function looks like: String -> IO (Either
> MyErrorType Connection)
> 1) *All exceptions: *I could throw an exception if Nothing appears during
> decoding. The function looks the same: String -> IO Connection
> 2) *A mix*: I could leave the connect and readFile functions alone, but
> return IO (Nothing) if the decoding doesn't work out. Now we have: String
> -> IO (Maybe Connection)
> 3) Not 0)-2)
>
> So what is the proper way to do things?
>
> Many Thanks,
> Hillary
>
>
> PS For what it is worth, I have read:
> 0) https://www.schoolofhaskell.com/user/commercial/content/exce
> ptions-best-practices . I'd love to get feedback on how to apply this
> article to my question if it is appropriate.
> 1) https://lexi-lambda.github.io/blog/2016/06/12/four-months-with-haskell/
> . From this article and others, I get the sense that there is great
> confusion in the community as to how to handle errors correctly, and worse
> yet, that different approaches are not easily compatible.
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20160925/ce4506fa/attachment-0001.html>
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 99, Issue 19
*****************************************