src/HOL/Complex/ex/mirtac.ML
author chaieb
Tue Jun 05 20:44:12 2007 +0200 (2007-06-05)
changeset 23264 324622260d29
child 23318 6d68b07ab5cf
permissions -rw-r--r--
Added twe Examples for Quantifier elimination ofer linear real arithmetic and over the mixed theory of linear real artihmetic with integers
chaieb@23264
     1
structure MirTac =
chaieb@23264
     2
struct
chaieb@23264
     3
chaieb@23264
     4
val trace = ref false;
chaieb@23264
     5
fun trace_msg s = if !trace then tracing s else ();
chaieb@23264
     6
chaieb@23264
     7
val mir_ss = 
chaieb@23264
     8
let val ths = map thm ["real_of_int_inject", "real_of_int_less_iff", "real_of_int_le_iff"]
chaieb@23264
     9
in @{simpset} delsimps ths addsimps (map (fn th => th RS sym) ths)
chaieb@23264
    10
end;
chaieb@23264
    11
chaieb@23264
    12
val nT = HOLogic.natT;
chaieb@23264
    13
  val nat_arith = map thm ["add_nat_number_of", "diff_nat_number_of", 
chaieb@23264
    14
                       "mult_nat_number_of", "eq_nat_number_of", "less_nat_number_of"];
chaieb@23264
    15
chaieb@23264
    16
  val comp_arith = (map thm ["Let_def", "if_False", "if_True", "add_0", 
chaieb@23264
    17
                 "add_Suc", "add_number_of_left", "mult_number_of_left", 
chaieb@23264
    18
                 "Suc_eq_add_numeral_1"])@
chaieb@23264
    19
                 (map (fn s => thm s RS sym) ["numeral_1_eq_1", "numeral_0_eq_0"])
chaieb@23264
    20
                 @ arith_simps@ nat_arith @ rel_simps 
chaieb@23264
    21
  val ths = [@{thm "mult_numeral_1"}, @{thm "mult_numeral_1_right"}, 
chaieb@23264
    22
             @{thm "real_of_nat_number_of"},
chaieb@23264
    23
             @{thm "real_of_nat_Suc"}, @{thm "real_of_nat_one"}, @{thm "real_of_one"},
chaieb@23264
    24
             @{thm "real_of_int_zero"}, @{thm "real_of_nat_zero"},
chaieb@23264
    25
             @{thm "Ring_and_Field.divide_zero"}, 
chaieb@23264
    26
             @{thm "divide_divide_eq_left"}, @{thm "times_divide_eq_right"}, 
chaieb@23264
    27
             @{thm "times_divide_eq_left"}, @{thm "divide_divide_eq_right"},
chaieb@23264
    28
             @{thm "diff_def"}, @{thm "minus_divide_left"}]
chaieb@23264
    29
val comp_ths = ths @ comp_arith @ simp_thms 
chaieb@23264
    30
chaieb@23264
    31
val powerarith = 
chaieb@23264
    32
    (map thm ["nat_number_of", "zpower_number_of_even", 
chaieb@23264
    33
	      "zpower_Pls", "zpower_Min"]) @ 
chaieb@23264
    34
    [thm "zpower_number_of_odd"]
chaieb@23264
    35
chaieb@23264
    36
val comp_arith = comp_ths @ powerarith @[thm"not_false_eq_true", thm "not_true_eq_false"];
chaieb@23264
    37
chaieb@23264
    38
chaieb@23264
    39
val zdvd_int = @{thm "zdvd_int"};
chaieb@23264
    40
val zdiff_int_split = @{thm "zdiff_int_split"};
chaieb@23264
    41
val all_nat = @{thm "all_nat"};
chaieb@23264
    42
val ex_nat = @{thm "ex_nat"};
chaieb@23264
    43
val number_of1 = @{thm "number_of1"};
chaieb@23264
    44
val number_of2 = @{thm "number_of2"};
chaieb@23264
    45
val split_zdiv = @{thm "split_zdiv"};
chaieb@23264
    46
val split_zmod = @{thm "split_zmod"};
chaieb@23264
    47
val mod_div_equality' = @{thm "mod_div_equality'"};
chaieb@23264
    48
val split_div' = @{thm "split_div'"};
chaieb@23264
    49
val Suc_plus1 = @{thm "Suc_plus1"};
chaieb@23264
    50
val imp_le_cong = @{thm "imp_le_cong"};
chaieb@23264
    51
