Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it possible to print all reductions in Haskell - using WinHugs?

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?

like image 546
Mohan Narayanaswamy Avatar asked Jan 06 '09 15:01

Mohan Narayanaswamy


3 Answers

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.

like image 140
A. Rex Avatar answered Oct 14 '22 07:10

A. Rex


Here you have a few examples of the use of Debug.Trace and Hugs.Observe.

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


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

Hope this helps you to figure out how to print all reductions using WinHungs.

like image 29
Juan Antonio Cano Avatar answered Oct 14 '22 06:10

Juan Antonio Cano


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:

  • Wikipedia, SECD machine
  • Lambda the Ultimate, Implementing fast interpreters
like image 2
ADEpt Avatar answered Oct 14 '22 06:10

ADEpt