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. Re:  Haskell Serialization (Ashish Agarwal)
   2.  Re: cabal haddock problem (Ashish Agarwal)
   3.  More Data.Vector fun (Philip Scott)
   4. Re:  More Data.Vector fun (Daniel Fischer)
   5.  foldl by foldr (Matt Andrew)
   6. Re:  foldl by foldr (Edward Z. Yang)
   7. Re:  foldl by foldr (Matt Andrew)


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

Message: 1
Date: Thu, 13 May 2010 15:40:55 -0400
From: Ashish Agarwal <[email protected]>
Subject: Re: [Haskell-beginners] Haskell Serialization
To: "Stephen Blackheath [to Haskell-Beginners]"
        <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

Thanks for the reference. Sounds like a neat idea so I'll definitely check
it out!

Right now, there are lots of complications in designing this library, at
least if I want a nice API. I have lots and lots of type definitions to
encode the various constructs of the protocol I'm working with. New versions
could make arbitrary changes: add or remove a field to a record, change the
type of a record field, add or remove one of several possible cases (which I
encode with sum types), and so on. It's not at all clear to me yet how I
should factor my type definitions, and/or use other features such as
polymorphic types, type classes, etc. But I guess this is a different
thread.

On Wed, May 12, 2010 at 4:35 PM, Stephen Blackheath [to Haskell-Beginners] <
[email protected]> wrote:

> Ashish,
>
> I've been using XML picklers for serialization, which you might want to
> consider if you can handle a little extra CPU/data cost.  I'm the author
> of hexpat-pickle, so this is a plug for my package.  I lifted the idea
> wholesale from the HXT package.
>
> Handling changes in protocol version is really easy.  I've been doing a
> lot of
>
> xpSomething = new `xpTryCatch` old
>  where
>     new = ...
>     old = ...
>
> If the new encoding fails to parse, it tries the old one, and on
> transmission it uses the new one.  I find it very quick and convenient
> to bang out a new pickler now that I'm familiar with it.  You write your
> pickler and your unpickler with the same code - it works well.
>
> I am also working on hexpat-iteratee which is a lot more socket friendly
> since it doesn't use Haskell's lazy I/O.  The learning curve increases a
> little bit with doing it that way, though.
>
>
> Steve
>
> On 13/05/10 05:15, Ashish Agarwal wrote:
> > Thanks for all the advice. It seems I should avoid making my types
> > instances of Binary. In fact, this relates to another design issue I've
> > been grappling with, supporting multiple versions of the protocol.
> > Probably I'll need something like, putProtVersion1, putProtVersion2,
> > etc. Or something along this line.
> _______________________________________________
> 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/20100513/afd516bd/attachment-0001.html

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

Message: 2
Date: Thu, 13 May 2010 15:44:16 -0400
From: Ashish Agarwal <[email protected]>
Subject: [Haskell-beginners] Re: cabal haddock problem
To: Haskell-beginners <[email protected]>
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

I fixed this. The problem was that we had pulled in files from elsewhere
(the AFRP library), which were not written with haddock in mind. I was able
to get haddock to work by escaping all occurrences of $'s and *'s that came
after a dash. Too bad the error message is unrelated to the problem.


On Wed, Apr 21, 2010 at 6:30 PM, Ashish Agarwal <[email protected]>wrote:

> I've setup a cabal file for a project. Building works fine but cabal
> haddock gives the following error:
>
> src/AFRP/AFRP.hs:1:0:
>     File name does not match module name:
>     Saw: `Main'
>     Expected: `AFRP.AFRP'
>
> However, the module name specified in AFRP.hs is in fact AFRP.AFRP so why
> is haddock seeing Main, especially since cabal build completes successfully?
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20100513/00a8711b/attachment-0001.html

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

Message: 3
Date: Thu, 13 May 2010 22:26:31 +0100
From: Philip Scott <[email protected]>
Subject: [Haskell-beginners] More Data.Vector fun
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

Hey ho,

This should be a quick one, I hope! I am mucking about with the moadic 
initialisers for vectors, and am experiencing all kinds of fun. For 
example, I think I should be able to make a new vector of, say, 3 
thingies* long:

Prelude Data.Vector.Generic.Mutable> new 3

Now of course, poor old haskell does not know what sort of thing I am 
going to put in it yet, so it gets a bit cross:

<interactive>:1:0:
     Ambiguous type variable `m' in the constraint:
       `Control.Monad.Primitive.PrimMonad m'
         arising from a use of `new' at <interactive>:1:0-4
     Probable fix: add a type signature that fixes these type variable(s)

<interactive>:1:0:
     Ambiguous type variables `v', `a' in the constraint:
       `MVector v a' arising from a use of `new' at <interactive>:1:0-4
     Probable fix: add a type signature that fixes these type variable(s)