val conj_le_cong = @{thm "conj_le_cong"};
chaieb@23264
    52
val nat_mod_add_eq = @{thm "mod_add1_eq"} RS sym;
chaieb@23264
    53
val nat_mod_add_left_eq = @{thm "mod_add_left_eq"} RS sym;
chaieb@23264
    54
val nat_mod_add_right_eq = @{thm "mod_add_right_eq"} RS sym;
chaieb@23264
    55
val int_mod_add_eq = @{thm "zmod_zadd1_eq"} RS sym;
chaieb@23264
    56
val int_mod_add_left_eq = @{thm "zmod_zadd_left_eq"} RS sym;
chaieb@23264
    57
val int_mod_add_right_eq = @{thm "zmod_zadd_right_eq"} RS sym;
chaieb@23264
    58
val nat_div_add_eq = @{thm "div_add1_eq"} RS sym;
chaieb@23264
    59
val int_div_add_eq = @{thm "zdiv_zadd1_eq"} RS sym;
chaieb@23264
    60
val ZDIVISION_BY_ZERO_MOD = @{thm "DIVISION_BY_ZERO"} RS conjunct2;
chaieb@23264
    61
val ZDIVISION_BY_ZERO_DIV = @{thm "DIVISION_BY_ZERO"} RS conjunct1;
chaieb@23264
    62
chaieb@23264
    63
fun prepare_for_mir sg q fm = 
chaieb@23264
    64
  let
chaieb@23264
    65
    val ps = Logic.strip_params fm
chaieb@23264
    66
    val hs = map HOLogic.dest_Trueprop (Logic.strip_assums_hyp fm)
chaieb@23264
    67
    val c = HOLogic.dest_Trueprop (Logic.strip_assums_concl fm)
chaieb@23264
    68
    fun mk_all ((s, T), (P,n)) =
chaieb@23264
    69
      if 0 mem loose_bnos P then
chaieb@23264
    70
        (HOLogic.all_const T $ Abs (s, T, P), n)
chaieb@23264
    71
      else (incr_boundvars ~1 P, n-1)
chaieb@23264
    72
    fun mk_all2 (v, t) = HOLogic.all_const (fastype_of v) $ lambda v t;
chaieb@23264
    73
      val rhs = hs
chaieb@23264
    74
(*    val (rhs,irhs) = List.partition (relevant (rev ps)) hs *)
chaieb@23264
    75
    val np = length ps
