src/HOL/Library/Sum_of_Squares/sum_of_squares.ML
author haftmann
Tue, 19 Nov 2013 10:05:53 +0100
changeset 54489 03ff4d1e6784
parent 53975 22ee3fb9d596
child 54742 7a86358a3c0b
permissions -rw-r--r--
eliminiated neg_numeral in favour of - (numeral _)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
41474
60d091240485 renamed Sum_Of_Squares to Sum_of_Squares;
wenzelm
parents: 40721
diff changeset
     1
(*  Title:      HOL/Library/Sum_of_Squares/sum_of_squares.ML
32949
aa6c470a962a eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents: 32839
diff changeset
     2
    Author:     Amine Chaieb, University of Cambridge
aa6c470a962a eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents: 32839
diff changeset
     3
    Author:     Philipp Meyer, TU Muenchen
32268
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents: 32150
diff changeset
     4
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
     5
A tactic for proving nonlinear inequalities.
32268
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents: 32150
diff changeset
     6
*)
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents: 32150
diff changeset
     7
32949
aa6c470a962a eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents: 32839
diff changeset
     8
signature SUM_OF_SQUARES =
32268
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents: 32150
diff changeset
     9
sig
32949
aa6c470a962a eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents: 32839
diff changeset
    10
  datatype proof_method = Certificate of RealArith.pss_tree | Prover of string -> string
aa6c470a962a eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents: 32839
diff changeset
    11
  val sos_tac: (RealArith.pss_tree -> unit) -> proof_method -> Proof.context -> int -> tactic
38805
b09d8603f865 Sum_Of_Squares: proper configuration options;
wenzelm
parents: 38795
diff changeset
    12
  val trace: bool Config.T
32332
bc5cec7b2be6 misc changes to SOS by Philipp Meyer:
wenzelm
parents: 32268
diff changeset
    13
  exception Failure of string;
32268
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents: 32150
diff changeset
    14
end
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents: 32150
diff changeset
    15
41474
60d091240485 renamed Sum_Of_Squares to Sum_of_Squares;
wenzelm
parents: 40721
diff changeset
    16
structure Sum_of_Squares: SUM_OF_SQUARES =
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    17
struct
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    18
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    19
val rat_0 = Rat.zero;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    20
val rat_1 = Rat.one;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    21
val rat_2 = Rat.two;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    22
val rat_10 = Rat.rat_of_int 10;
33029
2fefe039edf1 uniform use of Integer.min/max;
wenzelm
parents: 33002
diff changeset
    23
val max = Integer.max;
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    24
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    25
val denominator_rat = Rat.quotient_of_rat #> snd #> Rat.rat_of_int;
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
    26
fun int_of_rat a =
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    27
    case Rat.quotient_of_rat a of (i,1) => i | _ => error "int_of_rat: not an int";
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    28
fun lcm_rat x y = Rat.rat_of_int (Integer.lcm (int_of_rat x) (int_of_rat y));
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    29
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
    30
fun rat_pow r i =
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
    31
 let fun pow r i =
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
    32
   if i = 0 then rat_1 else
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    33
   let val d = pow r (i div 2)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    34
   in d */ d */ (if i mod 2 = 0 then rat_1 else r)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    35
   end
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    36
 in if i < 0 then pow (Rat.inv r) (~ i) else pow r i end;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    37
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
    38
fun round_rat r =
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    39
 let val (a,b) = Rat.quotient_of_rat (Rat.abs r)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    40
     val d = a div b
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    41
     val s = if r </ rat_0 then (Rat.neg o Rat.rat_of_int) else Rat.rat_of_int
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    42
     val x2 = 2 * (a - (b * d))
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    43
 in s (if x2 >= b then d + 1 else d) end
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    44
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    45
val abs_rat = Rat.abs;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    46
val pow2 = rat_pow rat_2;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    47
val pow10 = rat_pow rat_10;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    48
42616
92715b528e78 added Attrib.setup_config_XXX conveniences, with implicit setup of the background theory;
wenzelm
parents: 41490
diff changeset
    49
val trace = Attrib.setup_config_bool @{binding sos_trace} (K false);
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    50
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    51
exception Sanity;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    52
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    53
exception Unsolvable;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    54
32332
bc5cec7b2be6 misc changes to SOS by Philipp Meyer:
wenzelm
parents: 32268
diff changeset
    55
exception Failure of string;
bc5cec7b2be6 misc changes to SOS by Philipp Meyer:
wenzelm
parents: 32268
diff changeset
    56
32645
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
    57
