src/HOL/Real/ferrante_rackoff.ML
changeset 21078 101aefd61aac
parent 20485 3078fd2eec7b
child 21588 cd0dc678a205
equal deleted inserted replaced
21077:d6c141871b29 21078:101aefd61aac
     3     Author:     Amine Chaieb, TU Muenchen
     3     Author:     Amine Chaieb, TU Muenchen
     4 
     4 
     5 Ferrante and Rackoff Algorithm.
     5 Ferrante and Rackoff Algorithm.
     6 *)
     6 *)
     7 
     7 
     8 structure Ferrante_Rackoff:
     8 signature FERRANTE_RACKOFF =
     9 sig
     9 sig
    10   val trace : bool ref
    10   val ferrack_tac: bool -> int -> tactic
    11   val ferrack_tac : bool -> int -> tactic
    11   val setup: theory -> theory
    12   val setup : theory -> theory
    12   val trace: bool ref
    13 end =
    13 end;
       
    14 
       
    15 structure Ferrante_Rackoff : FERRANTE_RACKOFF =
    14 struct
    16 struct
    15 
    17 
    16 val trace = ref false;
    18 val trace = ref false;
    17 fun trace_msg s = if !trace then tracing s else ();
    19 fun trace_msg s = if !trace then tracing s else ();
    18 
    20 
    19 val context_ss = simpset_of (the_context ());
    21 val binarith = map thm ["Pls_0_eq", "Min_1_eq", "pred_Pls", "pred_Min","pred_1","pred_0",
       
    22   "succ_Pls", "succ_Min", "succ_1", "succ_0",
       
    23   "add_Pls", "add_Min", "add_BIT_0", "add_BIT_10", "add_BIT_11",
       
    24   "minus_Pls", "minus_Min", "minus_1", "minus_0",
       
    25   "mult_Pls", "mult_Min", "mult_1", "mult_0", 
       
    26   "add_Pls_right", "add_Min_right"];
    20 
    27 
    21 val nT = HOLogic.natT;
    28 val intarithrel = 
    22 val binarith = map thm
    29   map thm ["int_eq_number_of_eq", "int_neg_number_of_BIT", "int_le_number_of_eq",
    23   ["Pls_0_eq", "Min_1_eq",
    30     "int_iszero_number_of_0", "int_less_number_of_eq_neg"]
    24  "pred_Pls","pred_Min","pred_1","pred_0",
    31   @ map (fn s => thm s RS thm "lift_bool") ["int_iszero_number_of_Pls",
    25   "succ_Pls", "succ_Min", "succ_1", "succ_0",
    32     "int_iszero_number_of_1", "int_neg_number_of_Min"]
    26   "add_Pls", "add_Min", "add_BIT_0", "add_BIT_10",
    33   @ map (fn s => thm s RS thm "nlift_bool") ["int_nonzero_number_of_Min",
    27   "add_BIT_11", "minus_Pls", "minus_Min", "minus_1", 
    34     "int_not_neg_number_of_Pls"];
    28   "minus_0", "mult_Pls", "mult_Min", "mult_1", "mult_0", 
    35  
    29   "add_Pls_right", "add_Min_right"];
       
    30  val intarithrel = 
       
    31      (map thm ["int_eq_number_of_eq","int_neg_number_of_BIT", 
       
    32 		"int_le_number_of_eq","int_iszero_number_of_0",
       
    33 		"int_less_number_of_eq_neg"]) @
       
    34      (map (fn s => thm s RS thm "lift_bool") 
       
    35 	  ["int_iszero_number_of_Pls","int_iszero_number_of_1",
       
    36 	   "int_neg_number_of_Min"])@
       
    37      (map (fn s => thm s RS thm "nlift_bool") 
       
    38 	  ["int_nonzero_number_of_Min","int_not_neg_number_of_Pls"]);
       
    39      
       
    40 val intarith = map thm ["int_number_of_add_sym", "int_number_of_minus_sym",
    36 val intarith = map thm ["int_number_of_add_sym", "int_number_of_minus_sym",
    41 			"int_number_of_diff_sym", "int_number_of_mult_sym"];
    37   "int_number_of_diff_sym", "int_number_of_mult_sym"];
       
    38 
    42 val natarith = map thm ["add_nat_number_of", "diff_nat_number_of",
    39 val natarith = map thm ["add_nat_number_of", "diff_nat_number_of",
    43 			"mult_nat_number_of", "eq_nat_number_of",
    40   "mult_nat_number_of", "eq_nat_number_of", "less_nat_number_of"];
    44 			"less_nat_number_of"]
    41 
    45 val powerarith = 
    42 val powerarith =
    46     (map thm ["nat_number_of", "zpower_number_of_even", 
    43   map thm ["nat_number_of", "zpower_number_of_even",
    47 	      "zpower_Pls", "zpower_Min"]) @ 
    44   "zpower_Pls", "zpower_Min"]
    48     [(Tactic.simplify true [thm "zero_eq_Numeral0_nring", 
    45   @ [Tactic.simplify true [thm "zero_eq_Numeral0_nring", thm "one_eq_Numeral1_nring"]
    49 			   thm "one_eq_Numeral1_nring"] 
    46     (thm "zpower_number_of_odd")]
    50   (thm "zpower_number_of_odd"))]
       
    51 
    47 
    52 val comp_arith = binarith @ intarith @ intarithrel @ natarith 
    48 val comp_arith = binarith @ intarith @ intarithrel @ natarith 
    53 	    @ powerarith @[thm"not_false_eq_true", thm "not_true_eq_false"];
    49   @ powerarith @ [thm "not_false_eq_true", thm "not_true_eq_false"];
    54 
    50 
    55 fun prepare_for_linr sg q fm = 
    51 fun prepare_for_linr q fm = 
    56   let
    52   let
    57     val ps = Logic.strip_params fm
    53     val ps = Logic.strip_params fm
    58     val hs = map HOLogic.dest_Trueprop (Logic.strip_assums_hyp fm)
    54     val hs = map HOLogic.dest_Trueprop (Logic.strip_assums_hyp fm)
    59     val c = HOLogic.dest_Trueprop (Logic.strip_assums_concl fm)
    55     val c = HOLogic.dest_Trueprop (Logic.strip_assums_concl fm)
    60     fun mk_all ((s, T), (P,n)) =
    56     fun mk_all ((s, T), (P, n)) =
    61       if 0 mem loose_bnos P then
    57       if 0 mem loose_bnos P then
    62         (HOLogic.all_const T $ Abs (s, T, P), n)
    58         (HOLogic.all_const T $ Abs (s, T, P), n)
    63       else (incr_boundvars ~1 P, n-1)
    59       else (incr_boundvars ~1 P, n-1)
    64     fun mk_all2 (v, t) = HOLogic.all_const (fastype_of v) $ lambda v t;
    60     fun mk_all2 (v, t) = HOLogic.all_const (fastype_of v) $ lambda v t;
    65       val rhs = hs
    61     val rhs = hs;
    66 (*    val (rhs,irhs) = List.partition (relevant (rev ps)) hs *)
    62     val np = length ps;
    67     val np = length ps
    63     val (fm', np) = Library.foldr mk_all (ps, (Library.foldr HOLogic.mk_imp (rhs, c), np));
    68     val (fm',np) =  foldr (fn ((x, T), (fm,n)) => mk_all ((x, T), (fm,n)))
    64     val (vs, _) = List.partition (fn t => q orelse (type_of t) = HOLogic.natT)
    69       (foldr HOLogic.mk_imp c rhs, np) ps
       
    70     val (vs, _) = List.partition (fn t => q orelse (type_of t) = nT)
       
    71       (term_frees fm' @ term_vars fm');
    65       (term_frees fm' @ term_vars fm');
    72     val fm2 = foldr mk_all2 fm' vs
    66     val fm2 = Library.foldr mk_all2 (vs, fm');
    73   in (fm2, np + length vs, length rhs) end;
    67   in (fm2, np + length vs, length rhs) end;
    74 
    68 
    75 (*Object quantifier to meta --*)
    69 fun spec_step n th = if n = 0 then th else spec_step (n - 1) th RS spec ;
    76 fun spec_step n th = if (n=0) then th else (spec_step (n-1) th) RS spec ;
    70 fun mp_step n th = if n = 0 then th else mp_step (n - 1) th RS mp;
    77 
    71 
    78 (* object implication to meta---*)
    72 local
    79 fun mp_step n th = if (n=0) then th else (mp_step (n-1) th) RS mp;
    73   val context_ss = simpset_of (the_context ())
    80 
    74 in fun ferrack_tac q i =  ObjectLogic.atomize_tac i
    81 
    75   THEN REPEAT_DETERM (split_tac [split_min, split_max,abs_split] i)
    82 fun ferrack_tac q i = 
    76   THEN (fn st =>
    83     (ObjectLogic.atomize_tac i) 
       
    84 	THEN (REPEAT_DETERM (split_tac [split_min, split_max,abs_split] i))
       
    85 	THEN (fn st =>
       
    86   let
    77   let
    87     val g = List.nth (prems_of st, i - 1)
    78     val g = nth (prems_of st) (i - 1)
    88     val sg = sign_of_thm st
    79     val thy = sign_of_thm st
    89     (* Transform the term*)
    80     (* Transform the term*)
    90     val (t,np,nh) = prepare_for_linr sg q g
    81     val (t,np,nh) = prepare_for_linr q g
    91     (* Some simpsets for dealing with mod div abs and nat*)
    82     (* Some simpsets for dealing with mod div abs and nat*)
    92     val simpset0 = HOL_basic_ss addsimps comp_arith addsplits [split_min, split_max]
    83     val simpset0 = HOL_basic_ss addsimps comp_arith addsplits [split_min, split_max]
    93     (* simp rules for elimination of abs *)
    84     (* simp rules for elimination of abs *)
    94     val simpset3 = HOL_basic_ss addsplits [abs_split]
    85     val simpset3 = HOL_basic_ss addsplits [abs_split]
    95     val ct = cterm_of sg (HOLogic.mk_Trueprop t)
    86     val ct = cterm_of thy (HOLogic.mk_Trueprop t)
    96     (* Theorem for the nat --> int transformation *)
    87     (* Theorem for the nat --> int transformation *)
    97     val pre_thm = Seq.hd (EVERY
    88     val pre_thm = Seq.hd (EVERY
    98       [simp_tac simpset0 1, TRY (simp_tac context_ss 1)]
    89       [simp_tac simpset0 1, TRY (simp_tac context_ss 1)]
    99       (trivial ct))
    90       (trivial ct))
   100     fun assm_tac i = REPEAT_DETERM_N nh (assume_tac i)
    91     fun assm_tac i = REPEAT_DETERM_N nh (assume_tac i)
   101     (* The result of the quantifier elimination *)
    92     (* The result of the quantifier elimination *)
   102     val (th, tac) = case (prop_of pre_thm) of
    93     val (th, tac) = case (prop_of pre_thm) of
   103         Const ("==>", _) $ (Const ("Trueprop", _) $ t1) $ _ =>
    94         Const ("==>", _) $ (Const ("Trueprop", _) $ t1) $ _ =>
   104     let val pth = Ferrante_Rackoff_Proof.qelim (cterm_of sg (Pattern.eta_long [] t1))
    95     let val pth = Ferrante_Rackoff_Proof.qelim (cterm_of thy (Pattern.eta_long [] t1))
   105     in 
    96     in 
   106           (trace_msg ("calling procedure with term:\n" ^
    97           (trace_msg ("calling procedure with term:\n" ^
   107              Sign.string_of_term sg t1);
    98              Sign.string_of_term thy t1);
   108            ((pth RS iffD2) RS pre_thm,
    99            ((pth RS iffD2) RS pre_thm,
   109             assm_tac (i + 1) THEN (if q then I else TRY) (rtac TrueI i)))
   100             assm_tac (i + 1) THEN (if q then I else TRY) (rtac TrueI i)))
   110     end
   101     end
   111       | _ => (pre_thm, assm_tac i)
   102       | _ => (pre_thm, assm_tac i)
   112   in (rtac (((mp_step nh) o (spec_step np)) th) i 
   103   in (rtac (((mp_step nh) o (spec_step np)) th) i 
   113       THEN tac) st
   104       THEN tac) st
   114   end handle Subscript => no_tac st | Ferrante_Rackoff_Proof.FAILURE _ => no_tac st);
   105   end handle Subscript => no_tac st | Ferrante_Rackoff_Proof.FAILURE _ => no_tac st);
       
   106 end; (*local*)
   115 
   107 
   116 fun ferrack_args meth =
   108 fun ferrack_args meth =
   117  let val parse_flag = 
   109  let
   118          Args.$$$ "no_quantify" >> (K (K false));
   110    val parse_flag =  Args.$$$ "no_quantify" >> (K (K false));
   119  in
   111  in
   120    Method.simple_args 
   112    Method.simple_args 
   121   (Scan.optional (Args.$$$ "(" |-- Scan.repeat1 parse_flag --| Args.$$$ ")") [] >>
   113   (Scan.optional (Args.$$$ "(" |-- Scan.repeat1 parse_flag --| Args.$$$ ")") [] >>
   122     curry (Library.foldl op |>) true)
   114     curry (Library.foldl op |>) true)
   123     (fn q => fn _ => meth q 1)
   115     (fn q => fn _ => meth q 1)