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.  Y-combinator (Jon Harrop)
   2. Re:  Y-combinator ([email protected])
   3. Re:  Y-combinator (Edward Z. Yang)
   4. Re:  Y-combinator (Jon Harrop)
   5. Re:  Y-combinator (Brent Yorgey)
   6.  State Transformer Confusion (Chris Pettitt)
   7. Re:  Y-combinator (Jon Harrop)
   8. Re:  State Transformer Confusion (Daniel Fischer)
   9. Re:  State Transformer Confusion (Daniel Fischer)
  10. Re:  State Transformer Confusion (Chris Pettitt)


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

Message: 1
Date: Wed, 24 Feb 2010 20:41:01 +0000
From: Jon Harrop <[email protected]>
Subject: [Haskell-beginners] Y-combinator
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="us-ascii"


OCaml can do this:

$ ocaml -rectypes
        Objective Caml version 3.11.1

# let fix f = (fun x -> f(x x)) (fun x y -> f(x x) y);;
val fix : (('a -> 'b) -> 'a -> 'b) -> 'a -> 'b = <fun>

# fix (fun f -> function 0 -> 1 | n -> n*f(n-1)) 10;;
- : int = 3628800

Haskell 98 doesn't seem to be able to type check that definition of the 
y-combinator directly. Is there a Haskell extension that would let it handle 
this?

-- 
Dr Jon Harrop, Flying Frog Consultancy Ltd.
http://www.ffconsultancy.com/?e


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

Message: 2
Date: Wed, 24 Feb 2010 17:30:19 -0300
From: [email protected]
Subject: Re: [Haskell-beginners] Y-combinator
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=utf-8

http://lmgtfy.com/?q=y+combinator+haskell

Cheers,
Edgar

On Wed, 24/Feb/2010 at 20:41 +0000, Jon Harrop wrote:
> 
> OCaml can do this:
> 
> $ ocaml -rectypes
>         Objective Caml version 3.11.1
> 
> # let fix f = (fun x -> f(x x)) (fun x y -> f(x x) y);;
> val fix : (('a -> 'b) -> 'a -> 'b) -> 'a -> 'b = <fun>
> 
> # fix (fun f -> function 0 -> 1 | n -> n*f(n-1)) 10;;
> - : int = 3628800
> 
> Haskell 98 doesn't seem to be able to type check that definition of the 
> y-combinator directly. Is there a Haskell extension that would let it handle 
> this?
> 
> -- 
> Dr Jon Harrop, Flying Frog Consultancy Ltd.
> http://www.ffconsultancy.com/?e
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners


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

Message: 3
Date: Wed, 24 Feb 2010 15:53:14 -0500
From: "Edward Z. Yang" <[email protected]>
Subject: Re: [Haskell-beginners] Y-combinator
To: Jon Harrop <[email protected]>
Cc: beginners <[email protected]>
Message-ID: <1267044766-sup-1...@ezyang>
Content-Type: text/plain; charset=UTF-8

Excerpts from Jon Harrop's message of Wed Feb 24 15:41:01 -0500 2010:
> OCaml can do this:
> 
> $ ocaml -rectypes
>         Objective Caml version 3.11.1
> 
> # let fix f = (fun x -> f(x x)) (fun x y -> f(x x) y);;
> val fix : (('a -> 'b) -> 'a -> 'b) -> 'a -> 'b = <fun>
> 
> # fix (fun f -> function 0 -> 1 | n -> n*f(n-1)) 10;;
> - : int = 3628800

http://haskell.org/hoogle/3/?q=fix

Cheers,
Edward


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

Message: 4
Date: Wed, 24 Feb 2010 23:53:50 +0000
From: Jon Harrop <[email protected]>
Subject: Re: [Haskell-beginners] Y-combinator
To: "Edward Z. Yang" <[email protected]>
Cc: beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="utf-8"

On Wednesday 24 February 2010 20:53:14 Edward Z. Yang wrote:
> http://haskell.org/hoogle/3/?q=fix

That leads to the following source:

http://haskell.org/ghc/docs/latest/html/libraries/base/src/Data-Function.html

but it uses a workaround rather than extending the type system. I'm looking 
for an equivalent of OCaml's -rectypes that enables recursive types in 
Haskell (e.g. in ghci)?

-- 
Dr Jon Harrop, Flying Frog Consultancy Ltd.
http://www.ffconsultancy.com/?e


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

Message: 5
Date: Wed, 24 Feb 2010 19:31:59 -0500
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] Y-combinator
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