datatype proof_method =
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
    58
    Certificate of RealArith.pss_tree
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
    59
  | Prover of (string -> string)
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
    60
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    61
(* Turn a rational into a decimal string with d sig digits.                  *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    62
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    63
local
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    64
fun normalize y =
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    65
  if abs_rat y </ (rat_1 // rat_10) then normalize (rat_10 */ y) - 1
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    66
  else if abs_rat y >=/ rat_1 then normalize (y // rat_10) + 1
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
    67
  else 0
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    68
 in
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    69
fun decimalize d x =
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    70
  if x =/ rat_0 then "0.0" else
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
    71
  let
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    72
   val y = Rat.abs x
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    73
   val e = normalize y
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    74
   val z = pow10(~ e) */ y +/ rat_1
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    75
   val k = int_of_rat (round_rat(pow10 d */ z))
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    76
  in (if x </ rat_0 then "-0." else "0.") ^
40627
becf5d5187cc renamed raw "explode" function to "raw_explode" to emphasize its meaning;
wenzelm
parents: 39027
diff changeset
    77
     implode(tl(raw_explode(string_of_int k))) ^
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    78
     (if e = 0 then "" else "e"^string_of_int e)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    79
  end
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    80
end;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    81
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    82
(* Iterations over numbers, and lists indexed by numbers.                    *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    83
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    84
fun itern k l f a =
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    85
  case l of
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    86
    [] => a
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    87
  | h::t => itern (k + 1) t f (f h k a);
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    88
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    89
fun iter (m,n) f a =
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    90
  if n < m then a
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    91
  else iter (m+1,n) f (f m a);
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    92
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    93
(* The main types.                                                           *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    94
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
    95
type vector = int* Rat.rat FuncUtil.Intfunc.table;
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    96
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
    97
type matrix = (int*int)*(Rat.rat FuncUtil.Intpairfunc.table);
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    98
44453
082edd97ffe1 comment out dead code to avoid compiler warnings
huffman
parents: 42616
diff changeset
    99
fun iszero (_,r) = r =/ rat_0;
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   100
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   101
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   102
(* Vectors. Conventionally indexed 1..n.                                     *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   103
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   104
fun vector_0 n = (n,FuncUtil.Intfunc.empty):vector;
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   105
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   106
fun dim (v:vector) = fst v;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   107
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   108
fun vector_cmul c (v:vector) =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   109
 let val n = dim v
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   110
 in if c =/ rat_0 then vector_0 n
39027
e4262f9e6a4e Table.map replaces Table.map'
haftmann
parents: 38805
diff changeset
   111
    else (n,FuncUtil.Intfunc.map (fn _ => fn x => c */ x) (snd v))
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   112
 end;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   113
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   114
fun vector_of_list l =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   115
 let val n = length l
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   116
 in (n,fold_rev2 (curry FuncUtil.Intfunc.update) (1 upto n) l FuncUtil.Intfunc.empty) :vector
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   117
 end;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   118
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   119
(* Matrices; again rows and columns indexed from 1.                          *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   120
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   121
fun dimensions (m:matrix) = fst m;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   122
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   123
fun row k (m:matrix) =
44453
082edd97ffe1 comment out dead code to avoid compiler warnings
huffman
parents: 42616
diff changeset
   124
 let val (_,j) = dimensions m
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   125
 in (j,
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   126
   FuncUtil.Intpairfunc.fold (fn ((i,j), c) => fn a => if i = k then FuncUtil.Intfunc.update (j,c) a else a) (snd m) FuncUtil.Intfunc.empty ) : vector
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   127
 end;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   128
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   129
(* Monomials.                                                                *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   130
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   131
fun monomial_eval assig m =
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   132
  FuncUtil.Ctermfunc.fold (fn (x, k) => fn a => a */ rat_pow (FuncUtil.Ctermfunc.apply assig x) k)
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   133
        m rat_1;
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   134
val monomial_1 = FuncUtil.Ctermfunc.empty;
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   135
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   136
fun monomial_var x = FuncUtil.Ctermfunc.onefunc (x, 1);
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   137
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   138
val monomial_mul =
33002
f3f02f36a3e2 uniform use of Integer.add/mult/sum/prod;
wenzelm
parents: 32949
diff changeset
   139
  FuncUtil.Ctermfunc.combine Integer.add (K false);
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   140
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   141
fun monomial_multidegree m =
44453
082edd97ffe1 comment out dead code to avoid compiler warnings
huffman
parents: 42616
diff changeset
   142
 FuncUtil.Ctermfunc.fold (fn (_, k) => fn a => k + a) m 0;;
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   143
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   144
fun monomial_variables m = FuncUtil.Ctermfunc.dom m;;
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   145
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   146
(* Polynomials.                                                              *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   147
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   148
fun eval assig p =
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   149
  FuncUtil.Monomialfunc.fold (fn (m, c) => fn a => a +/ c */ monomial_eval assig m) p rat_0;
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   150
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   151
val poly_0 = FuncUtil.Monomialfunc.empty;
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   152
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   153
fun poly_isconst p =
44453
082edd97ffe1 comment out dead code to avoid compiler warnings
huffman
parents: 42616
diff changeset
   154
  FuncUtil.Monomialfunc.fold (fn (m, _) => fn a => FuncUtil.Ctermfunc.is_empty m andalso a) p true;
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   155
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   156
fun poly_var x = FuncUtil.Monomialfunc.onefunc (monomial_var x,rat_1);
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   157
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   158
fun poly_const c =
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   159
  if c =/ rat_0 then poly_0 else FuncUtil.Monomialfunc.onefunc(monomial_1, c);
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   160
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   161
fun poly_cmul c p =
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   162
  if c =/ rat_0 then poly_0
39027
e4262f9e6a4e Table.map replaces Table.map'
haftmann
parents: 38805
diff changeset
   163
  else FuncUtil.Monomialfunc.map (fn _ => fn x => c */ x) p;
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   164
39027
e4262f9e6a4e Table.map replaces Table.map'
haftmann
parents: 38805
diff changeset
   165
fun poly_neg p = FuncUtil.Monomialfunc.map (K Rat.neg) p;;
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   166
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   167
fun poly_add p1 p2 =
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   168
  FuncUtil.Monomialfunc.combine (curry op +/) (fn x => x =/ rat_0) p1 p2;
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   169
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   170
fun poly_sub p1 p2 = poly_add p1 (poly_neg p2);
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   171
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   172
fun poly_cmmul (c,m) p =
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   173
 if c =/ rat_0 then poly_0
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   174
 else if FuncUtil.Ctermfunc.is_empty m
39027
e4262f9e6a4e Table.map replaces Table.map'
haftmann
parents: 38805
diff changeset
   175
      then FuncUtil.Monomialfunc.map (fn _ => fn d => c */ d) p
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   176
      else FuncUtil.Monomialfunc.fold (fn (m', d) => fn a => (FuncUtil.Monomialfunc.update (monomial_mul m m', c */ d) a)) p poly_0;
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   177
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   178
fun poly_mul p1 p2 =
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   179
  FuncUtil.Monomialfunc.fold (fn (m, c) => fn a => poly_add (poly_cmmul (c,m) p2) a) p1 poly_0;
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   180
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   181
fun poly_square p = poly_mul p p;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   182
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   183
fun poly_pow p k =
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   184
 if k = 0 then poly_const rat_1
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   185
 else if k = 1 then p
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   186
 else let val q = poly_square(poly_pow p (k div 2)) in
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   187
      if k mod 2 = 1 then poly_mul p q else q end;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   188
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   189
fun multidegree p =
44453
082edd97ffe1 comment out dead code to avoid compiler warnings
huffman
parents: 42616
diff changeset
   190
  FuncUtil.Monomialfunc.fold (fn (m, _) => fn a => max (monomial_multidegree m) a) p 0;
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   191
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   192
fun poly_variables p =
44453
082edd97ffe1 comment out dead code to avoid compiler warnings
huffman
parents: 42616
diff changeset
   193
  sort FuncUtil.cterm_ord (FuncUtil.Monomialfunc.fold_rev (fn (m, _) => union (is_equal o FuncUtil.cterm_ord) (monomial_variables m)) p []);;
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   194
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   195
(* Conversion from HOL term.                                                 *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   196
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   197
local
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   198
 val neg_tm = @{cterm "uminus :: real => _"}
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   199
 val add_tm = @{cterm "op + :: real => _"}
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   200
 val sub_tm = @{cterm "op - :: real => _"}
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   201
 val mul_tm = @{cterm "op * :: real => _"}
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   202
 val inv_tm = @{cterm "inverse :: real => _"}
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   203
 val div_tm = @{cterm "op / :: real => _"}
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   204
 val pow_tm = @{cterm "op ^ :: real => _"}
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   205
 val zero_tm = @{cterm "0:: real"}
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   206
 val is_numeral = can (HOLogic.dest_number o term_of)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   207
 fun poly_of_term tm =
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   208
  if tm aconvc zero_tm then poly_0
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   209
  else if RealArith.is_ratconst tm
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   210
       then poly_const(RealArith.dest_ratconst tm)
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   211
  else
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   212
  (let val (lop,r) = Thm.dest_comb tm
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   213
   in if lop aconvc neg_tm then poly_neg(poly_of_term r)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   214
      else if lop aconvc inv_tm then
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   215
       let val p = poly_of_term r
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   216
       in if poly_isconst p
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   217
          then poly_const(Rat.inv (eval FuncUtil.Ctermfunc.empty p))
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   218
          else error "poly_of_term: inverse of non-constant polyomial"
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   219
       end
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   220
   else (let val (opr,l) = Thm.dest_comb lop
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   221
         in
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   222
          if opr aconvc pow_tm andalso is_numeral r
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   223
          then poly_pow (poly_of_term l) ((snd o HOLogic.dest_number o term_of) r)
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   224
          else if opr aconvc add_tm
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   225
           then poly_add (poly_of_term l) (poly_of_term r)
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   226
          else if opr aconvc sub_tm
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   227
           then poly_sub (poly_of_term l) (poly_of_term r)
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   228
          else if opr aconvc mul_tm
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   229
           then poly_mul (poly_of_term l) (poly_of_term r)
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   230
          else if opr aconvc div_tm
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   231
           then let
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   232
                  val p = poly_of_term l
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   233
                  val q = poly_of_term r
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   234
                in if poly_isconst q then poly_cmul (Rat.inv (eval FuncUtil.Ctermfunc.empty q)) p
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   235
                   else error "poly_of_term: division by non-constant polynomial"
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   236
                end
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   237
          else poly_var tm
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   238
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   239
         end
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   240
         handle CTERM ("dest_comb",_) => poly_var tm)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   241
   end
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   242
   handle CTERM ("dest_comb",_) => poly_var tm)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   243
in
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   244
val poly_of_term = fn tm =>
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   245
 if type_of (term_of tm) = @{typ real} then poly_of_term tm
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   246
 else error "poly_of_term: term does not have real type"
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   247
end;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   248
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   249
(* String of vector (just a list of space-separated numbers).                *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   250
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   251
fun sdpa_of_vector (v:vector) =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   252
 let
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   253
  val n = dim v
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   254
  val strs = map (decimalize 20 o (fn i => FuncUtil.Intfunc.tryapplyd (snd v) i rat_0)) (1 upto n)
32830
47c1b15c03fe replaced and tuned uses of foldr1
Philipp Meyer
parents: 32829
diff changeset
   255
 in space_implode " " strs ^ "\n"
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   256
 end;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   257
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   258
fun triple_int_ord ((a,b,c),(a',b',c')) =
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   259
 prod_ord int_ord (prod_ord int_ord int_ord)
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   260
    ((a,(b,c)),(a',(b',c')));
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   261
structure Inttriplefunc = FuncFun(type key = int*int*int val ord = triple_int_ord);
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   262
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   263
fun index_char str chr pos =
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   264
  if pos >= String.size str then ~1
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   265
  else if String.sub(str,pos) = chr then pos
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   266
  else index_char str chr (pos + 1);
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   267
fun rat_of_quotient (a,b) = if b = 0 then rat_0 else Rat.rat_of_quotient (a,b);
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   268
fun rat_of_string s =
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   269
 let val n = index_char s #"/" 0 in
33035
15eab423e573 standardized basic operations on type option;
wenzelm
parents: 33029
diff changeset
   270
  if n = ~1 then s |> Int.fromString |> the |> Rat.rat_of_int
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   271
  else
32949
aa6c470a962a eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents: 32839
diff changeset
   272
   let val SOME numer = Int.fromString(String.substring(s,0,n))
aa6c470a962a eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents: 32839
diff changeset
   273
       val SOME den = Int.fromString (String.substring(s,n+1,String.size s - n - 1))
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   274
   in rat_of_quotient(numer, den)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   275
   end
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   276
 end;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   277
53975
22ee3fb9d596 backout c6297fa1031a -- strange parsers are required to make this work;
wenzelm
parents: 53972
diff changeset
   278
36692
54b64d4ad524 farewell to old-style mem infixes -- type inference in situations with mem_int and mem_string should provide enough information to resolve the type of (op =)
haftmann
parents: 36350
diff changeset
   279
fun isnum x = member (op =) ["0","1","2","3","4","5","6","7","8","9"] x;
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   280
53975
22ee3fb9d596 backout c6297fa1031a -- strange parsers are required to make this work;
wenzelm
parents: 53972
diff changeset
   281
(* More parser basics. *)
22ee3fb9d596 backout c6297fa1031a -- strange parsers are required to make this work;
wenzelm
parents: 53972
diff changeset
   282
(* FIXME improper use of parser combinators ahead *)
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   283
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   284
 val numeral = Scan.one isnum
33906
1eebf19b773e fixed csdp output parser
Philipp Meyer
parents: 33069
diff changeset
   285
 val decimalint = Scan.repeat1 numeral >> (rat_of_string o implode)
1eebf19b773e fixed csdp output parser
Philipp Meyer
parents: 33069
diff changeset
   286
 val decimalfrac = Scan.repeat1 numeral
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   287
    >> (fn s => rat_of_string(implode s) // pow10 (length s))
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   288
 val decimalsig =
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   289
    decimalint -- Scan.option (Scan.$$ "." |-- decimalfrac)
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   290
    >> (fn (h,NONE) => h | (h,SOME x) => h +/ x)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   291
 fun signed prs =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   292
       $$ "-" |-- prs >> Rat.neg
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   293
    || $$ "+" |-- prs
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   294
    || prs;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   295
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   296
fun emptyin def xs = if null xs then (def,xs) else Scan.fail xs
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   297
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   298
 val exponent = ($$ "e" || $$ "E") |-- signed decimalint;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   299
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   300
 val decimal = signed decimalsig -- (emptyin rat_0|| exponent)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   301
    >> (fn (h, x) => h */ pow10 (int_of_rat x));
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   302
53975
22ee3fb9d596 backout c6297fa1031a -- strange parsers are required to make this work;
wenzelm
parents: 53972
diff changeset
   303
 fun mkparser p s =
22ee3fb9d596 backout c6297fa1031a -- strange parsers are required to make this work;
wenzelm
parents: 53972
diff changeset
   304
  let val (x,rst) = p (raw_explode s)
22ee3fb9d596 backout c6297fa1031a -- strange parsers are required to make this work;
wenzelm
parents: 53972
diff changeset
   305
  in if null rst then x
22ee3fb9d596 backout c6297fa1031a -- strange parsers are required to make this work;
wenzelm
parents: 53972
diff changeset
   306
     else error "mkparser: unparsed input"
22ee3fb9d596 backout c6297fa1031a -- strange parsers are required to make this work;
wenzelm
parents: 53972
diff changeset
   307
  end;;
22ee3fb9d596 backout c6297fa1031a -- strange parsers are required to make this work;
wenzelm
parents: 53972
diff changeset
   308
32332
bc5cec7b2be6 misc changes to SOS by Philipp Meyer:
wenzelm
parents: 32268
diff changeset
   309
(* Parse back csdp output.                                                      *)
53975
22ee3fb9d596 backout c6297fa1031a -- strange parsers are required to make this work;
wenzelm
parents: 53972
diff changeset
   310
(* FIXME improper use of parser combinators ahead *)
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   311
44453
082edd97ffe1 comment out dead code to avoid compiler warnings
huffman
parents: 42616
diff changeset
   312
 fun ignore _ = ((),[])
33067
a70d5baa53ee use plain Scan.repeat (NB: Scan.bulk is for cascading sources -- mostly interna use);
wenzelm
parents: 33042
diff changeset
   313
 fun csdpoutput inp =
a70d5baa53ee use plain Scan.repeat (NB: Scan.bulk is for cascading sources -- mostly interna use);
wenzelm
parents: 33042
diff changeset
   314
   ((decimal -- Scan.repeat (Scan.$$ " " |-- Scan.option decimal) >>
a70d5baa53ee use plain Scan.repeat (NB: Scan.bulk is for cascading sources -- mostly interna use);
wenzelm
parents: 33042
diff changeset
   315
    (fn (h,to) => map_filter I ((SOME h)::to))) --| ignore >> vector_of_list) inp
53975
22ee3fb9d596 backout c6297fa1031a -- strange parsers are required to make this work;
wenzelm
parents: 53972
diff changeset
   316
 val parse_csdpoutput = mkparser csdpoutput
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   317
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   318
(* Try some apparently sensible scaling first. Note that this is purely to   *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   319
(* get a cleaner translation to floating-point, and doesn't affect any of    *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   320
(* the results, in principle. In practice it seems a lot better when there   *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   321
(* are extreme numbers in the original problem.                              *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   322
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   323
  (* Version for (int*int*int) keys *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   324
local
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   325
  fun max_rat x y = if x </ y then y else x
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   326
  fun common_denominator fld amat acc =
44453
082edd97ffe1 comment out dead code to avoid compiler warnings
huffman
parents: 42616
diff changeset
   327
      fld (fn (_,c) => fn a => lcm_rat (denominator_rat c) a) amat acc
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   328
  fun maximal_element fld amat acc =
44453
082edd97ffe1 comment out dead code to avoid compiler warnings
huffman
parents: 42616
diff changeset
   329
    fld (fn (_,c) => fn maxa => max_rat maxa (abs_rat c)) amat acc
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   330
fun float_of_rat x = let val (a,b) = Rat.quotient_of_rat x
41490
0f1e411a1448 eliminated obsolete LargeInt -- Int is unbounded;
wenzelm
parents: 41474
diff changeset
   331
                     in Real.fromInt a / Real.fromInt b end;
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   332
fun int_of_float x = (trunc x handle Overflow => 0 | Domain => 0)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   333
in
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   334
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   335
fun tri_scale_then solver (obj:vector)  mats =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   336
 let
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   337
  val cd1 = fold_rev (common_denominator Inttriplefunc.fold) mats (rat_1)
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   338
  val cd2 = common_denominator FuncUtil.Intfunc.fold (snd obj)  (rat_1)
39027
e4262f9e6a4e Table.map replaces Table.map'
haftmann
parents: 38805
diff changeset
   339
  val mats' = map (Inttriplefunc.map (fn _ => fn x => cd1 */ x)) mats
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   340
  val obj' = vector_cmul cd2 obj
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   341
  val max1 = fold_rev (maximal_element Inttriplefunc.fold) mats' (rat_0)
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   342
  val max2 = maximal_element FuncUtil.Intfunc.fold (snd obj') (rat_0)
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   343
  val scal1 = pow2 (20 - int_of_float(Math.ln (float_of_rat max1) / Math.ln 2.0))
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   344
  val scal2 = pow2 (20 - int_of_float(Math.ln (float_of_rat max2) / Math.ln 2.0))
39027
e4262f9e6a4e Table.map replaces Table.map'
haftmann
parents: 38805
diff changeset
   345
  val mats'' = map (Inttriplefunc.map (fn _ => fn x => x */ scal1)) mats'
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   346
  val obj'' = vector_cmul scal2 obj'
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   347
 in solver obj'' mats''
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   348
  end
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   349
end;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   350
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   351
(* Round a vector to "nice" rationals.                                       *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   352
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   353
fun nice_rational n x = round_rat (n */ x) // n;;
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   354
fun nice_vector n ((d,v) : vector) =
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   355
 (d, FuncUtil.Intfunc.fold (fn (i,c) => fn a =>
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   356
   let val y = nice_rational n c
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   357
   in if c =/ rat_0 then a
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   358
      else FuncUtil.Intfunc.update (i,y) a end) v FuncUtil.Intfunc.empty):vector
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   359
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   360
fun dest_ord f x = is_equal (f x);
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   361
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   362
(* Stuff for "equations" ((int*int*int)->num functions).                         *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   363
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   364
fun tri_equation_cmul c eq =
39027
e4262f9e6a4e Table.map replaces Table.map'
haftmann
parents: 38805
diff changeset
   365
  if c =/ rat_0 then Inttriplefunc.empty else Inttriplefunc.map (fn _ => fn d => c */ d) eq;
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   366
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   367
fun tri_equation_add eq1 eq2 = Inttriplefunc.combine (curry op +/) (fn x => x =/ rat_0) eq1 eq2;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   368
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   369
fun tri_equation_eval assig eq =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   370
 let fun value v = Inttriplefunc.apply assig v
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   371
 in Inttriplefunc.fold (fn (v, c) => fn a => a +/ value v */ c) eq rat_0
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   372
 end;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   373
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   374
(* Eliminate all variables, in an essentially arbitrary order.               *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   375
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   376
fun tri_eliminate_all_equations one =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   377
 let
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   378
  fun choose_variable eq =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   379
   let val (v,_) = Inttriplefunc.choose eq
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   380
   in if is_equal (triple_int_ord(v,one)) then
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   381
      let val eq' = Inttriplefunc.delete_safe v eq
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   382
      in if Inttriplefunc.is_empty eq' then error "choose_variable"
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   383
         else fst (Inttriplefunc.choose eq')
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   384
      end
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   385
    else v
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   386
   end
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   387
  fun eliminate dun eqs = case eqs of
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   388
    [] => dun
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   389
  | eq::oeqs =>
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   390
    if Inttriplefunc.is_empty eq then eliminate dun oeqs else
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   391
    let val v = choose_variable eq
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   392
        val a = Inttriplefunc.apply eq v
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   393
        val eq' = tri_equation_cmul ((Rat.rat_of_int ~1) // a)
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   394
                   (Inttriplefunc.delete_safe v eq)
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   395
        fun elim e =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   396
         let val b = Inttriplefunc.tryapplyd e v rat_0
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   397
         in if b =/ rat_0 then e
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   398
            else tri_equation_add e (tri_equation_cmul (Rat.neg b // a) eq)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   399
         end
39027
e4262f9e6a4e Table.map replaces Table.map'
haftmann
parents: 38805
diff changeset
   400
    in eliminate (Inttriplefunc.update(v, eq') (Inttriplefunc.map (K elim) dun))
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   401
                 (map elim oeqs)
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   402
    end
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   403
in fn eqs =>
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   404
 let
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   405
  val assig = eliminate Inttriplefunc.empty eqs
44453
082edd97ffe1 comment out dead code to avoid compiler warnings
huffman
parents: 42616
diff changeset
   406
  val vs = Inttriplefunc.fold (fn (_, f) => fn a => remove (dest_ord triple_int_ord) one (Inttriplefunc.dom f) @ a) assig []
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   407
 in (distinct (dest_ord triple_int_ord) vs,assig)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   408
 end
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   409
end;
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   410
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   411
(* Multiply equation-parametrized poly by regular poly and add accumulator.  *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   412
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   413
fun tri_epoly_pmul p q acc =
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   414
 FuncUtil.Monomialfunc.fold (fn (m1, c) => fn a =>
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   415
  FuncUtil.Monomialfunc.fold (fn (m2,e) => fn b =>
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   416
   let val m =  monomial_mul m1 m2
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   417
       val es = FuncUtil.Monomialfunc.tryapplyd b m Inttriplefunc.empty
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   418
   in FuncUtil.Monomialfunc.update (m,tri_equation_add (tri_equation_cmul c e) es) b
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   419
   end) q a) p acc ;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   420
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   421
(* Hence produce the "relevant" monomials: those whose squares lie in the    *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   422
(* Newton polytope of the monomials in the input. (This is enough according  *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   423
(* to Reznik: "Extremal PSD forms with few terms", Duke Math. Journal,       *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   424
(* vol 45, pp. 363--374, 1978.                                               *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   425
(*                                                                           *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   426
(* These are ordered in sort of decreasing degree. In particular the         *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   427
(* constant monomial is last; this gives an order in diagonalization of the  *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   428
(* quadratic form that will tend to display constants.                       *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   429
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   430
(* Diagonalize (Cholesky/LDU) the matrix corresponding to a quadratic form.  *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   431
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   432
local
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   433
fun diagonalize n i m =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   434
 if FuncUtil.Intpairfunc.is_empty (snd m) then []
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   435
 else
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   436
  let val a11 = FuncUtil.Intpairfunc.tryapplyd (snd m) (i,i) rat_0
32332
bc5cec7b2be6 misc changes to SOS by Philipp Meyer:
wenzelm
parents: 32268
diff changeset
   437
  in if a11 </ rat_0 then raise Failure "diagonalize: not PSD"
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   438
    else if a11 =/ rat_0 then
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   439
          if FuncUtil.Intfunc.is_empty (snd (row i m)) then diagonalize n (i + 1) m
32332
bc5cec7b2be6 misc changes to SOS by Philipp Meyer:
wenzelm
parents: 32268
diff changeset
   440
          else raise Failure "diagonalize: not PSD ___ "
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   441
    else
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   442
     let
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   443
      val v = row i m
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   444
      val v' = (fst v, FuncUtil.Intfunc.fold (fn (i, c) => fn a =>
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   445
       let val y = c // a11
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   446
       in if y = rat_0 then a else FuncUtil.Intfunc.update (i,y) a
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   447
       end)  (snd v) FuncUtil.Intfunc.empty)
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   448
      fun upt0 x y a = if y = rat_0 then a else FuncUtil.Intpairfunc.update (x,y) a
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   449
      val m' =
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   450
      ((n,n),
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   451
      iter (i+1,n) (fn j =>
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   452
          iter (i+1,n) (fn k =>
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   453
              (upt0 (j,k) (FuncUtil.Intpairfunc.tryapplyd (snd m) (j,k) rat_0 -/ FuncUtil.Intfunc.tryapplyd (snd v) j rat_0 */ FuncUtil.Intfunc.tryapplyd (snd v') k rat_0))))
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   454
          FuncUtil.Intpairfunc.empty)
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   455
     in (a11,v')::diagonalize n (i + 1) m'
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   456
     end
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   457
  end
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   458
in
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   459
fun diag m =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   460
 let
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   461
   val nn = dimensions m
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   462
   val n = fst nn
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   463
 in if snd nn <> n then error "diagonalize: non-square matrix"
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   464
    else diagonalize n 1 m
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   465
 end
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   466
end;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   467
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   468
(* Enumeration of monomials with given multidegree bound.                    *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   469
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   470
fun enumerate_monomials d vars =
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   471
 if d < 0 then []
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   472
 else if d = 0 then [FuncUtil.Ctermfunc.empty]
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   473
 else if null vars then [monomial_1] else
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   474
 let val alts =
33063
4d462963a7db map_range (and map_index) combinator
haftmann
parents: 33042
diff changeset
   475
  map_range (fn k => let val oths = enumerate_monomials (d - k) (tl vars)
4d462963a7db map_range (and map_index) combinator
haftmann
parents: 33042
diff changeset
   476
               in map (fn ks => if k = 0 then ks else FuncUtil.Ctermfunc.update (hd vars, k) ks) oths end) (d + 1)
32830
47c1b15c03fe replaced and tuned uses of foldr1
Philipp Meyer
parents: 32829
diff changeset
   477
 in flat alts
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   478
 end;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   479
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   480
(* Enumerate products of distinct input polys with degree <= d.              *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   481
(* We ignore any constant input polynomials.                                 *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   482
(* Give the output polynomial and a record of how it was derived.            *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   483
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   484
fun enumerate_products d pols =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   485
if d = 0 then [(poly_const rat_1,RealArith.Rational_lt rat_1)]
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   486
else if d < 0 then [] else
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   487
case pols of
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   488
   [] => [(poly_const rat_1,RealArith.Rational_lt rat_1)]
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   489
 | (p,b)::ps =>
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   490
    let val e = multidegree p
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   491
    in if e = 0 then enumerate_products d ps else
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   492
       enumerate_products d ps @
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   493
       map (fn (q,c) => (poly_mul p q,RealArith.Product(b,c)))
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   494
         (enumerate_products (d - e) ps)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   495
    end
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   496
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   497
(* Convert regular polynomial. Note that we treat (0,0,0) as -1.             *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   498
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   499
fun epoly_of_poly p =
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   500
  FuncUtil.Monomialfunc.fold (fn (m,c) => fn a => FuncUtil.Monomialfunc.update (m, Inttriplefunc.onefunc ((0,0,0), Rat.neg c)) a) p FuncUtil.Monomialfunc.empty;
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   501
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   502
(* String for block diagonal matrix numbered k.                              *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   503
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   504
fun sdpa_of_blockdiagonal k m =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   505
 let
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   506
  val pfx = string_of_int k ^" "
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   507
  val ents =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   508
    Inttriplefunc.fold
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   509
      (fn ((b,i,j),c) => fn a => if i > j then a else ((b,i,j),c)::a)
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   510
      m []
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   511
  val entss = sort (triple_int_ord o pairself fst) ents
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   512
 in fold_rev (fn ((b,i,j),c) => fn a =>
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   513
     pfx ^ string_of_int b ^ " " ^ string_of_int i ^ " " ^ string_of_int j ^
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   514
     " " ^ decimalize 20 c ^ "\n" ^ a) entss ""
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   515
 end;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   516
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   517
(* SDPA for problem using block diagonal (i.e. multiple SDPs)                *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   518
32268
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents: 32150
diff changeset
   519
fun sdpa_of_blockproblem nblocks blocksizes obj mats =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   520
 let val m = length mats - 1
32268
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents: 32150
diff changeset
   521
 in
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   522
  string_of_int m ^ "\n" ^
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   523
  string_of_int nblocks ^ "\n" ^
32830
47c1b15c03fe replaced and tuned uses of foldr1
Philipp Meyer
parents: 32829
diff changeset
   524
  (space_implode " " (map string_of_int blocksizes)) ^
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   525
  "\n" ^
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   526
  sdpa_of_vector obj ^
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   527
  fold_rev2 (fn k => fn m => fn a => sdpa_of_blockdiagonal (k - 1) m ^ a)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   528
    (1 upto length mats) mats ""
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   529
 end;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   530
32268
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents: 32150
diff changeset
   531
(* Run prover on a problem in block diagonal form.                       *)
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents: 32150
diff changeset
   532
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents: 32150
diff changeset
   533
fun run_blockproblem prover nblocks blocksizes obj mats=
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents: 32150
diff changeset
   534
  parse_csdpoutput (prover (sdpa_of_blockproblem nblocks blocksizes obj mats))
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents: 32150
diff changeset
   535
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   536
(* 3D versions of matrix operations to consider blocks separately.           *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   537
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   538
val bmatrix_add = Inttriplefunc.combine (curry op +/) (fn x => x =/ rat_0);
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   539
fun bmatrix_cmul c bm =
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   540
  if c =/ rat_0 then Inttriplefunc.empty
39027
e4262f9e6a4e Table.map replaces Table.map'
haftmann
parents: 38805
diff changeset
   541
  else Inttriplefunc.map (fn _ => fn x => c */ x) bm;
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   542
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   543
val bmatrix_neg = bmatrix_cmul (Rat.rat_of_int ~1);
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   544
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   545
(* Smash a block matrix into components.                                     *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   546
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   547
fun blocks blocksizes bm =
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   548
 map (fn (bs,b0) =>
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   549
      let val m = Inttriplefunc.fold
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   550
          (fn ((b,i,j),c) => fn a => if b = b0 then FuncUtil.Intpairfunc.update ((i,j),c) a else a) bm FuncUtil.Intpairfunc.empty
44453
082edd97ffe1 comment out dead code to avoid compiler warnings
huffman
parents: 42616
diff changeset
   551
          val _ = FuncUtil.Intpairfunc.fold (fn ((i,j),_) => fn a => max a (max i j)) m 0
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   552
      in (((bs,bs),m):matrix) end)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   553
 (blocksizes ~~ (1 upto length blocksizes));;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   554
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   555
(* FIXME : Get rid of this !!!*)
32268
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents: 32150
diff changeset
   556
local
44453
082edd97ffe1 comment out dead code to avoid compiler warnings
huffman
parents: 42616
diff changeset
   557
  fun tryfind_with msg _ [] = raise Failure msg
082edd97ffe1 comment out dead code to avoid compiler warnings
huffman
parents: 42616
diff changeset
   558
    | tryfind_with _ f (x::xs) = (f x handle Failure s => tryfind_with s f xs);
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   559
in
32268
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents: 32150
diff changeset
   560
  fun tryfind f = tryfind_with "tryfind" f
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents: 32150
diff changeset
   561
end
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents: 32150
diff changeset
   562
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   563
(* Positiv- and Nullstellensatz. Flag "linf" forces a linear representation. *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   564
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   565
38805
b09d8603f865 Sum_Of_Squares: proper configuration options;
wenzelm
parents: 38795
diff changeset
   566
fun real_positivnullstellensatz_general ctxt prover linf d eqs leqs pol =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   567
let
33042
ddf1f03a9ad9 curried union as canonical list operation
haftmann
parents: 33039
diff changeset
   568
 val vars = fold_rev (union (op aconvc) o poly_variables)
ddf1f03a9ad9 curried union as canonical list operation
haftmann
parents: 33039
diff changeset
   569
   (pol :: eqs @ map fst leqs) []
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   570
 val monoid = if linf then
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   571
      (poly_const rat_1,RealArith.Rational_lt rat_1)::
44453
082edd97ffe1 comment out dead code to avoid compiler warnings
huffman
parents: 42616
diff changeset
   572
      (filter (fn (p,_) => multidegree p <= d) leqs)
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   573
    else enumerate_products d leqs
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   574
 val nblocks = length monoid
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   575
 fun mk_idmultiplier k p =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   576
  let
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   577
   val e = d - multidegree p
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   578
   val mons = enumerate_monomials e vars
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   579
   val nons = mons ~~ (1 upto length mons)
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   580
  in (mons,
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   581
      fold_rev (fn (m,n) => FuncUtil.Monomialfunc.update(m,Inttriplefunc.onefunc((~k,~n,n),rat_1))) nons FuncUtil.Monomialfunc.empty)
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   582
  end
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   583
44453
082edd97ffe1 comment out dead code to avoid compiler warnings
huffman
parents: 42616
diff changeset
   584
 fun mk_sqmultiplier k (p,_) =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   585
  let
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   586
   val e = (d - multidegree p) div 2
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   587
   val mons = enumerate_monomials e vars
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   588
   val nons = mons ~~ (1 upto length mons)
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   589
  in (mons,
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   590
      fold_rev (fn (m1,n1) =>
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   591
       fold_rev (fn (m2,n2) => fn  a =>
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   592
        let val m = monomial_mul m1 m2
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   593
        in if n1 > n2 then a else
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   594
          let val c = if n1 = n2 then rat_1 else rat_2
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   595
              val e = FuncUtil.Monomialfunc.tryapplyd a m Inttriplefunc.empty
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   596
          in FuncUtil.Monomialfunc.update(m, tri_equation_add (Inttriplefunc.onefunc((k,n1,n2), c)) e) a
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   597
          end
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   598
        end)  nons)
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   599
       nons FuncUtil.Monomialfunc.empty)
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   600
  end
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   601
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   602
  val (sqmonlist,sqs) = split_list (map2 mk_sqmultiplier (1 upto length monoid) monoid)
44453
082edd97ffe1 comment out dead code to avoid compiler warnings
huffman
parents: 42616
diff changeset
   603
  val (_(*idmonlist*),ids) =  split_list(map2 mk_idmultiplier (1 upto length eqs) eqs)
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   604
  val blocksizes = map length sqmonlist
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   605
  val bigsum =
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   606
    fold_rev2 (fn p => fn q => fn a => tri_epoly_pmul p q a) eqs ids
44453
082edd97ffe1 comment out dead code to avoid compiler warnings
huffman
parents: 42616
diff changeset
   607
            (fold_rev2 (fn (p,_) => fn s => fn a => tri_epoly_pmul p s a) monoid sqs
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   608
                     (epoly_of_poly(poly_neg pol)))
44453
082edd97ffe1 comment out dead code to avoid compiler warnings
huffman
parents: 42616
diff changeset
   609
  val eqns = FuncUtil.Monomialfunc.fold (fn (_,e) => fn a => e::a) bigsum []
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   610
  val (pvs,assig) = tri_eliminate_all_equations (0,0,0) eqns
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   611
  val qvars = (0,0,0)::pvs
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   612
  val allassig = fold_rev (fn v => Inttriplefunc.update(v,(Inttriplefunc.onefunc(v,rat_1)))) pvs assig
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   613
  fun mk_matrix v =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   614
    Inttriplefunc.fold (fn ((b,i,j), ass) => fn m =>
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   615
        if b < 0 then m else
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   616
         let val c = Inttriplefunc.tryapplyd ass v rat_0
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   617
         in if c = rat_0 then m else
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   618
            Inttriplefunc.update ((b,j,i), c) (Inttriplefunc.update ((b,i,j), c) m)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   619
         end)
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   620
          allassig Inttriplefunc.empty
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   621
  val diagents = Inttriplefunc.fold
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   622
    (fn ((b,i,j), e) => fn a => if b > 0 andalso i = j then tri_equation_add e a else a)
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   623
    allassig Inttriplefunc.empty
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   624
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   625
  val mats = map mk_matrix qvars
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   626
  val obj = (length pvs,
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   627
            itern 1 pvs (fn v => fn i => FuncUtil.Intfunc.updatep iszero (i,Inttriplefunc.tryapplyd diagents v rat_0))
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   628
                        FuncUtil.Intfunc.empty)
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   629
  val raw_vec = if null pvs then vector_0 0
32268
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents: 32150
diff changeset
   630
                else tri_scale_then (run_blockproblem prover nblocks blocksizes) obj mats
44453
082edd97ffe1 comment out dead code to avoid compiler warnings
huffman
parents: 42616
diff changeset
   631
  fun int_element (_,v) i = FuncUtil.Intfunc.tryapplyd v i rat_0
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   632
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   633
  fun find_rounding d =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   634
   let
32949
aa6c470a962a eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents: 32839
diff changeset
   635
    val _ =
38805
b09d8603f865 Sum_Of_Squares: proper configuration options;
wenzelm
parents: 38795
diff changeset
   636
      if Config.get ctxt trace
32949
aa6c470a962a eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents: 32839
diff changeset
   637
      then writeln ("Trying rounding with limit "^Rat.string_of_rat d ^ "\n")
aa6c470a962a eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents: 32839
diff changeset
   638
      else ()
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   639
    val vec = nice_vector d raw_vec
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   640
    val blockmat = iter (1,dim vec)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   641
     (fn i => fn a => bmatrix_add (bmatrix_cmul (int_element vec i) (nth mats i)) a)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   642
     (bmatrix_neg (nth mats 0))
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   643
    val allmats = blocks blocksizes blockmat
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   644
   in (vec,map diag allmats)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   645
   end
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   646
  val (vec,ratdias) =
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   647
    if null pvs then find_rounding rat_1
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   648
    else tryfind find_rounding (map Rat.rat_of_int (1 upto 31) @
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   649
                                map pow2 (5 upto 66))
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   650
  val newassigs =
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   651
    fold_rev (fn k => Inttriplefunc.update (nth pvs (k - 1), int_element vec k))
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   652
           (1 upto dim vec) (Inttriplefunc.onefunc ((0,0,0), Rat.rat_of_int ~1))
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   653
  val finalassigs =
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   654
    Inttriplefunc.fold (fn (v,e) => fn a => Inttriplefunc.update(v, tri_equation_eval newassigs e) a) allassig newassigs
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   655
  fun poly_of_epoly p =
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   656
    FuncUtil.Monomialfunc.fold (fn (v,e) => fn a => FuncUtil.Monomialfunc.updatep iszero (v,tri_equation_eval finalassigs e) a)
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   657
          p FuncUtil.Monomialfunc.empty
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   658
  fun  mk_sos mons =
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   659
   let fun mk_sq (c,m) =
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   660
    (c,fold_rev (fn k=> fn a => FuncUtil.Monomialfunc.updatep iszero (nth mons (k - 1), int_element m k) a)
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   661
                 (1 upto length mons) FuncUtil.Monomialfunc.empty)
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   662
   in map mk_sq
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   663
   end
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   664
  val sqs = map2 mk_sos sqmonlist ratdias
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   665
  val cfs = map poly_of_epoly ids
44453
082edd97ffe1 comment out dead code to avoid compiler warnings
huffman
parents: 42616
diff changeset
   666
  val msq = filter (fn (_,b) => not (null b)) (map2 pair monoid sqs)
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   667
  fun eval_sq sqs = fold_rev (fn (c,q) => poly_add (poly_cmul c (poly_mul q q))) sqs poly_0
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   668
  val sanity =
44453
082edd97ffe1 comment out dead code to avoid compiler warnings
huffman
parents: 42616
diff changeset
   669
    fold_rev (fn ((p,_),s) => poly_add (poly_mul p (eval_sq s))) msq
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   670
           (fold_rev2 (fn p => fn q => poly_add (poly_mul p q)) cfs eqs
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   671
                    (poly_neg pol))
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   672
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   673
in if not(FuncUtil.Monomialfunc.is_empty sanity) then raise Sanity else
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   674
  (cfs,map (fn (a,b) => (snd a,b)) msq)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   675
 end
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   676
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   677
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   678
(* Iterative deepening.                                                      *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   679
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   680
fun deepen f n =
32949
aa6c470a962a eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents: 32839
diff changeset
   681
  (writeln ("Searching with depth limit " ^ string_of_int n);
aa6c470a962a eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents: 32839
diff changeset
   682
    (f n handle Failure s => (writeln ("failed with message: " ^ s); deepen f (n + 1))));
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   683
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   684
32645
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
   685
(* Map back polynomials and their composites to a positivstellensatz.        *)
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   686
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   687
fun cterm_of_sqterm (c,p) = RealArith.Product(RealArith.Rational_lt c,RealArith.Square p);
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   688
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   689
fun cterm_of_sos (pr,sqs) = if null sqs then pr
32830
47c1b15c03fe replaced and tuned uses of foldr1
Philipp Meyer
parents: 32829
diff changeset
   690
  else RealArith.Product(pr,foldr1 RealArith.Sum (map cterm_of_sqterm sqs));
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   691
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   692
(* Interface to HOL.                                                         *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   693
local
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   694
  open Conv
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   695
  val concl = Thm.dest_arg o cprop_of
35408
b48ab741683b modernized structure Term_Ord;
wenzelm
parents: 33906
diff changeset
   696
  fun simple_cterm_ord t u = Term_Ord.fast_term_ord (term_of t, term_of u) = LESS
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   697
in
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   698
  (* FIXME: Replace tryfind by get_first !! *)
32645
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
   699
fun real_nonlinear_prover proof_method ctxt =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   700
 let
44453
082edd97ffe1 comment out dead code to avoid compiler warnings
huffman
parents: 42616
diff changeset
   701
  val {add = _, mul = _, neg = _, pow = _,
082edd97ffe1 comment out dead code to avoid compiler warnings
huffman
parents: 42616
diff changeset
   702
       sub = _, main = real_poly_conv} =
082edd97ffe1 comment out dead code to avoid compiler warnings
huffman
parents: 42616
diff changeset
   703
      Semiring_Normalizer.semiring_normalizers_ord_wrapper ctxt
36753
5cf4e9128f22 renamed Normalizer to the more specific Semiring_Normalizer
haftmann
parents: 36751
diff changeset
   704
      (the (Semiring_Normalizer.match ctxt @{cterm "(0::real) + 1"}))
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   705
     simple_cterm_ord
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   706
  fun mainf cert_choice translator (eqs,les,lts) =
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   707
  let
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   708
   val eq0 = map (poly_of_term o Thm.dest_arg1 o concl) eqs
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   709
   val le0 = map (poly_of_term o Thm.dest_arg o concl) les
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   710
   val lt0 = map (poly_of_term o Thm.dest_arg o concl) lts
33063
4d462963a7db map_range (and map_index) combinator
haftmann
parents: 33042
diff changeset
   711
   val eqp0 = map_index (fn (i, t) => (t,RealArith.Axiom_eq i)) eq0
4d462963a7db map_range (and map_index) combinator
haftmann
parents: 33042
diff changeset
   712
   val lep0 = map_index (fn (i, t) => (t,RealArith.Axiom_le i)) le0
4d462963a7db map_range (and map_index) combinator
haftmann
parents: 33042
diff changeset
   713
   val ltp0 = map_index (fn (i, t) => (t,RealArith.Axiom_lt i)) lt0
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   714
   val (keq,eq) = List.partition (fn (p,_) => multidegree p = 0) eqp0
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   715
   val (klep,lep) = List.partition (fn (p,_) => multidegree p = 0) lep0
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   716
   val (kltp,ltp) = List.partition (fn (p,_) => multidegree p = 0) ltp0
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   717
   fun trivial_axiom (p,ax) =
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   718
    case ax of
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   719
       RealArith.Axiom_eq n => if eval FuncUtil.Ctermfunc.empty p <>/ Rat.zero then nth eqs n
32332
bc5cec7b2be6 misc changes to SOS by Philipp Meyer:
wenzelm
parents: 32268
diff changeset
   720
                     else raise Failure "trivial_axiom: Not a trivial axiom"
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   721
     | RealArith.Axiom_le n => if eval FuncUtil.Ctermfunc.empty p </ Rat.zero then nth les n
32332
bc5cec7b2be6 misc changes to SOS by Philipp Meyer:
wenzelm
parents: 32268
diff changeset
   722
                     else raise Failure "trivial_axiom: Not a trivial axiom"
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   723
     | RealArith.Axiom_lt n => if eval FuncUtil.Ctermfunc.empty p <=/ Rat.zero then nth lts n
32332
bc5cec7b2be6 misc changes to SOS by Philipp Meyer:
wenzelm
parents: 32268
diff changeset
   724
                     else raise Failure "trivial_axiom: Not a trivial axiom"
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   725
     | _ => error "trivial_axiom: Not a trivial axiom"
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   726
   in
32645
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
   727
  (let val th = tryfind trivial_axiom (keq @ klep @ kltp)
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
   728
   in
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 47108
diff changeset
   729
    (fconv_rule (arg_conv (arg1_conv (real_poly_conv ctxt))
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 47108
diff changeset
   730
      then_conv Numeral_Simprocs.field_comp_conv ctxt) th,
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 47108
diff changeset
   731
      RealArith.Trivial)
32645
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
   732
   end)
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   733
   handle Failure _ =>
32645
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
   734
     (let val proof =
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
   735
       (case proof_method of Certificate certs =>
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
   736
         (* choose certificate *)
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
   737
         let
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   738
           fun chose_cert [] (RealArith.Cert c) = c
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   739
             | chose_cert (RealArith.Left::s) (RealArith.Branch (l, _)) = chose_cert s l
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   740
             | chose_cert (RealArith.Right::s) (RealArith.Branch (_, r)) = chose_cert s r
32645
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
   741
             | chose_cert _ _ = error "certificate tree in invalid form"
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
   742
         in
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
   743
           chose_cert cert_choice certs
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
   744
         end
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
   745
       | Prover prover =>
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
   746
         (* call prover *)
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   747
         let
32645
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
   748
          val pol = fold_rev poly_mul (map fst ltp) (poly_const Rat.one)
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
   749
          val leq = lep @ ltp
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
   750
          fun tryall d =
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
   751
           let val e = multidegree pol
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
   752
               val k = if e = 0 then 0 else d div e
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   753
               val eq' = map fst eq
38805
b09d8603f865 Sum_Of_Squares: proper configuration options;
wenzelm
parents: 38795
diff changeset
   754
           in tryfind (fn i => (d,i,real_positivnullstellensatz_general ctxt prover false d eq' leq
32645
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
   755
                                 (poly_neg(poly_pow pol i))))
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
   756
                   (0 upto k)
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
   757
           end
44453
082edd97ffe1 comment out dead code to avoid compiler warnings
huffman
parents: 42616
diff changeset
   758
         val (_,i,(cert_ideal,cert_cone)) = deepen tryall 0
32645
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
   759
         val proofs_ideal =
44453
082edd97ffe1 comment out dead code to avoid compiler warnings
huffman
parents: 42616
diff changeset
   760
           map2 (fn q => fn (_,ax) => RealArith.Eqmul(q,ax)) cert_ideal eq
32645
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
   761
         val proofs_cone = map cterm_of_sos cert_cone
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   762
         val proof_ne = if null ltp then RealArith.Rational_lt Rat.one else
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   763
           let val p = foldr1 RealArith.Product (map snd ltp)
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   764
           in  funpow i (fn q => RealArith.Product(p,q)) (RealArith.Rational_lt Rat.one)
32645
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
   765
           end
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   766
         in
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   767
           foldr1 RealArith.Sum (proof_ne :: proofs_ideal @ proofs_cone)
32645
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
   768
         end)
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
   769
     in
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   770
        (translator (eqs,les,lts) proof, RealArith.Cert proof)
32645
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
   771
     end)
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   772
   end
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   773
 in mainf end
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   774
end
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   775
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   776
fun C f x y = f y x;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   777
  (* FIXME : This is very bad!!!*)
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   778
fun subst_conv eqs t =
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   779
 let
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: 44469
diff changeset
   780
  val t' = fold (Thm.lambda o Thm.lhs_of) eqs t
36945
9bec62c10714 less pervasive names from structure Thm;
wenzelm
parents: 36753
diff changeset
   781
 in Conv.fconv_rule (Thm.beta_conversion true) (fold (C Thm.combination) eqs (Thm.reflexive t'))
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   782
 end
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   783
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   784
(* A wrapper that tries to substitute away variables first.                  *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   785
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   786
local
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   787
 open Conv
35408
b48ab741683b modernized structure Term_Ord;
wenzelm
parents: 33906
diff changeset
   788
  fun simple_cterm_ord t u = Term_Ord.fast_term_ord (term_of t, term_of u) = LESS
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   789
 val concl = Thm.dest_arg o cprop_of
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   790
 val shuffle1 =
36350
bc7982c54e37 dropped group_simps, ring_simps, field_eq_simps
haftmann
parents: 35625
diff changeset
   791
   fconv_rule (rewr_conv @{lemma "(a + x == y) == (x == y - (a::real))" by (atomize (full)) (simp add: field_simps) })
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   792
 val shuffle2 =
36350
bc7982c54e37 dropped group_simps, ring_simps, field_eq_simps
haftmann
parents: 35625
diff changeset
   793
    fconv_rule (rewr_conv @{lemma "(x + a == y) ==  (x == y - (a::real))" by (atomize (full)) (simp add: field_simps)})
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   794
 fun substitutable_monomial fvs tm = case term_of tm of
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   795
    Free(_,@{typ real}) => if not (member (op aconvc) fvs tm) then (Rat.one,tm)
32332
bc5cec7b2be6 misc changes to SOS by Philipp Meyer:
wenzelm
parents: 32268
diff changeset
   796
                           else raise Failure "substitutable_monomial"
44453
082edd97ffe1 comment out dead code to avoid compiler warnings
huffman
parents: 42616
diff changeset
   797
  | @{term "op * :: real => _"}$_$(Free _) =>
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   798
     if RealArith.is_ratconst (Thm.dest_arg1 tm) andalso not (member (op aconvc) fvs (Thm.dest_arg tm))
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   799
         then (RealArith.dest_ratconst (Thm.dest_arg1 tm),Thm.dest_arg tm) else raise Failure "substitutable_monomial"
44453
082edd97ffe1 comment out dead code to avoid compiler warnings
huffman
parents: 42616
diff changeset
   800
  | @{term "op + :: real => _"}$_$_ =>
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   801
       (substitutable_monomial (Thm.add_cterm_frees (Thm.dest_arg tm) fvs) (Thm.dest_arg1 tm)
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   802
        handle Failure _ => substitutable_monomial (Thm.add_cterm_frees (Thm.dest_arg1 tm) fvs) (Thm.dest_arg tm))
32332
bc5cec7b2be6 misc changes to SOS by Philipp Meyer:
wenzelm
parents: 32268
diff changeset
   803
  | _ => raise Failure "substitutable_monomial"
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   804
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   805
  fun isolate_variable v th =
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   806
   let val w = Thm.dest_arg1 (cprop_of th)
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   807
   in if v aconvc w then th
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   808
      else case term_of w of
44453
082edd97ffe1 comment out dead code to avoid compiler warnings
huffman
parents: 42616
diff changeset
   809
           @{term "op + :: real => _"}$_$_ =>
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   810
              if Thm.dest_arg1 w aconvc v then shuffle2 th
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   811
              else isolate_variable v (shuffle1 th)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   812
          | _ => error "isolate variable : This should not happen?"
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   813
   end
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   814
in
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   815
32268
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents: 32150
diff changeset
   816
fun real_nonlinear_subst_prover prover ctxt =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   817
 let
44453
082edd97ffe1 comment out dead code to avoid compiler warnings
huffman
parents: 42616
diff changeset
   818
  val {add = _, mul = real_poly_mul_conv, neg = _,
082edd97ffe1 comment out dead code to avoid compiler warnings
huffman
parents: 42616
diff changeset
   819
       pow = _, sub = _, main = real_poly_conv} =
082edd97ffe1 comment out dead code to avoid compiler warnings
huffman
parents: 42616
diff changeset
   820
      Semiring_Normalizer.semiring_normalizers_ord_wrapper ctxt
36753
5cf4e9128f22 renamed Normalizer to the more specific Semiring_Normalizer
haftmann
parents: 36751
diff changeset
   821
      (the (Semiring_Normalizer.match ctxt @{cterm "(0::real) + 1"}))
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   822
     simple_cterm_ord
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   823
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   824
  fun make_substitution th =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   825
   let
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   826
    val (c,v) = substitutable_monomial [] (Thm.dest_arg1(concl 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: 44469
diff changeset
   827
    val th1 = Drule.arg_cong_rule (Thm.apply @{cterm "op * :: real => _"} (RealArith.cterm_of_rat (Rat.inv c))) (mk_meta_eq th)
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 47108
diff changeset
   828
    val th2 = fconv_rule (binop_conv (real_poly_mul_conv ctxt)) th1
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 47108
diff changeset
   829
   in fconv_rule (arg_conv (real_poly_conv ctxt)) (isolate_variable v th2)
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   830
   end
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   831
   fun oprconv cv ct =
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   832
    let val g = Thm.dest_fun2 ct
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   833
    in if g aconvc @{cterm "op <= :: real => _"}
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   834
         orelse g aconvc @{cterm "op < :: real => _"}
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   835
       then arg_conv cv ct else arg1_conv cv ct
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   836
    end
32645
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
   837
  fun mainf cert_choice translator =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   838
   let
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   839
    fun substfirst(eqs,les,lts) =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   840
      ((let
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   841
           val eth = tryfind make_substitution eqs
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 47108
diff changeset
   842
           val modify =
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 47108
diff changeset
   843
            fconv_rule (arg_conv (oprconv(subst_conv [eth] then_conv (real_poly_conv ctxt))))
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   844
       in  substfirst
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   845
             (filter_out (fn t => (Thm.dest_arg1 o Thm.dest_arg o cprop_of) t
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   846
                                   aconvc @{cterm "0::real"}) (map modify eqs),
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   847
                                   map modify les,map modify lts)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   848
       end)
32645
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
   849
       handle Failure  _ => real_nonlinear_prover prover ctxt cert_choice translator (rev eqs, rev les, rev lts))
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   850
    in substfirst
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   851
   end
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   852
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   853
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   854
 in mainf
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   855
 end
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   856
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   857
(* Overall function. *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   858
32645
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
   859
fun real_sos prover ctxt =
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   860
  RealArith.gen_prover_real_arith ctxt (real_nonlinear_subst_prover prover ctxt)
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   861
end;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   862
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   863
val known_sos_constants =
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   864
  [@{term "op ==>"}, @{term "Trueprop"},
38795
848be46708dc formerly unnamed infix conjunction and disjunction now named HOL.conj and HOL.disj
haftmann
parents: 38786
diff changeset
   865
   @{term HOL.implies}, @{term HOL.conj}, @{term HOL.disj},
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   866
   @{term "Not"}, @{term "op = :: bool => _"},
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   867
   @{term "All :: (real => _) => _"}, @{term "Ex :: (real => _) => _"},
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   868
   @{term "op = :: real => _"}, @{term "op < :: real => _"},
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   869
   @{term "op <= :: real => _"},
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   870
   @{term "op + :: real => _"}, @{term "op - :: real => _"},
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   871
   @{term "op * :: real => _"}, @{term "uminus :: real => _"},
31512
27118561c2e0 Tuned sos tactic to reject non SOS goals
chaieb
parents: 31131
diff changeset
   872
   @{term "op / :: real => _"}, @{term "inverse :: real => _"},
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   873
   @{term "op ^ :: real => _"}, @{term "abs :: real => _"},
31512
27118561c2e0 Tuned sos tactic to reject non SOS goals
chaieb
parents: 31131
diff changeset
   874
   @{term "min :: real => _"}, @{term "max :: real => _"},
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46497
diff changeset
   875
   @{term "0::real"}, @{term "1::real"},
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46497
diff changeset
   876
   @{term "numeral :: num => nat"},
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46497
diff changeset
   877
   @{term "numeral :: num => real"},
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46497
diff changeset
   878
   @{term "Num.Bit0"}, @{term "Num.Bit1"}, @{term "Num.One"}];
31512
27118561c2e0 Tuned sos tactic to reject non SOS goals
chaieb
parents: 31131
diff changeset
   879
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   880
fun check_sos kcts ct =
31512
27118561c2e0 Tuned sos tactic to reject non SOS goals
chaieb
parents: 31131
diff changeset
   881
 let
27118561c2e0 Tuned sos tactic to reject non SOS goals
chaieb
parents: 31131
diff changeset
   882
  val t = term_of ct
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   883
  val _ = if not (null (Term.add_tfrees t [])
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   884
                  andalso null (Term.add_tvars t []))
31512
27118561c2e0 Tuned sos tactic to reject non SOS goals
chaieb
parents: 31131
diff changeset
   885
          then error "SOS: not sos. Additional type varables" else ()
27118561c2e0 Tuned sos tactic to reject non SOS goals
chaieb
parents: 31131
diff changeset
   886
  val fs = Term.add_frees t []
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   887
  val _ = if exists (fn ((_,T)) => not (T = @{typ "real"})) fs
31512
27118561c2e0 Tuned sos tactic to reject non SOS goals
chaieb
parents: 31131
diff changeset
   888
          then error "SOS: not sos. Variables with type not real" else ()
27118561c2e0 Tuned sos tactic to reject non SOS goals
chaieb
parents: 31131
diff changeset
   889
  val vs = Term.add_vars t []
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   890
  val _ = if exists (fn ((_,T)) => not (T = @{typ "real"})) vs
31512
27118561c2e0 Tuned sos tactic to reject non SOS goals
chaieb
parents: 31131
diff changeset
   891
          then error "SOS: not sos. Variables with type not real" else ()
27118561c2e0 Tuned sos tactic to reject non SOS goals
chaieb
parents: 31131
diff changeset
   892
  val ukcs = subtract (fn (t,p) => Const p aconv t) kcts (Term.add_consts t [])
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   893
  val _ = if  null ukcs then ()
31512
27118561c2e0 Tuned sos tactic to reject non SOS goals
chaieb
parents: 31131
diff changeset
   894
              else error ("SOSO: Unknown constants in Subgoal:" ^ commas (map fst ukcs))
27118561c2e0 Tuned sos tactic to reject non SOS goals
chaieb
parents: 31131
diff changeset
   895
in () end
27118561c2e0 Tuned sos tactic to reject non SOS goals
chaieb
parents: 31131
diff changeset
   896
32838
d9dfd30af9ae core_sos_tac: SUBPROOF body operates on subgoal 1;
wenzelm
parents: 32831
diff changeset
   897
fun core_sos_tac print_cert prover = SUBPROOF (fn {concl, context, ...} =>
32831
1352736e5727 changed core_sos_tac to use SUBPROOF
Philipp Meyer
parents: 32830
diff changeset
   898
  let
32838
d9dfd30af9ae core_sos_tac: SUBPROOF body operates on subgoal 1;
wenzelm
parents: 32831
diff changeset
   899
    val _ = check_sos known_sos_constants concl
d9dfd30af9ae core_sos_tac: SUBPROOF body operates on subgoal 1;
wenzelm
parents: 32831
diff changeset
   900
    val (ths, certificates) = real_sos prover context (Thm.dest_arg concl)
d9dfd30af9ae core_sos_tac: SUBPROOF body operates on subgoal 1;
wenzelm
parents: 32831
diff changeset
   901
    val _ = print_cert certificates
d9dfd30af9ae core_sos_tac: SUBPROOF body operates on subgoal 1;
wenzelm
parents: 32831
diff changeset
   902
  in rtac ths 1 end)
31131
d9752181691a Now deals with division
chaieb
parents: 31119
diff changeset
   903
44453
082edd97ffe1 comment out dead code to avoid compiler warnings
huffman
parents: 42616
diff changeset
   904
fun default_SOME _ NONE v = SOME v
082edd97ffe1 comment out dead code to avoid compiler warnings
huffman
parents: 42616
diff changeset
   905
  | default_SOME _ (SOME v) _ = SOME v;
31131
d9752181691a Now deals with division
chaieb
parents: 31119
diff changeset
   906
d9752181691a Now deals with division
chaieb
parents: 31119
diff changeset
   907
fun lift_SOME f NONE a = f a
44453
082edd97ffe1 comment out dead code to avoid compiler warnings
huffman
parents: 42616
diff changeset
   908
  | lift_SOME _ (SOME a) _ = SOME a;
31131
d9752181691a Now deals with division
chaieb
parents: 31119
diff changeset
   909
d9752181691a Now deals with division
chaieb
parents: 31119
diff changeset
   910
d9752181691a Now deals with division
chaieb
parents: 31119
diff changeset
   911
local
d9752181691a Now deals with division
chaieb
parents: 31119
diff changeset
   912
 val is_numeral = can (HOLogic.dest_number o term_of)
d9752181691a Now deals with division
chaieb
parents: 31119
diff changeset
   913
in
d9752181691a Now deals with division
chaieb
parents: 31119
diff changeset
   914
fun get_denom b ct = case term_of ct of
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   915
  @{term "op / :: real => _"} $ _ $ _ =>
31131
d9752181691a Now deals with division
chaieb
parents: 31119
diff changeset
   916
     if is_numeral (Thm.dest_arg ct) then get_denom b (Thm.dest_arg1 ct)
d9752181691a Now deals with division
chaieb
parents: 31119
diff changeset
   917
     else default_SOME (get_denom b) (get_denom b (Thm.dest_arg ct))   (Thm.dest_arg ct, b)
d9752181691a Now deals with division
chaieb
parents: 31119
diff changeset
   918
 | @{term "op < :: real => _"} $ _ $ _ => lift_SOME (get_denom true) (get_denom true (Thm.dest_arg ct)) (Thm.dest_arg1 ct)
d9752181691a Now deals with division
chaieb
parents: 31119
diff changeset
   919
 | @{term "op <= :: real => _"} $ _ $ _ => lift_SOME (get_denom true) (get_denom true (Thm.dest_arg ct)) (Thm.dest_arg1 ct)
d9752181691a Now deals with division
chaieb
parents: 31119
diff changeset
   920
 | _ $ _ => lift_SOME (get_denom b) (get_denom b (Thm.dest_fun ct)) (Thm.dest_arg ct)
d9752181691a Now deals with division
chaieb
parents: 31119
diff changeset
   921
 | _ => NONE
d9752181691a Now deals with division
chaieb
parents: 31119
diff changeset
   922
end;
d9752181691a Now deals with division
chaieb
parents: 31119
diff changeset
   923
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   924
fun elim_one_denom_tac ctxt =
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   925
CSUBGOAL (fn (P,i) =>
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   926
 case get_denom false P of
31131
d9752181691a Now deals with division
chaieb
parents: 31119
diff changeset
   927
   NONE => no_tac
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   928
 | SOME (d,ord) =>
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   929
     let
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 47108
diff changeset
   930
      val simp_ctxt =
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 47108
diff changeset
   931
        ctxt addsimps @{thms field_simps}
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 47108
diff changeset
   932
        addsimps [@{thm nonzero_power_divide}, @{thm power_divide}]
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   933
      val th = instantiate' [] [SOME d, SOME (Thm.dest_arg P)]
31131
d9752181691a Now deals with division
chaieb
parents: 31119
diff changeset
   934
         (if ord then @{lemma "(d=0 --> P) & (d>0 --> P) & (d<(0::real) --> P) ==> P" by auto}
d9752181691a Now deals with division
chaieb
parents: 31119
diff changeset
   935
          else @{lemma "(d=0 --> P) & (d ~= (0::real) --> P) ==> P" by blast})
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 47108
diff changeset
   936
     in rtac th i THEN Simplifier.asm_full_simp_tac simp_ctxt i end);
31131
d9752181691a Now deals with division
chaieb
parents: 31119
diff changeset
   937
d9752181691a Now deals with division
chaieb
parents: 31119
diff changeset
   938
fun elim_denom_tac ctxt i = REPEAT (elim_one_denom_tac ctxt i);
d9752181691a Now deals with division
chaieb
parents: 31119
diff changeset
   939
32949
aa6c470a962a eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents: 32839
diff changeset
   940
fun sos_tac print_cert prover ctxt =
35625
9c818cab0dd0 modernized structure Object_Logic;
wenzelm
parents: 35408
diff changeset
   941
  Object_Logic.full_atomize_tac THEN'
32949
aa6c470a962a eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents: 32839
diff changeset
   942
  elim_denom_tac ctxt THEN'
aa6c470a962a eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents: 32839
diff changeset
   943
  core_sos_tac print_cert prover ctxt;
31131
d9752181691a Now deals with division
chaieb
parents: 31119
diff changeset
   944
31512
27118561c2e0 Tuned sos tactic to reject non SOS goals
chaieb
parents: 31131
diff changeset
   945
end;