I have a functor that takes a Set
type like:
module type MySet = functor (S : Set.S) -> sig
val my_method : S.t -> S.elt -> S.elt list option
end
module MySet_Make : MySet = functor (S : Set.S) -> struct
let my_method set el = Some [el] (* whatever *)
end
module IntSet = Set.Make(Int)
module MyIntSet = MySet_Make(IntSet)
S.elt
is the type of elements of the set
I want to apply [@@deriving show]
(from https://github.com/ocaml-ppx/ppx_deriving#plugin-show) to S.elt
within my functor somehow, so that in one of my methods I can rely on having a show : S.elt -> string
function available.
I feel like it must be possible but I can't work out the right syntax.
Alternatively - if there's a way to specify in the signature that the Set type S
was made having elements of a "showable" type.
e.g. I can define:
module type Showable = sig
type t [@@deriving show]
end
...but I can't work out how to specify that as a type constraint to elements of (S : Set.S)
Ok, after a couple of hours more fumbling around in the dark I found a recipe that does everything I wanted...
First we define a "showable" type, representing a module type that has had
[@@deriving show]
(from https://github.com/ocaml-ppx/ppx_deriving#plugin-show) applied to it:(I don't know if there's some way to get this directly from
ppx_deriving.show
without defining it manually?)Then we re-define and extend the
Set
andSet.OrderedType
(i.e. element) types to require that the elements are "showable":I think with the original code in my question I had got confused and used some kind of higher-order functor syntax (?) ...I don't know how it seemed to work at all, but at some point I realised my
MySet_Make
was returning a functor rather than a module. So we'll fix that now and just use a normal functor.The other thing we can fix is to make
MySet
a further extension ofShowableSet
... soMySet_Make
will take the element type as a parameter instead of another Set type. This makes the eventual code all simpler too:Then we just need an
OrderedShowable
version ofInt
as the element type.Int
is already ordered so we just have to extend it by deriving "show" and then we can make a concreteMySet
:And we can use it like: