| author | wenzelm | 
| Thu, 20 Aug 2015 17:39:07 +0200 | |
| changeset 60986 | 077f663b6c24 | 
| parent 60949 | ccbf9379e355 | 
| child 61075 | f6b0d827240e | 
| permissions | -rw-r--r-- | 
| 33443 | 1 | (* Title: HOL/Library/positivstellensatz.ML | 
| 2 | Author: Amine Chaieb, University of Cambridge | |
| 3 | ||
| 4 | A generic arithmetic prover based on Positivstellensatz certificates | |
| 58628 | 5 | --- also implements Fourier-Motzkin elimination as a special case | 
| 6 | Fourier-Motzkin elimination. | |
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 7 | *) | 
| 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 8 | |
| 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 9 | (* A functor for finite mappings based on Tables *) | 
| 32645 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32402diff
changeset | 10 | |
| 46594 | 11 | signature FUNC = | 
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 12 | sig | 
| 46594 | 13 | include TABLE | 
| 14 | val apply : 'a table -> key -> 'a | |
| 15 | val applyd :'a table -> (key -> 'a) -> key -> 'a | |
| 16 |   val combine : ('a -> 'a -> 'a) -> ('a -> bool) -> 'a table -> 'a table -> 'a table
 | |
| 17 | val dom : 'a table -> key list | |
| 18 | val tryapplyd : 'a table -> key -> 'a -> 'a | |
| 19 | val updatep : (key * 'a -> bool) -> key * 'a -> 'a table -> 'a table | |
| 20 | val choose : 'a table -> key * 'a | |
| 21 | val onefunc : key * 'a -> 'a table | |
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 22 | end; | 
| 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 23 | |
| 46594 | 24 | functor FuncFun(Key: KEY) : FUNC = | 
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 25 | struct | 
| 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 26 | |
| 31971 
8c1b845ed105
renamed functor TableFun to Table, and GraphFun to Graph;
 wenzelm parents: 
31120diff
changeset | 27 | structure Tab = Table(Key); | 
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 28 | |
| 32829 
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
 Philipp Meyer parents: 
32828diff
changeset | 29 | open Tab; | 
| 
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
 Philipp Meyer parents: 
32828diff
changeset | 30 | |
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 31 | fun dom a = sort Key.ord (Tab.keys a); | 
| 46594 | 32 | fun applyd f d x = case Tab.lookup f x of | 
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 33 | SOME y => y | 
| 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 34 | | NONE => d x; | 
| 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 35 | |
| 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 36 | fun apply f x = applyd f (fn _ => raise Tab.UNDEF x) x; | 
| 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 37 | fun tryapplyd f a d = applyd f (K d) a; | 
| 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 38 | fun updatep p (k,v) t = if p (k, v) then t else update (k,v) t | 
| 46594 | 39 | fun combine f z a b = | 
| 40 | let | |
| 41 | fun h (k,v) t = case Tab.lookup t k of | |
| 42 | NONE => Tab.update (k,v) t | |
| 43 | | SOME v' => let val w = f v v' | |
| 44 | in if z w then Tab.delete k t else Tab.update (k,w) t end; | |
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 45 | in Tab.fold h a b end; | 
| 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 46 | |
| 52049 | 47 | fun choose f = | 
| 48 | (case Tab.min f of | |
| 49 | SOME entry => entry | |
| 50 | | NONE => error "FuncFun.choose : Completely empty function") | |
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 51 | |
| 32829 
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
 Philipp Meyer parents: 
32828diff
changeset | 52 | fun onefunc kv = update kv empty | 
| 
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
 Philipp Meyer parents: 
32828diff
changeset | 53 | |
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 54 | end; | 
| 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 55 | |
| 32645 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32402diff
changeset | 56 | (* Some standard functors and utility functions for them *) | 
| 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32402diff
changeset | 57 | |
| 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32402diff
changeset | 58 | structure FuncUtil = | 
| 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32402diff
changeset | 59 | struct | 
| 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32402diff
changeset | 60 | |
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 61 | structure Intfunc = FuncFun(type key = int val ord = int_ord); | 
| 32645 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32402diff
changeset | 62 | structure Ratfunc = FuncFun(type key = Rat.rat val ord = Rat.ord); | 
| 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32402diff
changeset | 63 | structure Intpairfunc = FuncFun(type key = int*int val ord = prod_ord int_ord int_ord); | 
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 64 | structure Symfunc = FuncFun(type key = string val ord = fast_string_ord); | 
| 35408 | 65 | structure Termfunc = FuncFun(type key = term val ord = Term_Ord.fast_term_ord); | 
| 32645 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32402diff
changeset | 66 | |
| 59582 | 67 | val cterm_ord = Term_Ord.fast_term_ord o apply2 Thm.term_of | 
| 32645 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32402diff
changeset | 68 | |
| 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32402diff
changeset | 69 | structure Ctermfunc = FuncFun(type key = cterm val ord = cterm_ord); | 
| 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32402diff
changeset | 70 | |
| 32829 
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
 Philipp Meyer parents: 
32828diff
changeset | 71 | type monomial = int Ctermfunc.table; | 
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 72 | |
| 59058 
a78612c67ec0
renamed "pairself" to "apply2", in accordance to @{apply 2};
 wenzelm parents: 
58628diff
changeset | 73 | val monomial_ord = list_ord (prod_ord cterm_ord int_ord) o apply2 Ctermfunc.dest | 
| 32645 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32402diff
changeset | 74 | |
| 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32402diff
changeset | 75 | structure Monomialfunc = FuncFun(type key = monomial val ord = monomial_ord) | 
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 76 | |
| 32829 
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
 Philipp Meyer parents: 
32828diff
changeset | 77 | type poly = Rat.rat Monomialfunc.table; | 
| 32645 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32402diff
changeset | 78 | |
| 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32402diff
changeset | 79 | (* The ordering so we can create canonical HOL polynomials. *) | 
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 80 | |
| 59058 
a78612c67ec0
renamed "pairself" to "apply2", in accordance to @{apply 2};
 wenzelm parents: 
58628diff
changeset | 81 | fun dest_monomial mon = sort (cterm_ord o apply2 fst) (Ctermfunc.dest mon); | 
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 82 | |
| 32645 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32402diff
changeset | 83 | fun monomial_order (m1,m2) = | 
| 46594 | 84 | if Ctermfunc.is_empty m2 then LESS | 
| 85 | else if Ctermfunc.is_empty m1 then GREATER | |
| 86 | else | |
| 87 | let | |
| 88 | val mon1 = dest_monomial m1 | |
| 32645 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32402diff
changeset | 89 | val mon2 = dest_monomial m2 | 
| 33002 | 90 | val deg1 = fold (Integer.add o snd) mon1 0 | 
| 46594 | 91 | val deg2 = fold (Integer.add o snd) mon2 0 | 
| 92 | in if deg1 < deg2 then GREATER | |
| 93 | else if deg1 > deg2 then LESS | |
| 94 | else list_ord (prod_ord cterm_ord int_ord) (mon1,mon2) | |
| 95 | end; | |
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 96 | |
| 32645 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32402diff
changeset | 97 | end | 
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 98 | |
| 32645 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32402diff
changeset | 99 | (* positivstellensatz datatype and prover generation *) | 
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 100 | |
| 46594 | 101 | signature REAL_ARITH = | 
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 102 | sig | 
| 46594 | 103 | |
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 104 | datatype positivstellensatz = | 
| 46594 | 105 | Axiom_eq of int | 
| 106 | | Axiom_le of int | |
| 107 | | Axiom_lt of int | |
| 108 | | Rational_eq of Rat.rat | |
| 109 | | Rational_le of Rat.rat | |
| 110 | | Rational_lt of Rat.rat | |
| 111 | | Square of FuncUtil.poly | |
| 112 | | Eqmul of FuncUtil.poly * positivstellensatz | |
| 113 | | Sum of positivstellensatz * positivstellensatz | |
| 114 | | Product of positivstellensatz * positivstellensatz; | |
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 115 | |
| 46594 | 116 | datatype pss_tree = Trivial | Cert of positivstellensatz | Branch of pss_tree * pss_tree | 
| 32645 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32402diff
changeset | 117 | |
| 46594 | 118 | datatype tree_choice = Left | Right | 
| 32645 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32402diff
changeset | 119 | |
| 46594 | 120 | type prover = tree_choice list -> | 
| 121 | (thm list * thm list * thm list -> positivstellensatz -> thm) -> | |
| 122 | thm list * thm list * thm list -> thm * pss_tree | |
| 123 | type cert_conv = cterm -> thm * pss_tree | |
| 32645 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32402diff
changeset | 124 | |
| 46594 | 125 | val gen_gen_real_arith : | 
| 126 | Proof.context -> (Rat.rat -> cterm) * conv * conv * conv * | |
| 127 | conv * conv * conv * conv * conv * conv * prover -> cert_conv | |
| 128 | val real_linear_prover : (thm list * thm list * thm list -> positivstellensatz -> thm) -> | |
| 129 | thm list * thm list * thm list -> thm * pss_tree | |
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 130 | |
| 46594 | 131 | val gen_real_arith : Proof.context -> | 
| 132 | (Rat.rat -> cterm) * conv * conv * conv * conv * conv * conv * conv * prover -> cert_conv | |
| 32645 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32402diff
changeset | 133 | |
| 46594 | 134 | val gen_prover_real_arith : Proof.context -> prover -> cert_conv | 
| 32645 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32402diff
changeset | 135 | |
| 46594 | 136 | val is_ratconst : cterm -> bool | 
| 137 | val dest_ratconst : cterm -> Rat.rat | |
| 138 | val cterm_of_rat : Rat.rat -> cterm | |
| 32645 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32402diff
changeset | 139 | |
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 140 | end | 
| 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 141 | |
| 32645 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32402diff
changeset | 142 | structure RealArith : REAL_ARITH = | 
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 143 | struct | 
| 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 144 | |
| 46594 | 145 | open Conv | 
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 146 | (* ------------------------------------------------------------------------- *) | 
| 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 147 | (* Data structure for Positivstellensatz refutations. *) | 
| 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 148 | (* ------------------------------------------------------------------------- *) | 
| 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 149 | |
| 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 150 | datatype positivstellensatz = | 
| 46594 | 151 | Axiom_eq of int | 
| 152 | | Axiom_le of int | |
| 153 | | Axiom_lt of int | |
| 154 | | Rational_eq of Rat.rat | |
| 155 | | Rational_le of Rat.rat | |
| 156 | | Rational_lt of Rat.rat | |
| 157 | | Square of FuncUtil.poly | |
| 158 | | Eqmul of FuncUtil.poly * positivstellensatz | |
| 159 | | Sum of positivstellensatz * positivstellensatz | |
| 160 | | Product of positivstellensatz * positivstellensatz; | |
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 161 | (* Theorems used in the procedure *) | 
| 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 162 | |
| 32645 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32402diff
changeset | 163 | datatype pss_tree = Trivial | Cert of positivstellensatz | Branch of pss_tree * pss_tree | 
| 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32402diff
changeset | 164 | datatype tree_choice = Left | Right | 
| 46594 | 165 | type prover = tree_choice list -> | 
| 32645 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32402diff
changeset | 166 | (thm list * thm list * thm list -> positivstellensatz -> thm) -> | 
| 46594 | 167 | thm list * thm list * thm list -> thm * pss_tree | 
| 32645 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32402diff
changeset | 168 | type cert_conv = cterm -> thm * pss_tree | 
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 169 | |
| 32645 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32402diff
changeset | 170 | |
| 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32402diff
changeset | 171 | (* Some useful derived rules *) | 
| 46594 | 172 | fun deduct_antisym_rule tha thb = | 
| 59582 | 173 | Thm.equal_intr (Thm.implies_intr (Thm.cprop_of thb) tha) | 
| 174 | (Thm.implies_intr (Thm.cprop_of tha) thb); | |
| 32645 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32402diff
changeset | 175 | |
| 44058 | 176 | fun prove_hyp tha thb = | 
| 59582 | 177 | if exists (curry op aconv (Thm.concl_of tha)) (Thm.hyps_of thb) (* FIXME !? *) | 
| 36945 | 178 | then Thm.equal_elim (Thm.symmetric (deduct_antisym_rule tha thb)) tha else thb; | 
| 32645 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32402diff
changeset | 179 | |
| 33443 | 180 | val pth = @{lemma "(((x::real) < y) == (y - x > 0))" and "((x <= y) == (y - x >= 0))" and
 | 
| 181 | "((x = y) == (x - y = 0))" and "((~(x < y)) == (x - y >= 0))" and | |
| 182 | "((~(x <= y)) == (x - y > 0))" and "((~(x = y)) == (x - y > 0 | -(x - y) > 0))" | |
| 183 | by (atomize (full), auto simp add: less_diff_eq le_diff_eq not_less)}; | |
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 184 | |
| 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 185 | val pth_final = @{lemma "(~p ==> False) ==> p" by blast}
 | 
| 46594 | 186 | val pth_add = | 
| 33443 | 187 |   @{lemma "(x = (0::real) ==> y = 0 ==> x + y = 0 )" and "( x = 0 ==> y >= 0 ==> x + y >= 0)" and
 | 
| 188 | "(x = 0 ==> y > 0 ==> x + y > 0)" and "(x >= 0 ==> y = 0 ==> x + y >= 0)" and | |
| 189 | "(x >= 0 ==> y >= 0 ==> x + y >= 0)" and "(x >= 0 ==> y > 0 ==> x + y > 0)" and | |
| 190 | "(x > 0 ==> y = 0 ==> x + y > 0)" and "(x > 0 ==> y >= 0 ==> x + y > 0)" and | |
| 191 | "(x > 0 ==> y > 0 ==> x + y > 0)" by simp_all}; | |
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 192 | |
| 46594 | 193 | val pth_mul = | 
| 33443 | 194 |   @{lemma "(x = (0::real) ==> y = 0 ==> x * y = 0)" and "(x = 0 ==> y >= 0 ==> x * y = 0)" and
 | 
| 195 | "(x = 0 ==> y > 0 ==> x * y = 0)" and "(x >= 0 ==> y = 0 ==> x * y = 0)" and | |
| 196 | "(x >= 0 ==> y >= 0 ==> x * y >= 0)" and "(x >= 0 ==> y > 0 ==> x * y >= 0)" and | |
| 197 | "(x > 0 ==> y = 0 ==> x * y = 0)" and "(x > 0 ==> y >= 0 ==> x * y >= 0)" and | |
| 198 | "(x > 0 ==> y > 0 ==> x * y > 0)" | |
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 199 | by (auto intro: mult_mono[where a="0::real" and b="x" and d="y" and c="0", simplified] | 
| 33443 | 200 | mult_strict_mono[where b="x" and d="y" and a="0" and c="0", simplified])}; | 
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 201 | |
| 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 202 | val pth_emul = @{lemma "y = (0::real) ==> x * y = 0"  by simp};
 | 
| 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 203 | val pth_square = @{lemma "x * x >= (0::real)"  by simp};
 | 
| 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 204 | |
| 33443 | 205 | val weak_dnf_simps = | 
| 45654 | 206 |   List.take (@{thms simp_thms}, 34) @
 | 
| 33443 | 207 |     @{lemma "((P & (Q | R)) = ((P&Q) | (P&R)))" and "((Q | R) & P) = ((Q&P) | (R&P))" and
 | 
| 208 | "(P & Q) = (Q & P)" and "((P | Q) = (Q | P))" by blast+}; | |
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 209 | |
| 44454 | 210 | (* | 
| 33443 | 211 | val nnfD_simps = | 
| 212 |   @{lemma "((~(P & Q)) = (~P | ~Q))" and "((~(P | Q)) = (~P & ~Q) )" and
 | |
| 213 | "((P --> Q) = (~P | Q) )" and "((P = Q) = ((P & Q) | (~P & ~ Q)))" and | |
| 214 | "((~(P = Q)) = ((P & ~ Q) | (~P & Q)) )" and "((~ ~(P)) = P)" by blast+}; | |
| 44454 | 215 | *) | 
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 216 | |
| 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 217 | val choice_iff = @{lemma "(ALL x. EX y. P x y) = (EX f. ALL x. P x (f x))" by metis};
 | 
| 33443 | 218 | val prenex_simps = | 
| 219 | map (fn th => th RS sym) | |
| 220 |     ([@{thm "all_conj_distrib"}, @{thm "ex_disj_distrib"}] @
 | |
| 37598 | 221 |       @{thms "HOL.all_simps"(1-4)} @ @{thms "ex_simps"(1-4)});
 | 
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 222 | |
| 33443 | 223 | val real_abs_thms1 = @{lemma
 | 
| 224 | "((-1 * abs(x::real) >= r) = (-1 * x >= r & 1 * x >= r))" and | |
| 225 | "((-1 * abs(x) + a >= r) = (a + -1 * x >= r & a + 1 * x >= r))" and | |
| 226 | "((a + -1 * abs(x) >= r) = (a + -1 * x >= r & a + 1 * x >= r))" and | |
| 227 | "((a + -1 * abs(x) + b >= r) = (a + -1 * x + b >= r & a + 1 * x + b >= r))" and | |
| 228 | "((a + b + -1 * abs(x) >= r) = (a + b + -1 * x >= r & a + b + 1 * x >= r))" and | |
| 229 | "((a + b + -1 * abs(x) + c >= r) = (a + b + -1 * x + c >= r & a + b + 1 * x + c >= r))" and | |
| 230 | "((-1 * max x y >= r) = (-1 * x >= r & -1 * y >= r))" and | |
| 231 | "((-1 * max x y + a >= r) = (a + -1 * x >= r & a + -1 * y >= r))" and | |
| 232 | "((a + -1 * max x y >= r) = (a + -1 * x >= r & a + -1 * y >= r))" and | |
| 233 | "((a + -1 * max x y + b >= r) = (a + -1 * x + b >= r & a + -1 * y + b >= r))" and | |
| 234 | "((a + b + -1 * max x y >= r) = (a + b + -1 * x >= r & a + b + -1 * y >= r))" and | |
| 235 | "((a + b + -1 * max x y + c >= r) = (a + b + -1 * x + c >= r & a + b + -1 * y + c >= r))" and | |
| 236 | "((1 * min x y >= r) = (1 * x >= r & 1 * y >= r))" and | |
| 237 | "((1 * min x y + a >= r) = (a + 1 * x >= r & a + 1 * y >= r))" and | |
| 238 | "((a + 1 * min x y >= r) = (a + 1 * x >= r & a + 1 * y >= r))" and | |
| 239 | "((a + 1 * min x y + b >= r) = (a + 1 * x + b >= r & a + 1 * y + b >= r))" and | |
| 240 | "((a + b + 1 * min x y >= r) = (a + b + 1 * x >= r & a + b + 1 * y >= r))" and | |
| 241 | "((a + b + 1 * min x y + c >= r) = (a + b + 1 * x + c >= r & a + b + 1 * y + c >= r))" and | |
| 242 | "((min x y >= r) = (x >= r & y >= r))" and | |
| 243 | "((min x y + a >= r) = (a + x >= r & a + y >= r))" and | |
| 244 | "((a + min x y >= r) = (a + x >= r & a + y >= r))" and | |
| 245 | "((a + min x y + b >= r) = (a + x + b >= r & a + y + b >= r))" and | |
| 246 | "((a + b + min x y >= r) = (a + b + x >= r & a + b + y >= r))" and | |
| 247 | "((a + b + min x y + c >= r) = (a + b + x + c >= r & a + b + y + c >= r))" and | |
| 248 | "((-1 * abs(x) > r) = (-1 * x > r & 1 * x > r))" and | |
| 249 | "((-1 * abs(x) + a > r) = (a + -1 * x > r & a + 1 * x > r))" and | |
| 250 | "((a + -1 * abs(x) > r) = (a + -1 * x > r & a + 1 * x > r))" and | |
| 251 | "((a + -1 * abs(x) + b > r) = (a + -1 * x + b > r & a + 1 * x + b > r))" and | |
| 252 | "((a + b + -1 * abs(x) > r) = (a + b + -1 * x > r & a + b + 1 * x > r))" and | |
| 253 | "((a + b + -1 * abs(x) + c > r) = (a + b + -1 * x + c > r & a + b + 1 * x + c > r))" and | |
| 254 | "((-1 * max x y > r) = ((-1 * x > r) & -1 * y > r))" and | |
| 255 | "((-1 * max x y + a > r) = (a + -1 * x > r & a + -1 * y > r))" and | |
| 256 | "((a + -1 * max x y > r) = (a + -1 * x > r & a + -1 * y > r))" and | |
| 257 | "((a + -1 * max x y + b > r) = (a + -1 * x + b > r & a + -1 * y + b > r))" and | |
| 258 | "((a + b + -1 * max x y > r) = (a + b + -1 * x > r & a + b + -1 * y > r))" and | |
| 259 | "((a + b + -1 * max x y + c > r) = (a + b + -1 * x + c > r & a + b + -1 * y + c > r))" and | |
| 260 | "((min x y > r) = (x > r & y > r))" and | |
| 261 | "((min x y + a > r) = (a + x > r & a + y > r))" and | |
| 262 | "((a + min x y > r) = (a + x > r & a + y > r))" and | |
| 263 | "((a + min x y + b > r) = (a + x + b > r & a + y + b > r))" and | |
| 264 | "((a + b + min x y > r) = (a + b + x > r & a + b + y > r))" and | |
| 265 | "((a + b + min x y + c > r) = (a + b + x + c > r & a + b + y + c > r))" | |
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 266 | by auto}; | 
| 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 267 | |
| 35028 
108662d50512
more consistent naming of type classes involving orderings (and lattices) -- c.f. NEWS
 haftmann parents: 
33443diff
changeset | 268 | val abs_split' = @{lemma "P (abs (x::'a::linordered_idom)) == (x >= 0 & P x | x < 0 & P (-x))"
 | 
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 269 | by (atomize (full)) (auto split add: abs_split)}; | 
| 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 270 | |
| 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 271 | val max_split = @{lemma "P (max x y) == ((x::'a::linorder) <= y & P y | x > y & P x)"
 | 
| 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 272 | by (atomize (full)) (cases "x <= y", auto simp add: max_def)}; | 
| 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 273 | |
| 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 274 | val min_split = @{lemma "P (min x y) == ((x::'a::linorder) <= y & P x | x > y & P y)"
 | 
| 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 275 | by (atomize (full)) (cases "x <= y", auto simp add: min_def)}; | 
| 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 276 | |
| 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 277 | |
| 39920 | 278 | (* Miscellaneous *) | 
| 46594 | 279 | fun literals_conv bops uops cv = | 
| 280 | let | |
| 281 | fun h t = | |
| 59582 | 282 | (case Thm.term_of t of | 
| 46594 | 283 | b$_$_ => if member (op aconv) bops b then binop_conv h t else cv t | 
| 284 | | u$_ => if member (op aconv) uops u then arg_conv h t else cv t | |
| 59582 | 285 | | _ => cv t) | 
| 46594 | 286 | in h end; | 
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 287 | |
| 46594 | 288 | fun cterm_of_rat x = | 
| 289 | let | |
| 290 | val (a, b) = Rat.quotient_of_rat x | |
| 291 | in | |
| 292 |     if b = 1 then Numeral.mk_cnumber @{ctyp "real"} a
 | |
