views:

448

answers:

3

I have written the following function.. and executed using WinHugs

teneven = [x | x <- [1..10], even x]

My output :

Main> teneven
[2,4,6,8,10] :: [Integer]
(63 reductions, 102 cells)

is there anyway to print all the reductions.. so I can learn the core evaluation happening inside WinHugs?

+1  A: 

Believe me, you dont want to go this way.

Set (and order) of reductions used in each particular case would depend on particular language implementation (hugs could do it one way, ghci - in other way, jhc - in yet another, etc).

Better read something about general ways to implement compiler/interpreter/virual machine for functional language - like SECD machine, etc.

Several links:

ADEpt
+2  A: 

Some ideas:

  1. The debug command-line option (which you can set with :set +d in Hugs) is informative, but is very verbose and does not show you the reductions in Haskell syntax.

  2. Try Hat - the Haskell Tracer. I just tried it on a simple program and it's pretty cool. I'm not on Windows, though, and I don't know how difficult it would be to get it running. It's likely fairly difficult, which is a shame since it's cool and essentially what you want. If you do get it running, you can get something like this information from Hat:

    main = {IO}
    teneven = [2,4,6,8,10]
    _foldr (\..) [1,2,3,4,5,6,7,8, ...] [] = [2,4,6,8,10]
    (\..) 1 [2,4,6,8,10] = [2,4,6,8,10]
    (\..) 2 [4,6,8,10] = [2,4,6,8,10]
    (\..) 3 [4,6,8,10] = [4,6,8,10]
    (\..) 4 [6,8,10] = [4,6,8,10]
    (\..) 5 [6,8,10] = [6,8,10]
    (\..) 6 [8,10] = [6,8,10]
    (\..) 7 [8,10] = [8,10]
    (\..) 8 [10] = [8,10]
    (\..) 9 [10] = [10]
    (\..) 10 [] = [10]
    

    The lambda there is even. Also, if you want, Hat can trace into calls of foldr and other internal calls; by default, it doesn't do that.

A. Rex
+2  A: 

Hi there!

import Debug.Trace
fact :: Integer -> Integer
fact 0 = trace "fact 0 ->> 1" 1
fact n = trace ("fact " ++ show n) (n * fact (n-1))

or

import Hugs.Observe
fact :: Integer -> Integer
fact 0 = observe "fact 0" 1
fact n = observe "fact n" (n *  fact (n-1))
Jacano