src/CTT/CTT.thy
author wenzelm
Sat Nov 04 15:24:40 2017 +0100 (20 months ago)
changeset 67003 49850a679c2c
parent 65447 fae6051ec192
child 67399 eab6ce8368fa
permissions -rw-r--r--
more robust sorted_entries;
wenzelm@17441
     1
(*  Title:      CTT/CTT.thy
clasohm@0
     2
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
clasohm@0
     3
    Copyright   1993  University of Cambridge
clasohm@0
     4
*)
clasohm@0
     5
wenzelm@17441
     6
theory CTT
wenzelm@17441
     7
imports Pure
wenzelm@17441
     8
begin
wenzelm@17441
     9
wenzelm@65447
    10
section \<open>Constructive Type Theory: axiomatic basis\<close>
wenzelm@65447
    11
wenzelm@48891
    12
ML_file "~~/src/Provers/typedsimp.ML"
wenzelm@39557
    13
setup Pure_Thy.old_appl_syntax_setup
wenzelm@26956
    14
wenzelm@17441
    15
typedecl i
wenzelm@17441
    16
typedecl t
wenzelm@17441
    17
typedecl o
clasohm@0
    18
clasohm@0
    19
consts
wenzelm@63505
    20
  \<comment> \<open>Types\<close>
wenzelm@17441
    21
  F         :: "t"
wenzelm@63505
    22
  T         :: "t"          \<comment> \<open>\<open>F\<close> is empty, \<open>T\<close> contains one element\<close>
wenzelm@58977
    23
  contr     :: "i\<Rightarrow>i"
clasohm@0
    24
  tt        :: "i"
wenzelm@63505
    25
  \<comment> \<open>Natural numbers\<close>
clasohm@0
    26
  N         :: "t"
wenzelm@58977
    27
  succ      :: "i\<Rightarrow>i"
wenzelm@58977
    28
  rec       :: "[i, i, [i,i]\<Rightarrow>i] \<Rightarrow> i"
wenzelm@63505
    29
  \<comment> \<open>Unions\<close>
wenzelm@58977
    30
  inl       :: "i\<Rightarrow>i"
wenzelm@58977
    31
  inr       :: "i\<Rightarrow>i"
wenzelm@60555
    32
  "when"    :: "[i, i\<Rightarrow>i, i\<Rightarrow>i]\<Rightarrow>i"
wenzelm@63505
    33
  \<comment> \<open>General Sum and Binary Product\<close>
wenzelm@58977
    34
  Sum       :: "[t, i\<Rightarrow>t]\<Rightarrow>t"
wenzelm@58977
    35
  fst       :: "i\<Rightarrow>i"
wenzelm@58977
    36
  snd       :: "i\<Rightarrow>i"
wenzelm@58977
    37
  split     :: "[i, [i,i]\<Rightarrow>i] \<Rightarrow>i"
wenzelm@63505
    38
  \<comment> \<open>General Product and Function Space\<close>
wenzelm@58977
    39
  Prod      :: "[t, i\<Rightarrow>t]\<Rightarrow>t"
wenzelm@63505
    40
  \<comment> \<open>Types\<close>
wenzelm@58977
    41
  Plus      :: "[t,t]\<Rightarrow>t"           (infixr "+" 40)
wenzelm@63505
    42
  \<comment> \<open>Equality type\<close>
wenzelm@58977
    43
  Eq        :: "[t,i,i]\<Rightarrow>t"
clasohm@0
    44
  eq        :: "i"
wenzelm@63505
    45
  \<comment> \<open>Judgements\<close>
wenzelm@58977
    46
  Type      :: "t \<Rightarrow> prop"          ("(_ type)" [10] 5)
wenzelm@58977
    47
  Eqtype    :: "[t,t]\<Rightarrow>prop"        ("(_ =/ _)" [10,10] 5)
wenzelm@58977
    48
  Elem      :: "[i, t]\<Rightarrow>prop"       ("(_ /: _)" [10,10] 5)
wenzelm@58977
    49
  Eqelem    :: "[i,i,t]\<Rightarrow>prop"      ("(_ =/ _ :/ _)" [10,10,10] 5)
wenzelm@58977
    50
  Reduce    :: "[i,i]\<Rightarrow>prop"        ("Reduce[_,_]")
wenzelm@14765
    51
wenzelm@63505
    52
  \<comment> \<open>Types\<close>
wenzelm@63505
    53
wenzelm@63505
    54
  \<comment> \<open>Functions\<close>
wenzelm@61391
    55
  lambda    :: "(i \<Rightarrow> i) \<Rightarrow> i"      (binder "\<^bold>\<lambda>" 10)
wenzelm@58977
    56
  app       :: "[i,i]\<Rightarrow>i"           (infixl "`" 60)
wenzelm@63505
    57
  \<comment> \<open>Natural numbers\<close>
wenzelm@41310
    58
  Zero      :: "i"                  ("0")
wenzelm@63505
    59
  \<comment> \<open>Pairing\<close>
wenzelm@58977
    60
  pair      :: "[i,i]\<Rightarrow>i"           ("(1<_,/_>)")
clasohm@0
    61
wenzelm@14765
    62
syntax
wenzelm@61391
    63
  "_PROD"   :: "[idt,t,t]\<Rightarrow>t"       ("(3\<Prod>_:_./ _)" 10)
wenzelm@61391
    64
  "_SUM"    :: "[idt,t,t]\<Rightarrow>t"       ("(3\<Sum>_:_./ _)" 10)
clasohm@0
    65
translations
wenzelm@61391
    66
  "\<Prod>x:A. B" \<rightleftharpoons> "CONST Prod(A, \<lambda>x. B)"
wenzelm@61391
    67
  "\<Sum>x:A. B" \<rightleftharpoons> "CONST Sum(A, \<lambda>x. B)"
wenzelm@19761
    68
wenzelm@63505
    69
abbreviation Arrow :: "[t,t]\<Rightarrow>t"  (infixr "\<longrightarrow>" 30)
wenzelm@63505
    70
  where "A \<longrightarrow> B \<equiv> \<Prod>_:A. B"
wenzelm@63505
    71
wenzelm@63505
    72
abbreviation Times :: "[t,t]\<Rightarrow>t"  (infixr "\<times>" 50)
wenzelm@63505
    73
  where "A \<times> B \<equiv> \<Sum>_:A. B"
paulson@10467
    74
wenzelm@63505
    75
text \<open>
wenzelm@63505
    76
  Reduction: a weaker notion than equality;  a hack for simplification.
wenzelm@63505
    77
  \<open>Reduce[a,b]\<close> means either that \<open>a = b : A\<close> for some \<open>A\<close> or else
wenzelm@63505
    78
    that \<open>a\<close> and \<open>b\<close> are textually identical.
clasohm@0
    79
wenzelm@63505
    80
  Does not verify \<open>a:A\<close>!  Sound because only \<open>trans_red\<close> uses a \<open>Reduce\<close>
wenzelm@63505
    81
  premise. No new theorems can be proved about the standard judgements.
wenzelm@63505
    82
\<close>
wenzelm@63505
    83
axiomatization
wenzelm@63505
    84
where
wenzelm@51308
    85
  refl_red: "\<And>a. Reduce[a,a]" and
wenzelm@58977
    86
  red_if_equal: "\<And>a b A. a = b : A \<Longrightarrow> Reduce[a,b]" and
wenzelm@58977
    87
  trans_red: "\<And>a b c A. \<lbrakk>a = b : A; Reduce[b,c]\<rbrakk> \<Longrightarrow> a = c : A" and
clasohm@0
    88
wenzelm@63505
    89
  \<comment> \<open>Reflexivity\<close>
clasohm@0
    90
wenzelm@58977
    91
  refl_type: "\<And>A. A type \<Longrightarrow> A = A" and
wenzelm@58977
    92
  refl_elem: "\<And>a A. a : A \<Longrightarrow> a = a : A" and
clasohm@0
    93
wenzelm@63505
    94
  \<comment> \<open>Symmetry\<close>
clasohm@0
    95
wenzelm@58977
    96
  sym_type:  "\<And>A B. A = B \<Longrightarrow> B = A" and
wenzelm@58977
    97
  sym_elem:  "\<And>a b A. a = b : A \<Longrightarrow> b = a : A" and
clasohm@0
    98
wenzelm@63505
    99
  \<comment> \<open>Transitivity\<close>
clasohm@0
   100
wenzelm@58977
   101
  trans_type:   "\<And>A B C. \<lbrakk>A = B; B = C\<rbrakk> \<Longrightarrow> A = C" and
wenzelm@58977
   102
  trans_elem:   "\<And>a b c A. \<lbrakk>a = b : A; b = c : A\<rbrakk> \<Longrightarrow> a = c : A" and
clasohm@0
   103
wenzelm@58977
   104
  equal_types:  "\<And>a A B. \<lbrakk>a : A; A = B\<rbrakk> \<Longrightarrow> a : B" and
wenzelm@58977
   105
  equal_typesL: "\<And>a b A B. \<lbrakk>a = b : A; A = B\<rbrakk> \<Longrightarrow> a = b : B" and
clasohm@0
   106
wenzelm@63505
   107
  \<comment> \<open>Substitution\<close>
clasohm@0
   108
wenzelm@58977
   109
  subst_type:   "\<And>a A B. \<lbrakk>a : A; \<And>z. z:A \<Longrightarrow> B(z) type\<rbrakk> \<Longrightarrow> B(a) type" and
wenzelm@58977
   110
  subst_typeL:  "\<And>a c A B D. \<lbrakk>a = c : A; \<And>z. z:A \<Longrightarrow> B(z) = D(z)\<rbrakk> \<Longrightarrow> B(a) = D(c)" and
clasohm@0
   111
wenzelm@58977
   112
  subst_elem:   "\<And>a b A B. \<lbrakk>a : A; \<And>z. z:A \<Longrightarrow> b(z):B(z)\<rbrakk> \<Longrightarrow> b(a):B(a)" and
wenzelm@17441
   113
  subst_elemL:
wenzelm@58977
   114
    "\<And>a b c d A B. \<lbrakk>a = c : A; \<And>z. z:A \<Longrightarrow> b(z)=d(z) : B(z)\<rbrakk> \<Longrightarrow> b(a)=d(c) : B(a)" and
clasohm@0
   115
clasohm@0
   116
wenzelm@63505
   117
  \<comment> \<open>The type \<open>N\<close> -- natural numbers\<close>
clasohm@0
   118
wenzelm@51308
   119
  NF: "N type" and
wenzelm@51308
   120
  NI0: "0 : N" and
wenzelm@58977
   121
  NI_succ: "\<And>a. a : N \<Longrightarrow> succ(a) : N" and
wenzelm@58977
   122
  NI_succL:  "\<And>a b. a = b : N \<Longrightarrow> succ(a) = succ(b) : N" and
clasohm@0
   123
wenzelm@17441
   124
  NE:
wenzelm@58977
   125
   "\<And>p a b C. \<lbrakk>p: N; a: C(0); \<And>u v. \<lbrakk>u: N; v: C(u)\<rbrakk> \<Longrightarrow> b(u,v): C(succ(u))\<rbrakk>
wenzelm@58977
   126
   \<Longrightarrow> rec(p, a, \<lambda>u v. b(u,v)) : C(p)" and
clasohm@0
   127
wenzelm@17441
   128
  NEL:
wenzelm@58977
   129
   "\<And>p q a b c d C. \<lbrakk>p = q : N; a = c : C(0);
wenzelm@58977
   130
      \<And>u v. \<lbrakk>u: N; v: C(u)\<rbrakk> \<Longrightarrow> b(u,v) = d(u,v): C(succ(u))\<rbrakk>
wenzelm@58977
   131
   \<Longrightarrow> rec(p, a, \<lambda>u v. b(u,v)) = rec(q,c,d) : C(p)" and
clasohm@0
   132
wenzelm@17441
   133
  NC0:
wenzelm@58977
   134
   "\<And>a b C. \<lbrakk>a: C(0); \<And>u v. \<lbrakk>u: N; v: C(u)\<rbrakk> \<Longrightarrow> b(u,v): C(succ(u))\<rbrakk>
wenzelm@58977
   135
   \<Longrightarrow> rec(0, a, \<lambda>u v. b(u,v)) = a : C(0)" and
clasohm@0
   136
wenzelm@17441
   137
  NC_succ:
wenzelm@58977
   138
   "\<And>p a b C. \<lbrakk>p: N;  a: C(0); \<And>u v. \<lbrakk>u: N; v: C(u)\<rbrakk> \<Longrightarrow> b(u,v): C(succ(u))\<rbrakk> \<Longrightarrow>
wenzelm@58977
   139
   rec(succ(p), a, \<lambda>u v. b(u,v)) = b(p, rec(p, a, \<lambda>u v. b(u,v))) : C(succ(p))" and
clasohm@0
   140
wenzelm@63505
   141
  \<comment> \<open>The fourth Peano axiom.  See page 91 of Martin-Löf's book.\<close>
wenzelm@58977
   142
  zero_ne_succ: "\<And>a. \<lbrakk>a: N; 0 = succ(a) : N\<rbrakk> \<Longrightarrow> 0: F" and
clasohm@0
   143
clasohm@0
   144
wenzelm@63505
   145
  \<comment> \<open>The Product of a family of types\<close>
clasohm@0
   146
wenzelm@61391
   147
  ProdF: "\<And>A B. \<lbrakk>A type; \<And>x. x:A \<Longrightarrow> B(x) type\<rbrakk> \<Longrightarrow> \<Prod>x:A. B(x) type" and
clasohm@0
   148
wenzelm@17441
   149
  ProdFL:
wenzelm@61391
   150
    "\<And>A B C D. \<lbrakk>A = C; \<And>x. x:A \<Longrightarrow> B(x) = D(x)\<rbrakk> \<Longrightarrow> \<Prod>x:A. B(x) = \<Prod>x:C. D(x)" and
clasohm@0
   151
wenzelm@17441
   152
  ProdI:
wenzelm@61391
   153
    "\<And>b A B. \<lbrakk>A type; \<And>x. x:A \<Longrightarrow> b(x):B(x)\<rbrakk> \<Longrightarrow> \<^bold>\<lambda>x. b(x) : \<Prod>x:A. B(x)" and
clasohm@0
   154
wenzelm@58977
   155
  ProdIL: "\<And>b c A B. \<lbrakk>A type; \<And>x. x:A \<Longrightarrow> b(x) = c(x) : B(x)\<rbrakk> \<Longrightarrow>
wenzelm@61391
   156
    \<^bold>\<lambda>x. b(x) = \<^bold>\<lambda>x. c(x) : \<Prod>x:A. B(x)" and
clasohm@0
   157
wenzelm@61391
   158
  ProdE:  "\<And>p a A B. \<lbrakk>p : \<Prod>x:A. B(x); a : A\<rbrakk> \<Longrightarrow> p`a : B(a)" and
wenzelm@61391
   159
  ProdEL: "\<And>p q a b A B. \<lbrakk>p = q: \<Prod>x:A. B(x); a = b : A\<rbrakk> \<Longrightarrow> p`a = q`b : B(a)" and
clasohm@0
   160
wenzelm@61391
   161
  ProdC: "\<And>a b A B. \<lbrakk>a : A; \<And>x. x:A \<Longrightarrow> b(x) : B(x)\<rbrakk> \<Longrightarrow> (\<^bold>\<lambda>x. b(x)) ` a = b(a) : B(a)" and
clasohm@0
   162
wenzelm@61391
   163
  ProdC2: "\<And>p A B. p : \<Prod>x:A. B(x) \<Longrightarrow> (\<^bold>\<lambda>x. p`x) = p : \<Prod>x:A. B(x)" and
clasohm@0
   164
clasohm@0
   165
wenzelm@63505
   166
  \<comment> \<open>The Sum of a family of types\<close>
clasohm@0
   167
wenzelm@61391
   168
  SumF:  "\<And>A B. \<lbrakk>A type; \<And>x. x:A \<Longrightarrow> B(x) type\<rbrakk> \<Longrightarrow> \<Sum>x:A. B(x) type" and
wenzelm@61391
   169
  SumFL: "\<And>A B C D. \<lbrakk>A = C; \<And>x. x:A \<Longrightarrow> B(x) = D(x)\<rbrakk> \<Longrightarrow> \<Sum>x:A. B(x) = \<Sum>x:C. D(x)" and
clasohm@0
   170
wenzelm@61391
   171
  SumI:  "\<And>a b A B. \<lbrakk>a : A; b : B(a)\<rbrakk> \<Longrightarrow> <a,b> : \<Sum>x:A. B(x)" and
wenzelm@61391
   172
  SumIL: "\<And>a b c d A B. \<lbrakk> a = c : A; b = d : B(a)\<rbrakk> \<Longrightarrow> <a,b> = <c,d> : \<Sum>x:A. B(x)" and
clasohm@0
   173
wenzelm@61391
   174
  SumE: "\<And>p c A B C. \<lbrakk>p: \<Sum>x:A. B(x); \<And>x y. \<lbrakk>x:A; y:B(x)\<rbrakk> \<Longrightarrow> c(x,y): C(<x,y>)\<rbrakk>
wenzelm@58977
   175
    \<Longrightarrow> split(p, \<lambda>x y. c(x,y)) : C(p)" and
clasohm@0
   176
wenzelm@61391
   177
  SumEL: "\<And>p q c d A B C. \<lbrakk>p = q : \<Sum>x:A. B(x);
wenzelm@58977
   178
      \<And>x y. \<lbrakk>x:A; y:B(x)\<rbrakk> \<Longrightarrow> c(x,y)=d(x,y): C(<x,y>)\<rbrakk>
wenzelm@58977
   179
    \<Longrightarrow> split(p, \<lambda>x y. c(x,y)) = split(q, \<lambda>x y. d(x,y)) : C(p)" and
clasohm@0
   180
wenzelm@58977
   181
  SumC: "\<And>a b c A B C. \<lbrakk>a: A;  b: B(a); \<And>x y. \<lbrakk>x:A; y:B(x)\<rbrakk> \<Longrightarrow> c(x,y): C(<x,y>)\<rbrakk>
wenzelm@58977
   182
    \<Longrightarrow> split(<a,b>, \<lambda>x y. c(x,y)) = c(a,b) : C(<a,b>)" and
clasohm@0
   183
wenzelm@61391
   184
  fst_def:   "\<And>a. fst(a) \<equiv> split(a, \<lambda>x y. x)" and
wenzelm@61391
   185
  snd_def:   "\<And>a. snd(a) \<equiv> split(a, \<lambda>x y. y)" and
clasohm@0
   186
clasohm@0
   187
wenzelm@63505
   188
  \<comment> \<open>The sum of two types\<close>
clasohm@0
   189
wenzelm@58977
   190
  PlusF: "\<And>A B. \<lbrakk>A type; B type\<rbrakk> \<Longrightarrow> A+B type" and
wenzelm@58977
   191
  PlusFL: "\<And>A B C D. \<lbrakk>A = C; B = D\<rbrakk> \<Longrightarrow> A+B = C+D" and
clasohm@0
   192
wenzelm@58977
   193
  PlusI_inl: "\<And>a A B. \<lbrakk>a : A; B type\<rbrakk> \<Longrightarrow> inl(a) : A+B" and
wenzelm@58977
   194
  PlusI_inlL: "\<And>a c A B. \<lbrakk>a = c : A; B type\<rbrakk> \<Longrightarrow> inl(a) = inl(c) : A+B" and
clasohm@0
   195
wenzelm@58977
   196
  PlusI_inr: "\<And>b A B. \<lbrakk>A type; b : B\<rbrakk> \<Longrightarrow> inr(b) : A+B" and