| 293 |     else Thm.apply (Thm.apply @{cterm "op / :: real => _"}
 | |
| 294 |       (Numeral.mk_cnumber @{ctyp "real"} a))
 | |
| 295 |       (Numeral.mk_cnumber @{ctyp "real"} b)
 | |
| 296 | end; | |
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 297 | |
| 46594 | 298 | fun dest_ratconst t = | 
| 59582 | 299 | case Thm.term_of t of | 
| 46594 | 300 |     Const(@{const_name divide}, _)$a$b => Rat.rat_of_quotient(HOLogic.dest_number a |> snd, HOLogic.dest_number b |> snd)
 | 
| 59582 | 301 | | _ => Rat.rat_of_int (HOLogic.dest_number (Thm.term_of t) |> snd) | 
| 46594 | 302 | fun is_ratconst t = can dest_ratconst t | 
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 303 | |
| 44454 | 304 | (* | 
| 46594 | 305 | fun find_term p t = if p t then t else | 
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 306 | case t of | 
| 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 307 | a$b => (find_term p a handle TERM _ => find_term p b) | 
| 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 308 | | Abs (_,_,t') => find_term p t' | 
| 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 309 |  | _ => raise TERM ("find_term",[t]);
 | 
| 44454 | 310 | *) | 
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 311 | |
| 46594 | 312 | fun find_cterm p t = | 
| 313 | if p t then t else | |
| 59582 | 314 | case Thm.term_of t of | 
| 46594 | 315 | _$_ => (find_cterm p (Thm.dest_fun t) handle CTERM _ => find_cterm p (Thm.dest_arg t)) | 
| 316 | | Abs (_,_,_) => find_cterm p (Thm.dest_abs NONE t |> snd) | |
| 317 |   | _ => raise CTERM ("find_cterm",[t]);
 | |
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 318 | |
| 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 319 | (* Some conversions-related stuff which has been forbidden entrance into Pure/conv.ML*) | 
| 60801 | 320 | fun instantiate_cterm' ty tms = Drule.cterm_rule (Thm.instantiate' ty tms) | 
| 59582 | 321 | fun is_comb t = (case Thm.term_of t of _ $ _ => true | _ => false); | 
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 322 | |
| 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 323 | fun is_binop ct ct' = ct aconvc (Thm.dest_fun (Thm.dest_fun ct')) | 
| 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 324 | handle CTERM _ => false; | 
| 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 325 | |
| 32645 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32402diff
changeset | 326 | |
| 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32402diff
changeset | 327 | (* Map back polynomials to HOL. *) | 
| 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32402diff
changeset | 328 | |
| 46594 | 329 | fun cterm_of_varpow x k = if k = 1 then x else Thm.apply (Thm.apply @{cterm "op ^ :: real => _"} x)
 | 
