src/HOL/Library/Sum_of_Squares/positivstellensatz_tools.ML
author wenzelm
Sat, 15 Feb 2014 21:11:29 +0100
changeset 55508 90c42b130652
parent 43946 ba88bb44c192
child 58629 a6a6cd499d4e
permissions -rw-r--r--
tuned whitespace;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
41474
60d091240485 renamed Sum_Of_Squares to Sum_of_Squares;
wenzelm
parents: 37744
diff changeset
     1
(*  Title:      HOL/Library/Sum_of_Squares/positivstellensatz_tools.ML
60d091240485 renamed Sum_Of_Squares to Sum_of_Squares;
wenzelm
parents: 37744
diff changeset
     2
    Author:     Philipp Meyer, TU Muenchen
32645
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
     3
41474
60d091240485 renamed Sum_Of_Squares to Sum_of_Squares;
wenzelm
parents: 37744
diff changeset
     4
Functions for generating a certificate from a positivstellensatz and vice
60d091240485 renamed Sum_Of_Squares to Sum_of_Squares;
wenzelm
parents: 37744
diff changeset
     5
versa.
32645
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
     6
*)
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
     7
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
     8
signature POSITIVSTELLENSATZ_TOOLS =
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
     9
sig
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
    10
  val pss_tree_to_cert : RealArith.pss_tree -> string
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
    11
  val cert_to_pss_tree : Proof.context -> string -> RealArith.pss_tree
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
    12
end
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
    13
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
    14
structure PositivstellensatzTools : POSITIVSTELLENSATZ_TOOLS =
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
    15
struct
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
    16
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
    17
(*** certificate generation ***)
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
    18
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
    19
fun string_of_rat r =
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
    20
  let
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
    21
    val (nom, den) = Rat.quotient_of_rat r
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
    22
  in
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
    23
    if den = 1 then string_of_int nom
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
    24
    else string_of_int nom ^ "/" ^ string_of_int den
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
    25
  end
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
    26
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
    27
(* map polynomials to strings *)
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
    28
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
    29
fun string_of_varpow x k =
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
    30
  let
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
    31
    val term = term_of x
55508
90c42b130652 tuned whitespace;
wenzelm
parents: 43946
diff changeset
    32
    val name =
90c42b130652 tuned whitespace;
wenzelm
parents: 43946
diff changeset
    33
      (case term of
90c42b130652 tuned whitespace;
wenzelm
parents: 43946
diff changeset
    34
        Free (n, _) => n
90c42b130652 tuned whitespace;
wenzelm
parents: 43946
diff changeset
    35
      | _ => error "Term in monomial not free variable")
32645
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
    36
  in
55508
90c42b130652 tuned whitespace;
wenzelm
parents: 43946
diff changeset
    37
    if k = 1 then name else name ^ "^" ^ string_of_int k
32645
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
    38
  end
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
    39
55508
90c42b130652 tuned whitespace;
wenzelm
parents: 43946
diff changeset
    40
fun string_of_monomial m =
90c42b130652 tuned whitespace;
wenzelm
parents: 43946
diff changeset
    41
  if FuncUtil.Ctermfunc.is_empty m then "1"
90c42b130652 tuned whitespace;
wenzelm
parents: 43946
diff changeset
    42
  else
90c42b130652 tuned whitespace;
wenzelm
parents: 43946
diff changeset
    43
    let
90c42b130652 tuned whitespace;
wenzelm
parents: 43946
diff changeset
    44
      val m' = FuncUtil.dest_monomial m
90c42b130652 tuned whitespace;
wenzelm
parents: 43946
diff changeset
    45
      val vps = fold_rev (fn (x,k) => cons (string_of_varpow x k)) m' []
90c42b130652 tuned whitespace;
wenzelm
parents: 43946
diff changeset
    46
    in foldr1 (fn (s, t) => s ^ "*" ^ t) vps end
