(* Recursive types *) (* w = mu a. a -> a SML has no mu type as such; simulate using a datatype *) datatype w = M of w -> w (* the roll operation *) fun roll x = M x (* the unroll operation *) fun unroll (M x) = x (* Define a peculiar function: fOmega : w -> w *) val fOmega = fn x => unroll(x) x ; (* fOmega simulates the untypable lambda-term (fn x => x x). Notice that (fn x => x x) (fn x => x x) (ignoring its ill-typedness) reduces to itself: (fn x => x x) (fn x => x x) |-> {fn x => x x / x} x x = (fn x => x x) (fn x => x x) |-> {fn x => x x / x} x x . . . *) (* commented out because it doesn't terminate: fOmega (roll fOmega); *) (* Factorial example: *) (* Polymorphic w *) datatype 'a w = M of 'a w -> 'a fun roll x = M x fun unroll (M x) = x val F = fn f => fn x => if x=0 then 1 else x * ((unroll f) f) (x - 1) val fact = F (roll F); fact 5; (* prints 120 *)