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