src/HOL/Library/comm_ring.ML
author haftmann
Tue, 26 Sep 2006 13:34:16 +0200
changeset 20713 823967ef47f1
parent 20623 6ae83d153dd4
child 21588 cd0dc678a205
permissions -rw-r--r--
renamed 0 and 1 to HOL.zero and HOL.one respectivly; introduced corresponding syntactic classes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17516
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
     1
(*  ID:         $Id$
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
     2
    Author:     Amine Chaieb
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
     3
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
     4
Tactic for solving equalities over commutative rings.
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
     5
*)
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
     6
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
     7
signature COMM_RING =
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
     8
sig
20623
6ae83d153dd4 tuned method setup;
wenzelm
parents: 19233
diff changeset
     9
  val comm_ring_tac : Proof.context -> int -> tactic
18708
4b3dadb4fe33 setup: theory -> theory;
wenzelm
parents: 17516
diff changeset
    10
  val setup : theory -> theory
17516
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    11
end
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    12
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    13
structure CommRing: COMM_RING =
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    14
struct
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    15
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    16
(* The Cring exception for erronous uses of cring_tac *)
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    17
exception CRing of string;
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    18
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    19
(* Zero and One of the commutative ring *)
20713
823967ef47f1 renamed 0 and 1 to HOL.zero and HOL.one respectivly; introduced corresponding syntactic classes
haftmann
parents: 20623
diff changeset
    20
fun cring_zero T = Const("HOL.zero",T);
823967ef47f1 renamed 0 and 1 to HOL.zero and HOL.one respectivly; introduced corresponding syntactic classes
haftmann
parents: 20623
diff changeset
    21
fun cring_one T = Const("HOL.one",T);
17516
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    22
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    23
(* reification functions *)
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    24
(* add two polynom expressions *)
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    25
fun polT t = Type ("Commutative_Ring.pol",[t]);
20623
6ae83d153dd4 tuned method setup;
wenzelm
parents: 19233
diff changeset
    26
fun polexT t = Type("Commutative_Ring.polex",[t]);
17516
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    27
val nT = HOLogic.natT;
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    28
fun listT T = Type ("List.list",[T]);
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    29
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    30
(* Reification of the constructors *)
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    31
(* Nat*)
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    32
val succ = Const("Suc",nT --> nT);
20713
823967ef47f1 renamed 0 and 1 to HOL.zero and HOL.one respectivly; introduced corresponding syntactic classes
haftmann
parents: 20623
diff changeset
    33
val zero = Const("HOL.zero",nT);
823967ef47f1 renamed 0 and 1 to HOL.zero and HOL.one respectivly; introduced corresponding syntactic classes
haftmann
parents: 20623
diff changeset
    34
val one = Const("HOL.one",nT);
17516
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    35
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    36
(* Lists *)
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    37
fun reif_list T [] = Const("List.list.Nil",listT T)
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    38
  | reif_list T (x::xs) = Const("List.list.Cons",[T,listT T] ---> listT T)
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    39
                             $x$(reif_list T xs);
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    40
20623
6ae83d153dd4 tuned method setup;
wenzelm
parents: 19233
diff changeset
    41
(* pol *)
17516
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    42
fun pol_Pc t = Const("Commutative_Ring.pol.Pc",t --> polT t);
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    43
fun pol_Pinj t = Const("Commutative_Ring.pol.Pinj",[nT,polT t] ---> polT t);
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    44
fun pol_PX t = Const("Commutative_Ring.pol.PX",[polT t, nT, polT t] ---> polT t);
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    45
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    46
(* polex *)
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    47
fun polex_add t = Const("Commutative_Ring.polex.Add",[polexT t,polexT t] ---> polexT t);
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    48
fun polex_sub t = Const("Commutative_Ring.polex.Sub",[polexT t,polexT t] ---> polexT t);
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    49
fun polex_mul t = Const("Commutative_Ring.polex.Mul",[polexT t,polexT t] ---> polexT t);
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    50
fun polex_neg t = Const("Commutative_Ring.polex.Neg",polexT t --> polexT t);
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    51
fun polex_pol t = Const("Commutative_Ring.polex.Pol",polT t --> polexT t);
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    52
fun polex_pow t = Const("Commutative_Ring.polex.Pow",[polexT t, nT] ---> polexT t);
20623
6ae83d153dd4 tuned method setup;
wenzelm
parents: 19233
diff changeset
    53
17516
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    54
(* reification of natural numbers *)
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    55
fun reif_nat n =
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    56
    if n>0 then succ$(reif_nat (n-1))
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    57
    else if n=0 then zero
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    58
    else raise CRing "ring_tac: reif_nat negative n";
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    59
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    60
(* reification of polynoms : primitive cring expressions *)
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    61
fun reif_pol T vs t =
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    62
    case t of
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    63
       Free(_,_) =>
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    64
        let val i = find_index_eq t vs
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    65
        in if i = 0
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    66
           then (pol_PX T)$((pol_Pc T)$ (cring_one T))
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    67
                          $one$((pol_Pc T)$(cring_zero T))
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    68
           else (pol_Pinj T)$(reif_nat i)$
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    69
                            ((pol_PX T)$((pol_Pc T)$ (cring_one T))
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    70
                                       $one$
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    71
                                       ((pol_Pc T)$(cring_zero T)))
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    72
        end
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    73
      | _ => (pol_Pc T)$ t;
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    74
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    75
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    76
(* reification of polynom expressions *)
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    77
fun reif_polex T vs t =
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    78
    case t of
19233
77ca20b0ed77 renamed HOL + - * etc. to HOL.plus HOL.minus HOL.times etc.
haftmann
parents: 18708
diff changeset
    79
        Const("HOL.plus",_)$a$b => (polex_add T)
17516
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    80
                                   $ (reif_polex T vs a)$(reif_polex T vs b)
19233
77ca20b0ed77 renamed HOL + - * etc. to HOL.plus HOL.minus HOL.times etc.
haftmann
parents: 18708
diff changeset
    81
      | Const("HOL.minus",_)$a$b => (polex_sub T)
17516
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    82
                                   $ (reif_polex T vs a)$(reif_polex T vs b)
19233
77ca20b0ed77 renamed HOL + - * etc. to HOL.plus HOL.minus HOL.times etc.
haftmann
parents: 18708
diff changeset
    83
      | Const("HOL.times",_)$a$b =>  (polex_mul T)
17516
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    84
                                    $ (reif_polex T vs a)$ (reif_polex T vs b)
19233
77ca20b0ed77 renamed HOL + - * etc. to HOL.plus HOL.minus HOL.times etc.
haftmann
parents: 18708
diff changeset
    85
      | Const("HOL.uminus",_)$a => (polex_neg T)
17516
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    86
                                   $ (reif_polex T vs a)
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    87
      | (Const("Nat.power",_)$a$n) => (polex_pow T) $ (reif_polex T vs a) $ n
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    88
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    89
      | _ => (polex_pol T) $ (reif_pol T vs t);
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    90
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    91
(* reification of the equation *)
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    92
val cr_sort = Sign.read_sort (the_context ()) "{comm_ring,recpower}";
20623
6ae83d153dd4 tuned method setup;
wenzelm
parents: 19233
diff changeset
    93
fun reif_eq thy (eq as Const("op =",Type("fun",a::_))$lhs$rhs) =
6ae83d153dd4 tuned method setup;
wenzelm
parents: 19233
diff changeset
    94
    if Sign.of_sort thy (a,cr_sort)
17516
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    95
    then
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    96
        let val fs = term_frees eq
20623
6ae83d153dd4 tuned method setup;
wenzelm
parents: 19233
diff changeset
    97
            val cvs = cterm_of thy (reif_list a fs)
6ae83d153dd4 tuned method setup;
wenzelm
parents: 19233
diff changeset
    98
            val clhs = cterm_of thy (reif_polex a fs lhs)
6ae83d153dd4 tuned method setup;
wenzelm
parents: 19233
diff changeset
    99
            val crhs = cterm_of thy (reif_polex a fs rhs)
6ae83d153dd4 tuned method setup;
wenzelm
parents: 19233
diff changeset
   100
            val ca = ctyp_of thy a
17516
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
   101
        in (ca,cvs,clhs, crhs)
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
   102
        end
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
   103
    else raise CRing "reif_eq: not an equation over comm_ring + recpower"
20623
6ae83d153dd4 tuned method setup;
wenzelm
parents: 19233
diff changeset
   104
  | reif_eq _ _ = raise CRing "reif_eq: not an equation";
17516
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
   105
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
   106
(*The cring tactic  *)
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
   107
(* Attention: You have to make sure that no t^0 is in the goal!! *)
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
   108
(* Use simply rewriting t^0 = 1 *)
20623
6ae83d153dd4 tuned method setup;
wenzelm
parents: 19233
diff changeset
   109
val cring_simps =
6ae83d153dd4 tuned method setup;
wenzelm
parents: 19233
diff changeset
   110
  map thm ["mkPX_def", "mkPinj_def","sub_def", "power_add","even_def","pow_if"] @
6ae83d153dd4 tuned method setup;
wenzelm
parents: 19233
diff changeset
   111
  [sym OF [thm "power_add"]];
17516
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
   112
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
   113
val norm_eq = thm "norm_eq"
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
   114
20623
6ae83d153dd4 tuned method setup;
wenzelm
parents: 19233
diff changeset
   115
fun comm_ring_tac ctxt = SUBGOAL (fn (g, i) =>
6ae83d153dd4 tuned method setup;
wenzelm
parents: 19233
diff changeset
   116
  let
6ae83d153dd4 tuned method setup;
wenzelm
parents: 19233
diff changeset
   117
    val thy = ProofContext.theory_of ctxt
6ae83d153dd4 tuned method setup;
wenzelm
parents: 19233
diff changeset
   118
    val cring_ss = Simplifier.local_simpset_of ctxt  (* FIXME really the full simpset!? *)
6ae83d153dd4 tuned method setup;
wenzelm
parents: 19233
diff changeset
   119
      addsimps cring_simps
6ae83d153dd4 tuned method setup;
wenzelm
parents: 19233
diff changeset
   120
    val (ca, cvs, clhs, crhs) = reif_eq thy (HOLogic.dest_Trueprop g)
6ae83d153dd4 tuned method setup;
wenzelm
parents: 19233
diff changeset
   121
    val norm_eq_th =
6ae83d153dd4 tuned method setup;
wenzelm
parents: 19233
diff changeset
   122
      simplify cring_ss (instantiate' [SOME ca] [SOME clhs, SOME crhs, SOME cvs] norm_eq)
6ae83d153dd4 tuned method setup;
wenzelm
parents: 19233
diff changeset
   123
  in
6ae83d153dd4 tuned method setup;
wenzelm
parents: 19233
diff changeset
   124
    cut_rules_tac [norm_eq_th] i
6ae83d153dd4 tuned method setup;
wenzelm
parents: 19233
diff changeset
   125
    THEN (simp_tac cring_ss i)
6ae83d153dd4 tuned method setup;
wenzelm
parents: 19233
diff changeset
   126
    THEN (simp_tac cring_ss i)
6ae83d153dd4 tuned method setup;
wenzelm
parents: 19233
diff changeset
   127
  end);
6ae83d153dd4 tuned method setup;
wenzelm
parents: 19233
diff changeset
   128
6ae83d153dd4 tuned method setup;
wenzelm
parents: 19233
diff changeset
   129
val comm_ring_meth =
6ae83d153dd4 tuned method setup;
wenzelm
parents: 19233
diff changeset
   130
  Method.ctxt_args (fn ctxt => Method.SIMPLE_METHOD' HEADGOAL (comm_ring_tac ctxt));
17516
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
   131
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
   132
val setup =
20623
6ae83d153dd4 tuned method setup;
wenzelm
parents: 19233
diff changeset
   133
  Method.add_method ("comm_ring", comm_ring_meth,
6ae83d153dd4 tuned method setup;
wenzelm
parents: 19233
diff changeset
   134
    "reflective decision procedure for equalities over commutative rings") #>
6ae83d153dd4 tuned method setup;
wenzelm
parents: 19233
diff changeset
   135
  Method.add_method ("algebra", comm_ring_meth,
6ae83d153dd4 tuned method setup;
wenzelm
parents: 19233
diff changeset
   136
    "method for proving algebraic properties (same as comm_ring)");
17516
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
   137
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
   138
end;