ExamplePeano.t 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. import tom.library.sl.*;
  2. public class ExamplePeano{
  3. %include { sl.tom }
  4. //-------- Java classes -----------
  5. static class JNat { }
  6. static class JNatList { }
  7. static class Jzero extends JNat {
  8. public Jzero() {}
  9. public boolean equals(Object o) {
  10. if(o instanceof Jzero) {
  11. return true;
  12. }
  13. return false;
  14. }
  15. }
  16. static class Jsuc extends JNat {
  17. public JNat n;
  18. public Jsuc() { }
  19. public Jsuc(JNat n) { this.n = n; }
  20. public boolean equals(Object o) {
  21. if(o instanceof Jsuc) {
  22. Jsuc obj = (Jsuc) o;
  23. return n.equals(obj.n);
  24. }
  25. return false;
  26. }
  27. }
  28. static class Jplus extends JNat {
  29. public JNat n1;
  30. public JNat n2;
  31. public Jplus() { }
  32. public Jplus(JNat n1, JNat n2) {
  33. this.n1 = n1;
  34. this.n2 = n2;
  35. }
  36. public boolean equals(Object o) {
  37. if(o instanceof Jplus) {
  38. Jplus obj = (Jplus) o;
  39. return n1.equals(obj.n1) && n2.equals(obj.n2);
  40. }
  41. return false;
  42. }
  43. }
  44. static class JconcJNat extends JNatList {
  45. public JNat head;
  46. public JNatList tail;
  47. public JconcJNat() { head = null; tail = null; }
  48. public JconcJNat(JNat h, JNatList ntail) {
  49. head = h;
  50. tail = ntail;
  51. }
  52. public boolean isEmpty() {
  53. return (head == null && tail == null);
  54. }
  55. public boolean equals(Object o) {
  56. if (o instanceof JconcJNat) {
  57. JconcJNat obj = (JconcJNat) o;
  58. if (this.isEmpty() && obj.isEmpty()) {
  59. return true;
  60. } else if (!this.isEmpty() && !obj.isEmpty()) {
  61. return
  62. head.equals(obj.head) && tail.equals(obj.tail);
  63. }
  64. }
  65. return false;
  66. }
  67. }
  68. //-------- Tom mappings -----------
  69. %typeterm Nat {
  70. implement { JNat }
  71. is_sort(s) { (s instanceof JNat) }
  72. equals(t1,t2) { (t1.equals(t2)) }
  73. }
  74. %typeterm NatList {
  75. implement { JNatList }
  76. is_sort(s) { (s instanceof JNatList) }
  77. equals(t1,t2) { (t1.equals(t2)) }
  78. }
  79. %op Nat zero() {
  80. is_fsym(s) { (s instanceof Jzero) }
  81. make() { new Jzero() }
  82. }
  83. %op Nat suc(n:Nat) {
  84. is_fsym(s) { (s instanceof Jsuc) }
  85. get_slot(n,s) { ((Jsuc)s).n }
  86. make(t0) { new Jsuc(t0) }
  87. }
  88. %op Nat plus(n1:Nat,n2:Nat) {
  89. is_fsym(s) { (s instanceof Jplus) }
  90. get_slot(n1,s) { ((Jplus)s).n1 }
  91. get_slot(n2,s) { ((Jplus)s).n2 }
  92. make(t0,t1) { new Jplus(t0,t1) }
  93. }
  94. %oplist NatList concNat(Nat*) {
  95. is_fsym(s) { (s instanceof JconcJNat) }
  96. get_head(l) { ((JconcJNat)l).head }
  97. get_tail(l) { ((JconcJNat)l).tail }
  98. is_empty(l) { ((JconcJNat)l).isEmpty() }
  99. make_empty() { new JconcJNat() }
  100. make_insert(t,l) { new JconcJNat(t,l) }
  101. }
  102. //---------------------------------
  103. public static void main(String[] args) {
  104. ExamplePeano exPeano = new ExamplePeano();
  105. exPeano.run();
  106. }
  107. public void run() {
  108. JNat one = `suc(zero());
  109. JNat zero = `zero();
  110. JNat result = peanoPlus(one,zero);
  111. System.out.print("one + zero = " + printJNat(result) + "\n");
  112. JNatList nList = `concNat(zero(),suc(zero()),suc(suc(zero())));
  113. printSolutions(nList);
  114. try {
  115. JNat number = `plus(one,zero);
  116. Strategy addition = `addition();
  117. JNat reducedNumber1 = addition.visit(number, new LocalIntrospector());
  118. System.out.println("By addition: Term '" + printJNat(number) + "' reduces to '" +
  119. printJNat(reducedNumber1) +"'.");
  120. Strategy recursiveAddition = `TopDown(addition());
  121. JNat reducedNumber2 = recursiveAddition.visit(number, new LocalIntrospector());
  122. System.out.println("By recursiveAddition: Term '" + printJNat(number) + "' reduces to '" +
  123. printJNat(reducedNumber2) +"'.");
  124. } catch (VisitFailure e) {
  125. System.out.println("strategy failed");
  126. }
  127. int total = variadicPlus(nList);
  128. System.out.println("Total = " + total);
  129. printPositiveIntegers(nList);
  130. }
  131. public JNat peanoPlus(JNat t1, JNat t2) {
  132. %match(Nat t1, Nat t2) {
  133. zero(), x -> { return `x; }
  134. suc(y), x -> { return `suc(peanoPlus(y,x)); }
  135. }
  136. return null;
  137. }
  138. public int variadicPlus(JNatList nList) {
  139. %match {
  140. !concNat(x*,suc(y),z*) << nList || concNat() << nList -> {
  141. return 0;
  142. }
  143. concNat(x*,suc(y),z*) << nList -> {
  144. return variadicPlus(`concNat(y,x*,z*)) + 1;
  145. }
  146. }
  147. return -1;
  148. }
  149. public void printPositiveIntegers(JNatList nList) {
  150. int counter = 0;
  151. %match {
  152. concNat(x*,pnat@suc(y),z*) << NatList nList -> {
  153. counter++;
  154. System.out.println("Positive integer #" + counter + " = " + `pnat);
  155. }
  156. }
  157. }
  158. public String printFirstElement(JNat t) {
  159. %match {
  160. plus[n1=x] << t -> { return printJNat(`x); }
  161. }
  162. return "";
  163. }
  164. %strategy addition() extends Identity() {
  165. visit Nat {
  166. plus(zero(), x) -> { return `x; }
  167. plus(suc(y), x) -> { return `suc(plus(y,x)); }
  168. }
  169. }
  170. public void printSolutions(JNatList nList) {
  171. %match(nList) {
  172. concNat(x*,y*) -> {
  173. System.out.print("x = " + printJNatList(`x) + "\t\t");
  174. System.out.println("y = " + printJNatList(`y));
  175. }
  176. }
  177. }
  178. public String printJNat(JNat n) {
  179. %match(n) {
  180. zero() -> { return "0"; }
  181. suc(x) -> { return ("suc(" + printJNat(`x) + ")"); }
  182. plus(x1,x2) -> { return ("plus(" + printJNat(`x1) + "," + printJNat(`x2) + ")"); }
  183. }
  184. return "";
  185. }
  186. public String printJNatList(JNatList nList) {
  187. String result = "(";
  188. %match(nList) {
  189. concNat(x) -> { return (result + printJNat(`x) + ")"); }
  190. concNat(head,tail*) -> { result += (printJNat(`head) + "," + printJNatList(`tail)); }
  191. }
  192. // CASE concNat()
  193. return (result += ")");
  194. }
  195. }
  196. /* Corresponding java code generated by tom
  197. public JNat peanoPlus(JNat t1, JNat t2) {
  198. if (t1 instanceof JNat) {
  199. if (t1 instanceof Jzero) {
  200. if (t2 instanceof JNat) { return ((JNat ) t2); }
  201. }
  202. }
  203. if (t1 instanceof JNat) {
  204. if (t1 instanceof Jsuc) {
  205. if (t2 instanceof JNat) {
  206. return new Jsuc(peanoPlus(((Jsuc)t1).n,(( JNat ) t2)));
  207. }
  208. }
  209. }
  210. return null;
  211. }
  212. */