32645
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
    47
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
    48
fun string_of_cmonomial (m,c) =
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
    49
  if FuncUtil.Ctermfunc.is_empty m then string_of_rat c
32645
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
    50
  else if c = Rat.one then string_of_monomial m
55508
90c42b130652 tuned whitespace;
wenzelm
parents: 43946
diff changeset
    51
  else string_of_rat c ^ "*" ^ string_of_monomial m;
32645
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
    52
55508
90c42b130652 tuned whitespace;
wenzelm
parents: 43946
diff changeset
    53
fun string_of_poly p =
90c42b130652 tuned whitespace;
wenzelm
parents: 43946
diff changeset
    54
  if FuncUtil.Monomialfunc.is_empty p then "0"
90c42b130652 tuned whitespace;
wenzelm
parents: 43946
diff changeset
    55
  else
90c42b130652 tuned whitespace;
wenzelm
parents: 43946
diff changeset
    56
    let
90c42b130652 tuned whitespace;
wenzelm
parents: 43946
diff changeset
    57
      val cms = map string_of_cmonomial
90c42b130652 tuned whitespace;
wenzelm
parents: 43946
diff changeset
    58
        (sort (prod_ord FuncUtil.monomial_order (K EQUAL)) (FuncUtil.Monomialfunc.dest p))
90c42b130652 tuned whitespace;
wenzelm
parents: 43946
diff changeset
    59
    in foldr1 (fn (t1, t2) => t1 ^ " + " ^ t2) cms end;
32645
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
    60
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32646
diff changeset
    61
fun pss_to_cert (RealArith.Axiom_eq i) = "A=" ^ string_of_int i
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32646
diff changeset
    62
  | pss_to_cert (RealArith.Axiom_le i) = "A<=" ^ string_of_int i
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32646
diff changeset
    63
  | pss_to_cert (RealArith.Axiom_lt i) = "A<" ^ string_of_int i
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32646
diff changeset
    64
  | pss_to_cert (RealArith.Rational_eq r) = "R=" ^ string_of_rat r
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32646
diff changeset
    65
  | pss_to_cert (RealArith.Rational_le r) = "R<=" ^ string_of_rat r
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32646
diff changeset
    66
  | pss_to_cert (RealArith.Rational_lt r) = "R<" ^ string_of_rat r
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32646
diff changeset
    67
  | pss_to_cert (RealArith.Square p) = "[" ^ string_of_poly p ^ "]^2"
55508
90c42b130652 tuned whitespace;
wenzelm
parents: 43946
diff changeset
    68
  | pss_to_cert (RealArith.Eqmul (p, pss)) =
90c42b130652 tuned whitespace;
wenzelm
parents: 43946
diff changeset
    69
      "([" ^ string_of_poly p ^ "] * " ^ pss_to_cert pss ^ ")"
90c42b130652 tuned whitespace;
wenzelm
parents: 43946
diff changeset
    70
  | pss_to_cert (RealArith.Sum (pss1, pss2)) =
90c42b130652 tuned whitespace;
wenzelm
parents: 43946
diff changeset
    71
      "(" ^ pss_to_cert pss1 ^ " + " ^ pss_to_cert pss2 ^ ")"
90c42b130652 tuned whitespace;
wenzelm
parents: 43946
diff changeset
    72
  | pss_to_cert (RealArith.Product (pss1, pss2)) =
90c42b130652 tuned whitespace;
wenzelm
parents: 43946
diff changeset
    73
      "(" ^ pss_to_cert pss1 ^ " * " ^ pss_to_cert pss2 ^ ")"
32645
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
    74
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32646
diff changeset
    75
fun pss_tree_to_cert RealArith.Trivial = "()"
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32646
diff changeset
    76
  | pss_tree_to_cert (RealArith.Cert pss) = "(" ^ pss_to_cert pss ^ ")"