On Wed, Feb 24, 2010 at 11:53:50PM +0000, Jon Harrop wrote:
> On Wednesday 24 February 2010 20:53:14 Edward Z. Yang wrote:
> > http://haskell.org/hoogle/3/?q=fix
> 
> That leads to the following source:
> 
> http://haskell.org/ghc/docs/latest/html/libraries/base/src/Data-Function.html
> 
> but it uses a workaround rather than extending the type system. I'm looking 
> for an equivalent of OCaml's -rectypes that enables recursive types in 
> Haskell (e.g. in ghci)?

Haskell has recursive types, but they are iso-recursive rather than
equi-recursive; the recursion must always be guarded by a data
constructor.  I am not sure what you mean by saying that Data.Function
contains a "workaround".  What exactly are you trying to do?

-Brent


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

Message: 6
Date: Wed, 24 Feb 2010 18:04:02 -0800
From: Chris Pettitt <[email protected]>
Subject: [Haskell-beginners] State Transformer Confusion
To: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="utf-8"

Hello Haskell-Beginners,

I'm having a fair amount of difficulty understanding the ST monad. In
my basic experimentation I'm finding that I'm getting more heap
allocated with STUArray than with a regular Array, which is not what I
would expect.

I've attached a sample program that attempts to sum a list of Ints
into an array of length 1, using STUArray and UArray. The idea is
based loosely on http://www.haskell.org/haskellwiki/Monad/ST, but
using an array instead of an STRef.

I compile using these arguments: ghc -fforce-recomp -O2 -prof
-auto-all -caf-all --make sttest.hs

When I run the ST-based function (sumInPlace1), I get the following output:

./sttest +RTS -sstderr
1784293664
     240,944,212 bytes allocated in the heap
          35,448 bytes copied during GC
           4,052 bytes maximum residency (1 sample(s))
          12,332 bytes maximum slop
               1 MB total memory in use (0 MB lost due to fragmentation)

  Generation 0:   459 collections,     0 parallel,  0.00s,  0.00s elapsed
  Generation 1:     1 collections,     0 parallel,  0.00s,  0.00s elapsed

  INIT  time    0.00s  (  0.00s elapsed)
  MUT   time    0.30s  (  0.31s elapsed)
  GC    time    0.00s  (  0.00s elapsed)
  RP    time    0.00s  (  0.00s elapsed)
  PROF  time    0.00s  (  0.00s elapsed)
  EXIT  time    0.00s  (  0.00s elapsed)
  Total time    0.30s  (  0.31s elapsed)

  %GC time       0.8%  (1.2% elapsed)

  Alloc rate    804,910,126 bytes per MUT second

  Productivity  99.0% of total user, 96.5% of total elapsed



While with the Array based function (sumInPlace2), I get the following output:

./sttest +RTS -sstderr
1784293664
     173,627,572 bytes allocated in the heap
          22,596 bytes copied during GC
           3,920 bytes maximum residency (1 sample(s))
          12,464 bytes maximum slop
               1 MB total memory in use (0 MB lost due to fragmentation)

  Generation 0:   333 collections,     0 parallel,  0.00s,  0.00s elapsed
  Generation 1:     1 collections,     0 parallel,  0.00s,  0.00s elapsed

  INIT  time    0.00s  (  0.00s elapsed)
  MUT   time    0.14s  (  0.15s elapsed)
  GC    time    0.00s  (  0.00s elapsed)
  RP    time    0.00s  (  0.00s elapsed)
  PROF  time    0.00s  (  0.00s elapsed)
  EXIT  time    0.00s  (  0.00s elapsed)
  Total time    0.15s  (  0.15s elapsed)

  %GC time       1.3%  (1.9% elapsed)

  Alloc rate    1,200,569,571 bytes per MUT second

  Productivity  98.3% of total user, 94.7% of total elapsed




One additional point of confusion for me: when I run either function
with +RTS -hc and use hp2ps I get an empty graph. I've seen these
tools work quite well before, so I suspect I'm doing something wrong
now.

Thanks,
Chris
-------------- next part --------------
A non-text attachment was scrubbed...
Name: sttest.hs
Type: application/octet-stream
Size: 589 bytes
Desc: not available
Url : 
http://www.haskell.org/pipermail/beginners/attachments/20100224/8665b2ea/sttest-0001.obj

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

