src/HOL/Decision_Procs/Reflective_Field.thy
author haftmann
Tue, 07 Feb 2017 22:15:06 +0100
changeset 64998 d51478d6aae4
parent 64962 bf41e1109db3
child 64999 f8f76a501d25
permissions -rw-r--r--
isabelle update_cartouches
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
64962
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
     1
(*  Title:      HOL/Decision_Procs/Reflective_Field.thy
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
     2
    Author:     Stefan Berghofer
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
     3
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
     4
Reducing equalities in fields to equalities in rings.
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
     5
*)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
     6
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
     7
theory Reflective_Field
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
     8
imports Commutative_Ring
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
     9
begin
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    10
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    11
datatype fexpr =
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    12
    FCnst int
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    13
  | FVar nat
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    14
  | FAdd fexpr fexpr
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    15
  | FSub fexpr fexpr
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    16
  | FMul fexpr fexpr
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    17
  | FNeg fexpr
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    18
  | FDiv fexpr fexpr
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    19
  | FPow fexpr nat
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    20
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    21
fun (in field) nth_el :: "'a list \<Rightarrow> nat \<Rightarrow> 'a" where
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    22
  "nth_el [] n = \<zero>"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    23
| "nth_el (x # xs) 0 = x"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    24
| "nth_el (x # xs) (Suc n) = nth_el xs n"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    25
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    26
lemma (in field) nth_el_Cons:
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    27
  "nth_el (x # xs) n = (if n = 0 then x else nth_el xs (n - 1))"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    28
  by (cases n) simp_all
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    29
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    30
lemma (in field) nth_el_closed [simp]:
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    31
  "in_carrier xs \<Longrightarrow> nth_el xs n \<in> carrier R"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    32
  by (induct xs n rule: nth_el.induct) (simp_all add: in_carrier_def)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    33
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    34
primrec (in field) feval :: "'a list \<Rightarrow> fexpr \<Rightarrow> 'a"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    35
where
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    36
  "feval xs (FCnst c) = \<guillemotleft>c\<guillemotright>"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    37
