Are there something like Haskell type families in Ocaml?

I was studying the possibilities of GADTs to emulate dependently typed language features. I basically found that Ocaml and Haskell have rather similar possibilities to define types indexed by types (i.e. GADTs).

The singleton types are rather equivalent as well. I.e. I can have a runtime representation of a type to make runtime decisions.

However I have found no equivalent of Haskell’s type families in Ocaml. My question: Is there a way to use singleton types to do type computation in Ocaml?

Thanks for any hint.

Good interesting question!)

In Haskell type families are type level functions - function from types to types.
In Ocaml we keep types in distinct modules: Your_module.t , Another_module.t, etc. And Ocaml has functions from modules to modules. It is called functors. Functors have enough expressive power. They can be recursive, generative or applicative, first-class…

1 Like

Haskell has a Functor typeclass. With one commonly used function, fmap; it maps functions, is the way they put it. So for example, using their Maybe data type, like OCaml’s optionMaybe is already an instance of Functor - I can “map” +1 to int option values.

fmap ((+) 1) (Just 1) --> Just 2
fmap ((+) 1) Nothing --> Nothing

Of course this is classic Haskell, while type families are an extension, and obviously don’t serve the same purpose. There’s obviously some parallel between the Haskell and OCaml Functor, though limited by the differences between typeclass and module. [edit - I see that here, for my example, the same functionality is simply supplied by OCaml ad hoc in ]

I do not yet see, how functors have the same expressive power as type families. E.g. I have tried to express the following Haskell code snippet in Ocaml and I did not succeed.

data Color :: * where
    R :: Color
    B :: Color

data Nat = Z | S Nat

type family Incr (c :: Color) (x :: Nat) :: Nat where
    Incr R x = x
    Incr B x = S x

You can find the complete Haskell code here

Can you express this little Haskell snippet in Ocaml?

OCaml does not support kinds or type families. Sometimes GADTs are enough. Typically, they are enough for Red Black trees. In fact, they should be enough for translating the code that you linked since the Incr family does not seem really useful (it is used to merge two constructors but it requires to introduce two singleton types).