src/HOL/Decision_Procs/mir_tac.ML
author huffman
Sun Mar 25 20:15:39 2012 +0200 (2012-03-25 ago)
changeset 47108 2a1953f0d20d
parent 45654 cf10bde35973
child 47142 d64fa2ca54b8
permissions -rw-r--r--
merged fork with new numeral representation (see NEWS)
hoelzl@30439
     1
(*  Title:      HOL/Decision_Procs/mir_tac.ML
haftmann@23858
     2
    Author:     Amine Chaieb, TU Muenchen
haftmann@23858
     3
*)
haftmann@23858
     4
wenzelm@31240
     5
signature MIR_TAC =
wenzelm@31240
     6
sig
wenzelm@32740
     7
  val trace: bool Unsynchronized.ref
wenzelm@31240
     8
  val mir_tac: Proof.context -> bool -> int -> tactic
wenzelm@31240
     9
  val setup: theory -> theory
wenzelm@31240
    10
end
wenzelm@31240
    11
haftmann@29788
    12
structure Mir_Tac =
chaieb@23264
    13
struct
chaieb@23264
    14
wenzelm@32740
    15
val trace = Unsynchronized.ref false;
chaieb@23264
    16
fun trace_msg s = if !trace then tracing s else ();
chaieb@23264
    17
chaieb@23264
    18
val mir_ss = 
wenzelm@39159
    19
let val ths = [@{thm "real_of_int_inject"}, @{thm "real_of_int_less_iff"}, @{thm "real_of_int_le_iff"}]
chaieb@23264
    20
in @{simpset} delsimps ths addsimps (map (fn th => th RS sym) ths)
chaieb@23264
    21
end;
chaieb@23264
    22
chaieb@23264
    23
val nT = HOLogic.natT;
huffman@47108
    24
  val nat_arith = [@{thm diff_nat_numeral}];
chaieb@23264
    25
wenzelm@39159
    26
  val comp_arith = [@{thm "Let_def"}, @{thm "if_False"}, @{thm "if_True"}, @{thm "add_0"},
huffman@47108
    27
                 @{thm "add_Suc"}, @{thm add_numeral_left}, @{thm mult_numeral_left(1)},
wenzelm@39159
    28
                 @{thm "Suc_eq_plus1"}] @
huffman@47108
    29
                 (map (fn th => th RS sym) [@{thm "numeral_1_eq_1"}])
haftmann@25481
    30
                 @ @{thms arith_simps} @ nat_arith @ @{thms rel_simps} 
chaieb@23264
    31
  val ths = [@{thm "mult_numeral_1"}, @{thm "mult_numeral_1_right"}, 
huffman@47108
    32
             @{thm real_of_nat_numeral},
chaieb@23264
    33
             @{thm "real_of_nat_Suc"}, @{thm "real_of_nat_one"}, @{thm "real_of_one"},
chaieb@23264
    34
             @{thm "real_of_int_zero"}, @{thm "real_of_nat_zero"},
haftmann@36308
    35
             @{thm "divide_zero"}, 
chaieb@23264
    36
             @{thm "divide_divide_eq_left"}, @{thm "times_divide_eq_right"}, 
chaieb@23264
    37
             @{thm "times_divide_eq_left"}, @{thm "divide_divide_eq_right"},
haftmann@37887
    38
             @{thm "diff_minus"}, @{thm "minus_divide_left"}]
wenzelm@45654
    39
val comp_ths = ths @ comp_arith @ @{thms simp_thms};
chaieb@23264
    40
chaieb@23264
    41
chaieb@23264
    42
val zdvd_int = @{thm "zdvd_int"};
chaieb@23264
    43
val zdiff_int_split = @{thm "zdiff_int_split"};
chaieb@23264
    44
val all_nat = @{thm "all_nat"};
chaieb@23264
    45
val ex_nat = @{thm "ex_nat"};
chaieb@23264
    46
val split_zdiv = @{thm "split_zdiv"};
chaieb@23264
    47
val split_zmod = @{thm "split_zmod"};
chaieb@23264
    48
val mod_div_equality' = @{thm "mod_div_equality'"};
chaieb@23264
    49
val split_div' = @{thm "split_div'"};
chaieb@23264
    50
val imp_le_cong = @{thm "imp_le_cong"};
chaieb@23264
    51
val conj_le_cong = @{thm "conj_le_cong"};
nipkow@30224
    52
val mod_add_eq = @{thm "mod_add_eq"} RS sym;
nipkow@30034
    53
val mod_add_left_eq = @{thm "mod_add_left_eq"} RS sym;
nipkow@30034
    54
val mod_add_right_eq = @{thm "mod_add_right_eq"} RS sym;
chaieb@23264
    55