| 32828 | 330 |   (Numeral.mk_cnumber @{ctyp nat} k)
 | 
| 32645 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32402diff
changeset | 331 | |
| 46594 | 332 | fun cterm_of_monomial m = | 
| 333 |   if FuncUtil.Ctermfunc.is_empty m then @{cterm "1::real"}
 | |
| 334 | else | |
| 335 | let | |
| 336 | val m' = FuncUtil.dest_monomial m | |
| 337 | val vps = fold_rev (fn (x,k) => cons (cterm_of_varpow x k)) m' [] | |
| 338 |     in foldr1 (fn (s, t) => Thm.apply (Thm.apply @{cterm "op * :: real => _"} s) t) vps
 | |
| 339 | end | |
| 32645 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32402diff
changeset | 340 | |
| 46594 | 341 | fun cterm_of_cmonomial (m,c) = | 
| 342 | if FuncUtil.Ctermfunc.is_empty m then cterm_of_rat c | |
| 343 | else if c = Rat.one then cterm_of_monomial m | |
| 344 |   else Thm.apply (Thm.apply @{cterm "op *::real => _"} (cterm_of_rat c)) (cterm_of_monomial m);
 | |
| 32645 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32402diff
changeset | 345 | |
| 46594 | 346 | fun cterm_of_poly p = | 
| 347 |   if FuncUtil.Monomialfunc.is_empty p then @{cterm "0::real"}
 | |
