10 def o(r: Rec) = Cn(r.arity, this, List(r)) |
10 def o(r: Rec) = Cn(r.arity, this, List(r)) |
11 def o(r: Rec, f: Rec) = Cn(r.arity, this, List(r, f)) |
11 def o(r: Rec, f: Rec) = Cn(r.arity, this, List(r, f)) |
12 def o(r: Rec, f: Rec, g: Rec) = Cn(r.arity, this, List(r, f, g)) |
12 def o(r: Rec, f: Rec, g: Rec) = Cn(r.arity, this, List(r, f, g)) |
13 def o(r: Rec, f: Rec, g: Rec, h: Rec) = Cn(r.arity, this, List(r, f, g, h)) |
13 def o(r: Rec, f: Rec, g: Rec, h: Rec) = Cn(r.arity, this, List(r, f, g, h)) |
14 def arity : Int |
14 def arity : Int |
|
15 def size : Int |
15 } |
16 } |
16 |
17 |
17 case object Z extends Rec { |
18 case object Z extends Rec { |
18 override def eval(ns: List[Int]) = ns match { |
19 override def eval(ns: List[Int]) = ns match { |
19 case n::Nil => 0 |
20 case n::Nil => 0 |
20 case _ => throw new IllegalArgumentException("Z args: " + ns) |
21 case _ => throw new IllegalArgumentException("Z args: " + ns) |
21 } |
22 } |
22 override def arity = 1 |
23 override def arity = 1 |
|
24 override def size = 1 |
23 } |
25 } |
24 |
26 |
25 case object S extends Rec { |
27 case object S extends Rec { |
26 override def eval(ns: List[Int]) = ns match { |
28 override def eval(ns: List[Int]) = ns match { |
27 case n::Nil => n + 1 |
29 case n::Nil => n + 1 |
28 case _ => throw new IllegalArgumentException("S args: " + ns) |
30 case _ => throw new IllegalArgumentException("S args: " + ns) |
29 } |
31 } |
30 override def arity = 1 |
32 override def arity = 1 |
|
33 override def size = 1 |
31 } |
34 } |
32 |
35 |
33 case class Id(n: Int, m: Int) extends Rec { |
36 case class Id(n: Int, m: Int) extends Rec { |
34 require(m < n, println("Id m < n:" + m + " " + n)) |
37 require(m < n, println("Id m < n:" + m + " " + n)) |
35 |
38 |
36 override def eval(ns: List[Int]) = |
39 override def eval(ns: List[Int]) = |
37 if (ns.length == n && m < n) ns(m) |
40 if (ns.length == n && m < n) ns(m) |
38 else throw new IllegalArgumentException("Id args: " + ns + "," + n + "," + m) |
41 else throw new IllegalArgumentException("Id args: " + ns + "," + n + "," + m) |
39 |
42 |
40 override def arity = n |
43 override def arity = n |
|
44 override def size = 1 |
41 } |
45 } |
42 |
46 |
43 case class Cn(n: Int, f: Rec, gs: List[Rec]) extends Rec { |
47 case class Cn(n: Int, f: Rec, gs: List[Rec]) extends Rec { |
44 require(f.arity == gs.length, |
48 require(f.arity == gs.length, |
45 println("CN " + f + " " + gs.mkString(",") + "\n" + |
49 println("CN " + f + " " + gs.mkString(",") + "\n" + |
60 ) |
64 ) |
61 throw new IllegalArgumentException(msg.mkString("\n")) |
65 throw new IllegalArgumentException(msg.mkString("\n")) |
62 } |
66 } |
63 |
67 |
64 override def arity = n |
68 override def arity = n |
65 override def toString = f.toString + gs.map(_.toString).mkString ("(",", ", ")") |
69 override def toString = f.toString + " o " + gs.map(_.toString).mkString ("(",", ", ")") |
|
70 override def size = 1 + f.size + gs.map(_.size).sum |
66 } |
71 } |
67 |
72 |
68 // syntactic convenience |
73 // syntactic convenience |
69 object Cn { |
74 object Cn { |
70 def apply(n: Int, f: Rec, g: Rec) : Rec = new Cn(n, f, List(g)) |
75 def apply(n: Int, f: Rec, g: Rec) : Rec = new Cn(n, f, List(g)) |
89 throw new IllegalArgumentException(msg.mkString("\n")) |
94 throw new IllegalArgumentException(msg.mkString("\n")) |
90 } |
95 } |
91 |
96 |
92 override def arity = n + 1 |
97 override def arity = n + 1 |
93 override def toString = "Pr(" + f.toString + ", " + g.toString + ")" |
98 override def toString = "Pr(" + f.toString + ", " + g.toString + ")" |
|
99 override def size = 1 + f.size + g.size |
94 } |
100 } |
95 |
101 |
96 // syntactic convenience |
102 // syntactic convenience |
97 object Pr { |
103 object Pr { |
98 def apply(r: Rec, f: Rec) : Rec = Pr(r.arity, r, f) |
104 def apply(r: Rec, f: Rec) : Rec = Pr(r.arity, r, f) |
220 case n::Nil => triangle(n) |
228 case n::Nil => triangle(n) |
221 case _ => throw new IllegalArgumentException("Tr args: " + ns) |
229 case _ => throw new IllegalArgumentException("Tr args: " + ns) |
222 } |
230 } |
223 |
231 |
224 override def arity = 1 |
232 override def arity = 1 |
|
233 override def size = Triangle.size |
225 } |
234 } |
226 |
235 |
227 //(0 until 200).map(MaxTriangleFast.eval(_)) |
236 //(0 until 200).map(MaxTriangleFast.eval(_)) |
228 |
237 |
229 |
238 |
230 val Penc = Add o (TriangleFast o (Add o (Id(2, 0), Id(2, 1))), Id(2, 0)) |
239 val Penc = Add o (Triangle o (Add o (Id(2, 0), Id(2, 1))), Id(2, 0)) |
231 val Pdec1 = Minus o (Id(1, 0), Triangle o (MaxTriangle o (Id(1, 0)))) |
240 val Pdec1 = Minus o (Id(1, 0), Triangle o (MaxTriangle o (Id(1, 0)))) |
232 val Pdec2 = Minus o (MaxTriangle o (Id(1, 0)), Pdec1 o (Id(1, 0))) |
241 val Pdec2 = Minus o (MaxTriangle o (Id(1, 0)), Pdec1 o (Id(1, 0))) |
233 |
242 |
234 def Lenc(fs: List[Rec]) : Rec = fs match { |
243 def Lenc(fs: List[Rec]) : Rec = fs match { |
235 case Nil => Z |
244 case Nil => Z |