Ppxlib: Getting the original definition of `typ_constr` like `type_declaration` from `core_type` of `ptyp_constr`

Just to hopefully help clear things up, here are a few points.

expr_of_type takes a core_type and derives an expression. That expression is the necessary function to convert core_type into a string. In the simplest case it will be a function core_type -> string. As you rightly pointed out in the case of int we get string_of_int

Some types are more complicated than just a primitive like int and they take “arguments”. We already saw one with int list. The ppx treats lists specially and captures them, but what if we tried a different type constructor that takes an argument?

type int_option = int option [@@deriving stringify] 
(* Unbound value option_stringify *)

We get this because of the new case we added to handle type constructors, but what type should option_stringify have? It needs to be able to handle any type we might give it. To allow this option_stringify must be a higher-order function:

let option_stringify convert = function
  | Some value -> "Some " ^ convert value
  | None -> "None"

With convert we could pass in a function for converting different values inside the option. For ints we could use string_of_int, for floats we could us string_of_float etc. Altogether our code now looks like:

let option_stringify convert = function
  | Some value -> "Some " ^ convert value
  | None -> "None"

type int_option = int option [@@deriving stringify] 

let () =
  let v = Some 10 in 
  print_endline (int_option_stringify v)

And if we look at what our ppx generated and specifically where the eapply was used (using dune describe pp example.ml):

let int_option_stringify x = 
  (* here is our eapply part:
      - option_stringify is the result of Exp.ident part
      - string_of_int is the result of recursively calling `expr_of_type` on the arguments 
        to this type (in this case there is only one, namely, `int`) *) 
  (option_stringify string_of_int) x

Hopefully that helps a little more. Also in terms of documentation, I couldn’t agree more. I’ll look into this shortly (I help maintain ppxlib) but please do open issues or continue to comment on the large documentation tracking issue. More examples using Ast_builder etc. would be great.

1 Like