Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use s-expression in OCaml utop

Tags:

ocaml

I am learning OCaml with a book titled as "Real World OCaml" and so far it works well.

I have a problem with sexp and Sexplib.

# module type M = sig
#   type t with sexp
# end;;

This is a textbook example but I have a syntax error in utop, with with word underlined. Core.Std is open.

Can anyone explain this? I doubt if they have changed language syntax.

like image 671
Yixing Liu Avatar asked Jan 05 '17 14:01

Yixing Liu


1 Answers

Replace "with sexp" with [@@deriving sexp]; this also requires the ppx_sexp_conv rewriter. Alternatively, you can also use ppx_jane, which includes all Janestreet PPX rewriters (and is automatically used when you use the corebuild command).

# #use "topfind";;
# #require "core";;
# #require "ppx_sexp_conv";;
# open Core.Std;;

# module type M = sig type t [@@deriving sexp]  end;;
module type M =
  sig
    type t
    val t_of_sexp : Sexplib.Sexp.t -> t
    val sexp_of_t : t -> Sexplib.Sexp.t
  end

Explanation :

http://blogs.janestreet.com/extension-points-or-how-ocaml-is-becoming-more-like-lisp/

like image 125
V. Michel Avatar answered Sep 20 '22 15:09

V. Michel