Hello,
I have started to use the show
PPX extension available from https://github.com/thierry-martinez/ppx_show and found it very useful.
But i’m now facing facing a problem when trying to use it in coordination with functors. I’ll try to illustrate it using a small example (which actually is a heavily trimmed down version of the code i’m working on).
Suppose i want to manipulate expressions in which constants can be of any type (not just int
). I can do this by writing
(* file expr.mli *)
module type T = sig
type value
type t = Const of value | Var of string
val zero: t
end
module type VALUE = sig
type t
val from_int: int -> t
end
module Make (V: VALUE) : T with type value = V.t
(* file expr.ml *)
module type T = sig
type value
type t = Const of value | Var of string
val zero: t
end
module type VALUE = sig
type t
val from_int: int -> t
end
module Make (V: VALUE) = struct
type value = V.t
type t = Const of value | Var of string
let zero = Const (V.from_int 0)
end
So far, so good. In particular, i can write (in file main.ml
for ex):
module Int = struct
type t = int
let from_int x = x
end
module IntExpr = Expr.Make(Int)
let v = IntExpr.zero
Now, suppose i want to add a show
function to module Expr
to display (as strings) values of type Expr.t
. I suppose i have to rewrite the definition of type t
in the Make
functor as
type t = Const of value | Var of string [@@deriving show]
It seems (?) i also have to add the PPX annotation in T
signature (in expr.ml
and expr.mli
).
Doing this, i get the following error message:
File ".../expr.ml", line 1:
Error: Unbound value pp_value
Seems normal: the derived show
function for values of type T.t
needs to know how to show values of type Value.t
.
So, i added this line to the VALUE
signature in files expr.{ml,mli}
:
val pp_value: t -> string
and this line to the definition of the Int
module in main.ml
:
let pp_value = string_of_int
but i still get the error:
File ".../expr.ml", line 1:
Error: Unbound value pp_value
I suspect this has to do with the fact that the PPX rewritter does not have the correct function in scope in the context of functor application. I tried to inspect the ppx-expansed code (*.pp.ml
generated by dune
in _build
directory) but unfortunately it is not in human readable form
Any help on this subject would be appreciated
Jocelyn