55508
90c42b130652 tuned whitespace;
wenzelm
parents: 43946
diff changeset
    77
  | pss_tree_to_cert (RealArith.Branch (t1, t2)) =
90c42b130652 tuned whitespace;
wenzelm
parents: 43946
diff changeset
    78
      "(" ^ pss_tree_to_cert t1 ^ " & " ^ pss_tree_to_cert t2 ^ ")"
90c42b130652 tuned whitespace;
wenzelm
parents: 43946
diff changeset
    79
32645
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
    80
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
    81
(*** certificate parsing ***)
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
    82
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
    83
(* basic parser *)
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
    84
32646
962b4354ed90 used standard fold function and type aliases
Philipp Meyer
parents: 32645
diff changeset
    85
val str = Scan.this_string
32645
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
    86
55508
90c42b130652 tuned whitespace;
wenzelm
parents: 43946
diff changeset
    87
val number =
90c42b130652 tuned whitespace;
wenzelm
parents: 43946
diff changeset
    88
  Scan.repeat1 (Scan.one Symbol.is_ascii_digit >> (fn s => ord s - ord "0"))
90c42b130652 tuned whitespace;
wenzelm
parents: 43946
diff changeset
    89
    >> foldl1 (fn (n, d) => n * 10 + d)
32645
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
    90
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
    91
val nat = number
32646
962b4354ed90 used standard fold function and type aliases
Philipp Meyer
parents: 32645
diff changeset
    92
val int = Scan.optional (str "~" >> K ~1) 1 -- nat >> op *;
962b4354ed90 used standard fold function and type aliases
Philipp Meyer
parents: 32645
diff changeset
    93
val rat = int --| str "/" -- int >> Rat.rat_of_quotient
32645
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
    94
val rat_int = rat || int >> Rat.rat_of_int
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
    95
55508
90c42b130652 tuned whitespace;
wenzelm
parents: 43946
diff changeset
    96
32645
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
    97
(* polynomial parser *)
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
    98
32646
962b4354ed90 used standard fold function and type aliases
Philipp Meyer
parents: 32645
diff changeset
    99
fun repeat_sep s f = f ::: Scan.repeat (str s |-- f)
32645
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
   100
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
   101
val parse_id = Scan.one Symbol.is_letter ::: Scan.many Symbol.is_letdig >> implode
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
   102
32646
962b4354ed90 used standard fold function and type aliases
Philipp Meyer
parents: 32645
diff changeset
   103
fun parse_varpow ctxt = parse_id -- Scan.optional (str "^" |-- nat) 1 >>
55508
90c42b130652 tuned whitespace;
wenzelm
parents: 43946
diff changeset
   104
  (fn (x, k) => (cterm_of (Proof_Context.theory_of ctxt) (Free (x, @{typ real})), k))
32645
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
   105
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
   106
fun parse_monomial ctxt = repeat_sep "*" (parse_varpow ctxt) >>
33339
d41f77196338 eliminated some old folds;
wenzelm
parents: 33030
diff changeset
   107
  (fn xs => fold FuncUtil.Ctermfunc.update xs FuncUtil.Ctermfunc.empty)
32645
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
   108
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
   109
fun parse_cmonomial ctxt =
32646
962b4354ed90 used standard fold function and type aliases
Philipp Meyer
parents: 32645
diff changeset
   110
  rat_int --| str "*" -- (parse_monomial ctxt) >> swap ||
32645
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
   111
  (parse_monomial ctxt) >> (fn m => (m, Rat.one)) ||
32829
671eb46eb0a3 tuned FuncFun and FuncUtil structure in positivstellensatz.ML
Philipp Meyer
parents: 32828
diff changeset
   112
  rat_int >> (fn r => (FuncUtil.Ctermfunc.empty, r))
32645
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
   113
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
   114
