simplify_formula.ml 7.54 KB
Newer Older
Andrei Paskevich's avatar
Andrei Paskevich committed
1 2 3
(********************************************************************)
(*                                                                  *)
(*  The Why3 Verification Platform   /   The Why3 Development Team  *)
4
(*  Copyright 2010-2015   --   INRIA - CNRS - Paris-Sud University  *)
Andrei Paskevich's avatar
Andrei Paskevich committed
5 6 7 8
(*                                                                  *)
(*  This software is distributed under the terms of the GNU Lesser  *)
(*  General Public License version 2.1, with the special exception  *)
(*  on linking described in file LICENSE.                           *)
9
(*                                                                  *)
Andrei Paskevich's avatar
Andrei Paskevich committed
10
(********************************************************************)
11 12 13 14

open Term
open Decl

15
let rec fmla_simpl f = TermTF.t_map_simp (fun t -> t) fmla_simpl f
16 17 18

let decl_l d =
  match d.d_node with
Andrei Paskevich's avatar
Andrei Paskevich committed
19
    | Dprop (k,pr,f) ->
20
        let f = fmla_simpl f in
21
        begin match f.t_node, k with
22 23 24
          | Ttrue, Paxiom -> [[]]
          | Tfalse, Paxiom -> []
          | Ttrue, Pgoal -> []
25 26
          | _ -> [[create_prop_decl k pr f]]
        end
27
    | _ -> [[DeclTF.decl_map (fun t -> t) fmla_simpl d]]
28

29
let simplify_formula = Trans.rewriteTF (fun t -> t) fmla_simpl None
30

31
let simplify_formula_and_task = Trans.decl_l decl_l None
32

Andrei Paskevich's avatar
Andrei Paskevich committed
33
let () = Trans.register_transform
34
  "simplify_formula" simplify_formula
35
  ~desc:"Simplify@ the@ formulas@ using@ propositional@ simplifications."
36

Andrei Paskevich's avatar
Andrei Paskevich committed
37
let () = Trans.register_transform_l
38
  "simplify_formula_and_task" simplify_formula_and_task
39
  ~desc:"Same as simplify_formula, but also@ \
Andrei Paskevich's avatar
Andrei Paskevich committed
40
         remove@ axioms@ and@ goals@ that@ become@ trivial."
41 42 43 44 45 46 47

(** remove_trivial_quantification
    Original version in the alt-ergo prover by Sylvain Conchon *)

(** transform \exists x. x == y /\ F into F[y/x] *)
(** transform \forall x. x <> y \/ F into F[y/x] *)

Andrei Paskevich's avatar
Andrei Paskevich committed
48
(** test if the freevariable of a term
49
    are included in a given set *)
50
let t_boundvars_in fvars t =
51
  try
52 53 54
    t_v_fold (fun () u -> if Svs.mem u fvars then raise Exit) () t;
    false
  with Exit -> true
55 56 57

exception Subst_found of term

58 59
let rec fmla_find_subst boundvars var sign f =
  let fnF = fmla_find_subst boundvars var in
60 61 62
  match f.t_node with
    | Tapp (ls,[{t_node=Tvar vs} as tv;t])
    | Tapp (ls,[t;{t_node=Tvar vs} as tv])
Andrei Paskevich's avatar
Andrei Paskevich committed
63
        when sign && ls_equal ls ps_equ && vs_equal vs var
64
          && not (t_equal t tv) && not (t_boundvars_in boundvars t) ->
65
        raise (Subst_found t)
66 67 68
    | Tbinop (Tor, f1, f2)  when not sign -> (fnF sign f1); (fnF sign f2)
    | Tbinop (Tand, f1, f2) when sign ->  (fnF sign f1); (fnF sign f2)
    | Tbinop (Timplies, f1, f2) when not sign ->
69
        (fnF (not sign) f1); (fnF sign f2)
70 71 72
    | Tnot f1 -> fnF (not sign) f1
    | Tquant (_,fb) ->
        let vsl,trl,f' = t_open_quant fb in
Andrei Paskevich's avatar
Andrei Paskevich committed
73 74 75
        if trl = []
        then
          let boundvars =
76 77
            List.fold_left (fun s v -> Svs.add v s) boundvars vsl in
          fmla_find_subst boundvars var sign f'
78
    | Tlet (_,fb) ->
79
        let vs,f' = t_open_bound fb in
80 81
        let boundvars = Svs.add vs boundvars in
        fmla_find_subst boundvars var sign f'
82 83
    | Tbinop (_, _, _) | Tif ( _, _, _)  | Tcase (_, _)
    | Tapp _ | Tfalse | Ttrue -> ()
84
    | Tvar _ | Tconst _ | Teps _ -> raise (FmlaExpected f)
85 86 87

let rec fmla_quant sign f = function
  | [] -> [], f
Andrei Paskevich's avatar
Andrei Paskevich committed
88
  | vs::l ->
89
      let vsl, f = fmla_quant sign f l in
90
      try
91
        fmla_find_subst (Svs.singleton vs) vs sign f;
92
        vs::vsl, f
93
      with Subst_found t ->
94
        let f = t_subst_single vs t f in
95
        vsl, fmla_simpl f
96

97
let rec fmla_remove_quant f =
98
  match f.t_node with
99 100
    | Tquant (k,fb) ->
        let vsl,trl,f',close = t_open_quant_cb fb in
