What is the point of [@@deriving sexp]?

I’m familiar with Clojure and understand the usefulness of symbolic exprs. What I don’t understand is shows up in situations that seem very unrelated to lisp / scheme / mini-interpreters.

For example, in core_unix.ml

type seek_command =
  Unix.seek_command =
  | SEEK_SET
  | SEEK_CUR
  | SEEK_END
[@@deriving sexp]

type file_kind = Unix.file_kind =
  | S_REG
  | S_DIR
  | S_CHR
  | S_BLK
  | S_LNK
  | S_FIFO
  | S_SOCK
[@@deriving sexp]

If you’re asking specifically why Janestreet’s Base/Core libraries use sexp everywhere, it’s because they basically use it as the default data serialization format. See the Data Serialization with S-Expressions chapter in Real World OCaml.

Edit: And if you want to work with Base/Core, you will probably end up sprinkling @@deriving sexp over many of your types as well. Many of their functors require it, and many other things (Map, Set, etc) will work only with the sexp converters, and the deriving sexp is the easiest way to get the functionality.

6 Likes

S-expressions being strongly associated with lisps is basically a historical accident. Leaving aside Jane Street’s particular uses of them, they are a useful approach to all kinds of data interchange and symbolic representation tasks.

You might find it interesting to know that s-expressions are how intermediate “lambda forms” are emitted by the OCaml compiler, if you ask for them: Going through the OCaml compiler pipeline (manually) - Dev.Poga

1 Like

@cemerick

Slightly off topic:

  1. Thanks for your work on clojure/cider-mode!

  2. I’m really really curious – do you use an “repl-driven” development style in OCaml? If so, what is your workflow like; if not, why ?

I’ve used sexps as a less-incredibly-wordy JSON. E.g. the IDL for GitHub - chetmurthy/ocaml-cppffigen: C++ FFI Generator for Ocaml is sexps.

I don’t think I ever contributed directly to cider-mode? But then, that would have been a long time ago, so who knows. :bowing_man:

I posted about my experience and general practice with OCaml vs. typical Clojure/lisp “repl-driven” styles some months ago that probably answers your other questions: What's your development workflow? - #8 by cemerick

My fault; I confused you with bbatsov

Ah, of course. He’s here, too, FWIW @bbatsov

1 Like

You did contribute directly to CIDER! :slight_smile: I remembered seeing a CIDER PR from you and I traced it back to Feb 2014 (see Commits · clojure-emacs/cider · GitHub).

2 Likes

@cemerick @bbatsov : Watch out, this might get you guys cancelled in 2030:

Why did you migrate from Clojure to … eventually Rust ?

For me it was the lack of a type system: (1) I wanted to understand datashapes without having to run the code and (2) I wanted dumb errors caught at compile time and (3) I hated writing unit tests for things a HM type system could catch.