Advertisement
Guest User

Untitled

a guest
Mar 24th, 2014
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 3.47 KB | None | 0 0
  1. import leon.lang._
  2. import leon.annotation._
  3. import leon.collection._
  4. import leon._
  5.  
  6. object Lang {
  7.   case class Id(v: Int)
  8.  
  9.   def isLiteral(l: Expr) = l match {
  10.     case _: Var => true
  11.     case _: Abs => true
  12.     //case _: Record => true
  13.     case _ => false
  14.   }
  15.  
  16.   abstract class Expr
  17.   case class Var(id: Id) extends Expr
  18.   case class App(f: Expr, arg: Expr) extends Expr
  19.   case class Abs(b: Id, tpe: Type, e: Expr) extends Expr
  20.   //case class Record(fields: Map[Id, Expr]) extends Expr
  21.   //case class Select(r: Expr, f: Var) extends Expr
  22.  
  23.   abstract class Type
  24.   case class Param(k : Int) extends Type
  25.   case class TFunction(from: Type, to: Type) extends Type
  26.   //case class TRecord(fields: Map[Id, Type]) extends Type
  27. }
  28.  
  29.  
  30.  
  31. object TypeChecker {
  32.   import Lang._
  33.  
  34.   def typeOf(e: Expr, exp: Option[Type], env: Map[Id, Type]): Option[Type] = {
  35.     val t: Option[Type] = e match {
  36.       case Var(id) =>
  37.         if (env contains id) {
  38.           Some(env(id))
  39.         } else {
  40.           None()
  41.         }
  42.  
  43.       case App(f, arg) =>
  44.         typeOf(f, None(), env) match {
  45.           case Some(TFunction(from, to)) =>
  46.             typeOf(arg, Some(from), env) match {
  47.               case Some(_) => Some(to)
  48.               case _ => None()
  49.             }
  50.           case _ => None()
  51.         }
  52.       case Abs(id, tpe, e) =>
  53.         val nenv = env.updated(id, tpe)
  54.         (exp, typeOf(e, None(), nenv)) match {
  55.           case (Some(TFunction(from, to)), Some(to2)) if (from == tpe) && (to == to2) =>
  56.             Some(TFunction(tpe, to2))
  57.           case (None(), Some(to2)) =>
  58.             Some(TFunction(tpe, to2))
  59.           case _ =>
  60.             None()
  61.         }
  62.     }
  63.  
  64.     if(exp.orElse(t) == t) {
  65.       t
  66.     } else {
  67.       None()
  68.     }
  69.   }
  70.  
  71.   def typeChecks(e: Expr): Boolean = {
  72.     typeOf(e, None(), Map[Id, Type]()).isDefined
  73.   }
  74.  
  75.   def typeOf(e: Expr): Option[Type] = {
  76.     typeOf(e, None(), Map[Id, Type]())
  77.   }
  78. }
  79.  
  80. object Evaluator {
  81.   import Lang._
  82.   import TypeChecker._
  83.  
  84.   def subst(e: Expr, t: (Id, Expr)): Expr = e match {
  85.     case Var(id) if id == t._1 => t._2
  86.     case App(f, arg) => App(subst(f, t), subst(arg, t))
  87.     case Abs(b, id, e) => Abs(b, id, subst(e, t))
  88.     case _ => e
  89.   }
  90.  
  91.   def eval(e: Expr): Expr = {
  92.     require(typeChecks(e))
  93.     e match {
  94.     case App(f, arg) =>
  95.       eval(f) match {
  96.         case Abs(id, _, e) => eval(subst(e, id -> arg))
  97.         case _ => e // stuck
  98.       }
  99.     case _ =>
  100.       e
  101.   }} ensuring { res => isLiteral(res) }
  102. }
  103.  
  104. object Main {
  105.   import Lang._
  106.   import Evaluator._
  107.   import TypeChecker._
  108.  
  109.   def synthesize_identity() : Expr = {
  110.     choose { (e : Expr) =>
  111.       val t_identity : Type = TFunction(Param(1), Param(1))
  112.       typeOf(e) == Some(t_identity)
  113.     }
  114.   }
  115.  
  116.   def synthesize_K() : Expr = {
  117.     choose { (e : Expr) =>
  118.       val t1 : Type = TFunction(Param(1), TFunction(Param(2), Param(1)))
  119.       typeOf(e) == Some(t1)
  120.     }
  121.   }
  122.   def synthesize_K2() : Expr = {
  123.     choose { (e : Expr) =>
  124.       val t1 : Type = TFunction(Param(1), TFunction(Param(2), Param(2)))
  125.       typeOf(e) == Some(t1)
  126.     }
  127.   }
  128.  
  129.  
  130.   def synthesize_S() : Expr = {
  131.     choose { (e : Expr) =>
  132.       val tz = Param(1)
  133.       val ty = TFunction(Param(1), Param(2))
  134.       val tx = TFunction(Param(1), TFunction(Param(2), Param(3)))
  135.       val t1 : Type = TFunction(tx,
  136.                         TFunction(ty,
  137.                           TFunction(tz, Param(3))))
  138.       typeOf(e) == Some(t1)
  139.     }
  140.   }
  141.  
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement