src/HOL/Algebra/ringsimp.ML
changeset 20168 ed7bced29e1b
parent 20129 95e84d2c9f2c
child 20348 d59364649bcc
equal deleted inserted replaced
20167:fe5fd4fd4114 20168:ed7bced29e1b
     1 (*
     1 (*
     2   Title:     Normalisation method for locale cring
     2   Title:     Normalisation method for locales ring and cring
     3   Id:        $Id$
     3   Id:        $Id$
     4   Author:    Clemens Ballarin
     4   Author:    Clemens Ballarin
     5   Copyright: TU Muenchen
     5   Copyright: TU Muenchen
     6 *)
     6 *)
     7 
     7 
     8 (*** Term order for commutative rings ***)
     8 signature ALGEBRA =
       
     9 sig
       
    10   val print_structures: Context.generic -> unit
       
    11   val setup: Theory.theory -> Theory.theory
       
    12 end;
     9 
    13 
    10 fun ring_ord (Const (a, _)) =
    14 structure Algebra: ALGEBRA =
    11     find_index_eq a ["CRing.ring.zero", "CRing.ring.add", "CRing.a_inv",
    15 struct
    12       "CRing.minus", "Group.monoid.one", "Group.monoid.mult"]
       
    13   | ring_ord _ = ~1;
       
    14 
    16 
    15 fun termless_ring (a, b) = (Term.term_lpo ring_ord (a, b) = LESS);
       
    16 
    17 
    17 val cring_ss = HOL_ss settermless termless_ring;
    18 (** Theory and context data **)
    18 
    19 
    19 fun cring_normalise ctxt = let
    20 fun struct_eq ((s1, ts1), (s2, ts2)) =
    20     fun filter p t = (case HOLogic.dest_Trueprop t of
    21   (s1 = s2) andalso equal_lists (op aconv) (ts1, ts2);
    21         Const (p', _) $ Free (s, _) => if p = p' then [s] else []
       
    22       | _ => [])
       
    23       handle TERM _ => [];
       
    24     fun filter21 p t = (case HOLogic.dest_Trueprop t of
       
    25         Const (p', _) $ Free (s, _) $ _ => if p = p' then [s] else []
       
    26       | _ => [])
       
    27       handle TERM _ => [];
       
    28     fun filter22 p t = (case HOLogic.dest_Trueprop t of
       
    29         Const (p', _) $ _ $ Free (s, _) => if p = p' then [s] else []
       
    30       | _ => [])
       
    31       handle TERM _ => [];
       
    32     fun filter31 p t = (case HOLogic.dest_Trueprop t of
       
    33         Const (p', _) $ Free (s, _) $ _ $ _ => if p = p' then [s] else []
       
    34       | _ => [])
       
    35       handle TERM _ => [];
       
    36     fun filter32 p t = (case HOLogic.dest_Trueprop t of
       
    37         Const (p', _) $ _ $ Free (s, _) $ _ => if p = p' then [s] else []
       
    38       | _ => [])
       
    39       handle TERM _ => [];
       
    40 
    22 
    41     val prems = ProofContext.prems_of ctxt;
    23 structure AlgebraData = GenericDataFun
    42     val non_comm_rings = List.concat (map (filter "CRing.ring" o #prop o rep_thm) prems);
    24 (struct
    43     val comm_rings = List.concat (map (filter "CRing.cring" o #prop o rep_thm) prems) @
    25   val name = "Algebra/algebra";
    44         List.concat (map (filter "CRing.domain" o #prop o rep_thm) prems) @
    26   type T = ((string * term list) * thm list) list;
    45         List.concat (map (filter21 "Module.module" o #prop o rep_thm) prems) @
    27     (* Algebraic structures:
    46         List.concat (map (filter21 "Module.algebra" o #prop o rep_thm) prems) @
    28        identifier of the structure, list of operations and simp rules,
    47         List.concat (map (filter22 "Module.algebra" o #prop o rep_thm) prems) @
    29        identifier and operations identify the structure uniquely. *)
    48         List.concat (map (filter "UnivPoly.UP_cring" o #prop o rep_thm) prems) @
    30   val empty = [];
    49         List.concat (map (filter "UnivPoly.UP_domain" o #prop o rep_thm) prems) @
    31   val extend = I;
    50         List.concat (map (filter31 "CRing.ring_hom_cring" o #prop o rep_thm) prems) @
    32   fun merge _ (structs1, structs2) = gen_merge_lists
    51         List.concat (map (filter32 "CRing.ring_hom_cring" o #prop o rep_thm) prems);
    33     (fn ((s1, _), (s2, _)) => struct_eq (s1, s2)) structs1 structs2;
    52     val _ = tracing
    34   fun print generic structs =
    53       ("Non-commutative rings in proof context: " ^ commas non_comm_rings ^
    35     let
    54        "\nCommutative rings in proof context: " ^ commas comm_rings);
    36       val ctxt = Context.proof_of generic;
    55     val simps =
    37       val pretty_term = Pretty.quote o ProofContext.pretty_term ctxt;
    56       List.concat (map (fn s => ProofContext.get_thms ctxt (Name (s ^ ".ring_simprules")))
    38       fun pretty_struct ((s, ts), _) = Pretty.block
    57         non_comm_rings) @
    39         [Pretty.str s, Pretty.str ":", Pretty.brk 1,
    58       List.concat (map (fn s => ProofContext.get_thms ctxt (Name (s ^ ".cring_simprules")))
    40          Pretty.enclose "(" ")" (Pretty.breaks (map pretty_term ts))];
    59         comm_rings);
    41     in
    60   in Method.SIMPLE_METHOD' HEADGOAL (asm_full_simp_tac (cring_ss addsimps simps))
    42       Pretty.big_list "Algebraic structures:" (map pretty_struct structs) |>
    61   end;
    43         Pretty.writeln
       
    44     end
       
    45 end);
    62 
    46 
    63 (*
    47 val print_structures = AlgebraData.print;
    64 val ring_ss = HOL_basic_ss settermless termless_ring addsimps
    48 
    65   [a_assoc, l_zero, l_neg, a_comm, m_assoc, l_one, l_distr, m_comm, minus_def,
    49 
    66    r_zero, r_neg, r_neg2, r_neg1, minus_add, minus_minus, minus0,
    50 (** Method **)
    67    a_lcomm, m_lcomm, (*r_one,*) r_distr, l_null, r_null, l_minus, r_minus];
    51 
    68 *)
    52 fun struct_tac ((s, ts), simps) =
       
    53   let
       
    54     val ops = map (fst o Term.strip_comb) ts;
       
    55     fun ord (Const (a, _)) = find_index (fn (Const (b, _)) => a=b | _ => false) ops
       
    56       | ord (Free (a, _)) = find_index (fn (Free (b, _)) => a=b | _ => false) ops;
       
    57     fun less (a, b) = (Term.term_lpo ord (a, b) = LESS);
       
    58   in asm_full_simp_tac (HOL_ss settermless less addsimps simps) end;
       
    59 
       
    60 fun algebra_tac ctxt =
       
    61   let val _ = print_structures (Context.Proof ctxt)
       
    62   in EVERY' (map (fn s => TRY o struct_tac s) (AlgebraData.get (Context.Proof ctxt))) end;
       
    63 
       
    64 val method =
       
    65   Method.ctxt_args (fn ctxt => Method.SIMPLE_METHOD' HEADGOAL (algebra_tac ctxt))
       
    66 
       
    67 
       
    68 (** Attribute **)
       
    69 
       
    70 fun add_struct_thm s =
       
    71   Thm.declaration_attribute (fn thm => fn ctxt =>
       
    72     AlgebraData.map (fn structs => 
       
    73       if AList.defined struct_eq structs s
       
    74       then AList.map_entry struct_eq s (fn thms => thm :: thms) structs
       
    75       else (s, [thm])::structs) ctxt);
       
    76 fun del_struct s =
       
    77   Thm.declaration_attribute (fn _ => fn ctxt =>
       
    78     AlgebraData.map (AList.delete struct_eq s) ctxt);
       
    79 
       
    80 val attribute = Attrib.syntax
       
    81      (Scan.lift ((Args.add >> K true || Args.del >> K false) --| Args.colon ||
       
    82         Scan.succeed true) -- Scan.lift Args.name -- 
       
    83       Scan.repeat Args.term
       
    84       >> (fn ((b, n), ts) => if b then add_struct_thm (n, ts) else del_struct (n, ts)));
       
    85 
       
    86 
       
    87 (** Setup **)
       
    88 
       
    89 val setup =
       
    90   AlgebraData.init #>
       
    91   Method.add_methods [("algebra", method, "normalisation of algebraic structure")] #>
       
    92   Attrib.add_attributes [("algebra", attribute, "theorems controlling algebra method")];
       
    93 
       
    94 
       
    95 end;  (* struct *)