src/HOL/ex/coopertac.ML
changeset 23274 f997514ad8f4
child 23318 6d68b07ab5cf
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/ex/coopertac.ML	Wed Jun 06 16:12:08 2007 +0200
     1.3 @@ -0,0 +1,167 @@
     1.4 +structure LinZTac =
     1.5 +struct
     1.6 +
     1.7 +val trace = ref false;
     1.8 +fun trace_msg s = if !trace then tracing s else ();
     1.9 +
    1.10 +val cooper_ss = @{simpset};
    1.11 +
    1.12 +val nT = HOLogic.natT;
    1.13 +val binarith = map thm
    1.14 +  ["Pls_0_eq", "Min_1_eq"];
    1.15 + val intarithrel = 
    1.16 +     (map thm ["int_eq_number_of_eq","int_neg_number_of_BIT", 
    1.17 +		"int_le_number_of_eq","int_iszero_number_of_0",
    1.18 +		"int_less_number_of_eq_neg"]) @
    1.19 +     (map (fn s => thm s RS thm "lift_bool") 
    1.20 +	  ["int_iszero_number_of_Pls","int_iszero_number_of_1",
    1.21 +	   "int_neg_number_of_Min"])@
    1.22 +     (map (fn s => thm s RS thm "nlift_bool") 
    1.23 +	  ["int_nonzero_number_of_Min","int_not_neg_number_of_Pls"]);
    1.24 +     
    1.25 +val intarith = map thm ["int_number_of_add_sym", "int_number_of_minus_sym",
    1.26 +			"int_number_of_diff_sym", "int_number_of_mult_sym"];
    1.27 +val natarith = map thm ["add_nat_number_of", "diff_nat_number_of",
    1.28 +			"mult_nat_number_of", "eq_nat_number_of",
    1.29 +			"less_nat_number_of"]
    1.30 +val powerarith = 
    1.31 +    (map thm ["nat_number_of", "zpower_number_of_even", 
    1.32 +	      "zpower_Pls", "zpower_Min"]) @ 
    1.33 +    [simplify (HOL_basic_ss addsimps [thm "zero_eq_Numeral0_nring", 
    1.34 +			   thm "one_eq_Numeral1_nring"])
    1.35 +  (thm "zpower_number_of_odd")]
    1.36 +
    1.37 +val comp_arith = binarith @ intarith @ intarithrel @ natarith 
    1.38 +	    @ powerarith @[thm"not_false_eq_true", thm "not_true_eq_false"];
    1.39 +
    1.40 +
    1.41 +val zdvd_int = thm "zdvd_int";
    1.42 +val zdiff_int_split = thm "zdiff_int_split";
    1.43 +val all_nat = thm "all_nat";
    1.44 +val ex_nat = thm "ex_nat";
    1.45 +val number_of1 = thm "number_of1";
    1.46 +val number_of2 = thm "number_of2";
    1.47 +val split_zdiv = thm "split_zdiv";
    1.48 +val split_zmod = thm "split_zmod";
    1.49 +val mod_div_equality' = thm "mod_div_equality'";
    1.50 +val split_div' = thm "split_div'";
    1.51 +val Suc_plus1 = thm "Suc_plus1";
    1.52 +val imp_le_cong = thm "imp_le_cong";
    1.53 +val conj_le_cong = thm "conj_le_cong";
    1.54 +val nat_mod_add_eq = mod_add1_eq RS sym;
    1.55 +val nat_mod_add_left_eq = mod_add_left_eq RS sym;
    1.56 +val nat_mod_add_right_eq = mod_add_right_eq RS sym;
    1.57 +val int_mod_add_eq = @{thm "zmod_zadd1_eq"} RS sym;
    1.58 +val int_mod_add_left_eq = @{thm "zmod_zadd_left_eq"} RS sym;
    1.59 +val int_mod_add_right_eq = @{thm "zmod_zadd_right_eq"} RS sym;
    1.60 +val nat_div_add_eq = @{thm "div_add1_eq"} RS sym;
    1.61 +val int_div_add_eq = @{thm "zdiv_zadd1_eq"} RS sym;
    1.62 +val ZDIVISION_BY_ZERO_MOD = @{thm "DIVISION_BY_ZERO"} RS conjunct2;
    1.63 +val ZDIVISION_BY_ZERO_DIV = @{thm "DIVISION_BY_ZERO"} RS conjunct1;
    1.64 +
    1.65 +(*
    1.66 +val fn_rews = List.concat (map thms ["allpairs.simps","iupt.simps","decr.simps", "decrnum.simps","disjuncts.simps","simpnum.simps", "simpfm.simps","numadd.simps","nummul.simps","numneg_def","numsub","simp_num_pair_def","not.simps","prep.simps","qelim.simps","minusinf.simps","plusinf.simps","rsplit0.simps","rlfm.simps","\\<Upsilon>.simps","\\<upsilon>.simps","linrqe_def", "ferrack_def", "Let_def", "numsub_def", "numneg_def","DJ_def", "imp_def", "evaldjf_def", "djf_def", "split_def", "eq_def", "disj_def", "simp_num_pair_def", "conj_def", "lt_def", "neq_def","gt_def"]);
    1.67 +*)
    1.68 +fun prepare_for_linz q fm = 
    1.69 +  let
    1.70 +    val ps = Logic.strip_params fm
    1.71 +    val hs = map HOLogic.dest_Trueprop (Logic.strip_assums_hyp fm)
    1.72 +    val c = HOLogic.dest_Trueprop (Logic.strip_assums_concl fm)
    1.73 +    fun mk_all ((s, T), (P,n)) =
    1.74 +      if 0 mem loose_bnos P then
    1.75 +        (HOLogic.all_const T $ Abs (s, T, P), n)
    1.76 +      else (incr_boundvars ~1 P, n-1)
    1.77 +    fun mk_all2 (v, t) = HOLogic.all_const (fastype_of v) $ lambda v t;
    1.78 +      val rhs = hs
    1.79 +(*    val (rhs,irhs) = List.partition (relevant (rev ps)) hs *)
    1.80 +    val np = length ps
    1.81 +    val (fm',np) =  foldr (fn ((x, T), (fm,n)) => mk_all ((x, T), (fm,n)))
    1.82 +      (foldr HOLogic.mk_imp c rhs, np) ps
    1.83 +    val (vs, _) = List.partition (fn t => q orelse (type_of t) = nT)
    1.84 +      (term_frees fm' @ term_vars fm');
    1.85 +    val fm2 = foldr mk_all2 fm' vs
    1.86 +  in (fm2, np + length vs, length rhs) end;
    1.87 +
    1.88 +(*Object quantifier to meta --*)
    1.89 +fun spec_step n th = if (n=0) then th else (spec_step (n-1) th) RS spec ;
    1.90 +
    1.91 +(* object implication to meta---*)
    1.92 +fun mp_step n th = if (n=0) then th else (mp_step (n-1) th) RS mp;
    1.93 +
    1.94 +
    1.95 +fun linz_tac ctxt q i = ObjectLogic.atomize_tac i THEN (fn st =>
    1.96 +  let
    1.97 +    val g = List.nth (prems_of st, i - 1)
    1.98 +    val thy = ProofContext.theory_of ctxt
    1.99 +    (* Transform the term*)
   1.100 +    val (t,np,nh) = prepare_for_linz q g
   1.101 +    (* Some simpsets for dealing with mod div abs and nat*)
   1.102 +    val mod_div_simpset = HOL_basic_ss 
   1.103 +			addsimps [refl,nat_mod_add_eq, nat_mod_add_left_eq, 
   1.104 +				  nat_mod_add_right_eq, int_mod_add_eq, 
   1.105 +				  int_mod_add_right_eq, int_mod_add_left_eq,
   1.106 +				  nat_div_add_eq, int_div_add_eq,
   1.107 +				  mod_self, @{thm "zmod_self"},
   1.108 +				  DIVISION_BY_ZERO_MOD,DIVISION_BY_ZERO_DIV,
   1.109 +				  ZDIVISION_BY_ZERO_MOD,ZDIVISION_BY_ZERO_DIV,
   1.110 +				  @{thm "zdiv_zero"}, @{thm "zmod_zero"}, @{thm "div_0"}, @{thm "mod_0"},
   1.111 +				  @{thm "zdiv_1"}, @{thm "zmod_1"}, @{thm "div_1"}, @{thm "mod_1"},
   1.112 +				  Suc_plus1]
   1.113 +			addsimps add_ac
   1.114 +			addsimprocs [cancel_div_mod_proc]
   1.115 +    val simpset0 = HOL_basic_ss
   1.116 +      addsimps [mod_div_equality', Suc_plus1]
   1.117 +      addsimps comp_arith
   1.118 +      addsplits [split_zdiv, split_zmod, split_div', @{thm "split_min"}, @{thm "split_max"}]
   1.119 +    (* Simp rules for changing (n::int) to int n *)
   1.120 +    val simpset1 = HOL_basic_ss
   1.121 +      addsimps [nat_number_of_def, zdvd_int] @ map (fn r => r RS sym)
   1.122 +        [int_int_eq, zle_int, zless_int, zadd_int, zmult_int]
   1.123 +      addsplits [zdiff_int_split]
   1.124 +    (*simp rules for elimination of int n*)
   1.125 +
   1.126 +    val simpset2 = HOL_basic_ss
   1.127 +      addsimps [nat_0_le, all_nat, ex_nat, number_of1, number_of2, int_0, int_1]
   1.128 +      addcongs [conj_le_cong, imp_le_cong]
   1.129 +    (* simp rules for elimination of abs *)
   1.130 +    val simpset3 = HOL_basic_ss addsplits [abs_split]
   1.131 +    val ct = cterm_of thy (HOLogic.mk_Trueprop t)
   1.132 +    (* Theorem for the nat --> int transformation *)
   1.133 +    val pre_thm = Seq.hd (EVERY
   1.134 +      [simp_tac mod_div_simpset 1, simp_tac simpset0 1,
   1.135 +       TRY (simp_tac simpset1 1), TRY (simp_tac simpset2 1),
   1.136 +       TRY (simp_tac simpset3 1), TRY (simp_tac cooper_ss 1)]
   1.137 +      (trivial ct))
   1.138 +    fun assm_tac i = REPEAT_DETERM_N nh (assume_tac i)
   1.139 +    (* The result of the quantifier elimination *)
   1.140 +    val (th, tac) = case (prop_of pre_thm) of
   1.141 +        Const ("==>", _) $ (Const ("Trueprop", _) $ t1) $ _ =>
   1.142 +    let val pth = linzqe_oracle thy (Pattern.eta_long [] t1)
   1.143 +    in 
   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 linz_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 linz_method ctxt q i = Method.METHOD (fn facts =>
   1.163 +  Method.insert_tac facts 1 THEN linz_tac ctxt q i);
   1.164 +
   1.165 +val setup =
   1.166 +  Method.add_method ("cooper",
   1.167 +     linz_args linz_method,
   1.168 +     "decision procedure for linear integer arithmetic");
   1.169 +
   1.170 +end
   1.171 \ No newline at end of file