src/HOL/Tools/groebner.ML
author haftmann
Thu, 08 Jul 2010 16:19:24 +0200
changeset 37744 3daaf23b9ab4
parent 37388 793618618f78
child 38549 d0385f2764d8
permissions -rw-r--r--
tuned titles
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37744
3daaf23b9ab4 tuned titles
haftmann
parents: 37388
diff changeset
     1
(*  Title:      HOL/Tools/groebner.ML
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
     2
    Author:     Amine Chaieb, TU Muenchen
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
     3
*)
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
     4
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
     5
signature GROEBNER =
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
     6
sig
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
     7
  val ring_and_ideal_conv :
30866
dd5117e2d73e now deals with devision in fields
chaieb
parents: 29800
diff changeset
     8
    {idom: thm list, ring: cterm list * thm list, field: cterm list * thm list,
dd5117e2d73e now deals with devision in fields
chaieb
parents: 29800
diff changeset
     9
     vars: cterm list, semiring: cterm list * thm list, ideal : thm list} ->
23487
c48defc2b28c made type conv pervasive;
wenzelm
parents: 23334
diff changeset
    10
    (cterm -> Rat.rat) -> (Rat.rat -> cterm) ->
c48defc2b28c made type conv pervasive;
wenzelm
parents: 23334
diff changeset
    11
    conv ->  conv ->
36723
b91ef008b560 moved method syntax here
haftmann
parents: 36713
diff changeset
    12
     {ring_conv : conv, 
b91ef008b560 moved method syntax here
haftmann
parents: 36713
diff changeset
    13
     simple_ideal: (cterm list -> cterm -> (cterm * cterm -> order) -> cterm list),
b91ef008b560 moved method syntax here
haftmann
parents: 36713
diff changeset
    14
     multi_ideal: cterm list -> cterm list -> cterm list -> (cterm * cterm) list,
b91ef008b560 moved method syntax here
haftmann
parents: 36713
diff changeset
    15
     poly_eq_ss: simpset, unwind_conv : conv}
b91ef008b560 moved method syntax here
haftmann
parents: 36713
diff changeset
    16
  val ring_tac: thm list -> thm list -> Proof.context -> int -> tactic
b91ef008b560 moved method syntax here
haftmann
parents: 36713
diff changeset
    17
  val ideal_tac: thm list -> thm list -> Proof.context -> int -> tactic
b91ef008b560 moved method syntax here
haftmann
parents: 36713
diff changeset
    18
  val algebra_tac: thm list -> thm list -> Proof.context -> int -> tactic
b91ef008b560 moved method syntax here
haftmann
parents: 36713
diff changeset
    19
  val algebra_method: (Proof.context -> Method.method) context_parser
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
    20
end
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
    21
25251
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
    22
structure Groebner : GROEBNER =
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
    23
struct
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
    24
36713
4898bf611209 avoid open; tuned references to theorems
haftmann
parents: 36702
diff changeset
    25
open Conv Drule Thm;
25251
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
    26
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
    27
fun is_comb ct =
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
    28
  (case Thm.term_of ct of
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
    29
    _ $ _ => true
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
    30
  | _ => false);
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
    31
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
    32
val concl = Thm.cprop_of #> Thm.dest_arg;
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
    33
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
    34
fun is_binop ct ct' =
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
    35
  (case Thm.term_of ct' of
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
    36
    c $ _ $ _ => term_of ct aconv c
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
    37
  | _ => false);
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
    38
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
    39
