src/HOL/Decision_Procs/commutative_ring_tac.ML
author haftmann
Thu, 28 Jan 2010 11:48:49 +0100
changeset 34974 18b41bba42b5
parent 33495 1464ddca182b
child 35267 8dfd816713c6
permissions -rw-r--r--
new theory Algebras.thy for generic algebraic structures
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
29265
5b4247055bd7 moved old add_term_vars, add_term_frees etc. to structure OldTerm;
wenzelm
parents: 26939
diff changeset
     1
(*  Author:     Amine Chaieb
17516
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
     2
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
     3
Tactic for solving equalities over commutative rings.
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
     4
*)
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
     5
33356
9157d0f9f00e moved Commutative_Ring into session Decision_Procs
haftmann
parents: 32149
diff changeset
     6
signature COMMUTATIVE_RING_TAC =
17516
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
     7
sig
33356
9157d0f9f00e moved Commutative_Ring into session Decision_Procs
haftmann
parents: 32149
diff changeset
     8
  val tac: Proof.context -> int -> tactic
9157d0f9f00e moved Commutative_Ring into session Decision_Procs
haftmann
parents: 32149
diff changeset
     9
  val setup: theory -> theory
17516
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    10
end
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    11
33356
9157d0f9f00e moved Commutative_Ring into session Decision_Procs
haftmann
parents: 32149
diff changeset
    12
structure Commutative_Ring_Tac: COMMUTATIVE_RING_TAC =
17516
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    13
struct
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    14
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    15
(* Zero and One of the commutative ring *)
34974
18b41bba42b5 new theory Algebras.thy for generic algebraic structures
haftmann
parents: 33495
diff changeset
    16
fun cring_zero T = Const (@{const_name Algebras.zero}, T);
18b41bba42b5 new theory Algebras.thy for generic algebraic structures
haftmann
parents: 33495
diff changeset
    17
fun cring_one T = Const (@{const_name Algebras.one}, T);
17516
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    18
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    19
(* reification functions *)
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    20
(* add two polynom expressions *)
33356
9157d0f9f00e moved Commutative_Ring into session Decision_Procs
haftmann
parents: 32149
diff changeset
    21
fun polT t = Type (@{type_name Commutative_Ring.pol}, [t]);
9157d0f9f00e moved Commutative_Ring into session Decision_Procs
haftmann
parents: 32149
diff changeset
    22
fun polexT t = Type (@{type_name Commutative_Ring.polex}, [t]);
17516
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    23
20623
6ae83d153dd4 tuned method setup;
wenzelm
parents: 19233
diff changeset
    24
(* pol *)
33356
9157d0f9f00e moved Commutative_Ring into session Decision_Procs
haftmann
parents: 32149
diff changeset
    25
fun pol_Pc t = Const (@{const_name Commutative_Ring.pol.Pc}, t --> polT t);
9157d0f9f00e moved Commutative_Ring into session Decision_Procs
haftmann
parents: 32149
diff changeset
    26
fun pol_Pinj t = Const (@{const_name Commutative_Ring.pol.Pinj}, HOLogic.natT --> polT t --> polT t);
9157d0f9f00e moved Commutative_Ring into session Decision_Procs
haftmann
parents: 32149
diff changeset
    27
fun pol_PX t = Const (@{const_name Commutative_Ring.pol.PX}, polT t --> HOLogic.natT --> polT t --> polT t);
17516
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    28
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    29
(* polex *)
33356
9157d0f9f00e moved Commutative_Ring into session Decision_Procs
haftmann
parents: 32149
diff changeset
    30
fun polex_add t = Const (@{const_name Commutative_Ring.polex.Add}, polexT t --> polexT t --> polexT t);
9157d0f9f00e moved Commutative_Ring into session Decision_Procs
haftmann
parents: 32149
diff changeset
    31
fun polex_sub t = Const (@{const_name Commutative_Ring.polex.Sub}, polexT t --> polexT t --> polexT t);
9157d0f9f00e moved Commutative_Ring into session Decision_Procs
haftmann
parents: 32149
diff changeset
    32
fun polex_mul t = Const (@{const_name Commutative_Ring.polex.Mul}, polexT t --> polexT t --> polexT t);
9157d0f9f00e moved Commutative_Ring into session Decision_Procs
haftmann
parents: 32149
diff changeset
    33
fun polex_neg t = Const (@{const_name Commutative_Ring.polex.Neg}, polexT t --> polexT t);
9157d0f9f00e moved Commutative_Ring into session Decision_Procs
haftmann
parents: 32149
diff changeset
    34