wenzelm@58977
   197
  PlusI_inrL: "\<And>b d A B. \<lbrakk>A type; b = d : B\<rbrakk> \<Longrightarrow> inr(b) = inr(d) : A+B" and
clasohm@0
   198
wenzelm@17441
   199
  PlusE:
wenzelm@58977
   200
    "\<And>p c d A B C. \<lbrakk>p: A+B;
wenzelm@58977
   201
      \<And>x. x:A \<Longrightarrow> c(x): C(inl(x));
wenzelm@58977
   202
      \<And>y. y:B \<Longrightarrow> d(y): C(inr(y)) \<rbrakk> \<Longrightarrow> when(p, \<lambda>x. c(x), \<lambda>y. d(y)) : C(p)" and
clasohm@0
   203
wenzelm@17441
   204
  PlusEL:
wenzelm@58977
   205
    "\<And>p q c d e f A B C. \<lbrakk>p = q : A+B;
wenzelm@58977
   206
      \<And>x. x: A \<Longrightarrow> c(x) = e(x) : C(inl(x));
wenzelm@58977
   207
      \<And>y. y: B \<Longrightarrow> d(y) = f(y) : C(inr(y))\<rbrakk>
wenzelm@58977
   208
    \<Longrightarrow> when(p, \<lambda>x. c(x), \<lambda>y. d(y)) = when(q, \<lambda>x. e(x), \<lambda>y. f(y)) : C(p)" and
clasohm@0
   209
wenzelm@17441
   210
  PlusC_inl:
wenzelm@64980
   211
    "\<And>a c d A B C. \<lbrakk>a: A;
wenzelm@58977
   212
      \<And>x. x:A \<Longrightarrow> c(x): C(inl(x));
wenzelm@58977
   213
      \<And>y. y:B \<Longrightarrow> d(y): C(inr(y)) \<rbrakk>
wenzelm@58977
   214
    \<Longrightarrow> when(inl(a), \<lambda>x. c(x), \<lambda>y. d(y)) = c(a) : C(inl(a))" and
clasohm@0
   215
wenzelm@17441
   216
  PlusC_inr:
wenzelm@58977
   217
    "\<And>b c d A B C. \<lbrakk>b: B;
wenzelm@58977
   218
      \<And>x. x:A \<Longrightarrow> c(x): C(inl(x));
wenzelm@58977
   219
      \<And>y. y:B \<Longrightarrow> d(y): C(inr(y))\<rbrakk>
wenzelm@58977
   220
    \<Longrightarrow> when(inr(b), \<lambda>x. c(x), \<lambda>y. d(y)) = d(b) : C(inr(b))" and
clasohm@0
   221
clasohm@0
   222
wenzelm@63505
   223
  \<comment> \<open>The type \<open>Eq\<close>\<close>
clasohm@0
   224
wenzelm@58977
   225
  EqF: "\<And>a b A. \<lbrakk>A type; a : A; b : A\<rbrakk> \<Longrightarrow> Eq(A,a,b) type" and
wenzelm@58977
   226
  EqFL: "\<And>a b c d A B. \<lbrakk>A = B; a = c : A; b = d : A\<rbrakk> \<Longrightarrow> Eq(A,a,b) = Eq(B,c,d)" and
wenzelm@58977
   227
  EqI: "\<And>a b A. a = b : A \<Longrightarrow> eq : Eq(A,a,b)" and
wenzelm@58977
   228
  EqE: "\<And>p a b A. p : Eq(A,a,b) \<Longrightarrow> a = b : A" and
clasohm@0
   229
wenzelm@63505
   230
  \<comment> \<open>By equality of types, can prove \<open>C(p)\<close> from \<open>C(eq)\<close>, an elimination rule\<close>
wenzelm@58977
   231
  EqC: "\<And>p a b A. p : Eq(A,a,b) \<Longrightarrow> p = eq : Eq(A,a,b)" and
clasohm@0
   232
wenzelm@63505
   233
wenzelm@63505
   234
  \<comment> \<open>The type \<open>F\<close>\<close>
clasohm@0
   235
wenzelm@51308
   236
  FF: "F type" and
wenzelm@58977
   237
  FE: "\<And>p C. \<lbrakk>p: F; C type\<rbrakk> \<Longrightarrow> contr(p) : C" and
wenzelm@58977
   238
  FEL: "\<And>p q C. \<lbrakk>p = q : F; C type\<rbrakk> \<Longrightarrow> contr(p) = contr(q) : C" and
clasohm@0
   239
wenzelm@63505
   240
wenzelm@63505
   241
  \<comment> \<open>The type T\<close>
wenzelm@63505
   242
  \<comment> \<open>
wenzelm@63505
   243
    Martin-Löf's book (page 68) discusses elimination and computation.
wenzelm@63505
   244
    Elimination can be derived by computation and equality of types,
wenzelm@63505
   245
    but with an extra premise \<open>C(x)\<close> type \<open>x:T\<close>.
wenzelm@63505
   246
    Also computation can be derived from elimination.
wenzelm@63505
   247
  \<close>
clasohm@0
   248
wenzelm@51308
   249
  TF: "T type" and
wenzelm@51308
   250
  TI: "tt : T" and
wenzelm@58977
   251
  TE: "\<And>p c C. \<lbrakk>p : T; c : C(tt)\<rbrakk> \<Longrightarrow> c : C(p)" and
wenzelm@58977
   252
  TEL: "\<And>p q c d C. \<lbrakk>p = q : T; c = d : C(tt)\<rbrakk> \<Longrightarrow> c = d : C(p)" and
wenzelm@58977
   253
  TC: "\<And>p. p : T \<Longrightarrow> p = tt : T"
clasohm@0
   254
wenzelm@19761
   255
wenzelm@19761
   256
subsection "Tactics and derived rules for Constructive Type Theory"
wenzelm@19761
   257
wenzelm@63505
   258
text \<open>Formation rules.\<close>
wenzelm@19761
   259
lemmas form_rls = NF ProdF SumF PlusF EqF FF TF
wenzelm@19761
   260
  and formL_rls = ProdFL SumFL PlusFL EqFL
wenzelm@19761
   261
wenzelm@63505
   262
text \<open>
wenzelm@63505
   263
  Introduction rules. OMITTED:
wenzelm@63505
   264
  \<^item> \<open>EqI\<close>, because its premise is an \<open>eqelem\<close>, not an \<open>elem\<close>.
wenzelm@63505
   265
\<close>
wenzelm@19761
   266
lemmas intr_rls = NI0 NI_succ ProdI SumI PlusI_inl PlusI_inr TI
wenzelm@19761
   267
  and intrL_rls = NI_succL ProdIL SumIL PlusI_inlL PlusI_inrL
wenzelm@19761
   268
wenzelm@63505
   269
text \<open>
wenzelm@63505
   270
  Elimination rules. OMITTED:
wenzelm@63505
   271
  \<^item> \<open>EqE\<close>, because its conclusion is an \<open>eqelem\<close>, not an \<open>elem\<close>
wenzelm@63505
   272
  \<^item> \<open>TE\<close>, because it does not involve a constructor.
wenzelm@63505
   273
\<close>
wenzelm@19761
   274
lemmas elim_rls = NE ProdE SumE PlusE FE
wenzelm@19761
   275
  and elimL_rls = NEL ProdEL SumEL PlusEL FEL
wenzelm@19761
   276
wenzelm@63505
   277
text \<open>OMITTED: \<open>eqC\<close> are \<open>TC\<close> because they make rewriting loop: \<open>p = un = un = \<dots>\<close>\<close>
wenzelm@19761
   278
lemmas comp_rls = NC0 NC_succ ProdC SumC PlusC_inl PlusC_inr
wenzelm@19761
   279
wenzelm@63505
   280
text \<open>Rules with conclusion \<open>a:A\<close>, an elem judgement.\<close>
wenzelm@19761
   281
lemmas element_rls = intr_rls elim_rls
wenzelm@19761
   282
wenzelm@63505
   283
text \<open>Definitions are (meta)equality axioms.\<close>
wenzelm@19761
   284
lemmas basic_defs = fst_def snd_def
wenzelm@19761
   285
wenzelm@63505
   286
text \<open>Compare with standard version: \<open>B\<close> is applied to UNSIMPLIFIED expression!\<close>
wenzelm@58977
   287
lemma SumIL2: "\<lbrakk>c = a : A; d = b : B(a)\<rbrakk> \<Longrightarrow> <c,d> = <a,b> : Sum(A,B)"
wenzelm@65338
   288
  by (rule sym_elem) (rule SumIL; rule sym_elem)
wenzelm@19761
   289
wenzelm@19761
   290
lemmas intrL2_rls = NI_succL ProdIL SumIL2 PlusI_inlL PlusI_inrL
wenzelm@19761
   291
wenzelm@63505
   292
text \<open>
wenzelm@63505
   293
  Exploit \<open>p:Prod(A,B)\<close> to create the assumption \<open>z:B(a)\<close>.
wenzelm@63505
   294
  A more natural form of product elimination.
wenzelm@63505
   295
\<close>
wenzelm@19761
   296
lemma subst_prodE:
wenzelm@19761
   297
  assumes "p: Prod(A,B)"
wenzelm@19761
   298
    and "a: A"
wenzelm@58977
   299
    and "\<And>z. z: B(a) \<Longrightarrow> c(z): C(z)"
wenzelm@19761
   300
  shows "c(p`a): C(p`a)"
wenzelm@63505
   301
  by (rule assms ProdE)+
wenzelm@19761
   302
wenzelm@19761
   303
wenzelm@60770
   304
subsection \<open>Tactics for type checking\<close>
wenzelm@19761
   305
wenzelm@60770
   306
ML \<open>
wenzelm@19761
   307
local
wenzelm@19761
   308
wenzelm@56250
   309
fun is_rigid_elem (Const(@{const_name Elem},_) $ a $ _) = not(is_Var (head_of a))
wenzelm@56250
   310
  | is_rigid_elem (Const(@{const_name Eqelem},_) $ a $ _ $ _) = not(is_Var (head_of a))
