ExempleHumain.t 7.2 KB

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