OCaml toplevel with syntax extensions
Asked Answered
E

1

14

I don't know how to accomplish this in general, but I'll ask about one instance in particular for clarity:

Sexplib looks interesting to me. I want to play around with it. I've downloaded it, installed it just fine (I'm pretty sure, anyway), etc. I want to use the "with sexp" syntax extension in a toplevel. How would I go about doing this? All the examples I've found of its use assume you already know how to make the toplevel and/or compile with the syntax extensions.

My best shot at it was something like this:

ocamlmktop -I +site-lib/sexplib -pp "camlp4 -I +site-lib/sexplib pa_sexp_conv.cma" -o sexplib-top

When I run this toplevel, I can open Sexplib just fine, but when I try using the with sexp syntax extension, I get a syntax error.

Ecliptic answered 15/9, 2011 at 22:52 Comment(0)
A
15

It is XXI century already - use ocamlfind :

        Objective Caml version 3.11.2

# #use "topfind";;
- : unit = ()

# #camlp4o;;
/usr/lib/ocaml/dynlink.cma: loaded
/usr/lib/ocaml/camlp4: added to search path
/usr/lib/ocaml/camlp4/camlp4o.cma: loaded
    Camlp4 Parsing version 3.11.2

# #require "sexplib.syntax";;
/usr/lib/ocaml/unix.cma: loaded
/usr/lib/ocaml/bigarray.cma: loaded
/usr/lib/ocaml/nums.cma: loaded
/usr/lib/ocaml/num-top: added to search path
/usr/lib/ocaml/num-top/num_top.cma: loaded
/usr/lib/ocaml/sexplib: added to search path
/usr/lib/ocaml/sexplib/sexplib.cma: loaded
/usr/lib/ocaml/type-conv: added to search path
/usr/lib/ocaml/type-conv/pa_type_conv.cmo: loaded
/usr/lib/ocaml/sexplib/pa_sexp_conv.cmo: loaded

# type t = { x : int; y : float; } with sexp;;
type t = { x : int; y : float; }
val t_of_sexp__ : Sexplib.Sexp.t -> t = <fun>
val t_of_sexp : Sexplib.Sexp.t -> t = <fun>
val sexp_of_t : t -> Sexplib.Sexp.t = <fun>
Amyotonia answered 16/9, 2011 at 6:48 Comment(4)
Well, paint me orange and call me Susan. Thank you. It'd be really helpful if any learning resource online or in print, anywhere, at all, ever mentioned this, ever.Ecliptic
Wow, enjoyed this idiomatic expression :) Here is an article about findlib (in russian) - github.com/camlunity/kamlo_wiki/blob/master/Findlib.md - expanded with this exampleAmyotonia
What does the #camlp4o;; above do? When I enter that part above I don't see any list of modules loaded - but no error either. Doesn't seem to work.Stepp
Indeed, as of more recent versions, you must open Sexplib.Std before defining type t = ... in order for the required {int,float}_of_sexp,sexp_of_{int,float} standard functions to be in scope.Tantalus

© 2022 - 2024 — McMap. All rights reserved.