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 exception is not catching?! (Baa)
2. Re: Why exception is not catching?! (Chris Sasarak)
3. Re: Why exception is not catching?! (Baa)
4. Coredump functionality for Haskell (Baa)
----------------------------------------------------------------------
Message: 1
Date: Thu, 14 Dec 2017 16:33:52 +0200
From: Baa <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: [Haskell-beginners] Why exception is not catching?!
Message-ID: <20171214163352.7e2033ff@Pavel>
Content-Type: text/plain; charset=US-ASCII
Hello, dear List!
Super simple function - "safe enum conversion". It's based on `Either`
because only it instantiates `MonadCatch` (Maybe does not, but with
MaybeT I've got error that internal monad Indetity is not MonadCatch,
so I avoid to use Maybe). That it is:
data A = A1|A2 deriving (Enum, Show)
data B = B1|B2|B3 deriving (Enum, Show)
conv :: B -> Either SomeException A
conv b = safeConv
where catchError (e::SomeException) = Left e
safeConv = (Right $ toEnum $ fromEnum b) `catch` catchError
-- toRes (Left _) = Nothing
-- toRes (Right r) = Just r
But exception is not catching and I'm getting it in GHCI repl :)
Exceptions are different?! What's wrong with this code?
===
Best regards, Paul
------------------------------
Message: 2
Date: Thu, 14 Dec 2017 22:35:19 -0500
From: Chris Sasarak <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Why exception is not catching?!
Message-ID: <[email protected]>
Content-Type: text/plain
I think that this might be related to where it says in the documentation that
'catch' must be called from inside the IO monad:
http://hackage.haskell.org/package/base-4.10.1.0/docs/Control-Exception-Base.html#v:catch.
That doc also points to the evaluate function for details on catching
exceptions in pure code.
The reason it works in the GHCi repl is that GHCI is "special." Everything you
evaluate it is in the IO monad/is implicitly in a 'do' block. This is why when
you bind variables in GHCi you have to use 'let <defs>' rather than 'let <defs>
in <expression>'. I think that what you're running into is that because your
code is implicitly in an IO action in GHCi, you can catch the exception. In
compiled code (I assume this is where it's breaking?) it's a pure function
though.
I'd like for someone else to chime in though, at the least maybe look into
using evaluate.
-Chris
Baa writes:
> Hello, dear List!
> Super simple function - "safe enum conversion". It's based on `Either`
> because only it instantiates `MonadCatch` (Maybe does not, but with
> MaybeT I've got error that internal monad Indetity is not MonadCatch,
> so I avoid to use Maybe). That it is:
>
> data A = A1|A2 deriving (Enum, Show)
> data B = B1|B2|B3 deriving (Enum, Show)
> conv :: B -> Either SomeException A
> conv b = safeConv
> where catchError (e::SomeException) = Left e
> safeConv = (Right $ toEnum $ fromEnum b) `catch` catchError
> -- toRes (Left _) = Nothing
> -- toRes (Right r) = Just r
>
> But exception is not catching and I'm getting it in GHCI repl :)
> Exceptions are different?! What's wrong with this code?
>
> ===
> Best regards, Paul
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
------------------------------
Message: 3
Date: Fri, 15 Dec 2017 10:47:24 +0200
From: Baa <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] Why exception is not catching?!
Message-ID: <20171215104724.2f098c13@Pavel>
Content-Type: text/plain; charset=US-ASCII
Hello, Chris! Yes, you are right. Gabriel answered already too:
http://www.haskellforall.com/2012/07/errors-10-simplified-error-handling.html?showComment=1513269598213#c3541282127755203659
What I can't understand in this case is: if partial functions CAN raise
"IO" exception CallError (I'm not sure in terminology here), and this
exception can be catched only in IO monad, why is this not reflected in
the types?! So, seems Haskell is not pure functional language, if there
are such surprices. Sure, there are compromises but they can be
reflected in types/signatures in some way :) Strange.
Solution may be to use "spoon" package which uses `deepseq` to force to
normal form but needs data to be NFData... Thanks to Gabriel for
help :)
===
Best regards, Paul
> I think that this might be related to where it says in the
> documentation that 'catch' must be called from inside the IO monad:
> http://hackage.haskell.org/package/base-4.10.1.0/docs/Control-Exception-Base.html#v:catch.
> That doc also points to the evaluate function for details on catching
> exceptions in pure code.
>
> The reason it works in the GHCi repl is that GHCI is "special."
> Everything you evaluate it is in the IO monad/is implicitly in a 'do'
> block. This is why when you bind variables in GHCi you have to use
> 'let <defs>' rather than 'let <defs> in <expression>'. I think that
> what you're running into is that because your code is implicitly in
> an IO action in GHCi, you can catch the exception. In compiled code
> (I assume this is where it's breaking?) it's a pure function though.
>
> I'd like for someone else to chime in though, at the least maybe look
> into using evaluate.
>
> -Chris
>
> Baa writes:
>
> > Hello, dear List!
> > Super simple function - "safe enum conversion". It's based on
> > `Either` because only it instantiates `MonadCatch` (Maybe does not,
> > but with MaybeT I've got error that internal monad Indetity is not
> > MonadCatch, so I avoid to use Maybe). That it is:
> >
> > data A = A1|A2 deriving (Enum, Show)
> > data B = B1|B2|B3 deriving (Enum, Show)
> > conv :: B -> Either SomeException A
> > conv b = safeConv
> > where catchError (e::SomeException) = Left e
> > safeConv = (Right $ toEnum $ fromEnum b) `catch` catchError
> > -- toRes (Left _) = Nothing
> > -- toRes (Right r) = Just r
> >
> > But exception is not catching and I'm getting it in GHCI repl :)
> > Exceptions are different?! What's wrong with this code?
> >
> > ===
> > Best regards, Paul
> > _______________________________________________
> > Beginners mailing list
> > [email protected]
> > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
------------------------------
Message: 4
Date: Fri, 15 Dec 2017 13:30:16 +0200
From: Baa <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: [Haskell-beginners] Coredump functionality for Haskell
Message-ID: <20171215133016.756c0fc9@Pavel>
Content-Type: text/plain; charset=US-ASCII
Hello, All!
In C if I need I can abort program and generate coredump - to research
state of stack/vars. Is it possible in Haskell - to abort and to
generate some dump which can be easy explored/researched in debugger
with Haskell symbolic info (not standard Linux coredump)? And
to generate it without any coding/hacking?
===
Best regards, Paul
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 114, Issue 23
******************************************