Developers, Developers, Developers! Maksim Sorokin IT Blog

26Mar/10Off

Printing Data Structures With Standard ML

Standard ML is a nice functional programming language. But unfortunately it is painful to debug applications. At least for me after using Haskell for some time. The problem is that there is no way to easy print out the data structure in order to debug it in good old "sysout" way.

For certain reason, I had to modify Prolog interpreter written in Standard ML. I used Moscow ML implementation. Interpreter had structures which look like:

...
datatype Term = Var of string * pos

              | Fun of string * Term list * pos

type Atom = string * Term list

datatype Query = And of Query * Query * pos
               | Or of Query * Query * pos
...

So in order to print those structure, one can do that in the top level mode of Moscow ML. But this may be somewhat inconvenient. So I ended up writing my own PrettyPrinter module where I created separate data type for types I wanted to print:

datatype PrintType =
         Clause of Prolog.Clause
       | ClauseList of Prolog.Clause list
       | Atom of Prolog.Atom
       | Query of Prolog.Query
       | Term of Prolog.Term
       ...

And had following print and show functions:

fun show (Clause (atom, query)) =
		(show (Atom atom)) ^ " " ^ (show (Query query))
  | show (ClauseList cl) =
		"[" ^ (showList (ClauseList cl))  ^ "]"
  | show (Atom (atomName, terms)) =
  ...
and showList(Clause _) = ""             (* shouldn't happen *)
  | showList(ClauseList []) = ""
  | showList(ClauseList (c::cs)) =
		(show (Clause c)) ^ (getListSeparator cs) ^ (showList (ClauseList cs))
  ...

fun print pt = BasicIO.print (show pt ^ "\n")

Annoying.

Comments (0) Trackbacks (1)

Leave a comment