I'm trying to implement a binary tree in which every node can hold information of type 'a or type 'b. The simple solution is to use 2 constructors like this:
datatype ('a, 'b) Tree = Lf
| Br1 of 'a * (('a, 'b) Tree) * (('a, 'b) Tree)
| Br2 of 'b * (('a, 'b) Tree) * (('a, 'b) Tree);
Br1(100,Lf,Br2("hello",Lf,Lf));
>val it = Br1 (100, Lf, Br2 ("hello", Lf, Lf)): (int, string) Tree;
However, i'd like to use 1 constructor, so that the result would be the following:
Br(100,Lf,Br("hello",Lf,Lf));
>val it = Br (100, Lf, Br ("hello", Lf, Lf)): (int, string) Tree;
Pattern matching doesn't seem to work, it returns a long type clash error upon calling Br:
datatype ('a, 'b) Tree = Lf
| Br of 'a * (('a, 'b) Tree) * (('a, 'b) Tree)
| Br of 'b * (('a, 'b) Tree) * (('a, 'b) Tree);
I had a feeling it had something to do with a union datatype, so i tried the following, but when i try to call Br like this, it gives an error:
local
datatype ('a,'b) u = t1 of 'a
| t2 of 'b;
in
datatype ('a, 'b) Tree = Lf
| Br of ('a,'b) u * (('a, 'b) Tree) * (('a, 'b) Tree);
end;
Br(100,Lf,Br("hello",Lf,Lf));
Elaboration failed: Unbound type "u".
Maybe the syntax is incorrect, or my idea is wrong?