fun dest_binary ct ct' =
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
    40
  if is_binop ct ct' then Thm.dest_binop ct'
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
    41
  else raise CTERM ("dest_binary: bad binop", [ct, ct'])
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
    42
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
    43
fun inst_thm inst = Thm.instantiate ([], inst);
23557
3fe7aea46633 HOLogic.conj_intr/elims;
wenzelm
parents: 23514
diff changeset
    44
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
    45
val rat_0 = Rat.zero;
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
    46
val rat_1 = Rat.one;
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
    47
val minus_rat = Rat.neg;
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
    48
val denominator_rat = Rat.quotient_of_rat #> snd #> Rat.rat_of_int;
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
    49
fun int_of_rat a =
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
    50
    case Rat.quotient_of_rat a of (i,1) => i | _ => error "int_of_rat: not an int";
23514
25e69e56355d dropped Library.lcm
haftmann
parents: 23487
diff changeset
    51
val lcm_rat = fn x => fn y => Rat.rat_of_int (Integer.lcm (int_of_rat x) (int_of_rat y));
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
    52
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
    53
val (eqF_intr, eqF_elim) =
36713
4898bf611209 avoid open; tuned references to theorems
haftmann
parents: 36702
diff changeset
    54
  let val [th1,th2] = @{thms PFalse}
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
    55
  in (fn th => th COMP th2, fn th => th COMP th1) end;
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
    56
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
    57
val (PFalse, PFalse') =
36713
4898bf611209 avoid open; tuned references to theorems
haftmann
parents: 36702
diff changeset
    58
 let val PFalse_eq = nth @{thms simp_thms} 13
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
    59
 in (PFalse_eq RS iffD1, PFalse_eq RS iffD2) end;
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
    60
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
    61
25251
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
    62
(* Type for recording history, i.e. how a polynomial was obtained. *)
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
    63
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
    64
datatype history =
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 23579
diff changeset
    65
   Start of int
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 23579
diff changeset
    66
 | Mmul of (Rat.rat * int list) * history
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
    67
 | Add of history * history;
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
    68
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
    69
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
    70
(* Monomial ordering. *)
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
    71
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
    72
fun morder_lt m1 m2=
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
    73
    let fun lexorder l1 l2 =
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
    74
            case (l1,l2) of
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
    75
                ([],[]) => false
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
    76
              | (x1::o1,x2::o2) => x1 > x2 orelse x1 = x2 andalso lexorder o1 o2
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
    77
              | _ => error "morder: inconsistent monomial lengths"
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 23579
diff changeset
    78
        val n1 = Integer.sum m1
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 23579
diff changeset
    79
        val n2 = Integer.sum m2 in
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
    80
    n1 < n2 orelse n1 = n2 andalso lexorder m1 m2
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
    81
    end;
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
    82
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
    83
fun morder_le m1 m2 = morder_lt m1 m2 orelse (m1 = m2);
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
    84
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
    85
fun morder_gt m1 m2 = morder_lt m2 m1;
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
    86
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
    87
(* Arithmetic on canonical polynomials. *)
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
    88
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
    89
fun grob_neg l = map (fn (c,m) => (minus_rat c,m)) l;
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
    90
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
    91
fun grob_add l1 l2 =
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
    92
  case (l1,l2) of
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
    93
    ([],l2) => l2
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
    94
  | (l1,[]) => l1
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
    95
  | ((c1,m1)::o1,(c2,m2)::o2) =>
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
    96
        if m1 = m2 then
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
    97
          let val c = c1+/c2 val rest = grob_add o1 o2 in
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
    98
          if c =/ rat_0 then rest else (c,m1)::rest end
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
    99
        else if morder_lt m2 m1 then (c1,m1)::(grob_add o1 l2)
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   100
        else (c2,m2)::(grob_add l1 o2);
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   101
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   102
fun grob_sub l1 l2 = grob_add l1 (grob_neg l2);
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   103
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 23579
diff changeset
   104
fun grob_mmul (c1,m1) (c2,m2) = (c1*/c2, ListPair.map (op +) (m1, m2));
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   105
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   106
fun grob_cmul cm pol = map (grob_mmul cm) pol;
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   107
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   108
fun grob_mul l1 l2 =
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   109
  case l1 of
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   110
    [] => []
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   111
  | (h1::t1) => grob_add (grob_cmul h1 l2) (grob_mul t1 l2);
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   112
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   113
fun grob_inv l =
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   114
  case l of
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   115
    [(c,vs)] => if (forall (fn x => x = 0) vs) then
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   116
                  if (c =/ rat_0) then error "grob_inv: division by zero"
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   117
                  else [(rat_1 // c,vs)]
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   118
              else error "grob_inv: non-constant divisor polynomial"
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   119
  | _ => error "grob_inv: non-constant divisor polynomial";
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   120
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   121
fun grob_div l1 l2 =
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   122
  case l2 of
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   123
    [(c,l)] => if (forall (fn x => x = 0) l) then
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   124
                 if c =/ rat_0 then error "grob_div: division by zero"
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   125
                 else grob_cmul (rat_1 // c,l) l1
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   126
             else error "grob_div: non-constant divisor polynomial"
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   127
  | _ => error "grob_div: non-constant divisor polynomial";
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   128
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   129
fun grob_pow vars l n =
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   130
  if n < 0 then error "grob_pow: negative power"
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   131
  else if n = 0 then [(rat_1,map (fn v => 0) vars)]
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 23579
diff changeset
   132
  else grob_mul l (grob_pow vars l (n - 1));
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   133
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   134
fun degree vn p =
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   135
 case p of
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   136
  [] => error "Zero polynomial"
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   137
| [(c,ns)] => nth ns vn
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 23579
diff changeset
   138
| (c,ns)::p' => Int.max (nth ns vn, degree vn p');
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   139
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   140
fun head_deg vn p = let val d = degree vn p in
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   141
 (d,fold (fn (c,r) => fn q => grob_add q [(c, map_index (fn (i,n) => if i = vn then 0 else n) r)]) (filter (fn (c,ns) => c <>/ rat_0 andalso nth ns vn = d) p) []) end;
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   142
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 23579
diff changeset
   143
val is_zerop = forall (fn (c,ns) => c =/ rat_0 andalso forall (curry (op =) 0) ns);
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   144
val grob_pdiv =
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   145
 let fun pdiv_aux vn (n,a) p k s =
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   146
  if is_zerop s then (k,s) else
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   147
  let val (m,b) = head_deg vn s
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   148
  in if m < n then (k,s) else
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 23579
diff changeset
   149
     let val p' = grob_mul p [(rat_1, map_index (fn (i,v) => if i = vn then m - n else 0)
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   150
                                                (snd (hd s)))]
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   151
     in if a = b then pdiv_aux vn (n,a) p k (grob_sub s p')
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 23579
diff changeset
   152
        else pdiv_aux vn (n,a) p (k + 1) (grob_sub (grob_mul a s) (grob_mul b p'))
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   153
     end
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   154
  end
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   155
 in fn vn => fn s => fn p => pdiv_aux vn (head_deg vn p) p 0 s
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   156
 end;
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   157
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   158
(* Monomial division operation. *)
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   159
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   160
fun mdiv (c1,m1) (c2,m2) =
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   161
  (c1//c2,
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 23579
diff changeset
   162
   map2 (fn n1 => fn n2 => if n1 < n2 then error "mdiv" else n1 - n2) m1 m2);
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   163
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   164
(* Lowest common multiple of two monomials. *)
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   165
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 23579
diff changeset
   166
fun mlcm (c1,m1) (c2,m2) = (rat_1, ListPair.map Int.max (m1, m2));
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   167
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   168
(* Reduce monomial cm by polynomial pol, returning replacement for cm.  *)
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   169
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   170
fun reduce1 cm (pol,hpol) =
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   171
  case pol of
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   172
    [] => error "reduce1"
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   173
  | cm1::cms => ((let val (c,m) = mdiv cm cm1 in
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   174
                    (grob_cmul (minus_rat c,m) cms,
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   175
                     Mmul((minus_rat c,m),hpol)) end)
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   176
                handle  ERROR _ => error "reduce1");
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   177
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   178
(* Try this for all polynomials in a basis.  *)
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   179
fun tryfind f l =
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   180
    case l of
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   181
        [] => error "tryfind"
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   182
      | (h::t) => ((f h) handle ERROR _ => tryfind f t);
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   183
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   184
fun reduceb cm basis = tryfind (fn p => reduce1 cm p) basis;
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   185
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   186
(* Reduction of a polynomial (always picking largest monomial possible).     *)
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   187
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   188
fun reduce basis (pol,hist) =
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   189
  case pol of
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   190
    [] => (pol,hist)
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   191
  | cm::ptl => ((let val (q,hnew) = reduceb cm basis in
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   192
                   reduce basis (grob_add q ptl,Add(hnew,hist)) end)
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   193
               handle (ERROR _) =>
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   194
                   (let val (q,hist') = reduce basis (ptl,hist) in
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   195
                       (cm::q,hist') end));
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   196
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   197
(* Check for orthogonality w.r.t. LCM.                                       *)
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   198
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   199
fun orthogonal l p1 p2 =
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   200
  snd l = snd(grob_mmul (hd p1) (hd p2));
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   201
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   202
(* Compute S-polynomial of two polynomials.                                  *)
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   203
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   204
fun spoly cm ph1 ph2 =
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   205
  case (ph1,ph2) of
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   206
    (([],h),p) => ([],h)
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   207
  | (p,([],h)) => ([],h)
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   208
  | ((cm1::ptl1,his1),(cm2::ptl2,his2)) =>
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   209
        (grob_sub (grob_cmul (mdiv cm cm1) ptl1)
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   210
                  (grob_cmul (mdiv cm cm2) ptl2),
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   211
         Add(Mmul(mdiv cm cm1,his1),
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   212
             Mmul(mdiv (minus_rat(fst cm),snd cm) cm2,his2)));
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   213
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   214
(* Make a polynomial monic.                                                  *)
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   215
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   216
fun monic (pol,hist) =
23579
1a8ca0e480cd avoid polymorphic equality;
wenzelm
parents: 23557
diff changeset
   217
  if null pol then (pol,hist) else
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   218
  let val (c',m') = hd pol in
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   219
  (map (fn (c,m) => (c//c',m)) pol,
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   220
   Mmul((rat_1 // c',map (K 0) m'),hist)) end;
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   221
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   222
(* The most popular heuristic is to order critical pairs by LCM monomial.    *)
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   223
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   224
fun forder ((c1,m1),_) ((c2,m2),_) = morder_lt m1 m2;
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   225
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   226
fun poly_lt  p q =
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   227
  case (p,q) of
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   228
    (p,[]) => false
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   229
  | ([],q) => true
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   230
  | ((c1,m1)::o1,(c2,m2)::o2) =>
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   231
        c1 </ c2 orelse
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   232
        c1 =/ c2 andalso ((morder_lt m1 m2) orelse m1 = m2 andalso poly_lt o1 o2);
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   233
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   234
fun align  ((p,hp),(q,hq)) =
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   235
  if poly_lt p q then ((p,hp),(q,hq)) else ((q,hq),(p,hp));
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   236
fun forall2 p l1 l2 =
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   237
  case (l1,l2) of
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   238
    ([],[]) => true
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   239
  | (h1::t1,h2::t2) => p h1 h2 andalso forall2 p t1 t2
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   240
  | _ => false;
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   241
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   242
fun poly_eq p1 p2 =
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 23579
diff changeset
   243
  forall2 (fn (c1,m1) => fn (c2,m2) => c1 =/ c2 andalso (m1: int list) = m2) p1 p2;
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   244
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   245
fun memx ((p1,h1),(p2,h2)) ppairs =
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   246
  not (exists (fn ((q1,_),(q2,_)) => poly_eq p1 q1 andalso poly_eq p2 q2) ppairs);
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   247
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   248
(* Buchberger's second criterion.                                            *)
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   249
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   250
fun criterion2 basis (lcm,((p1,h1),(p2,h2))) opairs =
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   251
  exists (fn g => not(poly_eq (fst g) p1) andalso not(poly_eq (fst g) p2) andalso
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   252
                   can (mdiv lcm) (hd(fst g)) andalso
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   253
                   not(memx (align (g,(p1,h1))) (map snd opairs)) andalso
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   254
                   not(memx (align (g,(p2,h2))) (map snd opairs))) basis;
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   255
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   256
(* Test for hitting constant polynomial.                                     *)
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   257
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   258
fun constant_poly p =
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 23579
diff changeset
   259
  length p = 1 andalso forall (fn x => x = 0) (snd(hd p));
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   260
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   261
(* Grobner basis algorithm.                                                  *)
25251
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   262
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   263
(* FIXME: try to get rid of mergesort? *)
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   264
fun merge ord l1 l2 =
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   265
 case l1 of
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   266
  [] => l2
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   267
 | h1::t1 =>
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   268
   case l2 of
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   269
    [] => l1
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   270
   | h2::t2 => if ord h1 h2 then h1::(merge ord t1 l2)
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   271
               else h2::(merge ord l1 t2);
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   272
fun mergesort ord l =
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   273
 let
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   274
 fun mergepairs l1 l2 =
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   275
  case (l1,l2) of
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   276
   ([s],[]) => s
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   277
 | (l,[]) => mergepairs [] l
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   278
 | (l,[s1]) => mergepairs (s1::l) []
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   279
 | (l,(s1::s2::ss)) => mergepairs ((merge ord s1 s2)::l) ss
23579
1a8ca0e480cd avoid polymorphic equality;
wenzelm
parents: 23557
diff changeset
   280
 in if null l  then []  else mergepairs [] (map (fn x => [x]) l)
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   281
 end;
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   282
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   283
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   284
fun grobner_basis basis pairs =
25251
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   285
 case pairs of
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   286
   [] => basis
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   287
 | (l,(p1,p2))::opairs =>
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   288
   let val (sph as (sp,hist)) = monic (reduce basis (spoly l p1 p2))
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   289
   in 
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   290
    if null sp orelse criterion2 basis (l,(p1,p2)) opairs
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   291
    then grobner_basis basis opairs
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   292
    else if constant_poly sp then grobner_basis (sph::basis) []
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   293
    else 
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   294
     let 
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   295
      val rawcps = map (fn p => (mlcm (hd(fst p)) (hd sp),align(p,sph)))
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   296
                              basis
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   297
      val newcps = filter (fn (l,(p,q)) => not(orthogonal l (fst p) (fst q)))
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   298
                        rawcps
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   299
     in grobner_basis (sph::basis)
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   300
                 (merge forder opairs (mergesort forder newcps))
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   301
     end
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   302
   end;
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   303
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   304
(* Interreduce initial polynomials.                                          *)
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   305
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   306
fun grobner_interreduce rpols ipols =
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   307
  case ipols of
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   308
    [] => map monic (rev rpols)
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   309
  | p::ps => let val p' = reduce (rpols @ ps) p in
23579
1a8ca0e480cd avoid polymorphic equality;
wenzelm
parents: 23557
diff changeset
   310
             if null (fst p') then grobner_interreduce rpols ps
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   311
             else grobner_interreduce (p'::rpols) ps end;
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   312
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   313
(* Overall function.                                                         *)
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   314
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   315
fun grobner pols =
33063
4d462963a7db map_range (and map_index) combinator
haftmann
parents: 33050
diff changeset
   316
    let val npols = map_index (fn (n, p) => (p, Start n)) pols
23579
1a8ca0e480cd avoid polymorphic equality;
wenzelm
parents: 23557
diff changeset
   317
        val phists = filter (fn (p,_) => not (null p)) npols
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   318
        val bas = grobner_interreduce [] (map monic phists)
25538
58e8ba3b792b map_product and fold_product
haftmann
parents: 25251
diff changeset
   319
        val prs0 = map_product pair bas bas
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   320
        val prs1 = filter (fn ((x,_),(y,_)) => poly_lt x y) prs0
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   321
        val prs2 = map (fn (p,q) => (mlcm (hd(fst p)) (hd(fst q)),(p,q))) prs1
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   322
        val prs3 =
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   323
            filter (fn (l,(p,q)) => not(orthogonal l (fst p) (fst q))) prs2 in
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   324
        grobner_basis bas (mergesort forder prs3) end;
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   325
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   326
(* Get proof of contradiction from Grobner basis.                            *)
25251
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   327
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   328
fun find p l =
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   329
  case l of
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   330
      [] => error "find"
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   331
    | (h::t) => if p(h) then h else find p t;
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   332
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   333
fun grobner_refute pols =
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   334
  let val gb = grobner pols in
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   335
  snd(find (fn (p,h) => length p = 1 andalso forall (fn x=> x=0) (snd(hd p))) gb)
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   336
  end;
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   337
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   338
(* Turn proof into a certificate as sum of multipliers.                      *)
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   339
(* In principle this is very inefficient: in a heavily shared proof it may   *)
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   340
(* make the same calculation many times. Could put in a cache or something.  *)
25251
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   341
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   342
fun resolve_proof vars prf =
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   343
  case prf of
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   344
    Start(~1) => []
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   345
  | Start m => [(m,[(rat_1,map (K 0) vars)])]
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   346
  | Mmul(pol,lin) =>
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   347
        let val lis = resolve_proof vars lin in
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   348
            map (fn (n,p) => (n,grob_cmul pol p)) lis end
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   349
  | Add(lin1,lin2) =>
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   350
        let val lis1 = resolve_proof vars lin1
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   351
            val lis2 = resolve_proof vars lin2
33042
ddf1f03a9ad9 curried union as canonical list operation
haftmann
parents: 33039
diff changeset
   352
            val dom = distinct (op =) (union (op =) (map fst lis1) (map fst lis2))
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   353
        in
23557
3fe7aea46633 HOLogic.conj_intr/elims;
wenzelm
parents: 23514
diff changeset
   354
            map (fn n => let val a = these (AList.lookup (op =) lis1 n)
3fe7aea46633 HOLogic.conj_intr/elims;
wenzelm
parents: 23514
diff changeset
   355
                             val b = these (AList.lookup (op =) lis2 n)
3fe7aea46633 HOLogic.conj_intr/elims;
wenzelm
parents: 23514
diff changeset
   356
                         in (n,grob_add a b) end) dom end;
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   357
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   358
(* Run the procedure and produce Weak Nullstellensatz certificate.           *)
25251
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   359
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   360
fun grobner_weak vars pols =
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   361
    let val cert = resolve_proof vars (grobner_refute pols)
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   362
        val l =
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   363
            fold_rev (fold_rev (lcm_rat o denominator_rat o fst) o snd) cert (rat_1) in
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   364
        (l,map (fn (i,p) => (i,map (fn (d,m) => (l*/d,m)) p)) cert) end;
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   365
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   366
(* Prove a polynomial is in ideal generated by others, using Grobner basis.  *)
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   367
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   368
fun grobner_ideal vars pols pol =
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   369
  let val (pol',h) = reduce (grobner pols) (grob_neg pol,Start(~1)) in
24913
eb6fd8f78d56 fixed bug in grobner_ideal
chaieb
parents: 24630
diff changeset
   370
  if not (null pol') then error "grobner_ideal: not in the ideal" else
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   371
  resolve_proof vars h end;
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   372
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   373
(* Produce Strong Nullstellensatz certificate for a power of pol.            *)
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   374
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   375
fun grobner_strong vars pols pol =
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   376
    let val vars' = @{cterm "True"}::vars
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   377
        val grob_z = [(rat_1,1::(map (fn x => 0) vars))]
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   378
        val grob_1 = [(rat_1,(map (fn x => 0) vars'))]
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   379
        fun augment p= map (fn (c,m) => (c,0::m)) p
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   380
        val pols' = map augment pols
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   381
        val pol' = augment pol
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   382
        val allpols = (grob_sub (grob_mul grob_z pol') grob_1)::pols'
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   383
        val (l,cert) = grobner_weak vars' allpols
33029
2fefe039edf1 uniform use of Integer.min/max;
wenzelm
parents: 30866
diff changeset
   384
        val d = fold (fold (Integer.max o hd o snd) o snd) cert 0
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   385
        fun transform_monomial (c,m) =
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   386
            grob_cmul (c,tl m) (grob_pow vars pol (d - hd m))
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   387
        fun transform_polynomial q = fold_rev (grob_add o transform_monomial) q []
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   388
        val cert' = map (fn (c,q) => (c-1,transform_polynomial q))
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   389
                        (filter (fn (k,_) => k <> 0) cert) in
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   390
        (d,l,cert') end;
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   391
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   392
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   393
(* Overall parametrized universal procedure for (semi)rings.                 *)
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   394
(* We return an ideal_conv and the actual ring prover.                       *)
25251
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   395
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   396
fun refute_disj rfn tm =
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   397
 case term_of tm of
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   398
  Const("op |",_)$l$r =>
25251
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   399
   compose_single(refute_disj rfn (dest_arg tm),2,compose_single(refute_disj rfn (dest_arg1 tm),2,disjE))
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   400
  | _ => rfn tm ;
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   401
36713
4898bf611209 avoid open; tuned references to theorems
haftmann
parents: 36702
diff changeset
   402
val notnotD = @{thm notnotD};
25251
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   403
fun mk_binop ct x y = capply (capply ct x) y
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   404
25251
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   405
val mk_comb = capply;
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   406
fun is_neg t =
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   407
    case term_of t of
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   408
      (Const("Not",_)$p) => true
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   409
    | _  => false;
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   410
fun is_eq t =
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   411
 case term_of t of
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   412
 (Const("op =",_)$_$_) => true
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   413
| _  => false;
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   414
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   415
fun end_itlist f l =
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   416
  case l of
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   417
        []     => error "end_itlist"
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   418
      | [x]    => x
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   419
      | (h::t) => f h (end_itlist f t);
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   420
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   421
val list_mk_binop = fn b => end_itlist (mk_binop b);
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   422
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   423
val list_dest_binop = fn b =>
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   424
 let fun h acc t =
25251
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   425
  ((let val (l,r) = dest_binary b t in h (h acc r) l end)
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   426
   handle CTERM _ => (t::acc)) (* Why had I handle _ => ? *)
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   427
 in h []
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   428
 end;
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   429
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   430
val strip_exists =
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   431
 let fun h (acc, t) =
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   432
      case (term_of t) of
25251
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   433
       Const("Ex",_)$Abs(x,T,p) => h (dest_abs NONE (dest_arg t) |>> (fn v => v::acc))
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   434
     | _ => (acc,t)
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   435
 in fn t => h ([],t)
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   436
 end;
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   437
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   438
fun is_forall t =
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   439
 case term_of t of
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   440
  (Const("All",_)$Abs(_,_,_)) => true
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   441
| _ => false;
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   442
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   443
val mk_object_eq = fn th => th COMP meta_eq_to_obj_eq;
36713
4898bf611209 avoid open; tuned references to theorems
haftmann
parents: 36702
diff changeset
   444
val bool_simps = @{thms bool_simps};
4898bf611209 avoid open; tuned references to theorems
haftmann
parents: 36702
diff changeset
   445
val nnf_simps = @{thms nnf_simps};
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   446
val nnf_conv = Simplifier.rewrite (HOL_basic_ss addsimps bool_simps addsimps nnf_simps)
36713
4898bf611209 avoid open; tuned references to theorems
haftmann
parents: 36702
diff changeset
   447
val weak_dnf_conv = Simplifier.rewrite (HOL_basic_ss addsimps @{thms weak_dnf_simps});
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   448
val initial_conv =
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   449
    Simplifier.rewrite
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   450
     (HOL_basic_ss addsimps nnf_simps
35410
1ea89d2a1bd4 more antiquotations;
wenzelm
parents: 35408
diff changeset
   451
       addsimps [not_all, not_ex]
1ea89d2a1bd4 more antiquotations;
wenzelm
parents: 35408
diff changeset
   452
       addsimps map (fn th => th RS sym) (@{thms ex_simps} @ @{thms all_simps}));
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   453
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   454
val specl = fold_rev (fn x => fn th => instantiate' [] [SOME x] (th RS spec));
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   455
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   456
val cTrp = @{cterm "Trueprop"};
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   457
val cConj = @{cterm "op &"};
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   458
val (cNot,false_tm) = (@{cterm "Not"}, @{cterm "False"});
23557
3fe7aea46633 HOLogic.conj_intr/elims;
wenzelm
parents: 23514
diff changeset
   459
val assume_Trueprop = mk_comb cTrp #> assume;
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   460
val list_mk_conj = list_mk_binop cConj;
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   461
val conjs = list_dest_binop cConj;
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   462
val mk_neg = mk_comb cNot;
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   463
25251
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   464
fun striplist dest = 
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   465
 let
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   466
  fun h acc x = case try dest x of
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   467
    SOME (a,b) => h (h acc b) a
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   468
  | NONE => x::acc
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   469
 in h [] end;
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   470
fun list_mk_binop b = foldr1 (fn (s,t) => Thm.capply (Thm.capply b s) t);
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   471
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   472
val eq_commute = mk_meta_eq @{thm eq_commute};
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   473
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   474
fun sym_conv eq = 
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   475
 let val (l,r) = Thm.dest_binop eq
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   476
 in instantiate' [SOME (ctyp_of_term l)] [SOME l, SOME r] eq_commute
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   477
 end;
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   478
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   479
  (* FIXME : copied from cqe.ML -- complex QE*)
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   480
fun conjuncts ct =
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   481
 case term_of ct of
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   482
  @{term "op &"}$_$_ => (Thm.dest_arg1 ct)::(conjuncts (Thm.dest_arg ct))
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   483
| _ => [ct];
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   484
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   485
fun fold1 f = foldr1 (uncurry f);
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   486
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   487
val list_conj = fold1 (fn c => fn c' => Thm.capply (Thm.capply @{cterm "op &"} c) c') ;
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   488
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   489
fun mk_conj_tab th = 
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   490
 let fun h acc th = 
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   491
   case prop_of th of
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   492
   @{term "Trueprop"}$(@{term "op &"}$p$q) => 
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   493
     h (h acc (th RS conjunct2)) (th RS conjunct1)
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   494
  | @{term "Trueprop"}$p => (p,th)::acc
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   495
in fold (Termtab.insert Thm.eq_thm) (h [] th) Termtab.empty end;
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   496
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   497
fun is_conj (@{term "op &"}$_$_) = true
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   498
  | is_conj _ = false;
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   499
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   500
fun prove_conj tab cjs = 
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   501
 case cjs of 
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   502
   [c] => if is_conj (term_of c) then prove_conj tab (conjuncts c) else tab c
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   503
 | c::cs => conjI OF [prove_conj tab [c], prove_conj tab cs];
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   504
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   505
fun conj_ac_rule eq = 
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   506
 let 
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   507
  val (l,r) = Thm.dest_equals eq
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   508
  val ctabl = mk_conj_tab (assume (Thm.capply @{cterm Trueprop} l))
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   509
  val ctabr = mk_conj_tab (assume (Thm.capply @{cterm Trueprop} r))
33035
15eab423e573 standardized basic operations on type option;
wenzelm
parents: 33029
diff changeset
   510
  fun tabl c = the (Termtab.lookup ctabl (term_of c))
15eab423e573 standardized basic operations on type option;
wenzelm
parents: 33029
diff changeset
   511
  fun tabr c = the (Termtab.lookup ctabr (term_of c))
25251
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   512
  val thl  = prove_conj tabl (conjuncts r) |> implies_intr_hyps
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   513
  val thr  = prove_conj tabr (conjuncts l) |> implies_intr_hyps
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   514
  val eqI = instantiate' [] [SOME l, SOME r] @{thm iffI}
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   515
 in implies_elim (implies_elim eqI thl) thr |> mk_meta_eq end;
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   516
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   517
 (* END FIXME.*)
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   518
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   519
   (* Conversion for the equivalence of existential statements where 
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   520
      EX quantifiers are rearranged differently *)
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   521
 fun ext T = cterm_rule (instantiate' [SOME T] []) @{cpat Ex}
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   522
 fun mk_ex v t = Thm.capply (ext (ctyp_of_term v)) (Thm.cabs v t)
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   523
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   524
fun choose v th th' = case concl_of th of 
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   525
  @{term Trueprop} $ (Const("Ex",_)$_) => 
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   526
   let
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   527
    val p = (funpow 2 Thm.dest_arg o cprop_of) th
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   528
    val T = (hd o Thm.dest_ctyp o ctyp_of_term) p
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   529
    val th0 = fconv_rule (Thm.beta_conversion true)
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   530
        (instantiate' [SOME T] [SOME p, (SOME o Thm.dest_arg o cprop_of) th'] exE)
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   531
    val pv = (Thm.rhs_of o Thm.beta_conversion true) 
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   532
          (Thm.capply @{cterm Trueprop} (Thm.capply p v))
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   533
    val th1 = forall_intr v (implies_intr pv th')
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   534
   in implies_elim (implies_elim th0 th) th1  end
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   535
| _ => error ""
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   536
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   537
fun simple_choose v th = 
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   538
   choose v (assume ((Thm.capply @{cterm Trueprop} o mk_ex v) ((Thm.dest_arg o hd o #hyps o Thm.crep_thm) th))) th
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   539
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   540
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   541
 fun mkexi v th = 
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   542
  let 
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   543
   val p = Thm.cabs v (Thm.dest_arg (Thm.cprop_of th))
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   544
  in implies_elim 
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   545
    (fconv_rule (Thm.beta_conversion true) (instantiate' [SOME (ctyp_of_term v)] [SOME p, SOME v] @{thm exI}))
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   546
      th
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   547
  end
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   548
 fun ex_eq_conv t = 
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   549
  let 
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   550
  val (p0,q0) = Thm.dest_binop t
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   551
  val (vs',P) = strip_exists p0 
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   552
  val (vs,_) = strip_exists q0 
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   553
   val th = assume (Thm.capply @{cterm Trueprop} P)
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   554
   val th1 =  implies_intr_hyps (fold simple_choose vs' (fold mkexi vs th))
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   555
   val th2 =  implies_intr_hyps (fold simple_choose vs (fold mkexi vs' th))
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   556
   val p = (Thm.dest_arg o Thm.dest_arg1 o cprop_of) th1
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   557
   val q = (Thm.dest_arg o Thm.dest_arg o cprop_of) th1
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   558
  in implies_elim (implies_elim (instantiate' [] [SOME p, SOME q] iffI) th1) th2
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   559
     |> mk_meta_eq
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   560
  end;
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   561
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   562
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   563
 fun getname v = case term_of v of 
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   564
  Free(s,_) => s
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   565
 | Var ((s,_),_) => s
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   566
 | _ => "x"
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   567
 fun mk_eq s t = Thm.capply (Thm.capply @{cterm "op == :: bool => _"} s) t
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   568
 fun mkeq s t = Thm.capply @{cterm Trueprop} (Thm.capply (Thm.capply @{cterm "op = :: bool => _"} s) t)
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   569
 fun mk_exists v th = arg_cong_rule (ext (ctyp_of_term v))
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   570
   (Thm.abstract_rule (getname v) v th)
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   571
 val simp_ex_conv = 
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   572
     Simplifier.rewrite (HOL_basic_ss addsimps @{thms simp_thms(39)})
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   573
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   574
fun frees t = Thm.add_cterm_frees t [];
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   575
fun free_in v t = member op aconvc (frees t) v;
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   576
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   577
val vsubst = let
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   578
 fun vsubst (t,v) tm =  
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   579
   (Thm.rhs_of o Thm.beta_conversion false) (Thm.capply (Thm.cabs v tm) t)
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   580
in fold vsubst end;
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   581
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   582
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   583
(** main **)
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   584
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   585
fun ring_and_ideal_conv
30866
dd5117e2d73e now deals with devision in fields
chaieb
parents: 29800
diff changeset
   586
  {vars, semiring = (sr_ops, sr_rules), ring = (r_ops, r_rules), 
dd5117e2d73e now deals with devision in fields
chaieb
parents: 29800
diff changeset
   587
   field = (f_ops, f_rules), idom, ideal}
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   588
  dest_const mk_const ring_eq_conv ring_normalize_conv =
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   589
let
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   590
  val [add_pat, mul_pat, pow_pat, zero_tm, one_tm] = sr_ops;
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   591
  val [ring_add_tm, ring_mul_tm, ring_pow_tm] =
25251
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   592
    map dest_fun2 [add_pat, mul_pat, pow_pat];
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   593
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   594
  val (ring_sub_tm, ring_neg_tm) =
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   595
    (case r_ops of
30866
dd5117e2d73e now deals with devision in fields
chaieb
parents: 29800
diff changeset
   596
     [sub_pat, neg_pat] => (dest_fun2 sub_pat, dest_fun neg_pat)
dd5117e2d73e now deals with devision in fields
chaieb
parents: 29800
diff changeset
   597
    |_  => (@{cterm "True"}, @{cterm "True"}));
dd5117e2d73e now deals with devision in fields
chaieb
parents: 29800
diff changeset
   598
dd5117e2d73e now deals with devision in fields
chaieb
parents: 29800
diff changeset
   599
  val (field_div_tm, field_inv_tm) =
dd5117e2d73e now deals with devision in fields
chaieb
parents: 29800
diff changeset
   600
    (case f_ops of
dd5117e2d73e now deals with devision in fields
chaieb
parents: 29800
diff changeset
   601
       [div_pat, inv_pat] => (dest_fun2 div_pat, dest_fun inv_pat)
dd5117e2d73e now deals with devision in fields
chaieb
parents: 29800
diff changeset
   602
     | _ => (@{cterm "True"}, @{cterm "True"}));
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   603
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   604
  val [idom_thm, neq_thm] = idom;
25251
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   605
  val [idl_sub, idl_add0] = 
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   606
     if length ideal = 2 then ideal else [eq_commute, eq_commute]
30866
dd5117e2d73e now deals with devision in fields
chaieb
parents: 29800
diff changeset
   607
  fun ring_dest_neg t =
dd5117e2d73e now deals with devision in fields
chaieb
parents: 29800
diff changeset
   608
    let val (l,r) = dest_comb t 
dd5117e2d73e now deals with devision in fields
chaieb
parents: 29800
diff changeset
   609
    in if Term.could_unify(term_of l,term_of ring_neg_tm) then r 
dd5117e2d73e now deals with devision in fields
chaieb
parents: 29800
diff changeset
   610
       else raise CTERM ("ring_dest_neg", [t])
dd5117e2d73e now deals with devision in fields
chaieb
parents: 29800
diff changeset
   611
    end
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   612
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   613
 val ring_mk_neg = fn tm => mk_comb (ring_neg_tm) (tm);
30866
dd5117e2d73e now deals with devision in fields
chaieb
parents: 29800
diff changeset
   614
 fun field_dest_inv t =
25251
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   615
    let val (l,r) = dest_comb t in
30866
dd5117e2d73e now deals with devision in fields
chaieb
parents: 29800
diff changeset
   616
        if Term.could_unify(term_of l, term_of field_inv_tm) then r 
dd5117e2d73e now deals with devision in fields
chaieb
parents: 29800
diff changeset
   617
        else raise CTERM ("field_dest_inv", [t])
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   618
    end
25251
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   619
 val ring_dest_add = dest_binary ring_add_tm;
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   620
 val ring_mk_add = mk_binop ring_add_tm;
25251
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   621
 val ring_dest_sub = dest_binary ring_sub_tm;
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   622
 val ring_mk_sub = mk_binop ring_sub_tm;
25251
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   623
 val ring_dest_mul = dest_binary ring_mul_tm;
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   624
 val ring_mk_mul = mk_binop ring_mul_tm;
30866
dd5117e2d73e now deals with devision in fields
chaieb
parents: 29800
diff changeset
   625
 val field_dest_div = dest_binary field_div_tm;
dd5117e2d73e now deals with devision in fields
chaieb
parents: 29800
diff changeset
   626
 val field_mk_div = mk_binop field_div_tm;
25251
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   627
 val ring_dest_pow = dest_binary ring_pow_tm;
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   628
 val ring_mk_pow = mk_binop ring_pow_tm ;
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   629
 fun grobvars tm acc =
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   630
    if can dest_const tm then acc
25251
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   631
    else if can ring_dest_neg tm then grobvars (dest_arg tm) acc
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   632
    else if can ring_dest_pow tm then grobvars (dest_arg1 tm) acc
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   633
    else if can ring_dest_add tm orelse can ring_dest_sub tm
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   634
            orelse can ring_dest_mul tm
25251
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   635
    then grobvars (dest_arg1 tm) (grobvars (dest_arg tm) acc)
30866
dd5117e2d73e now deals with devision in fields
chaieb
parents: 29800
diff changeset
   636
    else if can field_dest_inv tm
dd5117e2d73e now deals with devision in fields
chaieb
parents: 29800
diff changeset
   637
         then
dd5117e2d73e now deals with devision in fields
chaieb
parents: 29800
diff changeset
   638
          let val gvs = grobvars (dest_arg tm) [] 
dd5117e2d73e now deals with devision in fields
chaieb
parents: 29800
diff changeset
   639
          in if null gvs then acc else tm::acc
dd5117e2d73e now deals with devision in fields
chaieb
parents: 29800
diff changeset
   640
          end
dd5117e2d73e now deals with devision in fields
chaieb
parents: 29800
diff changeset
   641
    else if can field_dest_div tm then
dd5117e2d73e now deals with devision in fields
chaieb
parents: 29800
diff changeset
   642
         let val lvs = grobvars (dest_arg1 tm) acc
dd5117e2d73e now deals with devision in fields
chaieb
parents: 29800
diff changeset
   643
             val gvs = grobvars (dest_arg tm) []
dd5117e2d73e now deals with devision in fields
chaieb
parents: 29800
diff changeset
   644
          in if null gvs then lvs else tm::acc
dd5117e2d73e now deals with devision in fields
chaieb
parents: 29800
diff changeset
   645
          end 
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   646
    else tm::acc ;
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   647
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   648
fun grobify_term vars tm =
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   649
((if not (member (op aconvc) vars tm) then raise CTERM ("Not a variable", [tm]) else
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   650
     [(rat_1,map (fn i => if i aconvc tm then 1 else 0) vars)])
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   651
handle  CTERM _ =>
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   652
 ((let val x = dest_const tm
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   653
 in if x =/ rat_0 then [] else [(x,map (fn v => 0) vars)]
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   654
 end)
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   655
 handle ERROR _ =>
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   656
  ((grob_neg(grobify_term vars (ring_dest_neg tm)))
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   657
  handle CTERM _ =>
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   658
   (
30866
dd5117e2d73e now deals with devision in fields
chaieb
parents: 29800
diff changeset
   659
   (grob_inv(grobify_term vars (field_dest_inv tm)))
dd5117e2d73e now deals with devision in fields
chaieb
parents: 29800
diff changeset
   660
   handle CTERM _ => 
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   661
    ((let val (l,r) = ring_dest_add tm
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   662
    in grob_add (grobify_term vars l) (grobify_term vars r)
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   663
    end)
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   664
    handle CTERM _ =>
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   665
     ((let val (l,r) = ring_dest_sub tm
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   666
     in grob_sub (grobify_term vars l) (grobify_term vars r)
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   667
     end)
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   668
     handle  CTERM _ =>
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   669
      ((let val (l,r) = ring_dest_mul tm
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   670
      in grob_mul (grobify_term vars l) (grobify_term vars r)
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   671
      end)
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   672
       handle CTERM _ =>
30866
dd5117e2d73e now deals with devision in fields
chaieb
parents: 29800
diff changeset
   673
        (  (let val (l,r) = field_dest_div tm
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   674
          in grob_div (grobify_term vars l) (grobify_term vars r)
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   675
          end)
30866
dd5117e2d73e now deals with devision in fields
chaieb
parents: 29800
diff changeset
   676
         handle CTERM _ =>
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   677
          ((let val (l,r) = ring_dest_pow tm
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   678
          in grob_pow vars (grobify_term vars l) ((term_of #> HOLogic.dest_number #> snd) r)
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   679
          end)
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   680
           handle CTERM _ => error "grobify_term: unknown or invalid term")))))))));
25251
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   681
val eq_tm = idom_thm |> concl |> dest_arg |> dest_arg |> dest_fun2;
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   682
val dest_eq = dest_binary eq_tm;
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   683
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   684
fun grobify_equation vars tm =
25251
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   685
    let val (l,r) = dest_binary eq_tm tm
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   686
    in grob_sub (grobify_term vars l) (grobify_term vars r)
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   687
    end;
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   688
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   689
fun grobify_equations tm =
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   690
 let
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   691
  val cjs = conjs tm
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   692
  val  rawvars = fold_rev (fn eq => fn a =>
25251
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   693
                                       grobvars (dest_arg1 eq) (grobvars (dest_arg eq) a)) cjs []
35408
b48ab741683b modernized structure Term_Ord;
wenzelm
parents: 33957
diff changeset
   694
  val vars = sort (fn (x, y) => Term_Ord.term_ord(term_of x,term_of y))
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   695
                  (distinct (op aconvc) rawvars)
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   696
 in (vars,map (grobify_equation vars) cjs)
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   697
 end;
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   698
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   699
val holify_polynomial =
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   700
 let fun holify_varpow (v,n) =
37388
793618618f78 tuned quotes, antiquotations and whitespace
haftmann
parents: 36752
diff changeset
   701
  if n = 1 then v else ring_mk_pow v (Numeral.mk_cnumber @{ctyp nat} n)  (* FIXME *)
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   702
 fun holify_monomial vars (c,m) =
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 23579
diff changeset
   703
  let val xps = map holify_varpow (filter (fn (_,n) => n <> 0) (vars ~~ m))
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   704
   in end_itlist ring_mk_mul (mk_const c :: xps)
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   705
  end
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   706
 fun holify_polynomial vars p =
23579
1a8ca0e480cd avoid polymorphic equality;
wenzelm
parents: 23557
diff changeset
   707
     if null p then mk_const (rat_0)
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   708
     else end_itlist ring_mk_add (map (holify_monomial vars) p)
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   709
 in holify_polynomial
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   710
 end ;
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   711
val idom_rule = simplify (HOL_basic_ss addsimps [idom_thm]);
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   712
fun prove_nz n = eqF_elim
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   713
                 (ring_eq_conv(mk_binop eq_tm (mk_const n) (mk_const(rat_0))));
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   714
val neq_01 = prove_nz (rat_1);
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   715
fun neq_rule n th = [prove_nz n, th] MRS neq_thm;
25251
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   716
fun mk_add th1 = combination(arg_cong_rule ring_add_tm th1);
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   717
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   718
fun refute tm =
23557
3fe7aea46633 HOLogic.conj_intr/elims;
wenzelm
parents: 23514
diff changeset
   719
 if tm aconvc false_tm then assume_Trueprop tm else
29800
f73a68c9d810 Now catch ERROR exception thrown by find and friends
chaieb
parents: 29269
diff changeset
   720
 ((let
23557
3fe7aea46633 HOLogic.conj_intr/elims;
wenzelm
parents: 23514
diff changeset
   721
   val (nths0,eths0) = List.partition (is_neg o concl) (HOLogic.conj_elims (assume_Trueprop tm))
25251
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   722
   val  nths = filter (is_eq o dest_arg o concl) nths0
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   723
   val eths = filter (is_eq o concl) eths0
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   724
  in
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   725
   if null eths then
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   726
    let
23557
3fe7aea46633 HOLogic.conj_intr/elims;
wenzelm
parents: 23514
diff changeset
   727
      val th1 = end_itlist (fn th1 => fn th2 => idom_rule(HOLogic.conj_intr th1 th2)) nths
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   728
      val th2 = Conv.fconv_rule
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   729
                ((arg_conv #> arg_conv)
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   730
                     (binop_conv ring_normalize_conv)) th1
25251
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   731
      val conc = th2 |> concl |> dest_arg
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   732
      val (l,r) = conc |> dest_eq
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   733
    in implies_intr (mk_comb cTrp tm)
25251
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   734
                    (equal_elim (arg_cong_rule cTrp (eqF_intr th2))
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   735
                           (reflexive l |> mk_object_eq))
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   736
    end
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   737
   else
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   738
   let
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   739
    val (vars,l,cert,noteqth) =(
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   740
     if null nths then
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   741
      let val (vars,pols) = grobify_equations(list_mk_conj(map concl eths))
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   742
          val (l,cert) = grobner_weak vars pols
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   743
      in (vars,l,cert,neq_01)
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   744
      end
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   745
     else
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   746
      let
23557
3fe7aea46633 HOLogic.conj_intr/elims;
wenzelm
parents: 23514
diff changeset
   747
       val nth = end_itlist (fn th1 => fn th2 => idom_rule(HOLogic.conj_intr th1 th2)) nths
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   748
       val (vars,pol::pols) =
25251
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   749
          grobify_equations(list_mk_conj(dest_arg(concl nth)::map concl eths))
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   750
       val (deg,l,cert) = grobner_strong vars pols pol
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   751
       val th1 = Conv.fconv_rule((arg_conv o arg_conv)(binop_conv ring_normalize_conv)) nth
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 23579
diff changeset
   752
       val th2 = funpow deg (idom_rule o HOLogic.conj_intr th1) neq_01
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   753
      in (vars,l,cert,th2)
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   754
      end)
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   755
    val cert_pos = map (fn (i,p) => (i,filter (fn (c,m) => c >/ rat_0) p)) cert
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   756
    val cert_neg = map (fn (i,p) => (i,map (fn (c,m) => (minus_rat c,m))
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   757
                                            (filter (fn (c,m) => c </ rat_0) p))) cert
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   758
    val  herts_pos = map (fn (i,p) => (i,holify_polynomial vars p)) cert_pos
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   759
    val  herts_neg = map (fn (i,p) => (i,holify_polynomial vars p)) cert_neg
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   760
    fun thm_fn pols =
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   761
        if null pols then reflexive(mk_const rat_0) else
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   762
        end_itlist mk_add
25251
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   763
            (map (fn (i,p) => arg_cong_rule (mk_comb ring_mul_tm p)
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 23579
diff changeset
   764
              (nth eths i |> mk_meta_eq)) pols)
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   765
    val th1 = thm_fn herts_pos
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   766
    val th2 = thm_fn herts_neg
23557
3fe7aea46633 HOLogic.conj_intr/elims;
wenzelm
parents: 23514
diff changeset
   767
    val th3 = HOLogic.conj_intr(mk_add (symmetric th1) th2 |> mk_object_eq) noteqth
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   768
    val th4 = Conv.fconv_rule ((arg_conv o arg_conv o binop_conv) ring_normalize_conv)
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   769
                               (neq_rule l th3)
25251
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   770
    val (l,r) = dest_eq(dest_arg(concl th4))
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   771
   in implies_intr (mk_comb cTrp tm)
25251
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   772
                        (equal_elim (arg_cong_rule cTrp (eqF_intr th4))
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   773
                   (reflexive l |> mk_object_eq))
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   774
   end
29800
f73a68c9d810 Now catch ERROR exception thrown by find and friends
chaieb
parents: 29269
diff changeset
   775
  end) handle ERROR _ => raise CTERM ("Gorbner-refute: unable to refute",[tm]))
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   776
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   777
fun ring tm =
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   778
 let
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   779
  fun mk_forall x p =
25251
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   780
      mk_comb (cterm_rule (instantiate' [SOME (ctyp_of_term x)] []) @{cpat "All:: (?'a => bool) => _"}) (cabs x p)
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   781
  val avs = add_cterm_frees tm []
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   782
  val P' = fold mk_forall avs tm
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   783
  val th1 = initial_conv(mk_neg P')
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   784
  val (evs,bod) = strip_exists(concl th1) in
25251
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   785
   if is_forall bod then raise CTERM("ring: non-universal formula",[tm])
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   786
   else
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   787
   let
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   788
    val th1a = weak_dnf_conv bod
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   789
    val boda = concl th1a
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   790
    val th2a = refute_disj refute boda
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   791
    val th2b = [mk_object_eq th1a, (th2a COMP notI) COMP PFalse'] MRS trans
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   792
    val th2 = fold (fn v => fn th => (forall_intr v th) COMP allI) evs (th2b RS PFalse)
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   793
    val th3 = equal_elim
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   794
                (Simplifier.rewrite (HOL_basic_ss addsimps [not_ex RS sym])
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   795
                          (th2 |> cprop_of)) th2
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   796
    in specl avs
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   797
             ([[[mk_object_eq th1, th3 RS PFalse'] MRS trans] MRS PFalse] MRS notnotD)
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   798
   end
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   799
 end
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   800
fun ideal tms tm ord =
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   801
 let
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   802
  val rawvars = fold_rev grobvars (tm::tms) []
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   803
  val vars = sort ord (distinct (fn (x,y) => (term_of x) aconv (term_of y)) rawvars)
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   804
  val pols = map (grobify_term vars) tms
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   805
  val pol = grobify_term vars tm
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   806
  val cert = grobner_ideal vars pols pol
33063
4d462963a7db map_range (and map_index) combinator
haftmann
parents: 33050
diff changeset
   807
 in map_range (fn n => these (AList.lookup (op =) cert n) |> holify_polynomial vars)
4d462963a7db map_range (and map_index) combinator
haftmann
parents: 33050
diff changeset
   808
   (length pols)
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   809
 end
25251
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   810
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   811
fun poly_eq_conv t = 
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   812
 let val (a,b) = Thm.dest_binop t
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   813
 in fconv_rule (arg_conv (arg1_conv ring_normalize_conv)) 
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   814
     (instantiate' [] [SOME a, SOME b] idl_sub)
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   815
 end
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   816
 val poly_eq_simproc = 
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   817
  let 
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   818
   fun proc phi  ss t = 
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   819
    let val th = poly_eq_conv t
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   820
    in if Thm.is_reflexive th then NONE else SOME th
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   821
    end
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   822
   in make_simproc {lhss = [Thm.lhs_of idl_sub], 
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   823
                name = "poly_eq_simproc", proc = proc, identifier = []}
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   824
   end;
35410
1ea89d2a1bd4 more antiquotations;
wenzelm
parents: 35408
diff changeset
   825
  val poly_eq_ss = HOL_basic_ss addsimps @{thms simp_thms}
25251
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   826
                        addsimprocs [poly_eq_simproc]
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   827
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   828
 local
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   829
  fun is_defined v t =
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   830
  let 
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   831
   val mons = striplist(dest_binary ring_add_tm) t 
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   832
  in member (op aconvc) mons v andalso 
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   833
    forall (fn m => v aconvc m 
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   834
          orelse not(member (op aconvc) (Thm.add_cterm_frees m []) v)) mons
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   835
  end
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   836
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   837
  fun isolate_variable vars tm =
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   838
  let 
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   839
   val th = poly_eq_conv tm
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   840
   val th' = (sym_conv then_conv poly_eq_conv) tm
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   841
   val (v,th1) = 
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   842
   case find_first(fn v=> is_defined v (Thm.dest_arg1 (Thm.rhs_of th))) vars of
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   843
    SOME v => (v,th')
33035
15eab423e573 standardized basic operations on type option;
wenzelm
parents: 33029
diff changeset
   844
   | NONE => (the (find_first 
25251
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   845
          (fn v => is_defined v (Thm.dest_arg1 (Thm.rhs_of th'))) vars) ,th)
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   846
   val th2 = transitive th1 
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   847
        (instantiate' []  [(SOME o Thm.dest_arg1 o Thm.rhs_of) th1, SOME v] 
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   848
          idl_add0)
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   849
   in fconv_rule(funpow 2 arg_conv ring_normalize_conv) th2
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   850
   end
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   851
 in
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   852
 fun unwind_polys_conv tm =
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   853
 let 
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   854
  val (vars,bod) = strip_exists tm
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   855
  val cjs = striplist (dest_binary @{cterm "op &"}) bod
33035
15eab423e573 standardized basic operations on type option;
wenzelm
parents: 33029
diff changeset
   856
  val th1 = (the (get_first (try (isolate_variable vars)) cjs) 
25251
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   857
             handle Option => raise CTERM ("unwind_polys_conv",[tm]))
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   858
  val eq = Thm.lhs_of th1
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   859
  val bod' = list_mk_binop @{cterm "op &"} (eq::(remove op aconvc eq cjs))
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   860
  val th2 = conj_ac_rule (mk_eq bod bod')
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   861
  val th3 = transitive th2 
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   862
         (Drule.binop_cong_rule @{cterm "op &"} th1 
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   863
                (reflexive (Thm.dest_arg (Thm.rhs_of th2))))
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   864
  val v = Thm.dest_arg1(Thm.dest_arg1(Thm.rhs_of th3))
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   865
  val vars' = (remove op aconvc v vars) @ [v]
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   866
  val th4 = fconv_rule (arg_conv simp_ex_conv) (mk_exists v th3)
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   867
  val th5 = ex_eq_conv (mk_eq tm (fold mk_ex (remove op aconvc v vars) (Thm.lhs_of th4)))
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   868
 in transitive th5 (fold mk_exists (remove op aconvc v vars) th4)
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   869
 end;
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   870
end
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   871
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   872
local
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   873
 fun scrub_var v m =
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   874
  let 
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   875
   val ps = striplist ring_dest_mul m 
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   876
   val ps' = remove op aconvc v ps
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   877
  in if null ps' then one_tm else fold1 ring_mk_mul ps'
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   878
  end
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   879
 fun find_multipliers v mons =
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   880
  let 
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   881
   val mons1 = filter (fn m => free_in v m) mons 
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   882
   val mons2 = map (scrub_var v) mons1 
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   883
   in  if null mons2 then zero_tm else fold1 ring_mk_add mons2
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   884
  end
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   885
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   886
 fun isolate_monomials vars tm =
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   887
 let 
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   888
  val (cmons,vmons) =
33049
c38f02fdf35d curried inter as canonical list operation (beware of argument order)
haftmann
parents: 33039
diff changeset
   889
    List.partition (fn m => null (inter (op aconvc) vars (frees m)))
25251
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   890
                   (striplist ring_dest_add tm)
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   891
  val cofactors = map (fn v => find_multipliers v vmons) vars
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   892
  val cnc = if null cmons then zero_tm
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   893
             else Thm.capply ring_neg_tm
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   894
                    (list_mk_binop ring_add_tm cmons) 
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   895
  in (cofactors,cnc)
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   896
  end;
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   897
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   898
fun isolate_variables evs ps eq =
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   899
 let 
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   900
  val vars = filter (fn v => free_in v eq) evs
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   901
  val (qs,p) = isolate_monomials vars eq
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   902
  val rs = ideal (qs @ ps) p 
35408
b48ab741683b modernized structure Term_Ord;
wenzelm
parents: 33957
diff changeset
   903
              (fn (s,t) => Term_Ord.term_ord (term_of s, term_of t))
33957
e9afca2118d4 normalized uncurry take/drop
haftmann
parents: 33955
diff changeset
   904
 in (eq, take (length qs) rs ~~ vars)
25251
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   905
 end;
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   906
 fun subst_in_poly i p = Thm.rhs_of (ring_normalize_conv (vsubst i p));
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   907
in
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   908
 fun solve_idealism evs ps eqs =
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   909
  if null evs then [] else
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   910
  let 
33035
15eab423e573 standardized basic operations on type option;
wenzelm
parents: 33029
diff changeset
   911
   val (eq,cfs) = get_first (try (isolate_variables evs ps)) eqs |> the
25251
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   912
   val evs' = subtract op aconvc evs (map snd cfs)
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   913
   val eqs' = map (subst_in_poly cfs) (remove op aconvc eq eqs)
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   914
  in cfs @ solve_idealism evs' ps eqs'
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   915
  end;
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   916
end;
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   917
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   918
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   919
in {ring_conv = ring, simple_ideal = ideal, multi_ideal = solve_idealism, 
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   920
    poly_eq_ss = poly_eq_ss, unwind_conv = unwind_polys_conv}
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   921
end;
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   922
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   923
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   924
fun find_term bounds tm =
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   925
  (case term_of tm of
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   926
    Const ("op =", T) $ _ $ _ =>
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   927
      if domain_type T = HOLogic.boolT then find_args bounds tm
25251
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   928
      else dest_arg tm
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   929
  | Const ("Not", _) $ _ => find_term bounds (dest_arg tm)
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   930
  | Const ("All", _) $ _ => find_body bounds (dest_arg tm)
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   931
  | Const ("Ex", _) $ _ => find_body bounds (dest_arg tm)
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   932
  | Const ("op &", _) $ _ $ _ => find_args bounds tm
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   933
  | Const ("op |", _) $ _ $ _ => find_args bounds tm
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   934
  | Const ("op -->", _) $ _ $ _ => find_args bounds tm
25251
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   935
  | @{term "op ==>"} $_$_ => find_args bounds tm
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   936
  | Const("op ==",_)$_$_ => find_args bounds tm
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   937
  | @{term Trueprop}$_ => find_term bounds (dest_arg tm)
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   938
  | _ => raise TERM ("find_term", []))
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   939
and find_args bounds tm =
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   940
  let val (t, u) = Thm.dest_binop tm
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   941
  in (find_term bounds t handle TERM _ => find_term bounds u) end
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   942
and find_body bounds b =
25251
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   943
  let val (_, b') = dest_abs (SOME (Name.bound bounds)) b
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   944
  in find_term (bounds + 1) b' end;
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   945
25251
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   946
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   947
fun get_ring_ideal_convs ctxt form = 
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   948
 case try (find_term 0) form of
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   949
  NONE => NONE
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   950
| SOME tm =>
36752
cf558aeb35b0 delete Groebner_Basis directory -- only one file left
haftmann
parents: 36723
diff changeset
   951
  (case Semiring_Normalizer.match ctxt tm of
25251
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   952
    NONE => NONE
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   953
  | SOME (res as (theory, {is_const, dest_const, 
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   954
          mk_const, conv = ring_eq_conv})) =>
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   955
     SOME (ring_and_ideal_conv theory
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   956
          dest_const (mk_const (ctyp_of_term tm)) (ring_eq_conv ctxt)
36752
cf558aeb35b0 delete Groebner_Basis directory -- only one file left
haftmann
parents: 36723
diff changeset
   957
          (Semiring_Normalizer.semiring_normalize_wrapper ctxt res)))
25251
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   958
23579
1a8ca0e480cd avoid polymorphic equality;
wenzelm
parents: 23557
diff changeset
   959
fun ring_solve ctxt form =
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   960
  (case try (find_term 0 (* FIXME !? *)) form of
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   961
    NONE => reflexive form
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   962
  | SOME tm =>
36752
cf558aeb35b0 delete Groebner_Basis directory -- only one file left
haftmann
parents: 36723
diff changeset
   963
      (case Semiring_Normalizer.match ctxt tm of
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   964
        NONE => reflexive form
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   965
      | SOME (res as (theory, {is_const, dest_const, mk_const, conv = ring_eq_conv})) =>
25251
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   966
        #ring_conv (ring_and_ideal_conv theory
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   967
          dest_const (mk_const (ctyp_of_term tm)) (ring_eq_conv ctxt)
36752
cf558aeb35b0 delete Groebner_Basis directory -- only one file left
haftmann
parents: 36723
diff changeset
   968
          (Semiring_Normalizer.semiring_normalize_wrapper ctxt res)) form));
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
   969
36702
b455ebd63799 moved presimplification rules for algebraic methods into named thms functor
haftmann
parents: 36700
diff changeset
   970
fun presimplify ctxt add_thms del_thms = asm_full_simp_tac (Simplifier.context ctxt
b455ebd63799 moved presimplification rules for algebraic methods into named thms functor
haftmann
parents: 36700
diff changeset
   971
  (HOL_basic_ss addsimps (Algebra_Simplification.get ctxt) delsimps del_thms addsimps add_thms));
b455ebd63799 moved presimplification rules for algebraic methods into named thms functor
haftmann
parents: 36700
diff changeset
   972
23579
1a8ca0e480cd avoid polymorphic equality;
wenzelm
parents: 23557
diff changeset
   973
fun ring_tac add_ths del_ths ctxt =
35625
9c818cab0dd0 modernized structure Object_Logic;
wenzelm
parents: 35410
diff changeset
   974
  Object_Logic.full_atomize_tac
36702
b455ebd63799 moved presimplification rules for algebraic methods into named thms functor
haftmann
parents: 36700
diff changeset
   975
  THEN' presimplify ctxt add_ths del_ths
23579
1a8ca0e480cd avoid polymorphic equality;
wenzelm
parents: 23557
diff changeset
   976
  THEN' CSUBGOAL (fn (p, i) =>
35625
9c818cab0dd0 modernized structure Object_Logic;
wenzelm
parents: 35410
diff changeset
   977
    rtac (let val form = Object_Logic.dest_judgment p
25251
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   978
          in case get_ring_ideal_convs ctxt form of
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   979
           NONE => reflexive form
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   980
          | SOME thy => #ring_conv thy form
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   981
          end) i
23579
1a8ca0e480cd avoid polymorphic equality;
wenzelm
parents: 23557
diff changeset
   982
      handle TERM _ => no_tac
1a8ca0e480cd avoid polymorphic equality;
wenzelm
parents: 23557
diff changeset
   983
        | CTERM _ => no_tac
1a8ca0e480cd avoid polymorphic equality;
wenzelm
parents: 23557
diff changeset
   984
        | THM _ => no_tac);
23334
2443224cf6d7 Added algebra_tac; tuned;
chaieb
parents: 23330
diff changeset
   985
25251
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   986
local
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   987
 fun lhs t = case term_of t of
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   988
  Const("op =",_)$_$_ => Thm.dest_arg1 t
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   989
 | _=> raise CTERM ("ideal_tac - lhs",[t])
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   990
 fun exitac NONE = no_tac
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   991
   | exitac (SOME y) = rtac (instantiate' [SOME (ctyp_of_term y)] [NONE,SOME y] exI) 1
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   992
in 
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   993
fun ideal_tac add_ths del_ths ctxt = 
36702
b455ebd63799 moved presimplification rules for algebraic methods into named thms functor
haftmann
parents: 36700
diff changeset
   994
  presimplify ctxt add_ths del_ths
27671
f938cd3fa820 Tuned and corrected ideal_tac for algebra.
chaieb
parents: 25538
diff changeset
   995
 THEN'
25251
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   996
 CSUBGOAL (fn (p, i) =>
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   997
  case get_ring_ideal_convs ctxt p of
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   998
   NONE => no_tac
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
   999
 | SOME thy => 
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
  1000
  let
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
  1001
   fun poly_exists_tac {asms = asms, concl = concl, prems = prems,
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
  1002
            params = params, context = ctxt, schematics = scs} = 
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
  1003
    let
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
  1004
     val (evs,bod) = strip_exists (Thm.dest_arg concl)
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
  1005
     val ps = map_filter (try (lhs o Thm.dest_arg)) asms 
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
  1006
     val cfs = (map swap o #multi_ideal thy evs ps) 
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
  1007
                   (map Thm.dest_arg1 (conjuncts bod))
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
  1008
     val ws = map (exitac o AList.lookup op aconvc cfs) evs
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
  1009
    in EVERY (rev ws) THEN Method.insert_tac prems 1 
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
  1010
        THEN ring_tac add_ths del_ths ctxt 1
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
  1011
   end
27671
f938cd3fa820 Tuned and corrected ideal_tac for algebra.
chaieb
parents: 25538
diff changeset
  1012
  in  
f938cd3fa820 Tuned and corrected ideal_tac for algebra.
chaieb
parents: 25538
diff changeset
  1013
     clarify_tac @{claset} i 
35625
9c818cab0dd0 modernized structure Object_Logic;
wenzelm
parents: 35410
diff changeset
  1014
     THEN Object_Logic.full_atomize_tac i 
27671
f938cd3fa820 Tuned and corrected ideal_tac for algebra.
chaieb
parents: 25538
diff changeset
  1015
     THEN asm_full_simp_tac (Simplifier.context ctxt (#poly_eq_ss thy)) i 
f938cd3fa820 Tuned and corrected ideal_tac for algebra.
chaieb
parents: 25538
diff changeset
  1016
     THEN clarify_tac @{claset} i 
25251
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
  1017
     THEN (REPEAT (CONVERSION (#unwind_conv thy) i))
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
  1018
     THEN SUBPROOF poly_exists_tac ctxt i
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
  1019
  end
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
  1020
 handle TERM _ => no_tac
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
  1021
     | CTERM _ => no_tac
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
  1022
     | THM _ => no_tac); 
23252
67268bb40b21 Semiring normalization and Groebner Bases.
wenzelm
parents:
diff changeset
  1023
end;
25251
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
  1024
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
  1025
fun algebra_tac add_ths del_ths ctxt i = 
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
  1026
 ring_tac add_ths del_ths ctxt i ORELSE ideal_tac add_ths del_ths ctxt i
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
  1027
 
36723
b91ef008b560 moved method syntax here
haftmann
parents: 36713
diff changeset
  1028
local
b91ef008b560 moved method syntax here
haftmann
parents: 36713
diff changeset
  1029
b91ef008b560 moved method syntax here
haftmann
parents: 36713
diff changeset
  1030
fun keyword k = Scan.lift (Args.$$$ k -- Args.colon) >> K ()
b91ef008b560 moved method syntax here
haftmann
parents: 36713
diff changeset
  1031
val addN = "add"
b91ef008b560 moved method syntax here
haftmann
parents: 36713
diff changeset
  1032
val delN = "del"
b91ef008b560 moved method syntax here
haftmann
parents: 36713
diff changeset
  1033
val any_keyword = keyword addN || keyword delN
b91ef008b560 moved method syntax here
haftmann
parents: 36713
diff changeset
  1034
val thms = Scan.repeat (Scan.unless any_keyword Attrib.multi_thm) >> flat;
b91ef008b560 moved method syntax here
haftmann
parents: 36713
diff changeset
  1035
b91ef008b560 moved method syntax here
haftmann
parents: 36713
diff changeset
  1036
in
b91ef008b560 moved method syntax here
haftmann
parents: 36713
diff changeset
  1037
b91ef008b560 moved method syntax here
haftmann
parents: 36713
diff changeset
  1038
val algebra_method = ((Scan.optional (keyword addN |-- thms) []) -- 
b91ef008b560 moved method syntax here
haftmann
parents: 36713
diff changeset
  1039
   (Scan.optional (keyword delN |-- thms) [])) >>
b91ef008b560 moved method syntax here
haftmann
parents: 36713
diff changeset
  1040
  (fn (add_ths, del_ths) => fn ctxt =>
b91ef008b560 moved method syntax here
haftmann
parents: 36713
diff changeset
  1041
       SIMPLE_METHOD' (algebra_tac add_ths del_ths ctxt))
25251
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
  1042
759bffe1d416 (1) signatures updated according to normalizer_data.ML (added field ideal in entry);
chaieb
parents: 24913
diff changeset
  1043
end;
36723
b91ef008b560 moved method syntax here
haftmann
parents: 36713
diff changeset
  1044
b91ef008b560 moved method syntax here
haftmann
parents: 36713
diff changeset
  1045
end;