src/HOL/Decision_Procs/mir_tac.ML
changeset 29823 0ab754d13ccd
parent 29788 1b80ebe713a4
child 29948 cdf12a1cb963
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/Decision_Procs/mir_tac.ML	Fri Feb 06 15:15:32 2009 +0100
     1.3 @@ -0,0 +1,168 @@
     1.4 +(*  Title:      HOL/Reflection/mir_tac.ML
     1.5 +    Author:     Amine Chaieb, TU Muenchen
     1.6 +*)
     1.7 +
     1.8 +structure Mir_Tac =
     1.9 +struct
    1.10 +
    1.11 +val trace = ref false;
    1.12 +fun trace_msg s = if !trace then tracing s else ();
    1.13 +
    1.14 +val mir_ss = 
    1.15 +let val ths = map thm ["real_of_int_inject", "real_of_int_less_iff", "real_of_int_le_iff"]
    1.16 +in @{simpset} delsimps ths addsimps (map (fn th => th RS sym) ths)
    1.17 +end;
    1.18 +
    1.19 +val nT = HOLogic.natT;
    1.20 +  val nat_arith = map thm ["add_nat_number_of", "diff_nat_number_of", 
    1.21 +                       "mult_nat_number_of", "eq_nat_number_of", "less_nat_number_of"];
    1.22 +
    1.23 +  val comp_arith = (map thm ["Let_def", "if_False", "if_True", "add_0", 
    1.24 +                 "add_Suc", "add_number_of_left", "mult_number_of_left", 
    1.25 +                 "Suc_eq_add_numeral_1"])@
    1.26 +                 (map (fn s => thm s RS sym) ["numeral_1_eq_1", "numeral_0_eq_0"])
    1.27 +                 @ @{thms arith_simps} @ nat_arith @ @{thms rel_simps} 
    1.28 +  val ths = [@{thm "mult_numeral_1"}, @{thm "mult_numeral_1_right"}, 
    1.29 +             @{thm "real_of_nat_number_of"},
    1.30 +             @{thm "real_of_nat_Suc"}, @{thm "real_of_nat_one"}, @{thm "real_of_one"},
    1.31 +             @{thm "real_of_int_zero"}, @{thm "real_of_nat_zero"},
    1.32 +             @{thm "Ring_and_Field.divide_zero"}, 
    1.33 +             @{thm "divide_divide_eq_left"}, @{thm "times_divide_eq_right"}, 
    1.34 +             @{thm "times_divide_eq_left"}, @{thm "divide_divide_eq_right"},
    1.35 +             @{thm "diff_def"}, @{thm "minus_divide_left"}]
    1.36 +val comp_ths = ths @ comp_arith @ simp_thms 
    1.37 +
    1.38 +
    1.39 +val zdvd_int = @{thm "zdvd_int"};
    1.40 +val zdiff_int_split = @{thm "zdiff_int_split"};
    1.41 +val all_nat = @{thm "all_nat"};
    1.42 +val ex_nat = @{thm "ex_nat"};
    1.43 +val number_of1 = @{thm "number_of1"};
    1.44 +val number_of2 = @{thm "number_of2"};
    1.45 +val split_zdiv = @{thm "split_zdiv"};
    1.46 +val split_zmod = @{thm "split_zmod"};
    1.47 +val mod_div_equality' = @{thm "mod_div_equality'"};
    1.48 +val split_div' = @{thm "split_div'"};
    1.49 +val Suc_plus1 = @{thm "Suc_plus1"};
    1.50 +val imp_le_cong = @{thm "imp_le_cong"};
    1.51 +val conj_le_cong = @{thm "conj_le_cong"};
    1.52 +val nat_mod_add_eq = @{thm "mod_add1_eq"} RS sym;
    1.53 +val nat_mod_add_left_eq = @{thm "mod_add_left_eq"} RS sym;
    1.54 +val nat_mod_add_right_eq = @{thm "mod_add_right_eq"} RS sym;
    1.55 +val int_mod_add_eq = @{thm "zmod_zadd1_eq"} RS sym;
    1.56 +val int_mod_add_left_eq = @{thm "zmod_zadd_left_eq"} RS sym;
    1.57 +val int_mod_add_right_eq = @{thm "zmod_zadd_right_eq"} RS sym;
    1.58 +val nat_div_add_eq = @{thm "div_add1_eq"} RS sym;
    1.59 +val int_div_add_eq = @{thm "zdiv_zadd1_eq"} RS sym;
    1.60 +val ZDIVISION_BY_ZERO_MOD = @{thm "DIVISION_BY_ZERO"} RS conjunct2;
    1.61 +val ZDIVISION_BY_ZERO_DIV = @{thm "DIVISION_BY_ZERO"} RS conjunct1;
    1.62 +
    1.63 +fun prepare_for_mir thy q fm = 
    1.64 +  let
    1.65 +    val ps = Logic.strip_params fm
    1.66 +    val hs = map HOLogic.dest_Trueprop (Logic.strip_assums_hyp fm)
    1.67 +    val c = HOLogic.dest_Trueprop (Logic.strip_assums_concl fm)
    1.68 +    fun mk_all ((s, T), (P,n)) =
    1.69 +      if 0 mem loose_bnos P then
    1.70 +        (HOLogic.all_const T $ Abs (s, T, P), n)
    1.71 +      else (incr_boundvars ~1 P, n-1)
    1.72 +    fun mk_all2 (v, t) = HOLogic.all_const (fastype_of v) $ lambda v t;
    1.73 +      val rhs = hs
    1.74 +(*    val (rhs,irhs) = List.partition (relevant (rev ps)) hs *)
    1.75 +    val np = length ps
    1.76 +    val (fm',np) =  foldr (fn ((x, T), (fm,n)) => mk_all ((x, T), (fm,n)))
    1.77 +      (foldr HOLogic.mk_imp c rhs, np) ps
    1.78 +    val (vs, _) = List.partition (fn t => q orelse (type_of t) = nT)
    1.79 +      (OldTerm.term_frees fm' @ OldTerm.term_vars fm');
    1.80 +    val fm2 = foldr mk_all2 fm' vs
    1.81 +  in (fm2, np + length vs, length rhs) end;
    1.82 +
    1.83 +(*Object quantifier to meta --*)
    1.84 +fun spec_step n th = if (n=0) then th else (spec_step (n-1) th) RS spec ;
    1.85 +
    1.86 +(* object implication to meta---*)
    1.87 +fun mp_step n th = if (n=0) then th else (mp_step (n-1) th) RS mp;
    1.88 +
    1.89 +
    1.90 +fun mir_tac ctxt q i = 
    1.91 +    (ObjectLogic.atomize_prems_tac i)
    1.92 +        THEN (simp_tac (HOL_basic_ss addsimps [@{thm "abs_ge_zero"}] addsimps simp_thms) i)
    1.93 +        THEN (REPEAT_DETERM (split_tac [@{thm "split_min"}, @{thm "split_max"},@{thm "abs_split"}] i))
    1.94 +        THEN (fn st =>
    1.95 +  let
    1.96 +    val g = List.nth (prems_of st, i - 1)
    1.97 +    val thy = ProofContext.theory_of ctxt
    1.98 +    (* Transform the term*)
    1.99 +    val (t,np,nh) = prepare_for_mir thy q g
   1.100 +    (* Some simpsets for dealing with mod div abs and nat*)
   1.101 +    val mod_div_simpset = HOL_basic_ss 
   1.102 +                        addsimps [refl,nat_mod_add_eq, 
   1.103 +                                  @{thm "mod_self"}, @{thm "zmod_self"},
   1.104 +                                  @{thm "zdiv_zero"},@{thm "zmod_zero"},@{thm "div_0"}, @{thm "mod_0"},
   1.105 +                                  @{thm "zdiv_1"}, @{thm "zmod_1"}, @{thm "div_1"}, @{thm "mod_1"},
   1.106 +                                  @{thm "Suc_plus1"}]
   1.107 +                        addsimps @{thms add_ac}
   1.108 +                        addsimprocs [cancel_div_mod_proc]
   1.109 +    val simpset0 = HOL_basic_ss
   1.110 +      addsimps [mod_div_equality', Suc_plus1]
   1.111 +      addsimps comp_ths
   1.112 +      addsplits [@{thm "split_zdiv"}, @{thm "split_zmod"}, @{thm "split_div'"}, @{thm "split_min"}, @{thm "split_max"}]
   1.113 +    (* Simp rules for changing (n::int) to int n *)
   1.114 +    val simpset1 = HOL_basic_ss
   1.115 +      addsimps [@{thm "nat_number_of_def"}, @{thm "zdvd_int"}] @ map (fn r => r RS sym)
   1.116 +        [@{thm "int_int_eq"}, @{thm "zle_int"}, @{thm "zless_int"}, @{thm "zadd_int"}, 
   1.117 +         @{thm "zmult_int"}]
   1.118 +      addsplits [@{thm "zdiff_int_split"}]
   1.119 +    (*simp rules for elimination of int n*)
   1.120 +
   1.121 +    val simpset2 = HOL_basic_ss
   1.122 +      addsimps [@{thm "nat_0_le"}, @{thm "all_nat"}, @{thm "ex_nat"}, @{thm "number_of1"}, 
   1.123 +                @{thm "number_of2"}, @{thm "int_0"}, @{thm "int_1"}]
   1.124 +      addcongs [@{thm "conj_le_cong"}, @{thm "imp_le_cong"}]
   1.125 +    (* simp rules for elimination of abs *)
   1.126 +    val ct = cterm_of thy (HOLogic.mk_Trueprop t)
   1.127 +    (* Theorem for the nat --> int transformation *)
   1.128 +    val pre_thm = Seq.hd (EVERY
   1.129 +      [simp_tac mod_div_simpset 1, simp_tac simpset0 1,
   1.130 +       TRY (simp_tac simpset1 1), TRY (simp_tac simpset2 1), TRY (simp_tac mir_ss 1)]
   1.131 +      (trivial ct))
   1.132 +    fun assm_tac i = REPEAT_DETERM_N nh (assume_tac i)
   1.133 +    (* The result of the quantifier elimination *)
   1.134 +    val (th, tac) = case (prop_of pre_thm) of
   1.135 +        Const ("==>", _) $ (Const ("Trueprop", _) $ t1) $ _ =>
   1.136 +    let val pth =
   1.137 +          (* If quick_and_dirty then run without proof generation as oracle*)
   1.138 +             if !quick_and_dirty
   1.139 +             then mirfr_oracle (false, cterm_of thy (Pattern.eta_long [] t1))
   1.140 +             else mirfr_oracle (true, cterm_of thy (Pattern.eta_long [] t1))
   1.141 +    in 
   1.142 +          (trace_msg ("calling procedure with term:\n" ^
   1.143 +             Syntax.string_of_term ctxt t1);
   1.144 +           ((pth RS iffD2) RS pre_thm,
   1.145 +            assm_tac (i + 1) THEN (if q then I else TRY) (rtac TrueI i)))
   1.146 +    end
   1.147 +      | _ => (pre_thm, assm_tac i)
   1.148 +  in (rtac (((mp_step nh) o (spec_step np)) th) i 
   1.149 +      THEN tac) st
   1.150 +  end handle Subscript => no_tac st);
   1.151 +
   1.152 +fun mir_args meth =
   1.153 + let val parse_flag = 
   1.154 +         Args.$$$ "no_quantify" >> (K (K false));
   1.155 + in
   1.156 +   Method.simple_args 
   1.157 +  (Scan.optional (Args.$$$ "(" |-- Scan.repeat1 parse_flag --| Args.$$$ ")") [] >>
   1.158 +    curry (Library.foldl op |>) true)
   1.159 +    (fn q => fn ctxt => meth ctxt q 1)
   1.160 +  end;
   1.161 +
   1.162 +fun mir_method ctxt q i = Method.METHOD (fn facts =>
   1.163 +  Method.insert_tac facts 1 THEN mir_tac ctxt q i);
   1.164 +
   1.165 +val setup =
   1.166 +  Method.add_method ("mir",
   1.167 +     mir_args mir_method,
   1.168 +     "decision procedure for MIR arithmetic");
   1.169 +
   1.170 +
   1.171 +end