It is well-known that OCaml has a parametric polymorphism and this leads to some limitations. Haskell, through its type classes, offers an ad hoc polymorphism, which is, obviously, very convenient in several situations. It is also well-known that the system of modules and functors of OCaml allows to create a kind of ad hoc polymorphism. See the great recent answer of Simon Shine here for instance.
My point is that it is possible in Haskell to create types that derive several types classes. For instance :
data Person = Person { firstName :: String
, lastName :: String
, age :: Int
} deriving (Eq, Show, Read)
This is very convenient to define types having several features (allowing values of type Person
to support equality tests, be printable, and be readable in the given example).
My question is the following: Can we do the same, simply, in OCaml? By simply I mean with the ground syntax of the language and without many artifices.
To give a somewhat concrete example, suppose we have two OCaml signatures
module type Showable = sig
type t
val to_string : t -> string
end
module type Readable = sig
type t
val from_string : string -> t
end
The aim is to write a functor F
parametrized by a module which implements both Showable
and Readable
.