src/HOL/Tools/Groebner_Basis/normalizer_data.ML
changeset 30866 dd5117e2d73e
parent 30722 623d4831c8cf
child 33519 e31a85f92ce9
equal deleted inserted replaced
30865:5106e13d400f 30866:dd5117e2d73e
     9 sig
     9 sig
    10   type entry
    10   type entry
    11   val get: Proof.context -> simpset * (thm * entry) list
    11   val get: Proof.context -> simpset * (thm * entry) list
    12   val match: Proof.context -> cterm -> entry option
    12   val match: Proof.context -> cterm -> entry option
    13   val del: attribute
    13   val del: attribute
    14   val add: {semiring: cterm list * thm list, ring: cterm list * thm list, idom: thm list, ideal: thm list}
    14   val add: {semiring: cterm list * thm list, ring: cterm list * thm list, field: cterm list * thm list, idom: thm list, ideal: thm list}
    15     -> attribute
    15     -> attribute
    16   val funs: thm -> {is_const: morphism -> cterm -> bool,
    16   val funs: thm -> {is_const: morphism -> cterm -> bool,
    17     dest_const: morphism -> cterm -> Rat.rat,
    17     dest_const: morphism -> cterm -> Rat.rat,
    18     mk_const: morphism -> ctyp -> Rat.rat -> cterm,
    18     mk_const: morphism -> ctyp -> Rat.rat -> cterm,
    19     conv: morphism -> Proof.context -> cterm -> thm} -> declaration
    19     conv: morphism -> Proof.context -> cterm -> thm} -> declaration
    27 
    27 
    28 type entry =
    28 type entry =
    29  {vars: cterm list,
    29  {vars: cterm list,
    30   semiring: cterm list * thm list,
    30   semiring: cterm list * thm list,
    31   ring: cterm list * thm list,
    31   ring: cterm list * thm list,
       
    32   field: cterm list * thm list,
    32   idom: thm list,
    33   idom: thm list,
    33   ideal: thm list} *
    34   ideal: thm list} *
    34  {is_const: cterm -> bool,
    35  {is_const: cterm -> bool,
    35   dest_const: cterm -> Rat.rat,
    36   dest_const: cterm -> Rat.rat,
    36   mk_const: ctyp -> Rat.rat -> cterm,
    37   mk_const: ctyp -> Rat.rat -> cterm,
    55 
    56 
    56 fun match ctxt tm =
    57 fun match ctxt tm =
    57   let
    58   let
    58     fun match_inst
    59     fun match_inst
    59         ({vars, semiring = (sr_ops, sr_rules), 
    60         ({vars, semiring = (sr_ops, sr_rules), 
    60           ring = (r_ops, r_rules), idom, ideal},
    61           ring = (r_ops, r_rules), field = (f_ops, f_rules), idom, ideal},
    61          fns as {is_const, dest_const, mk_const, conv}) pat =
    62          fns as {is_const, dest_const, mk_const, conv}) pat =
    62        let
    63        let
    63         fun h instT =
    64         fun h instT =
    64           let
    65           let
    65             val substT = Thm.instantiate (instT, []);
    66             val substT = Thm.instantiate (instT, []);
    66             val substT_cterm = Drule.cterm_rule substT;
    67             val substT_cterm = Drule.cterm_rule substT;
    67 
    68 
    68             val vars' = map substT_cterm vars;
    69             val vars' = map substT_cterm vars;
    69             val semiring' = (map substT_cterm sr_ops, map substT sr_rules);
    70             val semiring' = (map substT_cterm sr_ops, map substT sr_rules);
    70             val ring' = (map substT_cterm r_ops, map substT r_rules);
    71             val ring' = (map substT_cterm r_ops, map substT r_rules);
       
    72             val field' = (map substT_cterm f_ops, map substT f_rules);
    71             val idom' = map substT idom;
    73             val idom' = map substT idom;
    72             val ideal' = map substT ideal;
    74             val ideal' = map substT ideal;
    73 
    75 
    74             val result = ({vars = vars', semiring = semiring', 
    76             val result = ({vars = vars', semiring = semiring', 
    75                            ring = ring', idom = idom', ideal = ideal'}, fns);
    77                            ring = ring', field = field', idom = idom', ideal = ideal'}, fns);
    76           in SOME result end
    78           in SOME result end
    77       in (case try Thm.match (pat, tm) of
    79       in (case try Thm.match (pat, tm) of
    78            NONE => NONE
    80            NONE => NONE
    79          | SOME (instT, _) => h instT)
    81          | SOME (instT, _) => h instT)
    80       end;
    82       end;
    81 
    83 
    82     fun match_struct (_,
    84     fun match_struct (_,
    83         entry as ({semiring = (sr_ops, _), ring = (r_ops, _), ...}, _): entry) =
    85         entry as ({semiring = (sr_ops, _), ring = (r_ops, _), field = (f_ops, _), ...}, _): entry) =
    84       get_first (match_inst entry) (sr_ops @ r_ops);
    86       get_first (match_inst entry) (sr_ops @ r_ops @ f_ops);
    85   in get_first match_struct (snd (get ctxt)) end;
    87   in get_first match_struct (snd (get ctxt)) end;
    86 
    88 
    87 
    89 
    88 (* logical content *)
    90 (* logical content *)
    89 
    91 
    90 val semiringN = "semiring";
    92 val semiringN = "semiring";
    91 val ringN = "ring";
    93 val ringN = "ring";
    92 val idomN = "idom";
    94 val idomN = "idom";
    93 val idealN = "ideal";
    95 val idealN = "ideal";
       
    96 val fieldN = "field";
    94 
    97 
    95 fun undefined _ = raise Match;
    98 fun undefined _ = raise Match;
    96 
    99 
    97 fun del_data key = apsnd (remove eq_data (key, []));
   100 fun del_data key = apsnd (remove eq_data (key, []));
    98 
   101 
   101    (fn th => Data.map (fn (ss,data) => (ss addsimps [th], data)));
   104    (fn th => Data.map (fn (ss,data) => (ss addsimps [th], data)));
   102 
   105 
   103 val del_ss = Thm.declaration_attribute 
   106 val del_ss = Thm.declaration_attribute 
   104    (fn th => Data.map (fn (ss,data) => (ss delsimps [th], data)));
   107    (fn th => Data.map (fn (ss,data) => (ss delsimps [th], data)));
   105 
   108 
   106 fun add {semiring = (sr_ops, sr_rules), ring = (r_ops, r_rules), idom, ideal} =
   109 fun add {semiring = (sr_ops, sr_rules), ring = (r_ops, r_rules), 
       
   110          field = (f_ops, f_rules), idom, ideal} =
   107   Thm.declaration_attribute (fn key => fn context => context |> Data.map
   111   Thm.declaration_attribute (fn key => fn context => context |> Data.map
   108     let
   112     let
   109       val ctxt = Context.proof_of context;
   113       val ctxt = Context.proof_of context;
   110 
   114 
   111       fun check kind name xs n =
   115       fun check kind name xs n =
   117       val _ =
   121       val _ =
   118         check_ops semiringN sr_ops 5 andalso
   122         check_ops semiringN sr_ops 5 andalso
   119         check_rules semiringN sr_rules 37 andalso
   123         check_rules semiringN sr_rules 37 andalso
   120         check_ops ringN r_ops 2 andalso
   124         check_ops ringN r_ops 2 andalso
   121         check_rules ringN r_rules 2 andalso
   125         check_rules ringN r_rules 2 andalso
       
   126         check_ops fieldN f_ops 2 andalso
       
   127         check_rules fieldN f_rules 2 andalso
   122         check_rules idomN idom 2;
   128         check_rules idomN idom 2;
   123 
   129 
   124       val mk_meta = LocalDefs.meta_rewrite_rule ctxt;
   130       val mk_meta = LocalDefs.meta_rewrite_rule ctxt;
   125       val sr_rules' = map mk_meta sr_rules;
   131       val sr_rules' = map mk_meta sr_rules;
   126       val r_rules' = map mk_meta r_rules;
   132       val r_rules' = map mk_meta r_rules;
       
   133       val f_rules' = map mk_meta f_rules;
   127 
   134 
   128       fun rule i = nth sr_rules' (i - 1);
   135       fun rule i = nth sr_rules' (i - 1);
   129 
   136 
   130       val (cx, cy) = Thm.dest_binop (hd sr_ops);
   137       val (cx, cy) = Thm.dest_binop (hd sr_ops);
   131       val cz = rule 34 |> Thm.rhs_of |> Thm.dest_arg |> Thm.dest_arg;
   138       val cz = rule 34 |> Thm.rhs_of |> Thm.dest_arg |> Thm.dest_arg;
   138       val (cp, cq) = rule 26 |> Thm.lhs_of |> Thm.dest_binop |> pairself Thm.dest_arg;
   145       val (cp, cq) = rule 26 |> Thm.lhs_of |> Thm.dest_binop |> pairself Thm.dest_arg;
   139 
   146 
   140       val vars = [ca, cb, cc, cd, cm, cn, cp, cq, cx, cy, cz, clx, crx, cly, cry];
   147       val vars = [ca, cb, cc, cd, cm, cn, cp, cq, cx, cy, cz, clx, crx, cly, cry];
   141       val semiring = (sr_ops, sr_rules');
   148       val semiring = (sr_ops, sr_rules');
   142       val ring = (r_ops, r_rules');
   149       val ring = (r_ops, r_rules');
       
   150       val field = (f_ops, f_rules');
   143       val ideal' = map (symmetric o mk_meta) ideal
   151       val ideal' = map (symmetric o mk_meta) ideal
   144     in
   152     in
   145       del_data key #>
   153       del_data key #>
   146       apsnd (cons (key, ({vars = vars, semiring = semiring, 
   154       apsnd (cons (key, ({vars = vars, semiring = semiring, 
   147                           ring = ring, idom = idom, ideal = ideal'},
   155                           ring = ring, field = field, idom = idom, ideal = ideal'},
   148              {is_const = undefined, dest_const = undefined, mk_const = undefined,
   156              {is_const = undefined, dest_const = undefined, mk_const = undefined,
   149              conv = undefined})))
   157              conv = undefined})))
   150     end);
   158     end);
   151 
   159 
   152 
   160 
   180 val delN = "del";
   188 val delN = "del";
   181 
   189 
   182 val any_keyword =
   190 val any_keyword =
   183   keyword2 semiringN opsN || keyword2 semiringN rulesN ||
   191   keyword2 semiringN opsN || keyword2 semiringN rulesN ||
   184   keyword2 ringN opsN || keyword2 ringN rulesN ||
   192   keyword2 ringN opsN || keyword2 ringN rulesN ||
       
   193   keyword2 fieldN opsN || keyword2 fieldN rulesN ||
   185   keyword2 idomN rulesN || keyword2 idealN rulesN;
   194   keyword2 idomN rulesN || keyword2 idealN rulesN;
   186 
   195 
   187 val thms = Scan.repeat (Scan.unless any_keyword Attrib.multi_thm) >> flat;
   196 val thms = Scan.repeat (Scan.unless any_keyword Attrib.multi_thm) >> flat;
   188 val terms = thms >> map Drule.dest_term;
   197 val terms = thms >> map Drule.dest_term;
   189 
   198 
   196     (Scan.lift (Args.$$$ delN >> K del) ||
   205     (Scan.lift (Args.$$$ delN >> K del) ||
   197       ((keyword2 semiringN opsN |-- terms) --
   206       ((keyword2 semiringN opsN |-- terms) --
   198        (keyword2 semiringN rulesN |-- thms)) --
   207        (keyword2 semiringN rulesN |-- thms)) --
   199       (optional (keyword2 ringN opsN |-- terms) --
   208       (optional (keyword2 ringN opsN |-- terms) --
   200        optional (keyword2 ringN rulesN |-- thms)) --
   209        optional (keyword2 ringN rulesN |-- thms)) --
       
   210       (optional (keyword2 fieldN opsN |-- terms) --
       
   211        optional (keyword2 fieldN rulesN |-- thms)) --
   201       optional (keyword2 idomN rulesN |-- thms) --
   212       optional (keyword2 idomN rulesN |-- thms) --
   202       optional (keyword2 idealN rulesN |-- thms)
   213       optional (keyword2 idealN rulesN |-- thms)
   203       >> (fn (((sr, r), id), idl) => add {semiring = sr, ring = r, idom = id, ideal = idl}))
   214       >> (fn ((((sr, r), f), id), idl) => 
       
   215              add {semiring = sr, ring = r, field = f, idom = id, ideal = idl}))
   204     "semiring normalizer data";
   216     "semiring normalizer data";
   205 
   217 
   206 end;
   218 end;
   207 
   219 
   208 
   220