Skip to content

Commit fd4bd85

Browse files
committed
Use pretty instead of show in lin2vareq domain (issue #1795)
1 parent c2a588f commit fd4bd85

File tree

1 file changed

+42
-37
lines changed

1 file changed

+42
-37
lines changed

src/cdomains/apron/linearTwoVarEqualityDomain.apron.ml

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,19 @@ module Rhs = struct
2222
(None , offset, divisor ) *)
2323
type t = ((GobZ.t * int) option * GobZ.t * GobZ.t) [@@deriving eq, ord, hash]
2424
let var_zero i = (Some (Z.one,i), Z.zero, Z.one)
25-
let show_coeff c =
26-
if Z.equal c Z.one then ""
27-
else if Z.equal c Z.minus_one then "-"
28-
else (Z.to_string c) ^"·"
29-
let show_rhs_formatted formatter = let ztostring n = (if Z.(geq n zero) then "+" else "") ^ Z.to_string n in
25+
let pretty_coeff () c =
26+
if Z.equal c Z.one then Pretty.nil
27+
else if Z.equal c Z.minus_one then Pretty.text "-"
28+
else Pretty.dprintf "%a·" GobZ.pretty c
29+
let pretty_rhs_formatted formatter () =
30+
let zpretty () n = Pretty.dprintf "%s%a" (if Z.(geq n zero) then "+" else "") GobZ.pretty n in
3031
function
31-
| (Some (coeff,v), o,_) when Z.equal o Z.zero -> Printf.sprintf "%s%s" (show_coeff coeff) (formatter v)
32-
| (Some (coeff,v), o,_) -> Printf.sprintf "%s%s %s" (show_coeff coeff) (formatter v) (ztostring o)
33-
| (None, o,_) -> Printf.sprintf "%s" (Z.to_string o)
34-
let show (v,o,d) =
35-
let rhs=show_rhs_formatted (Printf.sprintf "var_%d") (v,o,d) in
36-
if not (Z.equal d Z.one) then "(" ^ rhs ^ ")/" ^ (Z.to_string d) else rhs
32+
| (Some (coeff,v), o,_) when Z.equal o Z.zero -> Pretty.dprintf "%a%a" pretty_coeff coeff formatter v
33+
| (Some (coeff,v), o,_) -> Pretty.dprintf "%a%a %a" pretty_coeff coeff formatter v zpretty o
34+
| (None, o,_) -> GobZ.pretty () o
35+
let pretty () (v,o,d) =
36+
let rhs () = pretty_rhs_formatted (fun () -> Pretty.dprintf "var_%d") () (v,o,d) in
37+
if not (Z.equal d Z.one) then Pretty.dprintf "(%t)/%a" rhs GobZ.pretty d else rhs ()
3738

3839
(** factor out gcd from all terms, i.e. ax=by+c with a positive is the canonical form for adx+bdy+cd *)
3940
let canonicalize (v,o,d) =
@@ -55,13 +56,12 @@ module EqualitiesConjunction = struct
5556

5657
type t = int * ( Rhs.t IntMap.t ) [@@deriving eq, ord]
5758

58-
let show_formatted formatter econ =
59-
if IntMap.is_empty econ then "{}"
59+
let pretty_formatted formatter () econ =
60+
if IntMap.is_empty econ then Pretty.text "{}"
6061
else
61-
let str = IntMap.fold (fun i (refmonom,off,divi) acc -> Printf.sprintf "%s%s=%s ∧ %s" (Rhs.show_coeff divi) (formatter i) (Rhs.show_rhs_formatted formatter (refmonom,off,divi)) acc) econ "" in
62-
"{" ^ String.sub str 0 (String.length str - 4) ^ "}"
62+
Pretty.dprintf "{%a}" (Pretty.d_list "" (fun () (i, (refmonom,off,divi)) -> Pretty.dprintf "%a%a=%a" Rhs.pretty_coeff divi formatter i (Rhs.pretty_rhs_formatted formatter) (refmonom,off,divi))) (IntMap.bindings econ)
6363

64-
let show econ = show_formatted (Printf.sprintf "var_%d") econ
64+
let pretty = pretty_formatted (fun () -> Pretty.dprintf "var_%d")
6565

6666
let hash (dim,x) = dim + 13* IntMap.fold (fun k value acc -> 13 * 13 * acc + 31 * k + Rhs.hash value) x 0 (* TODO: derive *)
6767

@@ -124,11 +124,11 @@ module EqualitiesConjunction = struct
124124
(op dim (Array.length indexes), a)
125125

126126
let modify_variables_in_domain m cols op = let res = modify_variables_in_domain m cols op in if M.tracing then
127-
M.tracel "modify_dims" "dimarray bumping with (fun x -> x + %d) at positions [%s] in { %s } -> { %s }"
127+
M.tracel "modify_dims" "dimarray bumping with (fun x -> x + %d) at positions [%s] in { %a } -> { %a }"
128128
(op 0 1)
129129
(Array.fold_right (fun i str -> (string_of_int i) ^ ", " ^ str) cols "")
130-
(show (snd m))
131-
(show (snd res));
130+
pretty (snd m)
131+
pretty (snd res);
132132
res
133133

134134
(** required by AbstractRelationalDomainRepresentation, true if dimension is zero *)
@@ -166,7 +166,7 @@ module EqualitiesConjunction = struct
166166
| [] -> d) (* empty cluster means no work for us *)
167167
| _ -> d) (* variable is either a constant or expressed by another refvar *) in
168168
let res = (fst res, IntMap.remove var (snd res)) in (* set d(var) to unknown, finally *)
169-
if M.tracing then M.tracel "forget" "forget var_%d in { %s } -> { %s }" var (show (snd d)) (show (snd res));
169+
if M.tracing then M.tracel "forget" "forget var_%d in { %a } -> { %a }" var pretty (snd d) pretty (snd res);
170170
res
171171