| "feval xs (FVar n) = nth_el xs n"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    38
| "feval xs (FAdd a b) = feval xs a \<oplus> feval xs b"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    39
| "feval xs (FSub a b) = feval xs a \<ominus> feval xs b"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    40
| "feval xs (FMul a b) = feval xs a \<otimes> feval xs b"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    41
| "feval xs (FNeg a) = \<ominus> feval xs a"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    42
| "feval xs (FDiv a b) = feval xs a \<oslash> feval xs b"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    43
| "feval xs (FPow a n) = feval xs a (^) n"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    44
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    45
lemma (in field) feval_Cnst:
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    46
  "feval xs (FCnst 0) = \<zero>"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    47
  "feval xs (FCnst 1) = \<one>"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    48
  "feval xs (FCnst (numeral n)) = \<guillemotleft>numeral n\<guillemotright>"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    49
  by simp_all
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    50
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    51
datatype pexpr =
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    52
    PExpr1 pexpr1
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    53
  | PExpr2 pexpr2
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    54
and pexpr1 =
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    55
    PCnst int
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    56
  | PVar nat
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    57
  | PAdd pexpr pexpr
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    58
  | PSub pexpr pexpr
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    59
  | PNeg pexpr
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    60
and pexpr2 =
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    61
    PMul pexpr pexpr
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    62
  | PPow pexpr nat
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    63
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    64
lemma pexpr_cases [case_names PCnst PVar PAdd PSub PNeg PMul PPow]:
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    65
  assumes
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    66
    "\<And>c. e = PExpr1 (PCnst c) \<Longrightarrow> P"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    67
    "\<And>n. e = PExpr1 (PVar n) \<Longrightarrow> P"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    68
    "\<And>e1 e2. e = PExpr1 (PAdd e1 e2) \<Longrightarrow> P"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    69
    "\<And>e1 e2. e = PExpr1 (PSub e1 e2) \<Longrightarrow> P"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    70
    "\<And>e'. e = PExpr1 (PNeg e') \<Longrightarrow> P"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    71
    "\<And>e1 e2. e = PExpr2 (PMul e1 e2) \<Longrightarrow> P"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    72
    "\<And>e' n. e = PExpr2 (PPow e' n) \<Longrightarrow> P"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    73
  shows P
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    74
proof (cases e)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    75
  case (PExpr1 e')
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    76
  then show ?thesis
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    77
    apply (cases e')
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    78
    apply simp_all
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    79
    apply (erule assms)+
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    80
    done
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    81
next
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    82
  case (PExpr2 e')
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    83
  then show ?thesis
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    84
    apply (cases e')
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    85
    apply simp_all
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    86
    apply (erule assms)+
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    87
    done
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    88
qed
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    89
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    90
lemmas pexpr_cases2 = pexpr_cases [case_product pexpr_cases]
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    91
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    92
fun (in field) peval :: "'a list \<Rightarrow> pexpr \<Rightarrow> 'a"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    93
where
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    94
  "peval xs (PExpr1 (PCnst c)) = \<guillemotleft>c\<guillemotright>"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    95
| "peval xs (PExpr1 (PVar n)) = nth_el xs n"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    96
| "peval xs (PExpr1 (PAdd a b)) = peval xs a \<oplus> peval xs b"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    97
| "peval xs (PExpr1 (PSub a b)) = peval xs a \<ominus> peval xs b"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    98
| "peval xs (PExpr1 (PNeg a)) = \<ominus> peval xs a"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
    99
| "peval xs (PExpr2 (PMul a b)) = peval xs a \<otimes> peval xs b"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   100
| "peval xs (PExpr2 (PPow a n)) = peval xs a (^) n"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   101
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   102
lemma (in field) peval_Cnst:
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   103
  "peval xs (PExpr1 (PCnst 0)) = \<zero>"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   104
  "peval xs (PExpr1 (PCnst 1)) = \<one>"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   105
  "peval xs (PExpr1 (PCnst (numeral n))) = \<guillemotleft>numeral n\<guillemotright>"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   106
  "peval xs (PExpr1 (PCnst (- numeral n))) = \<ominus> \<guillemotleft>numeral n\<guillemotright>"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   107
  by simp_all
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   108
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   109
lemma (in field) peval_closed [simp]:
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   110
  "in_carrier xs \<Longrightarrow> peval xs e \<in> carrier R"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   111
  "in_carrier xs \<Longrightarrow> peval xs (PExpr1 e1) \<in> carrier R"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   112
  "in_carrier xs \<Longrightarrow> peval xs (PExpr2 e2) \<in> carrier R"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   113
  by (induct e and e1 and e2) simp_all
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   114
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   115
definition npepow :: "pexpr \<Rightarrow> nat \<Rightarrow> pexpr"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   116
where
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   117
  "npepow e n =
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   118
     (if n = 0 then PExpr1 (PCnst 1)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   119
      else if n = 1 then e
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   120
      else (case e of
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   121
          PExpr1 (PCnst c) \<Rightarrow> PExpr1 (PCnst (c ^ n))
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   122
        | _ \<Rightarrow> PExpr2 (PPow e n)))"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   123
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   124
lemma (in field) npepow_correct:
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   125
  "in_carrier xs \<Longrightarrow> peval xs (npepow e n) = peval xs (PExpr2 (PPow e n))"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   126
  by (cases e rule: pexpr_cases)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   127
    (simp_all add: npepow_def)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   128
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   129
fun npemul :: "pexpr \<Rightarrow> pexpr \<Rightarrow> pexpr"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   130
where
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   131
  "npemul x y = (case x of
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   132
       PExpr1 (PCnst c) \<Rightarrow>
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   133
         if c = 0 then x
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   134
         else if c = 1 then y else
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   135
           (case y of
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   136
              PExpr1 (PCnst d) \<Rightarrow> PExpr1 (PCnst (c * d))
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   137
            | _ \<Rightarrow> PExpr2 (PMul x y))
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   138
     | PExpr2 (PPow e1 n) \<Rightarrow>
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   139
         (case y of
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   140
            PExpr2 (PPow e2 m) \<Rightarrow>
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   141
              if n = m then npepow (npemul e1 e2) n
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   142
              else PExpr2 (PMul x y)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   143
          | PExpr1 (PCnst d) \<Rightarrow>
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   144
              if d = 0 then y
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   145
              else if d = 1 then x
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   146
              else PExpr2 (PMul x y)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   147
          | _ \<Rightarrow> PExpr2 (PMul x y))
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   148
     | _ \<Rightarrow> (case y of
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   149
         PExpr1 (PCnst d) \<Rightarrow>
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   150
           if d = 0 then y
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   151
           else if d = 1 then x
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   152
           else PExpr2 (PMul x y)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   153
       | _ \<Rightarrow> PExpr2 (PMul x y)))"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   154
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   155
lemma (in field) npemul_correct:
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   156
  "in_carrier xs \<Longrightarrow> peval xs (npemul e1 e2) = peval xs (PExpr2 (PMul e1 e2))"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   157
proof (induct e1 e2 rule: npemul.induct)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   158
  case (1 x y)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   159
  then show ?case
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   160
  proof (cases x y rule: pexpr_cases2)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   161
    case (PPow_PPow e n e' m)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   162
    then show ?thesis
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   163
    by (simp add: 1 npepow_correct nat_pow_distr
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   164
      npemul.simps [of "PExpr2 (PPow e n)" "PExpr2 (PPow e' m)"]
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   165
      del: npemul.simps)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   166
  qed simp_all
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   167
qed
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   168
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   169
declare npemul.simps [simp del]
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   170
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   171
definition npeadd :: "pexpr \<Rightarrow> pexpr \<Rightarrow> pexpr"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   172
where
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   173
  "npeadd x y = (case x of
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   174
       PExpr1 (PCnst c) \<Rightarrow>
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   175
         if c = 0 then y else
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   176
           (case y of
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   177
              PExpr1 (PCnst d) \<Rightarrow> PExpr1 (PCnst (c + d))
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   178
            | _ \<Rightarrow> PExpr1 (PAdd x y))
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   179
     | _ \<Rightarrow> (case y of
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   180
         PExpr1 (PCnst d) \<Rightarrow>
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   181
           if d = 0 then x
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   182
           else PExpr1 (PAdd x y)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   183
       | _ \<Rightarrow> PExpr1 (PAdd x y)))"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   184
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   185
lemma (in field) npeadd_correct:
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   186
  "in_carrier xs \<Longrightarrow> peval xs (npeadd e1 e2) = peval xs (PExpr1 (PAdd e1 e2))"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   187
  by (cases e1 e2 rule: pexpr_cases2) (simp_all add: npeadd_def)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   188
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   189
definition npesub :: "pexpr \<Rightarrow> pexpr \<Rightarrow> pexpr"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   190
where
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   191
  "npesub x y = (case y of
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   192
       PExpr1 (PCnst d) \<Rightarrow>
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   193
         if d = 0 then x else
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   194
           (case x of
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   195
              PExpr1 (PCnst c) \<Rightarrow> PExpr1 (PCnst (c - d))
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   196
            | _ \<Rightarrow> PExpr1 (PSub x y))
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   197
     | _ \<Rightarrow> (case x of
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   198
         PExpr1 (PCnst c) \<Rightarrow>
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   199
           if c = 0 then PExpr1 (PNeg y)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   200
           else PExpr1 (PSub x y)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   201
       | _ \<Rightarrow> PExpr1 (PSub x y)))"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   202
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   203
lemma (in field) npesub_correct:
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   204
  "in_carrier xs \<Longrightarrow> peval xs (npesub e1 e2) = peval xs (PExpr1 (PSub e1 e2))"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   205
  by (cases e1 e2 rule: pexpr_cases2) (simp_all add: npesub_def)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   206
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   207
definition npeneg :: "pexpr \<Rightarrow> pexpr"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   208
where
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   209
  "npeneg e = (case e of
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   210
       PExpr1 (PCnst c) \<Rightarrow> PExpr1 (PCnst (- c))
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   211
     | _ \<Rightarrow> PExpr1 (PNeg e))"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   212
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   213
lemma (in field) npeneg_correct:
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   214
  "peval xs (npeneg e) = peval xs (PExpr1 (PNeg e))"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   215
  by (cases e rule: pexpr_cases) (simp_all add: npeneg_def)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   216
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   217
lemma option_pair_cases [case_names None Some]:
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   218
  assumes
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   219
    "x = None \<Longrightarrow> P"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   220
    "\<And>p q. x = Some (p, q) \<Longrightarrow> P"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   221
  shows P
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   222
proof (cases x)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   223
  case None
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   224
  then show ?thesis by (rule assms)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   225
next
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   226
  case (Some r)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   227
  then show ?thesis
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   228
    apply (cases r)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   229
    apply simp
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   230
    by (rule assms)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   231
qed
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   232
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   233
fun isin :: "pexpr \<Rightarrow> nat \<Rightarrow> pexpr \<Rightarrow> nat \<Rightarrow> (nat * pexpr) option"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   234
where
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   235
  "isin e n (PExpr2 (PMul e1 e2)) m =
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   236
     (case isin e n e1 m of
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   237
        Some (k, e3) \<Rightarrow>
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   238
          if k = 0 then Some (0, npemul e3 (npepow e2 m))
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   239
          else (case isin e k e2 m of
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   240
              Some (l, e4) \<Rightarrow> Some (l, npemul e3 e4)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   241
            | None \<Rightarrow> Some (k, npemul e3 (npepow e2 m)))
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   242
      | None \<Rightarrow> (case isin e n e2 m of
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   243
          Some (k, e3) \<Rightarrow> Some (k, npemul (npepow e1 m) e3)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   244
        | None \<Rightarrow> None))"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   245
| "isin e n (PExpr2 (PPow e' k)) m =
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   246
     (if k = 0 then None else isin e n e' (k * m))"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   247
| "isin (PExpr1 e) n (PExpr1 e') m =
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   248
     (if e = e' then
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   249
        if n >= m then Some (n - m, PExpr1 (PCnst 1))
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   250
        else Some (0, npepow (PExpr1 e) (m - n))
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   251
      else None)"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   252
| "isin (PExpr2 e) n (PExpr1 e') m = None"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   253
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   254
lemma (in field) isin_correct:
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   255
  assumes "in_carrier xs"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   256
  and "isin e n e' m = Some (p, e'')"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   257
  shows
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   258
    "peval xs (PExpr2 (PPow e' m)) =
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   259
     peval xs (PExpr2 (PMul (PExpr2 (PPow e (n - p))) e''))"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   260
    "p \<le> n"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   261
  using assms
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   262
  by (induct e n e' m arbitrary: p e'' rule: isin.induct)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   263
    (force
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   264
       simp add:
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   265
         nat_pow_distr nat_pow_pow nat_pow_mult m_ac
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   266
         npemul_correct npepow_correct
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   267
       split: option.split_asm prod.split_asm if_split_asm)+
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   268
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   269
lemma (in field) isin_correct':
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   270
  "in_carrier xs \<Longrightarrow> isin e n e' 1 = Some (p, e'') \<Longrightarrow>
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   271
   peval xs e' = peval xs e (^) (n - p) \<otimes> peval xs e''"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   272
  "in_carrier xs \<Longrightarrow> isin e n e' 1 = Some (p, e'') \<Longrightarrow> p \<le> n"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   273
  using isin_correct [where m=1]
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   274
  by simp_all
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   275
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   276
fun split_aux :: "pexpr \<Rightarrow> nat \<Rightarrow> pexpr \<Rightarrow> pexpr \<times> pexpr \<times> pexpr"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   277
where
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   278
  "split_aux (PExpr2 (PMul e1 e2)) n e =
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   279
     (let
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   280
        (left1, common1, right1) = split_aux e1 n e;
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   281
        (left2, common2, right2) = split_aux e2 n right1
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   282
      in (npemul left1 left2, npemul common1 common2, right2))"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   283
| "split_aux (PExpr2 (PPow e' m)) n e =
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   284
     (if m = 0 then (PExpr1 (PCnst 1), PExpr1 (PCnst 1), e)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   285
      else split_aux e' (m * n) e)"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   286
| "split_aux (PExpr1 e') n e =
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   287
     (case isin (PExpr1 e') n e 1 of
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   288
        Some (m, e'') \<Rightarrow>
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   289
          (if m = 0 then (PExpr1 (PCnst 1), npepow (PExpr1 e') n, e'')
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   290
           else (npepow (PExpr1 e') m, npepow (PExpr1 e') (n - m), e''))
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   291
      | None \<Rightarrow> (npepow (PExpr1 e') n, PExpr1 (PCnst 1), e))"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   292
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   293
hide_const Left Right
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   294
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   295
abbreviation Left :: "pexpr \<Rightarrow> pexpr \<Rightarrow> pexpr" where
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   296
  "Left e1 e2 \<equiv> fst (split_aux e1 (Suc 0) e2)"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   297
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   298
abbreviation Common :: "pexpr \<Rightarrow> pexpr \<Rightarrow> pexpr" where
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   299
  "Common e1 e2 \<equiv> fst (snd (split_aux e1 (Suc 0) e2))"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   300
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   301
abbreviation Right :: "pexpr \<Rightarrow> pexpr \<Rightarrow> pexpr" where
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   302
  "Right e1 e2 \<equiv> snd (snd (split_aux e1 (Suc 0) e2))"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   303
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   304
lemma split_aux_induct [case_names 1 2 3]:
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   305
  assumes I1: "\<And>e1 e2 n e. P e1 n e \<Longrightarrow> P e2 n (snd (snd (split_aux e1 n e))) \<Longrightarrow>
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   306
    P (PExpr2 (PMul e1 e2)) n e"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   307
  and I2: "\<And>e' m n e. (m \<noteq> 0 \<Longrightarrow> P e' (m * n) e) \<Longrightarrow> P (PExpr2 (PPow e' m)) n e"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   308
  and I3: "\<And>e' n e. P (PExpr1 e') n e"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   309
  shows "P x y z"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   310
proof (induct x y z rule: split_aux.induct)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   311
  case 1
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   312
  from 1(1) 1(2) [OF refl prod.collapse prod.collapse]
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   313
  show ?case by (rule I1)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   314
next
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   315
  case 2
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   316
  then show ?case by (rule I2)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   317
next
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   318
  case 3
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   319
  then show ?case by (rule I3)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   320
qed
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   321
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   322
lemma (in field) split_aux_correct:
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   323
  "in_carrier xs \<Longrightarrow>
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   324
   peval xs (PExpr2 (PPow e\<^sub>1 n)) =
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   325
   peval xs (PExpr2 (PMul (fst (split_aux e\<^sub>1 n e\<^sub>2)) (fst (snd (split_aux e\<^sub>1 n e\<^sub>2)))))"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   326
  "in_carrier xs \<Longrightarrow>
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   327
   peval xs e\<^sub>2 =
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   328
   peval xs (PExpr2 (PMul (snd (snd (split_aux e\<^sub>1 n e\<^sub>2))) (fst (snd (split_aux e\<^sub>1 n e\<^sub>2)))))"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   329
  by (induct e\<^sub>1 n e\<^sub>2 rule: split_aux_induct)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   330
    (auto simp add: split_beta
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   331
       nat_pow_distr nat_pow_pow nat_pow_mult m_ac
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   332
       npemul_correct npepow_correct isin_correct'
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   333
       split: option.split)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   334
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   335
lemma (in field) split_aux_correct':
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   336
  "in_carrier xs \<Longrightarrow>
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   337
   peval xs e\<^sub>1 = peval xs (Left e\<^sub>1 e\<^sub>2) \<otimes> peval xs (Common e\<^sub>1 e\<^sub>2)"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   338
  "in_carrier xs \<Longrightarrow>
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   339
   peval xs e\<^sub>2 = peval xs (Right e\<^sub>1 e\<^sub>2) \<otimes> peval xs (Common e\<^sub>1 e\<^sub>2)"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   340
  using split_aux_correct [where n=1]
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   341
  by simp_all
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   342
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   343
fun fnorm :: "fexpr \<Rightarrow> pexpr \<times> pexpr \<times> pexpr list"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   344
where
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   345
  "fnorm (FCnst c) = (PExpr1 (PCnst c), PExpr1 (PCnst 1), [])"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   346
| "fnorm (FVar n) = (PExpr1 (PVar n), PExpr1 (PCnst 1), [])"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   347
| "fnorm (FAdd e1 e2) =
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   348
     (let
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   349
        (xn, xd, xc) = fnorm e1;
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   350
        (yn, yd, yc) = fnorm e2;
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   351
        (left, common, right) = split_aux xd 1 yd
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   352
      in
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   353
        (npeadd (npemul xn right) (npemul yn left),
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   354
         npemul left (npemul right common),
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   355
         List.union xc yc))"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   356
| "fnorm (FSub e1 e2) =
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   357
     (let
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   358
        (xn, xd, xc) = fnorm e1;
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   359
        (yn, yd, yc) = fnorm e2;
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   360
        (left, common, right) = split_aux xd 1 yd
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   361
      in
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   362
        (npesub (npemul xn right) (npemul yn left),
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   363
         npemul left (npemul right common),
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   364
         List.union xc yc))"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   365
| "fnorm (FMul e1 e2) =
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   366
     (let
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   367
        (xn, xd, xc) = fnorm e1;
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   368
        (yn, yd, yc) = fnorm e2;
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   369
        (left1, common1, right1) = split_aux xn 1 yd;
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   370
        (left2, common2, right2) = split_aux yn 1 xd
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   371
      in
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   372
        (npemul left1 left2,
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   373
         npemul right2 right1,
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   374
         List.union xc yc))"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   375
| "fnorm (FNeg e) =
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   376
     (let (n, d, c) = fnorm e
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   377
      in (npeneg n, d, c))"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   378
| "fnorm (FDiv e1 e2) =
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   379
     (let
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   380
        (xn, xd, xc) = fnorm e1;
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   381
        (yn, yd, yc) = fnorm e2;
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   382
        (left1, common1, right1) = split_aux xn 1 yn;
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   383
        (left2, common2, right2) = split_aux xd 1 yd
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   384
      in
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   385
        (npemul left1 right2,
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   386
         npemul left2 right1,
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   387
         List.insert yn (List.union xc yc)))"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   388
| "fnorm (FPow e m) =
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   389
     (let (n, d, c) = fnorm e
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   390
      in (npepow n m, npepow d m, c))"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   391
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   392
abbreviation Num :: "fexpr \<Rightarrow> pexpr" where
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   393
  "Num e \<equiv> fst (fnorm e)"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   394
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   395
abbreviation Denom :: "fexpr \<Rightarrow> pexpr" where
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   396
  "Denom e \<equiv> fst (snd (fnorm e))"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   397
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   398
abbreviation Cond :: "fexpr \<Rightarrow> pexpr list" where
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   399
  "Cond e \<equiv> snd (snd (fnorm e))"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   400
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   401
primrec (in field) nonzero :: "'a list \<Rightarrow> pexpr list \<Rightarrow> bool"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   402
where
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   403
  "nonzero xs [] = True"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   404
| "nonzero xs (p # ps) = (peval xs p \<noteq> \<zero> \<and> nonzero xs ps)"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   405
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   406
lemma (in field) nonzero_singleton:
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   407
  "nonzero xs [p] = (peval xs p \<noteq> \<zero>)"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   408
  by simp
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   409
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   410
lemma (in field) nonzero_append:
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   411
  "nonzero xs (ps @ qs) = (nonzero xs ps \<and> nonzero xs qs)"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   412
  by (induct ps) simp_all
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   413
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   414
lemma (in field) nonzero_idempotent:
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   415
  "p \<in> set ps \<Longrightarrow> (peval xs p \<noteq> \<zero> \<and> nonzero xs ps) = nonzero xs ps"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   416
  by (induct ps) auto
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   417
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   418
lemma (in field) nonzero_insert:
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   419
  "nonzero xs (List.insert p ps) = (peval xs p \<noteq> \<zero> \<and> nonzero xs ps)"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   420
  by (simp add: List.insert_def nonzero_idempotent)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   421
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   422
lemma (in field) nonzero_union:
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   423
  "nonzero xs (List.union ps qs) = (nonzero xs ps \<and> nonzero xs qs)"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   424
  by (induct ps rule: rev_induct)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   425
    (auto simp add: List.union_def nonzero_insert nonzero_append)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   426
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   427
lemma (in field) fnorm_correct:
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   428
  assumes "in_carrier xs"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   429
  and "nonzero xs (Cond e)"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   430
  shows "feval xs e = peval xs (Num e) \<oslash> peval xs (Denom e)"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   431
  and "peval xs (Denom e) \<noteq> \<zero>"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   432
  using assms
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   433
proof (induct e)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   434
  case (FCnst c) {
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   435
    case 1
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   436
    show ?case by simp
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   437
  next
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   438
    case 2
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   439
    show ?case by simp
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   440
  }
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   441
next
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   442
  case (FVar n) {
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   443
    case 1
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   444
    then show ?case by simp
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   445
  next
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   446
    case 2
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   447
    show ?case by simp
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   448
  }
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   449
next
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   450
  case (FAdd e1 e2)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   451
  note split = split_aux_correct' [where xs=xs and
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   452
    e\<^sub>1="Denom e1" and e\<^sub>2="Denom e2"]
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   453
  {
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   454
    case 1
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   455
    let ?left = "peval xs (Left (Denom e1) (Denom e2))"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   456
    let ?common = "peval xs (Common (Denom e1) (Denom e2))"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   457
    let ?right = "peval xs (Right (Denom e1) (Denom e2))"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   458
    from 1 FAdd
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   459
    have "feval xs (FAdd e1 e2) =
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   460
      (?common \<otimes> (peval xs (Num e1) \<otimes> ?right \<oplus> peval xs (Num e2) \<otimes> ?left)) \<oslash>
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   461
      (?common \<otimes> (?left \<otimes> (?right \<otimes> ?common)))"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   462
      by (simp add: split_beta split nonzero_union
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   463
        add_frac_eq r_distr m_ac)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   464
    also from 1 FAdd have "\<dots> =
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   465
      peval xs (Num (FAdd e1 e2)) \<oslash> peval xs (Denom (FAdd e1 e2))"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   466
      by (simp add: split_beta split nonzero_union npeadd_correct npemul_correct integral_iff)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   467
    finally show ?case .
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   468
  next
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   469
    case 2
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   470
    with FAdd show ?case
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   471
      by (simp add: split_beta split nonzero_union npemul_correct integral_iff)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   472
  }
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   473
next
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   474
  case (FSub e1 e2)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   475
  note split = split_aux_correct' [where xs=xs and
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   476
    e\<^sub>1="Denom e1" and e\<^sub>2="Denom e2"]
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   477
  {
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   478
    case 1
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   479
    let ?left = "peval xs (Left (Denom e1) (Denom e2))"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   480
    let ?common = "peval xs (Common (Denom e1) (Denom e2))"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   481
    let ?right = "peval xs (Right (Denom e1) (Denom e2))"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   482
    from 1 FSub
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   483
    have "feval xs (FSub e1 e2) =
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   484
      (?common \<otimes> (peval xs (Num e1) \<otimes> ?right \<ominus> peval xs (Num e2) \<otimes> ?left)) \<oslash>
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   485
      (?common \<otimes> (?left \<otimes> (?right \<otimes> ?common)))"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   486
      by (simp add: split_beta split nonzero_union
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   487
        diff_frac_eq r_diff_distr m_ac)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   488
    also from 1 FSub have "\<dots> =
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   489
      peval xs (Num (FSub e1 e2)) \<oslash> peval xs (Denom (FSub e1 e2))"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   490
      by (simp add: split_beta split nonzero_union npesub_correct npemul_correct integral_iff)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   491
    finally show ?case .
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   492
  next
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   493
    case 2
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   494
    with FSub show ?case
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   495
      by (simp add: split_beta split nonzero_union npemul_correct integral_iff)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   496
  }
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   497
next
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   498
  case (FMul e1 e2)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   499
  note split =
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   500
    split_aux_correct' [where xs=xs and
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   501
      e\<^sub>1="Num e1" and e\<^sub>2="Denom e2"]
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   502
    split_aux_correct' [where xs=xs and
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   503
      e\<^sub>1="Num e2" and e\<^sub>2="Denom e1"]
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   504
  {
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   505
    case 1
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   506
    let ?left\<^sub>1 = "peval xs (Left (Num e1) (Denom e2))"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   507
    let ?common\<^sub>1 = "peval xs (Common (Num e1) (Denom e2))"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   508
    let ?right\<^sub>1 = "peval xs (Right (Num e1) (Denom e2))"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   509
    let ?left\<^sub>2 = "peval xs (Left (Num e2) (Denom e1))"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   510
    let ?common\<^sub>2 = "peval xs (Common (Num e2) (Denom e1))"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   511
    let ?right\<^sub>2 = "peval xs (Right (Num e2) (Denom e1))"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   512
    from 1 FMul
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   513
    have "feval xs (FMul e1 e2) =
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   514
      ((?common\<^sub>1 \<otimes> ?common\<^sub>2) \<otimes> (?left\<^sub>1 \<otimes> ?left\<^sub>2)) \<oslash>
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   515
      ((?common\<^sub>1 \<otimes> ?common\<^sub>2) \<otimes> (?right\<^sub>2 \<otimes> ?right\<^sub>1))"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   516
      by (simp add: split_beta split nonzero_union
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   517
        nonzero_divide_divide_eq_left m_ac)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   518
    also from 1 FMul have "\<dots> =
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   519
      peval xs (Num (FMul e1 e2)) \<oslash> peval xs (Denom (FMul e1 e2))"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   520
      by (simp add: split_beta split nonzero_union npemul_correct integral_iff)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   521
    finally show ?case .
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   522
  next
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   523
    case 2
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   524
    with FMul show ?case
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   525
      by (simp add: split_beta split nonzero_union npemul_correct integral_iff)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   526
  }
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   527
next
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   528
  case (FNeg e)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   529
  {
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   530
    case 1
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   531
    with FNeg show ?case
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   532
      by (simp add: split_beta npeneg_correct)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   533
  next
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   534
    case 2
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   535
    with FNeg show ?case
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   536
      by (simp add: split_beta)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   537
  }
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   538
next
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   539
  case (FDiv e1 e2)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   540
  note split =
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   541
    split_aux_correct' [where xs=xs and
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   542
      e\<^sub>1="Num e1" and e\<^sub>2="Num e2"]
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   543
    split_aux_correct' [where xs=xs and
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   544
      e\<^sub>1="Denom e1" and e\<^sub>2="Denom e2"]
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   545
  {
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   546
    case 1
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   547
    let ?left\<^sub>1 = "peval xs (Left (Num e1) (Num e2))"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   548
    let ?common\<^sub>1 = "peval xs (Common (Num e1) (Num e2))"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   549
    let ?right\<^sub>1 = "peval xs (Right (Num e1) (Num e2))"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   550
    let ?left\<^sub>2 = "peval xs (Left (Denom e1) (Denom e2))"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   551
    let ?common\<^sub>2 = "peval xs (Common (Denom e1) (Denom e2))"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   552
    let ?right\<^sub>2 = "peval xs (Right (Denom e1) (Denom e2))"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   553
    from 1 FDiv
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   554
    have "feval xs (FDiv e1 e2) =
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   555
      ((?common\<^sub>1 \<otimes> ?common\<^sub>2) \<otimes> (?left\<^sub>1 \<otimes> ?right\<^sub>2)) \<oslash>
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   556
      ((?common\<^sub>1 \<otimes> ?common\<^sub>2) \<otimes> (?left\<^sub>2 \<otimes> ?right\<^sub>1))"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   557
      by (simp add: split_beta split nonzero_union nonzero_insert
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   558
        nonzero_divide_divide_eq m_ac)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   559
    also from 1 FDiv have "\<dots> =
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   560
      peval xs (Num (FDiv e1 e2)) \<oslash> peval xs (Denom (FDiv e1 e2))"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   561
      by (simp add: split_beta split nonzero_union nonzero_insert npemul_correct integral_iff)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   562
    finally show ?case .
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   563
  next
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   564
    case 2
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   565
    with FDiv show ?case
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   566
      by (simp add: split_beta split nonzero_union nonzero_insert npemul_correct integral_iff)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   567
  }
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   568
next
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   569
  case (FPow e n)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   570
  {
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   571
    case 1
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   572
    with FPow show ?case
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   573
      by (simp add: split_beta nonzero_power_divide npepow_correct)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   574
  next
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   575
    case 2
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   576
    with FPow show ?case
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   577
      by (simp add: split_beta npepow_correct)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   578
  }
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   579
qed
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   580
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   581
lemma (in field) feval_eq0:
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   582
  assumes "in_carrier xs"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   583
  and "fnorm e = (n, d, c)"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   584
  and "nonzero xs c"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   585
  and "peval xs n = \<zero>"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   586
  shows "feval xs e = \<zero>"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   587
  using assms fnorm_correct [of xs e]
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   588
  by simp
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   589
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   590
lemma (in field) fexpr_in_carrier:
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   591
  assumes "in_carrier xs"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   592
  and "nonzero xs (Cond e)"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   593
  shows "feval xs e \<in> carrier R"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   594
  using assms
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   595
proof (induct e)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   596
  case (FDiv e1 e2)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   597
  then have "feval xs e1 \<in> carrier R" "feval xs e2 \<in> carrier R"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   598
    "peval xs (Num e2) \<noteq> \<zero>" "nonzero xs (Cond e2)"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   599
    by (simp_all add: nonzero_union nonzero_insert split: prod.split_asm)
64998
d51478d6aae4 isabelle update_cartouches
haftmann
parents: 64962
diff changeset
   600
  from \<open>in_carrier xs\<close> \<open>nonzero xs (Cond e2)\<close>
64962
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   601
  have "feval xs e2 = peval xs (Num e2) \<oslash> peval xs (Denom e2)"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   602
    by (rule fnorm_correct)
64998
d51478d6aae4 isabelle update_cartouches
haftmann
parents: 64962
diff changeset
   603
  moreover from \<open>in_carrier xs\<close> \<open>nonzero xs (Cond e2)\<close>
64962
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   604
  have "peval xs (Denom e2) \<noteq> \<zero>" by (rule fnorm_correct)
64998
d51478d6aae4 isabelle update_cartouches
haftmann
parents: 64962
diff changeset
   605
  ultimately have "feval xs e2 \<noteq> \<zero>" using \<open>peval xs (Num e2) \<noteq> \<zero>\<close> \<open>in_carrier xs\<close>
64962
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   606
    by (simp add: divide_eq_0_iff)
64998
d51478d6aae4 isabelle update_cartouches
haftmann
parents: 64962
diff changeset
   607
  with \<open>feval xs e1 \<in> carrier R\<close> \<open>feval xs e2 \<in> carrier R\<close>
64962
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   608
  show ?case by simp
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   609
qed (simp_all add: nonzero_union split: prod.split_asm)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   610
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   611
lemma (in field) feval_eq:
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   612
  assumes "in_carrier xs"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   613
  and "fnorm (FSub e e') = (n, d, c)"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   614
  and "nonzero xs c"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   615
  shows "(feval xs e = feval xs e') = (peval xs n = \<zero>)"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   616
proof -
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   617
  from assms have "nonzero xs (Cond e)" "nonzero xs (Cond e')"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   618
    by (auto simp add: nonzero_union split: prod.split_asm)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   619
  with assms fnorm_correct [of xs "FSub e e'"]
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   620
  have "feval xs e \<ominus> feval xs e' = peval xs n \<oslash> peval xs d"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   621
    "peval xs d \<noteq> \<zero>"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   622
    by simp_all
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   623
  show ?thesis
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   624
  proof
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   625
    assume "feval xs e = feval xs e'"
64998
d51478d6aae4 isabelle update_cartouches
haftmann
parents: 64962
diff changeset
   626
    with \<open>feval xs e \<ominus> feval xs e' = peval xs n \<oslash> peval xs d\<close>
d51478d6aae4 isabelle update_cartouches
haftmann
parents: 64962
diff changeset
   627
      \<open>in_carrier xs\<close> \<open>nonzero xs (Cond e')\<close>
64962
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   628
    have "peval xs n \<oslash> peval xs d = \<zero>"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   629
      by (simp add: fexpr_in_carrier minus_eq r_neg)
64998
d51478d6aae4 isabelle update_cartouches
haftmann
parents: 64962
diff changeset
   630
    with \<open>peval xs d \<noteq> \<zero>\<close> \<open>in_carrier xs\<close>
64962
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   631
    show "peval xs n = \<zero>"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   632
      by (simp add: divide_eq_0_iff)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   633
  next
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   634
    assume "peval xs n = \<zero>"
64998
d51478d6aae4 isabelle update_cartouches
haftmann
parents: 64962
diff changeset
   635
    with \<open>feval xs e \<ominus> feval xs e' = peval xs n \<oslash> peval xs d\<close> \<open>peval xs d \<noteq> \<zero>\<close>
d51478d6aae4 isabelle update_cartouches
haftmann
parents: 64962
diff changeset
   636
      \<open>nonzero xs (Cond e)\<close> \<open>nonzero xs (Cond e')\<close> \<open>in_carrier xs\<close>
64962
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   637
    show "feval xs e = feval xs e'"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   638
      by (simp add: eq_diff0 fexpr_in_carrier)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   639
  qed
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   640
qed
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   641
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   642
code_reflect Field_Code
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   643
  datatypes fexpr = FCnst | FVar | FAdd | FSub | FMul | FNeg | FDiv | FPow
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   644
  and pexpr = PExpr1 | PExpr2
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   645
  and pexpr1 = PCnst | PVar | PAdd | PSub | PNeg
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   646
  and pexpr2 = PMul | PPow
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   647
  functions fnorm
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   648
    term_of_fexpr_inst.term_of_fexpr
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   649
    term_of_pexpr_inst.term_of_pexpr
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   650
    equal_pexpr_inst.equal_pexpr
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   651
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   652
definition field_codegen_aux :: "(pexpr \<times> pexpr list) itself" where
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   653
  "field_codegen_aux = (Code_Evaluation.TERM_OF_EQUAL::(pexpr \<times> pexpr list) itself)"
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   654
64998
d51478d6aae4 isabelle update_cartouches
haftmann
parents: 64962
diff changeset
   655
ML \<open>
64962
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   656
signature FIELD_TAC =
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   657
sig
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   658
  structure Field_Simps:
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   659
  sig
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   660
    type T
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   661
    val get: Context.generic -> T
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   662
    val put: T -> Context.generic -> Context.generic
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   663
    val map: (T -> T) -> Context.generic -> Context.generic
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   664
  end
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   665
  val eq_field_simps:
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   666
    (term * (thm list * thm list * thm list * thm * thm)) *
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   667
    (term * (thm list * thm list * thm list * thm * thm)) -> bool
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   668
  val field_tac: bool -> Proof.context -> int -> tactic
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   669
end
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   670
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   671
structure Field_Tac : FIELD_TAC =
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   672
struct
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   673
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   674
open Ring_Tac;
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   675
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   676
fun field_struct (Const (@{const_name Ring.ring.add}, _) $ R $ _ $ _) = SOME R
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   677
  | field_struct (Const (@{const_name Ring.a_minus}, _) $ R $ _ $ _) = SOME R
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   678
  | field_struct (Const (@{const_name Group.monoid.mult}, _) $ R $ _ $ _) = SOME R
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   679
  | field_struct (Const (@{const_name Ring.a_inv}, _) $ R $ _) = SOME R
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   680
  | field_struct (Const (@{const_name Group.pow}, _) $ R $ _ $ _) = SOME R
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   681
  | field_struct (Const (@{const_name Algebra_Aux.m_div}, _) $ R $ _ $ _) = SOME R
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   682
  | field_struct (Const (@{const_name Ring.ring.zero}, _) $ R) = SOME R
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   683
  | field_struct (Const (@{const_name Group.monoid.one}, _) $ R) = SOME R
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   684
  | field_struct (Const (@{const_name Algebra_Aux.of_integer}, _) $ R $ _) = SOME R
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   685
  | field_struct _ = NONE;
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   686
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   687
fun reif_fexpr vs (Const (@{const_name Ring.ring.add}, _) $ _ $ a $ b) =
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   688
      @{const FAdd} $ reif_fexpr vs a $ reif_fexpr vs b
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   689
  | reif_fexpr vs (Const (@{const_name Ring.a_minus}, _) $ _ $ a $ b) =
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   690
      @{const FSub} $ reif_fexpr vs a $ reif_fexpr vs b
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   691
  | reif_fexpr vs (Const (@{const_name Group.monoid.mult}, _) $ _ $ a $ b) =
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   692
      @{const FMul} $ reif_fexpr vs a $ reif_fexpr vs b
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   693
  | reif_fexpr vs (Const (@{const_name Ring.a_inv}, _) $ _ $ a) =
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   694
      @{const FNeg} $ reif_fexpr vs a
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   695
  | reif_fexpr vs (Const (@{const_name Group.pow}, _) $ _ $ a $ n) =
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   696
      @{const FPow} $ reif_fexpr vs a $ n
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   697
  | reif_fexpr vs (Const (@{const_name Algebra_Aux.m_div}, _) $ _ $ a $ b) =
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   698
      @{const FDiv} $ reif_fexpr vs a $ reif_fexpr vs b
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   699
  | reif_fexpr vs (Free x) =
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   700
      @{const FVar} $ HOLogic.mk_number HOLogic.natT (find_index (equal x) vs)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   701
  | reif_fexpr vs (Const (@{const_name Ring.ring.zero}, _) $ _) =
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   702
      @{term "FCnst 0"}
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   703
  | reif_fexpr vs (Const (@{const_name Group.monoid.one}, _) $ _) =
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   704
      @{term "FCnst 1"}
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   705
  | reif_fexpr vs (Const (@{const_name Algebra_Aux.of_integer}, _) $ _ $ n) =
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   706
      @{const FCnst} $ n
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   707
  | reif_fexpr _ _ = error "reif_fexpr: bad expression";
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   708
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   709
fun reif_fexpr' vs (Const (@{const_name Groups.plus}, _) $ a $ b) =
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   710
      @{const FAdd} $ reif_fexpr' vs a $ reif_fexpr' vs b
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   711
  | reif_fexpr' vs (Const (@{const_name Groups.minus}, _) $ a $ b) =
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   712
      @{const FSub} $ reif_fexpr' vs a $ reif_fexpr' vs b
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   713
  | reif_fexpr' vs (Const (@{const_name Groups.times}, _) $ a $ b) =
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   714
      @{const FMul} $ reif_fexpr' vs a $ reif_fexpr' vs b
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   715
  | reif_fexpr' vs (Const (@{const_name Groups.uminus}, _) $ a) =
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   716
      @{const FNeg} $ reif_fexpr' vs a
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   717
  | reif_fexpr' vs (Const (@{const_name Power.power}, _) $ a $ n) =
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   718
      @{const FPow} $ reif_fexpr' vs a $ n
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   719
  | reif_fexpr' vs (Const (@{const_name divide}, _) $ a $ b) =
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   720
      @{const FDiv} $ reif_fexpr' vs a $ reif_fexpr' vs b
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   721
  | reif_fexpr' vs (Free x) =
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   722
      @{const FVar} $ HOLogic.mk_number HOLogic.natT (find_index (equal x) vs)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   723
  | reif_fexpr' vs (Const (@{const_name zero_class.zero}, _)) =
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   724
      @{term "FCnst 0"}
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   725
  | reif_fexpr' vs (Const (@{const_name one_class.one}, _)) =
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   726
      @{term "FCnst 1"}
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   727
  | reif_fexpr' vs (Const (@{const_name numeral}, _) $ b) =
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   728
      @{const FCnst} $ (@{const numeral (int)} $ b)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   729
  | reif_fexpr' _ _ = error "reif_fexpr: bad expression";
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   730
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   731
fun eq_field_simps
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   732
  ((t, (ths1, ths2, ths3, th4, th)),
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   733
   (t', (ths1', ths2', ths3', th4', th'))) =
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   734
    t aconv t' andalso
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   735
    eq_list Thm.eq_thm (ths1, ths1') andalso
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   736
    eq_list Thm.eq_thm (ths2, ths2') andalso
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   737
    eq_list Thm.eq_thm (ths3, ths3') andalso
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   738
    Thm.eq_thm (th4, th4') andalso
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   739
    Thm.eq_thm (th, th');
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   740
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   741
structure Field_Simps = Generic_Data
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   742
(struct
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   743
  type T = (term * (thm list * thm list * thm list * thm * thm)) Net.net
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   744
  val empty = Net.empty
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   745
  val extend = I
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   746
  val merge = Net.merge eq_field_simps
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   747
end);
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   748
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   749
fun get_field_simps ctxt optcT t =
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   750
  (case get_matching_rules ctxt (Field_Simps.get (Context.Proof ctxt)) t of
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   751
     SOME (ths1, ths2, ths3, th4, th) =>
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   752
       let val tr =
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   753
         Thm.transfer (Proof_Context.theory_of ctxt) #>
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   754
         (case optcT of NONE => I | SOME cT => inst [cT] [] #> norm)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   755
       in (map tr ths1, map tr ths2, map tr ths3, tr th4, tr th) end
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   756
   | NONE => error "get_field_simps: lookup failed");
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   757
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   758
fun nth_el_conv (_, _, _, nth_el_Cons, _) =
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   759
  let
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   760
    val a = type_of_eqn nth_el_Cons;
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   761
    val If_conv_a = If_conv a;
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   762
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   763
    fun conv ys n = (case strip_app ys of
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   764
      (@{const_name Cons}, [x, xs]) =>
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   765
        transitive'
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   766
          (inst [] [x, xs, n] nth_el_Cons)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   767
          (If_conv_a (args2 nat_eq_conv)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   768
             Thm.reflexive
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   769
             (cong2' conv Thm.reflexive (args2 nat_minus_conv))))
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   770
  in conv end;
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   771
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   772
fun feval_conv (rls as
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   773
      ([feval_simps_1, feval_simps_2, feval_simps_3,
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   774
        feval_simps_4, feval_simps_5, feval_simps_6,
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   775
        feval_simps_7, feval_simps_8, feval_simps_9,
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   776
        feval_simps_10, feval_simps_11],
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   777
       _, _, _, _)) =
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   778
  let
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   779
    val nth_el_conv' = nth_el_conv rls;
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   780
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   781
    fun conv xs x = (case strip_app x of
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   782
        (@{const_name FCnst}, [c]) => (case strip_app c of
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   783
            (@{const_name zero_class.zero}, _) => inst [] [xs] feval_simps_9
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   784
          | (@{const_name one_class.one}, _) => inst [] [xs] feval_simps_10
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   785
          | (@{const_name numeral}, [n]) => inst [] [xs, n] feval_simps_11
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   786
          | _ => inst [] [xs, c] feval_simps_1)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   787
      | (@{const_name FVar}, [n]) =>
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   788
          transitive' (inst [] [xs, n] feval_simps_2) (args2 nth_el_conv')
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   789
      | (@{const_name FAdd}, [a, b]) =>
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   790
          transitive' (inst [] [xs, a, b] feval_simps_3)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   791
            (cong2 (args2 conv) (args2 conv))
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   792
      | (@{const_name FSub}, [a, b]) =>
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   793
          transitive' (inst [] [xs, a, b] feval_simps_4)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   794
            (cong2 (args2 conv) (args2 conv))
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   795
      | (@{const_name FMul}, [a, b]) =>
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   796
          transitive' (inst [] [xs, a, b] feval_simps_5)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   797
            (cong2 (args2 conv) (args2 conv))
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   798
      | (@{const_name FNeg}, [a]) =>
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   799
          transitive' (inst [] [xs, a] feval_simps_6)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   800
            (cong1 (args2 conv))
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   801
      | (@{const_name FDiv}, [a, b]) =>
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   802
          transitive' (inst [] [xs, a, b] feval_simps_7)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   803
            (cong2 (args2 conv) (args2 conv))
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   804
      | (@{const_name FPow}, [a, n]) =>
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   805
          transitive' (inst [] [xs, a, n] feval_simps_8)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   806
            (cong2 (args2 conv) Thm.reflexive))
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   807
  in conv end;
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   808
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   809
fun peval_conv (rls as
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   810
      (_,
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   811
       [peval_simps_1, peval_simps_2, peval_simps_3,
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   812
        peval_simps_4, peval_simps_5, peval_simps_6,
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   813
        peval_simps_7, peval_simps_8, peval_simps_9,
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   814
        peval_simps_10, peval_simps_11],
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   815
       _, _, _)) =
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   816
  let
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   817
    val nth_el_conv' = nth_el_conv rls;
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   818
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   819
    fun conv xs x = (case strip_app x of
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   820
        (@{const_name PExpr1}, [e]) => (case strip_app e of
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   821
            (@{const_name PCnst}, [c]) => (case strip_numeral c of
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   822
                (@{const_name zero_class.zero}, _) => inst [] [xs] peval_simps_8
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   823
              | (@{const_name one_class.one}, _) => inst [] [xs] peval_simps_9
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   824
              | (@{const_name numeral}, [n]) => inst [] [xs, n] peval_simps_10
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   825
              | (@{const_name uminus}, [n]) => inst [] [xs, n] peval_simps_11
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   826
              | _ => inst [] [xs, c] peval_simps_1)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   827
          | (@{const_name PVar}, [n]) =>
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   828
              transitive' (inst [] [xs, n] peval_simps_2) (args2 nth_el_conv')
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   829
          | (@{const_name PAdd}, [a, b]) =>
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   830
              transitive' (inst [] [xs, a, b] peval_simps_3)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   831
                (cong2 (args2 conv) (args2 conv))
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   832
          | (@{const_name PSub}, [a, b]) =>
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   833
              transitive' (inst [] [xs, a, b] peval_simps_4)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   834
                (cong2 (args2 conv) (args2 conv))
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   835
          | (@{const_name PNeg}, [a]) =>
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   836
              transitive' (inst [] [xs, a] peval_simps_5)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   837
                (cong1 (args2 conv)))
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   838
      | (@{const_name PExpr2}, [e]) => (case strip_app e of
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   839
            (@{const_name PMul}, [a, b]) =>
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   840
              transitive' (inst [] [xs, a, b] peval_simps_6)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   841
                (cong2 (args2 conv) (args2 conv))
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   842
          | (@{const_name PPow}, [a, n]) =>
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   843
              transitive' (inst [] [xs, a, n] peval_simps_7)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   844
                (cong2 (args2 conv) Thm.reflexive)))
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   845
  in conv end;
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   846
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   847
fun nonzero_conv (rls as
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   848
      (_, _,
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   849
       [nonzero_Nil, nonzero_Cons, nonzero_singleton],
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   850
       _, _)) =
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   851
  let
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   852
    val peval_conv' = peval_conv rls;
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   853
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   854
    fun conv xs qs = (case strip_app qs of
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   855
        (@{const_name Nil}, []) => inst [] [xs] nonzero_Nil
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   856
      | (@{const_name Cons}, [p, ps]) => (case Thm.term_of ps of
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   857
            Const (@{const_name Nil}, _) =>
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   858
              transitive' (inst [] [xs, p] nonzero_singleton)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   859
                (cong1 (cong2 (args2 peval_conv') Thm.reflexive))
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   860
          | _ => transitive' (inst [] [xs, p, ps] nonzero_Cons)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   861
              (cong2 (cong1 (cong2 (args2 peval_conv') Thm.reflexive)) (args2 conv))))
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   862
  in conv end;
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   863
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   864
val cv = Code_Evaluation.static_conv
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   865
  {ctxt = @{context},
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   866
   consts =
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   867
     [@{const_name nat_of_integer},
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   868
      @{const_name fnorm}, @{const_name field_codegen_aux}]};
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   869
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   870
fun field_tac in_prem ctxt =
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   871
  SUBGOAL (fn (g, i) =>
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   872
    let
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   873
      val (prems, concl) = Logic.strip_horn g;
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   874
      fun find_eq s = (case s of
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   875
          (_ $ (Const (@{const_name HOL.eq}, Type (_, [T, _])) $ t $ u)) =>
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   876
            (case (field_struct t, field_struct u) of
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   877
               (SOME R, _) => SOME ((t, u), R, T, NONE, mk_in_carrier ctxt R [], reif_fexpr)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   878
             | (_, SOME R) => SOME ((t, u), R, T, NONE, mk_in_carrier ctxt R [], reif_fexpr)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   879
             | _ =>
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   880
                 if Sign.of_sort (Proof_Context.theory_of ctxt) (T, @{sort field})
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   881
                 then SOME ((t, u), mk_ring T, T, SOME T, K @{thm in_carrier_trivial}, reif_fexpr')
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   882
                 else NONE)
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   883
        | _ => NONE);
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   884
      val ((t, u), R, T, optT, mkic, reif) =
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   885
        (case get_first find_eq
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   886
           (if in_prem then prems else [concl]) of
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   887
           SOME q => q
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   888
         | NONE => error "cannot determine field");
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   889
      val rls as (_, _, _, _, feval_eq) =
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   890
        get_field_simps ctxt (Option.map (Thm.ctyp_of ctxt) optT) R;
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   891
      val xs = [] |> Term.add_frees t |> Term.add_frees u |> filter (equal T o snd);
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   892
      val cxs = Thm.cterm_of ctxt (HOLogic.mk_list T (map Free xs));
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   893
      val ce = Thm.cterm_of ctxt (reif xs t);
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   894
      val ce' = Thm.cterm_of ctxt (reif xs u);
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   895
      val fnorm = cv ctxt
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   896
        (Thm.apply @{cterm fnorm} (Thm.apply (Thm.apply @{cterm FSub} ce) ce'));
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   897
      val (_, [n, dc]) = strip_app (Thm.rhs_of fnorm);
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   898
      val (_, [_, c]) = strip_app dc;
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   899
      val th =
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   900
        Conv.fconv_rule (Conv.concl_conv 1 (Conv.arg_conv
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   901
          (binop_conv
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   902
             (binop_conv
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   903
                (K (feval_conv rls cxs ce)) (K (feval_conv rls cxs ce')))
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   904
             (Conv.arg1_conv (K (peval_conv rls cxs n))))))
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   905
        ([mkic xs,
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   906
          mk_obj_eq fnorm,
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   907
          mk_obj_eq (nonzero_conv rls cxs c) RS @{thm iffD2}] MRS
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   908
         feval_eq);
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   909
      val th' = Drule.rotate_prems 1
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   910
        (th RS (if in_prem then @{thm iffD1} else @{thm iffD2}));
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   911
    in
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   912
      if in_prem then
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   913
        dresolve_tac ctxt [th'] 1 THEN defer_tac 1
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   914
      else
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   915
        resolve_tac ctxt [th'] 1
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   916
    end);
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   917
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   918
end
64998
d51478d6aae4 isabelle update_cartouches
haftmann
parents: 64962
diff changeset
   919
\<close>
64962
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   920
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   921
context field begin
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   922
64998
d51478d6aae4 isabelle update_cartouches
haftmann
parents: 64962
diff changeset
   923
local_setup \<open>
64962
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   924
Local_Theory.declaration {syntax = false, pervasive = false}
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   925
  (fn phi => Field_Tac.Field_Simps.map (Ring_Tac.insert_rules Field_Tac.eq_field_simps
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   926
    (Morphism.term phi @{term R},
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   927
     (Morphism.fact phi @{thms feval.simps [meta] feval_Cnst [meta]},
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   928
      Morphism.fact phi @{thms peval.simps [meta] peval_Cnst [meta]},
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   929
      Morphism.fact phi @{thms nonzero.simps [meta] nonzero_singleton [meta]},
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   930
      singleton (Morphism.fact phi) @{thm nth_el_Cons [meta]},
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   931
      singleton (Morphism.fact phi) @{thm feval_eq}))))
64998
d51478d6aae4 isabelle update_cartouches
haftmann
parents: 64962
diff changeset
   932
\<close>
64962
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   933
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   934
end
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   935
64998
d51478d6aae4 isabelle update_cartouches
haftmann
parents: 64962
diff changeset
   936
method_setup field = \<open>
64962
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   937
  Scan.lift (Args.mode "prems") -- Attrib.thms >> (fn (in_prem, thms) => fn ctxt =>
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   938
    SIMPLE_METHOD' (Field_Tac.field_tac in_prem ctxt THEN' Ring_Tac.ring_tac in_prem thms ctxt))
64998
d51478d6aae4 isabelle update_cartouches
haftmann
parents: 64962
diff changeset
   939
\<close> "reduce equations over fields to equations over rings"
64962
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   940
bf41e1109db3 Added new / improved tactics for fields and rings
berghofe
parents:
diff changeset
   941
end