views:

210

answers:

1

Inspired by this question:

Is explicit type recursion possible in F#?

type 'a Mu = In of 'a Mu 'a

let unIn (In x) = x

This code unfortunatly gives "Type parameter cannot be used as type constructor.

Remarks: This construct is used in the paper Functional Programming with Overloading and Higher-Order Polymorphism, for example.

Example of usage (taken from here):

type ('a, 'b) ListX =
    | Nil
    | Cons of 'a * 'b

type 'a List = ListX Mu
+8  A: 

No, this is not possible. Specifically, generics in F# have the same limitation as the CLR, namely a <T> or an <'a> must have kind " * ". This same limitation is what means you cannot author "type classes" directly in F#, since e.g. "Monad m" would take a higher-kinded argument 'm' (e.g. "* -> *", where e.g. 'list' and 'option' could be instances, those each themselves being generic type constructors), but this is not allowed.

Brian
Haskell type classes work on things of kind `*` too, and in fact classes on higher-kinded types were originally called 'constructor classes', though the distinction is mostly ignored now.
Ganesh Sittampalam