Message: 7
Date: Thu, 25 Feb 2010 03:32:25 +0000
From: Jon Harrop <[email protected]>
Subject: Re: [Haskell-beginners] Y-combinator
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="iso-8859-1"

On Thursday 25 February 2010 00:31:59 Brent Yorgey wrote:
> On Wed, Feb 24, 2010 at 11:53:50PM +0000, Jon Harrop wrote:
> > On Wednesday 24 February 2010 20:53:14 Edward Z. Yang wrote:
> > > http://haskell.org/hoogle/3/?q=fix
> >
> > That leads to the following source:
> >
> > http://haskell.org/ghc/docs/latest/html/libraries/base/src/Data-Function.
> >html
> >
> > but it uses a workaround rather than extending the type system. I'm
> > looking for an equivalent of OCaml's -rectypes that enables recursive
> > types in Haskell (e.g. in ghci)?
>
> Haskell has recursive types, but they are iso-recursive rather than
> equi-recursive; the recursion must always be guarded by a data
> constructor.  I am not sure what you mean by saying that Data.Function
> contains a "workaround".

Guarding the recursion with a constructor is the workaround I was referring 
to, like this:

# let fix f =
    (fun (`X x) -> f(x (`X x))) (`X(fun (`X x) y -> f(x (`X x)) y));;
val fix : (('a -> 'b) -> 'a -> 'b) -> 'a -> 'b = <fun>

> What exactly are you trying to do? 

I'm wondering if it is possible to get this to type in Haskell without 
altering the code, i.e. by enabling recursive types in the compiler as I did 
with OCaml using -rectypes.

-- 
Dr Jon Harrop, Flying Frog Consultancy Ltd.
http://www.ffconsultancy.com/?e


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

Message: 8
Date: Thu, 25 Feb 2010 03:54:17 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] State Transformer Confusion
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="utf-8"

Am Donnerstag 25 Februar 2010 03:04:02 schrieb Chris Pettitt:
> Hello Haskell-Beginners,
>
> I'm having a fair amount of difficulty understanding the ST monad. In
> my basic experimentation I'm finding that I'm getting more heap
> allocated with STUArray than with a regular Array, which is not what I
> would expect.

And ordinarily you don't. The problem is that the STUArray suffers very 
badly from profiling. Compiling without -prof, the STUArray code allocates 
about half as much as the UArray code and takes ~42% of the time.
With profiling, STUArray allocates ~40% more and takes ~50% longer than 
UArray.

Two things:
- profiling interacts badly with some optimisations, so what the profiling 
output says may deviate significantly from what your -O2 compiled 
production code actually does
- some datatypes suffer more from profiling than others, so what the 
profiling output says for different choices of datatype may deviate 
significantly from how your -O2 compiled production code behaves

Normally, -prof doesn't change the behaviour very much, but sometimes it 
does.


>
> One additional point of confusion for me: when I run either function
> with +RTS -hc and use hp2ps I get an empty graph. I've seen these
> tools work quite well before, so I suspect I'm doing something wrong
> now.

Too little actual heap usage and too short running time. Change the upper 
limit to 10 million and you should get a graph with actual bands.

>
> Thanks,
> Chris



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

Message: 9
Date: Thu, 25 Feb 2010 04:26:22 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] State Transformer Confusion
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="utf-8"

Am Donnerstag 25 Februar 2010 03:54:17 schrieb Daniel Fischer:
> Am Donnerstag 25 Februar 2010 03:04:02 schrieb Chris Pettitt:
> > Hello Haskell-Beginners,
> >
> > I'm having a fair amount of difficulty understanding the ST monad. In
> > my basic experimentation I'm finding that I'm getting more heap
> > allocated with STUArray than with a regular Array, which is not what I
> > would expect.
>
> And ordinarily you don't. The problem is that the STUArray suffers very
> badly from profiling. Compiling without -prof, the STUArray code
> allocates about half as much as the UArray code and takes ~42% of the
> time. With profiling, STUArray allocates ~40% more and takes ~50% longer
> than UArray.

Oh, and: In the UArray code, you specified the index type as Int, in the 
STUArray code, you didn't specify it, so it was taken to be Integer, which 
slows down performance significantly, changing sumInPlace1 to

sumInPlace1 :: [Int] -> Int
sumInPlace1 xs = (! 0) . runSTUArray $ do
        a <- newArray (0 :: Int, 0) 0
        forM_ xs $ \x -> do
            x' <- (readArray a 0)
            writeArray a 0 (x' + x)
        return a

makes the STUArray code allocate ~6% of what the UArray code allocates and 
run in ~8% of the time, because now we get a pretty nice loop involving 
only unboxed Ints. If we then replace readArray and writeArray with 
unsafeRead and unsafeWrite, we see that the most time is spent on bounds 
checking, because now the STUArray loop becomes really compact and runs a 
good three times faster, so it's now over forty times faster than the 
UArray (for which using unsafeAt instead of (!) doesn't make a noticeable 
difference).

>
> Two things:
> - profiling interacts badly with some optimisations, so what the
> profiling output says may deviate significantly from what your -O2
> compiled production code actually does
> - some datatypes suffer more from profiling than others, so what the
> profiling output says for different choices of datatype may deviate
> significantly from how your -O2 compiled production code behaves
>
> Normally, -prof doesn't change the behaviour very much, but sometimes it
> does.
>
> > One additional point of confusion for me: when I run either function
> > with +RTS -hc and use hp2ps I get an empty graph. I've seen these
> > tools work quite well before, so I suspect I'm doing something wrong
> > now.
>
> Too little actual heap usage and too short running time. Change the
> upper limit to 10 million and you should get a graph with actual bands.
>
> > Thanks,
> > Chris
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners



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

Message: 10
Date: Wed, 24 Feb 2010 19:47:22 -0800
From: Chris Pettitt <[email protected]>
Subject: Re: [Haskell-beginners] State Transformer Confusion
To: Daniel Fischer <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=UTF-8

Hi Daniel,

Thank you for your analysis. I dropped -prof from the compile and
explicitly specified the index type as Int - both yielded huge
improvements. These are great tips for me to keep in mind as I
continue to get deeper into Haskell.

Thanks,
Chris

On Wed, Feb 24, 2010 at 7:26 PM, Daniel Fischer
<[email protected]> wrote:
> Am Donnerstag 25 Februar 2010 03:54:17 schrieb Daniel Fischer:
>> Am Donnerstag 25 Februar 2010 03:04:02 schrieb Chris Pettitt:
>> > Hello Haskell-Beginners,
>> >
>> > I'm having a fair amount of difficulty understanding the ST monad. In
>> > my basic experimentation I'm finding that I'm getting more heap
>> > allocated with STUArray than with a regular Array, which is not what I
>> > would expect.
>>
>> And ordinarily you don't. The problem is that the STUArray suffers very
>> badly from profiling. Compiling without -prof, the STUArray code
>> allocates about half as much as the UArray code and takes ~42% of the
>> time. With profiling, STUArray allocates ~40% more and takes ~50% longer
>> than UArray.
>
> Oh, and: In the UArray code, you specified the index type as Int, in the
> STUArray code, you didn't specify it, so it was taken to be Integer, which
> slows down performance significantly, changing sumInPlace1 to
>
> sumInPlace1 :: [Int] -> Int
> sumInPlace1 xs = (! 0) . runSTUArray $ do
>        a <- newArray (0 :: Int, 0) 0
>        forM_ xs $ \x -> do
>            x' <- (readArray a 0)
>            writeArray a 0 (x' + x)
>        return a
>
> makes the STUArray code allocate ~6% of what the UArray code allocates and
> run in ~8% of the time, because now we get a pretty nice loop involving
> only unboxed Ints. If we then replace readArray and writeArray with
> unsafeRead and unsafeWrite, we see that the most time is spent on bounds
> checking, because now the STUArray loop becomes really compact and runs a
> good three times faster, so it's now over forty times faster than the
> UArray (for which using unsafeAt instead of (!) doesn't make a noticeable
> difference).
>
>>
>> Two things:
>> - profiling interacts badly with some optimisations, so what the
>> profiling output says may deviate significantly from what your -O2
>> compiled production code actually does
>> - some datatypes suffer more from profiling than others, so what the
>> profiling output says for different choices of datatype may deviate
>> significantly from how your -O2 compiled production code behaves
>>
>> Normally, -prof doesn't change the behaviour very much, but sometimes it
>> does.
>>
>> > One additional point of confusion for me: when I run either function
>> > with +RTS -hc and use hp2ps I get an empty graph. I've seen these
>> > tools work quite well before, so I suspect I'm doing something wrong
>> > now.
>>
>> Too little actual heap usage and too short running time. Change the
>> upper limit to 10 million and you should get a graph with actual bands.
>>
>> > Thanks,
>> > Chris
>>
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>


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

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


End of Beginners Digest, Vol 20, Issue 36
*****************************************

Reply via email to