wenzelm@56250
   311
  | is_rigid_elem (Const(@{const_name Type},_) $ a) = not(is_Var (head_of a))
wenzelm@19761
   312
  | is_rigid_elem _ = false
wenzelm@19761
   313
wenzelm@19761
   314
in
wenzelm@19761
   315
wenzelm@19761
   316
(*Try solving a:A or a=b:A by assumption provided a is rigid!*)
wenzelm@63505
   317
fun test_assume_tac ctxt = SUBGOAL (fn (prem, i) =>
wenzelm@63505
   318
  if is_rigid_elem (Logic.strip_assums_concl prem)
wenzelm@63505
   319
  then assume_tac ctxt i else no_tac)
wenzelm@19761
   320
wenzelm@58963
   321
fun ASSUME ctxt tf i = test_assume_tac ctxt i  ORELSE  tf i
wenzelm@19761
   322
wenzelm@63505
   323
end
wenzelm@60770
   324
\<close>
wenzelm@19761
   325
wenzelm@63505
   326
text \<open>
wenzelm@63505
   327
  For simplification: type formation and checking,
wenzelm@63505
   328
  but no equalities between terms.
wenzelm@63505
   329
\<close>
wenzelm@19761
   330
lemmas routine_rls = form_rls formL_rls refl_type element_rls
wenzelm@19761
   331
wenzelm@60770
   332
ML \<open>
wenzelm@59164
   333
fun routine_tac rls ctxt prems =
wenzelm@59164
   334
  ASSUME ctxt (filt_resolve_from_net_tac ctxt 4 (Tactic.build_net (prems @ rls)));
wenzelm@19761
   335
wenzelm@19761
   336
(*Solve all subgoals "A type" using formation rules. *)
wenzelm@59164
   337
val form_net = Tactic.build_net @{thms form_rls};
wenzelm@59164
   338
fun form_tac ctxt =
wenzelm@59164
   339
  REPEAT_FIRST (ASSUME ctxt (filt_resolve_from_net_tac ctxt 1 form_net));
wenzelm@19761
   340
wenzelm@19761
   341
(*Type checking: solve a:A (a rigid, A flexible) by intro and elim rules. *)
wenzelm@58963
   342
fun typechk_tac ctxt thms =
wenzelm@59164
   343
  let val tac =
wenzelm@59164
   344
    filt_resolve_from_net_tac ctxt 3
wenzelm@59164
   345
      (Tactic.build_net (thms @ @{thms form_rls} @ @{thms element_rls}))
wenzelm@58963
   346
  in  REPEAT_FIRST (ASSUME ctxt tac)  end
wenzelm@19761
   347
wenzelm@19761
   348
(*Solve a:A (a flexible, A rigid) by introduction rules.
wenzelm@19761
   349
  Cannot use stringtrees (filt_resolve_tac) since
wenzelm@19761
   350
  goals like ?a:SUM(A,B) have a trivial head-string *)
wenzelm@58963
   351
fun intr_tac ctxt thms =
wenzelm@59164
   352
  let val tac =
wenzelm@59164
   353
    filt_resolve_from_net_tac ctxt 1
wenzelm@59164
   354
      (Tactic.build_net (thms @ @{thms form_rls} @ @{thms intr_rls}))
wenzelm@58963
   355
  in  REPEAT_FIRST (ASSUME ctxt tac)  end
wenzelm@19761
   356
wenzelm@19761
   357
(*Equality proving: solve a=b:A (where a is rigid) by long rules. *)
wenzelm@58963
   358
fun equal_tac ctxt thms =
wenzelm@59164
   359
  REPEAT_FIRST
wenzelm@63505
   360
    (ASSUME ctxt
wenzelm@63505
   361
      (filt_resolve_from_net_tac ctxt 3
wenzelm@63505
   362
        (Tactic.build_net (thms @ @{thms form_rls element_rls intrL_rls elimL_rls refl_elem}))))
wenzelm@60770
   363
\<close>
wenzelm@19761
   364
wenzelm@60770
   365
method_setup form = \<open>Scan.succeed (fn ctxt => SIMPLE_METHOD (form_tac ctxt))\<close>
wenzelm@60770
   366
method_setup typechk = \<open>Attrib.thms >> (fn ths => fn ctxt => SIMPLE_METHOD (typechk_tac ctxt ths))\<close>
wenzelm@60770
   367
method_setup intr = \<open>Attrib.thms >> (fn ths => fn ctxt => SIMPLE_METHOD (intr_tac ctxt ths))\<close>
wenzelm@60770
   368
method_setup equal = \<open>Attrib.thms >> (fn ths => fn ctxt => SIMPLE_METHOD (equal_tac ctxt ths))\<close>
wenzelm@19761
   369
wenzelm@19761
   370
wenzelm@60770
   371
subsection \<open>Simplification\<close>
wenzelm@19761
   372
wenzelm@63505
   373
text \<open>To simplify the type in a goal.\<close>
wenzelm@58977
   374
lemma replace_type: "\<lbrakk>B = A; a : A\<rbrakk> \<Longrightarrow> a : B"
wenzelm@63505
   375
  apply (rule equal_types)
wenzelm@63505
   376
   apply (rule_tac [2] sym_type)
wenzelm@63505
   377
   apply assumption+
wenzelm@63505
   378
  done
wenzelm@19761
   379
wenzelm@63505
   380
text \<open>Simplify the parameter of a unary type operator.\<close>
wenzelm@19761
   381
lemma subst_eqtyparg:
wenzelm@23467
   382
  assumes 1: "a=c : A"
wenzelm@58977
   383
    and 2: "\<And>z. z:A \<Longrightarrow> B(z) type"
wenzelm@63505
   384
  shows "B(a) = B(c)"
wenzelm@63505
   385
  apply (rule subst_typeL)
wenzelm@63505
   386
   apply (rule_tac [2] refl_type)
wenzelm@63505
   387
   apply (rule 1)
wenzelm@63505
   388
  apply (erule 2)
wenzelm@63505
   389
  done
wenzelm@19761
   390
wenzelm@63505
   391
text \<open>Simplification rules for Constructive Type Theory.\<close>
wenzelm@19761
   392
lemmas reduction_rls = comp_rls [THEN trans_elem]
wenzelm@19761
   393
wenzelm@60770
   394
ML \<open>
wenzelm@19761
   395
(*Converts each goal "e : Eq(A,a,b)" into "a=b:A" for simplification.
wenzelm@19761
   396
  Uses other intro rules to avoid changing flexible goals.*)
wenzelm@59164
   397
val eqintr_net = Tactic.build_net @{thms EqI intr_rls}
wenzelm@58963
   398
fun eqintr_tac ctxt =
wenzelm@59164
   399
  REPEAT_FIRST (ASSUME ctxt (filt_resolve_from_net_tac ctxt 1 eqintr_net))
wenzelm@19761
   400
wenzelm@19761
   401
(** Tactics that instantiate CTT-rules.
wenzelm@19761
   402
    Vars in the given terms will be incremented!
wenzelm@19761
   403
    The (rtac EqE i) lets them apply to equality judgements. **)
wenzelm@19761
   404
wenzelm@27208
   405
fun NE_tac ctxt sp i =
wenzelm@60754
   406
  TRY (resolve_tac ctxt @{thms EqE} i) THEN
wenzelm@59780
   407
  Rule_Insts.res_inst_tac ctxt [((("p", 0), Position.none), sp)] [] @{thm NE} i
wenzelm@19761
   408
wenzelm@27208
   409
fun SumE_tac ctxt sp i =
wenzelm@60754
   410
  TRY (resolve_tac ctxt @{thms EqE} i) THEN
wenzelm@59780
   411
  Rule_Insts.res_inst_tac ctxt [((("p", 0), Position.none), sp)] [] @{thm SumE} i
wenzelm@19761
   412
wenzelm@27208
   413
fun PlusE_tac ctxt sp i =
wenzelm@60754
   414
  TRY (resolve_tac ctxt @{thms EqE} i) THEN
wenzelm@59780
   415
  Rule_Insts.res_inst_tac ctxt [((("p", 0), Position.none), sp)] [] @{thm PlusE} i
wenzelm@19761
   416
wenzelm@19761
   417
(** Predicate logic reasoning, WITH THINNING!!  Procedures adapted from NJ. **)
wenzelm@19761
   418
wenzelm@19761
   419
(*Finds f:Prod(A,B) and a:A in the assumptions, concludes there is z:B(a) *)
wenzelm@58963
   420
fun add_mp_tac ctxt i =
wenzelm@60754
   421
  resolve_tac ctxt @{thms subst_prodE} i  THEN  assume_tac ctxt i  THEN  assume_tac ctxt i
wenzelm@19761
   422
wenzelm@61391
   423
(*Finds P\<longrightarrow>Q and P in the assumptions, replaces implication by Q *)
wenzelm@60754
   424
fun mp_tac ctxt i = eresolve_tac ctxt @{thms subst_prodE} i  THEN  assume_tac ctxt i
wenzelm@19761
   425
wenzelm@19761
   426
(*"safe" when regarded as predicate calculus rules*)
wenzelm@19761
   427
val safe_brls = sort (make_ord lessb)
wenzelm@27208
   428
    [ (true, @{thm FE}), (true,asm_rl),
wenzelm@27208
   429
      (false, @{thm ProdI}), (true, @{thm SumE}), (true, @{thm PlusE}) ]
wenzelm@19761
   430
wenzelm@19761
   431
val unsafe_brls =
wenzelm@27208
   432
    [ (false, @{thm PlusI_inl}), (false, @{thm PlusI_inr}), (false, @{thm SumI}),
wenzelm@27208
   433
      (true, @{thm subst_prodE}) ]
wenzelm@19761
   434
wenzelm@19761
   435
(*0 subgoals vs 1 or more*)
wenzelm@19761
   436
val (safe0_brls, safep_brls) =
wenzelm@19761
   437
    List.partition (curry (op =) 0 o subgoals_of_brl) safe_brls