172172
let dim_add (ch: Apron.Dim.change) m =
@@ -184,10 +184,10 @@ module EqualitiesConjunction = struct
184184
let dim_remove ch m = Timing.wrap "dim remove" (fun m -> dim_remove ch m) m
185185

186186
let dim_remove ch m = let res = dim_remove ch m in if M.tracing then
187-
M.tracel "dim_remove" "dim remove at positions [%s] in { %s } -> { %s }"
187+
M.tracel "dim_remove" "dim remove at positions [%s] in { %a } -> { %a }"
188188
(Array.fold_right (fun i str -> (string_of_int i) ^ ", " ^ str) ch.dim "")
189-
(show (snd m))
190-
(show (snd res));
189+
pretty (snd m)
190+
pretty (snd res);
191191
res
192192

193193
exception Contradiction
@@ -238,7 +238,7 @@ module EqualitiesConjunction = struct
238238
let (_,newh1)= inverse i (coeff1,h1,o1,divi1) in
239239
let newh1 = Rhs.subst normalizedj j (Rhs.subst (Some(coeff,j),offs,divi) i newh1) in
240240
subst_var ts h1 newh1)) in
241-
if M.tracing then M.tracel "meet_with_one_conj" "meet_with_one_conj conj: %s eq: var_%d=%s -> %s " (show (snd ts)) i (Rhs.show (var,offs,divi)) (show (snd res))
241+
if M.tracing then M.tracel "meet_with_one_conj" "meet_with_one_conj conj: %a eq: var_%d=%a -> %a" pretty (snd ts) i Rhs.pretty (var,offs,divi) pretty (snd res)
242242
; res
243243

244244
(** affine transform variable i allover conj with transformer (Some (coeff,i)+offs)/divi *)
@@ -328,7 +328,7 @@ struct
328328

329329
let simplified_monomials_from_texp (t: t) texp =
330330
let res = simplified_monomials_from_texp t texp in
331-
if M.tracing then M.tracel "from_texp" "%s %a -> %s" (EConj.show @@ snd @@ BatOption.get t.d) Texpr1.Expr.pretty texp
331+
if M.tracing then M.tracel "from_texp" "%a %a -> %s" EConj.pretty (snd @@ BatOption.get t.d) Texpr1.Expr.pretty texp
332332
(BatOption.map_default (fun (l,(o,d)) -> List.fold_right (fun (a,x,b) acc -> Printf.sprintf "%s*var_%d/%s + %s" (Z.to_string a) x (Z.to_string b) acc) l ((Z.to_string o)^"/"^(Z.to_string d))) "" res);
333333
res
334334

@@ -406,24 +406,29 @@ struct
406406
else (subscr (i/10)) ^ transl.(i mod 10) in
407407
subscr i
408408

409-
let show_var env i =
410-
let res = Var.to_string (Environment.var_of_dim env i) in
409+
let pretty_var env () i =
410+
let res = Var.show (Environment.var_of_dim env i) in
411411
match String.split_on_char '#' res with
412-
| varname::rest::[] -> varname ^ (try to_subscript @@ int_of_string rest with _ -> "#" ^ rest)
413-
| _ -> res
412+
| varname::rest::[] -> Pretty.dprintf "%s%s" varname (try to_subscript @@ int_of_string rest with _ -> "#" ^ res)
413+
| _ -> Pretty.text res
414414

415415
(** prints the current variable equalities with resolved variable names *)
416-
let show varM =
416+
let pretty () varM =
417417
match varM.d with
418-
| None -> "\n"
419-
| Some arr when EConj.is_top_con arr -> "\n"
418+
| None -> Pretty.text ""
419+
| Some arr when EConj.is_top_con arr -> Pretty.text ""
420420
| Some arr ->
421421
if is_bot varM then
422-
"Bot \n"
422+
Pretty.text "Bot"
423423
else
424-
EConj.show_formatted (show_var varM.env) (snd arr) ^ (to_subscript @@ fst arr)
425-
426-
let pretty () (x:t) = text (show x)
424+
Pretty.dprintf "%a%s" (EConj.pretty_formatted (pretty_var varM.env)) (snd arr) (to_subscript @@ fst arr)
425+
426+
include Printable.SimplePretty (
427+
struct
428+
type nonrec t = t
429+
let pretty = pretty
430+
end
431+
)
427432
let printXml f x = BatPrintf.fprintf f "<value>\n<map>\n<key>\nequalities\n</key>\n<value>\n%s</value>\n<key>\nenv\n</key>\n<value>\n%a</value>\n</map>\n</value>\n" (XmlUtil.escape (show x)) Environment.printXml x.env
428433
let eval_interval ask = Bounds.bound_texpr
429434

@@ -439,7 +444,7 @@ struct
439444

440445
let meet_with_one_conj t i e =
441446
let res = meet_with_one_conj t i e in
442-
if M.tracing then M.tracel "meet" "%s with single eq %s=%s -> %s" (show t) (Z.(to_string @@ Tuple3.third e)^ show_var t.env i) (Rhs.show_rhs_formatted (show_var t.env) e) (show res);
447+
if M.tracing then M.tracel "meet" "%a with single eq %a%a=%a -> %a" pretty t GobZ.pretty (Tuple3.third e) (pretty_var t.env) i (Rhs.pretty_rhs_formatted (pretty_var t.env)) e pretty res;
443448
res
444449

445450
let meet t1 t2 =

0 commit comments

Comments
 (0)