package prettiest

  1. Overview
  2. Docs
Non-greedy pretty printer

Install

Dune Dependency

Authors

Maintainers

Sources

0.0.1.tar.gz
md5=4b6930c4dd9444cede6b45a2f2560d32

Description

Implementation of Jean-Philippe Bernardy's A Pretty But Not Greedy Printer (Functional Pearl). PACMPL 1(ICFP): 6:1-6:21 (2017) (https://jyp.github.io/pdf/Prettiest.pdf).

Published: 19 Jan 2018

README

prettiest

Implementation of Jean-Philippe Bernardy's A Pretty But Not Greedy Printer (Functional Pearl). PACMPL 1(ICFP): 6:1-6:21 (2017) in OCaml. The author of the paper has a version in Haskell.

I make no promises about performance.

Documentation

Example from the paper

A version of this example is included in the test folder. That folder also includes a larger example of pretty printing types.

type sexpr =
  | Atom of string
  | Sexpr of sexpr list

let rec pretty_sexpr xs =
  let open Prettiest in
  let open Prettiest.Infix in
  match xs with
  | Atom s -> text s
  | Sexpr xs -> text "(" <> (sep (List.map ~f:pretty_sexpr xs)) <> text ")"

let test_data =
  let abcd = Sexpr [Atom "a"; Atom "b"; Atom "c"; Atom "d"] in
  let abcd4 = Sexpr [abcd; abcd; abcd; abcd] in
  Sexpr [
    Sexpr [Atom "abcde"; abcd4];
    Sexpr [Atom "abcdefgh"; abcd4];
  ]

let fit = Option.value ~default:"did not fit"

let%expect_test "sexp" =
  test_data |> pretty_sexpr |> Prettiest.render 80 |> fit |> print_endline;
  Out_channel.newline stdout;
  test_data |> pretty_sexpr |> Prettiest.render 20 |> fit |> print_endline;
  [%expect {|
    ((abcde ((a b c d) (a b c d) (a b c d) (a b c d)))
     (abcdefgh ((a b c d) (a b c d) (a b c d) (a b c d))))

    ((abcde ((a b c d)
             (a b c d)
             (a b c d)
             (a b c d)))
     (abcdefgh
      ((a b c d)
       (a b c d)
       (a b c d)
       (a b c d))))
  |}]

Dependencies (5)

  1. jbuilder >= "1.0+beta12"
  2. ocaml-migrate-parsetree < "2.0.0"
  3. ppx_compare >= "v0.10"
  4. base >= "v0.10" & < "v0.11"
  5. ocaml >= "4.04.1"

Dev Dependencies

None

Used by

None

Conflicts

None

OCaml

Innovation. Community. Security.