wenzelm@19761
   438
wenzelm@58963
   439
fun safestep_tac ctxt thms i =
wenzelm@58963
   440
    form_tac ctxt ORELSE
wenzelm@59498
   441
    resolve_tac ctxt thms i  ORELSE
wenzelm@59498
   442
    biresolve_tac ctxt safe0_brls i  ORELSE  mp_tac ctxt i  ORELSE
wenzelm@59498
   443
    DETERM (biresolve_tac ctxt safep_brls i)
wenzelm@19761
   444
wenzelm@58963
   445
fun safe_tac ctxt thms i = DEPTH_SOLVE_1 (safestep_tac ctxt thms i)
wenzelm@19761
   446
wenzelm@59498
   447
fun step_tac ctxt thms = safestep_tac ctxt thms  ORELSE'  biresolve_tac ctxt unsafe_brls
wenzelm@19761
   448
wenzelm@19761
   449
(*Fails unless it solves the goal!*)
wenzelm@58963
   450
fun pc_tac ctxt thms = DEPTH_SOLVE_1 o (step_tac ctxt thms)
wenzelm@60770
   451
\<close>
wenzelm@19761
   452
wenzelm@60770
   453
method_setup eqintr = \<open>Scan.succeed (SIMPLE_METHOD o eqintr_tac)\<close>
wenzelm@60770
   454
