package ppx_wideopen

  1. Overview
  2. Docs
Ppx_wideopen syntax extension

Install

Dune Dependency

Authors

Maintainers

Sources

0.0.1.tar.gz
md5=0d03ea635e6a4ea32a12b9b2384d019a
sha512=7f1d8f98dda9e57b90a7f87247bc5e03e69b41dbe6b9fcacb789e32c03b622c0e99495980dea7fcc70827f58ea3718347ca10e6cff5e47373f77ada2b83bf1bf

Description

Wideopen is a syntax-extension that allows you to annotate an open statement in order to use a custom parsing utility for OCaml's litterals. It uses by default the 'of_string' function of the specified module.

Published: 24 Dec 2019

README

Wideopen

Why?

The shadowing trick allows the users to overwrite the usual arithmetic operators (+, -, ...) with their own by simply doing let open My_module in .... However the litterals in the program are still interpreted as integer or floats by OCaml's parser which forces the programmers to write boilerplate code to handle these cases.

Wideopen is a syntax-extension that allows you to use a custom parsing utility for OCaml's litterals using by default the of_string function of the specified module.

For example, the following piece of code (which uses the Zarith library) computes the number of solutions of a quadratic equation:

  let nb_solutions a b c =
    let open[@parse.int] Z in
    let delta = b * b - 4 * a * c in
    if delta > 0 then 2
    else if delta = 0 then 1
    else 0

Which is syntactic sugar for:

  let nb_solutions a b c =
    let open Z in
    let delta = (b * b) - (of_string "4") * a * c in
    if delta > (of_string "0")
    then of_string "2"
    else if delta = (of_string "0") then of_string "1" else of_string "0"

How it works?

Whenever an [@parse.] annotation is met, the litterals l appearing in the sub-expression are replaced with (of_string l). Also, note that as this extension works on the Parsetree of OCaml, we are able to handle litterals of arbitrary size (e.g. greater than 2^64), given that the parsing function being used accepts it.

The different annotations:

Wideopen provides three different annotation:

  • [@parse.int] which replaces only the integer litterals (the Pconst_integers constructor of OCaml's Parsetree)

  • [@parse.float] which replaces only the float litterals (the Pconst_float constructor of OCaml's Parsetree)

  • [@parse.all] which replaces both float and integer litterals.

Note that the latter annotation allows you in particular to manipulate "integers" and "floats" indiferently as a "bigger type" (Zarith's rationals using the Q module for example) without having to care about int-float conversion and operators.

Customizing the parsing utility:

By default, Wideopen uses the of_string of the specified module. However, to avoid having to define such a function within our modules, we can specify the name of the function to be used for the parsing as in the following example:

let open[@parse.all using parse] Mymodule in ...

Which parses both integers and floats using the parse function of the module Mymodule

Dependencies (3)

  1. dune >= "1.2"
  2. ppx_tools_versioned
  3. ocaml >= "4.04.0"

Dev Dependencies (1)

  1. zarith with-test

Used by

None

Conflicts

None

OCaml

Innovation. Community. Security.