chaieb@23264
    76
    val (fm',np) =  foldr (fn ((x, T), (fm,n)) => mk_all ((x, T), (fm,n)))
chaieb@23264
    77
      (foldr HOLogic.mk_imp c rhs, np) ps
chaieb@23264
    78
    val (vs, _) = List.partition (fn t => q orelse (type_of t) = nT)
chaieb@23264
    79
      (term_frees fm' @ term_vars fm');
chaieb@23264
    80
    val fm2 = foldr mk_all2 fm' vs
chaieb@23264
    81
  in (fm2, np + length vs, length rhs) end;
chaieb@23264
    82
chaieb@23264
    83
(*Object quantifier to meta --*)
chaieb@23264
    84
fun spec_step n th = if (n=0) then th else (spec_step (n-1) th) RS spec ;
chaieb@23264
    85
chaieb@23264
    86
(* object implication to meta---*)
chaieb@23264
    87
fun mp_step n th = if (n=0) then th else (mp_step (n-1) th) RS mp;
chaieb@23264
    88
chaieb@23264
    89
chaieb@23264
    90
fun mir_tac ctxt q i = 
chaieb@23264
    91
    (ObjectLogic.atomize_tac i)
chaieb@23264
    92
	THEN (simp_tac (HOL_basic_ss addsimps [@{thm "abs_ge_zero"}] addsimps simp_thms) i)
chaieb@23264
    93
	THEN (REPEAT_DETERM (split_tac [@{thm "split_min"}, @{thm "split_max"},@{thm "abs_split"}] i))
chaieb@23264
    94
	THEN (fn st =>
chaieb@23264
    95
  let
chaieb@23264
    96
    val g = List.nth (prems_of st, i - 1)
chaieb@23264
    97
    val sg = ProofContext.theory_of ctxt
chaieb@23264
    98
    (* Transform the term*)
chaieb@23264
    99
    val (t,np,nh) = prepare_for_mir sg q g
chaieb@23264
   100
    (* Some simpsets for dealing with mod div abs and nat*)
chaieb@23264
   101
    val mod_div_simpset = HOL_basic_ss 
chaieb@23264
   102
			addsimps [refl,nat_mod_add_eq, 
chaieb@23264
   103
				  @{thm "mod_self"}, @{thm "zmod_self"},
chaieb@23264
   104
				  @{thm "zdiv_zero"},@{thm "zmod_zero"},@{thm "div_0"}, @{thm "mod_0"},
chaieb@23264
   105
				  @{thm "zdiv_1"}, @{thm "zmod_1"}, @{thm "div_1"}, @{thm "mod_1"},
chaieb@23264
   106
				  @{thm "Suc_plus1"}]
chaieb@23264
   107
			addsimps add_ac
chaieb@23264
   108
			addsimprocs [cancel_div_mod_proc]
chaieb@23264
   109
    val simpset0 = HOL_basic_ss
chaieb@23264
   110
      addsimps [mod_div_equality', Suc_plus1]
chaieb@23264
   111
      addsimps comp_arith
chaieb@23264
   112
      addsplits [@{thm "split_zdiv"}, @{thm "split_zmod"}, @{thm "split_div'"}, @{thm "split_min"}, @{thm "split_max"}]
chaieb@23264
   113
    (* Simp rules for changing (n::int) to int n *)
chaieb@23264
   114
    val simpset1 = HOL_basic_ss
chaieb@23264
   115
      addsimps [nat_number_of_def, zdvd_int] @ map (fn r => r RS sym)
chaieb@23264
   116
        [int_int_eq, zle_int, zless_int, zadd_int, zmult_int]
chaieb@23264
   117
      addsplits [zdiff_int_split]
chaieb@23264
   118
    (*simp rules for elimination of int n*)
chaieb@23264
   119
chaieb@23264
   120
    val simpset2 = HOL_basic_ss
chaieb@23264
   121
      addsimps [nat_0_le, all_nat, ex_nat, number_of1, number_of2, int_0, int_1]
chaieb@23264
   122
      addcongs [conj_le_cong, imp_le_cong]
chaieb@23264
   123
    (* simp rules for elimination of abs *)
chaieb@23264
   124
    val ct = cterm_of sg (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)]
chaieb@23264
   129
      (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
chaieb@23264
   133
        Const ("==>", _) $ (Const ("Trueprop", _) $ t1) $ _ =>
chaieb@23264
   134
    let val pth = 
chaieb@23264
   135
          (* If quick_and_dirty then run without proof generation as oracle*)
chaieb@23264
   136
             if !quick_and_dirty 
chaieb@23264
   137
             then mircfr_oracle sg (Pattern.eta_long [] t1)
chaieb@23264
   138
	     else mirlfr_oracle sg (Pattern.eta_long [] t1)
chaieb@23264
   139
    in 
chaieb@23264
   140
          (trace_msg ("calling procedure with term:\n" ^
chaieb@23264
   141
             Sign.string_of_term sg 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)
chaieb@23264
   146
  in (rtac (((mp_step nh) o (spec_step np)) th) i 
chaieb@23264
   147
      THEN tac) st
chaieb@23264
   148
  end handle Subscript => no_tac st | ReflectedMir.MIR => no_tac st);
chaieb@23264
   149
chaieb@23264
   150
fun mir_args meth =
chaieb@23264
   151
 let val parse_flag = 
chaieb@23264
   152
         Args.$$$ "no_quantify" >> (K (K false));
chaieb@23264
   153
 in
chaieb@23264
   154
   Method.simple_args 
chaieb@23264
   155
  (Scan.optional (Args.$$$ "(" |-- Scan.repeat1 parse_flag --| Args.$$$ ")") [] >>
chaieb@23264
   156
    curry (Library.foldl op |>) true)
chaieb@23264
   157
    (fn q => fn ctxt => meth ctxt q 1)
chaieb@23264
   158
  end;
chaieb@23264
   159
chaieb@23264
   160
fun mir_method ctxt q i = Method.METHOD (fn facts =>
chaieb@23264
   161
  Method.insert_tac facts 1 THEN mir_tac ctxt q i);
chaieb@23264
   162
chaieb@23264
   163
val setup =
chaieb@23264
   164
  Method.add_method ("mir",
chaieb@23264
   165
     mir_args mir_method,
chaieb@23264
   166
     "decision procedure for MIR arithmetic");
chaieb@23264
   167
chaieb@23264
   168
chaieb@23264
   169
end