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.   Problem with catching error (Alexey G)
   2. Re:  Problem with catching error (David McBride)
   3. Re:  Problem with catching error (Daniel Fischer)
   4. Re:  Network client - reading and writing to a    socket
      (David McBride)
   5. Re:  Network client - reading and writing to a    socket
      (David McBride)
   6. Re:  Haskell-style types in C or C++ (Isaac Dupree)
   7.  A question on types (C K Kashyap)


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

Message: 1
Date: Mon, 1 Aug 2011 00:24:13 +0300
From: Alexey G <[email protected]>
Subject: [Haskell-beginners]  Problem with catching error
To: [email protected]
Message-ID:
        <caordkxvbhwh-ogfdztoricfcznteygip3nt90qt9nvhc0zb...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

Hello. I have some problem with catching error.

My function try to read value from string and if it's fails - return default
(d).
With this:
>import Control.Exception
>readIOWith :: Read a => a -> String -> IO a
>readIOWith d x = catch tryRead errHandler
>    where errHandler :: SomeException -> IO a
>              errHandler _ = return d
>              tryRead        = readIO x

I have error:

Could not deduce (a ~ a1)
    from the context (Read a)
      bound by the type signature for
                 readIOWith :: Read a => a -> String -> IO a
      at BWClub/Common/Helpers/Packets.hs:(19,1)-(22,33)
      `a' is a rigid type variable bound by
          the type signature for readIOWith :: Read a => a -> String -> IO a
          at BWClub/Common/Helpers/Packets.hs:19:1
      `a1' is a rigid type variable bound by
           the type signature for errHandler :: SomeException -> IO a1
           at BWClub/Common/Helpers/Packets.hs:21:11
    In the first argument of `return', namely `d'
    In the expression: return d
    In an equation for `errHandler': errHandler _ = return d

But this code works:
>readIOWith d x = catch tryRead (\e -> print (e :: SomeException) >> return
d)
>    where tryRead      = readIO x

Sorry for my english.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20110801/13fd1da4/attachment-0001.htm>

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

Message: 2
Date: Sun, 31 Jul 2011 17:59:18 -0400
From: David McBride <[email protected]>
Subject: Re: [Haskell-beginners] Problem with catching error
To: Alexey G <[email protected]>
Cc: [email protected]
Message-ID:
        <can+tr42k88drmvn2bnusbk0qwjmlmxp20dfrdayclrchrt5...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

The problem is it can't be sure that the a in readIOWith is the same
as the a in errHandler based on how it is used because the errHandler
version is ignored entirely in this case.

There are two options:

Remove the type signature of errHandler, and the compiler will infer
it for you.  Unfortunately that won't work in this case because the
exception type is ambiguous, so you need the signature to make things
works.

The second option is add the ScopedTypeVariables extension to your
code, then add a forall a. to the signature of readIOWith.  The code
will look like this:

{-# LANGUAGE ScopedTypeVariables #-}
import Control.Exception as E

readIOWith :: forall a. Read a => a -> String -> IO a
readIOWith d x = E.catch tryRead errHandler
  where errHandler :: SomeException -> IO a
        errHandler _ = return d
        tryRead        = readIO x

What this does is you are telling it that it should assume that all
a's in any part of the function as the same type.  The forall is
normally implied but for some reason this extension requires that you
specify it.  Remember that if your function has several type variables
in it, like a, b, and c, the forall needs to cover them all (forall a
b c.).

On Sun, Jul 31, 2011 at 5:24 PM, Alexey G <[email protected]> wrote:
> Hello. I have some problem with catching error.
> My function try to read value from string and if it's fails - return default
> (d).
> With this:
>>import Control.Exception
>>readIOWith :: Read a => a -> String -> IO a
>>readIOWith d x = catch tryRead errHandler
>> ? ?where errHandler :: SomeException -> IO a
>> ? ? ? ? ? ? ?errHandler _ = return d
>> ? ? ? ? ? ? ?tryRead ? ? ? ?= readIO x
> I have error:
>
> Could not deduce (a ~ a1)
> ? ? from the context (Read a)
> ? ? ? bound by the type signature for
> ? ? ? ? ? ? ? ? ?readIOWith :: Read a => a -> String -> IO a
> ? ? ? at BWClub/Common/Helpers/Packets.hs:(19,1)-(22,33)
> ? ? ? `a' is a rigid type variable bound by
> ? ? ? ? ? the type signature for readIOWith :: Read a => a -> String -> IO a
> ? ? ? ? ? at BWClub/Common/Helpers/Packets.hs:19:1
> ? ? ? `a1' is a rigid type variable bound by
> ? ? ? ? ? ?the type signature for errHandler :: SomeException -> IO a1
> ? ? ? ? ? ?at BWClub/Common/Helpers/Packets.hs:21:11
> ? ? In the first argument of `return', namely `d'
> ? ? In the expression: return d
> ? ? In an equation for `errHandler': errHandler _ = return d
>
> But this code works:
>>readIOWith d x = catch tryRead (\e -> print (e :: SomeException) >> return
>> d)
>> ? ?where tryRead ? ? ?= readIO x
> Sorry for my english.
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>



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

Message: 3
Date: Sun, 31 Jul 2011 23:59:15 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Problem with catching error
To: [email protected]
Message-ID: <[email protected]>
Content-Type: Text/Plain;  charset="utf-8"

On Sunday 31 July 2011, 23:24:13, Alexey G wrote:
> Hello. I have some problem with catching error.
> 
> My function try to read value from string and if it's fails - return
> default (d).
> 
> With this:
> >import Control.Exception
> >readIOWith :: Read a => a -> String -> IO a
> >readIOWith d x = catch tryRead errHandler
> >
> >    where errHandler :: SomeException -> IO a

There's your problem. The type variable 'a' in the local signature is 
completely unrelated to the type variable 'a' in the top-level signature 
(type variables have an implicit forall in Haskell), so the local signature 
says "errHandler can return `IO anything'", but it can of course only 
return `IO (Type of d)'.

If you remove the signature, it should compile.
(Another possibility is to bring the type variable into scope, that would 
require the ScopedTypeVariables language extension and an explicit forall.)

However, this shouldn't need any IO at all,

import Data.Char -- for isSpace

readWithDefault :: Read a => a -> String -> a
readWithDefault d s =
    case reads s of
      [(result,remaining)]
        | all isSpace remaining -> result
      _ -> d

If you really need the IO return type:

readIOWith d = return . readWithDefault d

> >    
> >              errHandler _ = return d
> >              tryRead        = readIO x
> 
> I have error:
> 
> Could not deduce (a ~ a1)
>     from the context (Read a)
>       bound by the type signature for
>                  readIOWith :: Read a => a -> String -> IO a
>       at BWClub/Common/Helpers/Packets.hs:(19,1)-(22,33)
>       `a' is a rigid type variable bound by
>           the type signature for readIOWith :: Read a => a -> String ->
> IO a at BWClub/Common/Helpers/Packets.hs:19:1
>       `a1' is a rigid type variable bound by
>            the type signature for errHandler :: SomeException -> IO a1
>            at BWClub/Common/Helpers/Packets.hs:21:11
>     In the first argument of `return', namely `d'
>     In the expression: return d
>     In an equation for `errHandler': errHandler _ = return d
> 
> But this code works:
> >readIOWith d x = catch tryRead (\e -> print (e :: SomeException) >>
> >return
> 
> d)
> 
> >    where tryRead      = readIO x

Here, there's no signature introducing a fresh type variable.

> 
> Sorry for my english.




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

Message: 4
Date: Sun, 31 Jul 2011 18:21:07 -0400
From: David McBride <[email protected]>
Subject: Re: [Haskell-beginners] Network client - reading and writing
        to a    socket
To: Patrick LeBoutillier <[email protected]>
Cc: [email protected], Manfred Lotz <[email protected]>
Message-ID:
        <CAN+Tr40TKLXW0uzASzVSQT=h=tzg6SFvXF_RhpnsjK=j7mi...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

It seems you are expected to fetch until you have the entire thing.
You can't just fetch 40000 and hope you get it all.  If you do it
blocking, it will be almost guaranteed to block unless there were
40000 to fetch.  If you do it non blocking, you have to keep fetching
until you are sure you have the entire message.

If you are doing this as simply as possible, that means you have to
fetch, and then repeatedly check the string until you are sure you
have the whole thing.  You'll do that by fetching once, checking the
beginning of the string for the {342} in the following example from
the rfc:

C:    a004 fetch 12 body[header]
S:    * 12 FETCH (BODY[HEADER] {342}
S:    Date: Wed, 17 Jul 1996 02:23:25 -0700 (PDT)
S:    From: Terry Gray <[email protected]>
S:    Subject: IMAP4rev1 WG mtg summary and minutes
S:    To: [email protected]
S:    cc: [email protected], John Klensin <[email protected]>
S:    Message-Id: <[email protected]>
S:    MIME-Version: 1.0
S:    Content-Type: TEXT/PLAIN; CHARSET=US-ASCII
S:
S:    )

And then fetching until you have the next 342 characters.  This is
pretty tedious in any language, not just haskell, but that's how this
protocol is.

If you want to do this the haskell way, which I highly recommend, I
would pull out the network-attoparsec library.  You should be able to
use parseWith and pass it a recv function that returns bytestrings.
Write a simple parser which will look for "* <num> FETCH
([a-zA-Z]+\[[a-zA-Z]+\] {[0-9]+} and then use that last number to take
(take is an attoparsec function) that last byte count.  If you wire
this up to your network socket, it will parse and slurp up the
response and it will pull the exact number of bytes needed and your
code will be very elegant.  Attoparsec is a little hard to use at
first, but it is well worth if if you have a mind to parse internet
protocols.

On Sun, Jul 31, 2011 at 3:14 PM, Patrick LeBoutillier
<[email protected]> wrote:
> Manfred,
>
>> The problem is that the message itself is some 30K big and I only
>> get some 16K of the message.
>>
>> How could I force to get the whole message?
>
> My guess is that you can't. This call:
>
> c' <- B.hGetNonBlocking h 40000
>
> tries to read as much as it can (up to 40000 bytes) but it won't block
> to wait for data. Perhaps the rest of your message is in a different
> TCP packet or delayed or whatever, but I think you have to keep on
> reading (and maybe block) until you know you have read the entire
> message. The IMAP specs will tell you how to identify the "end of the
> message".
>
> BTW: This issue is not Haskell specific. If you implement the same
> code in C, Perl or Java you will have to deal with the same problem.
> When you read from a socket, there is no general way of knowing that
> the other side has sent everything.
>
>
> Patrick
>
>>
>> --
>> Manfred
>>
>>
>>
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners
>>
>
>
>
> --
> =====================
> Patrick LeBoutillier
> Rosem?re, Qu?bec, Canada
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>



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

Message: 5
Date: Sun, 31 Jul 2011 18:22:19 -0400
From: David McBride <[email protected]>
Subject: Re: [Haskell-beginners] Network client - reading and writing
        to a    socket
To: Patrick LeBoutillier <[email protected]>
Cc: [email protected], Manfred Lotz <[email protected]>
Message-ID:
        <can+tr402v6xcnfq3rnagcbyadbegnntf6mvrumbnss99cnh...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

Sorry I said network-attoparsec, the attoparsec library is all you
need.  I'm not sure where I got network-attoparsec from.

On Sun, Jul 31, 2011 at 6:21 PM, David McBride <[email protected]> wrote:
> It seems you are expected to fetch until you have the entire thing.
> You can't just fetch 40000 and hope you get it all. ?If you do it
> blocking, it will be almost guaranteed to block unless there were
> 40000 to fetch. ?If you do it non blocking, you have to keep fetching
> until you are sure you have the entire message.
>
> If you are doing this as simply as possible, that means you have to
> fetch, and then repeatedly check the string until you are sure you
> have the whole thing. ?You'll do that by fetching once, checking the
> beginning of the string for the {342} in the following example from
> the rfc:
>
> C: ? ?a004 fetch 12 body[header]
> S: ? ?* 12 FETCH (BODY[HEADER] {342}
> S: ? ?Date: Wed, 17 Jul 1996 02:23:25 -0700 (PDT)
> S: ? ?From: Terry Gray <[email protected]>
> S: ? ?Subject: IMAP4rev1 WG mtg summary and minutes
> S: ? ?To: [email protected]
> S: ? ?cc: [email protected], John Klensin <[email protected]>
> S: ? ?Message-Id: <[email protected]>
> S: ? ?MIME-Version: 1.0
> S: ? ?Content-Type: TEXT/PLAIN; CHARSET=US-ASCII
> S:
> S: ? ?)
>
> And then fetching until you have the next 342 characters. ?This is
> pretty tedious in any language, not just haskell, but that's how this
> protocol is.
>
> If you want to do this the haskell way, which I highly recommend, I
> would pull out the network-attoparsec library. ?You should be able to
> use parseWith and pass it a recv function that returns bytestrings.
> Write a simple parser which will look for "* <num> FETCH
> ([a-zA-Z]+\[[a-zA-Z]+\] {[0-9]+} and then use that last number to take
> (take is an attoparsec function) that last byte count. ?If you wire
> this up to your network socket, it will parse and slurp up the
> response and it will pull the exact number of bytes needed and your
> code will be very elegant. ?Attoparsec is a little hard to use at
> first, but it is well worth if if you have a mind to parse internet
> protocols.
>
> On Sun, Jul 31, 2011 at 3:14 PM, Patrick LeBoutillier
> <[email protected]> wrote:
>> Manfred,
>>
>>> The problem is that the message itself is some 30K big and I only
>>> get some 16K of the message.
>>>
>>> How could I force to get the whole message?
>>
>> My guess is that you can't. This call:
>>
>> c' <- B.hGetNonBlocking h 40000
>>
>> tries to read as much as it can (up to 40000 bytes) but it won't block
>> to wait for data. Perhaps the rest of your message is in a different
>> TCP packet or delayed or whatever, but I think you have to keep on
>> reading (and maybe block) until you know you have read the entire
>> message. The IMAP specs will tell you how to identify the "end of the
>> message".
>>
>> BTW: This issue is not Haskell specific. If you implement the same
>> code in C, Perl or Java you will have to deal with the same problem.
>> When you read from a socket, there is no general way of knowing that
>> the other side has sent everything.
>>
>>
>> Patrick
>>
>>>
>>> --
>>> Manfred
>>>
>>>
>>>
>>> _______________________________________________
>>> Beginners mailing list
>>> [email protected]
>>> http://www.haskell.org/mailman/listinfo/beginners
>>>
>>
>>
>>
>> --
>> =====================
>> Patrick LeBoutillier
>> Rosem?re, Qu?bec, Canada
>>
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners
>>
>



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

Message: 6
Date: Sun, 31 Jul 2011 20:51:15 -0400
From: Isaac Dupree <[email protected]>
Subject: Re: [Haskell-beginners] Haskell-style types in C or C++
To: beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

On 07/31/11 15:58, Ertugrul Soeylemez wrote:
> Unions work, too, but they are not nearly as clean.  In general, avoid
> unions in C++, even though they seem to be a natural way to express
> ADTs.  They really are not.  Class hierarchies are the way to go.

Or boost::variant.  It's actual algebraic data types, at the expense of 
mighty weird syntax for pattern-matching because the Boost developers 
had to come up with something that compiled in C++.

~Isaac



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

Message: 7
Date: Mon, 1 Aug 2011 13:22:28 +0530
From: C K Kashyap <[email protected]>
Subject: [Haskell-beginners] A question on types
To: beginners <[email protected]>
Message-ID:
        <CAGdT1gqDkjT3PbqbCtOtRbnYZ8sPXj=0zdkqazwes87nis7...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

Hi,
To clarify my understanding, I created this table

|         | weak | strong  |
|---------+------+---------|
| dynamic | perl | Ruby    |
| static  | C    | Haskell |
|---------+------+---------|

Could someone please ratify this?
Regards,
Kashyap
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20110801/54c59243/attachment.htm>

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

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


End of Beginners Digest, Vol 38, Issue 1
****************************************

Reply via email to