2 // This is a simple (and useless) example for testing the hash cons feature
7 datatype EXP :: unique = num (int)
12 and LIST :: unique = nil
17 // implement the datatypes.
19 instantiate datatype EXP, LIST;
21 ostream& operator << (ostream& f, EXP e)
24 num n: { return f << n; }
25 | add(x,y): { return f << '(' << x << " + " << y << ')'; }
26 | sub(x,y): { return f << '(' << x << " - " << y << ')'; }
27 | mul(x,y): { return f << '(' << x << " * " << y << ')'; }
28 | div(x,y): { return f << '(' << x << " / " << y << ')'; }
32 ostream& operator << (ostream& f, LIST l)
36 case list(e,nil): return f << e;
37 case list(e,t): return f << e << ',' << t;
43 EXP e1 = mul(add(num(1),num(2)), add(num(2), num(3)));
44 EXP e2 = mul(add(num(1),num(2)), add(num(2), num(3)));
45 LIST l1 = list(e1, list(e2, nil));
47 cout << ((e1 == e2) ? "Good!\n" : "Bad boy! Naughty, naughty boy!\n");
48 cout << ((num(1) == num(1)) ? "Good!\n" : "Bad boy! Naughty, naughty boy!\n");
49 cout << ((num(1) != num(2)) ? "Good!\n" : "Bad boy! Naughty, naughty boy!\n");
50 cout << ((add(e1,e2) == add(e1,e2)) ? "Good!\n" : "Bad boy! Naughty, naughty boy!\n");
51 cout << ((add(e1,e2) != add(e1,num(2))) ? "Good!\n" : "Bad boy! Naughty, naughty boy!\n");