val nat_div_add_eq = @{thm "div_add1_eq"} RS sym;
chaieb@23264
    56
val int_div_add_eq = @{thm "zdiv_zadd1_eq"} RS sym;
chaieb@23264
    57
val ZDIVISION_BY_ZERO_MOD = @{thm "DIVISION_BY_ZERO"} RS conjunct2;
chaieb@23264
    58
val ZDIVISION_BY_ZERO_DIV = @{thm "DIVISION_BY_ZERO"} RS conjunct1;
chaieb@23264
    59
haftmann@27456
    60
fun prepare_for_mir thy q fm = 
chaieb@23264
    61
  let
chaieb@23264
    62
    val ps = Logic.strip_params fm
chaieb@23264
    63
    val hs = map HOLogic.dest_Trueprop (Logic.strip_assums_hyp fm)
chaieb@23264
    64
    val c = HOLogic.dest_Trueprop (Logic.strip_assums_concl fm)
chaieb@23264
    65
    fun mk_all ((s, T), (P,n)) =
wenzelm@42083
    66
      if Term.is_dependent P then
chaieb@23264
    67
        (HOLogic.all_const T $ Abs (s, T, P), n)
chaieb@23264
    68
      else (incr_boundvars ~1 P, n-1)
chaieb@23264
    69
    fun mk_all2 (v, t) = HOLogic.all_const (fastype_of v) $ lambda v t;
chaieb@23264
    70
      val rhs = hs
chaieb@23264
    71
(*    val (rhs,irhs) = List.partition (relevant (rev ps)) hs *)
chaieb@23264
    72
    val np = length ps
