package ppx_sexp_conv
Generation of S-expression conversion functions from type definitions
Install
Dune Dependency
Authors
Maintainers
Sources
ppx_sexp_conv-v0.9.0.tar.gz
sha256=f6d72c78661cbfe3cd087477fd27cd479502af80d648580a14286987ca3108e7
md5=272bbaf655aa6dd9769144c283020dfe
Description
Part of the Jane Street's PPX rewriters collection.
Published: 22 Mar 2017
README
README.org
#+TITLE: ppx_sexp_conv #+PARENT: ../README.md * [@@deriving sexp] =ppx_sexp_conv= is a PPX syntax extension that generates code for converting OCaml types to and from s-expressions, as defined in the [[https://github.com/janestreet/sexplib][=sexplib=]] library. S-expressions are defined by the following type: #+begin_src ocaml type sexp = Atom of string | List of sexp list #+end_src and are rendered as parenthesized lists of strings, /e.g./ =(This (is an) (s expression))=. =ppx_sexp_conv= fits into the [[https://github.com/whitequark/ppx_deriving][=ppx_deriving=]] framework, so you can invoke it the same way you invoke any other deriving plug-in. Thus, we can write #+begin_src ocaml type int_pair = (int * int) [@@deriving sexp] #+end_src to get two values defined automatically, =sexp_of_int_pair= and =int_pair_of_sexp=. If we only want one direction, we can write one of the following. #+begin_src ocaml type int_pair = (int * int) [@@deriving sexp_of] type int_pair = (int * int) [@@deriving of_sexp] #+end_src These sexp-converters depend on having a set of converters for basic values (/e.g./, =int_of_sexp=) already in scope. This can be done by writing: #+begin_src ocaml open Sexplib.Std #+end_src If you're using [[https://github.com/janestreet/core][=Core=]] or [[https://github.com/janestreet/core_kernel][=Core_kernel=]], you can get the same effect with =open Core= or =open Core_kernel.Std=. It's also possible to construct converters based on type expressions, /i.e./: #+begin_src ocaml [%sexp_of: (int * string) list] [1,"one"; 2,"two"] |> Sexp.to_string;; => "((1 one) (2 two))" [%sexp_of: (int * string) list] [1,"one"; 2,"two"] |> [%of_sexp: (int * string) list];; => [1,"one"; 2,"two"] #+end_src For =%sexp_of=, we can also omit the conversion of some types by putting underscores for that type name. #+begin_src ocaml [%sexp_of: (int * _) list] [1,"one"; 2,"two"] |> Sexp.to_string;; => "((1 _)(2 _))" #+end_src ** Conversion rules In the following, we'll review the serialization rules for different OCaml types. *** Basic types Basic types are represented as atoms. For numbers like =int=, =int32=, =int64=, =float=, the string in the atom is what is accepted the standard ocaml functions =int_of_string=, =Int32.of_string=, etc. For the types =char= or =string=, the string in the atom is respectively a one character string or the string itself. *** Lists and arrays OCaml-lists and arrays are represented as s-expression lists. *** Tuples and unit OCaml tuples are treated as lists of values in the same order as in the tuple. The type =unit= is treated like a 0-tuple. /e.g./: #+begin_src ocaml (3.14, "foo", "bar bla", 27) => (3.14 foo "bar bla" 27) #+end_src *** Options With options, =None= is treated as a zero-element list, and =Some= is treated as a singleton list, as shown below. #+begin_src ocaml None => () Some value => (value) #+end_src We also support reading options following the ordinary rules for variants /i.e./: #+begin_src ocaml None => None Some value => (Some value) #+end_src The rules for variants are described below. *** Records Records are represented as lists of lists, where each inner list is a key-value pair. Each pair consists of the name of the record field (first element), and its value (second element). /e.g./: #+begin_src ocaml { foo = (3,4); bar = "some string"; } => ((foo (3 4)) (bar "some string")) #+end_src Type specifications of records allow the use of a special type =sexp_option= which indicates that a record field should be optional. /e.g./: #+begin_src ocaml type t = { x : int option; y : int sexp_option; } [@@deriving sexp] #+end_src The type =sexp_option= is equivalent to ordinary options, but is treated specially by the code generator. The following examples show how this works. #+begin_src ocaml { x = Some 1; y = Some 2; } => ((x (1)) (y 2)) { x = None ; y = None; } => ((x ())) #+end_src Note that, when present, on optional value is represented as the bare value, rather than explicitly as an option. The types =sexp_list= and =sexp_array= can be used in ways similar to the type =sexp_option=. They assume the empty list and empty array respectively as default values. **** Defaults More complex default values can be specified explicitly using several constructs, /e.g./: #+begin_src ocaml let z_test v = v > 42 type t = { x : int [@default 42]; y : int [@default 3] [@sexp_drop_default]; z : int [@default 3] [@sexp_drop_if fun x -> x = 3]; } [@@deriving sexp] #+end_src The =@default= annotation lets one specify a default value to be selected if the field is not specified, when converting from an s-expression. The =@sexp_drop_default= annotation implies that the field will be dropped when generating the s-expression if the value being serialized is equal to the default according to polymorphic equality. Finally, =@sexp_drop_if= is like =@sexp_drop_default=, except that it lets you specify the condition under which the field is dropped. *** Variants Constant constructors in variants are represented as strings. Constructors with arguments are represented as lists, the first element being the constructor name, the rest being its arguments. Constructors may also be started in lowercase in S-expressions, but will always be converted to uppercase when converting from OCaml values. For example: #+begin_src ocaml type t = A | B of int * float * t [@@deriving sexp] B (42, 3.14, B (-1, 2.72, A)) => (B 42 3.14 (B -1 2.72 A)) #+end_src The above example also demonstrates recursion in data structures. *** Polymorphic variants Polymorphic variants behave almost the same as ordinary variants. The notable difference is that polymorphic variant constructors must always start with an either lower- or uppercase character, matching the way it was specified in the type definition. This is because OCaml distinguishes between upper and lowercase variant constructors. Note that type specifications containing unions of variant types are also supported by the S-expression converter, for example as in: #+begin_src ocaml type ab = [ `A | `B ] [@@deriving sexp] type cd = [ `C | `D ] [@@deriving sexp] type abcd = [ ab | cd ] [@@deriving sexp] #+end_src However, because `ppx_sexp_conv` needs to generate additional code to support inclusions of polymorphic variants, `ppx_sexp_conv` needs to know when processing a type definition whether it might be included in a polymorphic variant. `ppx_sexp_conv` will only generate the extra code automatically in the common case where the type definition is syntactically a polymorphic variant like in the example above. Otherwise, you will need to indicate it by using `[@@deriving sexp_poly]` (resp `of_sexp_poly`) instead of `[@@deriving sexp]` (resp `of_sexp`): #+begin_src ocaml type ab = [ `A | `B ] [@@deriving sexp] type alias_of_ab = ab [@@deriving sexp_poly] type abcd = [ ab | `C | `D ] [@@deriving sexp] #+end_src *** Polymorphic values There is nothing special about polymorphic values as long as there are conversion functions for the type parameters. /e.g./: #+begin_src ocaml type 'a t = A | B of 'a [@@deriving sexp] type foo = int t [@@deriving sexp] #+end_src In the above case the conversion functions will behave as if =foo= had been defined as a monomorphic version of =t= with ='a= replaced by =int= on the right hand side. If a data structure is indeed polymorphic and you want to convert it, you will have to supply the conversion functions for the type parameters at runtime. If you wanted to convert a value of type ='a t= as in the above example, you would have to write something like this: #+begin_src ocaml sexp_of_t sexp_of_a v #+end_src where =sexp_of_a=, which may also be named differently in this particular case, is a function that converts values of type ='a= to an S-expression. Types with more than one parameter require passing conversion functions for those parameters in the order of their appearance on the left hand side of the type definition. *** Opaque values Opaque values are ones for which we do not want to perform conversions. This may be, because we do not have S-expression converters for them, or because we do not want to apply them in a particular type context. /e.g./ to hide large, unimportant parts of configurations. To prevent the preprocessor from generating calls to converters, simply apply the qualifier =sexp_opaque= as if it were a type constructor, /e.g./: #+begin_src ocaml type foo = int * stuff sexp_opaque [@@deriving sexp] #+end_src Thus, there is no need to specify converters for type =stuff=, and if there are any, they will not be used in this particular context. Needless to say, it is not possible to convert such an S-expression back to the original value. Here is an example conversion: #+begin_src ocaml (42, some_stuff) => (42 <opaque>) #+end_src *** Exceptions S-expression converters for exceptions can be automatically registered. #+begin_src ocaml module M = struct exception Foo of int [@@deriving sexp] end #+end_src Such exceptions will be translated in a similar way as sum types, but their constructor will be prefixed with the fully qualified module path (here: =M.Foo=) so as to be able to discriminate between them without problems. The user can then easily convert an exception matching the above one to an S-expression using =sexp_of_exn=. User-defined conversion functions can be registered, too, by calling =add_exn_converter=. This should make it very convenient for users to catch arbitrary exceptions escaping their program and pretty-printing them, including all arguments, as S-expressions. The library already contains mappings for all known exceptions that can escape functions in the OCaml standard library. *** Hash tables The Stdlib's Hash tables, which are abstract values in OCaml, are represented as association lists, /i.e./ lists of key-value pairs, /e.g./: #+begin_src scheme ((foo 42) (bar 3)) #+end_src Reading in the above S-expression as hash table mapping strings to integers (=(string, int) Hashtbl.t=) will map =foo= to =42= and =bar= to =3=. Note that the order of elements in the list may matter, because the OCaml-implementation of hash tables keeps duplicates. Bindings will be inserted into the hash table in the order of appearance. Therefore, the last binding of a key will be the "visible" one, the others are "hidden". See the OCaml documentation on hash tables for details.
Dependencies (8)
-
ocaml-migrate-parsetree
>= "0.4" & < "2.0.0"
-
sexplib
>= "v0.9" & < "v0.10"
-
ppx_type_conv
>= "v0.9" & < "v0.10"
-
ppx_metaquot
>= "v0.9" & < "v0.10"
-
ppx_driver
>= "v0.9" & < "v0.10"
-
ppx_core
>= "v0.9" & < "v0.10"
-
jbuilder
>= "1.0+beta4"
-
ocaml
>= "4.03.0"
Dev Dependencies
None
- amf
-
async-zmq
>= "0.3.0"
-
awa
< "0.2.0"
- azblob
-
bin_prot
>= "v0.9.0" & < "v0.10.0"
-
biocaml
>= "0.4.0"
-
bistro
>= "0.4.0"
-
bookaml
>= "3.1"
-
camlhighlight
>= "4.0"
-
charrua-core
>= "0.3"
- charrua-server
-
cohttp
>= "0.20.1" & < "2.5.0"
-
cohttp-async
>= "1.1.1" & < "2.5.0"
-
cohttp-lwt
= "1.0.0" | >= "1.1.1" & < "2.5.0"
-
conduit
>= "0.12.0" & < "2.1.0"
-
conduit-async
< "2.3.0"
-
conduit-lwt
< "2.1.0"
-
conduit-lwt-unix
< "2.1.0"
- cookies
-
coq-serapi
< "8.7.2+0.4.13"
-
core_kernel
>= "v0.9.0" & < "v0.10.0"
- cudajit
- datakit-ci
-
diet
< "0.4"
- dns-forward
-
dockerfile
>= "1.3.0"
-
dockerfile-cmd
>= "6.0.0"
-
dockerfile-opam
>= "7.1.0"
- dune-expand
- erlang
-
ezjsonm-lwt
>= "1.0.0" & < "1.3.0"
-
frenetic
>= "5.0.0" & < "5.0.5"
-
graphql
< "0.4.0"
-
graphql-lwt
< "0.3.0"
-
graphql_parser
< "0.9.0"
- h1_parser
- hl_yaml
- http
-
ibx
>= "0.8.1"
-
ipaddr
>= "2.7.0"
- ipaddr-sexp
- jsonxt
-
lazy-trie
>= "1.2.0"
- ldp_tls
-
learn-ocaml
>= "0.13.0"
-
learn-ocaml-client
>= "0.13.0"
-
links
>= "0.9.2" & < "0.9.8"
-
little_logger
< "0.3.0"
- macaddr
- macaddr-sexp
- mecab
-
message-switch
>= "1.4.0"
-
mirage-block-unix
>= "2.10.0" & < "2.11.1"
-
mirage-conduit
< "2.0.0" | >= "3.0.0"
-
mirage-crypto-pk
< "0.10.4"
-
mirage-net-xen
>= "1.6.0" & < "1.7.1"
-
msgpck
< "1.1"
-
nbd
>= "2.1.0"
- netchannel
-
nocrypto
>= "0.5.3"
-
nsq
>= "0.2.5" & < "0.5.2"
-
obeam
= "0.1.0"
- obuilder
- obuilder-spec
- ocaml-basics
- ocaml-logicalform
- ocaml-topexpect
- oci
- ocluster
- opam-check-npm-deps
-
opass
>= "1.0.6"
- opine
-
opium
>= "0.15.0" & < "0.19.0"
- opium_kernel
-
otr
>= "0.3.1" & < "0.3.5"
-
parsexp
< "v0.10.0"
-
pgocaml
>= "4.2"
-
pgocaml_ppx
>= "4.2" & < "4.3.0"
-
planck
>= "2.2.0"
-
ppx_assert
= "v0.9.0"
-
ppx_bap
< "v0.14.0"
-
ppx_base
< "v0.10.0"
-
ppx_cstruct
>= "3.1.0"
-
ppx_custom_printf
= "v0.9.0"
-
ppx_expect
= "v0.9.0"
-
ppx_hash
< "v0.10.0"
- ppx_minidebug
-
ppx_protocol_conv
>= "3.1.0"
-
ppx_protocol_conv_json
>= "3.1.0"
- ppx_protocol_conv_jsonm
-
ppx_protocol_conv_msgpack
>= "3.1.0"
-
ppx_protocol_conv_xml_light
>= "3.1.0"
- ppx_protocol_conv_xmlm
-
ppx_protocol_conv_yaml
>= "3.1.0"
-
ppx_sexp_message
= "v0.9.0"
-
ppx_sexp_value
= "v0.9.0"
-
protocol-9p
>= "0.6.0"
- protocol-9p-unix
- qcow
-
qcow-format
>= "0.3"
-
reparse
>= "1.0.1" & < "2.0.0"
-
routes
>= "2.0.0"
- sel
-
shared-block-ring
>= "2.3.0" & < "3.0.0"
-
sihl
< "0.1.0"
-
ssh-agent
< "0.4.0"
- sslconf
-
tls
>= "0.7.1" & < "0.17.0"
-
torch
< "v0.16.0"
-
typerep_extended
>= "v0.9.0"
-
uri
>= "1.9.2"
- uri-re
-
uri-sexp
< "4.0.0"
-
vchan
>= "2.1.0" & < "6.0.2"
-
vchan-unix
< "6.0.2"
-
vchan-xen
< "6.0.2"
- vendredi
-
vmnet
>= "1.1.0"
- vscoq-language-server
-
wamp
= "1.0"
-
x509
>= "0.5.1" & < "0.6.3"
-
xapi-backtrace
< "0.7"
- xapi-idl
-
yaml
< "3.0.0"
- yaml-sexp
Conflicts
None
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>
On This Page