Andrei Paskevich's avatar
Andrei Paskevich committed
101
          if trl <> []
102
          then f
103 104
          else
            let sign = match k with
105
              | Tforall -> false | Texists -> true in
106 107
            let vsl, f' = fmla_quant sign f' vsl in
            let f' = fmla_remove_quant f' in
108
            t_quant k (close vsl [] f')
109
    | _ -> TermTF.t_map (fun t -> t) fmla_remove_quant f
110 111 112 113 114 115 116 117 118

(*let fmla_remove_quant f =
  Format.eprintf "@[<hov>%a =>|@\n" Pretty.print_fmla f;
  let f = fmla_remove_quant f in
  Format.eprintf "|=>%a@]@.@." Pretty.print_fmla f;
  Pretty.forget_all ();
  f
*)

Andrei Paskevich's avatar
Andrei Paskevich committed
119
let simplify_trivial_quantification =
120
  Trans.rewriteTF (fun t -> t) fmla_remove_quant None
121

Andrei Paskevich's avatar
Andrei Paskevich committed
122
let () = Trans.register_transform
123
  "simplify_trivial_quantification" simplify_trivial_quantification
124
  ~desc:"@[Simplify@ trivial@ quantifications:@]@\n  \
Andrei Paskevich's avatar
Andrei Paskevich committed
125 126 127
    @[\
     - @[transform \\exists x. x == y /\\ F@ into F[y/x],@]@\n\
     - @[transform \\forall x. x <> y \\/ F@ into F[y/x].@]@]"
128

129
let simplify_trivial_quantification_in_goal =
130
  Trans.goal (fun pr f -> [create_prop_decl Pgoal pr (fmla_remove_quant f)])
131

Andrei Paskevich's avatar
Andrei Paskevich committed
132 133
let () = Trans.register_transform
  "simplify_trivial_quantification_in_goal"
134
   simplify_trivial_quantification_in_goal
135
  ~desc:"Same@ as@ simplify_trivial_quantification, but@ only@ in@ goals."
136 137 138

(** linearize all the subformulas with the given connector (conj/disj);
    the returned array also contains the sign of each subformula *)
139
let fmla_flatten conj f =
140 141
  let terms = ref [] in
  let rec aux sign f =
142
    match f.t_node with
143 144
    | Tnot f -> aux (not sign) f
    | Tbinop (Tor, f1, f2) when sign <> conj ->
145
        aux sign f2; aux sign f1
146
    | Tbinop (Tand, f1, f2) when sign = conj ->
147
        aux sign f2; aux sign f1
148
    | Tbinop (Timplies, f1, f2) when sign <> conj ->
149 150
        aux sign f2; aux (not sign) f1
    | _ -> terms := (f, sign)::!terms in
151
  aux true f;
152 153 154
  Array.of_list !terms

(** recreate the structure of a given formula with linearized subformulas *)
155
let fmla_unflatten conj f formulas =
156
  let i = ref 0 in
157
  let rec aux sign f = t_label_copy f (match f.t_node with
158 159 160 161 162 163 164
    | Tnot f -> t_not (aux (not sign) f)
    | Tbinop (Tor, f1, f2) when sign <> conj ->
        let f1' = aux sign f1 in t_or f1' (aux sign f2)
    | Tbinop (Tand, f1, f2) when sign = conj ->
        let f1' = aux sign f1 in t_and f1' (aux sign f2)
    | Tbinop (Timplies, f1, f2) when sign <> conj ->
        let f1' = aux (not sign) f1 in t_implies f1' (aux sign f2)
165 166 167 168 169
    | _ ->
        let (t, s) = formulas.(!i) in
        assert (sign = s);
        incr i;
        t) in
170
  aux true f
171 172 173 174

(** substitute all the terms that appear as a side of an equality/disequality
    and that match the given filter

175 176 177
    equal terms can be substituted in all the terms of surrounding
    conjunctions, while disequal terms can be substituted in all the terms
    of surrounding disjunctions
178 179 180 181

    substitutions are not exported outside quantifiers (even if their free
    variables are untouched), so the transformation is possibly incomplete
    (but still correct) on formulas that have inner quantifiers *)
182
let fmla_cond_subst filter f =
183
  let rec aux f =
184
    match f.t_node with
185
    | Tbinop (o, _, _) when o <> Tiff ->
186
        let conj = match o with
187
          Tand -> true | Tor | Timplies -> false | Tiff -> assert false in
188
        let subf = fmla_flatten conj f in
189 190 191
        let subl = Array.length subf in
        for i = 0 to subl - 1 do
          let (f, s) = subf.(i) in
192
          subf.(i) <- (aux f, s);
193 194 195 196 197 198
        done;
        for i = 0 to subl - 1 do
          let do_subst t1 t2 =
            for j = 0 to subl - 1 do
              if j <> i then
                let (f, s) = subf.(j) in
199
                subf.(j) <- (t_replace t1 t2 f, s);
200 201
            done in
          let (f, s) = subf.(i) in
202 203
          match f.t_node with
          | Tapp (ls,[t1;t2]) when ls_equal ls ps_equ && s = conj ->
204 205 206 207
              if filter t1 t2 then do_subst t1 t2 else
              if filter t2 t1 then do_subst t2 t1
          | _ -> ()
        done;
208
        fmla_unflatten conj f subf
209
    | _ -> TermTF.t_map (fun t -> t) aux f in
210
  aux f