From 8d62fd12ca9ac8f11bb658c9b1ea4110159b52d1 Mon Sep 17 00:00:00 2001 From: Sam Blackshear Date: Thu, 10 Mar 2016 18:14:56 -0800 Subject: [PATCH] pretty-printable versions of common data structures Summary: public Many abstract domains are backed by sets or maps. It's tedious to write the code to pretty-print a set or map each time. These utilities allow pretty-printing of a set/map given functions for printing elements/keys and values. Reviewed By: jeremydubreil Differential Revision: D3031196 fb-gh-sync-id: 3bdbde5 shipit-source-id: 3bdbde5 --- infer/src/backend/prettyPrintable.ml | 64 +++++++++++++++++++++++++++ infer/src/backend/prettyPrintable.mli | 47 ++++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 infer/src/backend/prettyPrintable.ml create mode 100644 infer/src/backend/prettyPrintable.mli diff --git a/infer/src/backend/prettyPrintable.ml b/infer/src/backend/prettyPrintable.ml new file mode 100644 index 000000000..c36f2d1f3 --- /dev/null +++ b/infer/src/backend/prettyPrintable.ml @@ -0,0 +1,64 @@ +(* + * Copyright (c) 2016 - present Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + *) + +module F = Format + +(** Wrappers for making pretty-printable modules *) + +module type SetOrderedType = sig + include Set.OrderedType + + val pp_element : F.formatter -> t -> unit +end + +module type MapOrderedType = sig + include Map.OrderedType + + val pp_key : F.formatter -> t -> unit +end + +module type PPSet = sig + include Set.S + + val pp_element : F.formatter -> elt -> unit + val pp : F.formatter -> t -> unit +end + +module type PPMap = sig + include Map.S + + val pp_key : F.formatter -> key -> unit + val pp : pp_value:(F.formatter -> 'a -> unit) -> F.formatter -> 'a t -> unit +end + +let pp_collection ~pp_item fmt c = + let pp_collection fmt c = + let pp_sep fmt () = F.fprintf fmt ", " in + F.pp_print_list ~pp_sep pp_item fmt c in + F.fprintf fmt "{ %a }" pp_collection c + +module MakePPSet (Ord : SetOrderedType) = struct + include Set.Make(Ord) + + let pp_element = Ord.pp_element + + let pp fmt s = + let pp_item fmt e = F.fprintf fmt "%a" Ord.pp_element e in + pp_collection ~pp_item fmt (elements s) +end + +module MakePPMap (Ord : MapOrderedType) = struct + include Map.Make(Ord) + + let pp_key = Ord.pp_key + + let pp ~pp_value fmt m = + let pp_item fmt (k, v) = F.fprintf fmt "%a -> %a" Ord.pp_key k pp_value v in + pp_collection ~pp_item fmt (bindings m) +end diff --git a/infer/src/backend/prettyPrintable.mli b/infer/src/backend/prettyPrintable.mli new file mode 100644 index 000000000..d404c7d67 --- /dev/null +++ b/infer/src/backend/prettyPrintable.mli @@ -0,0 +1,47 @@ +(* + * Copyright (c) 2016 - present Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + *) + +module F = Format + +(** Wrappers for making pretty-printable modules *) + +module type SetOrderedType = sig + type t + val compare : t -> t -> int + val pp_element : F.formatter -> t -> unit +end + +module type MapOrderedType = sig + type t + val compare : t -> t -> int + val pp_key : F.formatter -> t -> unit +end + +module type PPSet = sig + include Set.S + val pp_element : F.formatter -> elt -> unit + val pp : F.formatter -> t -> unit +end + +module type PPMap = sig + include Map.S + val pp_key : F.formatter -> key -> unit + val pp : pp_value:(F.formatter -> 'a -> unit) -> F.formatter -> 'a t -> unit +end + +module MakePPSet : functor (Ord : SetOrderedType) -> sig + include Set.S + val pp_element : F.formatter -> Ord.t -> unit + val pp : F.formatter -> t -> unit +end + +module MakePPMap : functor (Ord : MapOrderedType) -> sig + include Map.S + val pp :pp_value:(F.formatter -> 'a -> unit) -> F.formatter -> 'a t -> unit +end