<?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-01-31T10:38:54-05: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[Abstract Generic Collections: 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[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>

