<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Developers, Developers, Developers! &#187; Standard ML</title>
	<atom:link href="http://maksim.sorokin.dk/it/tag/standard-ml/feed/" rel="self" type="application/rss+xml" />
	<link>http://maksim.sorokin.dk/it</link>
	<description>Maksim Sorokin IT Blog</description>
	<lastBuildDate>Sun, 05 Feb 2012 19:37:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.3</generator>
		<item>
		<title>Printing Data Structures With Standard ML</title>
		<link>http://maksim.sorokin.dk/it/2010/03/26/printing-data-structures-with-standard-ml/</link>
		<comments>http://maksim.sorokin.dk/it/2010/03/26/printing-data-structures-with-standard-ml/#comments</comments>
		<pubDate>Fri, 26 Mar 2010 16:49:05 +0000</pubDate>
		<dc:creator>Maksim Sorokin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[functional programming]]></category>
		<category><![CDATA[Standard ML]]></category>

		<guid isPermaLink="false">http://maksim.sorokin.dk/it/?p=111</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://en.wikipedia.org/wiki/Standard_ML">Standard ML</a> 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.<br />
<span id="more-111"></span></p>
<p>For certain reason, I had to modify <a href="http://en.wikipedia.org/wiki/Prolog">Prolog</a> interpreter written in Standard ML. I used <a href="http://www.itu.dk/~sestoft/mosml.html">Moscow ML</a> implementation. Interpreter had structures which look like:</p>
<pre class="brush: plain; title: ;">...
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
...</pre>
<p>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 <code>PrettyPrinter</code> module where I created separate data type for types I wanted to print:</p>
<pre class="brush: plain; title: ;">datatype PrintType =
         Clause of Prolog.Clause
       | ClauseList of Prolog.Clause list
       | Atom of Prolog.Atom
       | Query of Prolog.Query
       | Term of Prolog.Term
       ...</pre>
<p>And had following <code>print</code> and <code>show</code> functions:</p>
<pre class="brush: plain; title: ;">fun show (Clause (atom, query)) =
		(show (Atom atom)) ^ &quot; &quot; ^ (show (Query query))
  | show (ClauseList cl) =
		&quot;[&quot; ^ (showList (ClauseList cl))  ^ &quot;]&quot;
  | show (Atom (atomName, terms)) =
  ...
and showList(Clause _) = &quot;&quot;             (* shouldn't happen *)
  | showList(ClauseList []) = &quot;&quot;
  | showList(ClauseList (c::cs)) =
		(show (Clause c)) ^ (getListSeparator cs) ^ (showList (ClauseList cs))
  ...

fun print pt = BasicIO.print (show pt ^ &quot;\n&quot;)</pre>
<p>Annoying.</p>
]]></content:encoded>
			<wfw:commentRss>http://maksim.sorokin.dk/it/2010/03/26/printing-data-structures-with-standard-ml/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

