<?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>szeiger.de &#187; Scala</title>
	<atom:link href="http://szeiger.de/blog/category/scala/feed/" rel="self" type="application/rss+xml" />
	<link>http://szeiger.de</link>
	<description>Stefan Zeiger's Software Development Weblog</description>
	<lastBuildDate>Tue, 29 Jun 2010 21:33:20 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>A Zipper for scala.xml</title>
		<link>http://szeiger.de/blog/2009/12/27/a-zipper-for-scala-xml/</link>
		<comments>http://szeiger.de/blog/2009/12/27/a-zipper-for-scala-xml/#comments</comments>
		<pubDate>Sun, 27 Dec 2009 00:54:11 +0000</pubDate>
		<dc:creator>Stefan Zeiger</dc:creator>
				<category><![CDATA[Scala]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://szeiger.de/?p=54</guid>
		<description><![CDATA[In recent discussions on the scala-internals and scala-xml mailing lists, there were calls for a mutable, DOM-like XML model, where nodes hold references to their parent element, so that all standard XPath axes can be supported. In this post I&#8217;d like to present a different representation which is based on the immutable and persistent scala.xml [...]]]></description>
			<content:encoded><![CDATA[<p>In recent discussions on the <a href="http://www.scala-lang.org/node/199">scala-internals and scala-xml mailing lists</a>, there were calls for a mutable, DOM-like XML model, where nodes hold references to their parent element, so that all standard XPath axes can be supported. In this post I&#8217;d like to present a different representation which is based on the immutable and persistent scala.xml model, is completely immutable itself, yet allows navigation along all axes and &#8220;mutation&#8221;. It is based on the <a href="http://en.wikipedia.org/wiki/Zipper_%28data_structure%29">Zipper</a> technique which was first described by Gérard Huet in <a href="http://www.st.cs.uni-saarland.de/edu/seminare/2005/advanced-fp/docs/huet-zipper.pdf">this paper [PDF]</a>.</p>
<h3>The Problem</h3>
<p>Take this piece of XML (an Eclipse .project file):</p>
<div class="wp_syntax">
<div class="code">
<pre>&lt;projectDescription>
  &lt;name>scala-query&lt;/name>
  &lt;comment>&lt;/comment>
  &lt;projects>&lt;/projects>
  &lt;buildSpec>
    &lt;buildCommand>
      &lt;name>ch.epfl.lamp.sdt.core.scalabuilder&lt;/name>
      &lt;arguments>&lt;/arguments>
    &lt;/buildCommand>
  &lt;/buildSpec>
  &lt;natures>
    &lt;nature>ch.epfl.lamp.sdt.core.scalanature&lt;/nature>
    &lt;nature>org.eclipse.jdt.core.javanature&lt;/nature>
  &lt;/natures>
&lt;/projectDescription>
</pre>
</div>
</div>
<p>Its scala.xml model looks like this:</p>
<p><a href="http://szeiger.de/static/a-zipper-for-scala-xml/basic-doc.svg"><img src="http://szeiger.de/static/a-zipper-for-scala-xml/basic-doc.png" alt="XML model" /></a></p>
<p>(In reality, a pretty-printed document contains a bunch of additional text nodes with all the whitespace between the elements, but we can safely ignore them and work with a simplified model.)</p>
<p>Imagine you have a reference to the first &#8220;nature&#8221; element which we will call the <em>context</em>. The only other node reachable from this context is the text node below it. You cannot navigate to its parent or sibling without starting at the root element and searching for the right spot.</p>
<p>An obvious solution to this problem is to add a reference from each node to its parent but that means giving up <em>persistence</em> (the sharing of data between different versions of the tree).</p>
<p>Let&#8217;s look at the situation without the parent reference first and assume you wanted to add a third &#8220;nature&#8221; element before the two existing ones. Since you cannot modify the &#8220;natures&#8221; element in this immutable data structure, you have to make a copy of it which contains the two original children plus the new one. Now that you have a new &#8220;natures&#8221; element, you make a copy of the &#8220;projectDescription&#8221; root element above it which contains all the original children except for the &#8220;natures&#8221; element which is replaced with the new one. This gives you two separate trees (the original and the modified one) which share most of their nodes:</p>
<p><a href="http://szeiger.de/static/a-zipper-for-scala-xml/shared-tree.svg"><img src="http://szeiger.de/static/a-zipper-for-scala-xml/shared-tree.png" alt="XML models with shared sub-trees" /></a></p>
<p>(For the &#8220;natures&#8221; element we can even reuse the list of its children because we only prepend an element at the beginning. We need to copy the list of the root element&#8217;s children though because we replace its last element.)</p>
<p>Now try the same on a model with parent references. You can create the new &#8220;nature&#8221; element and a copy of the &#8220;natures&#8221; element above it. But what about the other two &#8220;nature&#8221; elements? You cannot reuse them because they contain a reference to the original &#8220;natures&#8221; element, so you have to create copies of them which point up to their new parent. You cannot even reuse the text nodes below them because they also reference their original parents. And now that you have copied the entire &#8220;natures&#8221; sub-tree, you face the same problem with the root element. All its children point up to the old root element, so you have to copy them recursively, too. In the end you have copied the tree entirely!</p>
<h3>The Zipper</h3>
<p>We can do better with the Zipper. The basic idea is to take an immutable data structure for the actual data (in this case XML documents in scala.xml models) and add a representation for a context that allows you to navigate through the data in all directions and modify parts of it (in the same way that you modified a document in the previous section &#8212; by creating a new document that shares most of the data).</p>
<p>We start with a type for the context:</p>
<div class="wp_syntax">
<div class="code">
<pre>sealed case class NodeLoc(node: Node, path: NodePath)
</pre>
</div>
</div>
<p>It consists of the node (or sub-tree) which it wraps plus a path to the root element. A path can either be the <em>top</em> (root) of the tree or a <em>hole</em>, i.e. the location of a node without the node itself:</p>
<div class="wp_syntax">
<div class="code">
<pre>sealed trait NodePath
final case object Top extends NodePath
final case class Hole(left: List[Node],
  parent: NodeLoc, right: List[Node]) extends NodePath
</pre>
</div>
</div>
<p>For example, here is a <code>NodeLoc</code> for the first &#8220;nature&#8221; element:</p>
<p><a href="http://szeiger.de/static/a-zipper-for-scala-xml/zipper-paths.svg"><img src="http://szeiger.de/static/a-zipper-for-scala-xml/zipper-paths.png" alt="XML model with Zipper" /></a></p>
<p>You can see that the original tree (in grey and yellow) is still intact. There are two <code>NodeLoc</code>s with holes which point up to a parent <code>NodeLoc</code> and contain lists of their preceding and following siblings. Note that the list of preceding siblings is in reverse order (compared to the corresponding list of children on the parent node) so that the first node in the list is the next element to the left of the hole.</p>
<p>We can now define the basic navigation methods on <code>NodeLoc</code>. In order to move to the left sibling, we create a new <code>NodeLoc</code> which contains the head of the &#8220;left&#8221; list as its node and a copy of the current hole with the remaining nodes on the &#8220;left&#8221; list and the current node appended to the &#8220;right&#8221; list:</p>
<div class="wp_syntax">
<div class="code">
<pre>  protected def create(node: Node, path: Hole) = NodeLoc(node, path)

  final def leftOpt = path match {
    case Hole(tl :: l, p, r) =>
      Some(create(tl, Hole(l, p, node :: r)))
    case _ => None
  }
</pre>
</div>
</div>
<p>Moving to the right is symmetrical to that:</p>
<div class="wp_syntax">
<div class="code">
<pre>  final def rightOpt = path match {
    case Hole(l, p, tr :: r) =>
      Some(create(tr, Hole(node :: l, p, r)))
    case _ => None
  }
</pre>
</div>
</div>
<p>Methods <code>left</code> and <code>right</code> which return a bare <code>NodeLoc</code> or throw an exception, and <code>isFirst</code> and <code>isLast</code> to ensure that you don&#8217;t run into such an exception, can be defined in the same way. I&#8217;m using <code>Option</code> here because it will be useful later on for creating the XPath axes.</p>
<p>Moving down to a child with the specified index position is accomplished by splitting the list of the current node&#8217;s children at the correct position, removing the child node and creating a <code>Hole</code> for the other children which points up to this <code>NodeLoc</code>:</p>
<div class="wp_syntax">
<div class="code">
<pre>  final def downOpt(idx: Int) = {
    val ch = node.child
    if(ch.isEmpty) None
    else Some(create(ch.head,
      Hole(ch.tail.take(idx).reverse.toList,
           this, ch.drop(idx+1).toList)))
  }
</pre>
</div>
</div>
<h3>Mutable Yet Immutable</h3>
<p>Replacing the node at the current location is extremely simple. We just create a <code>NodeLoc</code> with the new <code>Node</code> and the current path:</p>
<div class="wp_syntax">
<div class="code">
<pre>  final def set(n: Node) = NodeLoc(n, path)
</pre>
</div>
</div>
<p>There is no need to propagate the change up to the root element at this point. We could, for example, move through all siblings, replacing each one with a new node, all in constant time (per sibling). Parent elements are not copied until we move up and &#8220;unzip&#8221; the path into a new tree. This is done by constructing a new list of children from the current node and its left and right siblings, and creating a copy of the old parent with the new children:</p>
<div class="wp_syntax">
<div class="code">
<pre>  def upOpt = path match {
    case h : Hole =>
      val l = h.left.reverse ++ (node :: h.right)
      Some(NodeLoc(h.parent.node match {
        case e: Elem => e.copy(child = l)
        case _: Group => new Group(l)
      }, h.parent.path))
    case _ => None
  }
</pre>
</div>
</div>
<p>This is slightly messy with scala.xml because we have to match on the type of node and copy Elem and Group nodes (the only ones which can contain children) in different ways.</p>
<p>Always recreating the parent when moving up is a waste of time when no changes have been made to the current sub-tree. We can optimize for this case with a special <code>CachedParentNodeLoc</code> which returns the original parent when moving up:</p>
<div class="wp_syntax">
<div class="code">
<pre>final class CachedParentNodeLoc(node: Node, path: Hole)
extends NodeLoc(node, path) {
  override def upOpt = Some(path.parent)
  override protected def create(node: Node, path: Hole) =
    new CachedParentNodeLoc(node, path)
}

final class CachedTopNodeLoc(node: Node) extends NodeLoc(node, Top) {
  override def upOpt = None
  override protected def create(node: Node, path: Hole) =
    new CachedParentNodeLoc(node, path)
}

object NodeLoc {
  def apply(node: Node): NodeLoc = new CachedTopNodeLoc(node)
}
</pre>
</div>
</div>
<p>Note that the <code>leftOpt</code>, <code>rightOpt</code> and <code>downOpt</code> methods use <code>create(...)</code> instead of <code>NodeLoc(...)</code>, so that navigating left, right or down from a cached location returns another cached location.</p>
<h3>The XPath Axes</h3>
<p>The <a href="http://www.w3.org/TR/xpath">XPath specification</a> defines the following axes:</p>
<blockquote>
<ul>
<li>the <code>child</code> axis contains the children of the context node</li>
<li>the <code>descendant</code> axis contains the descendants of the context node; a descendant is a child or a child of a child and so on; thus the descendant axis never contains attribute or namespace<br />
nodes</li>
<li>the <code>parent</code> axis contains the parent of the context node, if there is one</li>
<li>the <code>ancestor</code> axis contains the ancestors of the context node; the ancestors of the context node consist of the parent of context node and the parent&#8217;s parent and so on; thus, the ancestor axis will always include the root node, unless the context node is the root node</li>
<li>the <code>following-sibling</code> axis contains all the following siblings of the context node; if the context node is an attribute node or namespace node, the <code>following-sibling</code> axis is empty</li>
<li>the <code>preceding-sibling</code> axis contains all the preceding siblings of the context node; if the context node is an attribute node or namespace node, the <code>preceding-sibling</code><br />
axis is empty</li>
<li>the <code>following</code> axis contains all nodes in the same document as the context node that are after the context node in document order, excluding any descendants and excluding attribute nodes and namespace nodes</li>
<li>the <code>preceding</code> axis contains all nodes in the same document as the context node that are before the context node in document order, excluding any ancestors and excluding attribute nodes and namespace nodes</li>
<li>the <code>attribute</code> axis contains the attributes of the context node; the axis will be empty unless the context node is an element</li>
<li>the <code>namespace</code> axis contains the namespace nodes of the context node; the axis will be empty unless the context node is an element</li>
<li>the <code>self</code> axis contains just the context node itself</li>
<li>the <code>descendant-or-self</code> axis contains the context node and the descendants of the context node</li>
<li>the <code>ancestor-or-self</code> axis contains the context node and the ancestors of the context node; thus, the ancestor axis will always include the root node</li>
</ul>
</blockquote>
<p>The <em>attribute</em> and <em>namespace</em> axes do not fit well into our model because scala.xml does not represent attributes and namespace declarations as <code>Node</code> values. All other axes can be defined as methods on <code>NodeLoc</code>.</p>
<p>We will represent an axis as an <code>Iterator[NodeLoc]</code>. The following helper method creates an iterator by starting with a given value and continually applying a function to it until it returns <code>None</code>. It is similar to <code>Iterator.iterate</code> in Scala 2.8 except the latter can only create infinite iterators.</p>
<div class="wp_syntax">
<div class="code">
<pre>  private final def iterate[T](start: Option[T])(f: T => Option[T]): Iterator[T] = new Iterator[T] {
    private[this] var acc = start
    def hasNext = acc.isDefined
    def next() = acc match {
      case None => throw new NoSuchElementException("next on empty iterator");
      case Some(x) => val res = x ; acc = f(x) ; res
    }
  }
</pre>
</div>
</div>
<p>We can use it like this to create the <em>child</em> axis: Start with the first child, then go right node by node until you reach the last child:</p>
<div class="wp_syntax">
<div class="code">
<pre>  final def childAxis = iterate(downOpt(0))(_.rightOpt)
</pre>
</div>
</div>
<p>The <em>descendant-or-self</em> axis consists of the current node plus the <em>descendant-or-self</em> axes of all children:</p>
<div class="wp_syntax">
<div class="code">
<pre>  final def descendantOrSelfAxis: Iterator[NodeLoc] =
    Iterator.single(this) ++ childAxis.flatMap(_.descendantOrSelfAxis)
</pre>
</div>
</div>
<p>And the <em>descendant</em> axis consists only of the <em>descendant-or-self</em> axes of all children, without the current node:</p>
<div class="wp_syntax">
<div class="code">
<pre>  final def descendantAxis = childAxis.flatMap(_.descendantOrSelfAxis)
</pre>
</div>
</div>
<p>The <em>parent</em>, <em>ancestor-or-self</em> and <em>ancestor</em> axes work similarly:</p>
<div class="wp_syntax">
<div class="code">
<pre>  final def parentAxis = upOpt.iterator

  final def ancestorOrSelfAxis: Iterator[NodeLoc] =
    Iterator.single(this) ++ upOpt.iterator.flatMap(_.ancestorOrSelfAxis)

  final def ancestorAxis = upOpt.iterator.flatMap(_.ancestorOrSelfAxis)
</pre>
</div>
</div>
<p>The <em>following-sibling</em> and <em>preceding-sibling</em> axes are straight-forward:</p>
<div class="wp_syntax">
<div class="code">
<pre>  final def followingSiblingAxis = iterate(rightOpt)(_.rightOpt)

  final def precedingSiblingAxis = iterate(leftOpt)(_.leftOpt)
</pre>
</div>
</div>
<p>For the <em>following</em> and <em>preceding</em> axes we need to define some helper methods for moving to the following or preceding node in document order:</p>
<div class="wp_syntax">
<div class="code">
<pre>  final def followingAxis = iterate(followingOpt)(_.followingOpt)

  final def followingOpt: Option[NodeLoc] =
    downOpt(0) orElse rightOutOpt

  private final def rightOutOpt: Option[NodeLoc] =
    rightOpt orElse upOpt.flatMap(_.rightOutOpt)

  final def precedingAxis = iterate(precedingOpt)(_.precedingOpt)

  final def precedingOpt: Option[NodeLoc] =
    leftOpt.map(n => n.downLastTransitiveOpt.getOrElse(n)) orElse upOpt

  private final def downLastTransitiveOpt: Option[NodeLoc] =
    downLastOpt.flatMap(_.downLastTransitiveOpt)

  final def downLastOpt = {
    val ch = node.child
    if(ch.isEmpty) None
    else Some(NodeLoc(ch.head, Hole(ch.reverse.toList.tail, this, Nil)))
  }
</pre>
</div>
</div>
<p>And finally the trivial <em>self</em> axis:</p>
<div class="wp_syntax">
<div class="code">
<pre>  final def selfAxis = Iterator.single(this)
</pre>
</div>
</div>
<p>Methods \ and \\ for finding all child or descendant elements with a given name can be defined by filtering the <em>child</em> and <em>descendant</em> axes:</p>
<div class="wp_syntax">
<div class="code">
<pre>  final def \ (Name: String) =
    for(n @ NodeLoc(Elem(_, Name, _, _, _*), _) &lt;- childAxis) yield n

  final def \\ (Name: String) =
    for(n @ NodeLoc(Elem(_, Name, _, _, _*), _) &lt;- descendantAxis) yield n
</pre>
</div>
</div>
<p>You can find the complete source code, including more convenience methods and a short demo <a href="http://gist.github.com/264070">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://szeiger.de/blog/2009/12/27/a-zipper-for-scala-xml/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>New ScalaQuery Features</title>
		<link>http://szeiger.de/blog/2009/12/21/new-scala-query-features/</link>
		<comments>http://szeiger.de/blog/2009/12/21/new-scala-query-features/#comments</comments>
		<pubDate>Mon, 21 Dec 2009 20:15:03 +0000</pubDate>
		<dc:creator>Stefan Zeiger</dc:creator>
				<category><![CDATA[Scala]]></category>
		<category><![CDATA[ScalaQuery]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[DSL]]></category>
		<category><![CDATA[JDBC]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://szeiger.de/?p=41</guid>
		<description><![CDATA[It&#8217;s been almost 3 months since my last summary of new features in ScalaQuery. I implemented some important changes in the following weeks but I only had little time to work on ScalaQuery after my extended one-month vacation ended in October.
Build system
Builds against newer versions of Scala 2.8 have been going well thanks to sbt [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s been almost 3 months since my last <a href="http://szeiger.de/blog/2009/09/27/a-scala-query-update/">summary of new features in ScalaQuery</a>. I implemented some important changes in the following weeks but I only had little time to work on ScalaQuery after my extended one-month vacation ended in October.</p>
<h3>Build system</h3>
<p>Builds against newer versions of Scala 2.8 have been going well thanks to <a href="http://code.google.com/p/simple-build-tool/wiki/0_6_Summary">sbt 0.6</a> which separates the Scala version used by the build system from the version used by the project being built. You need to use at least version 0.6.7 of the new xsbt launcher to build ScalaQuery. Other dependencies are downloaded automatically by sbt. I wrote an implementation of sbt&#8217;s test interface for <a href="http://www.junit.org/">JUnit</a> which you can find <a href="http://github.com/szeiger/junit-interface">here</a> (binaries are <a href="http://scala-tools.org/repo-releases/com/novocode/junit-interface/">here</a> on scala-tools.org). It allows you to run ScalaQuery&#8217;s JUnit test cases from sbt with the <code>test</code> command.</p>
<p>All published artifacts now contain the Scala version in the artifact ID. You can find the latest ScalaQuery snapshot in scala-tools.org&#8217;s <a href="http://scala-tools.org/repo-snapshots/">snapshots repository</a> as:</p>
<dl>
<dt>groupId:</dt>
<dd>com.novocode</dd>
<dt>artifactId:</dt>
<dd>scala-query_2.8.0.Beta1-RC4</dd>
<dt>version:</dt>
<dd>1.0.0-SNAPSHOT</dd>
<dl>
<h3>JDBC escape syntax</h3>
<p>ScalaQuery now uses <a href="http://java.sun.com/javase/6/docs/technotes/guides/jdbc/getstart/statement.html#1006519">JDBC escape syntax</a> and scalar functions where possible for better portability between DB engines:</p>
<ul>
<li>String column concatenation (with the <code>++</code> operator) and the <code>startsWith</code> and <code>endsWith</code> methods on String columns have been moved up to <code>BasicProfile</code>.</li>
<li>Escape characters for LIKE expressions are added to the queries.</li>
<li>You can add other functions to be called with the <code>{fn ...}</code> syntax through <code>SimpleScalarFunction()</code> and <code>SimpleScalarFunction.nullary()</code>.</li>
<li>Various scalar functions are supported: <code>CONVERT</code>, <code>USER</code>, <code>DATABASE</code>, <code>CURDATE</code>, <code>CURTIME</code>, <code>PI</code>, <code>MOD</code>, <code>ABS</code>, <code>CEILING</code>, <code>FLOOR</code>, <code>SIGN</code>, <code>DEGREES</code>, <code>RADIANS</code>, <code>IFNULL</code>, <code>UCASE</code>, <code>LCASE</code>, <code>LTRIM</code>, <code>RTRIM</code>.</li>
<li>Literals for <code>Date</code>, <code>Time</code> and <code>Timestamp</code> values use the JDBC escape syntax.</li>
<li>Explicit inner, left outer, right outer and full outer joins are supported. For example:</li>
</ul>
<div class="wp_syntax">
<div class="code">
<pre>  val q = for {
    Join(c,p) &lt;- Categories innerJoin Posts on (_.id is _.category)
    _ &lt;- Query orderBy p.id
  } yield p.id ~ c.id ~ c.name ~ p.title
</pre>
</div>
</div>
<h3>Complex inserts</h3>
<p>Queries and columns can now be inserted into tables (using SQL&#8217;s <code>INSERT...SELECT</code> syntax). This allows you to use scalar functions when inserting individual records and to copy data produced by a query. For example:</p>
<div class="wp_syntax">
<div class="code">
<pre>  val q2 = for(s &lt;- Src1 if s.id &lt;= 2) yield s
  Dst2.insert(q2)
</pre>
</div>
</div>
<h3>Various changes</h3>
<ul>
<li>String columns are created as VARCHAR(254) by default (except in H2 where no size is needed).</li>
<li>The operators <code>===</code> and <code>!=</code> can be used instead of is and isNot. They have the proper precedence when used with other operators like <code>&#038;&</code> and <code>||</code>.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://szeiger.de/blog/2009/12/21/new-scala-query-features/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A ScalaQuery Update</title>
		<link>http://szeiger.de/blog/2009/09/27/a-scala-query-update/</link>
		<comments>http://szeiger.de/blog/2009/09/27/a-scala-query-update/#comments</comments>
		<pubDate>Sun, 27 Sep 2009 00:03:30 +0000</pubDate>
		<dc:creator>Stefan Zeiger</dc:creator>
				<category><![CDATA[Scala]]></category>
		<category><![CDATA[ScalaQuery]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[DSL]]></category>
		<category><![CDATA[JDBC]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://szeiger.de/?p=35</guid>
		<description><![CDATA[Since my last post about ScalaQuery one month ago I have wasted quite some time trying to implement the OptionMapper type (which is used to make operations on columns interoperable between base types and Option types) in a way which does not require 2^n implicit functions and 2*n+2 type parameters for operations with n operands. [...]]]></description>
			<content:encoded><![CDATA[<p>Since my <a href="http://szeiger.de/blog/2009/08/27/profiles-drivers-and-more/">last post</a> about <a href="http://github.com/szeiger/scala-query">ScalaQuery</a> one month ago I have wasted quite some time trying to implement the <code>OptionMapper</code> type (which is used to make operations on columns interoperable between base types and <code>Option</code> types) in a way which does not require 2^n implicit functions and 2*n+2 type parameters for operations with n operands. The composable <code>OptionMapper</code> itself is not hard to write (see <code>NewOptionMapper</code> <a href="http://github.com/szeiger/scala-query/blob/new-option-mapper/src/main/scala/com/novocode/squery/combinator/TypeMapper.scala">here</a>) but it requires more type fidelity than ScalaQuery offers at the moment: Either <code>Projection</code>s need to preserve the subtypes of <code>Column</code>s they are storing, or the required types have to be reconstructed from implicit values where they are needed. You can find my failed attempts in the <a href="http://github.com/szeiger/scala-query/tree/projection-types">projection-types</a> and <a href="http://github.com/szeiger/scala-query/tree/new-option-mapper">new-option-mapper</a> branches. The first one might actually work once <a href="https://lampsvn.epfl.ch/trac/scala/ticket/2346">Scala bug #2346</a> is resolved. The second one would require a much more powerful type inferencer. For now, I just added an <code>OptionMapper3</code> for 3 parameters (which is needed by the <code>BETWEEN</code> operator).</p>
<p>There are also some interesting new features in ScalaQuery:</p>
<h3>Mapped Projections</h3>
<p>You can now create a bidirectional mapping of a <code>Projection</code> with the &lt;> operator. This is especially useful for a table&#8217;s &#8220;*&#8221; projection. The &lt;> operator takes a function from the projection tuple type <em>T</em> (either as a tuple or as multiple parameters) to the mapped type <em>R</em>, and a function from <em>R</em> back to <em>Some[T]</em>. The asymmetry with the unnecessary <em>Some</em> wrapper is deliberate, matching exactly the <code>apply</code> und <code>unapply</code> methods of a <em>case class</em>. For example, here is a simple <em>User</em> class and table:</p>
<div class="wp_syntax">
<div class="code">
<pre>  case class User(first: String, last: String)

  object Users extends Table[<b>User</b>]("users") {
    def first = column[String]("first", O NotNull)
    def last = column[String]("last", O NotNull)
    def * = first ~ last <b>&lt;> (User, User.unapply _)</b>
  }
</pre>
</div>
</div>
<p>Whenever you perform an insert, update or query directly on such a table (instead of a projection of individual columns), you can use the mapped type:</p>
<div class="wp_syntax">
<div class="code">
<pre>  Users.insert(User("Stefan", "Zeiger"))
  val someUsers: List[User] = Users.where(_.first startsWith "S").list
</pre>
</div>
</div>
<h3>Finders</h3>
<p>The new convenience method <code>Table.createFinderBy</code> allows you to create a simple <em>finder</em> query template which selects values from a table by matching on a single column, e.g.:</p>
<div class="wp_syntax">
<div class="code">
<pre>  val findUserByFirstName = Users.createFinderBy(_.first)
</pre>
</div>
</div>
<p>This is equivalent to:</p>
<div class="wp_syntax">
<div class="code">
<pre>  val findUserByFirstName = for {
    first <- Parameters[String]
    u <- Users if u.first is first
  } yield u
</pre>
</div>
</div>
<h3>Updatable ResultSets</h3>
<p>JDBC allows you to modify a <code>ResultSet</code> which was created with the <code>CONCUR_UPDATABLE</code> result set concurrency. ScalaQuery now offers a high-level interface to this functionality with the <code>mutate</code> method, e.g.:</p>
<div class="wp_syntax">
<div class="code">
<pre>  val q1 = for(u <- Users if u.last.is("Simpson") || u.last.is("Bouvier")) yield u
  q1.mutate { m =>
    if(m()._3 == "Bouvier") m() = m().copy(_3 = "Simpson")
    else if(m()._2 == "Homer") m.delete()
    else if(m()._2 == "Bart") m.insert((None, "Lisa", "Simpson"))
  }
</pre>
</div>
</div>
<h3>Statement Options</h3>
<p>The new <code>ResultSetType</code>, <code>ResultSetConcurrency</code> and <code>ResultSetHoldability</code> classes allow you to request the corresponding JDBC options when ScalaQuery creates a <code>PreparedStatement</code>, e.g.:</p>
<div class="wp_syntax">
<div class="code">
<pre>  myDB withSession {
    ResultSetType.ScrollInsensitive {
      <em>// All database calls in this block use TYPE_SCROLL_INSENSITIVE</em>
    }
  }
</pre>
</div>
</div>
<p>Or if you don't want to use the implicit <code>threadLocalSession</code>:</p>
<div class="wp_syntax">
<div class="code">
<pre>  myDB withSession { s1 =>
    ResultSetType.ScrollInsensitive(s1) { s2 =>
      <em>// All database calls on s2 use TYPE_SCROLL_INSENSITIVE</em>
    }
  }
</pre>
</div>
</div>
<p>The default for all three options is <code>Auto</code> which gives you values suitable for the invoker method you are calling (e.g. <code>ResultSetConcurrency.Updatable</code> for <code>mutate</code> and <code>ResultSetConcurrency.ReadOnly</code> for all other methods).</p>
<h3>Build System</h3>
<p>I have rewritten most of the test classes as proper unit tests (using <a href="http://junit.org/">JUnit</a> 4) with assertions. I'd like to investigate <a href="http://www.artima.com/scalatest/">ScalaTest</a> and <a href="http://code.google.com/p/specs/">specs</a> further (possibly together with <a href="http://code.google.com/p/scalacheck/">ScalaCheck</a>) in the future to replace JUnit but the binary incompatibilities between different <a href="http://www.scala-lang.org/node/212/">Scala 2.8 snapshot builds</a> would only complicate the build process right now. If you build with <a href="http://code.google.com/p/simple-build-tool/">sbt</a>, you still need to download a suitable Scala 2.8 snapshot yourself and set the paths in the properties because the build is done in forking mode. This also means that you cannot use the "test" command to run the unit tests from sbt at the moment.</p>
<p><a href="http://www.h2database.com/">H2</a> and JUnit are declared as test dependencies in the sbt project definition and the Eclipse build path refers to the local copies in <em>lib_managed</em>. If you want to build ScalaQuery with Eclipse (as I usually do), just run "<code>sbt update</code>" once to pull in the required JARs.</p>
<p>I am using <a href="http://freemarker.org/">FreeMarker</a> templates and the <a href="http://fmpp.sourceforge.net/">FMPP</a> tool to create repetitive code for the <code>Projection</code> and <code>Parameters</code> classes. This is not yet integrated into the sbt build process. You can use the supplied Eclipse launcher instead (after downloading FMPP and pointing the launcher to the correct path). The generated sources are checked in, so you only need to do this if you want to change the templates and rebuild the Scala sources.</p>
]]></content:encoded>
			<wfw:commentRss>http://szeiger.de/blog/2009/09/27/a-scala-query-update/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Profiles, Drivers &amp; More</title>
		<link>http://szeiger.de/blog/2009/08/27/profiles-drivers-and-more/</link>
		<comments>http://szeiger.de/blog/2009/08/27/profiles-drivers-and-more/#comments</comments>
		<pubDate>Thu, 27 Aug 2009 15:07:47 +0000</pubDate>
		<dc:creator>Stefan Zeiger</dc:creator>
				<category><![CDATA[Scala]]></category>
		<category><![CDATA[ScalaQuery]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[DSL]]></category>
		<category><![CDATA[JDBC]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://szeiger.de/?p=34</guid>
		<description><![CDATA[It&#8217;s been three weeks already since my last post about ScalaQuery so I&#8217;d like to provide an update on some recent changes.
Profiles &#038; Drivers
The biggest news is that ScalaQuery now supports database engine-specific features. Feature sets can be bundled in profiles which are then implemented by drivers (Of course, a driver can also add driver-specific [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s been three weeks already since my <a href="http://szeiger.de/blog/2009/08/06/efficient-parameterized-queries-in-scala-query/">last</a> post about <a href="http://github.com/szeiger/scala-query/tree/master">ScalaQuery</a> so I&#8217;d like to provide an update on some recent changes.</p>
<h3>Profiles &#038; Drivers</h3>
<p>The biggest news is that ScalaQuery now supports database engine-specific features. Feature sets can be bundled in <em>profiles</em> which are then implemented by <em>drivers</em> (Of course, a driver can also add driver-specific features which are not part of a profile). All previously existing features are now part of the <code>BasicProfile</code> which should work on all SQL databases with little or no customization. The <code>BasicProfile</code> provides:</p>
<ul>
<li>An object called <code>Implicit</code> with all implicit conversions which are required to use the profile&#8217;s features (including an implicit value of type <code>BasicProfile</code> which points to the driver implementing the profile).</li>
<li>Some methods for creating various statements and query templates. They have default implementations provided by the profile but can be overridden by other profiles or drivers. You do not usually call them directly.</li>
<li>A number of <code>TypeMapperDelegate</code> objects for the basic types supported by the profile. You still use <code>TypeMapper</code>s defined outside the profile but they do not implement the actual type mapping any more. Instead they ask the profile for a delegate implementation. (Alternatively, I could have moved the existing <code>TypeMapper</code> objects entirely into the profile but mappers for some basic types like <code>Int</code> and <code>Boolean</code> are used internally at many places. They would need to be threaded through several methods and constructors, thus complicating the implementation unnecessarily.)</li>
</ul>
<p>The <code>BasicProfile</code> is implemented by the <code>BasicDriver</code>.</p>
<p>There is also a new <code>ExtendedProfile</code> for features which are expected to be available in every commonly used SQL database system, albeit with a non-standard syntax. It currently provides string concatenation with the <code>++</code> operator (plus <code>startsWith</code> and <code>endsWith</code> methods which can be implemented with <code>LIKE</code> and string concatenation) and the <code>take</code> and <code>drop</code> methods needed for pagination.</p>
<p>If you only need to support a single database engine in your application, you can forget about profiles and just import a specific driver&#8217;s implicit conversions. Supporting multiple drivers is almost as simple: Give your DAO class (or whatever you have in your application design) a parameter of the required profile type and import this profile&#8217;s implicits. You can then call it with any driver which implements the profile. For example:</p>
<div class="wp_syntax">
<div class="code">
<pre>  def test(profile: ExtendedProfile) {
    import profile.Implicit._
    println("Using driver: "+profile.getClass.getName)
    val q1 = Users.where(_.name startsWith "quote ' and backslash \\").take(5)
    println(q1.selectStatement)
    val q2 = Users.where(_.name startsWith "St".bind).drop(10).take(5)
    println(q2.selectStatement)
    val q3 = Query(42 ~ "foo")
    println(q3.selectStatement)
    println
  }

  def main(args: Array[String]) {
    test(H2Driver)
    test(OracleDriver)
    test(MySQLDriver)
  }
</pre>
</div>
</div>
<p>This prints the different statements required for <a href="http://www.h2database.com/">H2</a>, <a href="http://www.oracle.com/database/index.html">Oracle</a> and <a href="http://www.mysql.com/">MySQL</a>:</p>
<div class="wp_syntax">
<div class="code">
<pre>Using driver: com.novocode.squery.combinator.extended.H2Driver$
SELECT t1.name FROM users t1 WHERE (t1.name like 'quote '' and backslash \%') LIMIT 5
SELECT t1.name FROM users t1 WHERE (t1.name like (? || '%')) LIMIT 5 OFFSET 10
SELECT 42,'foo'

Using driver: com.novocode.squery.combinator.extended.OracleDriver$
SELECT * FROM (SELECT t1.name FROM users t1 WHERE (t1.name like 'quote '' and backslash \%')) WHERE ROWNUM &lt;= 5
SELECT * FROM (SELECT t0.*, ROWNUM ROWNUM_O FROM (t1.name,ROWNUM ROWNUM_I FROM users t1 WHERE (t1.name like (? || '%'))) t0) WHERE ROWNUM_O BETWEEN (1+10) AND (10+5) ORDER BY ROWNUM_I
SELECT 42,'foo' FROM DUAL

Using driver: com.novocode.squery.combinator.extended.MySQLDriver$
SELECT t1.name FROM users t1 WHERE (t1.name like 'quote \' and backslash \\%') LIMIT 5
SELECT t1.name FROM users t1 WHERE (t1.name like concat(?,'%')) LIMIT 10,5
SELECT 42,'foo' FROM DUAL
</pre>
</div>
</div>
<h3>Updates</h3>
<p>You can now take a simple query (returning only named columns from a single table; no modifiers except <code>WHERE</code> restrictions) and call it as an <code>UPDATE</code> statement:</p>
<div class="wp_syntax">
<div class="code">
<pre>  val q = for(u &lt;- Users if u.id is 42) yield u.first ~ u.last
  q.update("foo", "bar")
</pre>
</div>
</div>
<h3>Filtering</h3>
<p>You may already have noticed in my previous post that the <code>Query</code> class now has a <code>filter</code> method which works like <code>where</code> for functions returning a <code>Column[Boolean]</code> or <code>Column[Option[Boolean]]</code>, so you can use Scala&#8217;s standard <em>if</em> clauses in for-comprehensions on queries. Unlike <code>where</code>, <code>filter</code> also accepts functions returning a plain <code>Boolean</code> value to enable the use of refutable patterns in queries (e.g. when destructuring a <code>Join</code>).</p>
<h3>Invokers</h3>
<p>Result type remapping and parameter application are now available for all <code>Invoker</code>s. These features were pushed down from the statement invokers for monadic queries into the invoker framework. Query templates are parameterized invokers now. They can be applied like any other invoker.</p>
]]></content:encoded>
			<wfw:commentRss>http://szeiger.de/blog/2009/08/27/profiles-drivers-and-more/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Efficient Parameterized Queries in ScalaQuery</title>
		<link>http://szeiger.de/blog/2009/08/06/efficient-parameterized-queries-in-scala-query/</link>
		<comments>http://szeiger.de/blog/2009/08/06/efficient-parameterized-queries-in-scala-query/#comments</comments>
		<pubDate>Thu, 06 Aug 2009 17:22:04 +0000</pubDate>
		<dc:creator>Stefan Zeiger</dc:creator>
				<category><![CDATA[Scala]]></category>
		<category><![CDATA[ScalaQuery]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[DSL]]></category>
		<category><![CDATA[JDBC]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://szeiger.de/?p=32</guid>
		<description><![CDATA[One question about ScalaQuery which keeps coming up is that of perfomance. The question is usually in the form &#8220;How does it compare to JDBC?&#8221; but that&#8217;s like comparing apples and, well, apple trees. After all, ScalaQuery is a layer on top of JDBC which provides mainly two things:

A nicer, more Scala-like way of handling [...]]]></description>
			<content:encoded><![CDATA[<p>One question about <a href="http://github.com/szeiger/scala-query/tree/master">ScalaQuery</a> which keeps coming up is that of perfomance. The question is usually in the form &#8220;How does it compare to <a href="http://java.sun.com/javase/technologies/database/">JDBC</a>?&#8221; but that&#8217;s like comparing apples and, well, apple trees. After all, ScalaQuery is a layer on top of JDBC which provides mainly two things:</p>
<ul>
<li>A nicer, more Scala-like way of handling database connections, performing queries and reading result sets. This is not optional when you access a database in your application. You will wrap SQL statement execution and result set reading in some way or another to abstract from the low-level JDBC API. Anyway, the overhead here is quite low.</li>
<li>A way of composing queries with an internal DSL based on a query monad and combinators. That&#8217;s the part I want to talk about in this post.</li>
</ul>
<p>If you want to optimize the query generation away, you can always fall back to the <code>StaticQuery</code> and <code>DynamicQuery</code> classes. These work a bit like <a href="http://ibatis.apache.org/">iBATIS</a>, except your SQL code is embedded directly in your Scala code and not in some XML files. But you&#8217;re still writing SQL! That&#8217;s nice for the special cases which are not covered by the combinator queries and which do not need to be composable but it&#8217;s probably not the reason why you want to use ScalaQuery in the first place.</p>
<p>When you&#8217;re constructing a query in the query monad, you normally have some variables which are used in the query like this:</p>
<div class="wp_syntax">
<div class="code">
<pre>def userNameByID(id: Int) =
  for(u &lt;- Users if u.id is id) yield u.first
</pre>
</div>
</div>
<p>This would insert the user ID directly into the SQL statement, thus requiring a new statement to be generated by ScalaQuery and parsed and optimized by the database server (which can be very expensive) for every invocation of the query. We can improve this by using a <em>bind variable</em> for the user ID:</p>
<div class="wp_syntax">
<div class="code">
<pre>def userNameByID(id: Int) =
  for(u &lt;- Users if u.id is id.bind) yield u.first
</pre>
</div>
</div>
<p>Now the generated SQL statement is always the same (e.g. &#8220;<code>SELECT t1.first FROM users t1 WHERE (t1.id=?)</code>&#8220;), so the database server needs to calculate an execution plan for it only once and can reuse it on subsequent invocations. But the query is still constructed as a tree of dozens of objects and compiled to the same SQL statement every time you call <code>userNameByID</code>.</p>
<p>This can be remedied with <em>query templates</em>, a recent addition to ScalaQuery:</p>
<div class="wp_syntax">
<div class="code">
<pre>val userNameByID = for {
  id &lt;- Parameters[Int]
  u &lt;- Users if u.id is id
} yield u.first
</pre>
</div>
</div>
<p>If you recall how for-comprehensions are desugared, this gets translated to <code>Parameters[Int].apply(...).flatMap(id => ...)</code>. The <code>apply</code> method on the <code>Parameters</code> object takes an implicit <code>TypeMapper</code> for each parameter type you specify and creates a <code>Parameters</code> instance. The <code>flatMap</code> method on <code>Parameters</code> takes a function which creates a <code>Query</code> and returns a <code>QueryTemplate</code> for it:</p>
<div class="wp_syntax">
<div class="code">
<pre>final class QueryTemplate[P, R](query: Query[ColumnBase[R]]) {
  def apply(param: P) = new AppliedQueryTemplate(built, param, query.value)
  lazy val built = QueryBuilder.buildSelect(query, NamingContext())
}

final class Parameters[P, C](c: C) {
  def flatMap[F](f: C => Query[ColumnBase[F]]): QueryTemplate[P, F] =
    new QueryTemplate[P, F](f(c))
  ...
}

object Parameters {
  def apply[P1](implicit tm1: TypeMapper[P1]) =
    new Parameters[P1, Column[P1]](new ParameterColumn(-1, tm1))
  ...
}
</pre>
</div>
</div>
<p>Note that, unlike <code>Query</code>, neither <code>Parameters</code> nor <code>QueryTemplate</code> is a monad. You cannot compose multiple parameter lists or query templates this way but by providing a suitable <code>flatMap</code> method, the parameters can be used as the first generator in a for-comprehension which otherwise operates in the <code>Query</code> monad.</p>
<p>Either one of the <code>userNameByID</code> functions/methods defined above can be used in the same way:</p>
<div class="wp_syntax">
<div class="code">
<pre>for(t &lt;- userNameByID(3)) println(t)
</pre>
</div>
</div>
<p>When you apply the parameters to a <code>QueryTemplate</code>, you get an <code>AppliedQueryTemplate</code> which can be lifted to a suitable <code>Invoker</code> by an implicit conversion (just like a <code>Query</code>). The first time you do this, the SQL code gets generated and then cached in the <code>QueryTemplate</code> for further applications.</p>
<p>If you specify more than one type parameter, the <code>Parameters</code> generator gives you a <code>Projection</code> instead of a single <code>Column</code>. It can be unpacked either with the extractor on the &#8220;<code>~</code>&#8221; object:</p>
<div class="wp_syntax">
<div class="code">
<pre>val userNameByIDRangeAndProduct = for {
  min ~ max ~ product &lt;- Parameters[Int, Int, String]
  u &lt;- Users if u.id >= min &#038;&#038;
    u.id &lt;= max &#038;&#038;
    Orders.where(o => (u.id is o.userID) &#038;&#038; (o.product is product)).exists
} yield u.first
</pre>
</div>
</div>
<p>&#8230;or with the <code>Projection</code> extractor (which is slightly more efficient but not as nice to read):</p>
<div class="wp_syntax">
<div class="code">
<pre>val userNameByIDRangeAndProduct = for {
  Projection(min, max, product) &lt;- Parameters[Int, Int, String]
  u &lt;- Users if u.id >= min &#038;&#038;
    u.id &lt;= max &#038;&#038;
    Orders.where(o => (u.id is o.userID) &#038;&#038; (o.product is product)).exists
} yield u.first
</pre>
</div>
</div>
<p>ScalaQuery&#8217;s invoker framework provides methods for invoking queries with parameters but these are currently used by the simple queries only. I expect to integrate query templates with this system in the future.</p>
]]></content:encoded>
			<wfw:commentRss>http://szeiger.de/blog/2009/08/06/efficient-parameterized-queries-in-scala-query/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>ScalaQuery for different Scala versions</title>
		<link>http://szeiger.de/blog/2009/07/22/scala-query-for-different-scala-versions/</link>
		<comments>http://szeiger.de/blog/2009/07/22/scala-query-for-different-scala-versions/#comments</comments>
		<pubDate>Wed, 22 Jul 2009 20:12:11 +0000</pubDate>
		<dc:creator>Stefan Zeiger</dc:creator>
				<category><![CDATA[Scala]]></category>
		<category><![CDATA[ScalaQuery]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://szeiger.de/?p=31</guid>
		<description><![CDATA[I have just created a new scala-2.7 branch for ScalaQuery. My original plan was to target only Scala 2.8 but since I&#8217;ve made lots of progress during the last few weeks and I&#8217;ve seen increased interest in ScalaQuery, I tried to build it with 2.7.5.
I had to change the semantics of SimpleFunction and SimpleBinaryOperator for [...]]]></description>
			<content:encoded><![CDATA[<p>I have just created a new <a href="http://github.com/szeiger/scala-query/tree/scala-2.7">scala-2.7</a> branch for <a href="http://github.com/szeiger/scala-query/tree/master">ScalaQuery</a>. My original plan was to target only Scala 2.8 but since I&#8217;ve made lots of progress during the last few weeks and I&#8217;ve seen increased interest in ScalaQuery, I tried to build it with 2.7.5.</p>
<p>I had to change the semantics of <code>SimpleFunction</code> and <code>SimpleBinaryOperator</code> for 2.7 but I prefer the new version anyway, so it went into the main line. The code on the scala-2.7 branch is currently identical to the master branch, except for the test classes which are different in two regards:</p>
<ul>
<li>Although the Scala Language Specification mandates that the part left to the &#8220;<code>&lt;-</code>&#8221; in a for comprehension is a pattern, the wildcard pattern &#8220;<code>_</code>&#8221; does not work in 2.7. I have changed it to a dummy variable named &#8220;<code>__</code>&#8220;.</li>
<li>The type inferencer in 2.7 cannot infer the correct type for the implicit <code>OptionMapper</code> objects. <code>OptionMapper[_,_,_,_]</code> has four type parameters, the last one being used for the return type of functions which use the mapper, so it is not yet known when looking for an implicit mapper and gets inferred as <code>Nothing</code>. Scala 2.8 apparently knows that this type parameter is undetermined, finds the single matching implicit object for the other three parameters and then fills in the fourth. The type-correct interoperability of option and non-option types in ScalaQuery relies heavily on the improved type inferencer and I don&#8217;t see any way of making it work nicely with 2.7. The work-around is to add type annotations to the boolean operators, e.g. <code>a &#038;&#038; b</code> might become <code>a.&#038;&[Boolean,Option[Boolean]](b)</code>. Yuck!</li>
</ul>
<p>I&#8217;m still focused on Scala 2.8 as a target platform and will probably not spend much time integrating new features into the scala-2.7 branch but contributions are always welcome.</p>
]]></content:encoded>
			<wfw:commentRss>http://szeiger.de/blog/2009/07/22/scala-query-for-different-scala-versions/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Implicits on Implicits</title>
		<link>http://szeiger.de/blog/2009/07/17/implicits-on-implicits/</link>
		<comments>http://szeiger.de/blog/2009/07/17/implicits-on-implicits/#comments</comments>
		<pubDate>Thu, 16 Jul 2009 22:51:55 +0000</pubDate>
		<dc:creator>Stefan Zeiger</dc:creator>
				<category><![CDATA[Scala]]></category>
		<category><![CDATA[ScalaQuery]]></category>

		<guid isPermaLink="false">http://szeiger.de/?p=30</guid>
		<description><![CDATA[I have recently made a change to ScalaQuery which enables the use of any type for a column without needing specific Column classes and factory methods on Table for it. For this I used an approach which I had not considered earlier and which I wasn&#8217;t even sure would work.
Scala (at least in version 2.7) [...]]]></description>
			<content:encoded><![CDATA[<p>I have recently made a change to <a href="http://github.com/szeiger/scala-query/tree/master">ScalaQuery</a> which enables the use of <em>any</em> type for a column without needing specific <code>Column</code> classes and factory methods on <code>Table</code> for it. For this I used an approach which I had not considered earlier and which I wasn&#8217;t even sure would work.</p>
<p>Scala (at least in version 2.7) only considers single function calls for implicit conversions. Take the following definitions for example:</p>
<div class="wp_syntax">
<div class="code">
<pre>class A
class B(a: A)
class C(b: B)

implicit def aToB(a: A) = new B(a)
implicit def bToC(b: B) = new C(b)

val a = new A

def useB(b: B) = ...
def useC(c: C) = ...
</pre>
</div>
</div>
<p>You can call useB(a) because the compiler finds the implicit conversion aToB(a) but you cannot call useC(a) &#8212; the compiler does not try the chained call bToC(aToB(c)).</p>
<p>But this does not mean that an implicit conversion may not rely on an implicit value! The following works just fine:</p>
<div class="wp_syntax">
<div class="code">
<pre>trait Column[T]
class ConstColumn[T](value: T, tm: TypeMapper[T]) extends Column[T]

implicit def valueToColumn[T](value: T)(implicit tm: TypeMapper[T]) =
  new ConstColumn(value, tm)

trait TypeMapper[T]
implicit object IntTypeMapper extends TypeMapper[Int]

val c1: Column[_] = 42
</pre>
</div>
</div>
<p>The Scala compiler recognizes that <code>valueToColumn[Int]</code> provides the desired conversion from <code>Int</code> to <code>Column[_]</code> and then looks for the required implicit <code>TypeMapper[Int]</code> value, which is available through the <code>implicit IntTypeMapper</code> object.</p>
<p>In ScalaQuery, the real <code>TypeMapper</code> contains only a few methods and there are predefined TypeMappers for most of the basic types used by JDBC. That&#8217;s nice because it removes some redundancy from my code base but the better news is that it allows you to write your own TypeMappers. For example, if you wanted to get the <code>java.lang.Integer</code> columns back which I <a href="http://szeiger.de/blog/2009/06/20/dealing-with-nulls-in-scalaquery/">removed</a> in favor of <code>Int</code> and <code>Option[Int]</code>, you could add this implicit object to your code:</p>
<div class="wp_syntax">
<div class="code">
<pre>implicit object IntegerTypeMapper extends TypeMapper[java.lang.Integer] {
  def zero = null
  def sqlType = java.sql.Types.INTEGER
  def setValue(v: java.lang.Integer, p: PositionedParameters) =
    if(v eq null) p.setIntOption(None) else p.setIntOption(Some(v.intValue))
  def setOption(v: Option[java.lang.Integer], p: PositionedParameters) = v match {
    case Some(null) => p.setIntOption(None)
    case Some(i) => p.setIntOption(Some(i.intValue))
    case None => p.setIntOption(None)
  }
  def nextValue(r: PositionedResult) = r.nextIntOption match {
    case None => null
    case Some(i) => java.lang.Integer.valueOf(i)
  }
}
</pre>
</div>
</div>
<p>The same should work for any database engine- or domain-specific types.</p>
]]></content:encoded>
			<wfw:commentRss>http://szeiger.de/blog/2009/07/17/implicits-on-implicits/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Formal Language Processing in Scala, Solutions to Part 5</title>
		<link>http://szeiger.de/blog/2009/07/15/formal-language-processing-in-scala-solutions-to-part-5/</link>
		<comments>http://szeiger.de/blog/2009/07/15/formal-language-processing-in-scala-solutions-to-part-5/#comments</comments>
		<pubDate>Wed, 15 Jul 2009 17:08:57 +0000</pubDate>
		<dc:creator>Stefan Zeiger</dc:creator>
				<category><![CDATA[Scala]]></category>
		<category><![CDATA[combinator]]></category>
		<category><![CDATA[interpreter]]></category>
		<category><![CDATA[language]]></category>
		<category><![CDATA[parser]]></category>

		<guid isPermaLink="false">http://szeiger.de/?p=29</guid>
		<description><![CDATA[This is the solution to the exercise from part 5.
Some of the details I left out of the specification, but which are needed to write the parser, include:

What are the precedence rules for the operators?
When does &#8220;+&#8221; do string concatenation?
Can you apply a higher-order function multiple times with f(p1)(p2) or do you have to write [...]]]></description>
			<content:encoded><![CDATA[<p>This is the solution to the exercise from <a href="http://szeiger.de/blog/2009/07/05/formal-language-processing-in-scala-part-5/">part 5</a>.<span id="more-29"></span></p>
<p>Some of the details I left out of the specification, but which are needed to write the parser, include:</p>
<ul>
<li>What are the precedence rules for the operators?</li>
<li>When does &#8220;+&#8221; do string concatenation?</li>
<li>Can you apply a higher-order function multiple times with <code>f(p1)(p2)</code> or do you have to write <code>(f(p1))(p2)</code>?</li>
<li>Can expressions be empty (i.e. can you have multiple consecutive semicolons)?</li>
<li>Is an extra semicolon at the end of a sequence allowed?</li>
</ul>
<p>And here is the complete parser and interpreter:</p>
<div class="wp_syntax">
<div class="code">
<pre>import scala.util.parsing.combinator.syntactical.StandardTokenParsers
import scala.util.parsing.input.PagedSeqReader
import scala.collection.immutable.PagedSeq

object Fun1a extends StandardTokenParsers with Application {

  sealed abstract class Expression
  case class Sequence(l: List[Expression]) extends Expression
  case class Lambda(params: List[String], expr: Expression) extends Expression
  case class Let(ident: String, expr: Expression) extends Expression
  case class Var(ident: String) extends Expression
  case class Lit(v: Any) extends Expression
  case class App(expr: Expression, args: List[Expression]) extends Expression
  case class Add(e1: Expression, e2: Expression) extends Expression
  case class Subtract(e1: Expression, e2: Expression) extends Expression
  case class Mult(e1: Expression, e2: Expression) extends Expression
  case class Div(e1: Expression, e2: Expression) extends Expression
  case class Equal(e1: Expression, e2: Expression) extends Expression
  case class If(e1: Expression, e2: Expression, e3: Expression) extends Expression

  lexical.reserved += ("let", "if", "then", "else")
  lexical.delimiters += (";", "(", ")", "+", "-", "*", "/", "\\", "->", ",", "=")

  val tokens = new lexical.Scanner(new PagedSeqReader(PagedSeq.fromFile("src/com/novocode/fun1/test.fun")))

  phrase(sequence)(tokens) match {
    case Success(tree, _) => eval(tree)
    case e: NoSuccess =>
      Console.err.println(e)
      exit(1)
  }

  def sequence = ( rep1sep(expression, ";") ^^ Sequence ) &lt;~ (";"?)

  def expression: Parser[Expression] = lambda | let | equality | ifExpr

  def ifExpr = "if" ~> expression ~ "then" ~ expression ~ "else" ~ expression ^^ {
    case e1 ~ _ ~ e2 ~ _ ~ e3 => If(e1, e2, e3)
  }

  def lambda = "\\" ~> repsep(ident, ",") ~ "->" ~ expression ^^ {
    case params ~ _ ~ expr => Lambda(params, expr)
  }

  def let = "let" ~> ident ~ "=" ~ expression ^^ {
    case ident ~ _ ~ expr => Let(ident, expr)
  }

  def equality = sum * ("=" ^^^ Equal)

  def sum = product * ("+" ^^^ Add | "-" ^^^ Subtract)

  def product = application * ("*" ^^^ Mult | "/" ^^^ Div)

  def application = simpleExpr ~ (args*) ^^ {
    case expr ~ l => l.foldLeft(expr)(App)
  }

  def args = "(" ~> repsep(expression, ",") &lt;~ ")"

  def simpleExpr = ident ^^ Var | "(" ~> expression &lt;~ ")" |
    numericLit ^^ Lit.compose(_.toInt) | stringLit ^^ Lit

  sealed trait Func extends (List[Any] => Any)

  case object Println extends Func {
    def apply(args: List[Any]) = args match {
      case v :: Nil => { println(v); v }
      case _ => error("println needs 1 argument")
    }
  }

  def eval(e: Expression): Any = e match {
    case Sequence(l) => l.foldLeft(():Any) { (z,n) => eval(n) }
    //case Lambda(params, ex) => Closure(ex, params)
    //case Let(id, ex)  => { val v = eval(ex); (v, context + ((id, v))) }
    //case Var(id) => (context getOrElse(id, error("Undefined var "+id)), context)
    case App(expr, args) => eval(expr) match {
      case f:Func => f(args.map(a => eval(a)))
      case _ => error("Only functions can be applied")
    }
    case Lit(v) => v
    case If(e1, e2, e3) => eval(e1) match {
      case b:Boolean => eval(if(b) e2 else e3)
      case _ => error("Not a boolean value in IF expression")
    }
    case Equal(e1, e2) => eval(e1) == eval(e2)
    case Add(e1, e2) => (eval(e1), eval(e2)) match {
      case (v1:String, v2) => v1 + v2.toString
      case (v1, v2:String) => v1.toString + v2
      case (i1:Int, i2:Int) => i1 + i2
      case _ => error("'+' requires two Int values or at least one String")
    }
    case Subtract(e1, e2) => (eval(e1), eval(e2)) match {
      case (i1:Int, i2:Int) => i1 - i2
      case _ => error("'-' requires two Int values")
    }
    case Mult(e1, e2) => (eval(e1), eval(e2)) match {
      case (i1:Int, i2:Int) => i1 * i2
      case _ => error("'*' requires two Int values")
    }
    case Div(e1, e2) => (eval(e1), eval(e2)) match {
      case (i1:Int, i2:Int) => i1 / i2
      case _ => error("'/' requires two Int values")
    }
  }
}
</pre>
</div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://szeiger.de/blog/2009/07/15/formal-language-processing-in-scala-solutions-to-part-5/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Formal Language Processing in Scala, Part 5</title>
		<link>http://szeiger.de/blog/2009/07/05/formal-language-processing-in-scala-part-5/</link>
		<comments>http://szeiger.de/blog/2009/07/05/formal-language-processing-in-scala-part-5/#comments</comments>
		<pubDate>Sun, 05 Jul 2009 14:43:11 +0000</pubDate>
		<dc:creator>Stefan Zeiger</dc:creator>
				<category><![CDATA[Scala]]></category>
		<category><![CDATA[combinator]]></category>
		<category><![CDATA[interpreter]]></category>
		<category><![CDATA[language]]></category>
		<category><![CDATA[parser]]></category>

		<guid isPermaLink="false">http://szeiger.de/?p=28</guid>
		<description><![CDATA[This is the fifth part in a series of articles on formal language processing in Scala. In this part I will introduce some new parser combinators, provide a specification for the Fun1 language and build an interpreter for it.
Binary Operators
I&#8217;ve already shown briefly in the solutions to part 4 how to build a left-recursive AST [...]]]></description>
			<content:encoded><![CDATA[<p>This is the fifth part in a <a href="http://szeiger.de/blog/2008/07/27/formal-language-processing-in-scala-part-1/">series</a> of articles on formal language processing in Scala. In this part I will introduce some new parser combinators, provide a specification for the Fun1 language and build an interpreter for it.<span id="more-28"></span></p>
<h3>Binary Operators</h3>
<p>I&#8217;ve already shown briefly in the <a href="http://szeiger.de/blog/2008/10/12/formal-language-processing-in-scala-solutions-to-part-4/">solutions to part 4</a> how to build a left-recursive AST for left-associative operators. I&#8217;d like to go back one step and look at this in more detail. We are always using repetitions (the &#8220;+&#8221; and &#8220;*&#8221; operators) instead of left-recursive productions in the grammar. Such a repetition creates a <code>List</code> of the individual results which I have so far drawn in the diagrams like an array, e.g. for the expression <code>1+2+3</code>:</p>
<p style="text-align: center;"><img class="aligncenter" src="http://szeiger.de/static/formal-language-processing-in-scala/5-ast1.png" alt="AST with flat List" title="AST with flat List"></a></p>
<p>But the lists are really linked lists consisting of <code>::</code> (cons) and <code>Nil</code> nodes, so we should draw the diagram like this:</p>
<p style="text-align: center;"><img class="aligncenter" src="http://szeiger.de/static/formal-language-processing-in-scala/5-ast2.png" alt="AST with :: and Nil" title="AST with :: and Nil"></a></p>
<p>Well, it looks like those repetitions are still right-recursive after all! But we are able to process them in a left-associative way in the interpreter for n-ary operators by starting with a 0 and left-folding the values computed from the list elements with the + operator:</p>
<div class="wp_syntax">
<div class="code">
<pre>  def compute(e: Expression): Int = e match {
    ...
    case Add(exprs) => exprs.foldLeft(0) { _ + compute(_) }
  }
</pre>
</div>
</div>
<p>Using an initial zero value here was for brevity&#8217;s sake only. All n-ary Add operations contain at least two elements, so <em>exprs</em> can never be empty and we do not really need the 0. Instead we can start with the first element&#8217;s value and just fold the rest:</p>
<div class="wp_syntax">
<div class="code">
<pre>    case Add(e :: l) => l.foldLeft(compute(e)) { _ + compute(_) }
</pre>
</div>
</div>
<p>The same approach allows us to construct a left-recursive AST from a list of <code>Expression</code> nodes directly in the parser. Instead of computing the values of the two sub-expressions and performing the addition, we just create a binary Add expression from them:</p>
<div class="wp_syntax">
<div class="code">
<pre>  lazy val sum = product ~ (("+" ~> product)*) ^^ {
    case e ~ l => l.foldLeft(e)(Add)
  }
</pre>
</div>
</div>
<p>This gives us the AST that we want:</p>
<p style="text-align: center;"><img class="aligncenter" src="http://szeiger.de/static/formal-language-processing-in-scala/5-ast3.png" alt="AST with binary Add" title="AST with binary Add"></a></p>
<p>Turning chains of expressions in the form <code>e ~ ((op ~ e)*)</code> into a left-recursive tree is a common operation in many parsers. Even in the tiny Fun0 language we need almost the same code again for the <code>product</code> production:</p>
<div class="wp_syntax">
<div class="code">
<pre>  lazy val product = simpleExpr ~ (("+" ~> simpleExpr)*) ^^ {
    case e ~ l => l.foldLeft(e)(Mult)
  }
</pre>
</div>
</div>
<p>If we have to distinguish between several operators, it gets slightly more difficult, but the basic pattern is still the same:</p>
<div class="wp_syntax">
<div class="code">
<pre>  lazy val sum = product ~ ( ( ("+"|"-") ~ product)* ) ^^ {
    case e ~ l => l.foldLeft(e) {
      case (z, "+" ~ p) => Add(z, p)
      case (z, "-" ~ p) => Subtract(z, p)
    }
  }
</pre>
</div>
</div>
<h3>Deducing The <em>chainl1</em> Combinator</h3>
<p>When you&#8217;re using a parser generator, you have to implement everything in terms of the built-in abstractions. It&#8217;s a bit like procedural programming: You can write all kinds of functions (<em>productions</em> in this case) that work on <em>data</em> but you cannot write new <em>abstractions</em>. With parser combinators and Scala&#8217;s flexible syntax and support for functional programming, you can. We&#8217;ve already developed some basic elements from EBNF in this series but we don&#8217;t have to stop there.</p>
<p>Let&#8217;s see how we can abstract the parsers for chains of left-associative operators into a reusable combinator by looking at the differences in our implementations of <code>sum</code> (with one or two operators) and <code>product</code>:</p>
<ul>
<li>There is a parser <i>p1</i> of type <code>Parser[Expression]</code> which parses the operands. The result of the combinator is also a <code>Parser[Expression]</code>. In our examples, <i>p1</i> is either <code>product</code> or <code>simpleExpr</code>. Of course, there shouldn&#8217;t be any code specific to expressions in the combinator, so we&#8217;ll use a type variable <i>A</i> and make <i>p1</i> a <code>Parser[A]</code>.</li>
<li>A combining function <i>f</i> of type <code>(A,A) => A</code> is used for folding the results of parsing the operands. This is one of the <code>Add</code>, <code>Subtract</code> and <code>Mult</code> constructors.</li>
<li>A parser <i>p2</i> parses the operators (&#8221;+&#8221;, &#8220;-&#8221; or &#8220;*&#8221;). It doesn&#8217;t produce a result per se but if we have different operators it determines which combining function to use, so we&#8217;ll make the combining function its result. That gives it a type of <code>Parser[(A,A) => A]</code>.</li>
</ul>
<p>Writing the combinator as a function of p1 and p2 is now straight-forward:</p>
<div class="wp_syntax">
<div class="code">
<pre>def chain[A](p1: => Parser[A], p2: => Parser[(A,A) => A]): Parser[A] =
  p1 ~ ( (p2 ~ p1)* ) ^^ {
    case e ~ l => l.foldLeft(e) { case (e1, f ~ e2) => f(e1,e2) }
  }
</pre>
</div>
</div>
<p>This allows us to construct the <code>sum</code> parser as:</p>
<div class="wp_syntax">
<div class="code">
<pre>  def sum = chain(product, "+" ^^ { _ => Add })
</pre>
</div>
</div>
<p>I mentioned above that <code>chain</code> is used frequently so it&#8217;s worth abstracting into a combinator. Actually, it is so useful that Scala&#8217;s parser combinator library provides it out of the box under the names <code>chainl1</code> and <code>*</code> (as a binary operator), so we can just write:</p>
<div class="wp_syntax">
<div class="code">
<pre>  def sum = product * ("+" ^^ { _ => Add })
</pre>
</div>
</div>
<h3>The ^^^ Combinator</h3>
<p>The parser <code>("+" ^^ { _ => Add })</code> which returns a fixed value <code>Add</code> still looks a bit awkward. Fortunately, there is the ^^^ combinator as a shortcut for such cases which allows us to write:</p>
<div class="wp_syntax">
<div class="code">
<pre>  def sum = product * ("+" ^^^ Add)
</pre>
</div>
</div>
<p>A version for multiple operators is equally simple:</p>
<div class="wp_syntax">
<div class="code">
<pre>  def sum = product * ("+" ^^^ Add | "-" ^^^ Subtract)
</pre>
</div>
</div>
<h3>The Fun1 Language</h3>
<p>We have dealt long enough with operators in Fun0 now and it is time to move on from this simple calculator to a real functional programming language with variables, first-class functions and conditional expressions. We&#8217;ll call this language Fun1. I will provide only an informal specification of it for now:</p>
<p>A Fun1 program consists of a sequence of expressions separated by semicolons. Expressions can be nested arbitrarily and grouped with parentheses, as in Fun0. The following kinds of expressions are allowed:</p>
<ul>
<li>String and Int literals</li>
<li>Operators +, -, * and / for arithmetic. The + operator also does string concatenation.</li>
<li>Conditional expressions of the form <code>if <i>e1</i> then <i>e2</i> else <i>e3</i></code> where e1 yields a boolean value</li>
<li>Variables</li>
<li>Lambda expressions (anonymous functions) with 0 or more parameters of the form <code>\<i>p1</i>,<i>p2</i>,<i>p3</i> -> <i>expr</i></code></li>
<li>Variable definitions of the form <code>let <i>v</i> = <i>expr</i></code>. When such a definition occurs in a sequence, the variable is visible to all following expressions in that sequence.</li>
<li>Function applications of the form <code><i>e1</i>(<i>e2</i>,<i>e3</i>,<i>e4</i>)</code> with 0 or more arguments where e1 yields a function value</li>
<li>Equality checks of the form <code><i>e1</i> = <i>e2</i></code> which yield a boolean value</li>
</ul>
<p>The syntactic conventions (e.g. case sensitivity, allowed characters in an identifier, syntax of literals, comments) are those of Scala&#8217;s <code>StandardTokenParsers</code> which we already used for Fun0. The supported types are integers, strings, functions and boolean values. A pre-defined <code>println</code> function can be used to print a single value to the console (and also return it to the caller).</p>
<p>Here is a simple program in Fun1:</p>
<div class="wp_syntax">
<div class="code">
<pre>  let add1 = \a,b -> a+b;
  println(add1(3,4))
</pre>
</div>
</div>
<blockquote><p>Exercise: Create a parser for Fun1 which uses the following AST nodes:</p>
<div class="wp_syntax">
<div class="code">
<pre>  sealed abstract class Expression
  case class Sequence(l: List[Expression]) extends Expression
  case class Lambda(params: List[String], expr: Expression) extends Expression
  case class Let(ident: String, expr: Expression) extends Expression
  case class Var(ident: String) extends Expression
  case class Lit(v: Any) extends Expression
  case class App(expr: Expression, args: List[Expression]) extends Expression
  case class Add(e1: Expression, e2: Expression) extends Expression
  case class Subtract(e1: Expression, e2: Expression) extends Expression
  case class Mult(e1: Expression, e2: Expression) extends Expression
  case class Div(e1: Expression, e2: Expression) extends Expression
  case class Equal(e1: Expression, e2: Expression) extends Expression
  case class If(e1: Expression, e2: Expression, e3: Expression) extends Expression
</pre>
</div>
</div>
<p>What information do you need for the parser that is missing or unclear in the informal description? Feel free to ask for it in a comment or come up with a sensible answer on your own. I have omitted some details deliberately (and probably forgotten some others).</p></blockquote>
<p>Let&#8217;s start with an interpreter in the same way as for Fun0: A function which takes an Expression parameter and matches on the kind of Expression to compute and return its value. We don&#8217;t need a separate method for evaluating statements anymore because there are none in Fun1 &#8212; Everything is an expression. Evaluating literals and arithmetic operators is basically the same as before except we now have to take into account that an expression may return any type of value and not just integers.</p>
<div class="wp_syntax">
<div class="code">
<pre>  def eval(e: Expression): Any = e match {
    case Lit(v) => v
    case Add(e1, e2) => (eval(e1), eval(e2)) match {
      case (v1:String, v2) => v1 + v2.toString
      case (v1, v2:String) => v1.toString + v2
      case (i1:Int, i2:Int) => i1 + i2
      case _ => error("'+' requires two Int values or at least one String")
    }
    case Subtract(e1, e2) => (eval(e1), eval(e2)) match {
      case (i1:Int, i2:Int) => i1 - i2
      case _ => error("'-' requires two Int values")
    }
    case Mult(e1, e2) => (eval(e1), eval(e2)) match {
      case (i1:Int, i2:Int) => i1 * i2
      case _ => error("'*' requires two Int values")
    }
    case Div(e1, e2) => (eval(e1), eval(e2)) match {
      case (i1:Int, i2:Int) => i1 / i2
      case _ => error("'/' requires two Int values")
    }
    // ...more cases...
  }
</pre>
</div>
</div>
<p>For the new equality expressions we can reuse Scala&#8217;s equality checks and return a plain Bool value:</p>
<div class="wp_syntax">
<div class="code">
<pre>    case Equal(e1, e2) => eval(e1) == eval(e2)
</pre>
</div>
</div>
<p>The conditional (<code>if</code>) expressions bring us back to the subject of <a href="http://en.wikipedia.org/wiki/Evaluation_strategy">evaluation strategies</a>. In Fun0 we used call-by-value, i.e. in order to evaluate an operator expression, we first evaluated the operands and then applied the operator to the results. This is the default in most programming languages for most kinds of expressions and we will stick with it, but we have to make at least one exception: An <code>if</code> expression must evaluate either the <code>then</code> clause or the <code>else</code> clause but not both. Even if we did not have any side-effects (which we do, in form of the <code>println</code> function) we couldn&#8217;t just compute both clauses and drop the result of one of them because lambdas and function application allow us to create inifinite recursion, so evaluating the wrong side of an <code>if</code> expression might turn a terminating program into a non-terminating one. With that out of the way, the actual implementation is quite simple:</p>
<div class="wp_syntax">
<div class="code">
<pre>    case If(e1, e2, e3) => eval(e1) match {
      case b:Boolean => eval(if(b) e2 else e3)
      case _ => error("Not a boolean value in IF expression")
    }
</pre>
</div>
</div>
<p>For function calls, we first of all need a representation of function values. Since functions in Fun1 can have different numbers of parameters, we&#8217;ll make it a subtrait of the function type <code>List[Any] => Any</code>:</p>
<div class="wp_syntax">
<div class="code">
<pre>  sealed trait Func extends (List[Any] => Any)
</pre>
</div>
</div>
<p>We can now implement the <code>println</code> function as:</p>
<div class="wp_syntax">
<div class="code">
<pre>  case object Println extends Func {
    def apply(args: List[Any]) = args match {
      case v :: Nil => { println(v); v }
      case _ => error("println needs 1 argument")
    }
  }
</pre>
</div>
</div>
<p>For interpreting function applications we just evaluate all sub-expressions and then invoke the function:</p>
<div class="wp_syntax">
<div class="code">
<pre>    case App(expr, args) => eval(expr) match {
      case f:Func => f(args.map(a => eval(a)))
      case _ => error("Only functions can be applied")
    }
</pre>
</div>
</div>
<h3>Environments</h3>
<p>In order to do variable assignments (with <code>let</code> expressions) and lookups, we need an <em>environment</em> which contains a mapping of variable names to their current values. We&#8217;ll integrate this into the interpreter in a purely functional style now and save the implementation with mutable state for later. Let&#8217;s start with a type alias for the environment:</p>
<div class="wp_syntax">
<div class="code">
<pre>  type Environment = Map[String, Any]
</pre>
</div>
</div>
<p>Notice that the <code>Map</code> here is a <code>scala.collection.immutable.Map</code> which can not be modified but allows a new map to be created which is the same as the existing one plus some new (or modified) entries.</p>
<p>The environment can be changed by a program and different invocations of the <code>eval</code> function must be able to see those changes. The only way to accomplish this without mutable state is to pass the current environment into <code>eval</code> and to have it return a possibly new environment along with the computed value:</p>
<div class="wp_syntax">
<div class="code">
<pre>  def eval(e: Expression, env: Environment): (Any, Environment) = e match {
    // ...
  }
</pre>
</div>
</div>
<p>In the initial call to <code>eval</code> with the parser&#8217;s result we provide an environment which contains only the <code>println</code> function we defined above:</p>
<div class="wp_syntax">
<div class="code">
<pre>  eval(tree, Map("println" -> Println))
</pre>
</div>
</div>
<p>We have to modify the existing code to pass the environment down into recursive calls to <code>eval</code>, drop the possibly modified environments they return, and return the result together with the original one. For example, the case for <code>Equal</code> becomes:</p>
<div class="wp_syntax">
<div class="code">
<pre>    case Equal(e1, e2) => (eval(e1, env)._1 == eval(e2, env)._1, env)
</pre>
</div>
</div>
<p>The other cases can be rewritten similarly.</p>
<p>We can now use the environment for variable lookups and definitions. The code for <code>Var</code> just performs the lookup while <code>Let</code> returns a new environment with all previous definitions plus the new binding:</p>
<div class="wp_syntax">
<div class="code">
<pre>    case Var(id) =>
      (env getOrElse(id, error("Undefined var "+id)), env)
    case Let(id, ex)  =>
      { val v = eval(ex, env)._1; (v, env + ((id, v))) }
</pre>
</div>
</div>
<p>For a sequence we go through all the sub-expressions, evaluating each in the environment returned by the previous one and returning the last value + environment. Notice the initial value <code>()</code> (&#8221;unit&#8221;) which gets returned for an empty sequence.</p>
<div class="wp_syntax">
<div class="code">
<pre>    case Sequence(l) =>
      l.foldLeft(():Any, env) { (z,n) => eval(n, z._2) }
</pre>
</div>
</div>
<p>Lambda expressions are supposed to close over the variables which are visible at their point of creation, so we cannot just return a <code>Lambda</code> expression object directly as its own value. Instead we wrap the parameter list and expression of the <code>Lambda</code> together with the current environment in a <code>Closure</code> object:</p>
<div class="wp_syntax">
<div class="code">
<pre>    case Lambda(params, ex) => (Closure(ex, params, env), env)
</pre>
</div>
</div>
<p>Since the environment is immutable, there is no need to create a copy. We can just keep this value around and use it to apply the lambda function at a later time.</p>
<p>We have already defined the <code>Func</code> trait and implemented function application for it, so we make <code>Closure</code> implement it, too. In order to apply the lambda function, we take the saved environment, add the function&#8217;s parameters on top of it, and use this new environment for evaluating the saved expression:</p>
<div class="wp_syntax">
<div class="code">
<pre>  case class Closure(ex: Expression, params: List[String],
                     env: Environment) extends Func {
    def apply(args: List[Any]) = {
      if(args.length != params.length)
        error("Wrong number of arguments for function")
      eval(ex, env ++ params.zip(args))._1
    }
    override def toString =
      "&lt;lambda \\" + params.mkString(",") + " -> " + ex + ">"
  }
</pre>
</div>
</div>
<p>This concludes the interpreter for Fun1 and it also brings me to the end of the content I had originally planned for this series almost one year ago. There are still lots of interesting topics left and I intend to pursue some of them in further installments but I have not yet decided what will be next. For now, I&#8217;ll leave you with a short Fun1 program to test your implementation:</p>
<div class="wp_syntax">
<div class="code">
<pre>// Lambda application -- prints "81"
println((\x -> x*x)(9));

// Shadowing s does not change the closure -- prints "foo!"
let s = "foo";
let ls = \x -> s + x;
let s = "bar";
println(ls("!"));

// N-ary and curried functions -- both print "7"
let add1 = \a,b -> a+b;
println(add1(3,4));
let add2 = \a -> \b -> a+b;
println(add2(3)(4));

// The Y combinator allows us to define recursive functions
let Y = \f ->
  (\x -> f(\y -> x(x)(y)))
  (\x -> f(\y -> x(x)(y)));

// Define a faculty function using Y -- prints "6! = 720"
let fact = Y(\fact -> \n -> if n = 0 then 1 else n*fact(n-1));
println("6! = "+fact(6));

// Print a nullary function "&lt;lambda...>" and its value "42"
let nullary = \-> 42;
println(nullary);
println(nullary())
</pre>
</div>
</div>
<p>I&#8217;ll make the source code for my reference implementation available in the solutions to this part, coming up in a few days.</p>
]]></content:encoded>
			<wfw:commentRss>http://szeiger.de/blog/2009/07/05/formal-language-processing-in-scala-part-5/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Dealing with NULLs in ScalaQuery</title>
		<link>http://szeiger.de/blog/2009/06/20/dealing-with-nulls-in-scalaquery/</link>
		<comments>http://szeiger.de/blog/2009/06/20/dealing-with-nulls-in-scalaquery/#comments</comments>
		<pubDate>Sat, 20 Jun 2009 14:56:42 +0000</pubDate>
		<dc:creator>Stefan Zeiger</dc:creator>
				<category><![CDATA[Scala]]></category>
		<category><![CDATA[ScalaQuery]]></category>
		<category><![CDATA[NULL]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://szeiger.de/?p=27</guid>
		<description><![CDATA[Previously, ScalaQuery used nullable Java types for all columns. This is straight-forward for reference types like java.lang.String which are mapped 1:1 to Scala types but not for Scala&#8217;s value types like scala.Int or scala.Boolean. Although they are often implemented in terms of Java types like java.lang.Integer and java.lang.Boolean, there is no 1:1 mapping and there [...]]]></description>
			<content:encoded><![CDATA[<p>Previously, <a href="http://github.com/szeiger/scala-query/tree">ScalaQuery</a> used nullable Java types for all columns. This is straight-forward for reference types like <code>java.lang.String</code> which are mapped 1:1 to Scala types but not for Scala&#8217;s value types like <code>scala.Int</code> or <code>scala.Boolean</code>. Although they are often implemented in terms of Java types like <code>java.lang.Integer</code> and <code>java.lang.Boolean</code>, there is no 1:1 mapping and there are no aliases for those wrapper types in Scala. Most of the time this is not a problem because you can use Scala&#8217;s value types everywhere and have the compiler generate optimized code with primitive types where applicable.</p>
<p>Except there is one thing you cannot do with Scala&#8217;s value types: They may never be <code>null</code>. The reference types are nullable but that&#8217;s probably <a href="http://lambda-the-ultimate.org/node/3186">not a good idea</a>, either. Scala needs nullable reference types for interfacing with Java code but for native Scala APIs, the Option type is the preferred solution. It makes <code>None</code> values explicit and thus prevents <code>NullPointerException</code>s at runtime when a <code>null</code> value is used in a place where it shouldn&#8217;t be allowed.</p>
<p>SQL databases also use <code>NULL</code> values for various things (e.g. missing values, undefined values, <a href="http://stackoverflow.com/questions/203493/why-does-oracle-9i-treat-an-empty-string-as-null">horrid abominations</a>) so this should work nicely with Scala&#8217;s <code>Option</code> type. I rejected <code>Option</code>s for database results in ScalaQuery at first because most columns are not nullable (so you wouldn&#8217;t want to have <em>all</em> columns return an </code>Option</code> instead of just the nullable ones) yet some operations like <em>outer joins</em> need to convert non-nullable columns to nullable ones (which can probably not be typed in Scala's type system, so you'd have to make all columns return an <code>Option</code>).</p>
<p>On the other hand, having nullable <code>java.lang.Integer</code> and other Java wrapper types in a Scala API is rather ugly, so I finally removed them in favor of a more practical yet null-free solution. All columns now use proper Scala types (<code>scala.Int</code>, <code>scala.Predef.String</code>, etc.). <code>NULL</code>s from the database are returned as a default value (0 for <code>Int</code>, an empty string for <code>String</code>) which can be changed with the <code>orElse</code> method, e.g.:</p>
<div class="wp_syntax">
<div class="code">
<pre>  for(u &lt;- Users)
    yield u.first.orElse("no first name")
        ~ u.last.orElse("no last name")
</pre>
</div>
</div>
<p>Note that this value is only used when extracting rows from the result set on the client side. It does not change the handling of <code>NULL</code>s inside the database server!</p>
<p>For nullable types like <code>scala.Predef.String</code> you can use <code>orElse(null)</code> to get the old behaviour back. The argument of <code>orElse()</code> is passed by name, so you can also run an arbitrary block of code or throw an exception when a <code>NULL</code> value is encountered in the result set. There is a convencience method called <code>orFail</code> which does just that. Maybe this should be the default instead of returning a zero value?</p>
<p>The preferred solution for distinguishing between regular values and <code>NULL</code> for any type is to use the <code>?</code> method to turn a <code>SimpleColumn</code> of type <code>T</code> into one of type <code>Option[T]</code>:</p>
<div class="wp_syntax">
<div class="code">
<pre>  for(u &lt;- Users) yield u.first.? ~ u.last.?
</pre>
</div>
</div>
<p>Currently there are no operations defined on option columns so it does not make sense to declare an option column directly as part of a table. Ideally, columns of types <code>T</code> and <code>Option[T]</code> should be fully interoperable because the database server uses three-valued logic anyway.</p>
<p>I have just pushed the new code to the <a href="http://github.com/szeiger/scala-query/tree/master">master</a> branch and updated the <a href="http://github.com/szeiger/scala-query/tree/stable">stable</a> branch to include the previously redesigned AST.</p>
]]></content:encoded>
			<wfw:commentRss>http://szeiger.de/blog/2009/06/20/dealing-with-nulls-in-scalaquery/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
