[Haskell-cafe] Graphical representation of Haskell code

Ronald Guida oddron at gmail.com
Tue Mar 23 01:31:31 EDT 2010

On Mon, Mar 22, 2010 at 7:02 PM, Dupont Corentin
<corentin.dupont at gmail.com> wrote:
> Hello, I’m relatively new to Haskell.
> I’m wondering if it exist a tool to graphically represent Haskell code.
> Let’s try to do it on a simple example, as an exercise:
> f = Map (+1)

Your graphic for "f = map (+1)" seems much more complex than the
corresponding code.  I would agree with Ivan Miljenovic:
> I'm of the opinion that unless you just use it on small snippets,
> the generated images will be too large and unwieldy.

The first question I would ask is /why/ would you like to visualize
some Haskell code?  If you want to see the high-level structure of
a complex program, try SourceGraph. (I have never used it though.)

On the other hand, if you are trying to visualize Haskell as part of
your efforts to learn the language, then I believe it would be best to
draw diagrams by hand, rather than relying on an automated tool.
The kinds of things that you'll want to depict are probably going to
vary considerably, depending on what you're trying to understand.

Consider a few different implementations of the "map" function:

  -- version 1: recursion
  map1 :: (a -> b) -> [a] -> [b]
  map1 f [] = []
  map1 f (x:xs) = (f x) : map1 f xs

  -- version 2: fold
  map2 :: (a -> b) -> [a] -> [b]
  map2 f = foldr ((:) . f) []

  -- version 3: continuation passing style
  map3 :: (a -> b) -> [a] -> [b]
  map3 f xs = map' (\x y -> f x : y) xs
      map' k [] = []
      map' k (y:ys) = k y  (map' k ys)

  -- version 4: list comprehension
  map4 :: (a -> b) -> [a] -> [b]
  map4 f xs = [f x | x <- xs]

  -- version 5: list monad
  map5 :: (a -> b) -> [a] -> [b]
  map5 f xs = xs >>= (return . f)

These all do exactly the same thing, but each one uses different
techniques.  If I'm trying to learn (or teach) Haskell, I would
probably need a slightly different visual "language" for each one
in order to capture the most relevant concepts in a useful way.
How would you visualize them?

@Mihai Maruseac:
I think a visual debugger would be a wonderful idea.  You may want
to consider how a visual debugger would work with each of these
versions of map.

:-) You might also consider several versions of factorial :-)

-- Ron

More information about the Haskell-Cafe mailing list