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