method_setup NE = \<open>
wenzelm@63120
   455
  Scan.lift Args.embedded_inner_syntax >> (fn s => fn ctxt => SIMPLE_METHOD' (NE_tac ctxt s))
wenzelm@60770
   456
\<close>
wenzelm@60770
   457
method_setup pc = \<open>Attrib.thms >> (fn ths => fn ctxt => SIMPLE_METHOD' (pc_tac ctxt ths))\<close>
wenzelm@60770
   458
method_setup add_mp = \<open>Scan.succeed (SIMPLE_METHOD' o add_mp_tac)\<close>
wenzelm@58972
   459
wenzelm@48891
   460
ML_file "rew.ML"
wenzelm@60770
   461
method_setup rew = \<open>Attrib.thms >> (fn ths => fn ctxt => SIMPLE_METHOD (rew_tac ctxt ths))\<close>
wenzelm@60770
   462
method_setup hyp_rew = \<open>Attrib.thms >> (fn ths => fn ctxt => SIMPLE_METHOD (hyp_rew_tac ctxt ths))\<close>
wenzelm@58972
   463
wenzelm@19761
   464
wenzelm@60770
   465
subsection \<open>The elimination rules for fst/snd\<close>
wenzelm@19761
   466
wenzelm@58977
   467
lemma SumE_fst: "p : Sum(A,B) \<Longrightarrow> fst(p) : A"
wenzelm@63505
   468
  apply (unfold basic_defs)
wenzelm@63505
   469
  apply (erule SumE)
wenzelm@63505
   470
  apply assumption
wenzelm@63505
   471
  done
wenzelm@19761
   472
wenzelm@63505
   473
text \<open>The first premise must be \<open>p:Sum(A,B)\<close>!!.\<close>
wenzelm@19761
   474
lemma SumE_snd:
wenzelm@19761
   475
  assumes major: "p: Sum(A,B)"
wenzelm@19761
   476
    and "A type"
wenzelm@58977
   477
    and "\<And>x. x:A \<Longrightarrow> B(x) type"
wenzelm@19761
   478
  shows "snd(p) : B(fst(p))"
wenzelm@19761
   479
  apply (unfold basic_defs)
wenzelm@19761
   480
  apply (rule major [THEN SumE])
wenzelm@19761
   481
  apply (rule SumC [THEN subst_eqtyparg, THEN replace_type])
wenzelm@63505
   482
      apply (typechk assms)
wenzelm@19761
   483
  done
wenzelm@19761
   484
wenzelm@65447
   485
wenzelm@65447
   486
section \<open>The two-element type (booleans and conditionals)\<close>
wenzelm@65447
   487
wenzelm@65447
   488
definition Bool :: "t"
wenzelm@65447
   489
  where "Bool \<equiv> T+T"
wenzelm@65447
   490
wenzelm@65447
   491
definition true :: "i"
wenzelm@65447
   492
  where "true \<equiv> inl(tt)"
wenzelm@65447
   493
wenzelm@65447
   494
definition false :: "i"
wenzelm@65447
   495
  where "false \<equiv> inr(tt)"
wenzelm@65447
   496
wenzelm@65447
   497
definition cond :: "[i,i,i]\<Rightarrow>i"
wenzelm@65447
   498
  where "cond(a,b,c) \<equiv> when(a, \<lambda>_. b, \<lambda>_. c)"
wenzelm@65447
   499
wenzelm@65447
   500
lemmas bool_defs = Bool_def true_def false_def cond_def
wenzelm@65447
   501
wenzelm@65447
   502
wenzelm@65447
   503
subsection \<open>Derivation of rules for the type \<open>Bool\<close>\<close>
wenzelm@65447
   504
wenzelm@65447
   505
text \<open>Formation rule.\<close>
wenzelm@65447
   506
lemma boolF: "Bool type"
wenzelm@65447
   507
  unfolding bool_defs by typechk
wenzelm@65447
   508
wenzelm@65447
   509
text \<open>Introduction rules for \<open>true\<close>, \<open>false\<close>.\<close>
wenzelm@65447
   510
wenzelm@65447
   511
lemma boolI_true: "true : Bool"
wenzelm@65447
   512
  unfolding bool_defs by typechk
wenzelm@65447
   513
wenzelm@65447
   514
lemma boolI_false: "false : Bool"
wenzelm@65447
   515
  unfolding bool_defs by typechk
wenzelm@65447
   516
wenzelm@65447
   517
text \<open>Elimination rule: typing of \<open>cond\<close>.\<close>
wenzelm@65447
   518
lemma boolE: "\<lbrakk>p:Bool; a : C(true); b : C(false)\<rbrakk> \<Longrightarrow> cond(p,a,b) : C(p)"
wenzelm@65447
   519
  unfolding bool_defs
wenzelm@65447
   520
  apply (typechk; erule TE)
wenzelm@65447
   521
   apply typechk
wenzelm@65447
   522
  done
wenzelm@65447
   523
wenzelm@65447
   524
lemma boolEL: "\<lbrakk>p = q : Bool; a = c : C(true); b = d : C(false)\<rbrakk>
wenzelm@65447
   525
  \<Longrightarrow> cond(p,a,b) = cond(q,c,d) : C(p)"
wenzelm@65447
   526
  unfolding bool_defs
wenzelm@65447
   527
  apply (rule PlusEL)
wenzelm@65447
   528
    apply (erule asm_rl refl_elem [THEN TEL])+
wenzelm@65447
   529
  done
wenzelm@65447
   530
wenzelm@65447
   531
text \<open>Computation rules for \<open>true\<close>, \<open>false\<close>.\<close>
wenzelm@65447
   532
wenzelm@65447
   533
lemma boolC_true: "\<lbrakk>a : C(true); b : C(false)\<rbrakk> \<Longrightarrow> cond(true,a,b) = a : C(true)"
wenzelm@65447
   534
  unfolding bool_defs
wenzelm@65447
   535
  apply (rule comp_rls)
wenzelm@65447
   536
    apply typechk
wenzelm@65447
   537
   apply (erule_tac [!] TE)
wenzelm@65447
   538
   apply typechk
wenzelm@65447
   539
  done
wenzelm@65447
   540
wenzelm@65447
   541
lemma boolC_false: "\<lbrakk>a : C(true); b : C(false)\<rbrakk> \<Longrightarrow> cond(false,a,b) = b : C(false)"
wenzelm@65447
   542
  unfolding bool_defs
wenzelm@65447
   543
  apply (rule comp_rls)
wenzelm@65447
   544
    apply typechk
wenzelm@65447
   545
   apply (erule_tac [!] TE)
wenzelm@65447
   546
   apply typechk
wenzelm@65447
   547
  done
wenzelm@65447
   548
wenzelm@65447
   549
section \<open>Elementary arithmetic\<close>
wenzelm@65447
   550
wenzelm@65447
   551
subsection \<open>Arithmetic operators and their definitions\<close>
wenzelm@65447
   552
wenzelm@65447
   553
definition add :: "[i,i]\<Rightarrow>i"   (infixr "#+" 65)
wenzelm@65447
   554
  where "a#+b \<equiv> rec(a, b, \<lambda>u v. succ(v))"
wenzelm@65447
   555
wenzelm@65447
   556
definition diff :: "[i,i]\<Rightarrow>i"   (infixr "-" 65)
wenzelm@65447
   557
  where "a-b \<equiv> rec(b, a, \<lambda>u v. rec(v, 0, \<lambda>x y. x))"
wenzelm@65447
   558
wenzelm@65447
   559
definition absdiff :: "[i,i]\<Rightarrow>i"   (infixr "|-|" 65)
wenzelm@65447
   560
  where "a|-|b \<equiv> (a-b) #+ (b-a)"
wenzelm@65447
   561
wenzelm@65447
   562
definition mult :: "[i,i]\<Rightarrow>i"   (infixr "#*" 70)
wenzelm@65447
   563
  where "a#*b \<equiv> rec(a, 0, \<lambda>u v. b #+ v)"
wenzelm@65447
   564
wenzelm@65447
   565
definition mod :: "[i,i]\<Rightarrow>i"   (infixr "mod" 70)
wenzelm@65447
   566
  where "a mod b \<equiv> rec(a, 0, \<lambda>u v. rec(succ(v) |-| b, 0, \<lambda>x y. succ(v)))"
wenzelm@65447
   567
wenzelm@65447
   568
definition div :: "[i,i]\<Rightarrow>i"   (infixr "div" 70)
wenzelm@65447
   569
  where "a div b \<equiv> rec(a, 0, \<lambda>u v. rec(succ(u) mod b, succ(v), \<lambda>x y. v))"
wenzelm@65447
   570
wenzelm@65447
   571
lemmas arith_defs = add_def diff_def absdiff_def mult_def mod_def div_def
wenzelm@65447
   572
wenzelm@65447
   573
wenzelm@65447
   574
subsection \<open>Proofs about elementary arithmetic: addition, multiplication, etc.\<close>
wenzelm@65447
   575
wenzelm@65447
   576
subsubsection \<open>Addition\<close>
wenzelm@65447
   577
wenzelm@65447
   578
text \<open>Typing of \<open>add\<close>: short and long versions.\<close>
wenzelm@65447
   579
wenzelm@65447
   580
lemma add_typing: "\<lbrakk>a:N; b:N\<rbrakk> \<Longrightarrow> a #+ b : N"
wenzelm@65447
   581
  unfolding arith_defs by typechk
wenzelm@65447
   582
wenzelm@65447
   583
lemma add_typingL: "\<lbrakk>a = c:N; b = d:N\<rbrakk> \<Longrightarrow> a #+ b = c #+ d : N"
wenzelm@65447
   584
  unfolding arith_defs by equal
wenzelm@65447
   585
wenzelm@65447
   586
wenzelm@65447
   587
text \<open>Computation for \<open>add\<close>: 0 and successor cases.\<close>
wenzelm@65447
   588
wenzelm@65447
   589
lemma addC0: "b:N \<Longrightarrow> 0 #+ b = b : N"
wenzelm@65447
   590
  unfolding arith_defs by rew
wenzelm@65447
   591
wenzelm@65447
   592
lemma addC_succ: "\<lbrakk>a:N; b:N\<rbrakk> \<Longrightarrow> succ(a) #+ b = succ(a #+ b) : N"
wenzelm@65447
   593
  unfolding arith_defs by rew
wenzelm@65447
   594
wenzelm@65447
   595
wenzelm@65447
   596
subsubsection \<open>Multiplication\<close>
wenzelm@65447
   597
wenzelm@65447
   598
text \<open>Typing of \<open>mult\<close>: short and long versions.\<close>
wenzelm@65447
   599
wenzelm@65447
   600
lemma mult_typing: "\<lbrakk>a:N; b:N\<rbrakk> \<Longrightarrow> a #* b : N"
wenzelm@65447
   601
  unfolding arith_defs by (typechk add_typing)
wenzelm@65447
   602
wenzelm@65447
   603
lemma mult_typingL: "\<lbrakk>a = c:N; b = d:N\<rbrakk> \<Longrightarrow> a #* b = c #* d : N"
wenzelm@65447
   604
  unfolding arith_defs by (equal add_typingL)
wenzelm@65447
   605
wenzelm@65447
   606
wenzelm@65447
   607
text \<open>Computation for \<open>mult\<close>: 0 and successor cases.\<close>
wenzelm@65447
   608
wenzelm@65447
   609
lemma multC0: "b:N \<Longrightarrow> 0 #* b = 0 : N"
wenzelm@65447
   610
  unfolding arith_defs by rew
wenzelm@65447
   611
wenzelm@65447
   612
lemma multC_succ: "\<lbrakk>a:N; b:N\<rbrakk> \<Longrightarrow> succ(a) #* b = b #+ (a #* b) : N"
wenzelm@65447
   613
  unfolding arith_defs by rew
wenzelm@65447
   614
wenzelm@65447
   615
wenzelm@65447
   616
subsubsection \<open>Difference\<close>
wenzelm@65447
   617
wenzelm@65447
   618
text \<open>Typing of difference.\<close>
wenzelm@65447
   619
wenzelm@65447
   620
lemma diff_typing: "\<lbrakk>a:N; b:N\<rbrakk> \<Longrightarrow> a - b : N"
wenzelm@65447
   621
  unfolding arith_defs by typechk
wenzelm@65447
   622
wenzelm@65447
   623
lemma diff_typingL: "\<lbrakk>a = c:N; b = d:N\<rbrakk> \<Longrightarrow> a - b = c - d : N"
wenzelm@65447
   624
  unfolding arith_defs by equal
wenzelm@65447
   625
wenzelm@65447
   626
wenzelm@65447
   627
text \<open>Computation for difference: 0 and successor cases.\<close>
wenzelm@65447
   628
wenzelm@65447
   629
lemma diffC0: "a:N \<Longrightarrow> a - 0 = a : N"
wenzelm@65447
   630
  unfolding arith_defs by rew
wenzelm@65447
   631
wenzelm@65447
   632
text \<open>Note: \<open>rec(a, 0, \<lambda>z w.z)\<close> is \<open>pred(a).\<close>\<close>
wenzelm@65447
   633
wenzelm@65447
   634
lemma diff_0_eq_0: "b:N \<Longrightarrow> 0 - b = 0 : N"
wenzelm@65447
   635
  unfolding arith_defs
wenzelm@65447
   636
  apply (NE b)
wenzelm@65447
   637
    apply hyp_rew
wenzelm@65447
   638
  done
wenzelm@65447
   639
wenzelm@65447
   640
text \<open>
wenzelm@65447
   641
  Essential to simplify FIRST!!  (Else we get a critical pair)
wenzelm@65447
   642
  \<open>succ(a) - succ(b)\<close> rewrites to \<open>pred(succ(a) - b)\<close>.
wenzelm@65447
   643
\<close>
wenzelm@65447
   644
lemma diff_succ_succ: "\<lbrakk>a:N; b:N\<rbrakk> \<Longrightarrow> succ(a) - succ(b) = a - b : N"
wenzelm@65447
   645
  unfolding arith_defs
wenzelm@65447
   646
  apply hyp_rew
wenzelm@65447
   647
  apply (NE b)
wenzelm@65447
   648
    apply hyp_rew
wenzelm@65447
   649
  done
wenzelm@65447
   650
wenzelm@65447
   651
wenzelm@65447
   652
subsection \<open>Simplification\<close>
wenzelm@65447
   653
wenzelm@65447
   654
lemmas arith_typing_rls = add_typing mult_typing diff_typing
wenzelm@65447
   655
  and arith_congr_rls = add_typingL mult_typingL diff_typingL
wenzelm@65447
   656
wenzelm@65447
   657
lemmas congr_rls = arith_congr_rls intrL2_rls elimL_rls
wenzelm@65447
   658
wenzelm@65447
   659
lemmas arithC_rls =
wenzelm@65447
   660
  addC0 addC_succ
wenzelm@65447
   661
  multC0 multC_succ
wenzelm@65447
   662
  diffC0 diff_0_eq_0 diff_succ_succ
wenzelm@65447
   663
wenzelm@65447
   664
ML \<open>
wenzelm@65447
   665
  structure Arith_simp = TSimpFun(
wenzelm@65447
   666
    val refl = @{thm refl_elem}
wenzelm@65447
   667
    val sym = @{thm sym_elem}
wenzelm@65447
   668
    val trans = @{thm trans_elem}
wenzelm@65447
   669
    val refl_red = @{thm refl_red}
wenzelm@65447
   670
    val trans_red = @{thm trans_red}
wenzelm@65447
   671
    val red_if_equal = @{thm red_if_equal}
wenzelm@65447
   672
    val default_rls = @{thms arithC_rls comp_rls}
wenzelm@65447
   673
    val routine_tac = routine_tac @{thms arith_typing_rls routine_rls}
wenzelm@65447
   674
  )
wenzelm@65447
   675
wenzelm@65447
   676
  fun arith_rew_tac ctxt prems =
wenzelm@65447
   677
    make_rew_tac ctxt (Arith_simp.norm_tac ctxt (@{thms congr_rls}, prems))
wenzelm@65447
   678
wenzelm@65447
   679
  fun hyp_arith_rew_tac ctxt prems =
wenzelm@65447
   680
    make_rew_tac ctxt
wenzelm@65447
   681
      (Arith_simp.cond_norm_tac ctxt (prove_cond_tac ctxt, @{thms congr_rls}, prems))
wenzelm@65447
   682
\<close>
wenzelm@65447
   683
wenzelm@65447
   684
method_setup arith_rew = \<open>
wenzelm@65447
   685
  Attrib.thms >> (fn ths => fn ctxt => SIMPLE_METHOD (arith_rew_tac ctxt ths))
wenzelm@65447
   686
\<close>
wenzelm@65447
   687
wenzelm@65447
   688
method_setup hyp_arith_rew = \<open>
wenzelm@65447
   689
  Attrib.thms >> (fn ths => fn ctxt => SIMPLE_METHOD (hyp_arith_rew_tac ctxt ths))
wenzelm@65447
   690
\<close>
wenzelm@65447
   691
wenzelm@65447
   692
wenzelm@65447
   693
subsection \<open>Addition\<close>
wenzelm@65447
   694
wenzelm@65447
   695
text \<open>Associative law for addition.\<close>
wenzelm@65447
   696
lemma add_assoc: "\<lbrakk>a:N; b:N; c:N\<rbrakk> \<Longrightarrow> (a #+ b) #+ c = a #+ (b #+ c) : N"
wenzelm@65447
   697
  apply (NE a)
wenzelm@65447
   698
    apply hyp_arith_rew
wenzelm@65447
   699
  done
wenzelm@65447
   700
wenzelm@65447
   701
text \<open>Commutative law for addition.  Can be proved using three inductions.
wenzelm@65447
   702
  Must simplify after first induction!  Orientation of rewrites is delicate.\<close>
wenzelm@65447
   703
lemma add_commute: "\<lbrakk>a:N; b:N\<rbrakk> \<Longrightarrow> a #+ b = b #+ a : N"
wenzelm@65447
   704
  apply (NE a)
wenzelm@65447
   705
    apply hyp_arith_rew
wenzelm@65447
   706
   apply (rule sym_elem)
wenzelm@65447
   707
   prefer 2
wenzelm@65447
   708
   apply (NE b)
wenzelm@65447
   709
     prefer 4
wenzelm@65447
   710
     apply (NE b)
wenzelm@65447
   711
       apply hyp_arith_rew
wenzelm@65447
   712
  done
wenzelm@65447
   713
wenzelm@65447
   714
wenzelm@65447
   715
subsection \<open>Multiplication\<close>
wenzelm@65447
   716
wenzelm@65447
   717
text \<open>Right annihilation in product.\<close>
wenzelm@65447
   718
lemma mult_0_right: "a:N \<Longrightarrow> a #* 0 = 0 : N"
wenzelm@65447
   719
  apply (NE a)
wenzelm@65447
   720
    apply hyp_arith_rew
wenzelm@65447
   721
  done
wenzelm@65447
   722
wenzelm@65447
   723
text \<open>Right successor law for multiplication.\<close>
wenzelm@65447
   724
lemma mult_succ_right: "\<lbrakk>a:N; b:N\<rbrakk> \<Longrightarrow> a #* succ(b) = a #+ (a #* b) : N"
wenzelm@65447
   725
  apply (NE a)
wenzelm@65447
   726
    apply (hyp_arith_rew add_assoc [THEN sym_elem])
wenzelm@65447
   727
  apply (assumption | rule add_commute mult_typingL add_typingL intrL_rls refl_elem)+
wenzelm@65447
   728
  done
wenzelm@65447
   729
wenzelm@65447
   730
text \<open>Commutative law for multiplication.\<close>
wenzelm@65447
   731
lemma mult_commute: "\<lbrakk>a:N; b:N\<rbrakk> \<Longrightarrow> a #* b = b #* a : N"
wenzelm@65447
   732
  apply (NE a)
wenzelm@65447
   733
    apply (hyp_arith_rew mult_0_right mult_succ_right)
wenzelm@65447
   734
  done
wenzelm@65447
   735
wenzelm@65447
   736
text \<open>Addition distributes over multiplication.\<close>
wenzelm@65447
   737
lemma add_mult_distrib: "\<lbrakk>a:N; b:N; c:N\<rbrakk> \<Longrightarrow> (a #+ b) #* c = (a #* c) #+ (b #* c) : N"
wenzelm@65447
   738
  apply (NE a)
wenzelm@65447
   739
    apply (hyp_arith_rew add_assoc [THEN sym_elem])
wenzelm@65447
   740
  done
wenzelm@65447
   741
wenzelm@65447
   742
text \<open>Associative law for multiplication.\<close>
wenzelm@65447
   743
lemma mult_assoc: "\<lbrakk>a:N; b:N; c:N\<rbrakk> \<Longrightarrow> (a #* b) #* c = a #* (b #* c) : N"
wenzelm@65447
   744
  apply (NE a)
wenzelm@65447
   745
    apply (hyp_arith_rew add_mult_distrib)
wenzelm@65447
   746
  done
wenzelm@65447
   747
wenzelm@65447
   748
wenzelm@65447
   749
subsection \<open>Difference\<close>
wenzelm@65447
   750
wenzelm@65447
   751
text \<open>
wenzelm@65447
   752
  Difference on natural numbers, without negative numbers
wenzelm@65447
   753
  \<^item> \<open>a - b = 0\<close>  iff  \<open>a \<le> b\<close>
wenzelm@65447
   754
  \<^item> \<open>a - b = succ(c)\<close> iff \<open>a > b\<close>
wenzelm@65447
   755
\<close>
wenzelm@65447
   756
wenzelm@65447
   757
lemma diff_self_eq_0: "a:N \<Longrightarrow> a - a = 0 : N"
wenzelm@65447
   758
  apply (NE a)
wenzelm@65447
   759
    apply hyp_arith_rew
wenzelm@65447
   760
  done
wenzelm@65447
   761
wenzelm@65447
   762
wenzelm@65447
   763
lemma add_0_right: "\<lbrakk>c : N; 0 : N; c : N\<rbrakk> \<Longrightarrow> c #+ 0 = c : N"
wenzelm@65447
   764
  by (rule addC0 [THEN [3] add_commute [THEN trans_elem]])
wenzelm@65447
   765
wenzelm@65447
   766
text \<open>
wenzelm@65447
   767
  Addition is the inverse of subtraction: if \<open>b \<le> x\<close> then \<open>b #+ (x - b) = x\<close>.
wenzelm@65447
   768
  An example of induction over a quantified formula (a product).
wenzelm@65447
   769
  Uses rewriting with a quantified, implicative inductive hypothesis.
wenzelm@65447
   770
\<close>
wenzelm@65447
   771
schematic_goal add_diff_inverse_lemma:
wenzelm@65447
   772
  "b:N \<Longrightarrow> ?a : \<Prod>x:N. Eq(N, b-x, 0) \<longrightarrow> Eq(N, b #+ (x-b), x)"
wenzelm@65447
   773
  apply (NE b)
wenzelm@65447
   774
    \<comment> \<open>strip one "universal quantifier" but not the "implication"\<close>
wenzelm@65447
   775
    apply (rule_tac [3] intr_rls)
wenzelm@65447
   776
    \<comment> \<open>case analysis on \<open>x\<close> in \<open>succ(u) \<le> x \<longrightarrow> succ(u) #+ (x - succ(u)) = x\<close>\<close>
wenzelm@65447
   777
     prefer 4
wenzelm@65447
   778
     apply (NE x)
wenzelm@65447
   779
       apply assumption
wenzelm@65447
   780
    \<comment> \<open>Prepare for simplification of types -- the antecedent \<open>succ(u) \<le> x\<close>\<close>
wenzelm@65447
   781
      apply (rule_tac [2] replace_type)
wenzelm@65447
   782
       apply (rule_tac [1] replace_type)
wenzelm@65447
   783
        apply arith_rew
wenzelm@65447
   784
    \<comment> \<open>Solves first 0 goal, simplifies others.  Two sugbgoals remain.
wenzelm@65447
   785
    Both follow by rewriting, (2) using quantified induction hyp.\<close>
wenzelm@65447
   786
   apply intr \<comment> \<open>strips remaining \<open>\<Prod>\<close>s\<close>
wenzelm@65447
   787
    apply (hyp_arith_rew add_0_right)
wenzelm@65447
   788
  apply assumption
wenzelm@65447
   789
  done
wenzelm@65447
   790
wenzelm@65447
   791
text \<open>
wenzelm@65447
   792
  Version of above with premise \<open>b - a = 0\<close> i.e. \<open>a \<ge> b\<close>.
wenzelm@65447
   793
  Using @{thm ProdE} does not work -- for \<open>?B(?a)\<close> is ambiguous.
wenzelm@65447
   794
  Instead, @{thm add_diff_inverse_lemma} states the desired induction scheme;
wenzelm@65447
   795
  the use of \<open>THEN\<close> below instantiates Vars in @{thm ProdE} automatically.
wenzelm@65447
   796
\<close>
wenzelm@65447
   797
lemma add_diff_inverse: "\<lbrakk>a:N; b:N; b - a = 0 : N\<rbrakk> \<Longrightarrow> b #+ (a-b) = a : N"
wenzelm@65447
   798
  apply (rule EqE)
wenzelm@65447
   799
  apply (rule add_diff_inverse_lemma [THEN ProdE, THEN ProdE])
wenzelm@65447
   800
    apply (assumption | rule EqI)+
wenzelm@65447
   801
  done
wenzelm@65447
   802
wenzelm@65447
   803
wenzelm@65447
   804
subsection \<open>Absolute difference\<close>
wenzelm@65447
   805
wenzelm@65447
   806
text \<open>Typing of absolute difference: short and long versions.\<close>
wenzelm@65447
   807
wenzelm@65447
   808
lemma absdiff_typing: "\<lbrakk>a:N; b:N\<rbrakk> \<Longrightarrow> a |-| b : N"
wenzelm@65447
   809
  unfolding arith_defs by typechk
wenzelm@65447
   810
wenzelm@65447
   811
lemma absdiff_typingL: "\<lbrakk>a = c:N; b = d:N\<rbrakk> \<Longrightarrow> a |-| b = c |-| d : N"
wenzelm@65447
   812
  unfolding arith_defs by equal
wenzelm@65447
   813
wenzelm@65447
   814
lemma absdiff_self_eq_0: "a:N \<Longrightarrow> a |-| a = 0 : N"
wenzelm@65447
   815
  unfolding absdiff_def by (arith_rew diff_self_eq_0)
wenzelm@65447
   816
wenzelm@65447
   817
lemma absdiffC0: "a:N \<Longrightarrow> 0 |-| a = a : N"
wenzelm@65447
   818
  unfolding absdiff_def by hyp_arith_rew
wenzelm@65447
   819
wenzelm@65447
   820
lemma absdiff_succ_succ: "\<lbrakk>a:N; b:N\<rbrakk> \<Longrightarrow> succ(a) |-| succ(b)  =  a |-| b : N"
wenzelm@65447
   821
  unfolding absdiff_def by hyp_arith_rew
wenzelm@65447
   822
wenzelm@65447
   823
text \<open>Note how easy using commutative laws can be?  ...not always...\<close>
wenzelm@65447
   824
lemma absdiff_commute: "\<lbrakk>a:N; b:N\<rbrakk> \<Longrightarrow> a |-| b = b |-| a : N"
wenzelm@65447
   825
  unfolding absdiff_def
wenzelm@65447
   826
  apply (rule add_commute)
wenzelm@65447
   827
   apply (typechk diff_typing)
wenzelm@65447
   828
  done
wenzelm@65447
   829
wenzelm@65447
   830
text \<open>If \<open>a + b = 0\<close> then \<open>a = 0\<close>. Surprisingly tedious.\<close>
wenzelm@65447
   831
schematic_goal add_eq0_lemma: "\<lbrakk>a:N; b:N\<rbrakk> \<Longrightarrow> ?c : Eq(N,a#+b,0) \<longrightarrow> Eq(N,a,0)"
wenzelm@65447
   832
  apply (NE a)
wenzelm@65447
   833
    apply (rule_tac [3] replace_type)
wenzelm@65447
   834
     apply arith_rew
wenzelm@65447
   835
  apply intr  \<comment> \<open>strips remaining \<open>\<Prod>\<close>s\<close>
wenzelm@65447
   836
   apply (rule_tac [2] zero_ne_succ [THEN FE])
wenzelm@65447
   837
     apply (erule_tac [3] EqE [THEN sym_elem])
wenzelm@65447
   838
    apply (typechk add_typing)
wenzelm@65447
   839
  done
wenzelm@65447
   840
wenzelm@65447
   841
text \<open>
wenzelm@65447
   842
  Version of above with the premise \<open>a + b = 0\<close>.
wenzelm@65447
   843
  Again, resolution instantiates variables in @{thm ProdE}.
wenzelm@65447
   844
\<close>
wenzelm@65447
   845
lemma add_eq0: "\<lbrakk>a:N; b:N; a #+ b = 0 : N\<rbrakk> \<Longrightarrow> a = 0 : N"
wenzelm@65447
   846
  apply (rule EqE)
wenzelm@65447
   847
  apply (rule add_eq0_lemma [THEN ProdE])
wenzelm@65447
   848
    apply (rule_tac [3] EqI)
wenzelm@65447
   849
    apply typechk
wenzelm@65447
   850
  done
wenzelm@65447
   851
wenzelm@65447
   852
text \<open>Here is a lemma to infer \<open>a - b = 0\<close> and \<open>b - a = 0\<close> from \<open>a |-| b = 0\<close>, below.\<close>
wenzelm@65447
   853
schematic_goal absdiff_eq0_lem:
wenzelm@65447
   854
  "\<lbrakk>a:N; b:N; a |-| b = 0 : N\<rbrakk> \<Longrightarrow> ?a : Eq(N, a-b, 0) \<times> Eq(N, b-a, 0)"
wenzelm@65447
   855
  apply (unfold absdiff_def)
wenzelm@65447
   856
  apply intr
wenzelm@65447
   857
   apply eqintr
wenzelm@65447
   858
   apply (rule_tac [2] add_eq0)
wenzelm@65447
   859
     apply (rule add_eq0)
wenzelm@65447
   860
       apply (rule_tac [6] add_commute [THEN trans_elem])
wenzelm@65447
   861
         apply (typechk diff_typing)
wenzelm@65447
   862
  done
wenzelm@65447
   863
wenzelm@65447
   864
text \<open>If \<open>a |-| b = 0\<close> then \<open>a = b\<close>
wenzelm@65447
   865
  proof: \<open>a - b = 0\<close> and \<open>b - a = 0\<close>, so \<open>b = a + (b - a) = a + 0 = a\<close>.
wenzelm@65447
   866
\<close>
wenzelm@65447
   867
lemma absdiff_eq0: "\<lbrakk>a |-| b = 0 : N; a:N; b:N\<rbrakk> \<Longrightarrow> a = b : N"
wenzelm@65447
   868
  apply (rule EqE)
wenzelm@65447
   869
  apply (rule absdiff_eq0_lem [THEN SumE])
wenzelm@65447
   870
     apply eqintr
wenzelm@65447
   871
  apply (rule add_diff_inverse [THEN sym_elem, THEN trans_elem])
wenzelm@65447
   872
     apply (erule_tac [3] EqE)
wenzelm@65447
   873
    apply (hyp_arith_rew add_0_right)
wenzelm@65447
   874
  done
wenzelm@65447
   875
wenzelm@65447
   876
wenzelm@65447
   877
subsection \<open>Remainder and Quotient\<close>
wenzelm@65447
   878
wenzelm@65447
   879
text \<open>Typing of remainder: short and long versions.\<close>
wenzelm@65447
   880
wenzelm@65447
   881
lemma mod_typing: "\<lbrakk>a:N; b:N\<rbrakk> \<Longrightarrow> a mod b : N"
wenzelm@65447
   882
  unfolding mod_def by (typechk absdiff_typing)
wenzelm@65447
   883
wenzelm@65447
   884
lemma mod_typingL: "\<lbrakk>a = c:N; b = d:N\<rbrakk> \<Longrightarrow> a mod b = c mod d : N"
wenzelm@65447
   885
  unfolding mod_def by (equal absdiff_typingL)
wenzelm@65447
   886
wenzelm@65447
   887
wenzelm@65447
   888
text \<open>Computation for \<open>mod\<close>: 0 and successor cases.\<close>
wenzelm@65447
   889
wenzelm@65447
   890
lemma modC0: "b:N \<Longrightarrow> 0 mod b = 0 : N"
wenzelm@65447
   891
  unfolding mod_def by (rew absdiff_typing)
wenzelm@65447
   892
wenzelm@65447
   893
lemma modC_succ: "\<lbrakk>a:N; b:N\<rbrakk> \<Longrightarrow>
wenzelm@65447
   894
  succ(a) mod b = rec(succ(a mod b) |-| b, 0, \<lambda>x y. succ(a mod b)) : N"
wenzelm@65447
   895
  unfolding mod_def by (rew absdiff_typing)
wenzelm@65447
   896
wenzelm@65447
   897
wenzelm@65447
   898
text \<open>Typing of quotient: short and long versions.\<close>
wenzelm@65447
   899
wenzelm@65447
   900
lemma div_typing: "\<lbrakk>a:N; b:N\<rbrakk> \<Longrightarrow> a div b : N"
wenzelm@65447
   901
  unfolding div_def by (typechk absdiff_typing mod_typing)
wenzelm@65447
   902
wenzelm@65447
   903
lemma div_typingL: "\<lbrakk>a = c:N; b = d:N\<rbrakk> \<Longrightarrow> a div b = c div d : N"
wenzelm@65447
   904
  unfolding div_def by (equal absdiff_typingL mod_typingL)
wenzelm@65447
   905
wenzelm@65447
   906
lemmas div_typing_rls = mod_typing div_typing absdiff_typing
wenzelm@65447
   907
wenzelm@65447
   908
wenzelm@65447
   909
text \<open>Computation for quotient: 0 and successor cases.\<close>
wenzelm@65447
   910
wenzelm@65447
   911
lemma divC0: "b:N \<Longrightarrow> 0 div b = 0 : N"
wenzelm@65447
   912
  unfolding div_def by (rew mod_typing absdiff_typing)
wenzelm@65447
   913
wenzelm@65447
   914
lemma divC_succ: "\<lbrakk>a:N; b:N\<rbrakk> \<Longrightarrow>
wenzelm@65447
   915
  succ(a) div b = rec(succ(a) mod b, succ(a div b), \<lambda>x y. a div b) : N"
wenzelm@65447
   916
  unfolding div_def by (rew mod_typing)
wenzelm@65447
   917
wenzelm@65447
   918
wenzelm@65447
   919
text \<open>Version of above with same condition as the \<open>mod\<close> one.\<close>
wenzelm@65447
   920
lemma divC_succ2: "\<lbrakk>a:N; b:N\<rbrakk> \<Longrightarrow>
wenzelm@65447
   921
  succ(a) div b =rec(succ(a mod b) |-| b, succ(a div b), \<lambda>x y. a div b) : N"
wenzelm@65447
   922
  apply (rule divC_succ [THEN trans_elem])
wenzelm@65447
   923
    apply (rew div_typing_rls modC_succ)
wenzelm@65447
   924
  apply (NE "succ (a mod b) |-|b")
wenzelm@65447
   925
    apply (rew mod_typing div_typing absdiff_typing)
wenzelm@65447
   926
  done
wenzelm@65447
   927
wenzelm@65447
   928
text \<open>For case analysis on whether a number is 0 or a successor.\<close>
wenzelm@65447
   929
lemma iszero_decidable: "a:N \<Longrightarrow> rec(a, inl(eq), \<lambda>ka kb. inr(<ka, eq>)) :
wenzelm@65447
   930
  Eq(N,a,0) + (\<Sum>x:N. Eq(N,a, succ(x)))"
wenzelm@65447
   931
  apply (NE a)
wenzelm@65447
   932
    apply (rule_tac [3] PlusI_inr)
wenzelm@65447
   933
     apply (rule_tac [2] PlusI_inl)
wenzelm@65447
   934
      apply eqintr
wenzelm@65447
   935
     apply equal
wenzelm@65447
   936
  done
wenzelm@65447
   937
wenzelm@65447
   938
text \<open>Main Result. Holds when \<open>b\<close> is 0 since \<open>a mod 0 = a\<close> and \<open>a div 0 = 0\<close>.\<close>
wenzelm@65447
   939
lemma mod_div_equality: "\<lbrakk>a:N; b:N\<rbrakk> \<Longrightarrow> a mod b #+ (a div b) #* b = a : N"
wenzelm@65447
   940
  apply (NE a)
wenzelm@65447
   941
    apply (arith_rew div_typing_rls modC0 modC_succ divC0 divC_succ2)
wenzelm@65447
   942
  apply (rule EqE)
wenzelm@65447
   943
    \<comment> \<open>case analysis on \<open>succ(u mod b) |-| b\<close>\<close>
wenzelm@65447
   944
  apply (rule_tac a1 = "succ (u mod b) |-| b" in iszero_decidable [THEN PlusE])
wenzelm@65447
   945
    apply (erule_tac [3] SumE)
wenzelm@65447
   946
    apply (hyp_arith_rew div_typing_rls modC0 modC_succ divC0 divC_succ2)
wenzelm@65447
   947
    \<comment> \<open>Replace one occurrence of \<open>b\<close> by \<open>succ(u mod b)\<close>. Clumsy!\<close>
wenzelm@65447
   948
  apply (rule add_typingL [THEN trans_elem])
wenzelm@65447
   949
    apply (erule EqE [THEN absdiff_eq0, THEN sym_elem])
wenzelm@65447
   950
     apply (rule_tac [3] refl_elem)
wenzelm@65447
   951
     apply (hyp_arith_rew div_typing_rls)
wenzelm@65447
   952
  done
wenzelm@65447
   953
wenzelm@19761
   954
end