| 348 | else | |
| 349 | let | |
| 350 | val cms = map cterm_of_cmonomial | |
| 351 | (sort (prod_ord FuncUtil.monomial_order (K EQUAL)) (FuncUtil.Monomialfunc.dest p)) | |
| 352 |     in foldr1 (fn (t1, t2) => Thm.apply(Thm.apply @{cterm "op + :: real => _"} t1) t2) cms
 | |
| 353 | end; | |
| 32645 
1cc5b24f5a01
sos method generates and uses proof certificates
 Philipp Meyer parents: 
32402diff
changeset | 354 | |
| 46594 | 355 | (* A general real arithmetic prover *) | 
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 356 | |
| 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 357 | fun gen_gen_real_arith ctxt (mk_numeric, | 
| 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 358 | numeric_eq_conv,numeric_ge_conv,numeric_gt_conv, | 
| 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 359 | poly_conv,poly_neg_conv,poly_add_conv,poly_mul_conv, | 
| 46594 | 360 | absconv1,absconv2,prover) = | 
| 361 | let | |
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
46594diff
changeset | 362 | val pre_ss = put_simpset HOL_basic_ss ctxt addsimps | 
| 58628 | 363 |       @{thms simp_thms ex_simps all_simps not_all not_ex ex_disj_distrib
 | 
| 364 | all_conj_distrib if_bool_eq_disj} | |
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
46594diff
changeset | 365 | val prenex_ss = put_simpset HOL_basic_ss ctxt addsimps prenex_simps | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
46594diff
changeset | 366 | val skolemize_ss = put_simpset HOL_basic_ss ctxt addsimps [choice_iff] | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
46594diff
changeset | 367 | val presimp_conv = Simplifier.rewrite pre_ss | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
46594diff
changeset | 368 | val prenex_conv = Simplifier.rewrite prenex_ss | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
46594diff
changeset | 369 | val skolemize_conv = Simplifier.rewrite skolemize_ss | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
46594diff
changeset | 370 | val weak_dnf_ss = put_simpset HOL_basic_ss ctxt addsimps weak_dnf_simps | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
46594diff
changeset | 371 | val weak_dnf_conv = Simplifier.rewrite weak_dnf_ss | 
| 46594 | 372 |     fun eqT_elim th = Thm.equal_elim (Thm.symmetric th) @{thm TrueI}
 | 
| 373 | fun oprconv cv ct = | |
| 374 | let val g = Thm.dest_fun2 ct | |
| 375 |       in if g aconvc @{cterm "op <= :: real => _"}
 | |
| 376 |             orelse g aconvc @{cterm "op < :: real => _"}
 | |
| 377 | then arg_conv cv ct else arg1_conv cv ct | |
| 378 | end | |
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 379 | |
| 46594 | 380 | fun real_ineq_conv th ct = | 
| 381 | let | |
| 382 | val th' = (Thm.instantiate (Thm.match (Thm.lhs_of th, ct)) th | |
| 383 |           handle Pattern.MATCH => raise CTERM ("real_ineq_conv", [ct]))
 | |
| 384 | in Thm.transitive th' (oprconv poly_conv (Thm.rhs_of th')) | |
| 385 | end | |
| 386 | val [real_lt_conv, real_le_conv, real_eq_conv, | |
| 387 | real_not_lt_conv, real_not_le_conv, _] = | |
| 388 | map real_ineq_conv pth | |
| 389 | fun match_mp_rule ths ths' = | |
| 390 | let | |
| 391 |         fun f ths ths' = case ths of [] => raise THM("match_mp_rule",0,ths)
 | |