wenzelm@33004
    73
    val (fm',np) = List.foldr (fn ((x, T), (fm,n)) => mk_all ((x, T), (fm,n)))
wenzelm@33004
    74
      (List.foldr HOLogic.mk_imp c rhs, np) ps
chaieb@23264
    75
    val (vs, _) = List.partition (fn t => q orelse (type_of t) = nT)
wenzelm@44121
    76
      (Misc_Legacy.term_frees fm' @ Misc_Legacy.term_vars fm');
wenzelm@33004
    77
    val fm2 = List.foldr mk_all2 fm' vs
chaieb@23264
    78
  in (fm2, np + length vs, length rhs) end;
chaieb@23264
    79
chaieb@23264
    80
(*Object quantifier to meta --*)
chaieb@23264
    81
fun spec_step n th = if (n=0) then th else (spec_step (n-1) th) RS spec ;
chaieb@23264
    82
chaieb@23264
    83
(* object implication to meta---*)
chaieb@23264
    84
fun mp_step n th = if (n=0) then th else (mp_step (n-1) th) RS mp;
chaieb@23264
    85
chaieb@23264
    86
wenzelm@42368
    87
fun mir_tac ctxt q = 
wenzelm@42368
    88
    Object_Logic.atomize_prems_tac
wenzelm@45654
    89
        THEN' simp_tac (HOL_basic_ss addsimps [@{thm "abs_ge_zero"}] addsimps @{thms simp_thms})
wenzelm@42368
    90
        THEN' (REPEAT_DETERM o split_tac [@{thm "split_min"}, @{thm "split_max"}, @{thm "abs_split"}])
wenzelm@42368
    91
        THEN' SUBGOAL (fn (g, i) =>
chaieb@23264
    92
  let
wenzelm@42361
    93
    val thy = Proof_Context.theory_of ctxt
chaieb@23264
    94
    (* Transform the term*)
haftmann@27456
    95
    val (t,np,nh) = prepare_for_mir thy q g
chaieb@23264
    96
    (* Some simpsets for dealing with mod div abs and nat*)
chaieb@23264
    97
    val mod_div_simpset = HOL_basic_ss 
nipkow@30224
    98
                        addsimps [refl, mod_add_eq, 
wenzelm@28290
    99
                                  @{thm "mod_self"}, @{thm "zmod_self"},
wenzelm@28290
   100
                                  @{thm "zdiv_zero"},@{thm "zmod_zero"},@{thm "div_0"}, @{thm "mod_0"},
nipkow@30031
   101
                                  @{thm "div_by_1"}, @{thm "mod_by_1"}, @{thm "div_1"}, @{thm "mod_1"},
nipkow@31790
   102
                                  @{thm "Suc_eq_plus1"}]
wenzelm@28290
   103
                        addsimps @{thms add_ac}
wenzelm@43594
   104
                        addsimprocs [@{simproc cancel_div_mod_nat}, @{simproc cancel_div_mod_int}]
chaieb@23264
   105
    val simpset0 = HOL_basic_ss
nipkow@31790
   106
      addsimps [mod_div_equality', @{thm Suc_eq_plus1}]
chaieb@23318
   107
      addsimps comp_ths
wenzelm@45620
   108
      |> fold Splitter.add_split
wenzelm@45620
   109
          [@{thm "split_zdiv"}, @{thm "split_zmod"}, @{thm "split_div'"},
wenzelm@45620
   110
            @{thm "split_min"}, @{thm "split_max"}]
chaieb@23264
   111
    (* Simp rules for changing (n::int) to int n *)
chaieb@23264
   112
    val simpset1 = HOL_basic_ss
huffman@47108
   113
      addsimps [@{thm "zdvd_int"}] @ map (fn r => r RS sym)
chaieb@23381
   114
        [@{thm "int_int_eq"}, @{thm "zle_int"}, @{thm "zless_int"}, @{thm "zadd_int"}, 
huffman@47108
   115
         @{thm nat_numeral}, @{thm "zmult_int"}]
wenzelm@45620
   116
      |> Splitter.add_split @{thm "zdiff_int_split"}
chaieb@23264
   117
    (*simp rules for elimination of int n*)
chaieb@23264
   118
chaieb@23264
   119
    val simpset2 = HOL_basic_ss
huffman@47108
   120
      addsimps [@{thm "nat_0_le"}, @{thm "all_nat"}, @{thm "ex_nat"}, @{thm zero_le_numeral}, 
huffman@47108
   121
                @{thm "int_0"}, @{thm "int_1"}]
wenzelm@45620
   122
      |> fold Simplifier.add_cong [@{thm "conj_le_cong"}, @{thm "imp_le_cong"}]
chaieb@23264
   123
    (* simp rules for elimination of abs *)
haftmann@27456
   124
    val ct = cterm_of thy (HOLogic.mk_Trueprop t)
chaieb@23264
   125
    (* Theorem for the nat --> int transformation *)
chaieb@23264
   126
    val pre_thm = Seq.hd (EVERY
chaieb@23264
   127
      [simp_tac mod_div_simpset 1, simp_tac simpset0 1,
chaieb@23264
   128
       TRY (simp_tac simpset1 1), TRY (simp_tac simpset2 1), TRY (simp_tac mir_ss 1)]
wenzelm@36945
   129
      (Thm.trivial ct))
chaieb@23264
   130
    fun assm_tac i = REPEAT_DETERM_N nh (assume_tac i)
chaieb@23264
   131
    (* The result of the quantifier elimination *)
chaieb@23264
   132
    val (th, tac) = case (prop_of pre_thm) of
haftmann@38558
   133
        Const ("==>", _) $ (Const (@{const_name Trueprop}, _) $ t1) $ _ =>
wenzelm@28290
   134
    let val pth =
chaieb@23264
   135
          (* If quick_and_dirty then run without proof generation as oracle*)
wenzelm@28290
   136
             if !quick_and_dirty
wenzelm@28290
   137
             then mirfr_oracle (false, cterm_of thy (Pattern.eta_long [] t1))
wenzelm@28290
   138
             else mirfr_oracle (true, cterm_of thy (Pattern.eta_long [] t1))
chaieb@23264
   139
    in 
chaieb@23264
   140
          (trace_msg ("calling procedure with term:\n" ^
wenzelm@26939
   141
             Syntax.string_of_term ctxt t1);
chaieb@23264
   142
           ((pth RS iffD2) RS pre_thm,
chaieb@23264
   143
            assm_tac (i + 1) THEN (if q then I else TRY) (rtac TrueI i)))
chaieb@23264
   144
    end
chaieb@23264
   145
      | _ => (pre_thm, assm_tac i)
wenzelm@42368
   146
  in rtac (((mp_step nh) o (spec_step np)) th) i THEN tac end);
chaieb@23264
   147
chaieb@23264
   148
val setup =
wenzelm@31240
   149
  Method.setup @{binding mir}
wenzelm@31240
   150
    let
wenzelm@31240
   151
      val parse_flag = Args.$$$ "no_quantify" >> K (K false)
wenzelm@31240
   152
    in
wenzelm@31240
   153
      Scan.lift (Scan.optional (Args.$$$ "(" |-- Scan.repeat1 parse_flag --| Args.$$$ ")") [] >>
wenzelm@31240
   154
        curry (Library.foldl op |>) true) >>
wenzelm@31240
   155
      (fn q => fn ctxt => SIMPLE_METHOD' (mir_tac ctxt q))
wenzelm@31240
   156
    end
wenzelm@31240
   157
    "decision procedure for MIR arithmetic";
chaieb@23264
   158
wenzelm@23590
   159
end