Now I have paid very close attention to this 'Probable Fix', but have 
not been able to find out precisely how to specify what sort of vector I 
would like (I guess this is v..) and am even more boggled by 'm'.. I 
guess this is asking what sort of Monad I am going to use to fill it up? 
An example would be greatly appreciated.

I find the type of 'new' quite interesting:

new
   :: forall (m :: * -> *) (v :: * -> * -> *) a.
      (PrimMonad m, Data.Vector.Generic.Mutable.MVector v a) =>
      Int -> m (v (PrimState m) a)

So I did a bit of reading up on exstential types, and I (just about) get 
what 'forall' does, however I don't know what the (m :: * -> *) and  (v 
:: * -> * -> *) bits mean. Just telling me what they are called would 
probably be enough to get me googling!

I get the feeling I am a bit out of my depth here, but hey ho, I guess 
that's how you learn to swim!

All the best,

Phil

* thingies == things of some type, which doesn't really matter at the 
momant.


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

Message: 4
Date: Fri, 14 May 2010 00:04:50 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] More Data.Vector fun
To: [email protected], [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="iso-8859-1"

On Thursday 13 May 2010 23:26:31, Philip Scott wrote:
> Hey ho,
>
> This should be a quick one, I hope! I am mucking about with the moadic
> initialisers for vectors, and am experiencing all kinds of fun. For
> example, I think I should be able to make a new vector of, say, 3
> thingies* long:
>
> Prelude Data.Vector.Generic.Mutable> new 3
>
> Now of course, poor old haskell does not know what sort of thing I am
> going to put in it yet, so it gets a bit cross:
>
> <interactive>:1:0:
>      Ambiguous type variable `m' in the constraint:
>        `Control.Monad.Primitive.PrimMonad m'
>          arising from a use of `new' at <interactive>:1:0-4
>      Probable fix: add a type signature that fixes these type
> variable(s)
>
> <interactive>:1:0:
>      Ambiguous type variables `v', `a' in the constraint:
>        `MVector v a' arising from a use of `new' at <interactive>:1:0-4
>      Probable fix: add a type signature that fixes these type
> variable(s)
>
>
> Now I have paid very close attention to this 'Probable Fix', but have
> not been able to find out precisely how to specify what sort of vector I
> would like (I guess this is v..) and am even more boggled by 'm'.. I
> guess this is asking what sort of Monad I am going to use to fill it up?

Yes, 'm' is the Monad. All known (by me) instances of PrimMonad are IO and 
(ST s)
'v' is the type of vector you want (very likely MVector [the datatype, not 
the class]), 'a' is the type of elements (Int, Char, ...).

But if you give a type signature, e.g.

Prelude Data.Vector.Generic.Mutable> new 3 :: IO (MVector (PrimState IO) 
Char)

you'll very likely meet "cannot find Show instance for ..."

> An example would be greatly appreciated.
>
> I find the type of 'new' quite interesting:
>
> new
>
>    :: forall (m :: * -> *) (v :: * -> * -> *) a.
>
>       (PrimMonad m, Data.Vector.Generic.Mutable.MVector v a) =>
>       Int -> m (v (PrimState m) a)
>
> So I did a bit of reading up on exstential types,

However, this is not an existential type, just an explicitly universally 
quantified type.

> and I (just about) get
> what 'forall' does, however I don't know what the (m :: * -> *) and  (v
>
> :: * -> * -> *) bits mean. Just telling me what they are called would
>
> probably be enough to get me googling!

Those are kind signatures.

m :: * -> *

means that the type variable m stands for a type constructor which takes 
one type and produces a type from that. Well known examples are [], Maybe, 
IO, (State Int).

v :: * -> * -> *

means that v stands for a type constructor taking two types and producing a 
type. Examples: Either, State.

* is the kind of types (Int, Char, Bool, [Double], Maybe (), ...)
If k1 and k2 are kinds, k1 -> k2 is the kind of type-expressions that take 
a type-expression of kind k1 to produce one of kind k2.
For example, the kind of StateT is

StateT :: * -> (* -> *) -> * -> *

, that means StateT takes
- one type (the state)
- one type constructor producing a type from a type (like IO, Maybe, [])
- another type (the result type)
and produces a type (e.g. StateT Int [] Bool).

>
> I get the feeling I am a bit out of my depth here, but hey ho, I guess
> that's how you learn to swim!
>
> All the best,
>
> Phil
>
> * thingies == things of some type, which doesn't really matter at the
> momant.



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

Message: 5
Date: Fri, 14 May 2010 12:29:01 +1000
From: Matt Andrew <[email protected]>
Subject: [Haskell-beginners] foldl by foldr
To: [email protected]
Message-ID: <m24oibqkr6.wl%[email protected]>
Content-Type: text/plain; charset=US-ASCII

Hi all,

