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