fun parse_poly ctxt = repeat_sep "+" (parse_cmonomial ctxt) >>
33339
d41f77196338 eliminated some old folds;
wenzelm
parents: 33030
diff changeset
   115
  (fn xs => fold FuncUtil.Monomialfunc.update xs FuncUtil.Monomialfunc.empty)
32645
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
   116
55508
90c42b130652 tuned whitespace;
wenzelm
parents: 43946
diff changeset
   117
32645
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
   118
(* positivstellensatz parser *)
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
   119
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
   120
val parse_axiom =
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32646
diff changeset
   121
  (str "A=" |-- int >> RealArith.Axiom_eq) ||
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32646
diff changeset
   122
  (str "A<=" |-- int >> RealArith.Axiom_le) ||
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32646
diff changeset
   123
  (str "A<" |-- int >> RealArith.Axiom_lt)
32645
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
   124
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
   125
val parse_rational =
32828
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32646
diff changeset
   126
  (str "R=" |-- rat_int >> RealArith.Rational_eq) ||
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32646
diff changeset
   127
  (str "R<=" |-- rat_int >> RealArith.Rational_le) ||
ad76967c703d removed opening of structures
Philipp Meyer
parents: 32646
diff changeset
   128
  (str "R<" |-- rat_int >> RealArith.Rational_lt)
32645
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
   129
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
   130
fun parse_cert ctxt input =
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
   131
  let
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
   132
    val pc = parse_cert ctxt
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
   133
    val pp = parse_poly ctxt
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
   134
  in
55508
90c42b130652 tuned whitespace;
wenzelm
parents: 43946
diff changeset
   135
    (parse_axiom ||
90c42b130652 tuned whitespace;
wenzelm
parents: 43946
diff changeset
   136
     parse_rational ||
90c42b130652 tuned whitespace;
wenzelm
parents: 43946
diff changeset
   137
     str "[" |-- pp --| str "]^2" >> RealArith.Square ||
90c42b130652 tuned whitespace;
wenzelm
parents: 43946
diff changeset
   138
     str "([" |-- pp --| str "]*" -- pc --| str ")" >> RealArith.Eqmul ||
90c42b130652 tuned whitespace;
wenzelm
parents: 43946
diff changeset
   139
     str "(" |-- pc --| str "*" -- pc --| str ")" >> RealArith.Product ||
90c42b130652 tuned whitespace;
wenzelm
parents: 43946
diff changeset
   140
     str "(" |-- pc --| str "+" -- pc --| str ")" >> RealArith.Sum) input
32645
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
   141
  end
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
   142
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
   143
fun parse_cert_tree ctxt input =
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
   144
  let
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
   145
    val pc = parse_cert ctxt
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
   146
    val pt = parse_cert_tree ctxt
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
   147
  in
55508
90c42b130652 tuned whitespace;
wenzelm
parents: 43946
diff changeset
   148
    (str "()" >> K RealArith.Trivial ||
90c42b130652 tuned whitespace;
wenzelm
parents: 43946
diff changeset
   149
     str "(" |-- pc --| str ")" >> RealArith.Cert ||
90c42b130652 tuned whitespace;
wenzelm
parents: 43946
diff changeset
   150
     str "(" |-- pt --| str "&" -- pt --| str ")" >> RealArith.Branch) input
32645
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
   151
  end
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
   152
55508
90c42b130652 tuned whitespace;
wenzelm
parents: 43946
diff changeset
   153
32645
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
   154
(* scanner *)
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
   155
43946
wenzelm
parents: 42361
diff changeset
   156
fun cert_to_pss_tree ctxt input_str =
wenzelm
parents: 42361
diff changeset
   157
  Symbol.scanner "Bad certificate" (parse_cert_tree ctxt)
wenzelm
parents: 42361
diff changeset
   158
    (filter_out Symbol.is_blank (Symbol.explode input_str))
32645
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
   159
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
   160
end
1cc5b24f5a01 sos method generates and uses proof certificates
Philipp Meyer
parents:
diff changeset
   161