I am working my way through 'Real World Haskell' as my first introduction to 
Haskell and am thoroughly enjoying both it and Haskell. I have, however, hit 
something of a brick wall in figuring out one of the examples used in chapter 
4. The example code is flagged as 'non-trivial' to understand, but I am trying 
to understand each thing presented reasonably well before I move on. I was 
hoping someone could shed some light on the issue for me; I have done a fair 
amount of searching and have not found an answer, so apologies if this has come 
up before and I have not found it.

The offending code is:

myFoldl :: (a -> b -> a) -> a -> [b] -> a
myFoldl f zero xs = foldr step id xs zero
    where step x g a = g (f a x)

The thing I am having trouble understanding is what the 'id' function is doing 
in a function that expects 3 arguments and is given 4 (foldr).

Here is the breakdown of where I get stuck: My understanding of what id does as 
a function is that it returns the identity of the object it is passed. Playing 
with ghci gives me the type signature (a -> a) and as expected id returns the 
argument it is given unaltered. Now I believe Haskell does not allow the 
passing of more arguments to a function than it expects. Therefore I am driven 
to the conclusion that Haskell is treating the call to 'foldr' above as either:

foldr step (id xs) zero 
(i.e. the second argument is the thunk (id xs))

or:
foldr (step id) xs zero
(i.e. given that 'step' is defined above as taking 3 arguments, whereas the 
type signature for 'foldr' expects a function taking 2 arguments it is 
accepting (step id) as a partially applied function taking two arguments)

The problem is that having simulated the above scope in ghci for an identical 
call to 'foldr,' explicitly parenthesising the call to 'foldr' in either of the 
above ways throws an error. In addition to this either way of understanding the 
function doesn't make much sense to me: the first seems entirely redundant, the 
second would result in 'id' being called by 'step' in a position where it has 
no arguments and thus would surely result in an error.

I'm obviously missing an important part of the puzzle and would appreciate any 
help in understanding this code.

Appreciate you taking the time to read this,

Matt Andrew


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

Message: 6
Date: Thu, 13 May 2010 22:40:39 -0400
From: "Edward Z. Yang" <[email protected]>
Subject: Re: [Haskell-beginners] foldl by foldr
To: Matt Andrew <[email protected]>
Cc: beginners <[email protected]>
Message-ID: <1273804667-sup-4...@ezyang>
Content-Type: text/plain; charset=UTF-8

Excerpts from Matt Andrew's message of Thu May 13 22:29:01 -0400 2010:
> The offending code is:
> 
> myFoldl :: (a -> b -> a) -> a -> [b] -> a
> myFoldl f zero xs = foldr step id xs zero
>     where step x g a = g (f a x)
> 
> The thing I am having trouble understanding is what the 'id' function is
> doing in a function that expects 3 arguments and is given 4 (foldr).

The trick between building foldl with foldr is the fact that, where other
folds might be accumulating values like integers or lists, this foldr is
accumulating *functions*.  This parenthesized version is equivalent:

    myFoldl f zero xs = (foldr step id xs) zero

Since foldr is returning a function.  In this case, id seems like a natural
base case, since it is an extremely simple function.

Hint: Try specializing this over some f and zero you know well, like + and 0,
and then manually drawing out the structure and seeing what happens.

Cheers,
Edward


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

Message: 7
Date: Fri, 14 May 2010 13:12:53 +1000
From: Matt Andrew <[email protected]>
Subject: Re: [Haskell-beginners] foldl by foldr
To: "Edward Z. Yang" <[email protected]>
Cc: [email protected]
Message-ID: <m239xvqiq2.wl%[email protected]>
Content-Type: text/plain; charset=US-ASCII

Thanks Edward, that makes a lot more sense.

Matt Andrew

At Thu, 13 May 2010 22:40:39 -0400,
Edward Z. Yang wrote:
> 
> Excerpts from Matt Andrew's message of Thu May 13 22:29:01 -0400 2010:
> > The offending code is:
> > 
> > myFoldl :: (a -> b -> a) -> a -> [b] -> a
> > myFoldl f zero xs = foldr step id xs zero
> >     where step x g a = g (f a x)
> > 
> > The thing I am having trouble understanding is what the 'id' function is
> > doing in a function that expects 3 arguments and is given 4 (foldr).
> 
> The trick between building foldl with foldr is the fact that, where other
> folds might be accumulating values like integers or lists, this foldr is
> accumulating *functions*.  This parenthesized version is equivalent:
> 
>     myFoldl f zero xs = (foldr step id xs) zero
> 
> Since foldr is returning a function.  In this case, id seems like a natural
> base case, since it is an extremely simple function.
> 
> Hint: Try specializing this over some f and zero you know well, like + and 0,
> and then manually drawing out the structure and seeing what happens.
> 
> Cheers,
> Edward


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

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


End of Beginners Digest, Vol 23, Issue 17
*****************************************

Reply via email to