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?
-
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:
-
Some ideas:
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.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 offoldr
and other internal calls; by default, it doesn't do that.
-
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))
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.