Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove ppx generators #342

Merged
merged 8 commits into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Next version

- #342: Add two submodules of combinators in `Util`:
- `Pp` to pretty-print values back to valid OCaml syntax
- `Equal` to test equality of values
- #337: Add 3 `Bytes.t` combinators to `Lin`: `bytes`, `bytes_small`, `bytes_small_printable`
- #329,340: Support `qcheck-lin` and `qcheck-stm` on OCaml 4.13.x and 4.14.x
without the `Domain` and `Effect` modes
Expand Down
8 changes: 2 additions & 6 deletions dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ the multicore run-time of OCaml 5.0.")
(tags ("test" "test suite" "property" "qcheck" "quickcheck" "multicore" "non-determinism"))
(depends
base-domains
(ppx_deriving (>= "5.2.1"))
(qcheck-core (>= "0.20"))
(ppx_deriving_qcheck (>= "0.2.0"))
(qcheck-lin (= :version))
(qcheck-stm (= :version))))

Expand All @@ -33,8 +31,7 @@ sequential and parallel tests against a declarative model.")
(depopts base-domains)
(depends
(qcheck-core (>= "0.20"))
(qcheck-multicoretests-util (= :version))
(ppx_deriving (and :with-test (>= "5.2.1")))))
(qcheck-multicoretests-util (= :version))))

(package
(name qcheck-lin)
Expand All @@ -49,8 +46,7 @@ and explained by some sequential interleaving.")
(depends
(ocaml (>= 4.13))
(qcheck-core (>= "0.20"))
(qcheck-multicoretests-util (= :version))
(ppx_deriving (and :with-test (>= "5.2.1")))))
(qcheck-multicoretests-util (= :version))))