| 392 | | th::ths => (ths' MRS th handle THM _ => f ths ths') | |
| 393 | in f ths ths' end | |
| 394 | fun mul_rule th th' = fconv_rule (arg_conv (oprconv poly_mul_conv)) | |
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 395 | (match_mp_rule pth_mul [th, th']) | 
| 46594 | 396 | fun add_rule th th' = fconv_rule (arg_conv (oprconv poly_add_conv)) | 
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 397 | (match_mp_rule pth_add [th, th']) | 
| 46594 | 398 | fun emul_rule ct th = fconv_rule (arg_conv (oprconv poly_mul_conv)) | 
| 60801 | 399 | (Thm.instantiate' [] [SOME ct] (th RS pth_emul)) | 
| 46594 | 400 | fun square_rule t = fconv_rule (arg_conv (oprconv poly_conv)) | 
| 60801 | 401 | (Thm.instantiate' [] [SOME t] pth_square) | 
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 402 | |
| 46594 | 403 | fun hol_of_positivstellensatz(eqs,les,lts) proof = | 
| 404 | let | |
| 405 | fun translate prf = | |
| 406 | case prf of | |
| 407 | Axiom_eq n => nth eqs n | |
| 408 | | Axiom_le n => nth les n | |
| 409 | | Axiom_lt n => nth lts n | |
| 410 |           | Rational_eq x => eqT_elim(numeric_eq_conv(Thm.apply @{cterm Trueprop}
 | |
| 411 |                           (Thm.apply (Thm.apply @{cterm "op =::real => _"} (mk_numeric x))
 | |
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 412 |                                @{cterm "0::real"})))
 | 
| 46594 | 413 |           | Rational_le x => eqT_elim(numeric_ge_conv(Thm.apply @{cterm Trueprop}
 | 
| 414 |                           (Thm.apply (Thm.apply @{cterm "op <=::real => _"}
 | |
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 415 |                                      @{cterm "0::real"}) (mk_numeric x))))
 | 
| 46594 | 416 |           | Rational_lt x => eqT_elim(numeric_gt_conv(Thm.apply @{cterm Trueprop}
 | 
| 46497 
89ccf66aa73d
renamed Thm.capply to Thm.apply, and Thm.cabs to Thm.lambda in conformance with similar operations in structure Term and Logic;
 wenzelm parents: 
45654diff
changeset | 417 |                       (Thm.apply (Thm.apply @{cterm "op <::real => _"} @{cterm "0::real"})
 | 
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 418 | (mk_numeric x)))) | 
| 46594 | 419 | | Square pt => square_rule (cterm_of_poly pt) | 
| 420 | | Eqmul(pt,p) => emul_rule (cterm_of_poly pt) (translate p) | |
| 421 | | Sum(p1,p2) => add_rule (translate p1) (translate p2) | |
| 422 | | Product(p1,p2) => mul_rule (translate p1) (translate p2) | |
| 423 | in fconv_rule (first_conv [numeric_ge_conv, numeric_gt_conv, numeric_eq_conv, all_conv]) | |
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 424 | (translate proof) | 
| 46594 | 425 | end | 
| 426 | ||
| 427 | val init_conv = presimp_conv then_conv | |
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
46594diff
changeset | 428 | nnf_conv ctxt then_conv skolemize_conv then_conv prenex_conv then_conv | 
| 46594 | 429 | weak_dnf_conv | 
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 430 | |
| 59582 | 431 | val concl = Thm.dest_arg o Thm.cprop_of | 
| 46594 | 432 | fun is_binop opr ct = (Thm.dest_fun2 ct aconvc opr handle CTERM _ => false) | 
| 433 |     val is_req = is_binop @{cterm "op =:: real => _"}
 | |
| 434 |     val is_ge = is_binop @{cterm "op <=:: real => _"}
 | |
| 435 |     val is_gt = is_binop @{cterm "op <:: real => _"}
 | |
| 436 |     val is_conj = is_binop @{cterm HOL.conj}
 | |
| 437 |     val is_disj = is_binop @{cterm HOL.disj}
 | |
| 438 |     fun conj_pair th = (th RS @{thm conjunct1}, th RS @{thm conjunct2})
 | |
| 439 | fun disj_cases th th1 th2 = | |
| 440 | let | |
| 441 | val (p,q) = Thm.dest_binop (concl th) | |
| 442 | val c = concl th1 | |
| 58628 | 443 | val _ = | 
| 444 | if c aconvc (concl th2) then () | |
| 445 | else error "disj_cases : conclusions not alpha convertible" | |
| 46594 | 446 | in Thm.implies_elim (Thm.implies_elim | 
| 60801 | 447 |           (Thm.implies_elim (Thm.instantiate' [] (map SOME [p,q,c]) @{thm disjE}) th)
 | 
| 46497 
89ccf66aa73d
renamed Thm.capply to Thm.apply, and Thm.cabs to Thm.lambda in conformance with similar operations in structure Term and Logic;
 wenzelm parents: 
45654diff
changeset | 448 |           (Thm.implies_intr (Thm.apply @{cterm Trueprop} p) th1))
 | 
| 
89ccf66aa73d
renamed Thm.capply to Thm.apply, and Thm.cabs to Thm.lambda in conformance with similar operations in structure Term and Logic;
 wenzelm parents: 
45654diff
changeset | 449 |         (Thm.implies_intr (Thm.apply @{cterm Trueprop} q) th2)
 | 
| 46594 | 450 | end | 
| 451 | fun overall cert_choice dun ths = | |
| 452 | case ths of | |
| 453 | [] => | |
| 454 | let | |
| 455 | val (eq,ne) = List.partition (is_req o concl) dun | |
| 456 | val (le,nl) = List.partition (is_ge o concl) ne | |
| 457 | val lt = filter (is_gt o concl) nl | |
| 458 | in prover (rev cert_choice) hol_of_positivstellensatz (eq,le,lt) end | |
| 459 | | th::oths => | |
| 460 | let | |
| 461 | val ct = concl th | |
| 462 | in | |
| 463 | if is_conj ct then | |
| 464 | let | |
| 465 | val (th1,th2) = conj_pair th | |
| 466 | in overall cert_choice dun (th1::th2::oths) end | |
| 467 | else if is_disj ct then | |
| 468 | let | |
| 58628 | 469 | val (th1, cert1) = | 
| 470 | overall (Left::cert_choice) dun | |
| 471 |                   (Thm.assume (Thm.apply @{cterm Trueprop} (Thm.dest_arg1 ct))::oths)
 | |
| 472 | val (th2, cert2) = | |
| 473 | overall (Right::cert_choice) dun | |
| 474 |                   (Thm.assume (Thm.apply @{cterm Trueprop} (Thm.dest_arg ct))::oths)
 | |
| 46594 | 475 | in (disj_cases th th1 th2, Branch (cert1, cert2)) end | 
| 476 | else overall cert_choice (th::dun) oths | |
| 477 | end | |
| 478 | fun dest_binary b ct = | |
| 479 | if is_binop b ct then Thm.dest_binop ct | |
| 480 |         else raise CTERM ("dest_binary",[b,ct])
 | |
| 481 |     val dest_eq = dest_binary @{cterm "op = :: real => _"}
 | |
| 482 | val neq_th = nth pth 5 | |
| 483 | fun real_not_eq_conv ct = | |
| 484 | let | |
| 485 | val (l,r) = dest_eq (Thm.dest_arg ct) | |
| 60642 
48dd1cefb4ae
simplified Thm.instantiate and derivatives: the LHS refers to non-certified variables -- this merely serves as index into already certified structures (or is ignored);
 wenzelm parents: 
59586diff
changeset | 486 |         val th = Thm.instantiate ([],[((("x", 0), @{typ real}),l),((("y", 0), @{typ real}),r)]) neq_th
 | 
| 46594 | 487 | val th_p = poly_conv(Thm.dest_arg(Thm.dest_arg1(Thm.rhs_of th))) | 
| 488 |         val th_x = Drule.arg_cong_rule @{cterm "uminus :: real => _"} th_p
 | |
| 489 | val th_n = fconv_rule (arg_conv poly_neg_conv) th_x | |
| 490 |         val th' = Drule.binop_cong_rule @{cterm HOL.disj}
 | |
| 491 |           (Drule.arg_cong_rule (Thm.apply @{cterm "op <::real=>_"} @{cterm "0::real"}) th_p)
 | |
| 492 |           (Drule.arg_cong_rule (Thm.apply @{cterm "op <::real=>_"} @{cterm "0::real"}) th_n)
 | |
| 493 | in Thm.transitive th th' | |
| 494 | end | |
| 495 | fun equal_implies_1_rule PQ = | |
| 496 | let | |
| 497 | val P = Thm.lhs_of PQ | |
| 498 | in Thm.implies_intr P (Thm.equal_elim PQ (Thm.assume P)) | |
| 499 | end | |
| 500 | (* FIXME!!! Copied from groebner.ml *) | |
| 501 | val strip_exists = | |
| 502 | let | |
| 503 | fun h (acc, t) = | |
| 59582 | 504 | case Thm.term_of t of | 
| 58628 | 505 |             Const(@{const_name Ex},_)$Abs(_,_,_) =>
 | 
| 506 | h (Thm.dest_abs NONE (Thm.dest_arg t) |>> (fn v => v::acc)) | |
| 46594 | 507 | | _ => (acc,t) | 
| 508 | in fn t => h ([],t) | |
| 509 | end | |
| 510 | fun name_of x = | |
| 59582 | 511 | case Thm.term_of x of | 
| 46594 | 512 | Free(s,_) => s | 
| 513 | | Var ((s,_),_) => s | |
| 514 | | _ => "x" | |
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 515 | |
| 58628 | 516 | fun mk_forall x th = | 
| 517 | Drule.arg_cong_rule | |
| 59586 | 518 |         (instantiate_cterm' [SOME (Thm.ctyp_of_cterm x)] [] @{cpat "All :: (?'a => bool) => _" })
 | 
| 58628 | 519 | (Thm.abstract_rule (name_of x) x th) | 
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 520 | |
| 60801 | 521 | val specl = fold_rev (fn x => fn th => Thm.instantiate' [] [SOME x] (th RS spec)); | 
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 522 | |
| 60801 | 523 |     fun ext T = Drule.cterm_rule (Thm.instantiate' [SOME T] []) @{cpat Ex}
 | 
| 59586 | 524 | fun mk_ex v t = Thm.apply (ext (Thm.ctyp_of_cterm v)) (Thm.lambda v t) | 
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 525 | |
| 46594 | 526 | fun choose v th th' = | 
| 59582 | 527 | case Thm.concl_of th of | 
| 46594 | 528 |         @{term Trueprop} $ (Const(@{const_name Ex},_)$_) =>
 | 
| 529 | let | |
| 59582 | 530 | val p = (funpow 2 Thm.dest_arg o Thm.cprop_of) th | 
| 59586 | 531 | val T = (hd o Thm.dest_ctyp o Thm.ctyp_of_cterm) p | 
| 46594 | 532 | val th0 = fconv_rule (Thm.beta_conversion true) | 
| 60801 | 533 | (Thm.instantiate' [SOME T] [SOME p, (SOME o Thm.dest_arg o Thm.cprop_of) th'] exE) | 
| 46594 | 534 | val pv = (Thm.rhs_of o Thm.beta_conversion true) | 
| 535 |             (Thm.apply @{cterm Trueprop} (Thm.apply p v))
 | |
| 536 | val th1 = Thm.forall_intr v (Thm.implies_intr pv th') | |
| 537 | in Thm.implies_elim (Thm.implies_elim th0 th) th1 end | |
| 538 |       | _ => raise THM ("choose",0,[th, th'])
 | |
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 539 | |
| 46594 | 540 | fun simple_choose v th = | 
| 58628 | 541 | choose v | 
| 542 | (Thm.assume | |
| 60949 | 543 |           ((Thm.apply @{cterm Trueprop} o mk_ex v) (Thm.dest_arg (hd (Thm.chyps_of th))))) th
 | 
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 544 | |
| 46594 | 545 | val strip_forall = | 
| 546 | let | |
| 547 | fun h (acc, t) = | |
| 59582 | 548 | case Thm.term_of t of | 
| 58628 | 549 |             Const(@{const_name All},_)$Abs(_,_,_) =>
 | 
| 550 | h (Thm.dest_abs NONE (Thm.dest_arg t) |>> (fn v => v::acc)) | |
| 46594 | 551 | | _ => (acc,t) | 
| 552 | in fn t => h ([],t) | |
| 553 | end | |
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 554 | |
| 46594 | 555 | fun f ct = | 
| 556 | let | |
| 557 | val nnf_norm_conv' = | |
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
46594diff
changeset | 558 | nnf_conv ctxt then_conv | 
| 46594 | 559 |           literals_conv [@{term HOL.conj}, @{term HOL.disj}] []
 | 
| 560 | (Conv.cache_conv | |
| 561 | (first_conv [real_lt_conv, real_le_conv, | |
| 562 | real_eq_conv, real_not_lt_conv, | |
| 563 | real_not_le_conv, real_not_eq_conv, all_conv])) | |
| 564 |         fun absremover ct = (literals_conv [@{term HOL.conj}, @{term HOL.disj}] []
 | |
| 565 | (try_conv (absconv1 then_conv binop_conv (arg_conv poly_conv))) then_conv | |
| 566 | try_conv (absconv2 then_conv nnf_norm_conv' then_conv binop_conv absremover)) ct | |
| 567 |         val nct = Thm.apply @{cterm Trueprop} (Thm.apply @{cterm "Not"} ct)
 | |
| 568 | val th0 = (init_conv then_conv arg_conv nnf_norm_conv') nct | |
| 569 | val tm0 = Thm.dest_arg (Thm.rhs_of th0) | |
| 570 | val (th, certificates) = | |
| 571 |           if tm0 aconvc @{cterm False} then (equal_implies_1_rule th0, Trivial) else
 | |
| 572 | let | |
| 573 | val (evs,bod) = strip_exists tm0 | |
| 574 | val (avs,ibod) = strip_forall bod | |
| 575 |             val th1 = Drule.arg_cong_rule @{cterm Trueprop} (fold mk_forall avs (absremover ibod))
 | |
| 576 | val (th2, certs) = overall [] [] [specl avs (Thm.assume (Thm.rhs_of th1))] | |
| 58628 | 577 | val th3 = | 
| 578 | fold simple_choose evs | |
| 579 |                 (prove_hyp (Thm.equal_elim th1 (Thm.assume (Thm.apply @{cterm Trueprop} bod))) th2)
 | |
| 46594 | 580 | in (Drule.implies_intr_hyps (prove_hyp (Thm.equal_elim th0 (Thm.assume nct)) th3), certs) | 
| 581 | end | |
| 60801 | 582 | in (Thm.implies_elim (Thm.instantiate' [] [SOME ct] pth_final) th, certificates) | 
| 46594 | 583 | end | 
| 584 | in f | |
| 585 | end; | |
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 586 | |
| 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 587 | (* A linear arithmetic prover *) | 
| 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 588 | local | 
| 32828 | 589 | val linear_add = FuncUtil.Ctermfunc.combine (curry op +/) (fn z => z =/ Rat.zero) | 
| 39027 | 590 | fun linear_cmul c = FuncUtil.Ctermfunc.map (fn _ => fn x => c */ x) | 
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 591 |   val one_tm = @{cterm "1::real"}
 | 
| 32829 
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
 Philipp Meyer parents: 
32828diff
changeset | 592 | fun contradictory p (e,_) = ((FuncUtil.Ctermfunc.is_empty e) andalso not(p Rat.zero)) orelse | 
| 33038 | 593 | ((eq_set (op aconvc) (FuncUtil.Ctermfunc.dom e, [one_tm])) andalso | 
| 32829 
671eb46eb0a3
tuned FuncFun and FuncUtil structure in positivstellensatz.ML
 Philipp Meyer parents: 
32828diff
changeset | 594 | not(p(FuncUtil.Ctermfunc.apply e one_tm))) | 
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 595 | |
| 46594 | 596 | fun linear_ineqs vars (les,lts) = | 
| 597 | case find_first (contradictory (fn x => x >/ Rat.zero)) lts of | |
| 598 | SOME r => r | |
| 599 | | NONE => | |
| 600 | (case find_first (contradictory (fn x => x >/ Rat.zero)) les of | |
| 601 | SOME r => r | |
| 602 | | NONE => | |
| 603 | if null vars then error "linear_ineqs: no contradiction" else | |
| 604 | let | |
| 605 | val ineqs = les @ lts | |
| 606 | fun blowup v = | |
| 607 | length(filter (fn (e,_) => FuncUtil.Ctermfunc.tryapplyd e v Rat.zero =/ Rat.zero) ineqs) + | |
| 608 | length(filter (fn (e,_) => FuncUtil.Ctermfunc.tryapplyd e v Rat.zero >/ Rat.zero) ineqs) * | |
| 609 | length(filter (fn (e,_) => FuncUtil.Ctermfunc.tryapplyd e v Rat.zero </ Rat.zero) ineqs) | |
| 610 | val v = fst(hd(sort (fn ((_,i),(_,j)) => int_ord (i,j)) | |
| 611 | (map (fn v => (v,blowup v)) vars))) | |
| 612 | fun addup (e1,p1) (e2,p2) acc = | |
| 613 | let | |
| 614 | val c1 = FuncUtil.Ctermfunc.tryapplyd e1 v Rat.zero | |
| 615 | val c2 = FuncUtil.Ctermfunc.tryapplyd e2 v Rat.zero | |
| 616 | in | |
| 617 | if c1 */ c2 >=/ Rat.zero then acc else | |
| 618 | let | |
| 619 | val e1' = linear_cmul (Rat.abs c2) e1 | |
| 620 | val e2' = linear_cmul (Rat.abs c1) e2 | |
| 621 | val p1' = Product(Rational_lt(Rat.abs c2),p1) | |
| 622 | val p2' = Product(Rational_lt(Rat.abs c1),p2) | |
| 623 | in (linear_add e1' e2',Sum(p1',p2'))::acc | |
| 624 | end | |
| 625 | end | |
| 626 | val (les0,les1) = | |
| 627 | List.partition (fn (e,_) => FuncUtil.Ctermfunc.tryapplyd e v Rat.zero =/ Rat.zero) les | |
| 628 | val (lts0,lts1) = | |
| 629 | List.partition (fn (e,_) => FuncUtil.Ctermfunc.tryapplyd e v Rat.zero =/ Rat.zero) lts | |
| 630 | val (lesp,lesn) = | |
| 631 | List.partition (fn (e,_) => FuncUtil.Ctermfunc.tryapplyd e v Rat.zero >/ Rat.zero) les1 | |
| 632 | val (ltsp,ltsn) = | |
| 633 | List.partition (fn (e,_) => FuncUtil.Ctermfunc.tryapplyd e v Rat.zero >/ Rat.zero) lts1 | |
| 634 | val les' = fold_rev (fn ep1 => fold_rev (addup ep1) lesp) lesn les0 | |
| 635 | val lts' = fold_rev (fn ep1 => fold_rev (addup ep1) (lesp@ltsp)) ltsn | |
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 636 | (fold_rev (fn ep1 => fold_rev (addup ep1) (lesn@ltsn)) ltsp lts0) | 
| 46594 | 637 | in linear_ineqs (remove (op aconvc) v vars) (les',lts') | 
| 638 | end) | |
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 639 | |
| 46594 | 640 | fun linear_eqs(eqs,les,lts) = | 
| 641 | case find_first (contradictory (fn x => x =/ Rat.zero)) eqs of | |
| 642 | SOME r => r | |
| 643 | | NONE => | |
| 644 | (case eqs of | |
| 645 | [] => | |
| 646 | let val vars = remove (op aconvc) one_tm | |
| 647 | (fold_rev (union (op aconvc) o FuncUtil.Ctermfunc.dom o fst) (les@lts) []) | |
| 648 | in linear_ineqs vars (les,lts) end | |
| 649 | | (e,p)::es => | |
| 650 | if FuncUtil.Ctermfunc.is_empty e then linear_eqs (es,les,lts) else | |
| 651 | let | |
| 652 | val (x,c) = FuncUtil.Ctermfunc.choose (FuncUtil.Ctermfunc.delete_safe one_tm e) | |
| 653 | fun xform (inp as (t,q)) = | |
| 654 | let val d = FuncUtil.Ctermfunc.tryapplyd t x Rat.zero in | |
| 655 | if d =/ Rat.zero then inp else | |
| 656 | let | |
| 657 | val k = (Rat.neg d) */ Rat.abs c // c | |
| 658 | val e' = linear_cmul k e | |
| 659 | val t' = linear_cmul (Rat.abs c) t | |
| 660 | val p' = Eqmul(FuncUtil.Monomialfunc.onefunc (FuncUtil.Ctermfunc.empty, k),p) | |
| 661 | val q' = Product(Rational_lt(Rat.abs c),q) | |
| 662 | in (linear_add e' t',Sum(p',q')) | |
| 663 | end | |
| 664 | end | |
| 665 | in linear_eqs(map xform es,map xform les,map xform lts) | |
| 666 | end) | |
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 667 | |
| 46594 | 668 | fun linear_prover (eq,le,lt) = | 
| 669 | let | |
| 670 | val eqs = map_index (fn (n, p) => (p,Axiom_eq n)) eq | |
| 671 | val les = map_index (fn (n, p) => (p,Axiom_le n)) le | |
| 672 | val lts = map_index (fn (n, p) => (p,Axiom_lt n)) lt | |
| 673 | in linear_eqs(eqs,les,lts) | |
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 674 | end | 
| 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 675 | |
| 46594 | 676 | fun lin_of_hol ct = | 
| 677 |     if ct aconvc @{cterm "0::real"} then FuncUtil.Ctermfunc.empty
 | |
| 678 | else if not (is_comb ct) then FuncUtil.Ctermfunc.onefunc (ct, Rat.one) | |
| 679 | else if is_ratconst ct then FuncUtil.Ctermfunc.onefunc (one_tm, dest_ratconst ct) | |
| 680 | else | |
| 681 | let val (lop,r) = Thm.dest_comb ct | |
| 682 | in | |
| 683 | if not (is_comb lop) then FuncUtil.Ctermfunc.onefunc (ct, Rat.one) | |
| 684 | else | |
| 685 | let val (opr,l) = Thm.dest_comb lop | |
| 686 | in | |
| 687 |             if opr aconvc @{cterm "op + :: real =>_"}
 | |
| 688 | then linear_add (lin_of_hol l) (lin_of_hol r) | |
| 689 |             else if opr aconvc @{cterm "op * :: real =>_"}
 | |
| 690 | andalso is_ratconst l then FuncUtil.Ctermfunc.onefunc (r, dest_ratconst l) | |
| 691 | else FuncUtil.Ctermfunc.onefunc (ct, Rat.one) | |
| 692 | end | |
| 693 | end | |
| 694 | ||
| 695 | fun is_alien ct = | |
| 59582 | 696 | case Thm.term_of ct of | 
| 697 |       Const(@{const_name "real"}, _)$ n =>
 | |
| 698 | if can HOLogic.dest_number n then false else true | |
| 699 | | _ => false | |
| 46594 | 700 | in | 
| 701 | fun real_linear_prover translator (eq,le,lt) = | |
| 702 | let | |
| 59582 | 703 | val lhs = lin_of_hol o Thm.dest_arg1 o Thm.dest_arg o Thm.cprop_of | 
| 704 | val rhs = lin_of_hol o Thm.dest_arg o Thm.dest_arg o Thm.cprop_of | |
| 46594 | 705 | val eq_pols = map lhs eq | 
| 706 | val le_pols = map rhs le | |
| 707 | val lt_pols = map rhs lt | |
| 708 | val aliens = filter is_alien | |
| 709 | (fold_rev (union (op aconvc) o FuncUtil.Ctermfunc.dom) | |
| 710 | (eq_pols @ le_pols @ lt_pols) []) | |
| 711 | val le_pols' = le_pols @ map (fn v => FuncUtil.Ctermfunc.onefunc (v,Rat.one)) aliens | |
| 712 | val (_,proof) = linear_prover (eq_pols,le_pols',lt_pols) | |
| 60801 | 713 |     val le' = le @ map (fn a => Thm.instantiate' [] [SOME (Thm.dest_arg a)] @{thm real_of_nat_ge_zero}) aliens
 | 
| 46594 | 714 | in ((translator (eq,le',lt) proof), Trivial) | 
| 715 | end | |
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 716 | end; | 
| 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 717 | |
| 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 718 | (* A less general generic arithmetic prover dealing with abs,max and min*) | 
| 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 719 | |
| 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 720 | local | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
46594diff
changeset | 721 | val absmaxmin_elim_ss1 = | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
46594diff
changeset | 722 |     simpset_of (put_simpset HOL_basic_ss @{context} addsimps real_abs_thms1)
 | 
| 46594 | 723 | fun absmaxmin_elim_conv1 ctxt = | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
46594diff
changeset | 724 | Simplifier.rewrite (put_simpset absmaxmin_elim_ss1 ctxt) | 
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 725 | |
| 46594 | 726 | val absmaxmin_elim_conv2 = | 
| 727 | let | |
| 60801 | 728 |       val pth_abs = Thm.instantiate' [SOME @{ctyp real}] [] abs_split'
 | 
| 729 |       val pth_max = Thm.instantiate' [SOME @{ctyp real}] [] max_split
 | |
| 730 |       val pth_min = Thm.instantiate' [SOME @{ctyp real}] [] min_split
 | |
| 46594 | 731 |       val abs_tm = @{cterm "abs :: real => _"}
 | 
| 60642 
48dd1cefb4ae
simplified Thm.instantiate and derivatives: the LHS refers to non-certified variables -- this merely serves as index into already certified structures (or is ignored);
 wenzelm parents: 
59586diff
changeset | 732 |       val p_v = (("P", 0), @{typ "real \<Rightarrow> bool"})
 | 
| 
48dd1cefb4ae
simplified Thm.instantiate and derivatives: the LHS refers to non-certified variables -- this merely serves as index into already certified structures (or is ignored);
 wenzelm parents: 
59586diff
changeset | 733 |       val x_v = (("x", 0), @{typ real})
 | 
| 
48dd1cefb4ae
simplified Thm.instantiate and derivatives: the LHS refers to non-certified variables -- this merely serves as index into already certified structures (or is ignored);
 wenzelm parents: 
59586diff
changeset | 734 |       val y_v = (("y", 0), @{typ real})
 | 
| 46594 | 735 |       val is_max = is_binop @{cterm "max :: real => _"}
 | 
| 736 |       val is_min = is_binop @{cterm "min :: real => _"}
 | |
| 737 | fun is_abs t = is_comb t andalso Thm.dest_fun t aconvc abs_tm | |
| 738 | fun eliminate_construct p c tm = | |
| 739 | let | |
| 740 | val t = find_cterm p tm | |
| 741 | val th0 = (Thm.symmetric o Thm.beta_conversion false) (Thm.apply (Thm.lambda t tm) t) | |
| 742 | val (p,ax) = (Thm.dest_comb o Thm.rhs_of) th0 | |
| 743 | in fconv_rule(arg_conv(binop_conv (arg_conv (Thm.beta_conversion false)))) | |
| 744 | (Thm.transitive th0 (c p ax)) | |
| 745 | end | |
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 746 | |
| 46594 | 747 | val elim_abs = eliminate_construct is_abs | 
| 748 | (fn p => fn ax => | |
| 60642 
48dd1cefb4ae
simplified Thm.instantiate and derivatives: the LHS refers to non-certified variables -- this merely serves as index into already certified structures (or is ignored);
 wenzelm parents: 
59586diff
changeset | 749 | Thm.instantiate ([], [(p_v,p), (x_v, Thm.dest_arg ax)]) pth_abs) | 
| 46594 | 750 | val elim_max = eliminate_construct is_max | 
| 751 | (fn p => fn ax => | |
| 752 | let val (ax,y) = Thm.dest_comb ax | |
| 60642 
48dd1cefb4ae
simplified Thm.instantiate and derivatives: the LHS refers to non-certified variables -- this merely serves as index into already certified structures (or is ignored);
 wenzelm parents: 
59586diff
changeset | 753 | in Thm.instantiate ([], [(p_v,p), (x_v, Thm.dest_arg ax), (y_v,y)]) | 
| 46594 | 754 | pth_max end) | 
| 755 | val elim_min = eliminate_construct is_min | |
| 756 | (fn p => fn ax => | |
| 757 | let val (ax,y) = Thm.dest_comb ax | |
| 60642 
48dd1cefb4ae
simplified Thm.instantiate and derivatives: the LHS refers to non-certified variables -- this merely serves as index into already certified structures (or is ignored);
 wenzelm parents: 
59586diff
changeset | 758 | in Thm.instantiate ([], [(p_v,p), (x_v, Thm.dest_arg ax), (y_v,y)]) | 
| 46594 | 759 | pth_min end) | 
| 760 | in first_conv [elim_abs, elim_max, elim_min, all_conv] | |
| 761 | end; | |
| 762 | in | |
| 763 | fun gen_real_arith ctxt (mkconst,eq,ge,gt,norm,neg,add,mul,prover) = | |
| 764 | gen_gen_real_arith ctxt | |
| 765 | (mkconst,eq,ge,gt,norm,neg,add,mul, | |
| 766 | absmaxmin_elim_conv1 ctxt,absmaxmin_elim_conv2,prover) | |
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 767 | end; | 
| 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 768 | |
| 46594 | 769 | (* An instance for reals*) | 
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 770 | |
| 46594 | 771 | fun gen_prover_real_arith ctxt prover = | 
| 772 | let | |
| 59582 | 773 | fun simple_cterm_ord t u = Term_Ord.term_ord (Thm.term_of t, Thm.term_of u) = LESS | 
| 46594 | 774 |     val {add, mul, neg, pow = _, sub = _, main} =
 | 
| 775 | Semiring_Normalizer.semiring_normalizers_ord_wrapper ctxt | |
| 776 |         (the (Semiring_Normalizer.match ctxt @{cterm "(0::real) + 1"}))
 | |
| 777 | simple_cterm_ord | |
| 778 | in gen_real_arith ctxt | |
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
46594diff
changeset | 779 | (cterm_of_rat, | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
46594diff
changeset | 780 | Numeral_Simprocs.field_comp_conv ctxt, | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
46594diff
changeset | 781 | Numeral_Simprocs.field_comp_conv ctxt, | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
46594diff
changeset | 782 | Numeral_Simprocs.field_comp_conv ctxt, | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
46594diff
changeset | 783 | main ctxt, neg ctxt, add ctxt, mul ctxt, prover) | 
| 46594 | 784 | end; | 
| 31120 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 785 | |
| 
fc654c95c29e
A generic arithmetic prover based on Positivstellensatz certificates --- also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination
 chaieb parents: diff
changeset | 786 | end |