src/HOL/Library/comm_ring.ML
author bulwahn
Sat, 19 Jul 2008 19:27:13 +0200
changeset 27656 d4f6e64ee7cc
parent 26939 1035c89b4c02
child 29265 5b4247055bd7
permissions -rw-r--r--
added verification framework for the HeapMonad and quicksort as example for this framework
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 *)
22997
d4f3b015b50b canonical prefixing of class constants
haftmann
parents: 22963
diff changeset
    20
fun cring_zero T = Const (@{const_name HOL.zero}, T);
d4f3b015b50b canonical prefixing of class constants
haftmann
parents: 22963
diff changeset
    21
fun cring_one T = Const (@{const_name 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 *)
22950
haftmann
parents: 21588
diff changeset
    25
fun polT t = Type ("Commutative_Ring.pol", [t]);
haftmann
parents: 21588
diff changeset
    26
fun polexT t = Type ("Commutative_Ring.polex", [t]);
17516
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    27
20623
6ae83d153dd4 tuned method setup;
wenzelm
parents: 19233
diff changeset
    28
(* pol *)
22950
haftmann
parents: 21588
diff changeset
    29
fun pol_Pc t = Const ("Commutative_Ring.pol.Pc", t --> polT t);
haftmann
parents: 21588
diff changeset
    30
fun pol_Pinj t = Const ("Commutative_Ring.pol.Pinj", HOLogic.natT --> polT t --> polT t);
haftmann
parents: 21588
diff changeset
    31
fun pol_PX t = Const ("Commutative_Ring.pol.PX", polT t --> HOLogic.natT --> polT t --> polT t);
17516
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    32
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    33
(* polex *)
22950
haftmann
parents: 21588
diff changeset
    34
fun polex_add t = Const ("Commutative_Ring.polex.Add", polexT t --> polexT t --> polexT t);
haftmann
parents: 21588
diff changeset
    35
fun polex_sub t = Const ("Commutative_Ring.polex.Sub", polexT t --> polexT t --> polexT t);
haftmann
parents: 21588
diff changeset
    36
fun polex_mul t = Const ("Commutative_Ring.polex.Mul", polexT t --> polexT t --> polexT t);
haftmann
parents: 21588
diff changeset
    37
fun polex_neg t = Const ("Commutative_Ring.polex.Neg", polexT t --> polexT t);
haftmann
parents: 21588
diff changeset
    38
fun polex_pol t = Const ("Commutative_Ring.polex.Pol", polT t --> polexT t);
haftmann
parents: 21588
diff changeset
    39
fun polex_pow t = Const ("Commutative_Ring.polex.Pow", polexT t --> HOLogic.natT --> polexT t);
17516
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    40
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    41
(* reification of polynoms : primitive cring expressions *)
22950
haftmann
parents: 21588
diff changeset
    42
fun reif_pol T vs (t as Free _) =
haftmann
parents: 21588
diff changeset
    43
      let
haftmann
parents: 21588
diff changeset
    44
        val one = @{term "1::nat"};
haftmann
parents: 21588
diff changeset
    45
        val i = find_index_eq t vs
haftmann
parents: 21588
diff changeset
    46
      in if i = 0
haftmann
parents: 21588
diff changeset
    47
        then pol_PX T $ (pol_Pc T $ cring_one T)
haftmann
parents: 21588
diff changeset
    48
          $ one $ (pol_Pc T $ cring_zero T)
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 23261
diff changeset
    49
        else pol_Pinj T $ HOLogic.mk_nat i
22950
haftmann
parents: 21588
diff changeset
    50
          $ (pol_PX T $ (pol_Pc T $ cring_one T)
haftmann
parents: 21588
diff changeset
    51
            $ one $ (pol_Pc T $ cring_zero T))
17516
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    52
        end
22950
haftmann
parents: 21588
diff changeset
    53
  | reif_pol T vs t = pol_Pc T $ t;
17516
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    54
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    55
(* reification of polynom expressions *)
22997
d4f3b015b50b canonical prefixing of class constants
haftmann
parents: 22963
diff changeset
    56
fun reif_polex T vs (Const (@{const_name HOL.plus}, _) $ a $ b) =
22950
haftmann
parents: 21588
diff changeset
    57
      polex_add T $ reif_polex T vs a $ reif_polex T vs b
22997
d4f3b015b50b canonical prefixing of class constants
haftmann
parents: 22963
diff changeset
    58
  | reif_polex T vs (Const (@{const_name HOL.minus}, _) $ a $ b) =
22950
haftmann
parents: 21588
diff changeset
    59
      polex_sub T $ reif_polex T vs a $ reif_polex T vs b
22997
d4f3b015b50b canonical prefixing of class constants
haftmann
parents: 22963
diff changeset
    60
  | reif_polex T vs (Const (@{const_name HOL.times}, _) $ a $ b) =
22950
haftmann
parents: 21588
diff changeset
    61
      polex_mul T $ reif_polex T vs a $ reif_polex T vs b
22997
d4f3b015b50b canonical prefixing of class constants
haftmann
parents: 22963
diff changeset
    62
  | reif_polex T vs (Const (@{const_name HOL.uminus}, _) $ a) =
22950
haftmann
parents: 21588
diff changeset
    63
      polex_neg T $ reif_polex T vs a
24996
ebd5f4cc7118 moved class power to theory Power
haftmann
parents: 24630
diff changeset
    64
  | reif_polex T vs (Const (@{const_name Power.power}, _) $ a $ n) =
22950
haftmann
parents: 21588
diff changeset
    65
      polex_pow T $ reif_polex T vs a $ n
haftmann
parents: 21588
diff changeset
    66
  | 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
    67
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    68
(* reification of the equation *)
22950
haftmann
parents: 21588
diff changeset
    69
val TFree (_, cr_sort) = @{typ "'a :: {comm_ring, recpower}"};
haftmann
parents: 21588
diff changeset
    70
haftmann
parents: 21588
diff changeset
    71
fun reif_eq thy (eq as Const("op =", Type("fun", [T, _])) $ lhs $ rhs) =
haftmann
parents: 21588
diff changeset
    72
      if Sign.of_sort thy (T, cr_sort) then
haftmann
parents: 21588
diff changeset
    73
        let
haftmann
parents: 21588
diff changeset
    74
          val fs = term_frees eq;
haftmann
parents: 21588
diff changeset
    75
          val cvs = cterm_of thy (HOLogic.mk_list T fs);
haftmann
parents: 21588
diff changeset
    76
          val clhs = cterm_of thy (reif_polex T fs lhs);
haftmann
parents: 21588
diff changeset
    77
          val crhs = cterm_of thy (reif_polex T fs rhs);
haftmann
parents: 21588
diff changeset
    78
          val ca = ctyp_of thy T;
haftmann
parents: 21588
diff changeset
    79
        in (ca, cvs, clhs, crhs) end
26939
1035c89b4c02 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 24996
diff changeset
    80
      else raise CRing ("reif_eq: not an equation over " ^ Syntax.string_of_sort_global thy cr_sort)
20623
6ae83d153dd4 tuned method setup;
wenzelm
parents: 19233
diff changeset
    81
  | reif_eq _ _ = raise CRing "reif_eq: not an equation";
17516
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    82
22950
haftmann
parents: 21588
diff changeset
    83
(* The cring tactic *)
17516
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    84
(* 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
    85
(* Use simply rewriting t^0 = 1 *)
20623
6ae83d153dd4 tuned method setup;
wenzelm
parents: 19233
diff changeset
    86
val cring_simps =
22950
haftmann
parents: 21588
diff changeset
    87
  [@{thm mkPX_def}, @{thm mkPinj_def}, @{thm sub_def}, @{thm power_add},
haftmann
parents: 21588
diff changeset
    88
    @{thm even_def}, @{thm pow_if}, sym OF [@{thm power_add}]];
17516
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
    89
20623
6ae83d153dd4 tuned method setup;
wenzelm
parents: 19233
diff changeset
    90
fun comm_ring_tac ctxt = SUBGOAL (fn (g, i) =>
6ae83d153dd4 tuned method setup;
wenzelm
parents: 19233
diff changeset
    91
  let
22950
haftmann
parents: 21588
diff changeset
    92
    val thy = ProofContext.theory_of ctxt;
haftmann
parents: 21588
diff changeset
    93
    val cring_ss = Simplifier.local_simpset_of ctxt  (*FIXME really the full simpset!?*)
haftmann
parents: 21588
diff changeset
    94
      addsimps cring_simps;
20623
6ae83d153dd4 tuned method setup;
wenzelm
parents: 19233
diff changeset
    95
    val (ca, cvs, clhs, crhs) = reif_eq thy (HOLogic.dest_Trueprop g)
6ae83d153dd4 tuned method setup;
wenzelm
parents: 19233
diff changeset
    96
    val norm_eq_th =
22950
haftmann
parents: 21588
diff changeset
    97
      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
    98
  in
6ae83d153dd4 tuned method setup;
wenzelm
parents: 19233
diff changeset
    99
    cut_rules_tac [norm_eq_th] i
6ae83d153dd4 tuned method setup;
wenzelm
parents: 19233
diff changeset
   100
    THEN (simp_tac cring_ss i)
6ae83d153dd4 tuned method setup;
wenzelm
parents: 19233
diff changeset
   101
    THEN (simp_tac cring_ss i)
6ae83d153dd4 tuned method setup;
wenzelm
parents: 19233
diff changeset
   102
  end);
6ae83d153dd4 tuned method setup;
wenzelm
parents: 19233
diff changeset
   103
6ae83d153dd4 tuned method setup;
wenzelm
parents: 19233
diff changeset
   104
val comm_ring_meth =
21588
cd0dc678a205 simplified method setup;
wenzelm
parents: 20713
diff changeset
   105
  Method.ctxt_args (Method.SIMPLE_METHOD' o comm_ring_tac);
17516
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
   106
45164074dad4 added Commutative_Ring (from Main HOL);
wenzelm
parents:
diff changeset
   107
val setup =
20623
6ae83d153dd4 tuned method setup;
wenzelm
parents: 19233
diff changeset
   108
  Method.add_method ("comm_ring", comm_ring_meth,
6ae83d153dd4 tuned method setup;
wenzelm
parents: 19233
diff changeset
   109
    "reflective decision procedure for equalities over commutative rings") #>
6ae83d153dd4 tuned method setup;
wenzelm
parents: 19233
diff changeset
   110
  Method.add_method ("algebra", comm_ring_meth,
6ae83d153dd4 tuned method setup;
wenzelm
parents: 19233
diff changeset
   111
    "method for proving algebraic properties (same as comm_ring)");
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
end;