author | haftmann |
Thu, 06 May 2010 16:41:14 +0200 | |
changeset 36703 | 6e870d7f32e5 |
parent 36702 | b455ebd63799 |
child 36704 | 9dd2fe596ace |
permissions | -rw-r--r-- |
23252 | 1 |
(* Title: HOL/Tools/Groebner_Basis/normalizer.ML |
2 |
Author: Amine Chaieb, TU Muenchen |
|
36700
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
3 |
|
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
4 |
Normalization of expressions in semirings. |
23252 | 5 |
*) |
6 |
||
7 |
signature NORMALIZER = |
|
8 |
sig |
|
36700
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
9 |
type entry |
36703
6e870d7f32e5
removed former algebra presimpset from signature
haftmann
parents:
36702
diff
changeset
|
10 |
val get: Proof.context -> (thm * entry) list |
36700
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
11 |
val match: Proof.context -> cterm -> entry option |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
12 |
val del: attribute |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
13 |
val add: {semiring: cterm list * thm list, ring: cterm list * thm list, field: cterm list * thm list, idom: thm list, ideal: thm list} |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
14 |
-> attribute |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
15 |
val funs: thm -> {is_const: morphism -> cterm -> bool, |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
16 |
dest_const: morphism -> cterm -> Rat.rat, |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
17 |
mk_const: morphism -> ctyp -> Rat.rat -> cterm, |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
18 |
conv: morphism -> Proof.context -> cterm -> thm} -> declaration |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
19 |
|
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
20 |
val semiring_normalize_conv : Proof.context -> conv |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
21 |
val semiring_normalize_ord_conv : Proof.context -> (cterm -> cterm -> bool) -> conv |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
22 |
val semiring_normalize_wrapper : Proof.context -> entry -> conv |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
23 |
val semiring_normalize_ord_wrapper : Proof.context -> entry -> |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
24 |
(cterm -> cterm -> bool) -> conv |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
25 |
val semiring_normalizers_conv : |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
26 |
cterm list -> cterm list * thm list -> cterm list * thm list -> cterm list * thm list -> |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
27 |
(cterm -> bool) * conv * conv * conv -> (cterm -> cterm -> bool) -> |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
28 |
{add: conv, mul: conv, neg: conv, main: conv, pow: conv, sub: conv} |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
29 |
val semiring_normalizers_ord_wrapper : |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
30 |
Proof.context -> entry -> (cterm -> cterm -> bool) -> |
27222 | 31 |
{add: conv, mul: conv, neg: conv, main: conv, pow: conv, sub: conv} |
36700
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
32 |
|
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
33 |
val setup: theory -> theory |
23252 | 34 |
end |
35 |
||
36 |
structure Normalizer: NORMALIZER = |
|
37 |
struct |
|
23559 | 38 |
|
36700
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
39 |
(* data *) |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
40 |
|
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
41 |
type entry = |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
42 |
{vars: cterm list, |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
43 |
semiring: cterm list * thm list, |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
44 |
ring: cterm list * thm list, |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
45 |
field: cterm list * thm list, |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
46 |
idom: thm list, |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
47 |
ideal: thm list} * |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
48 |
{is_const: cterm -> bool, |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
49 |
dest_const: cterm -> Rat.rat, |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
50 |
mk_const: ctyp -> Rat.rat -> cterm, |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
51 |
conv: Proof.context -> cterm -> thm}; |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
52 |
|
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
53 |
val eq_key = Thm.eq_thm; |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
54 |
fun eq_data arg = eq_fst eq_key arg; |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
55 |
|
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
56 |
structure Data = Generic_Data |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
57 |
( |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
58 |
type T = simpset * (thm * entry) list; |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
59 |
val empty = (HOL_basic_ss, []); |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
60 |
val extend = I; |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
61 |
fun merge ((ss, e), (ss', e')) : T = |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
62 |
(merge_ss (ss, ss'), AList.merge eq_key (K true) (e, e')); |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
63 |
); |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
64 |
|
36703
6e870d7f32e5
removed former algebra presimpset from signature
haftmann
parents:
36702
diff
changeset
|
65 |
val get = snd o Data.get o Context.Proof; |
36700
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
66 |
|
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
67 |
|
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
68 |
(* match data *) |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
69 |
|
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
70 |
fun match ctxt tm = |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
71 |
let |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
72 |
fun match_inst |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
73 |
({vars, semiring = (sr_ops, sr_rules), |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
74 |
ring = (r_ops, r_rules), field = (f_ops, f_rules), idom, ideal}, |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
75 |
fns as {is_const, dest_const, mk_const, conv}) pat = |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
76 |
let |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
77 |
fun h instT = |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
78 |
let |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
79 |
val substT = Thm.instantiate (instT, []); |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
80 |
val substT_cterm = Drule.cterm_rule substT; |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
81 |
|
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
82 |
val vars' = map substT_cterm vars; |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
83 |
val semiring' = (map substT_cterm sr_ops, map substT sr_rules); |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
84 |
val ring' = (map substT_cterm r_ops, map substT r_rules); |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
85 |
val field' = (map substT_cterm f_ops, map substT f_rules); |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
86 |
val idom' = map substT idom; |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
87 |
val ideal' = map substT ideal; |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
88 |
|
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
89 |
val result = ({vars = vars', semiring = semiring', |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
90 |
ring = ring', field = field', idom = idom', ideal = ideal'}, fns); |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
91 |
in SOME result end |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
92 |
in (case try Thm.match (pat, tm) of |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
93 |
NONE => NONE |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
94 |
| SOME (instT, _) => h instT) |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
95 |
end; |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
96 |
|
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
97 |
fun match_struct (_, |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
98 |
entry as ({semiring = (sr_ops, _), ring = (r_ops, _), field = (f_ops, _), ...}, _): entry) = |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
99 |
get_first (match_inst entry) (sr_ops @ r_ops @ f_ops); |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
100 |
in get_first match_struct (snd (get ctxt)) end; |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
101 |
|
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
102 |
|
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
103 |
(* logical content *) |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
104 |
|
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
105 |
val semiringN = "semiring"; |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
106 |
val ringN = "ring"; |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
107 |
val idomN = "idom"; |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
108 |
val idealN = "ideal"; |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
109 |
val fieldN = "field"; |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
110 |
|
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
111 |
fun undefined _ = raise Match; |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
112 |
|
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
113 |
fun del_data key = apsnd (remove eq_data (key, [])); |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
114 |
|
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
115 |
val del = Thm.declaration_attribute (Data.map o del_data); |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
116 |
|
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
117 |
fun add {semiring = (sr_ops, sr_rules), ring = (r_ops, r_rules), |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
118 |
field = (f_ops, f_rules), idom, ideal} = |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
119 |
Thm.declaration_attribute (fn key => fn context => context |> Data.map |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
120 |
let |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
121 |
val ctxt = Context.proof_of context; |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
122 |
|
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
123 |
fun check kind name xs n = |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
124 |
null xs orelse length xs = n orelse |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
125 |
error ("Expected " ^ string_of_int n ^ " " ^ kind ^ " for " ^ name); |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
126 |
val check_ops = check "operations"; |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
127 |
val check_rules = check "rules"; |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
128 |
|
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
129 |
val _ = |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
130 |
check_ops semiringN sr_ops 5 andalso |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
131 |
check_rules semiringN sr_rules 37 andalso |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
132 |
check_ops ringN r_ops 2 andalso |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
133 |
check_rules ringN r_rules 2 andalso |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
134 |
check_ops fieldN f_ops 2 andalso |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
135 |
check_rules fieldN f_rules 2 andalso |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
136 |
check_rules idomN idom 2; |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
137 |
|
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
138 |
val mk_meta = Local_Defs.meta_rewrite_rule ctxt; |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
139 |
val sr_rules' = map mk_meta sr_rules; |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
140 |
val r_rules' = map mk_meta r_rules; |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
141 |
val f_rules' = map mk_meta f_rules; |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
142 |
|
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
143 |
fun rule i = nth sr_rules' (i - 1); |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
144 |
|
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
145 |
val (cx, cy) = Thm.dest_binop (hd sr_ops); |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
146 |
val cz = rule 34 |> Thm.rhs_of |> Thm.dest_arg |> Thm.dest_arg; |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
147 |
val cn = rule 36 |> Thm.rhs_of |> Thm.dest_arg |> Thm.dest_arg; |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
148 |
val ((clx, crx), (cly, cry)) = |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
149 |
rule 13 |> Thm.rhs_of |> Thm.dest_binop |> pairself Thm.dest_binop; |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
150 |
val ((ca, cb), (cc, cd)) = |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
151 |
rule 20 |> Thm.lhs_of |> Thm.dest_binop |> pairself Thm.dest_binop; |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
152 |
val cm = rule 1 |> Thm.rhs_of |> Thm.dest_arg; |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
153 |
val (cp, cq) = rule 26 |> Thm.lhs_of |> Thm.dest_binop |> pairself Thm.dest_arg; |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
154 |
|
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
155 |
val vars = [ca, cb, cc, cd, cm, cn, cp, cq, cx, cy, cz, clx, crx, cly, cry]; |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
156 |
val semiring = (sr_ops, sr_rules'); |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
157 |
val ring = (r_ops, r_rules'); |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
158 |
val field = (f_ops, f_rules'); |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
159 |
val ideal' = map (symmetric o mk_meta) ideal |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
160 |
in |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
161 |
del_data key #> |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
162 |
apsnd (cons (key, ({vars = vars, semiring = semiring, |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
163 |
ring = ring, field = field, idom = idom, ideal = ideal'}, |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
164 |
{is_const = undefined, dest_const = undefined, mk_const = undefined, |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
165 |
conv = undefined}))) |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
166 |
end); |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
167 |
|
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
168 |
|
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
169 |
(* extra-logical functions *) |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
170 |
|
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
171 |
fun funs raw_key {is_const, dest_const, mk_const, conv} phi = |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
172 |
(Data.map o apsnd) (fn data => |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
173 |
let |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
174 |
val key = Morphism.thm phi raw_key; |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
175 |
val _ = AList.defined eq_key data key orelse |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
176 |
raise THM ("No data entry for structure key", 0, [key]); |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
177 |
val fns = {is_const = is_const phi, dest_const = dest_const phi, |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
178 |
mk_const = mk_const phi, conv = conv phi}; |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
179 |
in AList.map_entry eq_key key (apsnd (K fns)) data end); |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
180 |
|
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
181 |
|
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
182 |
(* concrete syntax *) |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
183 |
|
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
184 |
local |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
185 |
|
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
186 |
fun keyword k = Scan.lift (Args.$$$ k -- Args.colon) >> K (); |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
187 |
fun keyword2 k1 k2 = Scan.lift (Args.$$$ k1 -- Args.$$$ k2 -- Args.colon) >> K (); |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
188 |
fun keyword3 k1 k2 k3 = |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
189 |
Scan.lift (Args.$$$ k1 -- Args.$$$ k2 -- Args.$$$ k3 -- Args.colon) >> K (); |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
190 |
|
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
191 |
val opsN = "ops"; |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
192 |
val rulesN = "rules"; |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
193 |
|
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
194 |
val normN = "norm"; |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
195 |
val constN = "const"; |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
196 |
val delN = "del"; |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
197 |
|
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
198 |
val any_keyword = |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
199 |
keyword2 semiringN opsN || keyword2 semiringN rulesN || |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
200 |
keyword2 ringN opsN || keyword2 ringN rulesN || |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
201 |
keyword2 fieldN opsN || keyword2 fieldN rulesN || |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
202 |
keyword2 idomN rulesN || keyword2 idealN rulesN; |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
203 |
|
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
204 |
val thms = Scan.repeat (Scan.unless any_keyword Attrib.multi_thm) >> flat; |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
205 |
val terms = thms >> map Drule.dest_term; |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
206 |
|
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
207 |
fun optional scan = Scan.optional scan []; |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
208 |
|
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
209 |
in |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
210 |
|
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
211 |
val normalizer_setup = |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
212 |
Attrib.setup @{binding normalizer} |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
213 |
(Scan.lift (Args.$$$ delN >> K del) || |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
214 |
((keyword2 semiringN opsN |-- terms) -- |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
215 |
(keyword2 semiringN rulesN |-- thms)) -- |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
216 |
(optional (keyword2 ringN opsN |-- terms) -- |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
217 |
optional (keyword2 ringN rulesN |-- thms)) -- |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
218 |
(optional (keyword2 fieldN opsN |-- terms) -- |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
219 |
optional (keyword2 fieldN rulesN |-- thms)) -- |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
220 |
optional (keyword2 idomN rulesN |-- thms) -- |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
221 |
optional (keyword2 idealN rulesN |-- thms) |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
222 |
>> (fn ((((sr, r), f), id), idl) => |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
223 |
add {semiring = sr, ring = r, field = f, idom = id, ideal = idl})) |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
224 |
"semiring normalizer data"; |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
225 |
|
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
226 |
end; |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
227 |
|
25253 | 228 |
open Conv; |
23252 | 229 |
|
230 |
(* Very basic stuff for terms *) |
|
25253 | 231 |
|
232 |
fun is_comb ct = |
|
233 |
(case Thm.term_of ct of |
|
234 |
_ $ _ => true |
|
235 |
| _ => false); |
|
236 |
||
237 |
val concl = Thm.cprop_of #> Thm.dest_arg; |
|
238 |
||
239 |
fun is_binop ct ct' = |
|
240 |
(case Thm.term_of ct' of |
|
241 |
c $ _ $ _ => term_of ct aconv c |
|
242 |
| _ => false); |
|
243 |
||
244 |
fun dest_binop ct ct' = |
|
245 |
if is_binop ct ct' then Thm.dest_binop ct' |
|
246 |
else raise CTERM ("dest_binop: bad binop", [ct, ct']) |
|
247 |
||
248 |
fun inst_thm inst = Thm.instantiate ([], inst); |
|
249 |
||
23252 | 250 |
val dest_numeral = term_of #> HOLogic.dest_number #> snd; |
251 |
val is_numeral = can dest_numeral; |
|
252 |
||
253 |
val numeral01_conv = Simplifier.rewrite |
|
25481 | 254 |
(HOL_basic_ss addsimps [@{thm numeral_1_eq_1}, @{thm numeral_0_eq_0}]); |
23252 | 255 |
val zero1_numeral_conv = |
25481 | 256 |
Simplifier.rewrite (HOL_basic_ss addsimps [@{thm numeral_1_eq_1} RS sym, @{thm numeral_0_eq_0} RS sym]); |
23580
998a6fda9bb6
moved mk_cnumeral/mk_cnumber to Tools/numeral.ML;
wenzelm
parents:
23559
diff
changeset
|
257 |
fun zerone_conv cv = zero1_numeral_conv then_conv cv then_conv numeral01_conv; |
23252 | 258 |
val natarith = [@{thm "add_nat_number_of"}, @{thm "diff_nat_number_of"}, |
259 |
@{thm "mult_nat_number_of"}, @{thm "eq_nat_number_of"}, |
|
260 |
@{thm "less_nat_number_of"}]; |
|
36700
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
261 |
|
23252 | 262 |
val nat_add_conv = |
263 |
zerone_conv |
|
264 |
(Simplifier.rewrite |
|
265 |
(HOL_basic_ss |
|
25481 | 266 |
addsimps @{thms arith_simps} @ natarith @ @{thms rel_simps} |
35410 | 267 |
@ [@{thm if_False}, @{thm if_True}, @{thm Nat.add_0}, @{thm add_Suc}, |
31790 | 268 |
@{thm add_number_of_left}, @{thm Suc_eq_plus1}] |
25481 | 269 |
@ map (fn th => th RS sym) @{thms numerals})); |
23252 | 270 |
|
271 |
val zeron_tm = @{cterm "0::nat"}; |
|
272 |
val onen_tm = @{cterm "1::nat"}; |
|
273 |
val true_tm = @{cterm "True"}; |
|
274 |
||
275 |
||
276 |
(* The main function! *) |
|
30866 | 277 |
fun semiring_normalizers_conv vars (sr_ops, sr_rules) (r_ops, r_rules) (f_ops, f_rules) |
23252 | 278 |
(is_semiring_constant, semiring_add_conv, semiring_mul_conv, semiring_pow_conv) = |
279 |
let |
|
280 |
||
281 |
val [pthm_02, pthm_03, pthm_04, pthm_05, pthm_07, pthm_08, |
|
282 |
pthm_09, pthm_10, pthm_11, pthm_12, pthm_13, pthm_14, pthm_15, pthm_16, |
|
283 |
pthm_17, pthm_18, pthm_19, pthm_21, pthm_22, pthm_23, pthm_24, |
|
284 |
pthm_25, pthm_26, pthm_27, pthm_28, pthm_29, pthm_30, pthm_31, pthm_32, |
|
285 |
pthm_33, pthm_34, pthm_35, pthm_36, pthm_37, pthm_38,pthm_39,pthm_40] = sr_rules; |
|
286 |
||
287 |
val [ca, cb, cc, cd, cm, cn, cp, cq, cx, cy, cz, clx, crx, cly, cry] = vars; |
|
288 |
val [add_pat, mul_pat, pow_pat, zero_tm, one_tm] = sr_ops; |
|
289 |
val [add_tm, mul_tm, pow_tm] = map (Thm.dest_fun o Thm.dest_fun) [add_pat, mul_pat, pow_pat]; |
|
290 |
||
291 |
val dest_add = dest_binop add_tm |
|
292 |
val dest_mul = dest_binop mul_tm |
|
293 |
fun dest_pow tm = |
|
294 |
let val (l,r) = dest_binop pow_tm tm |
|
295 |
in if is_numeral r then (l,r) else raise CTERM ("dest_pow",[tm]) |
|
296 |
end; |
|
297 |
val is_add = is_binop add_tm |
|
298 |
val is_mul = is_binop mul_tm |
|
299 |
fun is_pow tm = is_binop pow_tm tm andalso is_numeral(Thm.dest_arg tm); |
|
300 |
||
301 |
val (neg_mul,sub_add,sub_tm,neg_tm,dest_sub,is_sub,cx',cy') = |
|
302 |
(case (r_ops, r_rules) of |
|
30866 | 303 |
([sub_pat, neg_pat], [neg_mul, sub_add]) => |
23252 | 304 |
let |
305 |
val sub_tm = Thm.dest_fun (Thm.dest_fun sub_pat) |
|
306 |
val neg_tm = Thm.dest_fun neg_pat |
|
307 |
val dest_sub = dest_binop sub_tm |
|
308 |
val is_sub = is_binop sub_tm |
|
309 |
in (neg_mul,sub_add,sub_tm,neg_tm,dest_sub,is_sub, neg_mul |> concl |> Thm.dest_arg, |
|
310 |
sub_add |> concl |> Thm.dest_arg |> Thm.dest_arg) |
|
30866 | 311 |
end |
312 |
| _ => (TrueI, TrueI, true_tm, true_tm, (fn t => (t,t)), K false, true_tm, true_tm)); |
|
313 |
||
314 |
val (divide_inverse, inverse_divide, divide_tm, inverse_tm, is_divide) = |
|
315 |
(case (f_ops, f_rules) of |
|
316 |
([divide_pat, inverse_pat], [div_inv, inv_div]) => |
|
317 |
let val div_tm = funpow 2 Thm.dest_fun divide_pat |
|
318 |
val inv_tm = Thm.dest_fun inverse_pat |
|
319 |
in (div_inv, inv_div, div_tm, inv_tm, is_binop div_tm) |
|
320 |
end |
|
321 |
| _ => (TrueI, TrueI, true_tm, true_tm, K false)); |
|
322 |
||
23252 | 323 |
in fn variable_order => |
324 |
let |
|
325 |
||
326 |
(* Conversion for "x^n * x^m", with either x^n = x and/or x^m = x possible. *) |
|
327 |
(* Also deals with "const * const", but both terms must involve powers of *) |
|
328 |
(* the same variable, or both be constants, or behaviour may be incorrect. *) |
|
329 |
||
330 |
fun powvar_mul_conv tm = |
|
331 |
let |
|
332 |
val (l,r) = dest_mul tm |
|
333 |
in if is_semiring_constant l andalso is_semiring_constant r |
|
334 |
then semiring_mul_conv tm |
|
335 |
else |
|
336 |
((let |
|
337 |
val (lx,ln) = dest_pow l |
|
338 |
in |
|
339 |
((let val (rx,rn) = dest_pow r |
|
340 |
val th1 = inst_thm [(cx,lx),(cp,ln),(cq,rn)] pthm_29 |
|
341 |
val (tm1,tm2) = Thm.dest_comb(concl th1) in |
|
342 |
transitive th1 (Drule.arg_cong_rule tm1 (nat_add_conv tm2)) end) |
|
343 |
handle CTERM _ => |
|
344 |
(let val th1 = inst_thm [(cx,lx),(cq,ln)] pthm_31 |
|
345 |
val (tm1,tm2) = Thm.dest_comb(concl th1) in |
|
346 |
transitive th1 (Drule.arg_cong_rule tm1 (nat_add_conv tm2)) end)) end) |
|
347 |
handle CTERM _ => |
|
348 |
((let val (rx,rn) = dest_pow r |
|
349 |
val th1 = inst_thm [(cx,rx),(cq,rn)] pthm_30 |
|
350 |
val (tm1,tm2) = Thm.dest_comb(concl th1) in |
|
351 |
transitive th1 (Drule.arg_cong_rule tm1 (nat_add_conv tm2)) end) |
|
352 |
handle CTERM _ => inst_thm [(cx,l)] pthm_32 |
|
353 |
||
354 |
)) |
|
355 |
end; |
|
356 |
||
357 |
(* Remove "1 * m" from a monomial, and just leave m. *) |
|
358 |
||
359 |
fun monomial_deone th = |
|
360 |
(let val (l,r) = dest_mul(concl th) in |
|
361 |
if l aconvc one_tm |
|
362 |
then transitive th (inst_thm [(ca,r)] pthm_13) else th end) |
|
363 |
handle CTERM _ => th; |
|
364 |
||
365 |
(* Conversion for "(monomial)^n", where n is a numeral. *) |
|
366 |
||
367 |
val monomial_pow_conv = |
|
368 |
let |
|
369 |
fun monomial_pow tm bod ntm = |
|
370 |
if not(is_comb bod) |
|
371 |
then reflexive tm |
|
372 |
else |
|
373 |
if is_semiring_constant bod |
|
374 |
then semiring_pow_conv tm |
|
375 |
else |
|
376 |
let |
|
377 |
val (lopr,r) = Thm.dest_comb bod |
|
378 |
in if not(is_comb lopr) |
|
379 |
then reflexive tm |
|
380 |
else |
|
381 |
let |
|
382 |
val (opr,l) = Thm.dest_comb lopr |
|
383 |
in |
|
384 |
if opr aconvc pow_tm andalso is_numeral r |
|
385 |
then |
|
386 |
let val th1 = inst_thm [(cx,l),(cp,r),(cq,ntm)] pthm_34 |
|
387 |
val (l,r) = Thm.dest_comb(concl th1) |
|
36700
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
388 |
in transitive th1 (Drule.arg_cong_rule l (nat_add_conv r)) |
23252 | 389 |
end |
390 |
else |
|
391 |
if opr aconvc mul_tm |
|
392 |
then |
|
393 |
let |
|
394 |
val th1 = inst_thm [(cx,l),(cy,r),(cq,ntm)] pthm_33 |
|
395 |
val (xy,z) = Thm.dest_comb(concl th1) |
|
396 |
val (x,y) = Thm.dest_comb xy |
|
397 |
val thl = monomial_pow y l ntm |
|
398 |
val thr = monomial_pow z r ntm |
|
399 |
in transitive th1 (combination (Drule.arg_cong_rule x thl) thr) |
|
400 |
end |
|
401 |
else reflexive tm |
|
402 |
end |
|
403 |
end |
|
404 |
in fn tm => |
|
405 |
let |
|
406 |
val (lopr,r) = Thm.dest_comb tm |
|
407 |
val (opr,l) = Thm.dest_comb lopr |
|
408 |
in if not (opr aconvc pow_tm) orelse not(is_numeral r) |
|
409 |
then raise CTERM ("monomial_pow_conv", [tm]) |
|
410 |
else if r aconvc zeron_tm |
|
411 |
then inst_thm [(cx,l)] pthm_35 |
|
412 |
else if r aconvc onen_tm |
|
413 |
then inst_thm [(cx,l)] pthm_36 |
|
414 |
else monomial_deone(monomial_pow tm l r) |
|
415 |
end |
|
416 |
end; |
|
417 |
||
418 |
(* Multiplication of canonical monomials. *) |
|
419 |
val monomial_mul_conv = |
|
420 |
let |
|
421 |
fun powvar tm = |
|
422 |
if is_semiring_constant tm then one_tm |
|
423 |
else |
|
424 |
((let val (lopr,r) = Thm.dest_comb tm |
|
425 |
val (opr,l) = Thm.dest_comb lopr |
|
426 |
in if opr aconvc pow_tm andalso is_numeral r then l |
|
427 |
else raise CTERM ("monomial_mul_conv",[tm]) end) |
|
428 |
handle CTERM _ => tm) (* FIXME !? *) |
|
429 |
fun vorder x y = |
|
430 |
if x aconvc y then 0 |
|
431 |
else |
|
432 |
if x aconvc one_tm then ~1 |
|
433 |
else if y aconvc one_tm then 1 |
|
434 |
else if variable_order x y then ~1 else 1 |
|
435 |
fun monomial_mul tm l r = |
|
436 |
((let val (lx,ly) = dest_mul l val vl = powvar lx |
|
437 |
in |
|
438 |
((let |
|
439 |
val (rx,ry) = dest_mul r |
|
440 |
val vr = powvar rx |
|
441 |
val ord = vorder vl vr |
|
442 |
in |
|
443 |
if ord = 0 |
|
444 |
then |
|
445 |
let |
|
446 |
val th1 = inst_thm [(clx,lx),(cly,ly),(crx,rx),(cry,ry)] pthm_15 |
|
447 |
val (tm1,tm2) = Thm.dest_comb(concl th1) |
|
448 |
val (tm3,tm4) = Thm.dest_comb tm1 |
|
449 |
val th2 = Drule.fun_cong_rule (Drule.arg_cong_rule tm3 (powvar_mul_conv tm4)) tm2 |
|
450 |
val th3 = transitive th1 th2 |
|
451 |
val (tm5,tm6) = Thm.dest_comb(concl th3) |
|
452 |
val (tm7,tm8) = Thm.dest_comb tm6 |
|
453 |
val th4 = monomial_mul tm6 (Thm.dest_arg tm7) tm8 |
|
454 |
in transitive th3 (Drule.arg_cong_rule tm5 th4) |
|
455 |
end |
|
456 |
else |
|
457 |
let val th0 = if ord < 0 then pthm_16 else pthm_17 |
|
458 |
val th1 = inst_thm [(clx,lx),(cly,ly),(crx,rx),(cry,ry)] th0 |
|
459 |
val (tm1,tm2) = Thm.dest_comb(concl th1) |
|
460 |
val (tm3,tm4) = Thm.dest_comb tm2 |
|
461 |
in transitive th1 (Drule.arg_cong_rule tm1 (monomial_mul tm2 (Thm.dest_arg tm3) tm4)) |
|
462 |
end |
|
463 |
end) |
|
464 |
handle CTERM _ => |
|
465 |
(let val vr = powvar r val ord = vorder vl vr |
|
466 |
in |
|
467 |
if ord = 0 then |
|
468 |
let |
|
469 |
val th1 = inst_thm [(clx,lx),(cly,ly),(crx,r)] pthm_18 |
|
470 |
val (tm1,tm2) = Thm.dest_comb(concl th1) |
|
471 |
val (tm3,tm4) = Thm.dest_comb tm1 |
|
472 |
val th2 = Drule.fun_cong_rule (Drule.arg_cong_rule tm3 (powvar_mul_conv tm4)) tm2 |
|
473 |
in transitive th1 th2 |
|
474 |
end |
|
475 |
else |
|
476 |
if ord < 0 then |
|
477 |
let val th1 = inst_thm [(clx,lx),(cly,ly),(crx,r)] pthm_19 |
|
478 |
val (tm1,tm2) = Thm.dest_comb(concl th1) |
|
479 |
val (tm3,tm4) = Thm.dest_comb tm2 |
|
480 |
in transitive th1 (Drule.arg_cong_rule tm1 (monomial_mul tm2 (Thm.dest_arg tm3) tm4)) |
|
481 |
end |
|
482 |
else inst_thm [(ca,l),(cb,r)] pthm_09 |
|
483 |
end)) end) |
|
484 |
handle CTERM _ => |
|
485 |
(let val vl = powvar l in |
|
486 |
((let |
|
487 |
val (rx,ry) = dest_mul r |
|
488 |
val vr = powvar rx |
|
489 |
val ord = vorder vl vr |
|
490 |
in if ord = 0 then |
|
491 |
let val th1 = inst_thm [(clx,l),(crx,rx),(cry,ry)] pthm_21 |
|
492 |
val (tm1,tm2) = Thm.dest_comb(concl th1) |
|
493 |
val (tm3,tm4) = Thm.dest_comb tm1 |
|
494 |
in transitive th1 (Drule.fun_cong_rule (Drule.arg_cong_rule tm3 (powvar_mul_conv tm4)) tm2) |
|
495 |
end |
|
496 |
else if ord > 0 then |
|
497 |
let val th1 = inst_thm [(clx,l),(crx,rx),(cry,ry)] pthm_22 |
|
498 |
val (tm1,tm2) = Thm.dest_comb(concl th1) |
|
499 |
val (tm3,tm4) = Thm.dest_comb tm2 |
|
500 |
in transitive th1 (Drule.arg_cong_rule tm1 (monomial_mul tm2 (Thm.dest_arg tm3) tm4)) |
|
501 |
end |
|
502 |
else reflexive tm |
|
503 |
end) |
|
504 |
handle CTERM _ => |
|
505 |
(let val vr = powvar r |
|
506 |
val ord = vorder vl vr |
|
507 |
in if ord = 0 then powvar_mul_conv tm |
|
508 |
else if ord > 0 then inst_thm [(ca,l),(cb,r)] pthm_09 |
|
509 |
else reflexive tm |
|
510 |
end)) end)) |
|
511 |
in fn tm => let val (l,r) = dest_mul tm in monomial_deone(monomial_mul tm l r) |
|
512 |
end |
|
513 |
end; |
|
514 |
(* Multiplication by monomial of a polynomial. *) |
|
515 |
||
516 |
val polynomial_monomial_mul_conv = |
|
517 |
let |
|
518 |
fun pmm_conv tm = |
|
519 |
let val (l,r) = dest_mul tm |
|
520 |
in |
|
521 |
((let val (y,z) = dest_add r |
|
522 |
val th1 = inst_thm [(cx,l),(cy,y),(cz,z)] pthm_37 |
|
523 |
val (tm1,tm2) = Thm.dest_comb(concl th1) |
|
524 |
val (tm3,tm4) = Thm.dest_comb tm1 |
|
525 |
val th2 = combination (Drule.arg_cong_rule tm3 (monomial_mul_conv tm4)) (pmm_conv tm2) |
|
526 |
in transitive th1 th2 |
|
527 |
end) |
|
528 |
handle CTERM _ => monomial_mul_conv tm) |
|
529 |
end |
|
530 |
in pmm_conv |
|
531 |
end; |
|
532 |
||
533 |
(* Addition of two monomials identical except for constant multiples. *) |
|
534 |
||
535 |
fun monomial_add_conv tm = |
|
536 |
let val (l,r) = dest_add tm |
|
537 |
in if is_semiring_constant l andalso is_semiring_constant r |
|
538 |
then semiring_add_conv tm |
|
539 |
else |
|
540 |
let val th1 = |
|
541 |
if is_mul l andalso is_semiring_constant(Thm.dest_arg1 l) |
|
542 |
then if is_mul r andalso is_semiring_constant(Thm.dest_arg1 r) then |
|
543 |
inst_thm [(ca,Thm.dest_arg1 l),(cm,Thm.dest_arg r), (cb,Thm.dest_arg1 r)] pthm_02 |
|
544 |
else inst_thm [(ca,Thm.dest_arg1 l),(cm,r)] pthm_03 |
|
545 |
else if is_mul r andalso is_semiring_constant(Thm.dest_arg1 r) |
|
546 |
then inst_thm [(cm,l),(ca,Thm.dest_arg1 r)] pthm_04 |
|
547 |
else inst_thm [(cm,r)] pthm_05 |
|
548 |
val (tm1,tm2) = Thm.dest_comb(concl th1) |
|
549 |
val (tm3,tm4) = Thm.dest_comb tm1 |
|
550 |
val th2 = Drule.arg_cong_rule tm3 (semiring_add_conv tm4) |
|
551 |
val th3 = transitive th1 (Drule.fun_cong_rule th2 tm2) |
|
552 |
val tm5 = concl th3 |
|
553 |
in |
|
554 |
if (Thm.dest_arg1 tm5) aconvc zero_tm |
|
555 |
then transitive th3 (inst_thm [(ca,Thm.dest_arg tm5)] pthm_11) |
|
556 |
else monomial_deone th3 |
|
557 |
end |
|
558 |
end; |
|
559 |
||
560 |
(* Ordering on monomials. *) |
|
561 |
||
562 |
fun striplist dest = |
|
563 |
let fun strip x acc = |
|
564 |
((let val (l,r) = dest x in |
|
565 |
strip l (strip r acc) end) |
|
566 |
handle CTERM _ => x::acc) (* FIXME !? *) |
|
567 |
in fn x => strip x [] |
|
568 |
end; |
|
569 |
||
570 |
||
571 |
fun powervars tm = |
|
572 |
let val ptms = striplist dest_mul tm |
|
573 |
in if is_semiring_constant (hd ptms) then tl ptms else ptms |
|
574 |
end; |
|
575 |
val num_0 = 0; |
|
576 |
val num_1 = 1; |
|
577 |
fun dest_varpow tm = |
|
578 |
((let val (x,n) = dest_pow tm in (x,dest_numeral n) end) |
|
579 |
handle CTERM _ => |
|
580 |
(tm,(if is_semiring_constant tm then num_0 else num_1))); |
|
581 |
||
582 |
val morder = |
|
583 |
let fun lexorder l1 l2 = |
|
584 |
case (l1,l2) of |
|
585 |
([],[]) => 0 |
|
586 |
| (vps,[]) => ~1 |
|
587 |
| ([],vps) => 1 |
|
588 |
| (((x1,n1)::vs1),((x2,n2)::vs2)) => |
|
589 |
if variable_order x1 x2 then 1 |
|
590 |
else if variable_order x2 x1 then ~1 |
|
591 |
else if n1 < n2 then ~1 |
|
592 |
else if n2 < n1 then 1 |
|
593 |
else lexorder vs1 vs2 |
|
594 |
in fn tm1 => fn tm2 => |
|
595 |
let val vdegs1 = map dest_varpow (powervars tm1) |
|
596 |
val vdegs2 = map dest_varpow (powervars tm2) |
|
33002 | 597 |
val deg1 = fold (Integer.add o snd) vdegs1 num_0 |
598 |
val deg2 = fold (Integer.add o snd) vdegs2 num_0 |
|
23252 | 599 |
in if deg1 < deg2 then ~1 else if deg1 > deg2 then 1 |
600 |
else lexorder vdegs1 vdegs2 |
|
601 |
end |
|
602 |
end; |
|
603 |
||
604 |
(* Addition of two polynomials. *) |
|
605 |
||
606 |
val polynomial_add_conv = |
|
607 |
let |
|
608 |
fun dezero_rule th = |
|
609 |
let |
|
610 |
val tm = concl th |
|
611 |
in |
|
612 |
if not(is_add tm) then th else |
|
613 |
let val (lopr,r) = Thm.dest_comb tm |
|
614 |
val l = Thm.dest_arg lopr |
|
615 |
in |
|
616 |
if l aconvc zero_tm |
|
617 |
then transitive th (inst_thm [(ca,r)] pthm_07) else |
|
618 |
if r aconvc zero_tm |
|
619 |
then transitive th (inst_thm [(ca,l)] pthm_08) else th |
|
620 |
end |
|
621 |
end |
|
622 |
fun padd tm = |
|
623 |
let |
|
624 |
val (l,r) = dest_add tm |
|
625 |
in |
|
626 |
if l aconvc zero_tm then inst_thm [(ca,r)] pthm_07 |
|
627 |
else if r aconvc zero_tm then inst_thm [(ca,l)] pthm_08 |
|
628 |
else |
|
629 |
if is_add l |
|
630 |
then |
|
631 |
let val (a,b) = dest_add l |
|
632 |
in |
|
633 |
if is_add r then |
|
634 |
let val (c,d) = dest_add r |
|
635 |
val ord = morder a c |
|
636 |
in |
|
637 |
if ord = 0 then |
|
638 |
let val th1 = inst_thm [(ca,a),(cb,b),(cc,c),(cd,d)] pthm_23 |
|
639 |
val (tm1,tm2) = Thm.dest_comb(concl th1) |
|
640 |
val (tm3,tm4) = Thm.dest_comb tm1 |
|
641 |
val th2 = Drule.arg_cong_rule tm3 (monomial_add_conv tm4) |
|
642 |
in dezero_rule (transitive th1 (combination th2 (padd tm2))) |
|
643 |
end |
|
644 |
else (* ord <> 0*) |
|
645 |
let val th1 = |
|
646 |
if ord > 0 then inst_thm [(ca,a),(cb,b),(cc,r)] pthm_24 |
|
647 |
else inst_thm [(ca,l),(cc,c),(cd,d)] pthm_25 |
|
648 |
val (tm1,tm2) = Thm.dest_comb(concl th1) |
|
649 |
in dezero_rule (transitive th1 (Drule.arg_cong_rule tm1 (padd tm2))) |
|
650 |
end |
|
651 |
end |
|
652 |
else (* not (is_add r)*) |
|
653 |
let val ord = morder a r |
|
654 |
in |
|
655 |
if ord = 0 then |
|
656 |
let val th1 = inst_thm [(ca,a),(cb,b),(cc,r)] pthm_26 |
|
657 |
val (tm1,tm2) = Thm.dest_comb(concl th1) |
|
658 |
val (tm3,tm4) = Thm.dest_comb tm1 |
|
659 |
val th2 = Drule.fun_cong_rule (Drule.arg_cong_rule tm3 (monomial_add_conv tm4)) tm2 |
|
660 |
in dezero_rule (transitive th1 th2) |
|
661 |
end |
|
662 |
else (* ord <> 0*) |
|
663 |
if ord > 0 then |
|
664 |
let val th1 = inst_thm [(ca,a),(cb,b),(cc,r)] pthm_24 |
|
665 |
val (tm1,tm2) = Thm.dest_comb(concl th1) |
|
666 |
in dezero_rule (transitive th1 (Drule.arg_cong_rule tm1 (padd tm2))) |
|
667 |
end |
|
668 |
else dezero_rule (inst_thm [(ca,l),(cc,r)] pthm_27) |
|
669 |
end |
|
670 |
end |
|
671 |
else (* not (is_add l)*) |
|
672 |
if is_add r then |
|
673 |
let val (c,d) = dest_add r |
|
674 |
val ord = morder l c |
|
675 |
in |
|
676 |
if ord = 0 then |
|
677 |
let val th1 = inst_thm [(ca,l),(cc,c),(cd,d)] pthm_28 |
|
678 |
val (tm1,tm2) = Thm.dest_comb(concl th1) |
|
679 |
val (tm3,tm4) = Thm.dest_comb tm1 |
|
680 |
val th2 = Drule.fun_cong_rule (Drule.arg_cong_rule tm3 (monomial_add_conv tm4)) tm2 |
|
681 |
in dezero_rule (transitive th1 th2) |
|
682 |
end |
|
683 |
else |
|
684 |
if ord > 0 then reflexive tm |
|
685 |
else |
|
686 |
let val th1 = inst_thm [(ca,l),(cc,c),(cd,d)] pthm_25 |
|
687 |
val (tm1,tm2) = Thm.dest_comb(concl th1) |
|
688 |
in dezero_rule (transitive th1 (Drule.arg_cong_rule tm1 (padd tm2))) |
|
689 |
end |
|
690 |
end |
|
691 |
else |
|
692 |
let val ord = morder l r |
|
693 |
in |
|
694 |
if ord = 0 then monomial_add_conv tm |
|
695 |
else if ord > 0 then dezero_rule(reflexive tm) |
|
696 |
else dezero_rule (inst_thm [(ca,l),(cc,r)] pthm_27) |
|
697 |
end |
|
698 |
end |
|
699 |
in padd |
|
700 |
end; |
|
701 |
||
702 |
(* Multiplication of two polynomials. *) |
|
703 |
||
704 |
val polynomial_mul_conv = |
|
705 |
let |
|
706 |
fun pmul tm = |
|
707 |
let val (l,r) = dest_mul tm |
|
708 |
in |
|
709 |
if not(is_add l) then polynomial_monomial_mul_conv tm |
|
710 |
else |
|
711 |
if not(is_add r) then |
|
712 |
let val th1 = inst_thm [(ca,l),(cb,r)] pthm_09 |
|
713 |
in transitive th1 (polynomial_monomial_mul_conv(concl th1)) |
|
714 |
end |
|
715 |
else |
|
716 |
let val (a,b) = dest_add l |
|
717 |
val th1 = inst_thm [(ca,a),(cb,b),(cc,r)] pthm_10 |
|
718 |
val (tm1,tm2) = Thm.dest_comb(concl th1) |
|
719 |
val (tm3,tm4) = Thm.dest_comb tm1 |
|
720 |
val th2 = Drule.arg_cong_rule tm3 (polynomial_monomial_mul_conv tm4) |
|
721 |
val th3 = transitive th1 (combination th2 (pmul tm2)) |
|
722 |
in transitive th3 (polynomial_add_conv (concl th3)) |
|
723 |
end |
|
724 |
end |
|
725 |
in fn tm => |
|
726 |
let val (l,r) = dest_mul tm |
|
727 |
in |
|
728 |
if l aconvc zero_tm then inst_thm [(ca,r)] pthm_11 |
|
729 |
else if r aconvc zero_tm then inst_thm [(ca,l)] pthm_12 |
|
730 |
else if l aconvc one_tm then inst_thm [(ca,r)] pthm_13 |
|
731 |
else if r aconvc one_tm then inst_thm [(ca,l)] pthm_14 |
|
732 |
else pmul tm |
|
733 |
end |
|
734 |
end; |
|
735 |
||
736 |
(* Power of polynomial (optimized for the monomial and trivial cases). *) |
|
737 |
||
23580
998a6fda9bb6
moved mk_cnumeral/mk_cnumber to Tools/numeral.ML;
wenzelm
parents:
23559
diff
changeset
|
738 |
fun num_conv n = |
998a6fda9bb6
moved mk_cnumeral/mk_cnumber to Tools/numeral.ML;
wenzelm
parents:
23559
diff
changeset
|
739 |
nat_add_conv (Thm.capply @{cterm Suc} (Numeral.mk_cnumber @{ctyp nat} (dest_numeral n - 1))) |
998a6fda9bb6
moved mk_cnumeral/mk_cnumber to Tools/numeral.ML;
wenzelm
parents:
23559
diff
changeset
|
740 |
|> Thm.symmetric; |
23252 | 741 |
|
742 |
||
743 |
val polynomial_pow_conv = |
|
744 |
let |
|
745 |
fun ppow tm = |
|
746 |
let val (l,n) = dest_pow tm |
|
747 |
in |
|
748 |
if n aconvc zeron_tm then inst_thm [(cx,l)] pthm_35 |
|
749 |
else if n aconvc onen_tm then inst_thm [(cx,l)] pthm_36 |
|
750 |
else |
|
751 |
let val th1 = num_conv n |
|
752 |
val th2 = inst_thm [(cx,l),(cq,Thm.dest_arg (concl th1))] pthm_38 |
|
753 |
val (tm1,tm2) = Thm.dest_comb(concl th2) |
|
754 |
val th3 = transitive th2 (Drule.arg_cong_rule tm1 (ppow tm2)) |
|
755 |
val th4 = transitive (Drule.arg_cong_rule (Thm.dest_fun tm) th1) th3 |
|
756 |
in transitive th4 (polynomial_mul_conv (concl th4)) |
|
757 |
end |
|
758 |
end |
|
759 |
in fn tm => |
|
760 |
if is_add(Thm.dest_arg1 tm) then ppow tm else monomial_pow_conv tm |
|
761 |
end; |
|
762 |
||
763 |
(* Negation. *) |
|
764 |
||
23580
998a6fda9bb6
moved mk_cnumeral/mk_cnumber to Tools/numeral.ML;
wenzelm
parents:
23559
diff
changeset
|
765 |
fun polynomial_neg_conv tm = |
23252 | 766 |
let val (l,r) = Thm.dest_comb tm in |
767 |
if not (l aconvc neg_tm) then raise CTERM ("polynomial_neg_conv",[tm]) else |
|
768 |
let val th1 = inst_thm [(cx',r)] neg_mul |
|
769 |
val th2 = transitive th1 (arg1_conv semiring_mul_conv (concl th1)) |
|
770 |
in transitive th2 (polynomial_monomial_mul_conv (concl th2)) |
|
771 |
end |
|
772 |
end; |
|
773 |
||
774 |
||
775 |
(* Subtraction. *) |
|
23580
998a6fda9bb6
moved mk_cnumeral/mk_cnumber to Tools/numeral.ML;
wenzelm
parents:
23559
diff
changeset
|
776 |
fun polynomial_sub_conv tm = |
23252 | 777 |
let val (l,r) = dest_sub tm |
778 |
val th1 = inst_thm [(cx',l),(cy',r)] sub_add |
|
779 |
val (tm1,tm2) = Thm.dest_comb(concl th1) |
|
780 |
val th2 = Drule.arg_cong_rule tm1 (polynomial_neg_conv tm2) |
|
781 |
in transitive th1 (transitive th2 (polynomial_add_conv (concl th2))) |
|
782 |
end; |
|
783 |
||
784 |
(* Conversion from HOL term. *) |
|
785 |
||
786 |
fun polynomial_conv tm = |
|
23407
0e4452fcbeb8
normalizer conversions depend on the proof context; added functions for conversion and wrapper that sill depend on the variable ordering (_ord)
chaieb
parents:
23330
diff
changeset
|
787 |
if is_semiring_constant tm then semiring_add_conv tm |
0e4452fcbeb8
normalizer conversions depend on the proof context; added functions for conversion and wrapper that sill depend on the variable ordering (_ord)
chaieb
parents:
23330
diff
changeset
|
788 |
else if not(is_comb tm) then reflexive tm |
23252 | 789 |
else |
790 |
let val (lopr,r) = Thm.dest_comb tm |
|
791 |
in if lopr aconvc neg_tm then |
|
792 |
let val th1 = Drule.arg_cong_rule lopr (polynomial_conv r) |
|
793 |
in transitive th1 (polynomial_neg_conv (concl th1)) |
|
794 |
end |
|
30866 | 795 |
else if lopr aconvc inverse_tm then |
796 |
let val th1 = Drule.arg_cong_rule lopr (polynomial_conv r) |
|
797 |
in transitive th1 (semiring_mul_conv (concl th1)) |
|
798 |
end |
|
23252 | 799 |
else |
800 |
if not(is_comb lopr) then reflexive tm |
|
801 |
else |
|
802 |
let val (opr,l) = Thm.dest_comb lopr |
|
803 |
in if opr aconvc pow_tm andalso is_numeral r |
|
804 |
then |
|
805 |
let val th1 = Drule.fun_cong_rule (Drule.arg_cong_rule opr (polynomial_conv l)) r |
|
806 |
in transitive th1 (polynomial_pow_conv (concl th1)) |
|
807 |
end |
|
30866 | 808 |
else if opr aconvc divide_tm |
809 |
then |
|
810 |
let val th1 = combination (Drule.arg_cong_rule opr (polynomial_conv l)) |
|
811 |
(polynomial_conv r) |
|
812 |
val th2 = (rewr_conv divide_inverse then_conv polynomial_mul_conv) |
|
813 |
(Thm.rhs_of th1) |
|
814 |
in transitive th1 th2 |
|
815 |
end |
|
23252 | 816 |
else |
817 |
if opr aconvc add_tm orelse opr aconvc mul_tm orelse opr aconvc sub_tm |
|
818 |
then |
|
819 |
let val th1 = combination (Drule.arg_cong_rule opr (polynomial_conv l)) (polynomial_conv r) |
|
820 |
val f = if opr aconvc add_tm then polynomial_add_conv |
|
821 |
else if opr aconvc mul_tm then polynomial_mul_conv |
|
822 |
else polynomial_sub_conv |
|
823 |
in transitive th1 (f (concl th1)) |
|
824 |
end |
|
825 |
else reflexive tm |
|
826 |
end |
|
827 |
end; |
|
828 |
in |
|
829 |
{main = polynomial_conv, |
|
830 |
add = polynomial_add_conv, |
|
831 |
mul = polynomial_mul_conv, |
|
832 |
pow = polynomial_pow_conv, |
|
833 |
neg = polynomial_neg_conv, |
|
834 |
sub = polynomial_sub_conv} |
|
835 |
end |
|
836 |
end; |
|
837 |
||
35410 | 838 |
val nat_exp_ss = |
839 |
HOL_basic_ss addsimps (@{thms nat_number} @ @{thms nat_arith} @ @{thms arith_simps} @ @{thms rel_simps}) |
|
840 |
addsimps [@{thm Let_def}, @{thm if_False}, @{thm if_True}, @{thm Nat.add_0}, @{thm add_Suc}]; |
|
23252 | 841 |
|
35408 | 842 |
fun simple_cterm_ord t u = Term_Ord.term_ord (term_of t, term_of u) = LESS; |
27222 | 843 |
|
30866 | 844 |
fun semiring_normalizers_ord_wrapper ctxt ({vars, semiring, ring, field, idom, ideal}, |
23407
0e4452fcbeb8
normalizer conversions depend on the proof context; added functions for conversion and wrapper that sill depend on the variable ordering (_ord)
chaieb
parents:
23330
diff
changeset
|
845 |
{conv, dest_const, mk_const, is_const}) ord = |
23252 | 846 |
let |
847 |
val pow_conv = |
|
848 |
arg_conv (Simplifier.rewrite nat_exp_ss) |
|
849 |
then_conv Simplifier.rewrite |
|
850 |
(HOL_basic_ss addsimps [nth (snd semiring) 31, nth (snd semiring) 34]) |
|
23330
01c09922ce59
Conversion for computation on constants now depends on the context
chaieb
parents:
23259
diff
changeset
|
851 |
then_conv conv ctxt |
01c09922ce59
Conversion for computation on constants now depends on the context
chaieb
parents:
23259
diff
changeset
|
852 |
val dat = (is_const, conv ctxt, conv ctxt, pow_conv) |
30866 | 853 |
in semiring_normalizers_conv vars semiring ring field dat ord end; |
27222 | 854 |
|
30866 | 855 |
fun semiring_normalize_ord_wrapper ctxt ({vars, semiring, ring, field, idom, ideal}, {conv, dest_const, mk_const, is_const}) ord = |
856 |
#main (semiring_normalizers_ord_wrapper ctxt ({vars = vars, semiring = semiring, ring = ring, field = field, idom = idom, ideal = ideal},{conv = conv, dest_const = dest_const, mk_const = mk_const, is_const = is_const}) ord); |
|
23252 | 857 |
|
23407
0e4452fcbeb8
normalizer conversions depend on the proof context; added functions for conversion and wrapper that sill depend on the variable ordering (_ord)
chaieb
parents:
23330
diff
changeset
|
858 |
fun semiring_normalize_wrapper ctxt data = |
0e4452fcbeb8
normalizer conversions depend on the proof context; added functions for conversion and wrapper that sill depend on the variable ordering (_ord)
chaieb
parents:
23330
diff
changeset
|
859 |
semiring_normalize_ord_wrapper ctxt data simple_cterm_ord; |
0e4452fcbeb8
normalizer conversions depend on the proof context; added functions for conversion and wrapper that sill depend on the variable ordering (_ord)
chaieb
parents:
23330
diff
changeset
|
860 |
|
0e4452fcbeb8
normalizer conversions depend on the proof context; added functions for conversion and wrapper that sill depend on the variable ordering (_ord)
chaieb
parents:
23330
diff
changeset
|
861 |
fun semiring_normalize_ord_conv ctxt ord tm = |
36700
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
862 |
(case match ctxt tm of |
23252 | 863 |
NONE => reflexive tm |
23407
0e4452fcbeb8
normalizer conversions depend on the proof context; added functions for conversion and wrapper that sill depend on the variable ordering (_ord)
chaieb
parents:
23330
diff
changeset
|
864 |
| SOME res => semiring_normalize_ord_wrapper ctxt res ord tm); |
0e4452fcbeb8
normalizer conversions depend on the proof context; added functions for conversion and wrapper that sill depend on the variable ordering (_ord)
chaieb
parents:
23330
diff
changeset
|
865 |
|
0e4452fcbeb8
normalizer conversions depend on the proof context; added functions for conversion and wrapper that sill depend on the variable ordering (_ord)
chaieb
parents:
23330
diff
changeset
|
866 |
fun semiring_normalize_conv ctxt = semiring_normalize_ord_conv ctxt simple_cterm_ord; |
23252 | 867 |
|
36700
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
868 |
(* theory setup *) |
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
869 |
|
36702
b455ebd63799
moved presimplification rules for algebraic methods into named thms functor
haftmann
parents:
36700
diff
changeset
|
870 |
val setup = normalizer_setup |
36700
9b85b9d74b83
dropped auxiliary method sring_norm; integrated normalizer.ML and normalizer_data.ML
haftmann
parents:
35410
diff
changeset
|
871 |
|
23252 | 872 |
end; |