(package
(name qcheck-multicoretests-util)
Expand Down
3 changes: 0 additions & 3 deletions lib/lin.mli
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@ module Internal : sig
val lin_test : rep_count:int -> retries:int -> count:int -> name:string -> lin_prop:(Spec.cmd list * Spec.cmd list * Spec.cmd list -> bool) -> QCheck.Test.t
val neg_lin_test : rep_count:int -> retries:int -> count:int -> name:string -> lin_prop:(Spec.cmd list * Spec.cmd list * Spec.cmd list -> bool) -> QCheck.Test.t
end

val pp_exn : Format.formatter -> exn -> unit
(** Format-based exception pretty printer *)
end
[@@alert internal "This module is exposed for internal uses only, its API may change at any time"]

Expand Down
2 changes: 1 addition & 1 deletion lib/lin_effect.ml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ module Make_internal (Spec : Internal.CmdSpec [@alert "-internal"]) = struct
let init = Spec.init
let cleanup = Spec.cleanup

type cmd = SchedYield | UserCmd of Spec.cmd [@@deriving qcheck]
type cmd = SchedYield | UserCmd of Spec.cmd

let show_cmd c = match c with
| SchedYield -> "<SchedYield>"
Expand Down
93 changes: 90 additions & 3 deletions lib/util.ml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,93 @@ let protect (f : 'a -> 'b) (a : 'a) : ('b, exn) result =
try Result.Ok (f a)
with e -> Result.Error e

let pp_exn fmt e = Format.fprintf fmt "%s" (Printexc.to_string e)
let show_exn e = Format.asprintf "%a" e pp_exn
let equal_exn = (=)
module Pp = struct
open Format

type 'a t = bool -> Format.formatter -> 'a -> unit

let to_show f x = asprintf "%a" (f false) x

let of_show f par fmt x =
fprintf fmt (if par then "(%s)" else "%s") (f x)

let cst0 name fmt = pp_print_string fmt name

let cst1 (pp : 'a t) name par fmt x =
fprintf fmt (if par then "(%s %a)" else "%s %a") name (pp true) x

let cst2 (pp1 : 'a t) (pp2 : 'b t) name par fmt x y =
fprintf fmt (if par then "(%s (%a, %a))" else "%s (%a, %a)") name (pp1 false) x (pp2 false) y

let cst3 (pp1 : 'a t) (pp2 : 'b t) (pp3 : 'c t) name par fmt x y z =
fprintf fmt
(if par then "(%s (%a, %a, %a))" else "%s (%a, %a, %a)")
name (pp1 false) x (pp2 false) y (pp3 false) z

let pp_exn = of_show Printexc.to_string
let pp_unit _ fmt () = pp_print_string fmt "()"
let pp_bool _ fmt b = fprintf fmt "%B" b
let pp_int par fmt i = fprintf fmt (if par && i < 0 then "(%d)" else "%d") i
let pp_int64 par fmt i = fprintf fmt (if par && i < 0L then "(%LdL)" else "%LdL") i
let pp_float par fmt f = fprintf fmt (if par && f < 0.0 then "(%F)" else "%F") f
let pp_char _ fmt c = fprintf fmt "%C" c
let pp_string _ fmt s = fprintf fmt "%S" s
let pp_bytes _ fmt s = fprintf fmt "%S" (Bytes.to_string s)

let pp_option (pp_s : 'a t) par fmt o =
match o with
| None -> pp_print_string fmt "None"
| Some s -> fprintf fmt (if par then "(Some %a)" else "Some %a") (pp_s true) s

let pp_result (pp_o : 'o t) (pp_e : 'e t) par fmt r =
let open Result in
match r with
| Ok o -> fprintf fmt (if par then "(Ok %a)" else "Ok %a") (pp_o true) o
| Error e -> fprintf fmt (if par then "(Error %a)" else "Error %a") (pp_e true) e

let pp_pair (pp_f : 'a t) (pp_s : 'b t) _ fmt (x,y) =
fprintf fmt "(%a, %a)" (pp_f false) x (pp_s false) y

let pp_list (pp_e : 'a t) _ fmt l =
pp_print_string fmt "[";
pp_print_list ~pp_sep:(fun fmt () -> fprintf fmt ";@ ") (pp_e false) fmt l;
pp_print_string fmt "]"

let pp_seq (pp_e : 'a t) _ fmt s =
pp_print_string fmt "<";
pp_print_seq ~pp_sep:(fun fmt () -> fprintf fmt ";@ ") (pp_e false) fmt s;
pp_print_string fmt ">"

let pp_array (pp_e : 'a t) _ fmt a =
pp_print_string fmt "[|";
pp_print_seq ~pp_sep:(fun fmt () -> fprintf fmt ";@ ") (pp_e false) fmt (Array.to_seq a);
pp_print_string fmt "|]"

type pp_field = Format.formatter -> unit

let pp_field name (pp_c : 'a t) c fmt =
fprintf fmt "%s =@ %a" name (pp_c false) c

let pp_record _ fmt fields =
pp_print_string fmt "{ ";
pp_print_list ~pp_sep:(fun fmt () -> fprintf fmt ";@ ") (fun fmt ppf -> ppf fmt) fmt fields;
fprintf fmt "@ }"
end

module Equal = struct
type 'a t = 'a -> 'a -> bool

let equal_exn = ( = )
let equal_unit = Unit.equal
let equal_bool = Bool.equal
let equal_int = Int.equal
let equal_int64 = Int64.equal
let equal_float = Float.equal
let equal_char = Char.equal
let equal_string = String.equal
let equal_option = Option.equal
let equal_result eq_o eq_e x y = Result.equal ~ok:eq_o ~error:eq_e x y
let equal_list = List.equal
let equal_seq = Seq.equal
let equal_array eq x y = Seq.equal eq (Array.to_seq x) (Array.to_seq y)
end
124 changes: 118 additions & 6 deletions lib/util.mli
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,123 @@ val print_triple_vertical :
val protect : ('a -> 'b) -> 'a -> ('b, exn) result
(** [protect f] turns an [exception] throwing function into a [result] returning function. *)

val pp_exn : Format.formatter -> exn -> unit
(** Format-based exception pretty printer *)
module Pp : sig
(** Pretty-printing combinators that generate valid OCaml syntax for common
types along with combinators for user-defined types *)

val show_exn : (Format.formatter -> (Format.formatter -> exn -> unit) -> unit) -> string
(** Format-based exception to-string function *)
type 'a t = bool -> Format.formatter -> 'a -> unit
(** The type of pretty-printers to valid OCaml syntax.
The [bool] argument asks the printer to wrap its output inside parentheses
if it produces a non-atomic expression. *)

val equal_exn : exn -> exn -> bool
(** equality function for comparing exceptions *)
val to_show : 'a t -> 'a -> string
(** [to_show pp] converts a pretty-printer to a simple ['a -> string] function. *)

val of_show : ('a -> string) -> 'a t
(** [of_show show] uses a simple ['a -> string] function as a pretty-printer.
Unfortunately, it will wrap the resulting string with parentheses in more
cases than strictly necessary. *)

val cst0 : string -> Format.formatter -> unit
(** [cst0 name fmt] pretty-prints a constructor [name] with no argument. *)

val cst1 : 'a t -> string -> bool -> Format.formatter -> 'a -> unit
(** [cst1 pp name par v fmt] pretty-prints a constructor [name] with one
parameter, using [pp] to pretty-print its argument [v], wrapping itself
into parentheses when [par]. *)

val cst2 : 'a t -> 'b t -> string -> bool -> Format.formatter -> 'a -> 'b -> unit
(** [cst2 pp1 pp2 name par v1 v2 fmt] pretty-prints a constructor [name] with
two parameters, using [pp]i to pretty-print its argument [v]i, wrapping
itself into parentheses when [par]. *)

val cst3 : 'a t -> 'b t -> 'c t -> string -> bool -> Format.formatter -> 'a -> 'b -> 'c -> unit
(** [cst3 pp1 pp2 pp3 name par v1 v2 v3 fmt] pretty-prints a constructor
[name] with three parameters, using [pp]i to pretty-print its argument
[v]i, wrapping itself into parentheses when [par]. *)

val pp_exn : exn t
(** Pretty-printer for exceptions reusing the standard {!Printexc.to_string}.
The exception message will be wrapped conservatively (ie too often) in
parentheses. *)

val pp_unit : unit t
(** Pretty-printer for type [unit] *)

val pp_bool : bool t
(** Pretty-printer for type [bool] *)

val pp_int : int t
(** Pretty-printer for type [int] *)

val pp_int64 : int64 t
(** Pretty-printer for type [int64] *)

val pp_float : float t
(** Pretty-printer for type [float] *)

val pp_char : char t
(** Pretty-printer for type [char] *)

val pp_string : string t
(** Pretty-printer for type [string] *)

val pp_bytes : bytes t
(** Pretty-printer for type [bytes] *)

val pp_option : 'a t -> 'a option t
(** [pp_option pp] pretty-prints a value of type ['a option] using [pp] to
pretty-print values of type ['a]. *)

val pp_result : 'o t -> 'e t -> ('o, 'e) result t
(** [pp_result pp_ok pp_error] pretty-prints a value of type [('o, 'e) result]
using [pp_ok] to pretty-print values of type ['o] and [pp_error] for
values of type ['e]. *)

val pp_pair : 'a t -> 'b t -> ('a * 'b) t
(** [pp_pair pp_a pp_b] pretty-prints a value of type ['a * 'b] using [pp_a]
to pretty-print values of type ['a] and [pp_b] for values of type ['b]. *)

val pp_list : 'a t -> 'a list t
(** [pp_list pp] pretty-prints a list using [pp] to pretty-print its elements. *)

val pp_seq : 'a t -> 'a Seq.t t
(** [pp_seq pp] pretty-prints a sequence using [pp] to pretty-print its elements. *)

val pp_array : 'a t -> 'a array t
(** [pp_array pp] pretty-prints an array using [pp] to pretty-print its elements. *)

type pp_field
(** The abtract type for the pretty-printer of a record field *)

val pp_field : string -> 'a t -> 'a -> pp_field
(** [pp_field name pp v] build a pretty-printer for a record field of given
[name] using [pp] to pretty-print its content value [v]. *)

val pp_record : pp_field list t
(** [pp_record flds] pretty-prints a record using the list of pretty-printers
of its fields. *)
end

module Equal : sig
(** Equality combinators for common types *)

type 'a t = 'a -> 'a -> bool
(** The usual type for equality functions *)

val equal_exn : exn t
(** equality function for comparing exceptions *)

val equal_unit : unit t
val equal_bool : bool t
val equal_int : int t
val equal_int64 : int64 t
val equal_float : float t
val equal_char : char t
val equal_string : string t
val equal_option : 'a t -> 'a option t
val equal_result : 'o t -> 'e t -> ('o, 'e) result t
val equal_list : 'a t -> 'a list t
val equal_seq : 'a t -> 'a Seq.t t
val equal_array : 'a t -> 'a array t
end
2 changes: 0 additions & 2 deletions multicoretests.opam
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ bug-reports: "https://github.com/ocaml-multicore/multicoretests/issues"
depends: [
"dune" {>= "3.0"}
"base-domains"
"ppx_deriving" {>= "5.2.1"}
"qcheck-core" {>= "0.20"}
"ppx_deriving_qcheck" {>= "0.2.0"}
"qcheck-lin" {= version}
"qcheck-stm" {= version}
"odoc" {with-doc}
Expand Down
1 change: 0 additions & 1 deletion qcheck-lin.opam
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ depends: [
"ocaml" {>= "4.13"}
"qcheck-core" {>= "0.20"}
"qcheck-multicoretests-util" {= version}
"ppx_deriving" {with-test & >= "5.2.1"}
"odoc" {with-doc}
]
depopts: ["base-domains"]
Expand Down
1 change: 0 additions & 1 deletion qcheck-stm.opam
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ depends: [
"dune" {>= "3.0"}
"qcheck-core" {>= "0.20"}
"qcheck-multicoretests-util" {= version}
"ppx_deriving" {with-test & >= "5.2.1"}
"odoc" {with-doc}
]
depopts: ["base-domains"]
Expand Down
2 changes: 0 additions & 2 deletions src/array/dune
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
(modules stm_tests)
(package multicoretests)
(libraries qcheck-stm.sequential qcheck-stm.domain)
(preprocess (pps ppx_deriving.show))
(action (run %{test} --verbose))
)

Expand All @@ -15,7 +14,6 @@
(package multicoretests)
(flags (:standard -w -27))
(libraries qcheck-lin.domain)
(preprocess (pps ppx_deriving_qcheck ppx_deriving.show ppx_deriving.eq))
; (action (run %{test} --verbose))
(action (echo "Skipping src/array/%{test} from the test suite\n\n"))
)
Expand Down
Loading