fun polex_pol t = Const (@{const_name Commutative_Ring.polex.Pol}, polT t --> polexT t);
9157d0f9f00e moved Commutative_Ring into session Decision_Procs
haftmann
parents: 32149
diff changeset
    35
fun polex_pow t = Const (@{const_name Commutative_Ring.polex.Pow}, polexT t --> HOLogic.natT --> polexT t);
17516
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    36
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    37
(* reification of polynoms : primitive cring expressions *)
22950
haftmann
parents: 21588
diff changeset
    38
fun reif_pol T vs (t as Free _) =
haftmann
parents: 21588
diff changeset
    39
      let
haftmann
parents: 21588
diff changeset
    40
        val one = @{term "1::nat"};
31986
a68f88d264f7 dropped find_index_eq
haftmann
parents: 31242
diff changeset
    41
        val i = find_index (fn t' => t' = t) vs
22950
haftmann
parents: 21588
diff changeset
    42
      in if i = 0
haftmann
parents: 21588
diff changeset
    43
        then pol_PX T $ (pol_Pc T $ cring_one T)
haftmann
parents: 21588
diff changeset
    44
          $ one $ (pol_Pc T $ cring_zero T)
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 23261
diff changeset
    45
        else pol_Pinj T $ HOLogic.mk_nat i
22950
haftmann
parents: 21588
diff changeset
    46
          $ (pol_PX T $ (pol_Pc T $ cring_one T)
haftmann
parents: 21588
diff changeset
    47
            $ one $ (pol_Pc T $ cring_zero T))
17516
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    48
        end
22950
haftmann
parents: 21588
diff changeset
    49
  | reif_pol T vs t = pol_Pc T $ t;
17516
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    50
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    51
(* reification of polynom expressions *)
34974
18b41bba42b5 new theory Algebras.thy for generic algebraic structures
haftmann
parents: 33495
diff changeset
    52
fun reif_polex T vs (Const (@{const_name Algebras.plus}, _) $ a $ b) =
22950
haftmann
parents: 21588
diff changeset
    53
      polex_add T $ reif_polex T vs a $ reif_polex T vs b
34974
18b41bba42b5 new theory Algebras.thy for generic algebraic structures
haftmann
parents: 33495
diff changeset
    54
  | reif_polex T vs (Const (@{const_name Algebras.minus}, _) $ a $ b) =
22950
haftmann
parents: 21588
diff changeset
    55
      polex_sub T $ reif_polex T vs a $ reif_polex T vs b
34974
18b41bba42b5 new theory Algebras.thy for generic algebraic structures
haftmann
parents: 33495
diff changeset
    56
  | reif_polex T vs (Const (@{const_name Algebras.times}, _) $ a $ b) =
22950
haftmann
parents: 21588
diff changeset
    57
      polex_mul T $ reif_polex T vs a $ reif_polex T vs b
34974
18b41bba42b5 new theory Algebras.thy for generic algebraic structures
haftmann
parents: 33495
diff changeset
    58
  | reif_polex T vs (Const (@{const_name Algebras.uminus}, _) $ a) =
22950
haftmann
parents: 21588
diff changeset
    59
      polex_neg T $ reif_polex T vs a
24996
ebd5f4cc7118 moved class power to theory Power
haftmann
parents: 24630
diff changeset
    60
  | reif_polex T vs (Const (@{const_name Power.power}, _) $ a $ n) =
22950
haftmann
parents: 21588
diff changeset
    61
      polex_pow T $ reif_polex T vs a $ n
haftmann
parents: 21588
diff changeset
    62
  | reif_polex T vs t = polex_pol T $ reif_pol T vs t;
17516
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    63
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    64
(* reification of the equation *)
31021
53642251a04f farewell to class recpower
haftmann
parents: 30510
diff changeset
    65
val cr_sort = @{sort "comm_ring_1"};
22950
haftmann
parents: 21588
diff changeset
    66
33356
9157d0f9f00e moved Commutative_Ring into session Decision_Procs
haftmann
parents: 32149
diff changeset
    67
fun reif_eq thy (eq as Const(@{const_name "op ="}, Type("fun", [T, _])) $ lhs $ rhs) =
22950
haftmann
parents: 21588
diff changeset
    68
      if Sign.of_sort thy (T, cr_sort) then
haftmann
parents: 21588
diff changeset
    69
        let
