author | wenzelm |
Mon, 25 Jun 2007 00:36:34 +0200 | |
changeset 23485 | 881b04972953 |
parent 23335 | 42b827dfa86b |
child 23581 | 297c6d706322 |
permissions | -rw-r--r-- |
23252 | 1 |
(* Title: HOL/Tools/Groebner_Basis/normalizer_data.ML |
2 |
ID: $Id$ |
|
3 |
Author: Amine Chaieb, TU Muenchen |
|
4 |
||
5 |
Ring normalization data. |
|
6 |
*) |
|
7 |
||
8 |
signature NORMALIZER_DATA = |
|
9 |
sig |
|
10 |
type entry |
|
23335
42b827dfa86b
Changed normalizer Data to contain a simpset besides the list of instances; Before the method starts, it simplifies with this simpset; Added attribute 'algebra' to add and delete theorems from the simpset above;
chaieb
parents:
23330
diff
changeset
|
11 |
val get: Proof.context -> simpset * ((thm * entry) list) |
23252 | 12 |
val match: Proof.context -> cterm -> entry option |
13 |
val del: attribute |
|
14 |
val add: {semiring: cterm list * thm list, ring: cterm list * thm list, idom: thm list} |
|
15 |
-> attribute |
|
16 |
val funs: thm -> {is_const: morphism -> cterm -> bool, |
|
17 |
dest_const: morphism -> cterm -> Rat.rat, |
|
18 |
mk_const: morphism -> ctyp -> Rat.rat -> cterm, |
|
23485 | 19 |
conv: morphism -> Proof.context -> cterm -> thm} -> |
20 |
morphism -> Context.generic -> Context.generic |
|
23252 | 21 |
val setup: theory -> theory |
22 |
end; |
|
23 |
||
24 |
structure NormalizerData: NORMALIZER_DATA = |
|
25 |
struct |
|
26 |
||
27 |
(* data *) |
|
28 |
||
29 |
type entry = |
|
30 |
{vars: cterm list, |
|
31 |
semiring: cterm list * thm list, |
|
32 |
ring: cterm list * thm list, |
|
33 |
idom: thm list} * |
|
34 |
{is_const: cterm -> bool, |
|
35 |
dest_const: cterm -> Rat.rat, |
|
36 |
mk_const: ctyp -> Rat.rat -> cterm, |
|
23330
01c09922ce59
Conversion for computation on constants now depends on the context
chaieb
parents:
23260
diff
changeset
|
37 |
conv: Proof.context -> cterm -> thm}; |
23252 | 38 |
|
39 |
val eq_key = Thm.eq_thm; |
|
40 |
fun eq_data arg = eq_fst eq_key arg; |
|
41 |
||
42 |
structure Data = GenericDataFun |
|
43 |
( |
|
23335
42b827dfa86b
Changed normalizer Data to contain a simpset besides the list of instances; Before the method starts, it simplifies with this simpset; Added attribute 'algebra' to add and delete theorems from the simpset above;
chaieb
parents:
23330
diff
changeset
|
44 |
type T = simpset * ((thm * entry) list); |
42b827dfa86b
Changed normalizer Data to contain a simpset besides the list of instances; Before the method starts, it simplifies with this simpset; Added attribute 'algebra' to add and delete theorems from the simpset above;
chaieb
parents:
23330
diff
changeset
|
45 |
val empty = (HOL_basic_ss, []); |
23252 | 46 |
val extend = I; |
23335
42b827dfa86b
Changed normalizer Data to contain a simpset besides the list of instances; Before the method starts, it simplifies with this simpset; Added attribute 'algebra' to add and delete theorems from the simpset above;
chaieb
parents:
23330
diff
changeset
|
47 |
fun merge _ ((ss, e), (ss', e')) = (merge_ss (ss, ss'), |
42b827dfa86b
Changed normalizer Data to contain a simpset besides the list of instances; Before the method starts, it simplifies with this simpset; Added attribute 'algebra' to add and delete theorems from the simpset above;
chaieb
parents:
23330
diff
changeset
|
48 |
AList.merge eq_key (K true) (e,e')); |
23252 | 49 |
); |
50 |
||
51 |
val get = Data.get o Context.Proof; |
|
52 |
||
53 |
||
54 |
(* match data *) |
|
55 |
||
56 |
fun match ctxt tm = |
|
57 |
let |
|
58 |
fun match_inst |
|
59 |
({vars, semiring = (sr_ops, sr_rules), ring = (r_ops, r_rules), idom}, |
|
60 |
fns as {is_const, dest_const, mk_const, conv}) pat = |
|
61 |
let |
|
62 |
fun h instT = |
|
63 |
let |
|
64 |
val substT = Thm.instantiate (instT, []); |
|
65 |
val substT_cterm = Drule.cterm_rule substT; |
|
66 |
||
67 |
val vars' = map substT_cterm vars; |
|
68 |
val semiring' = (map substT_cterm sr_ops, map substT sr_rules); |
|
69 |
val ring' = (map substT_cterm r_ops, map substT r_rules); |
|
70 |
val idom' = map substT idom; |
|
71 |
||
72 |
val result = ({vars = vars', semiring = semiring', ring = ring', idom = idom'}, fns); |
|
73 |
in SOME result end |
|
74 |
in (case try Thm.match (pat, tm) of |
|
75 |
NONE => NONE |
|
76 |
| SOME (instT, _) => h instT) |
|
77 |
end; |
|
78 |
||
79 |
fun match_struct (_, |
|
80 |
entry as ({semiring = (sr_ops, _), ring = (r_ops, _), ...}, _): entry) = |
|
81 |
get_first (match_inst entry) (sr_ops @ r_ops); |
|
23335
42b827dfa86b
Changed normalizer Data to contain a simpset besides the list of instances; Before the method starts, it simplifies with this simpset; Added attribute 'algebra' to add and delete theorems from the simpset above;
chaieb
parents:
23330
diff
changeset
|
82 |
in get_first match_struct (snd (get ctxt)) end; |
23252 | 83 |
|
84 |
||
85 |
(* logical content *) |
|
86 |
||
87 |
val semiringN = "semiring"; |
|
88 |
val ringN = "ring"; |
|
89 |
val idomN = "idom"; |
|
90 |
||
91 |
fun undefined _ = raise Match; |
|
92 |
||
23335
42b827dfa86b
Changed normalizer Data to contain a simpset besides the list of instances; Before the method starts, it simplifies with this simpset; Added attribute 'algebra' to add and delete theorems from the simpset above;
chaieb
parents:
23330
diff
changeset
|
93 |
fun del_data key = apsnd (remove eq_data (key, [])); |
23252 | 94 |
|
95 |
val del = Thm.declaration_attribute (Data.map o del_data); |
|
23335
42b827dfa86b
Changed normalizer Data to contain a simpset besides the list of instances; Before the method starts, it simplifies with this simpset; Added attribute 'algebra' to add and delete theorems from the simpset above;
chaieb
parents:
23330
diff
changeset
|
96 |
val add_ss = Thm.declaration_attribute |
42b827dfa86b
Changed normalizer Data to contain a simpset besides the list of instances; Before the method starts, it simplifies with this simpset; Added attribute 'algebra' to add and delete theorems from the simpset above;
chaieb
parents:
23330
diff
changeset
|
97 |
(fn th => Data.map (fn (ss,data) => (ss addsimps [th], data))); |
42b827dfa86b
Changed normalizer Data to contain a simpset besides the list of instances; Before the method starts, it simplifies with this simpset; Added attribute 'algebra' to add and delete theorems from the simpset above;
chaieb
parents:
23330
diff
changeset
|
98 |
|
42b827dfa86b
Changed normalizer Data to contain a simpset besides the list of instances; Before the method starts, it simplifies with this simpset; Added attribute 'algebra' to add and delete theorems from the simpset above;
chaieb
parents:
23330
diff
changeset
|
99 |
val del_ss = Thm.declaration_attribute |
42b827dfa86b
Changed normalizer Data to contain a simpset besides the list of instances; Before the method starts, it simplifies with this simpset; Added attribute 'algebra' to add and delete theorems from the simpset above;
chaieb
parents:
23330
diff
changeset
|
100 |
(fn th => Data.map (fn (ss,data) => (ss delsimps [th], data))); |
23252 | 101 |
|
102 |
fun add {semiring = (sr_ops, sr_rules), ring = (r_ops, r_rules), idom} = |
|
103 |
Thm.declaration_attribute (fn key => fn context => context |> Data.map |
|
104 |
let |
|
105 |
val ctxt = Context.proof_of context; |
|
106 |
||
107 |
fun check kind name xs n = |
|
108 |
null xs orelse length xs = n orelse |
|
109 |
error ("Expected " ^ string_of_int n ^ " " ^ kind ^ " for " ^ name); |
|
110 |
val check_ops = check "operations"; |
|
111 |
val check_rules = check "rules"; |
|
112 |
||
113 |
val _ = |
|
114 |
check_ops semiringN sr_ops 5 andalso |
|
115 |
check_rules semiringN sr_rules 37 andalso |
|
116 |
check_ops ringN r_ops 2 andalso |
|
117 |
check_rules ringN r_rules 2 andalso |
|
118 |
check_rules idomN idom 2; |
|
119 |
||
120 |
val mk_meta = LocalDefs.meta_rewrite_rule ctxt; |
|
121 |
val sr_rules' = map mk_meta sr_rules; |
|
122 |
val r_rules' = map mk_meta r_rules; |
|
123 |
||
124 |
fun rule i = nth sr_rules' (i - 1); |
|
125 |
||
126 |
val (cx, cy) = Thm.dest_binop (hd sr_ops); |
|
127 |
val cz = rule 34 |> Thm.rhs_of |> Thm.dest_arg |> Thm.dest_arg; |
|
128 |
val cn = rule 36 |> Thm.rhs_of |> Thm.dest_arg |> Thm.dest_arg; |
|
129 |
val ((clx, crx), (cly, cry)) = |
|
130 |
rule 13 |> Thm.rhs_of |> Thm.dest_binop |> pairself Thm.dest_binop; |
|
131 |
val ((ca, cb), (cc, cd)) = |
|
132 |
rule 20 |> Thm.lhs_of |> Thm.dest_binop |> pairself Thm.dest_binop; |
|
133 |
val cm = rule 1 |> Thm.rhs_of |> Thm.dest_arg; |
|
134 |
val (cp, cq) = rule 26 |> Thm.lhs_of |> Thm.dest_binop |> pairself Thm.dest_arg; |
|
135 |
||
136 |
val vars = [ca, cb, cc, cd, cm, cn, cp, cq, cx, cy, cz, clx, crx, cly, cry]; |
|
137 |
val _ = map print_thm sr_rules'; |
|
138 |
val semiring = (sr_ops, sr_rules'); |
|
139 |
val ring = (r_ops, r_rules'); |
|
140 |
in |
|
141 |
del_data key #> |
|
23335
42b827dfa86b
Changed normalizer Data to contain a simpset besides the list of instances; Before the method starts, it simplifies with this simpset; Added attribute 'algebra' to add and delete theorems from the simpset above;
chaieb
parents:
23330
diff
changeset
|
142 |
apsnd (cons (key, ({vars = vars, semiring = semiring, ring = ring, idom = idom}, |
42b827dfa86b
Changed normalizer Data to contain a simpset besides the list of instances; Before the method starts, it simplifies with this simpset; Added attribute 'algebra' to add and delete theorems from the simpset above;
chaieb
parents:
23330
diff
changeset
|
143 |
{is_const = undefined, dest_const = undefined, mk_const = undefined, |
42b827dfa86b
Changed normalizer Data to contain a simpset besides the list of instances; Before the method starts, it simplifies with this simpset; Added attribute 'algebra' to add and delete theorems from the simpset above;
chaieb
parents:
23330
diff
changeset
|
144 |
conv = undefined}))) |
23252 | 145 |
end); |
146 |
||
147 |
||
148 |
(* extra-logical functions *) |
|
149 |
||
23335
42b827dfa86b
Changed normalizer Data to contain a simpset besides the list of instances; Before the method starts, it simplifies with this simpset; Added attribute 'algebra' to add and delete theorems from the simpset above;
chaieb
parents:
23330
diff
changeset
|
150 |
fun funs raw_key {is_const, dest_const, mk_const, conv} phi = |
42b827dfa86b
Changed normalizer Data to contain a simpset besides the list of instances; Before the method starts, it simplifies with this simpset; Added attribute 'algebra' to add and delete theorems from the simpset above;
chaieb
parents:
23330
diff
changeset
|
151 |
(Data.map o apsnd) (fn data => |
23252 | 152 |
let |
153 |
val key = Morphism.thm phi raw_key; |
|
154 |
val _ = AList.defined eq_key data key orelse |
|
155 |
raise THM ("No data entry for structure key", 0, [key]); |
|
156 |
val fns = {is_const = is_const phi, dest_const = dest_const phi, |
|
157 |
mk_const = mk_const phi, conv = conv phi}; |
|
158 |
in AList.map_entry eq_key key (apsnd (K fns)) data end); |
|
159 |
||
160 |
||
161 |
(* concrete syntax *) |
|
162 |
||
163 |
local |
|
164 |
||
165 |
fun keyword k = Scan.lift (Args.$$$ k -- Args.colon) >> K (); |
|
166 |
fun keyword2 k1 k2 = Scan.lift (Args.$$$ k1 -- Args.$$$ k2 -- Args.colon) >> K (); |
|
167 |
fun keyword3 k1 k2 k3 = |
|
168 |
Scan.lift (Args.$$$ k1 -- Args.$$$ k2 -- Args.$$$ k3 -- Args.colon) >> K (); |
|
169 |
||
170 |
val opsN = "ops"; |
|
171 |
val rulesN = "rules"; |
|
172 |
||
173 |
val normN = "norm"; |
|
174 |
val constN = "const"; |
|
175 |
val delN = "del"; |
|
176 |
||
177 |
val any_keyword = |
|
178 |
keyword2 semiringN opsN || keyword2 semiringN rulesN || |
|
179 |
keyword2 ringN opsN || keyword2 ringN rulesN || |
|
180 |
keyword2 idomN rulesN; |
|
181 |
||
182 |
val thms = Scan.repeat (Scan.unless any_keyword Attrib.multi_thm) >> flat; |
|
183 |
val terms = thms >> map Drule.dest_term; |
|
184 |
||
185 |
fun optional scan = Scan.optional scan []; |
|
186 |
||
187 |
in |
|
188 |
||
189 |
fun att_syntax src = src |> Attrib.syntax |
|
190 |
(Scan.lift (Args.$$$ delN >> K del) || |
|
191 |
((keyword2 semiringN opsN |-- terms) -- |
|
192 |
(keyword2 semiringN rulesN |-- thms)) -- |
|
193 |
(optional (keyword2 ringN opsN |-- terms) -- |
|
194 |
optional (keyword2 ringN rulesN |-- thms)) -- |
|
195 |
optional (keyword2 idomN rulesN |-- thms) |
|
196 |
>> (fn ((sr, r), id) => add {semiring = sr, ring = r, idom = id})); |
|
197 |
||
198 |
end; |
|
199 |
||
200 |
||
201 |
(* theory setup *) |
|
202 |
||
203 |
val setup = |
|
23335
42b827dfa86b
Changed normalizer Data to contain a simpset besides the list of instances; Before the method starts, it simplifies with this simpset; Added attribute 'algebra' to add and delete theorems from the simpset above;
chaieb
parents:
23330
diff
changeset
|
204 |
Attrib.add_attributes |
42b827dfa86b
Changed normalizer Data to contain a simpset besides the list of instances; Before the method starts, it simplifies with this simpset; Added attribute 'algebra' to add and delete theorems from the simpset above;
chaieb
parents:
23330
diff
changeset
|
205 |
[("normalizer", att_syntax, "semiring normalizer data"), |
42b827dfa86b
Changed normalizer Data to contain a simpset besides the list of instances; Before the method starts, it simplifies with this simpset; Added attribute 'algebra' to add and delete theorems from the simpset above;
chaieb
parents:
23330
diff
changeset
|
206 |
("algebra", Attrib.add_del_args add_ss del_ss, "Pre-simplification for algebra")]; |
23252 | 207 |
|
208 |
end; |