[Haskell-cafe] = definition for list monad in ghc

2011-05-16 Thread Michael Vanier
Usually in monad tutorials, the = operator for the list monad is defined as: m = k = concat (map k m) -- or concatMap k m but in the GHC sources it's defined as: m = k = foldr ((++) . k) [] m As far as I can tell, this definition is equivalent to the previous one (correct me if

Re: [Haskell-cafe] = definition for list monad in ghc

2011-05-16 Thread Ivan Lazar Miljenovic
On 16 May 2011 19:07, Michael Vanier mvanie...@gmail.com wrote: Usually in monad tutorials, the = operator for the list monad is defined as: m = k = concat (map k m)  -- or concatMap k m but in the GHC sources it's defined as: m  =  k  =  foldr  ((++)  .  k)  []  m As far as I can tell,

Re: [Haskell-cafe] = definition for list monad in ghc

2011-05-16 Thread Daniel Fischer
On Monday 16 May 2011 11:07:15, Michael Vanier wrote: Usually in monad tutorials, the = operator for the list monad is defined as: m = k = concat (map k m) -- or concatMap k m but in the GHC sources it's defined as: m = k = foldr ((++) . k) [] m As far as I can tell, this

Re: [Haskell-cafe] = definition for list monad in ghc

2011-05-16 Thread Andrew Coppin
On 16/05/2011 10:07 AM, Michael Vanier wrote: Usually in monad tutorials, the = operator for the list monad is defined as: m = k = concat (map k m) -- or concatMap k m but in the GHC sources it's defined as: m = k = foldr ((++) . k) [] m As far as I can tell, this definition is equivalent to

Re: [Haskell-cafe] = definition for list monad in ghc

2011-05-16 Thread austin seipp
Looking at the Core for an utterly trivial example (test x = concatMap k x where k i = [i..i*2]), the foldr definition seems to cause a little extra optimization rules to fire, but the result seems pretty big. The definition using concatMap results in core like this: main_go2 = \ (ds_aqV ::

Re: [Haskell-cafe] = definition for list monad in ghc

2011-05-16 Thread Felipe Almeida Lessa
On Mon, May 16, 2011 at 3:49 PM, austin seipp a...@hacks.yi.org wrote: I don't know why GHC doesn't have this rule by default, though. We can at least rig it with a RULES pragma, however: $ cat concatmap.hs module Main where {-# RULES concatMap/foldr forall x k. concatMap k x = foldr ((++)

Re: [Haskell-cafe] = definition for list monad in ghc

2011-05-16 Thread Daniel Fischer
On Monday 16 May 2011 20:49:35, austin seipp wrote: Looking at the Core for an utterly trivial example (test x = concatMap k x where k i = [i..i*2]), the foldr definition seems to cause a little extra optimization rules to fire, but the result seems pretty big. The definition using concatMap

Re: [Haskell-cafe] = definition for list monad in ghc

2011-05-16 Thread austin seipp
You're both right indeed - I didn't look for the definition of concatMap in GHC.List. I thought it could be some behavior with the new inliner, so I defined concatMap in terms of foldr, put it in a seperate module, and then included it and used it in my test: Concatmap2.hs: module Concatmap2

Re: [Haskell-cafe] = definition for list monad in ghc

2011-05-16 Thread Daniel Fischer
On Monday 16 May 2011 20:49:35, austin seipp wrote: As you can see, with the foldr definition, GHC is able to fuse the iteration of the input list with the generation of the result - note the 'GHC.Base.++' call with the first argument being a list from [x..x*2], and the second list to append

Re: [Haskell-cafe] = definition for list monad in ghc

2011-05-16 Thread Daniel Fischer
On Monday 16 May 2011 22:26:18, I wrote: On Monday 16 May 2011 20:49:35, austin seipp wrote: As you can see, with the foldr definition, GHC is able to fuse the iteration of the input list with the generation of the result - note the 'GHC.Base.++' call with the first argument being a list