29265
5b4247055bd7 moved old add_term_vars, add_term_frees etc. to structure OldTerm;
wenzelm
parents: 26939
diff changeset
    70
          val fs = OldTerm.term_frees eq;
22950
haftmann
parents: 21588
diff changeset
    71
          val cvs = cterm_of thy (HOLogic.mk_list T fs);
haftmann
parents: 21588
diff changeset
    72
          val clhs = cterm_of thy (reif_polex T fs lhs);
haftmann
parents: 21588
diff changeset
    73
          val crhs = cterm_of thy (reif_polex T fs rhs);
haftmann
parents: 21588
diff changeset
    74
          val ca = ctyp_of thy T;
haftmann
parents: 21588
diff changeset
    75
        in (ca, cvs, clhs, crhs) end
33356
9157d0f9f00e moved Commutative_Ring into session Decision_Procs
haftmann
parents: 32149
diff changeset
    76
      else error ("reif_eq: not an equation over " ^ Syntax.string_of_sort_global thy cr_sort)
9157d0f9f00e moved Commutative_Ring into session Decision_Procs
haftmann
parents: 32149
diff changeset
    77
  | reif_eq _ _ = error "reif_eq: not an equation";
17516
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    78
22950
haftmann
parents: 21588
diff changeset
    79
(* The cring tactic *)
17516
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    80
(* 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
    81
(* Use simply rewriting t^0 = 1 *)
20623
6ae83d153dd4 tuned method setup;
wenzelm
parents: 19233
diff changeset
    82
val cring_simps =
22950
haftmann
parents: 21588
diff changeset
    83
  [@{thm mkPX_def}, @{thm mkPinj_def}, @{thm sub_def}, @{thm power_add},
haftmann
parents: 21588
diff changeset
    84
    @{thm even_def}, @{thm pow_if}, sym OF [@{thm power_add}]];
17516
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    85
33356
9157d0f9f00e moved Commutative_Ring into session Decision_Procs
haftmann
parents: 32149
diff changeset
    86
fun tac ctxt = SUBGOAL (fn (g, i) =>
20623
6ae83d153dd4 tuned method setup;
wenzelm
parents: 19233
diff changeset
    87
  let
22950
haftmann
parents: 21588
diff changeset
    88
    val thy = ProofContext.theory_of ctxt;
32149
ef59550a55d3 renamed simpset_of to global_simpset_of, and local_simpset_of to simpset_of -- same for claset and clasimpset;
wenzelm
parents: 31986
diff changeset
    89
    val cring_ss = Simplifier.simpset_of ctxt  (*FIXME really the full simpset!?*)
22950
haftmann
parents: 21588
diff changeset
    90
      addsimps cring_simps;
20623
6ae83d153dd4 tuned method setup;
wenzelm
parents: 19233
diff changeset
    91
    val (ca, cvs, clhs, crhs) = reif_eq thy (HOLogic.dest_Trueprop g)
6ae83d153dd4 tuned method setup;
wenzelm
parents: 19233
diff changeset
    92
    val norm_eq_th =
22950
haftmann
parents: 21588
diff changeset
    93
      simplify cring_ss (instantiate' [SOME ca] [SOME clhs, SOME crhs, SOME cvs] @{thm norm_eq})
20623
6ae83d153dd4 tuned method setup;
wenzelm
parents: 19233
diff changeset
    94
  in
6ae83d153dd4 tuned method setup;
wenzelm
parents: 19233
diff changeset
    95
    cut_rules_tac [norm_eq_th] i
6ae83d153dd4 tuned method setup;
wenzelm
parents: 19233
diff changeset
    96
    THEN (simp_tac cring_ss i)
6ae83d153dd4 tuned method setup;
wenzelm
parents: 19233
diff changeset
    97
    THEN (simp_tac cring_ss i)
6ae83d153dd4 tuned method setup;
wenzelm
parents: 19233
diff changeset
    98
  end);
6ae83d153dd4 tuned method setup;
wenzelm
parents: 19233
diff changeset
    99
17516
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
   100
val setup =
33356
9157d0f9f00e moved Commutative_Ring into session Decision_Procs
haftmann
parents: 32149
diff changeset
   101
  Method.setup @{binding comm_ring} (Scan.succeed (SIMPLE_METHOD' o tac))
9157d0f9f00e moved Commutative_Ring into session Decision_Procs
haftmann
parents: 32149
diff changeset
   102
    "reflective decision procedure for equalities over commutative rings";
17516
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
   103
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
   104
end;