[MLton-user] datatypes, opaque ascription and case ... of

Raymond Racine rracine@adelphia.net
Sun, 24 Oct 2004 20:25:47 -0400


On Sun, 2004-10-24 at 15:18, Tom 7 wrote:
> If I understand you correctly, I think a cleaner solution would look like 
> this:
> 
> signature RETE_NODE =
> sig
> 
>     type rete_beta_data
>     type rete_join_data
>     type rete_cc_data
>     ...
> 
>     datatype rete_node =
>          TBetaNode of rete_beta_data
>        | TJoinNode of rete_join_data
>        | TNCCNode  of rete_cc_data
>        | ...
> end

I did go down this road early on when I was starting both to code this
project and learn SML when I hit the following issues and concluded
(erroneously) that the cyclic types prevented an acceptable solution.  

At that time I tended to avoid the capricious use of datatypes as I some
how believed they involved lots of type bits, boxing and pointers and I
was concerned about Rete performance.

However, now that you pointed it out again I can see that a liberal
usage of datatypes solves all.  See Cycle4.

signature CYCLE =
sig

    type t1
    type t2
    type t3

    datatype t = T1 of t1 | T2 of t2 | T3 of t3

end

(* Doesn't work *)
structure Cycle1: CYCLE =
struct
 
  type t1 = {t2: t2, t3: t3}
  and t2 =  {t1: t1, t3: t3}
  and t3 =  {t1: t1, t2: t2}	   
 	    
  datatype t = T1 of t1 | T2 of t2 | T3 of t3
 			   
end 

(* Doesn't work *)
structure Cycle2: CYCLE =
struct
  	    
  datatype t = T1 of t1 | T2 of t2 | T3 of t3
  withtype t1 = {t2: t2, t3: t3}
  and t2 = {t1: t1, t3: t3}
  and t3 = {t1: t1, t2: t2}
				   
end 

(* Works for SML/NJ as withtype is a let* (nonstandard extension)
 * Fails for MLton where withtype is a let (spec compliant)
 * However is not a general solution for cyclic types *)
structure Cycle3: CYCLE =
struct
  	    
  datatype t = T1 of t1 | T2 of t2 | T3 of t3
  withtype t1 = {t1: int}
  and t2 = {t1: t1, t2: int}
  and t3 = {t1: t1, t2: t2, t3: int}
				   
end

(* Previously gave up here *)

(* But now I see ... *)
(*!!!! Works !!!!*)
structure Cycle4: CYCLE =
struct
 
  datatype t1 = T1 of {t2: t2, t3: t3}
  and t2 =  T2 of {t1: t1, t3: t3}
  and t3 =  T3 of {t1: t1, t2: t2}	   
 		  
  datatype t = TT1 of t1 | TT2 of t2 | TT3 of t3
 			   
end 

Thanks 

Ray