RE: Why is (monad) elegance so costly?

2001-11-02 Thread Saswat Anand


Hi,
  I feel embarrased when I post newbie questions after one year
of decent Haskell programming. But it feels much better to ask than to
suffer in ignorance. 

My newbie question is: Could anyone explain why the second version of the
following function is better. 

Is there a tutorial kind of paper(or related) that gives programmer-view
of closures? In particular when they are created, what do they contain and
where and how they should be avoided to make program faster.

Thank you very much,
Saswat

 
 You can see what is going on if you give the flag -ddump-simpl
 to GHC, and then look for the function Main.eval.  You'll see
 that eval has a shape like
 
   eval (Var x) = let ... in \env - ...
   eval (Add u v) = let ... in \env - ...
 
 This is bad, because eval is building a function closure for
 the \env, instead of taking its two arguments together as does
 simplEval.  We'd prefer
 
   eval (Var x) env = let ... in ...
   eval (Add u v) env = let .. in ...






___
Glasgow-haskell-users mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-users



Why is (monad) elegance so costly?

2001-10-19 Thread marku

I am about to rewrite my Z animation tool (JAZA) in a style that
makes more intensive use of state monads.

However, my experiments with a simplified lambda-calculus example
shows that (with GHC 5.00) the state monad is dramatically less
efficient than the simple identity monad:

4 TIMES SLOWER, and
7 TIMES MORE MEMORY!

Is this normal?  Acceptable?  Am I doing something wrong?

Can anyone suggest ways of reducing these overheads?

(I am very keen to use state-monads if possible, because it allows
my 'eval' code to be generic over the monad that is used, which
allows me to reuse the code with other similar monads.  In fact,
I am using it to simulate the 'visitor' design pattern from OO langs.)

Hugs gives slightly smaller differences (3 times more reductions and 
3.5 times more cells), but I had hoped that GHC would be able to
optimize most of the state monad overhead away  (especially when
the monad uses newtype)?

My code and speed measurements are attached.

Mark.


 LambdaCalc.hs