logo      
Custom Search

Re: Arrows in Scala

Date: December 28, 2006
From: "Burak Emir" <burak.emir@xxxxxxxxx>

In-reply-to: <8081081.post@xxxxxxxxxxxxxxx>
References: <F418C1909C57A14E9456C8BEA8856345014907E3@xxxxxxxxxxxxxxxxxx> <8081081.post@xxxxxxxxxxxxxxx>



On 12/28/06, steve.bendiola <steve.bendiola@xxxxxxxxx> wrote:

I am tryig to compile the Arrow example from
http://scala.sygneca.com/code/arrows http://scala.sygneca.com/code/arrows
and I get this error:
Arrow.scala:123 error: not found: value Left
    arr { i: I => if (sel(i)) Left(a(i)) else Right(b(i)) }
                                  ^
Arrow.scala:123 error: not found: value Right
    arr { i: I => if (sel(i)) Left(a(i)) else Right(b(i)) }
                                                     ^
two errors found
$ scala -version Scala code runner version 2.3.1 -- (c) 2002-2006 LAMP/EPFL

I guess by Either type, it is meant something like (maybe we should add this one to the library)
 
sealed trait Either[A,B]

case class Left[A,B](x:A) extends Either[A,B]
case class Right[A,B](x:B) extends Either[A,B]

This is a generic way to encode a type theoretic sum (coproduct). Left and Right are called "injections", they turn a value x of type S into a value of type Either[S,T] (or Either[T,S], resp). One can then later recover what the result was by matching

def distinguish(e:Either[S,T]) = e match {
  case Left(x) => // was left, x has type S
  case Right(y) => // was right, y has type T
}

It is called a *co*product, because consider that Tuple2[A,B] is a product, you need both components to construct it and once you have a product there are two projections functions, namely _1 and _2. You can draw all these in nice diagrams where a product is a coproduct with all arrows reversed. It seems like an interesting exercise to model products and sums with Arrows (capital A).

hope this helps,
Burak



--
Burak Emir
Research Assistant / PhD Candidate
Programming Methods Group
EPFL, 1015 Lausanne, Switzerland
http://lamp.epfl.ch/~emir


Custom Search
home | non blog view