src/HOL/Library/Sum_Of_Squares/sum_of_squares.ML
author wenzelm
Thu, 15 Oct 2009 21:08:03 +0200
changeset 32949 aa6c470a962a
parent 32839 a007a7cd8c2f
child 33002 f3f02f36a3e2
permissions -rw-r--r--
eliminated slightly odd get/set operations in favour of Unsynchronized.ref; eliminated aliases of Output operations; print_cert: use warning for increased chance that the user actually sees the output; misc tuning and simplification;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
32949
aa6c470a962a eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents: 32839
diff changeset
     1
(*  Title:      HOL/Library/Sum_Of_Squares/sum_of_squares.ML
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
aa6c470a962a eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents: 32839
diff changeset
    12
  val debugging: bool Unsynchronized.ref
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
32949
aa6c470a962a eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents: 32839
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;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    23
val rat_1_2 = rat_1 // rat_2;
32949
aa6c470a962a eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents: 32839
diff changeset
    24
val max = curry Int.max;
aa6c470a962a eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents: 32839
diff changeset
    25
val min = curry Int.min;
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    26
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    27
val denominator_rat = Rat.quotient_of_rat #> snd #> Rat.rat_of_int;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    28
val numerator_rat = Rat.quotient_of_rat #> fst #> Rat.rat_of_int;
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
    29
fun int_of_rat a =
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    30
    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
    31
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
    32
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
    33
fun rat_pow r i =
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
    34
 let fun pow r i =
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
    35
   if i = 0 then rat_1 else
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    36
   let val d = pow r (i div 2)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    37
   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
    38
   end
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    39
 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
    40
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
    41
fun round_rat r =
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    42
 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
    43
     val d = a div b
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    44
     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
    45
     val x2 = 2 * (a - (b * d))
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    46
 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
    47
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    48
val abs_rat = Rat.abs;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    49
val pow2 = rat_pow rat_2;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    50
val pow10 = rat_pow rat_10;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    51
32740
9dd0a2f83429 explicit indication of Unsynchronized.ref;
wenzelm
parents: 32646
diff changeset
    52
val debugging = Unsynchronized.ref false;
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    53
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    54
exception Sanity;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    55
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    56
exception Unsolvable;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    57
32332
bc5cec7b2be6 misc changes to SOS by Philipp Meyer:
wenzelm
parents: 32268
diff changeset
    58
exception Failure of string;
bc5cec7b2be6 misc changes to SOS by Philipp Meyer:
wenzelm
parents: 32268
diff changeset
    59
32645
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
    60
datatype proof_method =
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
    61
    Certificate of RealArith.pss_tree
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
    62
  | Prover of (string -> string)
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
    63
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    64
(* 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
    65
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    66
local
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    67
fun normalize y =
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    68
  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
    69
  else if abs_rat y >=/ rat_1 then normalize (y // rat_10) + 1
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
    70
  else 0
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    71
 in
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    72
fun decimalize d x =
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    73
  if x =/ rat_0 then "0.0" else
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
    74
  let
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    75
   val y = Rat.abs x
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    76
   val e = normalize y
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    77
   val z = pow10(~ e) */ y +/ rat_1
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    78
   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
    79
  in (if x </ rat_0 then "-0." else "0.") ^
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    80
     implode(tl(explode(string_of_int k))) ^
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    81
     (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
    82
  end
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    83
end;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    84
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    85
(* Iterations over numbers, and lists indexed by numbers.                    *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    86
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    87
fun itern k l f a =
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    88
  case l of
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    89
    [] => a
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    90
  | 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
    91
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    92
fun iter (m,n) f a =
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    93
  if n < m then a
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    94
  else iter (m+1,n) f (f m a);
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    95
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    96
(* The main types.                                                           *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    97
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
    98
type vector = int* Rat.rat FuncUtil.Intfunc.table;
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
    99
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   100
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
   101
32645
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
   102
fun iszero (k,r) = r =/ rat_0;
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   103
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   104
fun fold_rev2 f l1 l2 b =
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   105
  case (l1,l2) of
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   106
    ([],[]) => b
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   107
  | (h1::t1,h2::t2) => f h1 h2 (fold_rev2 f t1 t2 b)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   108
  | _ => error "fold_rev2";
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   109
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   110
(* Vectors. Conventionally indexed 1..n.                                     *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   111
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   112
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
   113
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   114
fun dim (v:vector) = fst v;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   115
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   116
fun vector_const c n =
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   117
  if c =/ rat_0 then vector_0 n
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   118
  else (n,fold_rev (fn k => FuncUtil.Intfunc.update (k,c)) (1 upto n) FuncUtil.Intfunc.empty) :vector;
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   120
val vector_1 = vector_const rat_1;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   121
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   122
fun vector_cmul c (v:vector) =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   123
 let val n = dim v
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   124
 in if c =/ rat_0 then vector_0 n
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   125
    else (n,FuncUtil.Intfunc.map (fn x => c */ x) (snd v))
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   126
 end;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   127
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   128
fun vector_neg (v:vector) = (fst v,FuncUtil.Intfunc.map Rat.neg (snd v)) :vector;
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   129
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   130
fun vector_add (v1:vector) (v2:vector) =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   131
 let val m = dim v1
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   132
     val n = dim v2
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   133
 in if m <> n then error "vector_add: incompatible dimensions"
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   134
    else (n,FuncUtil.Intfunc.combine (curry op +/) (fn x => x =/ rat_0) (snd v1) (snd v2)) :vector
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   135
 end;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   136
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   137
fun vector_sub v1 v2 = vector_add v1 (vector_neg v2);
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   138
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   139
fun vector_dot (v1:vector) (v2:vector) =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   140
 let val m = dim v1
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   141
     val n = dim v2
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   142
 in if m <> n then error "vector_dot: incompatible dimensions"
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   143
    else FuncUtil.Intfunc.fold (fn (i,x) => fn a => x +/ a)
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   144
        (FuncUtil.Intfunc.combine (curry op */) (fn x => x =/ rat_0) (snd v1) (snd v2)) rat_0
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   145
 end;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   146
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   147
fun vector_of_list l =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   148
 let val n = length l
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   149
 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
   150
 end;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   151
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   152
(* Matrices; again rows and columns indexed from 1.                          *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   153
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   154
fun matrix_0 (m,n) = ((m,n),FuncUtil.Intpairfunc.empty):matrix;
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   155
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   156
fun dimensions (m:matrix) = fst m;
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 matrix_const c (mn as (m,n)) =
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   159
  if m <> n then error "matrix_const: needs to be square"
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   160
  else if c =/ rat_0 then matrix_0 mn
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   161
  else (mn,fold_rev (fn k => FuncUtil.Intpairfunc.update ((k,k), c)) (1 upto n) FuncUtil.Intpairfunc.empty) :matrix;;
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   162
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   163
val matrix_1 = matrix_const rat_1;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   164
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   165
fun matrix_cmul c (m:matrix) =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   166
 let val (i,j) = dimensions m
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   167
 in if c =/ rat_0 then matrix_0 (i,j)
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   168
    else ((i,j),FuncUtil.Intpairfunc.map (fn x => c */ x) (snd m))
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   169
 end;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   170
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   171
fun matrix_neg (m:matrix) =
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   172
  (dimensions m, FuncUtil.Intpairfunc.map Rat.neg (snd m)) :matrix;
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   173
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   174
fun matrix_add (m1:matrix) (m2:matrix) =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   175
 let val d1 = dimensions m1
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   176
     val d2 = dimensions m2
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   177
 in if d1 <> d2
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   178
     then error "matrix_add: incompatible dimensions"
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   179
    else (d1,FuncUtil.Intpairfunc.combine (curry op +/) (fn x => x =/ rat_0) (snd m1) (snd m2)) :matrix
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   180
 end;;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   181
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   182
fun matrix_sub m1 m2 = matrix_add m1 (matrix_neg m2);
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   183
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   184
fun row k (m:matrix) =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   185
 let val (i,j) = dimensions m
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   186
 in (j,
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   187
   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
   188
 end;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   189
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   190
fun column k (m:matrix) =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   191
  let val (i,j) = dimensions m
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   192
  in (i,
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   193
   FuncUtil.Intpairfunc.fold (fn ((i,j), c) => fn a => if j = k then FuncUtil.Intfunc.update (i,c) a else a) (snd m)  FuncUtil.Intfunc.empty)
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   194
   : vector
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   195
 end;
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
fun transp (m:matrix) =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   198
  let val (i,j) = dimensions m
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   199
  in
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   200
  ((j,i),FuncUtil.Intpairfunc.fold (fn ((i,j), c) => fn a => FuncUtil.Intpairfunc.update ((j,i), c) a) (snd m) FuncUtil.Intpairfunc.empty) :matrix
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   201
 end;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   202
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   203
fun diagonal (v:vector) =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   204
 let val n = dim v
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   205
 in ((n,n),FuncUtil.Intfunc.fold (fn (i, c) => fn a => FuncUtil.Intpairfunc.update ((i,i), c) a) (snd v) FuncUtil.Intpairfunc.empty) : matrix
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   206
 end;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   207
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   208
fun matrix_of_list l =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   209
 let val m = length l
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   210
 in if m = 0 then matrix_0 (0,0) else
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   211
   let val n = length (hd l)
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   212
   in ((m,n),itern 1 l (fn v => fn i => itern 1 v (fn c => fn j => FuncUtil.Intpairfunc.update ((i,j), c))) FuncUtil.Intpairfunc.empty)
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   213
   end
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   214
 end;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   215
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   216
(* Monomials.                                                                *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   217
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   218
fun monomial_eval assig m =
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   219
  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
   220
        m rat_1;
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   221
val monomial_1 = FuncUtil.Ctermfunc.empty;
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   222
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   223
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
   224
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   225
val monomial_mul =
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   226
  FuncUtil.Ctermfunc.combine (curry op +) (K false);
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   227
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   228
fun monomial_pow m k =
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   229
  if k = 0 then monomial_1
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   230
  else FuncUtil.Ctermfunc.map (fn x => k * x) m;
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   231
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   232
fun monomial_divides m1 m2 =
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   233
  FuncUtil.Ctermfunc.fold (fn (x, k) => fn a => FuncUtil.Ctermfunc.tryapplyd m2 x 0 >= k andalso a) m1 true;;
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   234
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   235
fun monomial_div m1 m2 =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   236
 let val m = FuncUtil.Ctermfunc.combine (curry op +)
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   237
   (fn x => x = 0) m1 (FuncUtil.Ctermfunc.map (fn x => ~ x) m2)
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   238
 in if FuncUtil.Ctermfunc.fold (fn (x, k) => fn a => k >= 0 andalso a) m true then m
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   239
  else error "monomial_div: non-divisible"
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   240
 end;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   241
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   242
fun monomial_degree x m =
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   243
  FuncUtil.Ctermfunc.tryapplyd m x 0;;
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   244
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   245
fun monomial_lcm m1 m2 =
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   246
  fold_rev (fn x => FuncUtil.Ctermfunc.update (x, max (monomial_degree x m1) (monomial_degree x m2)))
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   247
          (gen_union (is_equal o  FuncUtil.cterm_ord) (FuncUtil.Ctermfunc.dom m1, FuncUtil.Ctermfunc.dom m2)) (FuncUtil.Ctermfunc.empty);
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   248
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   249
fun monomial_multidegree m =
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   250
 FuncUtil.Ctermfunc.fold (fn (x, k) => fn a => k + a) m 0;;
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   251
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   252
fun monomial_variables m = FuncUtil.Ctermfunc.dom m;;
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   253
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   254
(* Polynomials.                                                              *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   255
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   256
fun eval assig p =
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   257
  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
   258
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   259
val poly_0 = FuncUtil.Monomialfunc.empty;
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   260
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   261
fun poly_isconst p =
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   262
  FuncUtil.Monomialfunc.fold (fn (m, c) => 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
   263
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   264
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
   265
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   266
fun poly_const c =
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   267
  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
   268
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   269
fun poly_cmul c p =
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   270
  if c =/ rat_0 then poly_0
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   271
  else FuncUtil.Monomialfunc.map (fn x => c */ x) p;
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   272
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   273
fun poly_neg p = FuncUtil.Monomialfunc.map Rat.neg p;;
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   274
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   275
fun poly_add p1 p2 =
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   276
  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
   277
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   278
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
   279
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   280
fun poly_cmmul (c,m) p =
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   281
 if c =/ rat_0 then poly_0
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   282
 else if FuncUtil.Ctermfunc.is_empty m
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   283
      then FuncUtil.Monomialfunc.map (fn d => c */ d) p
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   284
      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
   285
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   286
fun poly_mul p1 p2 =
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   287
  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
   288
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   289
fun poly_div p1 p2 =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   290
 if not(poly_isconst p2)
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   291
 then error "poly_div: non-constant" else
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   292
 let val c = eval FuncUtil.Ctermfunc.empty p2
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   293
 in if c =/ rat_0 then error "poly_div: division by zero"
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   294
    else poly_cmul (Rat.inv c) p1
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   295
 end;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   296
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   297
fun poly_square p = poly_mul p p;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   298
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   299
fun poly_pow p k =
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   300
 if k = 0 then poly_const rat_1
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   301
 else if k = 1 then p
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   302
 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
   303
      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
   304
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   305
fun poly_exp p1 p2 =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   306
  if not(poly_isconst p2)
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   307
  then error "poly_exp: not a constant"
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   308
  else poly_pow p1 (int_of_rat (eval FuncUtil.Ctermfunc.empty p2));
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   309
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   310
fun degree x p =
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   311
 FuncUtil.Monomialfunc.fold (fn (m,c) => fn a => max (monomial_degree x m) a) p 0;
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   312
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   313
fun multidegree p =
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   314
  FuncUtil.Monomialfunc.fold (fn (m, c) => 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
   315
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   316
fun poly_variables p =
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   317
  sort FuncUtil.cterm_ord (FuncUtil.Monomialfunc.fold_rev (fn (m, c) => curry (gen_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
   318
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   319
(* Order monomials for human presentation.                                   *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   320
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   321
val humanorder_varpow = prod_ord FuncUtil.cterm_ord (rev_order o int_ord);
31119
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
local
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   324
 fun ord (l1,l2) = case (l1,l2) of
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   325
  (_,[]) => LESS
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   326
 | ([],_) => GREATER
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   327
 | (h1::t1,h2::t2) =>
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   328
   (case humanorder_varpow (h1, h2) of
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   329
     LESS => LESS
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   330
   | EQUAL => ord (t1,t2)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   331
   | GREATER => GREATER)
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   332
in fun humanorder_monomial m1 m2 =
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   333
 ord (sort humanorder_varpow (FuncUtil.Ctermfunc.dest m1),
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   334
  sort humanorder_varpow (FuncUtil.Ctermfunc.dest m2))
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   335
end;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   336
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   337
(* Conversions to strings.                                                   *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   338
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   339
fun string_of_vector min_size max_size (v:vector) =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   340
 let val n_raw = dim v
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   341
 in if n_raw = 0 then "[]" else
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   342
  let
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   343
   val n = max min_size (min n_raw max_size)
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   344
   val xs = map (Rat.string_of_rat 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
   345
  in "[" ^ space_implode ", " xs ^
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   346
  (if n_raw > max_size then ", ...]" else "]")
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   347
  end
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
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   350
fun string_of_matrix max_size (m:matrix) =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   351
 let
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   352
  val (i_raw,j_raw) = dimensions m
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   353
  val i = min max_size i_raw
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   354
  val j = min max_size j_raw
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   355
  val rstr = map (fn k => string_of_vector j j (row k m)) (1 upto i)
32830
47c1b15c03fe replaced and tuned uses of foldr1
Philipp Meyer
parents: 32829
diff changeset
   356
 in "["^ space_implode ";\n " rstr ^
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   357
  (if j > max_size then "\n ...]" else "]")
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   358
 end;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   359
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   360
fun string_of_term t =
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   361
 case t of
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   362
   a$b => "("^(string_of_term a)^" "^(string_of_term b)^")"
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   363
 | Abs x =>
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   364
    let val (xn, b) = Term.dest_abs x
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   365
    in "(\\"^xn^"."^(string_of_term b)^")"
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   366
    end
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   367
 | Const(s,_) => s
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   368
 | Free (s,_) => s
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   369
 | Var((s,_),_) => s
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   370
 | _ => error "string_of_term";
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   371
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   372
val string_of_cterm = string_of_term o term_of;
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
fun string_of_varpow x k =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   375
  if k = 1 then string_of_cterm x
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   376
  else string_of_cterm x^"^"^string_of_int k;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   377
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   378
fun string_of_monomial m =
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   379
 if FuncUtil.Ctermfunc.is_empty m then "1" else
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   380
 let val vps = fold_rev (fn (x,k) => fn a => string_of_varpow x k :: a)
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   381
  (sort humanorder_varpow (FuncUtil.Ctermfunc.dest m)) []
32830
47c1b15c03fe replaced and tuned uses of foldr1
Philipp Meyer
parents: 32829
diff changeset
   382
 in space_implode "*" vps
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   383
 end;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   384
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   385
fun string_of_cmonomial (c,m) =
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   386
 if FuncUtil.Ctermfunc.is_empty m then Rat.string_of_rat c
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   387
 else if c =/ rat_1 then string_of_monomial m
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   388
 else Rat.string_of_rat c ^ "*" ^ string_of_monomial m;;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   389
32645
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
   390
fun string_of_poly p =
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   391
 if FuncUtil.Monomialfunc.is_empty p then "<<0>>" else
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   392
 let
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   393
  val cms = sort (fn ((m1,_),(m2,_)) => humanorder_monomial m1  m2) (FuncUtil.Monomialfunc.dest p)
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   394
  val s = fold (fn (m,c) => fn a =>
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   395
             if c </ rat_0 then a ^ " - " ^ string_of_cmonomial(Rat.neg c,m)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   396
             else a ^ " + " ^ string_of_cmonomial(c,m))
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   397
          cms ""
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   398
  val s1 = String.substring (s, 0, 3)
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   399
  val s2 = String.substring (s, 3, String.size s - 3)
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   400
 in "<<" ^(if s1 = " + " then s2 else "-"^s2)^">>"
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   401
 end;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   402
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   403
(* Conversion from HOL term.                                                 *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   404
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   405
local
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   406
 val neg_tm = @{cterm "uminus :: real => _"}
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   407
 val add_tm = @{cterm "op + :: real => _"}
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   408
 val sub_tm = @{cterm "op - :: real => _"}
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   409
 val mul_tm = @{cterm "op * :: real => _"}
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   410
 val inv_tm = @{cterm "inverse :: real => _"}
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   411
 val div_tm = @{cterm "op / :: real => _"}
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   412
 val pow_tm = @{cterm "op ^ :: real => _"}
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   413
 val zero_tm = @{cterm "0:: real"}
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   414
 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
   415
 fun is_comb t = case t of _$_ => true | _ => false
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   416
 fun poly_of_term tm =
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   417
  if tm aconvc zero_tm then poly_0
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   418
  else if RealArith.is_ratconst tm
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   419
       then poly_const(RealArith.dest_ratconst tm)
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   420
  else
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   421
  (let val (lop,r) = Thm.dest_comb tm
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   422
   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
   423
      else if lop aconvc inv_tm then
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   424
       let val p = poly_of_term r
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   425
       in if poly_isconst p
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   426
          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
   427
          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
   428
       end
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   429
   else (let val (opr,l) = Thm.dest_comb lop
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   430
         in
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   431
          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
   432
          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
   433
          else if opr aconvc add_tm
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   434
           then poly_add (poly_of_term l) (poly_of_term r)
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   435
          else if opr aconvc sub_tm
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   436
           then poly_sub (poly_of_term l) (poly_of_term r)
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   437
          else if opr aconvc mul_tm
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   438
           then poly_mul (poly_of_term l) (poly_of_term r)
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   439
          else if opr aconvc div_tm
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   440
           then let
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   441
                  val p = poly_of_term l
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   442
                  val q = poly_of_term r
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   443
                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
   444
                   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
   445
                end
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   446
          else poly_var tm
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   447
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   448
         end
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   449
         handle CTERM ("dest_comb",_) => poly_var tm)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   450
   end
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   451
   handle CTERM ("dest_comb",_) => poly_var tm)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   452
in
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   453
val poly_of_term = fn tm =>
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   454
 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
   455
 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
   456
end;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   457
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   458
(* 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
   459
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   460
fun sdpa_of_vector (v:vector) =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   461
 let
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   462
  val n = dim v
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   463
  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
   464
 in space_implode " " strs ^ "\n"
31119
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
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   467
fun triple_int_ord ((a,b,c),(a',b',c')) =
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   468
 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
   469
    ((a,(b,c)),(a',(b',c')));
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   470
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
   471
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   472
(* String for block diagonal matrix numbered k.                              *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   473
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   474
fun sdpa_of_blockdiagonal k m =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   475
 let
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   476
  val pfx = string_of_int k ^" "
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   477
  val ents =
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   478
    Inttriplefunc.fold (fn ((b,i,j), c) => fn a => if i > j then a else ((b,i,j),c)::a) m []
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   479
  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
   480
 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
   481
     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
   482
     " " ^ decimalize 20 c ^ "\n" ^ a) entss ""
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   483
 end;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   484
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   485
(* String for a matrix numbered k, in SDPA sparse format.                    *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   486
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   487
fun sdpa_of_matrix k (m:matrix) =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   488
 let
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   489
  val pfx = string_of_int k ^ " 1 "
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   490
  val ms = FuncUtil.Intpairfunc.fold (fn ((i,j), c) => fn  a => if i > j then a else ((i,j),c)::a) (snd m) []
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   491
  val mss = sort ((prod_ord int_ord int_ord) o pairself fst) ms
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   492
 in fold_rev (fn ((i,j),c) => fn a =>
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   493
     pfx ^ string_of_int i ^ " " ^ string_of_int j ^
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   494
     " " ^ decimalize 20 c ^ "\n" ^ a) mss ""
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
(* ------------------------------------------------------------------------- *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   498
(* String in SDPA sparse format for standard SDP problem:                    *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   499
(*                                                                           *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   500
(*    X = v_1 * [M_1] + ... + v_m * [M_m] - [M_0] must be PSD                *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   501
(*    Minimize obj_1 * v_1 + ... obj_m * v_m                                 *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   502
(* ------------------------------------------------------------------------- *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   503
32268
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents: 32150
diff changeset
   504
fun sdpa_of_problem obj mats =
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 m = length mats - 1
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   507
  val (n,_) = dimensions (hd mats)
32268
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents: 32150
diff changeset
   508
 in
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   509
  string_of_int m ^ "\n" ^
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   510
  "1\n" ^
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   511
  string_of_int n ^ "\n" ^
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   512
  sdpa_of_vector obj ^
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   513
  fold_rev2 (fn k => fn m => fn a => sdpa_of_matrix (k - 1) m ^ a) (1 upto length mats) mats ""
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   514
 end;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   515
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   516
fun index_char str chr pos =
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   517
  if pos >= String.size str then ~1
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   518
  else if String.sub(str,pos) = chr then pos
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   519
  else index_char str chr (pos + 1);
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   520
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
   521
fun rat_of_string s =
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   522
 let val n = index_char s #"/" 0 in
32949
aa6c470a962a eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents: 32839
diff changeset
   523
  if n = ~1 then s |> Int.fromString |> valOf |> Rat.rat_of_int
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   524
  else
32949
aa6c470a962a eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents: 32839
diff changeset
   525
   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
   526
       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
   527
   in rat_of_quotient(numer, den)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   528
   end
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
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   531
fun isspace x = x = " " ;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   532
fun isnum x = x mem_string ["0","1","2","3","4","5","6","7","8","9"]
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   533
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   534
(* More parser basics.                                                       *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   535
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   536
 val word = Scan.this_string
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   537
 fun token s =
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   538
  Scan.repeat ($$ " ") |-- word s --| Scan.repeat ($$ " ")
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   539
 val numeral = Scan.one isnum
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   540
 val decimalint = Scan.bulk numeral >> (rat_of_string o implode)
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   541
 val decimalfrac = Scan.bulk numeral
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   542
    >> (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
   543
 val decimalsig =
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   544
    decimalint -- Scan.option (Scan.$$ "." |-- decimalfrac)
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   545
    >> (fn (h,NONE) => h | (h,SOME x) => h +/ x)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   546
 fun signed prs =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   547
       $$ "-" |-- prs >> Rat.neg
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   548
    || $$ "+" |-- prs
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   549
    || prs;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   550
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   551
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
   552
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   553
 val exponent = ($$ "e" || $$ "E") |-- signed decimalint;
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
 val decimal = signed decimalsig -- (emptyin rat_0|| exponent)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   556
    >> (fn (h, x) => h */ pow10 (int_of_rat x));
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   557
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   558
 fun mkparser p s =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   559
  let val (x,rst) = p (explode s)
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   560
  in if null rst then x
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   561
     else error "mkparser: unparsed input"
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   562
  end;;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   563
32332
bc5cec7b2be6 misc changes to SOS by Philipp Meyer:
wenzelm
parents: 32268
diff changeset
   564
(* Parse back csdp output.                                                      *)
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   565
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   566
 fun ignore inp = ((),[])
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   567
 fun csdpoutput inp =  ((decimal -- Scan.bulk (Scan.$$ " " |-- Scan.option decimal) >> (fn (h,to) => map_filter I ((SOME h)::to))) --| ignore >> vector_of_list) inp
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   568
 val parse_csdpoutput = mkparser csdpoutput
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   569
32268
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents: 32150
diff changeset
   570
(* Run prover on a problem in linear form.                       *)
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents: 32150
diff changeset
   571
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents: 32150
diff changeset
   572
fun run_problem prover obj mats =
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents: 32150
diff changeset
   573
  parse_csdpoutput (prover (sdpa_of_problem obj mats))
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents: 32150
diff changeset
   574
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   575
(* 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
   576
(* 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
   577
(* 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
   578
(* are extreme numbers in the original problem.                              *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   579
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   580
  (* Version for (int*int) keys *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   581
local
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   582
  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
   583
  fun common_denominator fld amat acc =
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   584
      fld (fn (m,c) => fn a => lcm_rat (denominator_rat c) a) amat acc
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   585
  fun maximal_element fld amat acc =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   586
    fld (fn (m,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
   587
fun float_of_rat x = let val (a,b) = Rat.quotient_of_rat x
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   588
                     in Real.fromLargeInt a / Real.fromLargeInt b end;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   589
in
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   590
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   591
fun pi_scale_then solver (obj:vector)  mats =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   592
 let
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   593
  val cd1 = fold_rev (common_denominator FuncUtil.Intpairfunc.fold) mats (rat_1)
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   594
  val cd2 = common_denominator FuncUtil.Intfunc.fold (snd obj)  (rat_1)
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   595
  val mats' = map (FuncUtil.Intpairfunc.map (fn x => cd1 */ x)) mats
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   596
  val obj' = vector_cmul cd2 obj
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   597
  val max1 = fold_rev (maximal_element FuncUtil.Intpairfunc.fold) mats' (rat_0)
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   598
  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
   599
  val scal1 = pow2 (20 - trunc(Math.ln (float_of_rat max1) / Math.ln 2.0))
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   600
  val scal2 = pow2 (20 - trunc(Math.ln (float_of_rat max2) / Math.ln 2.0))
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   601
  val mats'' = map (FuncUtil.Intpairfunc.map (fn x => x */ scal1)) mats'
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   602
  val obj'' = vector_cmul scal2 obj'
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   603
 in solver obj'' mats''
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   604
  end
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   605
end;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   606
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   607
(* 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
   608
(* 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
   609
(* 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
   610
(* are extreme numbers in the original problem.                              *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   611
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   612
  (* Version for (int*int*int) keys *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   613
local
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   614
  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
   615
  fun common_denominator fld amat acc =
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   616
      fld (fn (m,c) => fn a => lcm_rat (denominator_rat c) a) amat acc
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   617
  fun maximal_element fld amat acc =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   618
    fld (fn (m,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
   619
fun float_of_rat x = let val (a,b) = Rat.quotient_of_rat x
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   620
                     in Real.fromLargeInt a / Real.fromLargeInt b end;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   621
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
   622
in
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   623
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   624
fun tri_scale_then solver (obj:vector)  mats =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   625
 let
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   626
  val cd1 = fold_rev (common_denominator Inttriplefunc.fold) mats (rat_1)
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   627
  val cd2 = common_denominator FuncUtil.Intfunc.fold (snd obj)  (rat_1)
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   628
  val mats' = map (Inttriplefunc.map (fn x => cd1 */ x)) mats
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   629
  val obj' = vector_cmul cd2 obj
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   630
  val max1 = fold_rev (maximal_element Inttriplefunc.fold) mats' (rat_0)
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   631
  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
   632
  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
   633
  val scal2 = pow2 (20 - int_of_float(Math.ln (float_of_rat max2) / Math.ln 2.0))
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   634
  val mats'' = map (Inttriplefunc.map (fn x => x */ scal1)) mats'
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   635
  val obj'' = vector_cmul scal2 obj'
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   636
 in solver obj'' mats''
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   637
  end
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   638
end;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   639
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   640
(* Round a vector to "nice" rationals.                                       *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   641
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   642
fun nice_rational n x = round_rat (n */ x) // n;;
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   643
fun nice_vector n ((d,v) : vector) =
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   644
 (d, FuncUtil.Intfunc.fold (fn (i,c) => fn a =>
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   645
   let val y = nice_rational n c
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   646
   in if c =/ rat_0 then a
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   647
      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
   648
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   649
fun dest_ord f x = is_equal (f x);
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   650
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   651
(* Stuff for "equations" ((int*int*int)->num functions).                         *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   652
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   653
fun tri_equation_cmul c eq =
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   654
  if c =/ rat_0 then Inttriplefunc.empty else Inttriplefunc.map (fn d => c */ d) eq;
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   655
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   656
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
   657
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   658
fun tri_equation_eval assig eq =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   659
 let fun value v = Inttriplefunc.apply assig v
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   660
 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
   661
 end;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   662
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   663
(* Eliminate among linear equations: return unconstrained variables and      *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   664
(* assignments for the others in terms of them. We give one pseudo-variable  *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   665
(* "one" that's used for a constant term.                                    *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   666
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   667
local
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   668
  fun extract_first p l = case l of  (* FIXME : use find_first instead *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   669
   [] => error "extract_first"
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   670
 | h::t => if p h then (h,t) else
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   671
          let val (k,s) = extract_first p t in (k,h::s) end
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   672
fun eliminate vars dun eqs = case vars of
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   673
  [] => if forall Inttriplefunc.is_empty eqs then dun
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   674
        else raise Unsolvable
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   675
 | v::vs =>
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   676
  ((let
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   677
    val (eq,oeqs) = extract_first (fn e => Inttriplefunc.defined e v) eqs
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   678
    val a = Inttriplefunc.apply eq v
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   679
    val eq' = tri_equation_cmul ((Rat.neg rat_1) // a) (Inttriplefunc.delete_safe v eq)
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   680
    fun elim e =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   681
     let val b = Inttriplefunc.tryapplyd e v rat_0
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   682
     in if b =/ rat_0 then e else
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   683
        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
   684
     end
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   685
   in eliminate vs (Inttriplefunc.update (v,eq') (Inttriplefunc.map elim dun)) (map elim oeqs)
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   686
   end)
32332
bc5cec7b2be6 misc changes to SOS by Philipp Meyer:
wenzelm
parents: 32268
diff changeset
   687
  handle Failure _ => eliminate vs dun eqs)
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   688
in
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   689
fun tri_eliminate_equations one vars eqs =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   690
 let
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   691
  val assig = eliminate vars Inttriplefunc.empty eqs
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   692
  val vs = Inttriplefunc.fold (fn (x, f) => fn a => remove (dest_ord triple_int_ord) one (Inttriplefunc.dom f) @ a) assig []
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   693
  in (distinct (dest_ord triple_int_ord) vs, assig)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   694
  end
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   695
end;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   696
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   697
(* Eliminate all variables, in an essentially arbitrary order.               *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   698
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   699
fun tri_eliminate_all_equations one =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   700
 let
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   701
  fun choose_variable eq =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   702
   let val (v,_) = Inttriplefunc.choose eq
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   703
   in if is_equal (triple_int_ord(v,one)) then
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   704
      let val eq' = Inttriplefunc.delete_safe v eq
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   705
      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
   706
         else fst (Inttriplefunc.choose eq')
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   707
      end
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   708
    else v
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   709
   end
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   710
  fun eliminate dun eqs = case eqs of
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   711
    [] => dun
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   712
  | eq::oeqs =>
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   713
    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
   714
    let val v = choose_variable eq
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   715
        val a = Inttriplefunc.apply eq v
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   716
        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
   717
                   (Inttriplefunc.delete_safe v eq)
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   718
        fun elim e =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   719
         let val b = Inttriplefunc.tryapplyd e v rat_0
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   720
         in if b =/ rat_0 then e
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   721
            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
   722
         end
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   723
    in eliminate (Inttriplefunc.update(v, eq') (Inttriplefunc.map elim dun))
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   724
                 (map elim oeqs)
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   725
    end
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   726
in fn eqs =>
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   727
 let
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   728
  val assig = eliminate Inttriplefunc.empty eqs
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   729
  val vs = Inttriplefunc.fold (fn (x, f) => fn a => remove (dest_ord triple_int_ord) one (Inttriplefunc.dom f) @ a) assig []
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   730
 in (distinct (dest_ord triple_int_ord) vs,assig)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   731
 end
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   732
end;
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   733
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   734
(* Solve equations by assigning arbitrary numbers.                           *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   735
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   736
fun tri_solve_equations one eqs =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   737
 let
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   738
  val (vars,assigs) = tri_eliminate_all_equations one eqs
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   739
  val vfn = fold_rev (fn v => Inttriplefunc.update(v,rat_0)) vars
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   740
            (Inttriplefunc.onefunc(one, Rat.rat_of_int ~1))
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   741
  val ass =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   742
    Inttriplefunc.combine (curry op +/) (K false)
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   743
    (Inttriplefunc.map (tri_equation_eval vfn) assigs) vfn
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   744
 in if forall (fn e => tri_equation_eval ass e =/ rat_0) eqs
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   745
    then Inttriplefunc.delete_safe one ass else raise Sanity
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   746
 end;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   747
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   748
(* 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
   749
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   750
fun tri_epoly_pmul p q acc =
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   751
 FuncUtil.Monomialfunc.fold (fn (m1, c) => fn a =>
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   752
  FuncUtil.Monomialfunc.fold (fn (m2,e) => fn b =>
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   753
   let val m =  monomial_mul m1 m2
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   754
       val es = FuncUtil.Monomialfunc.tryapplyd b m Inttriplefunc.empty
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   755
   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
   756
   end) q a) p acc ;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   757
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   758
(* Usual operations on equation-parametrized poly.                           *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   759
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   760
fun tri_epoly_cmul c l =
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   761
  if c =/ rat_0 then Inttriplefunc.empty else Inttriplefunc.map (tri_equation_cmul c) l;;
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   762
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   763
val tri_epoly_neg = tri_epoly_cmul (Rat.rat_of_int ~1);
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   764
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   765
val tri_epoly_add = Inttriplefunc.combine tri_equation_add Inttriplefunc.is_empty;
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   766
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   767
fun tri_epoly_sub p q = tri_epoly_add p (tri_epoly_neg q);;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   768
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   769
(* Stuff for "equations" ((int*int)->num functions).                         *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   770
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   771
fun pi_equation_cmul c eq =
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   772
  if c =/ rat_0 then Inttriplefunc.empty else Inttriplefunc.map (fn d => c */ d) eq;
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   773
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   774
fun pi_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
   775
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   776
fun pi_equation_eval assig eq =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   777
 let fun value v = Inttriplefunc.apply assig v
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   778
 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
   779
 end;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   780
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   781
(* Eliminate among linear equations: return unconstrained variables and      *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   782
(* assignments for the others in terms of them. We give one pseudo-variable  *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   783
(* "one" that's used for a constant term.                                    *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   784
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   785
local
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   786
fun extract_first p l = case l of
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   787
   [] => error "extract_first"
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   788
 | h::t => if p h then (h,t) else
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   789
          let val (k,s) = extract_first p t in (k,h::s) end
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   790
fun eliminate vars dun eqs = case vars of
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   791
  [] => if forall Inttriplefunc.is_empty eqs then dun
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   792
        else raise Unsolvable
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   793
 | v::vs =>
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   794
   let
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   795
    val (eq,oeqs) = extract_first (fn e => Inttriplefunc.defined e v) eqs
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   796
    val a = Inttriplefunc.apply eq v
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   797
    val eq' = pi_equation_cmul ((Rat.neg rat_1) // a) (Inttriplefunc.delete_safe v eq)
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   798
    fun elim e =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   799
     let val b = Inttriplefunc.tryapplyd e v rat_0
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   800
     in if b =/ rat_0 then e else
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   801
        pi_equation_add e (pi_equation_cmul (Rat.neg b // a) eq)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   802
     end
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   803
   in eliminate vs (Inttriplefunc.update (v,eq') (Inttriplefunc.map elim dun)) (map elim oeqs)
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   804
   end
32332
bc5cec7b2be6 misc changes to SOS by Philipp Meyer:
wenzelm
parents: 32268
diff changeset
   805
  handle Failure _ => eliminate vs dun eqs
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   806
in
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   807
fun pi_eliminate_equations one vars eqs =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   808
 let
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   809
  val assig = eliminate vars Inttriplefunc.empty eqs
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   810
  val vs = Inttriplefunc.fold (fn (x, f) => fn a => remove (dest_ord triple_int_ord) one (Inttriplefunc.dom f) @ a) assig []
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   811
  in (distinct (dest_ord triple_int_ord) vs, assig)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   812
  end
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   813
end;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   814
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   815
(* Eliminate all variables, in an essentially arbitrary order.               *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   816
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   817
fun pi_eliminate_all_equations one =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   818
 let
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   819
  fun choose_variable eq =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   820
   let val (v,_) = Inttriplefunc.choose eq
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   821
   in if is_equal (triple_int_ord(v,one)) then
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   822
      let val eq' = Inttriplefunc.delete_safe v eq
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   823
      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
   824
         else fst (Inttriplefunc.choose eq')
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   825
      end
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   826
    else v
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   827
   end
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   828
  fun eliminate dun eqs = case eqs of
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   829
    [] => dun
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   830
  | eq::oeqs =>
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   831
    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
   832
    let val v = choose_variable eq
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   833
        val a = Inttriplefunc.apply eq v
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   834
        val eq' = pi_equation_cmul ((Rat.rat_of_int ~1) // a)
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   835
                   (Inttriplefunc.delete_safe v eq)
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   836
        fun elim e =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   837
         let val b = Inttriplefunc.tryapplyd e v rat_0
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   838
         in if b =/ rat_0 then e
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   839
            else pi_equation_add e (pi_equation_cmul (Rat.neg b // a) eq)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   840
         end
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   841
    in eliminate (Inttriplefunc.update(v, eq') (Inttriplefunc.map elim dun))
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   842
                 (map elim oeqs)
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   843
    end
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   844
in fn eqs =>
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   845
 let
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   846
  val assig = eliminate Inttriplefunc.empty eqs
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   847
  val vs = Inttriplefunc.fold (fn (x, f) => fn a => remove (dest_ord triple_int_ord) one (Inttriplefunc.dom f) @ a) assig []
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   848
 in (distinct (dest_ord triple_int_ord) vs,assig)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   849
 end
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   850
end;
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   851
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   852
(* Solve equations by assigning arbitrary numbers.                           *)
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
fun pi_solve_equations one eqs =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   855
 let
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   856
  val (vars,assigs) = pi_eliminate_all_equations one eqs
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   857
  val vfn = fold_rev (fn v => Inttriplefunc.update(v,rat_0)) vars
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   858
            (Inttriplefunc.onefunc(one, Rat.rat_of_int ~1))
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   859
  val ass =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   860
    Inttriplefunc.combine (curry op +/) (K false)
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   861
    (Inttriplefunc.map (pi_equation_eval vfn) assigs) vfn
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   862
 in if forall (fn e => pi_equation_eval ass e =/ rat_0) eqs
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   863
    then Inttriplefunc.delete_safe one ass else raise Sanity
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   864
 end;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   865
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   866
(* 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
   867
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   868
fun pi_epoly_pmul p q acc =
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   869
 FuncUtil.Monomialfunc.fold (fn (m1, c) => fn a =>
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   870
  FuncUtil.Monomialfunc.fold (fn (m2,e) => fn b =>
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   871
   let val m =  monomial_mul m1 m2
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   872
       val es = FuncUtil.Monomialfunc.tryapplyd b m Inttriplefunc.empty
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   873
   in FuncUtil.Monomialfunc.update (m,pi_equation_add (pi_equation_cmul c e) es) b
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   874
   end) q a) p acc ;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   875
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   876
(* Usual operations on equation-parametrized poly.                           *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   877
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   878
fun pi_epoly_cmul c l =
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   879
  if c =/ rat_0 then Inttriplefunc.empty else Inttriplefunc.map (pi_equation_cmul c) l;;
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   880
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   881
val pi_epoly_neg = pi_epoly_cmul (Rat.rat_of_int ~1);
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   882
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   883
val pi_epoly_add = Inttriplefunc.combine pi_equation_add Inttriplefunc.is_empty;
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   884
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   885
fun pi_epoly_sub p q = pi_epoly_add p (pi_epoly_neg q);;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   886
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   887
fun allpairs f l1 l2 =  fold_rev (fn x => (curry (op @)) (map (f x) l2)) l1 [];
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   888
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   889
(* 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
   890
(* 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
   891
(* 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
   892
(* vol 45, pp. 363--374, 1978.                                               *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   893
(*                                                                           *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   894
(* 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
   895
(* 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
   896
(* quadratic form that will tend to display constants.                       *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   897
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   898
(* 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
   899
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   900
local
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   901
fun diagonalize n i m =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   902
 if FuncUtil.Intpairfunc.is_empty (snd m) then []
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   903
 else
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   904
  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
   905
  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
   906
    else if a11 =/ rat_0 then
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   907
          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
   908
          else raise Failure "diagonalize: not PSD ___ "
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   909
    else
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   910
     let
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   911
      val v = row i m
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   912
      val v' = (fst v, FuncUtil.Intfunc.fold (fn (i, c) => fn a =>
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   913
       let val y = c // a11
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   914
       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
   915
       end)  (snd v) FuncUtil.Intfunc.empty)
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   916
      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
   917
      val m' =
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   918
      ((n,n),
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   919
      iter (i+1,n) (fn j =>
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   920
          iter (i+1,n) (fn k =>
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   921
              (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
   922
          FuncUtil.Intpairfunc.empty)
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   923
     in (a11,v')::diagonalize n (i + 1) m'
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   924
     end
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   925
  end
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   926
in
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   927
fun diag m =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   928
 let
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   929
   val nn = dimensions m
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   930
   val n = fst nn
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   931
 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
   932
    else diagonalize n 1 m
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   933
 end
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   934
end;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   935
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   936
fun gcd_rat a b = Rat.rat_of_int (Integer.gcd (int_of_rat a) (int_of_rat b));
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   937
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   938
(* Adjust a diagonalization to collect rationals at the start.               *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   939
  (* FIXME : Potentially polymorphic keys, but here only: integers!! *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   940
local
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   941
 fun upd0 x y a = if y =/ rat_0 then a else FuncUtil.Intfunc.update(x,y) a;
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   942
 fun mapa f (d,v) =
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   943
  (d, FuncUtil.Intfunc.fold (fn (i,c) => fn a => upd0 i (f c) a) v FuncUtil.Intfunc.empty)
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   944
 fun adj (c,l) =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   945
 let val a =
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   946
  FuncUtil.Intfunc.fold (fn (i,c) => fn a => lcm_rat a (denominator_rat c))
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   947
    (snd l) rat_1 //
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   948
  FuncUtil.Intfunc.fold (fn (i,c) => fn a => gcd_rat a (numerator_rat c))
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   949
    (snd l) rat_0
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   950
  in ((c // (a */ a)),mapa (fn x => a */ x) l)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   951
  end
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   952
in
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   953
fun deration d = if null d then (rat_0,d) else
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   954
 let val d' = map adj d
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   955
     val a = fold (lcm_rat o denominator_rat o fst) d' rat_1 //
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   956
          fold (gcd_rat o numerator_rat o fst) d' rat_0
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   957
 in ((rat_1 // a),map (fn (c,l) => (a */ c,l)) d')
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   958
 end
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   959
end;
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   960
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   961
(* Enumeration of monomials with given multidegree bound.                    *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   962
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   963
fun enumerate_monomials d vars =
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   964
 if d < 0 then []
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   965
 else if d = 0 then [FuncUtil.Ctermfunc.empty]
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   966
 else if null vars then [monomial_1] else
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   967
 let val alts =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   968
  map (fn k => let val oths = enumerate_monomials (d - k) (tl vars)
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   969
               in map (fn ks => if k = 0 then ks else FuncUtil.Ctermfunc.update (hd vars, k) ks) oths end) (0 upto d)
32830
47c1b15c03fe replaced and tuned uses of foldr1
Philipp Meyer
parents: 32829
diff changeset
   970
 in flat alts
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   971
 end;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   972
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   973
(* Enumerate products of distinct input polys with degree <= d.              *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   974
(* We ignore any constant input polynomials.                                 *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   975
(* 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
   976
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   977
fun enumerate_products d pols =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   978
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
   979
else if d < 0 then [] else
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   980
case pols of
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   981
   [] => [(poly_const rat_1,RealArith.Rational_lt rat_1)]
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   982
 | (p,b)::ps =>
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   983
    let val e = multidegree p
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   984
    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
   985
       enumerate_products d ps @
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
   986
       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
   987
         (enumerate_products (d - e) ps)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   988
    end
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   989
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   990
(* 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
   991
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   992
fun epoly_of_poly p =
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   993
  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
   994
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   995
(* String for block diagonal matrix numbered k.                              *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   996
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   997
fun sdpa_of_blockdiagonal k m =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
   998
 let
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
   999
  val pfx = string_of_int k ^" "
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1000
  val ents =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1001
    Inttriplefunc.fold
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1002
      (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
  1003
      m []
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1004
  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
  1005
 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
  1006
     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
  1007
     " " ^ decimalize 20 c ^ "\n" ^ a) entss ""
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1008
 end;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1009
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1010
(* 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
  1011
32268
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents: 32150
diff changeset
  1012
fun sdpa_of_blockproblem nblocks blocksizes obj mats =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1013
 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
  1014
 in
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1015
  string_of_int m ^ "\n" ^
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1016
  string_of_int nblocks ^ "\n" ^
32830
47c1b15c03fe replaced and tuned uses of foldr1
Philipp Meyer
parents: 32829
diff changeset
  1017
  (space_implode " " (map string_of_int blocksizes)) ^
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1018
  "\n" ^
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1019
  sdpa_of_vector obj ^
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1020
  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
  1021
    (1 upto length mats) mats ""
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1022
 end;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1023
32268
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents: 32150
diff changeset
  1024
(* 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
  1025
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents: 32150
diff changeset
  1026
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
  1027
  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
  1028
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1029
(* 3D versions of matrix operations to consider blocks separately.           *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1030
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1031
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
  1032
fun bmatrix_cmul c bm =
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
  1033
  if c =/ rat_0 then Inttriplefunc.empty
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
  1034
  else Inttriplefunc.map (fn x => c */ x) bm;
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1035
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1036
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
  1037
fun bmatrix_sub m1 m2 = bmatrix_add m1 (bmatrix_neg m2);;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1038
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1039
(* Smash a block matrix into components.                                     *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1040
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1041
fun blocks blocksizes bm =
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1042
 map (fn (bs,b0) =>
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1043
      let val m = Inttriplefunc.fold
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
  1044
          (fn ((b,i,j),c) => fn a => if b = b0 then FuncUtil.Intpairfunc.update ((i,j),c) a else a) bm FuncUtil.Intpairfunc.empty
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1045
          val d = FuncUtil.Intpairfunc.fold (fn ((i,j),c) => 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
  1046
      in (((bs,bs),m):matrix) end)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1047
 (blocksizes ~~ (1 upto length blocksizes));;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1048
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1049
(* FIXME : Get rid of this !!!*)
32268
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents: 32150
diff changeset
  1050
local
32332
bc5cec7b2be6 misc changes to SOS by Philipp Meyer:
wenzelm
parents: 32268
diff changeset
  1051
  fun tryfind_with msg f [] = raise Failure msg
bc5cec7b2be6 misc changes to SOS by Philipp Meyer:
wenzelm
parents: 32268
diff changeset
  1052
    | tryfind_with msg f (x::xs) = (f x handle Failure s => tryfind_with s f xs);
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1053
in
32268
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents: 32150
diff changeset
  1054
  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
  1055
end
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents: 32150
diff changeset
  1056
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1057
(* Positiv- and Nullstellensatz. Flag "linf" forces a linear representation. *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1058
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1059
32268
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents: 32150
diff changeset
  1060
fun real_positivnullstellensatz_general prover linf d eqs leqs pol =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1061
let
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1062
 val vars = fold_rev (curry (gen_union (op aconvc)) o poly_variables)
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1063
              (pol::eqs @ map fst leqs) []
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1064
 val monoid = if linf then
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
  1065
      (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
  1066
      (filter (fn (p,c) => multidegree p <= d) leqs)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1067
    else enumerate_products d leqs
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1068
 val nblocks = length monoid
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1069
 fun mk_idmultiplier k p =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1070
  let
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1071
   val e = d - multidegree p
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1072
   val mons = enumerate_monomials e vars
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1073
   val nons = mons ~~ (1 upto length mons)
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1074
  in (mons,
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
  1075
      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
  1076
  end
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1077
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1078
 fun mk_sqmultiplier k (p,c) =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1079
  let
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1080
   val e = (d - multidegree p) div 2
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1081
   val mons = enumerate_monomials e vars
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1082
   val nons = mons ~~ (1 upto length mons)
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1083
  in (mons,
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1084
      fold_rev (fn (m1,n1) =>
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1085
       fold_rev (fn (m2,n2) => fn  a =>
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1086
        let val m = monomial_mul m1 m2
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1087
        in if n1 > n2 then a else
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1088
          let val c = if n1 = n2 then rat_1 else rat_2
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1089
              val e = FuncUtil.Monomialfunc.tryapplyd a m Inttriplefunc.empty
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
  1090
          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
  1091
          end
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1092
        end)  nons)
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
  1093
       nons FuncUtil.Monomialfunc.empty)
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1094
  end
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1095
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1096
  val (sqmonlist,sqs) = split_list (map2 mk_sqmultiplier (1 upto length monoid) monoid)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1097
  val (idmonlist,ids) =  split_list(map2 mk_idmultiplier (1 upto length eqs) eqs)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1098
  val blocksizes = map length sqmonlist
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1099
  val bigsum =
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1100
    fold_rev2 (fn p => fn q => fn a => tri_epoly_pmul p q a) eqs ids
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1101
            (fold_rev2 (fn (p,c) => fn s => fn a => tri_epoly_pmul p s a) monoid sqs
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1102
                     (epoly_of_poly(poly_neg pol)))
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
  1103
  val eqns = FuncUtil.Monomialfunc.fold (fn (m,e) => fn a => e::a) bigsum []
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1104
  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
  1105
  val qvars = (0,0,0)::pvs
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1106
  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
  1107
  fun mk_matrix v =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1108
    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
  1109
        if b < 0 then m else
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1110
         let val c = Inttriplefunc.tryapplyd ass v rat_0
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1111
         in if c = rat_0 then m else
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1112
            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
  1113
         end)
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
  1114
          allassig Inttriplefunc.empty
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1115
  val diagents = Inttriplefunc.fold
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1116
    (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
  1117
    allassig Inttriplefunc.empty
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1118
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1119
  val mats = map mk_matrix qvars
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1120
  val obj = (length pvs,
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
  1121
            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
  1122
                        FuncUtil.Intfunc.empty)
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1123
  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
  1124
                else tri_scale_then (run_blockproblem prover nblocks blocksizes) obj mats
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
  1125
  fun int_element (d,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
  1126
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1127
  fun find_rounding d =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1128
   let
32949
aa6c470a962a eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents: 32839
diff changeset
  1129
    val _ =
aa6c470a962a eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents: 32839
diff changeset
  1130
      if !debugging
aa6c470a962a eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents: 32839
diff changeset
  1131
      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
  1132
      else ()
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1133
    val vec = nice_vector d raw_vec
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1134
    val blockmat = iter (1,dim vec)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1135
     (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
  1136
     (bmatrix_neg (nth mats 0))
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1137
    val allmats = blocks blocksizes blockmat
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1138
   in (vec,map diag allmats)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1139
   end
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1140
  val (vec,ratdias) =
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1141
    if null pvs then find_rounding rat_1
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1142
    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
  1143
                                map pow2 (5 upto 66))
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1144
  val newassigs =
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1145
    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
  1146
           (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
  1147
  val finalassigs =
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1148
    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
  1149
  fun poly_of_epoly p =
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
  1150
    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
  1151
          p FuncUtil.Monomialfunc.empty
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1152
  fun  mk_sos mons =
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1153
   let fun mk_sq (c,m) =
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
  1154
    (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
  1155
                 (1 upto length mons) FuncUtil.Monomialfunc.empty)
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1156
   in map mk_sq
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1157
   end
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1158
  val sqs = map2 mk_sos sqmonlist ratdias
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1159
  val cfs = map poly_of_epoly ids
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1160
  val msq = filter (fn (a,b) => not (null b)) (map2 pair monoid sqs)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1161
  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
  1162
  val sanity =
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1163
    fold_rev (fn ((p,c),s) => poly_add (poly_mul p (eval_sq s))) msq
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1164
           (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
  1165
                    (poly_neg pol))
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1166
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
  1167
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
  1168
  (cfs,map (fn (a,b) => (snd a,b)) msq)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1169
 end
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1170
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1171
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1172
(* Iterative deepening.                                                      *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1173
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1174
fun deepen f n =
32949
aa6c470a962a eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents: 32839
diff changeset
  1175
  (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
  1176
    (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
  1177
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1178
32645
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
  1179
(* 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
  1180
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
  1181
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
  1182
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1183
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
  1184
  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
  1185
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1186
(* Interface to HOL.                                                         *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1187
local
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
  1188
  open Conv
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
  1189
  val concl = Thm.dest_arg o cprop_of
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1190
  fun simple_cterm_ord t u = TermOrd.fast_term_ord (term_of t, term_of u) = LESS
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1191
in
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1192
  (* FIXME: Replace tryfind by get_first !! *)
32645
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
  1193
fun real_nonlinear_prover proof_method ctxt =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1194
 let
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1195
  val {add,mul,neg,pow,sub,main} =  Normalizer.semiring_normalizers_ord_wrapper ctxt
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1196
      (valOf (NormalizerData.match ctxt @{cterm "(0::real) + 1"}))
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1197
     simple_cterm_ord
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1198
  val (real_poly_add_conv,real_poly_mul_conv,real_poly_neg_conv,
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1199
       real_poly_pow_conv,real_poly_sub_conv,real_poly_conv) = (add,mul,neg,pow,sub,main)
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1200
  fun mainf cert_choice translator (eqs,les,lts) =
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1201
  let
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
  1202
   val eq0 = map (poly_of_term o Thm.dest_arg1 o concl) eqs
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
  1203
   val le0 = map (poly_of_term o Thm.dest_arg o concl) les
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
  1204
   val lt0 = map (poly_of_term o Thm.dest_arg o concl) lts
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
  1205
   val eqp0 = map (fn (t,i) => (t,RealArith.Axiom_eq i)) (eq0 ~~ (0 upto (length eq0 - 1)))
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
  1206
   val lep0 = map (fn (t,i) => (t,RealArith.Axiom_le i)) (le0 ~~ (0 upto (length le0 - 1)))
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
  1207
   val ltp0 = map (fn (t,i) => (t,RealArith.Axiom_lt i)) (lt0 ~~ (0 upto (length lt0 - 1)))
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1208
   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
  1209
   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
  1210
   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
  1211
   fun trivial_axiom (p,ax) =
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1212
    case ax of
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1213
       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
  1214
                     else raise Failure "trivial_axiom: Not a trivial axiom"
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1215
     | 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
  1216
                     else raise Failure "trivial_axiom: Not a trivial axiom"
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1217
     | 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
  1218
                     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
  1219
     | _ => error "trivial_axiom: Not a trivial axiom"
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1220
   in
32645
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
  1221
  (let val th = tryfind trivial_axiom (keq @ klep @ kltp)
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
  1222
   in
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
  1223
    (fconv_rule (arg_conv (arg1_conv real_poly_conv) then_conv field_comp_conv) th, RealArith.Trivial)
32645
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
  1224
   end)
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1225
   handle Failure _ =>
32645
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
  1226
     (let val proof =
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
  1227
       (case proof_method of Certificate certs =>
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
  1228
         (* choose certificate *)
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
  1229
         let
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
  1230
           fun chose_cert [] (RealArith.Cert c) = c
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
  1231
             | chose_cert (RealArith.Left::s) (RealArith.Branch (l, _)) = chose_cert s l
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
  1232
             | 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
  1233
             | chose_cert _ _ = error "certificate tree in invalid form"
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
  1234
         in
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
  1235
           chose_cert cert_choice certs
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
  1236
         end
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
  1237
       | Prover prover =>
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
  1238
         (* call prover *)
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1239
         let
32645
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
  1240
          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
  1241
          val leq = lep @ ltp
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
  1242
          fun tryall d =
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
  1243
           let val e = multidegree pol
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
  1244
               val k = if e = 0 then 0 else d div e
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1245
               val eq' = map fst eq
32645
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
  1246
           in tryfind (fn i => (d,i,real_positivnullstellensatz_general prover false d eq' leq
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
  1247
                                 (poly_neg(poly_pow pol i))))
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
  1248
                   (0 upto k)
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
  1249
           end
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
  1250
         val (d,i,(cert_ideal,cert_cone)) = deepen tryall 0
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
  1251
         val proofs_ideal =
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
  1252
           map2 (fn q => fn (p,ax) => RealArith.Eqmul(q,ax)) cert_ideal eq
32645
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
  1253
         val proofs_cone = map cterm_of_sos cert_cone
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
  1254
         val proof_ne = if null ltp then RealArith.Rational_lt Rat.one else
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1255
           let val p = foldr1 RealArith.Product (map snd ltp)
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
  1256
           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
  1257
           end
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1258
         in
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1259
           foldr1 RealArith.Sum (proof_ne :: proofs_ideal @ proofs_cone)
32645
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
  1260
         end)
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
  1261
     in
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
  1262
        (translator (eqs,les,lts) proof, RealArith.Cert proof)
32645
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
  1263
     end)
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1264
   end
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1265
 in mainf end
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1266
end
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1267
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1268
fun C f x y = f y x;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1269
  (* FIXME : This is very bad!!!*)
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1270
fun subst_conv eqs t =
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1271
 let
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1272
  val t' = fold (Thm.cabs o Thm.lhs_of) eqs t
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1273
 in Conv.fconv_rule (Thm.beta_conversion true) (fold (C combination) eqs (reflexive t'))
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1274
 end
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1275
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1276
(* A wrapper that tries to substitute away variables first.                  *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1277
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1278
local
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
  1279
 open Conv
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1280
  fun simple_cterm_ord t u = TermOrd.fast_term_ord (term_of t, term_of u) = LESS
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
  1281
 val concl = Thm.dest_arg o cprop_of
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1282
 val shuffle1 =
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1283
   fconv_rule (rewr_conv @{lemma "(a + x == y) == (x == y - (a::real))" by (atomize (full)) (simp add: ring_simps) })
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1284
 val shuffle2 =
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1285
    fconv_rule (rewr_conv @{lemma "(x + a == y) ==  (x == y - (a::real))" by (atomize (full)) (simp add: ring_simps)})
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1286
 fun substitutable_monomial fvs tm = case term_of tm of
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1287
    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
  1288
                           else raise Failure "substitutable_monomial"
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1289
  | @{term "op * :: real => _"}$c$(t as Free _ ) =>
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
  1290
     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
  1291
         then (RealArith.dest_ratconst (Thm.dest_arg1 tm),Thm.dest_arg tm) else raise Failure "substitutable_monomial"
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1292
  | @{term "op + :: real => _"}$s$t =>
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
  1293
       (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
  1294
        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
  1295
  | _ => raise Failure "substitutable_monomial"
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1296
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1297
  fun isolate_variable v th =
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
  1298
   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
  1299
   in if v aconvc w then th
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1300
      else case term_of w of
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1301
           @{term "op + :: real => _"}$s$t =>
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1302
              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
  1303
              else isolate_variable v (shuffle1 th)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1304
          | _ => error "isolate variable : This should not happen?"
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1305
   end
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1306
in
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1307
32268
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents: 32150
diff changeset
  1308
fun real_nonlinear_subst_prover prover ctxt =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1309
 let
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1310
  val {add,mul,neg,pow,sub,main} =  Normalizer.semiring_normalizers_ord_wrapper ctxt
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1311
      (valOf (NormalizerData.match ctxt @{cterm "(0::real) + 1"}))
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1312
     simple_cterm_ord
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1313
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1314
  val (real_poly_add_conv,real_poly_mul_conv,real_poly_neg_conv,
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1315
       real_poly_pow_conv,real_poly_sub_conv,real_poly_conv) = (add,mul,neg,pow,sub,main)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1316
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1317
  fun make_substitution th =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1318
   let
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
  1319
    val (c,v) = substitutable_monomial [] (Thm.dest_arg1(concl th))
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
  1320
    val th1 = Drule.arg_cong_rule (Thm.capply @{cterm "op * :: real => _"} (RealArith.cterm_of_rat (Rat.inv c))) (mk_meta_eq th)
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1321
    val th2 = fconv_rule (binop_conv real_poly_mul_conv) th1
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1322
   in fconv_rule (arg_conv real_poly_conv) (isolate_variable v th2)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1323
   end
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1324
   fun oprconv cv ct =
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1325
    let val g = Thm.dest_fun2 ct
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1326
    in if g aconvc @{cterm "op <= :: real => _"}
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1327
         orelse g aconvc @{cterm "op < :: real => _"}
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1328
       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
  1329
    end
32645
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
  1330
  fun mainf cert_choice translator =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1331
   let
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1332
    fun substfirst(eqs,les,lts) =
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1333
      ((let
31119
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1334
           val eth = tryfind make_substitution eqs
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1335
           val modify = fconv_rule (arg_conv (oprconv(subst_conv [eth] then_conv real_poly_conv)))
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1336
       in  substfirst
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1337
             (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
  1338
                                   aconvc @{cterm "0::real"}) (map modify eqs),
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1339
                                   map modify les,map modify lts)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1340
       end)
32645
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
  1341
       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
  1342
    in substfirst
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1343
   end
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1344
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1345
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1346
 in mainf
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1347
 end
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1348
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1349
(* Overall function. *)
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1350
32645
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents: 32332
diff changeset
  1351
fun real_sos prover ctxt =
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32740
diff changeset
  1352
  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
  1353
end;
2532bb2d65c7 A decision method for universal multivariate real arithmetic with add
chaieb
parents:
diff changeset
  1354
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1355
val known_sos_constants =
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1356
  [@{term "op ==>"}, @{term "Trueprop"},
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1357
   @{term "op -->"}, @{term "op &"}, @{term "op |"},
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1358
   @{term "Not"}, @{term "op = :: bool => _"},
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1359
   @{term "All :: (real => _) => _"}, @{term "Ex :: (real => _) => _"},
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1360
   @{term "op = :: real => _"}, @{term "op < :: real => _"},
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1361
   @{term "op <= :: real => _"},
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1362
   @{term "op + :: real => _"}, @{term "op - :: real => _"},
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1363
   @{term "op * :: real => _"}, @{term "uminus :: real => _"},
31512
27118561c2e0 Tuned sos tactic to reject non SOS goals
chaieb
parents: 31131
diff changeset
  1364
   @{term "op / :: real => _"}, @{term "inverse :: real => _"},
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1365
   @{term "op ^ :: real => _"}, @{term "abs :: real => _"},
31512
27118561c2e0 Tuned sos tactic to reject non SOS goals
chaieb
parents: 31131
diff changeset
  1366
   @{term "min :: real => _"}, @{term "max :: real => _"},
27118561c2e0 Tuned sos tactic to reject non SOS goals
chaieb
parents: 31131
diff changeset
  1367
   @{term "0::real"}, @{term "1::real"}, @{term "number_of :: int => real"},
27118561c2e0 Tuned sos tactic to reject non SOS goals
chaieb
parents: 31131
diff changeset
  1368
   @{term "number_of :: int => nat"},
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1369
   @{term "Int.Bit0"}, @{term "Int.Bit1"},
31512
27118561c2e0 Tuned sos tactic to reject non SOS goals
chaieb
parents: 31131
diff changeset
  1370
   @{term "Int.Pls"}, @{term "Int.Min"}];
27118561c2e0 Tuned sos tactic to reject non SOS goals
chaieb
parents: 31131
diff changeset
  1371
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1372
fun check_sos kcts ct =
31512
27118561c2e0 Tuned sos tactic to reject non SOS goals
chaieb
parents: 31131
diff changeset
  1373
 let
27118561c2e0 Tuned sos tactic to reject non SOS goals
chaieb
parents: 31131
diff changeset
  1374
  val t = term_of ct
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1375
  val _ = if not (null (Term.add_tfrees t [])
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1376
                  andalso null (Term.add_tvars t []))
31512
27118561c2e0 Tuned sos tactic to reject non SOS goals
chaieb
parents: 31131
diff changeset
  1377
          then error "SOS: not sos. Additional type varables" else ()
27118561c2e0 Tuned sos tactic to reject non SOS goals
chaieb
parents: 31131
diff changeset
  1378
  val fs = Term.add_frees t []
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1379
  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
  1380
          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
  1381
  val vs = Term.add_vars t []
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1382
  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
  1383
          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
  1384
  val ukcs = subtract (fn (t,p) => Const p aconv t) kcts (Term.add_consts t [])
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1385
  val _ = if  null ukcs then ()
31512
27118561c2e0 Tuned sos tactic to reject non SOS goals
chaieb
parents: 31131
diff changeset
  1386
              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
  1387
in () end
27118561c2e0 Tuned sos tactic to reject non SOS goals
chaieb
parents: 31131
diff changeset
  1388
32838
d9dfd30af9ae core_sos_tac: SUBPROOF body operates on subgoal 1;
wenzelm
parents: 32831
diff changeset
  1389
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
  1390
  let
32838
d9dfd30af9ae core_sos_tac: SUBPROOF body operates on subgoal 1;
wenzelm
parents: 32831
diff changeset
  1391
    val _ = check_sos known_sos_constants concl
d9dfd30af9ae core_sos_tac: SUBPROOF body operates on subgoal 1;
wenzelm
parents: 32831
diff changeset
  1392
    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
  1393
    val _ = print_cert certificates
d9dfd30af9ae core_sos_tac: SUBPROOF body operates on subgoal 1;
wenzelm
parents: 32831
diff changeset
  1394
  in rtac ths 1 end)
31131
d9752181691a Now deals with division
chaieb
parents: 31119
diff changeset
  1395
d9752181691a Now deals with division
chaieb
parents: 31119
diff changeset
  1396
fun default_SOME f NONE v = SOME v
d9752181691a Now deals with division
chaieb
parents: 31119
diff changeset
  1397
  | default_SOME f (SOME v) _ = SOME v;
d9752181691a Now deals with division
chaieb
parents: 31119
diff changeset
  1398
d9752181691a Now deals with division
chaieb
parents: 31119
diff changeset
  1399
fun lift_SOME f NONE a = f a
d9752181691a Now deals with division
chaieb
parents: 31119
diff changeset
  1400
  | lift_SOME f (SOME a) _ = SOME a;
d9752181691a Now deals with division
chaieb
parents: 31119
diff changeset
  1401
d9752181691a Now deals with division
chaieb
parents: 31119
diff changeset
  1402
d9752181691a Now deals with division
chaieb
parents: 31119
diff changeset
  1403
local
d9752181691a Now deals with division
chaieb
parents: 31119
diff changeset
  1404
 val is_numeral = can (HOLogic.dest_number o term_of)
d9752181691a Now deals with division
chaieb
parents: 31119
diff changeset
  1405
in
d9752181691a Now deals with division
chaieb
parents: 31119
diff changeset
  1406
fun get_denom b ct = case term_of ct of
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1407
  @{term "op / :: real => _"} $ _ $ _ =>
31131
d9752181691a Now deals with division
chaieb
parents: 31119
diff changeset
  1408
     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
  1409
     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
  1410
 | @{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
  1411
 | @{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
  1412
 | _ $ _ => 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
  1413
 | _ => NONE
d9752181691a Now deals with division
chaieb
parents: 31119
diff changeset
  1414
end;
d9752181691a Now deals with division
chaieb
parents: 31119
diff changeset
  1415
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1416
fun elim_one_denom_tac ctxt =
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1417
CSUBGOAL (fn (P,i) =>
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1418
 case get_denom false P of
31131
d9752181691a Now deals with division
chaieb
parents: 31119
diff changeset
  1419
   NONE => no_tac
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1420
 | SOME (d,ord) =>
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1421
     let
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1422
      val ss = simpset_of ctxt addsimps @{thms field_simps}
31131
d9752181691a Now deals with division
chaieb
parents: 31119
diff changeset
  1423
               addsimps [@{thm nonzero_power_divide}, @{thm power_divide}]
32839
a007a7cd8c2f tuned header;
wenzelm
parents: 32838
diff changeset
  1424
      val th = instantiate' [] [SOME d, SOME (Thm.dest_arg P)]
31131
d9752181691a Now deals with division
chaieb
parents: 31119
diff changeset
  1425
         (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
  1426
          else @{lemma "(d=0 --> P) & (d ~= (0::real) --> P) ==> P" by blast})
32949
aa6c470a962a eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents: 32839
diff changeset
  1427
     in rtac th i THEN Simplifier.asm_full_simp_tac ss i end);
31131
d9752181691a Now deals with division
chaieb
parents: 31119
diff changeset
  1428
d9752181691a Now deals with division
chaieb
parents: 31119
diff changeset
  1429
fun elim_denom_tac ctxt i = REPEAT (elim_one_denom_tac ctxt i);
d9752181691a Now deals with division
chaieb
parents: 31119
diff changeset
  1430
32949
aa6c470a962a eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents: 32839
diff changeset
  1431
fun sos_tac print_cert prover ctxt =
aa6c470a962a eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents: 32839
diff changeset
  1432
  ObjectLogic.full_atomize_tac THEN'
aa6c470a962a eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents: 32839
diff changeset
  1433
  elim_denom_tac ctxt THEN'
aa6c470a962a eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents: 32839
diff changeset
  1434
  core_sos_tac print_cert prover ctxt;
31131
d9752181691a Now deals with division
chaieb
parents: 31119
diff changeset
  1435
31512
27118561c2e0 Tuned sos tactic to reject non SOS goals
chaieb
parents: 31131
diff changeset
  1436
end;