<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title><![CDATA[Jordan Lewis]]></title>
  <link href="http://jordanlewis.org/atom.xml" rel="self"/>
  <link href="http://jordanlewis.org/"/>
  <updated>2012-05-03T10:33:46-04:00</updated>
  <id>http://jordanlewis.org/</id>
  <author>
    <name><![CDATA[Jordan Lewis]]></name>
    <email><![CDATA[jordanthelewis@gmail.com]]></email>
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  
  <entry>
    <title type="html"><![CDATA[PFDS Section 2.2]]></title>
    <link href="http://jordanlewis.org/blog/2012/05/01/pfds-section-2-dot-2/"/>
    <updated>2012-05-01T16:33:00-04:00</updated>
    <id>http://jordanlewis.org/blog/2012/05/01/pfds-section-2-dot-2</id>
    <content type="html"><![CDATA[<p>Section 2.2 presents immutable sets implemented with unbalanced binary search
trees, a slightly more complex example of immutable data sharing than the list
example in <a href="http://jordanlewis.org/blog/2012/01/27/chapter-1-dot-1/">Section 2.1</a>. My
first challenge was to reimplement Okasaki&#8217;s base implementation of unbalanced
binary search tree sets using idiomatic Scala. I had to learn a fair amount
more about Scala&#8217;s type system to be able to write such an implementation, so I
figured I&#8217;d write up some of the things I learned about Scala in the process as
well as the implementation.</p>

<h2>A generic trait for sets</h2>

<p>Okasaki&#8217;s set interface contains two methods, <code>insert</code> and <code>member</code>. Similar to
the generic stack trait implementation that I wrote about
<a href="http://jordanlewis.org/blog/2012/01/31/abstract-generic-collections-section-2-dot-1-redux/">last time</a>
, it&#8217;s easy to create this interface as a generic trait in Scala.</p>

<div><script src='https://gist.github.com/2565386.js?file='></script>
<noscript><pre><code>trait Set[T] {
  def insert(x: T): Set[T]
  def member(x: T): Boolean
}</code></pre></noscript></div>


<h2>Unbalanced binary search trees</h2>

<p>Unbalanced binary search trees are a great choice for a simple implementation
of Okasaki&#8217;s set interface, since <code>insert</code> and <code>member</code> in an unbalanced binary
search tree both take on average <code>O(log n)</code> time in the number of elements in
the tree, or <code>O(n)</code> time in the pathological case where each subsequent insert
is greater than the last.</p>

<p>I decided to follow Okasaki&#8217;s functor-like pattern for the implementation, so
I wrote a simple binary tree data type using case classes. This allowed me to
use Scala&#8217;s pattern matching feature in my implementation of the binary search
tree.</p>

<div><script src='https://gist.github.com/2573618.js?file='></script>
<noscript><pre><code>sealed abstract class BinaryTree[+T]
case object BinaryTreeLeaf extends BinaryTree[Nothing]
final case class BinaryTreeNode[T](left: BinaryTree[T], value: T, right: BinaryTree[T]) extends BinaryTree[T]</code></pre></noscript></div>


<p>Binary search trees rely on the ordering of the type of element that they&#8217;re
storing, so a generic binary search tree implementation similarly must require
that its type parameter has an order. In Scala, this can be accomplished with
either of two methods: either by using a <em>view bound</em> on the type parameter to
require that it be implicitly convertable to an Ordered type, or by requiring
an instance of Ordering, parameterized on the binary tree&#8217;s input type, as a
value parameter.</p>

<p>In trying to write the most Scala-idiomatic version of the data structure, I
ended up investigating both methods before deciding on one, since it wasn&#8217;t
obvious at first which method would be the simplest. If you&#8217;re already familiar
with Scala view bounds and type orderings, or if you just want to get to the
implementation of the 2.2 data structures already, then you should skip right
ahead to <a href="#implementation">the implementation</a>.</p>

<h3>View bounds</h3>

<p>A view bound is specified by the <code>&lt;%</code> operator. Specifying <code>T &lt;% U</code> in a type
parameter adds the restriction that the type parameter <code>T</code> must be <em>implicitly
convertible</em> to <code>U</code>, which is true when there exists a method</p>

<pre><code>implicit def t2u(t: T): U
</code></pre>

<p>somewhere in the current scope. In our case, we&#8217;re interested in view bounding
<code>T</code> by <code>Ordered[T]</code>, which will give us access to the comparison methods (<code>&lt;</code>,
<code>&gt;</code>, etc) defined on the <code>Ordered[T]</code> type for values of type <code>T</code>.</p>

<p>For example, to implement a generic less-than function lt that utilizes the
<code>&lt;</code> method on the input type&#8217;s <code>Ordered</code> implementation, we write</p>

<pre><code>def lt[T &lt;% Ordered[T]](x: T, y: T): Boolean = x &lt; y
</code></pre>

<p>Without specifying the view bound on <code>T</code>, we would not have access
to the <code>&lt;</code> method on <code>x</code>, since it is not defined for any arbitrary
type <code>T</code>.</p>

<h3>Ordering instances</h3>

<p>The other Scala-idiomatic way to provide or access the ordering of a type <code>T</code>
is through objects that implement the trait <code>Ordering[T]</code>. An object that
implements <code>Ordering[T]</code> provides the <code>compare(x: T, y: T)</code> method, which acts
as the underlying implementation for the trait&#8217;s other methods, which include
<code>lt(x: T, y: T)</code>, <code>gt(x: T, y: T)</code>, and the like.</p>

<p>For example, to implement a generic less-than function lt that utilizes the
<code>lt</code> method on the input type&#8217;s <code>Ordering</code>
implementation, we could write the curried method</p>

<pre><code>def lt[T](x: T, y: T)(implicit val ordering: Ordering[T]): Boolean = ordering.lt(x, y)
</code></pre>

<p>Note that we don&#8217;t have to specify a type bound on <code>T</code>, but we still ensure
that <code>T</code> is comparable at the type system level by requiring as an argument an
ordering object that&#8217;s parameterized on <code>T</code>. We make the ordering an implicit
and curried argument, so that if <code>T</code> has an implicit conversion to
<code>Ordering[T]</code>, as most of Scala&#8217;s comparable types do, the user doesn&#8217;t have to
explicitly pass in the ordering.</p>

<p>Using instances of <code>Ordering[T]</code> makes your code slightly less pretty, as you
can no longer write <code>x &lt; y</code>, you have to instead write <code>ordering.lt(x, y)</code>, but
it makes more explicit what&#8217;s going on under the hood. It wouldn&#8217;t necessarily
be obvious that writing <code>x &lt; y</code> where <code>x</code> and <code>y</code> are instances of type <code>T &lt;%
Ordered[T]</code> actually invokes the <code>&lt;</code> method on whatever <code>Ordered[T]</code> object <code>x</code>
and <code>y</code> are implicitly convertible to. I think I prefer the more explicit
version using <code>Ordering[T]</code>, but that&#8217;s probably because using implicit
conversions at all leaves me with a funny taste in my mouth. It seems to me
that overusing implicit conversions would lead to a special hell of confusing
spaghetti code.</p>

<p><a id="implementation"></a></p>

<h2>Scala implementation of unbalanced binary tree sets</h2>

<p>For the implementation, I wrote the actual <code>insert</code> and <code>member</code> logic as
private methods on a utility object. The class that actually implements the Set
trait uses the utility object as its underlying implementation. I used Scala&#8217;s
<em>companion object</em> idiom to structure the whole thing in an elegant way.</p>

<p>Here&#8217;s the companion object that contains the underlying implementation logic:</p>

<div><script src='https://gist.github.com/2573799.js?file='></script>
<noscript><pre><code>object UnbalancedTreeSet {
  private def insert[T](x: T, t: BinaryTree[T])(implicit ordering: Ordering[T]): BinaryTree[T] = t match {
    case BinaryTreeLeaf =&gt; BinaryTreeNode(BinaryTreeLeaf, x, BinaryTreeLeaf)
    case tn @ BinaryTreeNode(a, y, b) =&gt;
      if (ordering.lt(x, y)) BinaryTreeNode(insert(x, a), y, b)
      else if (ordering.lt(y, x)) BinaryTreeNode(a, y, insert(x, b))
      else tn
  }
  private def member[T](x: T, t: BinaryTree[T])(implicit ordering: Ordering[T]): Boolean = (x, t) match {
    case (_, BinaryTreeLeaf) =&gt; false
    case (_, BinaryTreeNode(a, y, b)) =&gt;
      if (ordering.lt(x, y)) member(x, a)
      else if (ordering.lt(y, x)) member(x, b)
      else true
  }
}</code></pre></noscript></div>


<p>And here&#8217;s the class that actually implements the Set interface using the
companion object:</p>

<div><script src='https://gist.github.com/2573976.js?file='></script>
<noscript><pre><code>class UnbalancedTreeSet[T] private (tree: BinaryTree[T]) (implicit val ordering: Ordering[T])
  extends Set[T] {

  def this()(implicit ordering : Ordering[T]) = this(BinaryTreeLeaf)(ordering)
  private def newSet(tree: BinaryTree[T]) = new UnbalancedTreeSet[T](tree);

  def insert(x: T) = newSet(UnbalancedTreeSet.insert(x, tree))
  def member(x: T) = UnbalancedTreeSet.member(x, tree)
}</code></pre></noscript></div>


<p>You can see the code in its entirety, as well as a small unit test, on
<a href="https://github.com/jordanlewis/PFDScala">GitHub</a>. Next time I&#8217;ll implement
solutions for section 2.2&#8217;s exercises.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Abstract Generic Collections: PFDS Section 2.1 Redux]]></title>
    <link href="http://jordanlewis.org/blog/2012/01/31/abstract-generic-collections-section-2-dot-1-redux/"/>
    <updated>2012-01-31T00:34:00-05:00</updated>
    <id>http://jordanlewis.org/blog/2012/01/31/abstract-generic-collections-section-2-dot-1-redux</id>
    <content type="html"><![CDATA[<p>At the end of my last post, I mentioned that I ended up reusing Scala&#8217;s build-in
List collection to implement the exercises instead of writing a generic abstract
Stack and sample implementations of those. Since then, I&#8217;ve spent some time
learning about how to implement generic collections in Scala. I came up with
a Stack trait, <em>a la</em> Okasaki&#8217;s Stack signature, and three implementations: the
first two are straightforward translations of the SML structures given in the
book, and the third is a more Scala-idiomatic implementation.</p>

<p>On an organizational note, I&#8217;ve also started a
<a href="https://github.com/jordanlewis/PFDScala">PFDScala repository</a> on GitHub for
this blog series, so I can have a centralized place to put all of the code I
write for the exercises and examples. I&#8217;ll still put the relevant snippets in
gists so I can embed them here.</p>

<h2>A Generic Trait for Stacks</h2>

<p>Scala traits are very powerful. They&#8217;re basically like mixin classes from Ruby,
in that your class can extend from multiple traits without having the troubles
that plague C++-style multiple inheritance.</p>

<p>Abstract traits can be used like Java interfaces. This is just what we need to
write a generic interface for Stacks:</p>

<div><script src='https://gist.github.com/1709179.js?file='></script>
<noscript><pre><code>trait Stack[+T] {
  def isEmpty : Boolean

  def cons[U &gt;: T](x: U) : Stack[U]
  def head : T
  def tail : Stack[T]
}
</code></pre></noscript></div>


<p>For those of you unfamiliar with Scala, this is similar to a Java interface or
a SML signature: we&#8217;re defining a type-parameterized trait/interface/signature
with a bunch of method signatures that classes which extend this trait must
implement themselves. What&#8217;s up with the weird type annotations, though?</p>

<p>In Scala, annotating a type parameter with &#8220;+&#8221; marks it as a covariant type
parameter. This means that if we have a type C[+T], a type with a single
covariant type parameter, and if type A is a subtype of type B, then type C[A]
is a subtype of C[B]. We can similarly annotate a type parameter with &#8220;-&#8221; for
the opposite effect.</p>

<p>The &#8220;>:&#8221; type operator is the supertype relationship: the type on the left of
it has to be a supertype of the type on the right of it.</p>

<p>So why do we need to annotate our types like this here? In Scala, by default,
types are invariant in their type parameters. This means if we had a
List[Integer], and List&#8217;s type parameter was invariant, then that list would
not be a subtype of a List[Number], even though it seems like it would be fine
because a List of Integers is just a specialized List of Numbers. The same thing
goes for our Stack type, so we mark its type parameter up as covariant.</p>

<p>However, to keep compile-time type checking sound, Scala has to impose some
restrictions on the types of methods in classes with covariant type parameters,
due to the problem of mutability: a mutable array of type T is actually
<em>contravariant</em> in its type parameter, because if you had an Array of type
Any, updating one of its cells to be a subtype of Any like String is not always
legal. So, in our Stack example, we can no longer simply say</p>

<pre><code>def cons(x: T) : Stack[T]
</code></pre>

<p>because the x is appearing in a <em>contravariant</em> position, meaning that it has
the potential to mutate state in a way that could break type safety.</p>

<p>Luckily, we can get around this problem of contraviariant position by imposing
a bound on the type of cons&#8217;s parameter: we say</p>

<pre><code>def cons(x: U &gt;: T) : Stack[U]
</code></pre>

<p>to ensure that the input type of cons is a supertype of the stack&#8217;s type. This
prevents any type safety issues caused by the potential contravariance of the
formal parameter, and allows users to generalize the type of a Stack by consing
a more general type onto it. For example, one could cons a String onto a
Stack[Int] and get out a Stack[Any].</p>

<h2>Implementation 1: Using builtin Lists as the backing store</h2>

<p>This class uses the builtin List implementation to provide the underlying
storage for the Stack. It also uses Scala&#8217;s <em>companion object</em> feature to
provide a static factory method for creating new ListStacks.</p>

<div><script src='https://gist.github.com/1709271.js?file='></script>
<noscript><pre><code>object ListStack {
  def apply[T] : ListStack[T] = new ListStack[T](List())
}

class ListStack[T] private (val l: List[T]) extends Stack[T] {
  def isEmpty = l.isEmpty

  def cons[U &gt;: T](x: U) = new ListStack(x::l)
  def head = l.head
  def tail = new ListStack(l.tail)
}
</code></pre></noscript></div>


<h2>Implementation 2: Using a custom List case class as the backing store</h2>

<p>This class uses a custom implementation of List as the backing store. It&#8217;s the
same idea as the first one, except we use a set of case classes to match on
instead of just wrapping List&#8217;s functions.</p>

<div><script src='https://gist.github.com/1709288.js?file='></script>
<noscript><pre><code>sealed abstract class LIST[+T]
case object NIL extends LIST[Nothing]
final case class CONS[T](head: T, tail: LIST[T]) extends LIST[T]

object CustomListStack {
  def apply[T] = new CustomListStack[T](NIL)
}

class CustomListStack[T] private (private val l: LIST[T]) extends Stack[T] {
  def isEmpty = l match {
    case NIL =&gt; true
    case CONS(_, _) =&gt; false
  }

  def cons[U &gt;: T](x: U) = new CustomListStack[U](new CONS[U](x, l))
  def head = l match {
    case NIL =&gt; throw new IllegalArgumentException(&quot;Fail&quot;)
    case CONS(x, _) =&gt; x
  }

  def tail = l match {
    case NIL =&gt; throw new IllegalArgumentException(&quot;Fail&quot;)
    case CONS(_, x) =&gt; new CustomListStack[T](x)
  }
}</code></pre></noscript></div>


<p>This shows off Scala&#8217;s case classes a little bit: they&#8217;re just like datatypes
in SML, except a bit more verbose to specify. The abstract class LIST is like
the name of the datatype, and the case classes that inherit from it are like
the datatype&#8217;s constructors. Making LIST <em>sealed</em> restricts classes from
extending it unless they&#8217;re in the same file: this allows the compiler to detect
non-exhaustive matches.</p>

<h2>Implementation 3: Implementing the Stack functions within case classes</h2>

<p>This implementation is a bit different from the other two: instead of storing
the actual Stack data as a data member inside of a single StackImpl class, this
puts the implementation inside of case classes that extend the implementation
class, which is made abstract.</p>

<p>This strategy seems the most idiomatic of the implementations I wrote. It
defines its own internal datatype like the custom List implementation, without
the mess of having to match on each of the cases of the custom List every time
it&#8217;s necessary to operate on the data. I think it produces the most compact
and readable code out of all three implementations.</p>

<div><script src='https://gist.github.com/1709315.js?file='></script>
<noscript><pre><code>sealed abstract class CustomStack[+T] extends Stack[T]
case object EmptyCustomStack extends CustomStack[Nothing] {
  def isEmpty = true
  def cons[T](x: T) = new NonEmptyCustomStack[T](x, EmptyCustomStack)
  def head = throw new IllegalArgumentException(&quot;Can't take head of an empty list&quot;)
  def tail = throw new IllegalArgumentException(&quot;Can't take head of an empty list&quot;)
}
final case class NonEmptyCustomStack[+T](head: T, tail: CustomStack[T]) extends CustomStack[T] {
  def isEmpty = false
  def cons[U &gt;: T](x: U) = new NonEmptyCustomStack[U](x, this)
}
</code></pre></noscript></div>


<h2>Conclusion</h2>

<p>That was a long-winded post for what was just defining a simple custom
collection interface and a few simple implementations. Again, all of this code is available in the
<a href="https://github.com/jordanlewis/PFDScala">PFDScala GitHub repo</a>. Next time
we&#8217;ll proceed with the next section, 2.2. Hopefully it will be easier to
implement the examples and exercise solutions with the improved understanding
of Scala&#8217;s type system that I gathered by writing this post.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[PFDS Section 2.1]]></title>
    <link href="http://jordanlewis.org/blog/2012/01/27/chapter-1-dot-1/"/>
    <updated>2012-01-27T22:09:00-05:00</updated>
    <id>http://jordanlewis.org/blog/2012/01/27/chapter-1-dot-1</id>
    <content type="html"><![CDATA[<p>This is the inaugural post of the PFDS series.</p>

<p>Section 2.1 discusses the ramifications of implementing lists and stacks in a
functional and immutable manner. Using the operation of <em>list catenation</em>
as a motivator, Okasaki introduces the idea of data sharing. We see that to
catenate two lists, we can share the second list, which doesn&#8217;t get modified,
but must copy all of the nodes in the first list just to modify the last one.</p>

<p>In Scala, the function catenate on the built-in list type looks like the
following:</p>

<div><script src='https://gist.github.com/1692508.js?file='></script>
<noscript><pre><code>def catenate[A](xs: List[A], ys: List[A]) : List[A] = xs match {
  case Nil =&gt; ys
  case x::xs =&gt; x :: (catenate(xs, ys))
 }</code></pre></noscript></div>


<p>Updating a single element of the list is similar. We have to copy all of the
elements in the list up to the element to be updated, and then we can point the
tail of the element we updated to the pre-existing tail of the old element,
thus sharing as much data as possible.</p>

<div><script src='https://gist.github.com/1692539.js?file='></script>
<noscript><pre><code>def update[A](xs: List[A], i: Int, y: A) : List[A] = (xs, i) match {
  case (Nil, _) =&gt; throw new AssertionError(&quot;Invalid subscript&quot;)
  case (x::xs, 0) =&gt; y::xs
  case (x::xs, i) =&gt; x::update(xs, i - 1, y)
}</code></pre></noscript></div>


<h3>Exercise 2.1</h3>

<p>This exercise is straightforward: we must write a function that takes a generic
list and returns a list of all of the suffix lists of the input list,
from longest to shortest. We must show that this function operates in linear
time and linear space with respect to the size of the input list.</p>

<p>Since no elements are being updated, it&#8217;s easy to see that all we have to do is
return a new list whose elements are every cons cell in the input list. This
is O(n) in time, as we are performing one cons operation for each element in the
input list, and O(n) in space, as we&#8217;re saving one cons cell per cons cell in
the input list.</p>

<div><script src='https://gist.github.com/1692330.js?file='></script>
<noscript><pre><code>def suffixes[A](xs: List[A]): List[List[A]] = xs match {
  case Nil =&gt; Nil::Nil
  case x::xs =&gt; (x::xs)::suffixes(xs)</code></pre></noscript></div>


<p>This was a pretty simple section, serving mainly as a refresher course in
functional programming fundamentals.</p>

<p>For this post, I reused Scala&#8217;s built-in List type to implement the exercise
and example functions. I had intended to define my own abstract generic Stack,
and show how it can be implemented with either the build-in List type or a set
of case classes Nil and Cons, like Okasaki does using Standard ML. However, I&#8217;m
still a Scala novice, and I ran into some difficulties with the type system
that go over my head at this point. I plan to revisit this at a later date once
I&#8217;ve learned a little bit more about Scala&#8217;s type system.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Notes on Purely Functional Data Structures]]></title>
    <link href="http://jordanlewis.org/blog/2012/01/03/notes-on-purely-functional-data-structures/"/>
    <updated>2012-01-03T01:24:00-05:00</updated>
    <id>http://jordanlewis.org/blog/2012/01/03/notes-on-purely-functional-data-structures</id>
    <content type="html"><![CDATA[<p>I heard a lot of good things about Mike Okasaki&#8217;s <a href="http://www.amazon.com/gp/product/0521663504/ref=as_li_qf_sp_asin_tl?ie=UTF8&tag=jordanlewisor-20&linkCode=as2&camp=1789&creative=9325&creativeASIN=0521663504">Purely Functional Data Structures</a><img src="http://www.assoc-amazon.com/e/ir?t=jordanlewisor-20&l=as2&o=1&a=0521663504" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /> at UChicago, but didn&#8217;t ever take the time to check it out. Lately I&#8217;ve missed the heady joy of reading and writing code in a strongly typed functional programming language like Standard ML, so when one of my coworkers at Knewton mentioned he was going to read the book I decided to get a copy for myself.</p>

<p>I&#8217;m going to try to read through the whole book and complete as many of the exercises that I can. To help myself keep the commitment, I&#8217;m going to follow in Eli Bendersky&#8217;s footsteps and post reading notes and exercise solutions along the way, as <a href="http://eli.thegreenplace.net/2007/06/19/introducing-the-sicp-reading-notes/">he did for SICP</a>.</p>

<p>The notes will be categorized under <a href="http://jordanlewis.org/blog/categories/pfds/">pfds</a>.</p>

<p>Also, I&#8217;ve recently begun to learn Scala, a strongly typed functional language on the JVM with nice features such as algebraic datatypes in the form of case classes, pattern matching, and lazy values. Given the usefulness of these language amenities for exploration of Okasaki&#8217;s concepts, I&#8217;m going to do the exercises in Scala instead of Standard ML or Haskell.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Hello World]]></title>
    <link href="http://jordanlewis.org/blog/2012/01/02/hello-world/"/>
    <updated>2012-01-02T01:50:00-05:00</updated>
    <id>http://jordanlewis.org/blog/2012/01/02/hello-world</id>
    <content type="html"><![CDATA[<p>Hi internet! I&#8217;ve gotten with the program and bloggified my website with the help of the pretty rad <a href="http://octopress.org">Octopress</a> framework. Hope you enjoy it.</p>
]]></content>
  </entry>
  
</feed>

