Problem getting ppx_sexp_conv to work

I’m trying to compile a program that uses the ppx_sexp_conv ppx extension and
I’m having some issues using it successfully. Here’s my source code:

(* *)

open Sexplib
open Sexplib.Std

type expr =
  | Int of int
  | Add of expr * expr
  | Sub of expr * expr
  | Mul of expr * expr
[@@deriving sexp]

let e1 = Add (Int 1, Sub (Mul (Int 10, Sub (Int 20, Int 2)), Int 10))

let s1 = sexp_of_expr e1

I compile it like this:

$ ocamlfind ocamlc -c -package sexplib,ppx_sexp_conv

I test it in the ocaml toplevel:

# #require "sexplib";;
# #load "test.cmo";;
# open Test;;
# s1;;
- : Ppx_sexp_conv_lib.Sexp.t = <abstr>
# expr_of_sexp s1;;
- : Test.expr = Add (Int 1, Sub (Mul (Int 10, Sub (Int 20, Int 2)), Int 10))

So far, so good. But notice that the Sexp.t type is abstract. So when I do this:

# open Sexplib;;
# Sexp.to_string s1;;

I get the error message:

“Ppx_sexp_conv_lib.Sexp.t is abstract because no corresponding cmi file was found in path.”

This is a real problem, because I can’t use any of the sexp functions in Sexplib
to manipulate the S-expressions (for instance, to save them to a file).
Obviously, I’m missing something basic here. A very ugly workaround is this:

let convert s : Sexp.t = Obj.magic s
let s2 = convert s1

This works, but I would rather not go there :slight_smile: What is the correct way to
compile/use this so that this doesn’t happen? BTW I don’t want to use dune (at
least, not exclusively and not yet) because I want to actually understand how to
do it at a low level.

Thanks in advance,


Follow-up: I took a few minutes to learn the basics of dune, and it worked right away. It does look like dune or something like it is pretty essential when using ppx’s. I appreciate that dune outputs a log file of all its actions for those of us who want to know exactly what is happening.

1 Like