Cannot use "with sexp"

175 views
Skip to first unread message

Stephen Bastians

unread,
Jul 8, 2016, 11:05:18 AM7/8/16
to ocaml-core
I'm an OCaml noob, so please bear with me.

In utop, this expression from the Real World OCaml book fails.

utop # type t = { foo: int; bar: float } with sexp ;;
Error: Parse error: [str_item] or ";;" expected (in [top_phrase])

If I try compiling a .ml file, I just get a syntax error.

I'm on 4.03.0 OCaml, on Ubuntu 16.04. I've installed core (version 113.33.02+4.03).



Jeremie Dimino

unread,
Jul 8, 2016, 11:15:03 AM7/8/16
to ocaml...@googlegroups.com
Hi Stephen,

`with sexp` is provided by pa_sexp_conv, so you need to install pa_sexp_conv with opam and then type the following in utop to enable the syntax:

utop # #require "pa_sexp_conv";;

However, note that pa_sexp_conv is deprecated and has been replaced by ppx_sexp_conv:

utop # require "ppx_sexp_conv";;
utop # open Sexplib.Std;;
utop # type t = int [@@deriving sexp];;
type t = int
val t_of_sexp : Sexplib.Type.t -> t = <fun>
val sexp_of_t : t -> Sexplib.Type.t = <fun>


--
You received this message because you are subscribed to the Google Groups "ocaml-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ocaml-core+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Jeremie

Stephen Bastians

unread,
Jul 8, 2016, 9:11:03 PM7/8/16
to ocaml-core
Thanks for your reply, Jeremie.

The first, deprecated method works, but the call to require using the new ppx crashes utop - here is the entire output:

────────┬──────────────────────────────────────────────────────────────┬────────
        │ Welcome to utop version 1.19.2 (using OCaml version 4.03.0)! │        
        └──────────────────────────────────────────────────────────────┘        
Findlib has been successfully loaded. Additional directives:
  #require "package";;      to load a package
  #list;;                   to list the available packages
  #camlp4o;;                to load camlp4 (standard syntax)
  #camlp4r;;                to load camlp4 (revised syntax)
  #predicates "p,q,...";;   to set these predicates
  Topfind.reset();;         to force that packages will be reloaded
  #thread;;                 to enable threads


Type #utop_help for help about using utop.

─( 09:01:04 )─< command 0 >──────────────────────────────────────{ counter: 0 }─
utop # #require "ppx_sexp_conv";;
File "", line 1, characters 0-4:                                                Error:Parse error: illegal begin of top_phrase                                  Fatal error: exception Exit                                                     
Raised at file "bytes.ml", line 219, characters 25-34
Called from file "src/pre_sexp.ml", line 86, characters 11-45

Ashish Agarwal

unread,
Jul 9, 2016, 9:00:33 AM7/9/16
to ocaml...@googlegroups.com
Stephen, I've hit your error sometimes too. PPX extensions can be fickle. The order in which you load them and the number of times you load them can cause different behavior. Try exactly the following:

$ utop -init ""
utop # #require "ppx_deriving";;
utop # #require "ppx_sexp_conv";;
utop # #require "sexplib";;
utop # open Sexplib.Std;;
utop # type t = int [@@deriving sexp];;

If you want to use Core, then you'll also be doing `#require "core"` and `open Core.Std`. In this case, you can disregard the `#require sexplib` and `open  Sexplib.Std` since Core does the equivalent. Let us know if that works.


Stephen Bastians

unread,
Jul 11, 2016, 12:15:03 AM7/11/16
to ocaml-core
Thanks, Ashish, your loading order does work.

I also followed the instructions in https://janestreet.github.io/installation.html, that also works.



 

Ashish Agarwal

unread,
Jul 11, 2016, 8:49:43 AM7/11/16
to ocaml...@googlegroups.com
Great. I haven't tried ppx_jane yet due to its version constraints, but hopefully that makes the experience easier.

On Mon, Jul 11, 2016 at 12:15 AM, Stephen Bastians <stev...@gmail.com> wrote:
Thanks, Ashish, your loading order does work.

I also followed the instructions in https://janestreet.github.io/installation.html, that also works.



 

--
Reply all
Reply to author
Forward
0 new messages