eliminate_if.ml 4.08 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 25 26
(**************************************************************************)
(*                                                                        *)
(*  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

(* eliminate if-then-else in terms *)

27
let rec elim_t case letl contT t = match t.t_node with
28 29 30 31 32 33
  | Tlet (t1,tb) ->
      let u,t2 = t_open_bound tb in
      let t_let e1 e2 =
        if e1 == t1 && e2 == t2 then t else t_let u e1 e2
      in
      let cont_in t1 t2 = contT (t_let t1 t2) in
34 35 36 37 38 39 40
      let cont_let t1 = elim_t case ((u,t1)::letl) (cont_in t1) t2 in
      elim_t case letl cont_let t1
  | Tcase _ ->
      t_map_cont (elim_t (Some t) letl) elim_f contT t
  | Tif _ when case <> None ->
      Trans.unsupportedExpression (Term (of_option case))
        "cannot eliminate 'if-then-else' under 'match' in terms"
41
  | Tif (f,t1,t2) ->
42
      let f = elim_f (fun f -> f) f in
43
      let f = List.fold_left (fun f (v,t) -> f_let v t f) f letl in
44
      f_if f (elim_t case letl contT t1) (elim_t case letl contT t2)
45
  | _ ->
46
      t_map_cont (elim_t case letl) elim_f contT t
47

48
and elim_f contF f = match f.f_node with
49
  | Fapp _ | Flet _ | Fcase _ ->
50
      contF (f_map_cont (elim_t None []) elim_f (fun f -> f) f)
51
  | _ ->
52
      f_map_cont (fun _ _ -> assert false) elim_f contF f
53

54 55 56 57 58 59 60
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
61 62 63 64 65 66

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
67
        | Term t when has_if t ->
68
            let f = elim_f (ls_defn_axiom ld) in
69 70 71 72 73
            let nm = ls.ls_name.id_short ^ "_def" in
            let pr = create_prsymbol (id_derive nm ls.ls_name) in
            create_prop_decl Paxiom pr f :: axl, (ls, None)
        | _ ->
            axl, make_ls_defn ls vl (e_map elim_t elim_f e)
74 75 76 77 78 79 80 81
      end

let remove_if_decl d = match d.d_node with
  | Dlogic l ->
      let axl, l = map_fold_left add_ld [] l in
      let d = create_logic_decl l in
      d :: List.rev axl
  | _ ->
82
      [decl_map (fun _ -> assert false) elim_f d]
83 84 85 86 87 88 89 90

let eliminate_if_term =
  Register.store (fun () -> Trans.decl remove_if_decl None)

let () = Register.register_transform "eliminate_if_term" eliminate_if_term

(* eliminate if-then-else in formulas *)

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

93
and elim_f sign f = match f.f_node with
94
  | Fif (f1,f2,f3) ->
95 96 97 98
      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
99 100 101
      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)
  | _ ->
102
      f_map_sign elim_t elim_f sign f
103 104

let eliminate_if_fmla =
105
  Register.store (fun () -> Trans.rewrite elim_t (elim_f true) None)
106 107 108

let () = Register.register_transform "eliminate_if_fmla" eliminate_if_fmla