eliminate_if.ml 4.23 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
(**************************************************************************)
(*                                                                        *)
(*  Copyright (C) 2010-                                                   *)
(*    Francois Bobot                                                      *)
(*    Jean-Christophe Filliatre                                           *)
(*    Johannes Kanig                                                      *)
(*    Andrei Paskevich                                                    *)
(*                                                                        *)
(*  This software is free software; you can redistribute it and/or        *)
(*  modify it under the terms of the GNU Library General Public           *)
(*  License version 2.1, with the special exception on linking            *)
(*  described in file LICENSE.                                            *)
(*                                                                        *)
(*  This software is distributed in the hope that it will be useful,      *)
(*  but WITHOUT ANY WARRANTY; without even the implied warranty of        *)
(*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                  *)
(*                                                                        *)
(**************************************************************************)

open Util
open Ident
open Term
open Decl

25
(** Eliminate if-then-else in terms *)
26

27
let rec elim_t letl contT t = match t.t_node with
28
  | Tlet (t1,tb) ->
29 30
      let u,t2,close = t_open_bound_cb tb in
      let cont_in t1 t2 = contT (t_let t1 (close u t2)) in
31 32
      let cont_let t1 = elim_t ((u,t1)::letl) (cont_in t1) t2 in
      elim_t letl cont_let t1
33
  | Tif (f,t1,t2) ->
34
      let f = elim_f (fun f -> f) f in
35
      let f = List.fold_left (fun f (v,t) -> f_let_close v t f) f letl in
36 37
      f_if f (elim_t letl contT t1) (elim_t letl contT t2)
  | Tcase _ ->
38
      Printer.unsupportedTerm t
39
        "cannot eliminate 'if-then-else' under 'match' in terms"
40
  | _ ->
41
      t_map_cont (elim_t letl) elim_f contT t
42

43
and elim_f contF f = match f.f_node with
44
  | Fapp _ | Flet _ | Fcase _ ->
45 46 47
      contF (f_map_cont (elim_t []) elim_f (fun f -> f) f)
  | _ -> f_map_cont elim_tr elim_f contF f

48
(* the only terms we can still meet are the terms in triggers *)
49
and elim_tr contT t = match t.t_node with
50
  | Tif _ ->
51
      Printer.unsupportedTerm t
52
        "cannot eliminate 'if-then-else' in trigger terms"
53
  | _ -> t_map_cont elim_tr elim_f contT t
54

55 56 57 58 59 60 61
let elim_f f = elim_f (fun f -> f) f

let rec elim_t t = t_map elim_t elim_f t

let rec has_if t = match t.t_node with
  | Tif _ -> true
  | _ -> t_any has_if ffalse t
62 63 64 65 66 67

let add_ld axl d = match d with
  | _, None -> axl, d
  | ls, Some ld ->
      let vl,e = open_ls_defn ld in
      begin match e with
68
        | Term t when has_if t ->
69
            let nm = ls.ls_name.id_string ^ "_def" in
70
            let pr = create_prsymbol (id_derive nm ls.ls_name) in
71
            let hd = t_app ls (List.map t_var vl) t.t_ty in
72
            let f = f_forall_close vl [[Term hd]] (elim_f (f_equ hd t)) in
73 74 75
            create_prop_decl Paxiom pr f :: axl, (ls, None)
        | _ ->
            axl, make_ls_defn ls vl (e_map elim_t elim_f e)
76 77
      end

78
let elim_d d = match d.d_node with
79 80 81 82 83
  | Dlogic l ->
      let axl, l = map_fold_left add_ld [] l in
      let d = create_logic_decl l in
      d :: List.rev axl
  | _ ->
84
      [decl_map (fun _ -> assert false) elim_f d]
85

86
let eliminate_if_term = Trans.decl elim_d None
87 88

(** Eliminate if-then-else in formulas *)
89

90
let rec elim_t t = t_map elim_t (elim_f true) t
91

92
and elim_f sign f = match f.f_node with
93
  | Fif (f1,f2,f3) ->
94 95 96 97
      let f1p = elim_f sign f1 in
      let f1n = elim_f (not sign) f1 in
      let f2 = elim_f sign f2 in
      let f3 = elim_f sign f3 in
98 99 100
      if sign then f_and (f_implies f1n f2) (f_or f1p f3)
              else f_or (f_and f1p f2) (f_and (f_not f1n) f3)
  | _ ->
101
      f_map_sign elim_t elim_f sign f
102

103
let eliminate_if_fmla = Trans.rewrite elim_t (elim_f true) None
104

105
let eliminate_if = Trans.compose eliminate_if_term eliminate_if_fmla
106 107

let () =
108 109 110
  Trans.register_transform "eliminate_if_term" eliminate_if_term;
  Trans.register_transform "eliminate_if_fmla" eliminate_if_fmla;
  Trans.register_transform "eliminate_if" eliminate_if
111