scala/recs2.scala
changeset 271 4457185b22ef
parent 270 ccec33db31d4
--- a/scala/recs2.scala	Wed Jun 26 14:42:42 2013 +0100
+++ b/scala/recs2.scala	Wed Jul 17 10:33:19 2013 +0100
@@ -12,6 +12,7 @@
   def o(r: Rec, f: Rec, g: Rec) = Cn(r.arity, this, List(r, f, g))
   def o(r: Rec, f: Rec, g: Rec, h: Rec) = Cn(r.arity, this, List(r, f, g, h))
   def arity : Int
+  def size : Int
 }
 
 case object Z extends Rec {
@@ -20,6 +21,7 @@
     case _ => throw new IllegalArgumentException("Z args: " + ns)
   }
   override def arity = 1
+  override def size = 1
 } 
 
 case object S extends Rec {
@@ -27,7 +29,8 @@
     case n::Nil => n + 1
     case _ => throw new IllegalArgumentException("S args: " + ns)
   }
-  override def arity = 1 
+  override def arity = 1
+  override def size = 1
 } 
 
 case class Id(n: Int, m: Int) extends Rec {
@@ -38,6 +41,7 @@
     else throw new IllegalArgumentException("Id args: " + ns + "," + n + "," + m)
 
   override def arity = n
+  override def size = 1
 }
 
 case class Cn(n: Int, f: Rec, gs: List[Rec]) extends Rec {
@@ -62,7 +66,8 @@
     }
 
   override def arity = n
-  override def toString = f.toString + gs.map(_.toString).mkString ("(",", ", ")")
+  override def toString = f.toString + " o " + gs.map(_.toString).mkString ("(",", ", ")")
+  override def size = 1 + f.size + gs.map(_.size).sum
 }
 
 // syntactic convenience
@@ -91,6 +96,7 @@
 
   override def arity = n + 1
   override def toString = "Pr(" + f.toString + ", " + g.toString + ")"
+  override def size = 1 + f.size + g.size
 }
 
 // syntactic convenience
@@ -107,6 +113,7 @@
     else throw new IllegalArgumentException("Mn: args")
 
   override def arity = n
+  override def size = 1 + f.size
 }
 
 object Mn {
@@ -211,6 +218,7 @@
   }
 
   override def arity = 1
+  override def size = MaxTriangle.size
 }
 
 case object TriangleFast extends Rec {
@@ -222,12 +230,13 @@
   }
 
   override def arity = 1
+  override def size = Triangle.size
 }
 
 //(0 until 200).map(MaxTriangleFast.eval(_))
 
 
-val Penc = Add o (TriangleFast o (Add o (Id(2, 0), Id(2, 1))), Id(2, 0))
+val Penc = Add o (Triangle o (Add o (Id(2, 0), Id(2, 1))), Id(2, 0))
 val Pdec1 = Minus o (Id(1, 0), Triangle o (MaxTriangle o (Id(1, 0)))) 
 val Pdec2 = Minus o (MaxTriangle o (Id(1, 0)), Pdec1 o (Id(1, 0)))