alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (unappgadteval.scala)

This example Scala source code file (unappgadteval.scala) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Scala tags/keywords

app, app, array, env, int, int, lam, nil, num, suc, term, term, var, var

The Scala unappgadteval.scala source code

/** Cleaned up in october 2010 by paulp.
 *  Hey, we should get this working.
 */

// Class hierarchy
trait Term[a]

object Var{ def unapply[a](x:Var[a]) = Some(x.name) }
class Var[a] (val name : String) extends Term[a]

object Num{ def unapply(x:Num) = Some(x.value) }
class Num (val value : Int) extends Term[Int]

object Lam{ def unapply[b,c](l: Lam[b,c]) = Some(l.x, l.e) }
class Lam[b, c](val x : Var[b], val e : Term[c]) extends Term[b => c]

object App{ def unapply[b,c](a: App[b,c]) = Some(a.f, a.e) }
class App[b, c] (val f : Term[b => c], val e : Term[b]) extends Term[c]

object Suc { def unapply(a: Suc) = true }
class Suc() extends Term[Int => Int]

// Environments :
abstract class Env { 
  def apply[a](v: Var[a]): a
  def extend[a](v: Var[a], x : a) = new Env {
    def apply[b](w: Var[b]): b = w match { 
      case _ : v.type => x // v eq w, hence a = b
      case _ => Env.this.apply(w)
    }}
}

object empty extends Env { 
  def apply[a](x: Var[a]): a = throw new Error("not found : "+x.name) 
}

object Test {
  val v1 = new Var[util.Random]("random")
  val v2 = new Var[Int]("Int")
  val v3 = new Var[List[String]]("list")
  
  val anEnv = (empty
    .extend(v1, new util.Random)
    .extend(v2, 58)
    .extend(v3, Nil)
  )
  
  def eval[a](t: Term[a], env : Env): a = t match {
    // First three work
    case v : Var[b]         => env(v)                     // a = b
    case n @ Num(value)     => value                      // a = Int
    case a @ App(f,e)       => eval(f, env)(eval(e, env)) // a = c

    // Next one fails like:
    //
    // found   : (Int) => Int
    // required: a    
    case i @ Suc()          => { (y: Int) => y + 1 }      // a = Int => Int
    
    // Next one fails like:
    //
    // error: '=>' expected but '[' found.
    //     case f @ Lam[b,c](x, e) => { (y: b) => eval(e, env.extend(x, y)) }  // a = b=>c
    //                 ^
    case f @ Lam[b,c](x, e) => { (y: b) => eval(e, env.extend(x, y)) }  // a = b=>c
  }
  
  val f1 = () => eval(v1, anEnv)
  val f2 = () => eval(v2, anEnv)
  val f3 = () => eval(v3, anEnv)
  
  def main(args: Array[String]): Unit = {
    println(f1())
    println(f2())
    println(f3())
  }
}

Other Scala examples (source code examples)

Here is a short list of links related to this Scala unappgadteval.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.