src/CTT/CTT.thy
author wenzelm
Mon Mar 25 17:21:26 2019 +0100 (3 months ago)
changeset 69981 3dced198b9ec
parent 69605 a96320074298
permissions -rw-r--r--
more strict AFP properties;
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@69605
    12
ML_file \<open>~~/src/Provers/typedsimp.ML\<close>
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@67443
   242
  \<comment> \<open>Martin-Löf's book (page 68) discusses elimination and computation.
wenzelm@63505
   243
    Elimination can be derived by computation and equality of types,
wenzelm@63505
   244
    but with an extra premise \<open>C(x)\<close> type \<open>x:T\<close>.
wenzelm@67443
   245
    Also computation can be derived from elimination.\<close>
clasohm@0
   246
wenzelm@51308
   247
  TF: "T type" and
wenzelm@51308
   248
  TI: "tt : T" and
wenzelm@58977
   249
  TE: "\<And>p c C. \<lbrakk>p : T; c : C(tt)\<rbrakk> \<Longrightarrow> c : C(p)" and
wenzelm@58977
   250
  TEL: "\<And>p q c d C. \<lbrakk>p = q : T; c = d : C(tt)\<rbrakk> \<Longrightarrow> c = d : C(p)" and
wenzelm@58977
   251
  TC: "\<And>p. p : T \<Longrightarrow> p = tt : T"
clasohm@0
   252
wenzelm@19761
   253
wenzelm@19761
   254
subsection "Tactics and derived rules for Constructive Type Theory"
wenzelm@19761
   255
wenzelm@63505
   256
text \<open>Formation rules.\<close>
wenzelm@19761
   257
lemmas form_rls = NF ProdF SumF PlusF EqF FF TF
wenzelm@19761
   258
  and formL_rls = ProdFL SumFL PlusFL EqFL
wenzelm@19761
   259
wenzelm@63505
   260
text \<open>
wenzelm@63505
   261
  Introduction rules. OMITTED:
wenzelm@63505
   262
  \<^item> \<open>EqI\<close>, because its premise is an \<open>eqelem\<close>, not an \<open>elem\<close>.
wenzelm@63505
   263
\<close>
wenzelm@19761
   264
lemmas intr_rls = NI0 NI_succ ProdI SumI PlusI_inl PlusI_inr TI
wenzelm@19761
   265
  and intrL_rls = NI_succL ProdIL SumIL PlusI_inlL PlusI_inrL
wenzelm@19761
   266
wenzelm@63505
   267
text \<open>
wenzelm@63505
   268
  Elimination rules. OMITTED:
wenzelm@63505
   269
  \<^item> \<open>EqE\<close>, because its conclusion is an \<open>eqelem\<close>, not an \<open>elem\<close>
wenzelm@63505
   270
  \<^item> \<open>TE\<close>, because it does not involve a constructor.
wenzelm@63505
   271
\<close>
wenzelm@19761
   272
lemmas elim_rls = NE ProdE SumE PlusE FE
wenzelm@19761
   273
  and elimL_rls = NEL ProdEL SumEL PlusEL FEL
wenzelm@19761
   274
wenzelm@63505
   275
text \<open>OMITTED: \<open>eqC\<close> are \<open>TC\<close> because they make rewriting loop: \<open>p = un = un = \<dots>\<close>\<close>
wenzelm@19761
   276
lemmas comp_rls = NC0 NC_succ ProdC SumC PlusC_inl PlusC_inr
wenzelm@19761
   277
wenzelm@63505
   278
text \<open>Rules with conclusion \<open>a:A\<close>, an elem judgement.\<close>
wenzelm@19761
   279
lemmas element_rls = intr_rls elim_rls
wenzelm@19761
   280
wenzelm@63505
   281
text \<open>Definitions are (meta)equality axioms.\<close>
wenzelm@19761
   282
lemmas basic_defs = fst_def snd_def
wenzelm@19761
   283
wenzelm@63505
   284
text \<open>Compare with standard version: \<open>B\<close> is applied to UNSIMPLIFIED expression!\<close>
wenzelm@58977
   285
lemma SumIL2: "\<lbrakk>c = a : A; d = b : B(a)\<rbrakk> \<Longrightarrow> <c,d> = <a,b> : Sum(A,B)"
wenzelm@65338
   286
  by (rule sym_elem) (rule SumIL; rule sym_elem)
wenzelm@19761
   287
wenzelm@19761
   288
lemmas intrL2_rls = NI_succL ProdIL SumIL2 PlusI_inlL PlusI_inrL
wenzelm@19761
   289
wenzelm@63505
   290
text \<open>
wenzelm@63505
   291
  Exploit \<open>p:Prod(A,B)\<close> to create the assumption \<open>z:B(a)\<close>.
wenzelm@63505
   292
  A more natural form of product elimination.
wenzelm@63505
   293
\<close>
wenzelm@19761
   294
lemma subst_prodE:
wenzelm@19761
   295
  assumes "p: Prod(A,B)"
wenzelm@19761
   296
    and "a: A"
wenzelm@58977
   297
    and "\<And>z. z: B(a) \<Longrightarrow> c(z): C(z)"
wenzelm@19761
   298
  shows "c(p`a): C(p`a)"
wenzelm@63505
   299
  by (rule assms ProdE)+
wenzelm@19761
   300
wenzelm@19761
   301
wenzelm@60770
   302
subsection \<open>Tactics for type checking\<close>
wenzelm@19761
   303
wenzelm@60770
   304
ML \<open>
wenzelm@19761
   305
local
wenzelm@19761
   306
wenzelm@69593
   307
fun is_rigid_elem (Const(\<^const_name>\<open>Elem\<close>,_) $ a $ _) = not(is_Var (head_of a))
wenzelm@69593
   308
  | is_rigid_elem (Const(\<^const_name>\<open>Eqelem\<close>,_) $ a $ _ $ _) = not(is_Var (head_of a))
wenzelm@69593
   309
  | is_rigid_elem (Const(\<^const_name>\<open>Type\<close>,_) $ a) = not(is_Var (head_of a))
wenzelm@19761
   310
  | is_rigid_elem _ = false
wenzelm@19761
   311
wenzelm@19761
   312
in
wenzelm@19761
   313
wenzelm@19761
   314
(*Try solving a:A or a=b:A by assumption provided a is rigid!*)
wenzelm@63505
   315
fun test_assume_tac ctxt = SUBGOAL (fn (prem, i) =>
wenzelm@63505
   316
  if is_rigid_elem (Logic.strip_assums_concl prem)
wenzelm@63505
   317
  then assume_tac ctxt i else no_tac)
wenzelm@19761
   318
wenzelm@58963
   319
fun ASSUME ctxt tf i = test_assume_tac ctxt i  ORELSE  tf i
wenzelm@19761
   320
wenzelm@63505
   321
end
wenzelm@60770
   322
\<close>
wenzelm@19761
   323
wenzelm@63505
   324
text \<open>
wenzelm@63505
   325
  For simplification: type formation and checking,
wenzelm@63505
   326
  but no equalities between terms.
wenzelm@63505
   327
\<close>
wenzelm@19761
   328
lemmas routine_rls = form_rls formL_rls refl_type element_rls
wenzelm@19761
   329
wenzelm@60770
   330
ML \<open>
wenzelm@59164
   331
fun routine_tac rls ctxt prems =
wenzelm@59164
   332
  ASSUME ctxt (filt_resolve_from_net_tac ctxt 4 (Tactic.build_net (prems @ rls)));
wenzelm@19761
   333
wenzelm@19761
   334
(*Solve all subgoals "A type" using formation rules. *)
wenzelm@59164
   335
val form_net = Tactic.build_net @{thms form_rls};
wenzelm@59164
   336
fun form_tac ctxt =
wenzelm@59164
   337
  REPEAT_FIRST (ASSUME ctxt (filt_resolve_from_net_tac ctxt 1 form_net));
wenzelm@19761
   338
wenzelm@19761
   339
(*Type checking: solve a:A (a rigid, A flexible) by intro and elim rules. *)
wenzelm@58963
   340
fun typechk_tac ctxt thms =
wenzelm@59164
   341
  let val tac =
wenzelm@59164
   342
    filt_resolve_from_net_tac ctxt 3
wenzelm@59164
   343
      (Tactic.build_net (thms @ @{thms form_rls} @ @{thms element_rls}))
wenzelm@58963
   344
  in  REPEAT_FIRST (ASSUME ctxt tac)  end
wenzelm@19761
   345
wenzelm@19761
   346
(*Solve a:A (a flexible, A rigid) by introduction rules.
wenzelm@19761
   347
  Cannot use stringtrees (filt_resolve_tac) since
wenzelm@19761
   348
  goals like ?a:SUM(A,B) have a trivial head-string *)
wenzelm@58963
   349
fun intr_tac ctxt thms =
wenzelm@59164
   350
  let val tac =
wenzelm@59164
   351
    filt_resolve_from_net_tac ctxt 1
wenzelm@59164
   352
      (Tactic.build_net (thms @ @{thms form_rls} @ @{thms intr_rls}))
wenzelm@58963
   353
  in  REPEAT_FIRST (ASSUME ctxt tac)  end
wenzelm@19761
   354
wenzelm@19761
   355
(*Equality proving: solve a=b:A (where a is rigid) by long rules. *)
wenzelm@58963
   356
fun equal_tac ctxt thms =
wenzelm@59164
   357
  REPEAT_FIRST
wenzelm@63505
   358
    (ASSUME ctxt
wenzelm@63505
   359
      (filt_resolve_from_net_tac ctxt 3
wenzelm@63505
   360
        (Tactic.build_net (thms @ @{thms form_rls element_rls intrL_rls elimL_rls refl_elem}))))
wenzelm@60770
   361
\<close>
wenzelm@19761
   362
wenzelm@60770
   363
method_setup form = \<open>Scan.succeed (fn ctxt => SIMPLE_METHOD (form_tac ctxt))\<close>
wenzelm@60770
   364
method_setup typechk = \<open>Attrib.thms >> (fn ths => fn ctxt => SIMPLE_METHOD (typechk_tac ctxt ths))\<close>
wenzelm@60770
   365
method_setup intr = \<open>Attrib.thms >> (fn ths => fn ctxt => SIMPLE_METHOD (intr_tac ctxt ths))\<close>
wenzelm@60770
   366
method_setup equal = \<open>Attrib.thms >> (fn ths => fn ctxt => SIMPLE_METHOD (equal_tac ctxt ths))\<close>
wenzelm@19761
   367
wenzelm@19761
   368
wenzelm@60770
   369
subsection \<open>Simplification\<close>
wenzelm@19761
   370
wenzelm@63505
   371
text \<open>To simplify the type in a goal.\<close>
wenzelm@58977
   372
lemma replace_type: "\<lbrakk>B = A; a : A\<rbrakk> \<Longrightarrow> a : B"
wenzelm@63505
   373
  apply (rule equal_types)
wenzelm@63505
   374
   apply (rule_tac [2] sym_type)
wenzelm@63505
   375
   apply assumption+
wenzelm@63505
   376
  done
wenzelm@19761
   377
wenzelm@63505
   378
text \<open>Simplify the parameter of a unary type operator.\<close>
wenzelm@19761
   379
lemma subst_eqtyparg:
wenzelm@23467
   380
  assumes 1: "a=c : A"
wenzelm@58977
   381
    and 2: "\<And>z. z:A \<Longrightarrow> B(z) type"
wenzelm@63505
   382
  shows "B(a) = B(c)"
wenzelm@63505
   383
  apply (rule subst_typeL)
wenzelm@63505
   384
   apply (rule_tac [2] refl_type)
wenzelm@63505
   385
   apply (rule 1)
wenzelm@63505
   386
  apply (erule 2)
wenzelm@63505
   387
  done
wenzelm@19761
   388
wenzelm@63505
   389
text \<open>Simplification rules for Constructive Type Theory.\<close>
wenzelm@19761
   390
lemmas reduction_rls = comp_rls [THEN trans_elem]
wenzelm@19761
   391
wenzelm@60770
   392
ML \<open>
wenzelm@19761
   393
(*Converts each goal "e : Eq(A,a,b)" into "a=b:A" for simplification.
wenzelm@19761
   394
  Uses other intro rules to avoid changing flexible goals.*)
wenzelm@59164
   395
val eqintr_net = Tactic.build_net @{thms EqI intr_rls}
wenzelm@58963
   396
fun eqintr_tac ctxt =
wenzelm@59164
   397
  REPEAT_FIRST (ASSUME ctxt (filt_resolve_from_net_tac ctxt 1 eqintr_net))
wenzelm@19761
   398
wenzelm@19761
   399
(** Tactics that instantiate CTT-rules.
wenzelm@19761
   400
    Vars in the given terms will be incremented!
wenzelm@19761
   401
    The (rtac EqE i) lets them apply to equality judgements. **)
wenzelm@19761
   402
wenzelm@27208
   403
fun NE_tac ctxt sp i =
wenzelm@60754
   404
  TRY (resolve_tac ctxt @{thms EqE} i) THEN
wenzelm@59780
   405
  Rule_Insts.res_inst_tac ctxt [((("p", 0), Position.none), sp)] [] @{thm NE} i
wenzelm@19761
   406
wenzelm@27208
   407
fun SumE_tac ctxt sp i =
wenzelm@60754
   408
  TRY (resolve_tac ctxt @{thms EqE} i) THEN
wenzelm@59780
   409
  Rule_Insts.res_inst_tac ctxt [((("p", 0), Position.none), sp)] [] @{thm SumE} i
wenzelm@19761
   410
wenzelm@27208
   411
fun PlusE_tac ctxt sp i =
wenzelm@60754
   412
  TRY (resolve_tac ctxt @{thms EqE} i) THEN
wenzelm@59780
   413
  Rule_Insts.res_inst_tac ctxt [((("p", 0), Position.none), sp)] [] @{thm PlusE} i
wenzelm@19761
   414
wenzelm@19761
   415
(** Predicate logic reasoning, WITH THINNING!!  Procedures adapted from NJ. **)
wenzelm@19761
   416
wenzelm@19761
   417
(*Finds f:Prod(A,B) and a:A in the assumptions, concludes there is z:B(a) *)
wenzelm@58963
   418
fun add_mp_tac ctxt i =
wenzelm@60754
   419
  resolve_tac ctxt @{thms subst_prodE} i  THEN  assume_tac ctxt i  THEN  assume_tac ctxt i
wenzelm@19761
   420
wenzelm@61391
   421
(*Finds P\<longrightarrow>Q and P in the assumptions, replaces implication by Q *)
wenzelm@60754
   422
fun mp_tac ctxt i = eresolve_tac ctxt @{thms subst_prodE} i  THEN  assume_tac ctxt i
wenzelm@19761
   423
wenzelm@19761
   424
(*"safe" when regarded as predicate calculus rules*)
wenzelm@19761
   425
val safe_brls = sort (make_ord lessb)
wenzelm@27208
   426
    [ (true, @{thm FE}), (true,asm_rl),
wenzelm@27208
   427
      (false, @{thm ProdI}), (true, @{thm SumE}), (true, @{thm PlusE}) ]
wenzelm@19761
   428
wenzelm@19761
   429
val unsafe_brls =
wenzelm@27208
   430
    [ (false, @{thm PlusI_inl}), (false, @{thm PlusI_inr}), (false, @{thm SumI}),
wenzelm@27208
   431
      (true, @{thm subst_prodE}) ]
wenzelm@19761
   432
wenzelm@19761
   433
(*0 subgoals vs 1 or more*)
wenzelm@19761
   434
val (safe0_brls, safep_brls) =
wenzelm@67405
   435
    List.partition (curry (op =) 0 o subgoals_of_brl) safe_brls
wenzelm@19761
   436
wenzelm@58963
   437
fun safestep_tac ctxt thms i =
wenzelm@58963
   438
    form_tac ctxt ORELSE
wenzelm@59498
   439
    resolve_tac ctxt thms i  ORELSE
wenzelm@59498
   440
    biresolve_tac ctxt safe0_brls i  ORELSE  mp_tac ctxt i  ORELSE
wenzelm@59498
   441
    DETERM (biresolve_tac ctxt safep_brls i)
wenzelm@19761
   442
wenzelm@58963
   443
fun safe_tac ctxt thms i = DEPTH_SOLVE_1 (safestep_tac ctxt thms i)
wenzelm@19761
   444
wenzelm@59498
   445
fun step_tac ctxt thms = safestep_tac ctxt thms  ORELSE'  biresolve_tac ctxt unsafe_brls
wenzelm@19761
   446
wenzelm@19761
   447
(*Fails unless it solves the goal!*)
wenzelm@58963
   448
fun pc_tac ctxt thms = DEPTH_SOLVE_1 o (step_tac ctxt thms)
wenzelm@60770
   449
\<close>
wenzelm@19761
   450
wenzelm@60770
   451
method_setup eqintr = \<open>Scan.succeed (SIMPLE_METHOD o eqintr_tac)\<close>
wenzelm@60770
   452
method_setup NE = \<open>
wenzelm@63120
   453
  Scan.lift Args.embedded_inner_syntax >> (fn s => fn ctxt => SIMPLE_METHOD' (NE_tac ctxt s))
wenzelm@60770
   454
\<close>
wenzelm@60770
   455
method_setup pc = \<open>Attrib.thms >> (fn ths => fn ctxt => SIMPLE_METHOD' (pc_tac ctxt ths))\<close>
wenzelm@60770
   456
method_setup add_mp = \<open>Scan.succeed (SIMPLE_METHOD' o add_mp_tac)\<close>
wenzelm@58972
   457
wenzelm@69605
   458
ML_file \<open>rew.ML\<close>
wenzelm@60770
   459
method_setup rew = \<open>Attrib.thms >> (fn ths => fn ctxt => SIMPLE_METHOD (rew_tac ctxt ths))\<close>
wenzelm@60770
   460
method_setup hyp_rew = \<open>Attrib.thms >> (fn ths => fn ctxt => SIMPLE_METHOD (hyp_rew_tac ctxt ths))\<close>
wenzelm@58972
   461
wenzelm@19761
   462
wenzelm@60770
   463
subsection \<open>The elimination rules for fst/snd\<close>
wenzelm@19761
   464
wenzelm@58977
   465
lemma SumE_fst: "p : Sum(A,B) \<Longrightarrow> fst(p) : A"
wenzelm@63505
   466
  apply (unfold basic_defs)
wenzelm@63505
   467
  apply (erule SumE)
wenzelm@63505
   468
  apply assumption
wenzelm@63505
   469
  done
wenzelm@19761
   470
wenzelm@63505
   471
text \<open>The first premise must be \<open>p:Sum(A,B)\<close>!!.\<close>
wenzelm@19761
   472
lemma SumE_snd:
wenzelm@19761
   473
  assumes major: "p: Sum(A,B)"
wenzelm@19761
   474
    and "A type"
wenzelm@58977
   475
    and "\<And>x. x:A \<Longrightarrow> B(x) type"
wenzelm@19761
   476
  shows "snd(p) : B(fst(p))"
wenzelm@19761
   477
  apply (unfold basic_defs)
wenzelm@19761
   478
  apply (rule major [THEN SumE])
wenzelm@19761
   479
  apply (rule SumC [THEN subst_eqtyparg, THEN replace_type])
wenzelm@63505
   480
      apply (typechk assms)
wenzelm@19761
   481
  done
wenzelm@19761
   482
wenzelm@65447
   483
wenzelm@65447
   484
section \<open>The two-element type (booleans and conditionals)\<close>
wenzelm@65447
   485
wenzelm@65447
   486
definition Bool :: "t"
wenzelm@65447
   487
  where "Bool \<equiv> T+T"
wenzelm@65447
   488
wenzelm@65447
   489
definition true :: "i"
wenzelm@65447
   490
  where "true \<equiv> inl(tt)"
wenzelm@65447
   491
wenzelm@65447
   492
definition false :: "i"
wenzelm@65447
   493
  where "false \<equiv> inr(tt)"
wenzelm@65447
   494
wenzelm@65447
   495
definition cond :: "[i,i,i]\<Rightarrow>i"
wenzelm@65447
   496
  where "cond(a,b,c) \<equiv> when(a, \<lambda>_. b, \<lambda>_. c)"
wenzelm@65447
   497
wenzelm@65447
   498
lemmas bool_defs = Bool_def true_def false_def cond_def
wenzelm@65447
   499
wenzelm@65447
   500
wenzelm@65447
   501
subsection \<open>Derivation of rules for the type \<open>Bool\<close>\<close>
wenzelm@65447
   502
wenzelm@65447
   503
text \<open>Formation rule.\<close>
wenzelm@65447
   504
lemma boolF: "Bool type"
wenzelm@65447
   505
  unfolding bool_defs by typechk
wenzelm@65447
   506
wenzelm@65447
   507
text \<open>Introduction rules for \<open>true\<close>, \<open>false\<close>.\<close>
wenzelm@65447
   508
wenzelm@65447
   509
lemma boolI_true: "true : Bool"
wenzelm@65447
   510
  unfolding bool_defs by typechk
wenzelm@65447
   511
wenzelm@65447
   512
lemma boolI_false: "false : Bool"
wenzelm@65447
   513
  unfolding bool_defs by typechk
wenzelm@65447
   514
wenzelm@65447
   515
text \<open>Elimination rule: typing of \<open>cond\<close>.\<close>
wenzelm@65447
   516
lemma boolE: "\<lbrakk>p:Bool; a : C(true); b : C(false)\<rbrakk> \<Longrightarrow> cond(p,a,b) : C(p)"
wenzelm@65447
   517
  unfolding bool_defs
wenzelm@65447
   518
  apply (typechk; erule TE)
wenzelm@65447
   519
   apply typechk
wenzelm@65447
   520
  done
wenzelm@65447
   521
wenzelm@65447
   522
lemma boolEL: "\<lbrakk>p = q : Bool; a = c : C(true); b = d : C(false)\<rbrakk>
wenzelm@65447
   523
  \<Longrightarrow> cond(p,a,b) = cond(q,c,d) : C(p)"
wenzelm@65447
   524
  unfolding bool_defs
wenzelm@65447
   525
  apply (rule PlusEL)
wenzelm@65447
   526
    apply (erule asm_rl refl_elem [THEN TEL])+
wenzelm@65447
   527
  done
wenzelm@65447
   528
wenzelm@65447
   529
text \<open>Computation rules for \<open>true\<close>, \<open>false\<close>.\<close>
wenzelm@65447
   530
wenzelm@65447
   531
lemma boolC_true: "\<lbrakk>a : C(true); b : C(false)\<rbrakk> \<Longrightarrow> cond(true,a,b) = a : C(true)"
wenzelm@65447
   532
  unfolding bool_defs
wenzelm@65447
   533
  apply (rule comp_rls)
wenzelm@65447
   534
    apply typechk
wenzelm@65447
   535
   apply (erule_tac [!] TE)
wenzelm@65447
   536
   apply typechk
wenzelm@65447
   537
  done
wenzelm@65447
   538
wenzelm@65447
   539
lemma boolC_false: "\<lbrakk>a : C(true); b : C(false)\<rbrakk> \<Longrightarrow> cond(false,a,b) = b : C(false)"
wenzelm@65447
   540
  unfolding bool_defs
wenzelm@65447
   541
  apply (rule comp_rls)
wenzelm@65447
   542
    apply typechk
wenzelm@65447
   543
   apply (erule_tac [!] TE)
wenzelm@65447
   544
   apply typechk
wenzelm@65447
   545
  done
wenzelm@65447
   546
wenzelm@65447
   547
section \<open>Elementary arithmetic\<close>
wenzelm@65447
   548
wenzelm@65447
   549
subsection \<open>Arithmetic operators and their definitions\<close>
wenzelm@65447
   550
wenzelm@65447
   551
definition add :: "[i,i]\<Rightarrow>i"   (infixr "#+" 65)
wenzelm@65447
   552
  where "a#+b \<equiv> rec(a, b, \<lambda>u v. succ(v))"
wenzelm@65447
   553
wenzelm@65447
   554
definition diff :: "[i,i]\<Rightarrow>i"   (infixr "-" 65)
wenzelm@65447
   555
  where "a-b \<equiv> rec(b, a, \<lambda>u v. rec(v, 0, \<lambda>x y. x))"
wenzelm@65447
   556
wenzelm@65447
   557
definition absdiff :: "[i,i]\<Rightarrow>i"   (infixr "|-|" 65)
wenzelm@65447
   558
  where "a|-|b \<equiv> (a-b) #+ (b-a)"
wenzelm@65447
   559
wenzelm@65447
   560
definition mult :: "[i,i]\<Rightarrow>i"   (infixr "#*" 70)
wenzelm@65447
   561
  where "a#*b \<equiv> rec(a, 0, \<lambda>u v. b #+ v)"
wenzelm@65447
   562
wenzelm@65447
   563
definition mod :: "[i,i]\<Rightarrow>i"   (infixr "mod" 70)
wenzelm@65447
   564
  where "a mod b \<equiv> rec(a, 0, \<lambda>u v. rec(succ(v) |-| b, 0, \<lambda>x y. succ(v)))"
wenzelm@65447
   565
wenzelm@65447
   566
definition div :: "[i,i]\<Rightarrow>i"   (infixr "div" 70)
wenzelm@65447
   567
  where "a div b \<equiv> rec(a, 0, \<lambda>u v. rec(succ(u) mod b, succ(v), \<lambda>x y. v))"
wenzelm@65447
   568
wenzelm@65447
   569
lemmas arith_defs = add_def diff_def absdiff_def mult_def mod_def div_def
wenzelm@65447
   570
wenzelm@65447
   571
wenzelm@65447
   572
subsection \<open>Proofs about elementary arithmetic: addition, multiplication, etc.\<close>
wenzelm@65447
   573
wenzelm@65447
   574
subsubsection \<open>Addition\<close>
wenzelm@65447
   575
wenzelm@65447
   576
text \<open>Typing of \<open>add\<close>: short and long versions.\<close>
wenzelm@65447
   577
wenzelm@65447
   578
lemma add_typing: "\<lbrakk>a:N; b:N\<rbrakk> \<Longrightarrow> a #+ b : N"
wenzelm@65447
   579
  unfolding arith_defs by typechk
wenzelm@65447
   580
wenzelm@65447
   581
lemma add_typingL: "\<lbrakk>a = c:N; b = d:N\<rbrakk> \<Longrightarrow> a #+ b = c #+ d : N"
wenzelm@65447
   582
  unfolding arith_defs by equal
wenzelm@65447
   583
wenzelm@65447
   584
wenzelm@65447
   585
text \<open>Computation for \<open>add\<close>: 0 and successor cases.\<close>
wenzelm@65447
   586
wenzelm@65447
   587
lemma addC0: "b:N \<Longrightarrow> 0 #+ b = b : N"
wenzelm@65447
   588
  unfolding arith_defs by rew
wenzelm@65447
   589
wenzelm@65447
   590
lemma addC_succ: "\<lbrakk>a:N; b:N\<rbrakk> \<Longrightarrow> succ(a) #+ b = succ(a #+ b) : N"
wenzelm@65447
   591
  unfolding arith_defs by rew
wenzelm@65447
   592
wenzelm@65447
   593
wenzelm@65447
   594
subsubsection \<open>Multiplication\<close>
wenzelm@65447
   595
wenzelm@65447
   596
text \<open>Typing of \<open>mult\<close>: short and long versions.\<close>
wenzelm@65447
   597
wenzelm@65447
   598
lemma mult_typing: "\<lbrakk>a:N; b:N\<rbrakk> \<Longrightarrow> a #* b : N"
wenzelm@65447
   599
  unfolding arith_defs by (typechk add_typing)
wenzelm@65447
   600
wenzelm@65447
   601
lemma mult_typingL: "\<lbrakk>a = c:N; b = d:N\<rbrakk> \<Longrightarrow> a #* b = c #* d : N"
wenzelm@65447
   602
  unfolding arith_defs by (equal add_typingL)
wenzelm@65447
   603
wenzelm@65447
   604
wenzelm@65447
   605
text \<open>Computation for \<open>mult\<close>: 0 and successor cases.\<close>
wenzelm@65447
   606
wenzelm@65447
   607
lemma multC0: "b:N \<Longrightarrow> 0 #* b = 0 : N"
wenzelm@65447
   608
  unfolding arith_defs by rew
wenzelm@65447
   609
wenzelm@65447
   610
lemma multC_succ: "\<lbrakk>a:N; b:N\<rbrakk> \<Longrightarrow> succ(a) #* b = b #+ (a #* b) : N"
wenzelm@65447
   611
  unfolding arith_defs by rew
wenzelm@65447
   612
wenzelm@65447
   613
wenzelm@65447
   614
subsubsection \<open>Difference\<close>
wenzelm@65447
   615
wenzelm@65447
   616
text \<open>Typing of difference.\<close>
wenzelm@65447
   617
wenzelm@65447
   618
lemma diff_typing: "\<lbrakk>a:N; b:N\<rbrakk> \<Longrightarrow> a - b : N"
wenzelm@65447
   619
  unfolding arith_defs by typechk
wenzelm@65447
   620
wenzelm@65447
   621
lemma diff_typingL: "\<lbrakk>a = c:N; b = d:N\<rbrakk> \<Longrightarrow> a - b = c - d : N"
wenzelm@65447
   622
  unfolding arith_defs by equal
wenzelm@65447
   623
wenzelm@65447
   624
wenzelm@65447
   625
text \<open>Computation for difference: 0 and successor cases.\<close>
wenzelm@65447
   626
wenzelm@65447
   627
lemma diffC0: "a:N \<Longrightarrow> a - 0 = a : N"
wenzelm@65447
   628
  unfolding arith_defs by rew
wenzelm@65447
   629
wenzelm@65447
   630
text \<open>Note: \<open>rec(a, 0, \<lambda>z w.z)\<close> is \<open>pred(a).\<close>\<close>
wenzelm@65447
   631
wenzelm@65447
   632
lemma diff_0_eq_0: "b:N \<Longrightarrow> 0 - b = 0 : N"
wenzelm@65447
   633
  unfolding arith_defs
wenzelm@65447
   634
  apply (NE b)
wenzelm@65447
   635
    apply hyp_rew
wenzelm@65447
   636
  done
wenzelm@65447
   637
wenzelm@65447
   638
text \<open>
wenzelm@65447
   639
  Essential to simplify FIRST!!  (Else we get a critical pair)
wenzelm@65447
   640
  \<open>succ(a) - succ(b)\<close> rewrites to \<open>pred(succ(a) - b)\<close>.
wenzelm@65447
   641
\<close>
wenzelm@65447
   642
lemma diff_succ_succ: "\<lbrakk>a:N; b:N\<rbrakk> \<Longrightarrow> succ(a) - succ(b) = a - b : N"
wenzelm@65447
   643
  unfolding arith_defs
wenzelm@65447
   644
  apply hyp_rew
wenzelm@65447
   645
  apply (NE b)
wenzelm@65447
   646
    apply hyp_rew
wenzelm@65447
   647
  done
wenzelm@65447
   648
wenzelm@65447
   649
wenzelm@65447
   650
subsection \<open>Simplification\<close>
wenzelm@65447
   651
wenzelm@65447
   652
lemmas arith_typing_rls = add_typing mult_typing diff_typing
wenzelm@65447
   653
  and arith_congr_rls = add_typingL mult_typingL diff_typingL
wenzelm@65447
   654
wenzelm@65447
   655
lemmas congr_rls = arith_congr_rls intrL2_rls elimL_rls
wenzelm@65447
   656
wenzelm@65447
   657
lemmas arithC_rls =
wenzelm@65447
   658
  addC0 addC_succ
wenzelm@65447
   659
  multC0 multC_succ
wenzelm@65447
   660
  diffC0 diff_0_eq_0 diff_succ_succ
wenzelm@65447
   661
wenzelm@65447
   662
ML \<open>
wenzelm@65447
   663
  structure Arith_simp = TSimpFun(
wenzelm@65447
   664
    val refl = @{thm refl_elem}
wenzelm@65447
   665
    val sym = @{thm sym_elem}
wenzelm@65447
   666
    val trans = @{thm trans_elem}
wenzelm@65447
   667
    val refl_red = @{thm refl_red}
wenzelm@65447
   668
    val trans_red = @{thm trans_red}
wenzelm@65447
   669
    val red_if_equal = @{thm red_if_equal}
wenzelm@65447
   670
    val default_rls = @{thms arithC_rls comp_rls}
wenzelm@65447
   671
    val routine_tac = routine_tac @{thms arith_typing_rls routine_rls}
wenzelm@65447
   672
  )
wenzelm@65447
   673
wenzelm@65447
   674
  fun arith_rew_tac ctxt prems =
wenzelm@65447
   675
    make_rew_tac ctxt (Arith_simp.norm_tac ctxt (@{thms congr_rls}, prems))
wenzelm@65447
   676
wenzelm@65447
   677
  fun hyp_arith_rew_tac ctxt prems =
wenzelm@65447
   678
    make_rew_tac ctxt
wenzelm@65447
   679
      (Arith_simp.cond_norm_tac ctxt (prove_cond_tac ctxt, @{thms congr_rls}, prems))
wenzelm@65447
   680
\<close>
wenzelm@65447
   681
wenzelm@65447
   682
method_setup arith_rew = \<open>
wenzelm@65447
   683
  Attrib.thms >> (fn ths => fn ctxt => SIMPLE_METHOD (arith_rew_tac ctxt ths))
wenzelm@65447
   684
\<close>
wenzelm@65447
   685
wenzelm@65447
   686
method_setup hyp_arith_rew = \<open>
wenzelm@65447
   687
  Attrib.thms >> (fn ths => fn ctxt => SIMPLE_METHOD (hyp_arith_rew_tac ctxt ths))
wenzelm@65447
   688
\<close>
wenzelm@65447
   689
wenzelm@65447
   690
wenzelm@65447
   691
subsection \<open>Addition\<close>
wenzelm@65447
   692
wenzelm@65447
   693
text \<open>Associative law for addition.\<close>
wenzelm@65447
   694
lemma add_assoc: "\<lbrakk>a:N; b:N; c:N\<rbrakk> \<Longrightarrow> (a #+ b) #+ c = a #+ (b #+ c) : N"
wenzelm@65447
   695
  apply (NE a)
wenzelm@65447
   696
    apply hyp_arith_rew
wenzelm@65447
   697
  done
wenzelm@65447
   698
wenzelm@65447
   699
text \<open>Commutative law for addition.  Can be proved using three inductions.
wenzelm@65447
   700
  Must simplify after first induction!  Orientation of rewrites is delicate.\<close>
wenzelm@65447
   701
lemma add_commute: "\<lbrakk>a:N; b:N\<rbrakk> \<Longrightarrow> a #+ b = b #+ a : N"
wenzelm@65447
   702
  apply (NE a)
wenzelm@65447
   703
    apply hyp_arith_rew
wenzelm@65447
   704
   apply (rule sym_elem)
wenzelm@65447
   705
   prefer 2
wenzelm@65447
   706
   apply (NE b)
wenzelm@65447
   707
     prefer 4
wenzelm@65447
   708
     apply (NE b)
wenzelm@65447
   709
       apply hyp_arith_rew
wenzelm@65447
   710
  done
wenzelm@65447
   711
wenzelm@65447
   712
wenzelm@65447
   713
subsection \<open>Multiplication\<close>
wenzelm@65447
   714
wenzelm@65447
   715
text \<open>Right annihilation in product.\<close>
wenzelm@65447
   716
lemma mult_0_right: "a:N \<Longrightarrow> a #* 0 = 0 : N"
wenzelm@65447
   717
  apply (NE a)
wenzelm@65447
   718
    apply hyp_arith_rew
wenzelm@65447
   719
  done
wenzelm@65447
   720
wenzelm@65447
   721
text \<open>Right successor law for multiplication.\<close>
wenzelm@65447
   722
lemma mult_succ_right: "\<lbrakk>a:N; b:N\<rbrakk> \<Longrightarrow> a #* succ(b) = a #+ (a #* b) : N"
wenzelm@65447
   723
  apply (NE a)
wenzelm@65447
   724
    apply (hyp_arith_rew add_assoc [THEN sym_elem])
wenzelm@65447
   725
  apply (assumption | rule add_commute mult_typingL add_typingL intrL_rls refl_elem)+
wenzelm@65447
   726
  done
wenzelm@65447
   727
wenzelm@65447
   728
text \<open>Commutative law for multiplication.\<close>
wenzelm@65447
   729
lemma mult_commute: "\<lbrakk>a:N; b:N\<rbrakk> \<Longrightarrow> a #* b = b #* a : N"
wenzelm@65447
   730
  apply (NE a)
wenzelm@65447
   731
    apply (hyp_arith_rew mult_0_right mult_succ_right)
wenzelm@65447
   732
  done
wenzelm@65447
   733
wenzelm@65447
   734
text \<open>Addition distributes over multiplication.\<close>
wenzelm@65447
   735
lemma add_mult_distrib: "\<lbrakk>a:N; b:N; c:N\<rbrakk> \<Longrightarrow> (a #+ b) #* c = (a #* c) #+ (b #* c) : N"
wenzelm@65447
   736
  apply (NE a)
wenzelm@65447
   737
    apply (hyp_arith_rew add_assoc [THEN sym_elem])
wenzelm@65447
   738
  done
wenzelm@65447
   739
wenzelm@65447
   740
text \<open>Associative law for multiplication.\<close>
wenzelm@65447
   741
lemma mult_assoc: "\<lbrakk>a:N; b:N; c:N\<rbrakk> \<Longrightarrow> (a #* b) #* c = a #* (b #* c) : N"
wenzelm@65447
   742
  apply (NE a)
wenzelm@65447
   743
    apply (hyp_arith_rew add_mult_distrib)
wenzelm@65447
   744
  done
wenzelm@65447
   745
wenzelm@65447
   746
wenzelm@65447
   747
subsection \<open>Difference\<close>
wenzelm@65447
   748
wenzelm@65447
   749
text \<open>
wenzelm@65447
   750
  Difference on natural numbers, without negative numbers
wenzelm@65447
   751
  \<^item> \<open>a - b = 0\<close>  iff  \<open>a \<le> b\<close>
wenzelm@65447
   752
  \<^item> \<open>a - b = succ(c)\<close> iff \<open>a > b\<close>
wenzelm@65447
   753
\<close>
wenzelm@65447
   754
wenzelm@65447
   755
lemma diff_self_eq_0: "a:N \<Longrightarrow> a - a = 0 : N"
wenzelm@65447
   756
  apply (NE a)
wenzelm@65447
   757
    apply hyp_arith_rew
wenzelm@65447
   758
  done
wenzelm@65447
   759
wenzelm@65447
   760
wenzelm@65447
   761
lemma add_0_right: "\<lbrakk>c : N; 0 : N; c : N\<rbrakk> \<Longrightarrow> c #+ 0 = c : N"
wenzelm@65447
   762
  by (rule addC0 [THEN [3] add_commute [THEN trans_elem]])
wenzelm@65447
   763
wenzelm@65447
   764
text \<open>
wenzelm@65447
   765
  Addition is the inverse of subtraction: if \<open>b \<le> x\<close> then \<open>b #+ (x - b) = x\<close>.
wenzelm@65447
   766
  An example of induction over a quantified formula (a product).
wenzelm@65447
   767
  Uses rewriting with a quantified, implicative inductive hypothesis.
wenzelm@65447
   768
\<close>
wenzelm@65447
   769
schematic_goal add_diff_inverse_lemma:
wenzelm@65447
   770
  "b:N \<Longrightarrow> ?a : \<Prod>x:N. Eq(N, b-x, 0) \<longrightarrow> Eq(N, b #+ (x-b), x)"
wenzelm@65447
   771
  apply (NE b)
wenzelm@65447
   772
    \<comment> \<open>strip one "universal quantifier" but not the "implication"\<close>
wenzelm@65447
   773
    apply (rule_tac [3] intr_rls)
wenzelm@65447
   774
    \<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
   775
     prefer 4
wenzelm@65447
   776
     apply (NE x)
wenzelm@65447
   777
       apply assumption
wenzelm@65447
   778
    \<comment> \<open>Prepare for simplification of types -- the antecedent \<open>succ(u) \<le> x\<close>\<close>
wenzelm@65447
   779
      apply (rule_tac [2] replace_type)
wenzelm@65447
   780
       apply (rule_tac [1] replace_type)
wenzelm@65447
   781
        apply arith_rew
wenzelm@65447
   782
    \<comment> \<open>Solves first 0 goal, simplifies others.  Two sugbgoals remain.
wenzelm@65447
   783
    Both follow by rewriting, (2) using quantified induction hyp.\<close>
wenzelm@65447
   784
   apply intr \<comment> \<open>strips remaining \<open>\<Prod>\<close>s\<close>
wenzelm@65447
   785
    apply (hyp_arith_rew add_0_right)
wenzelm@65447
   786
  apply assumption
wenzelm@65447
   787
  done
wenzelm@65447
   788
wenzelm@65447
   789
text \<open>
wenzelm@65447
   790
  Version of above with premise \<open>b - a = 0\<close> i.e. \<open>a \<ge> b\<close>.
wenzelm@65447
   791
  Using @{thm ProdE} does not work -- for \<open>?B(?a)\<close> is ambiguous.
wenzelm@65447
   792
  Instead, @{thm add_diff_inverse_lemma} states the desired induction scheme;
wenzelm@65447
   793
  the use of \<open>THEN\<close> below instantiates Vars in @{thm ProdE} automatically.
wenzelm@65447
   794
\<close>
wenzelm@65447
   795
lemma add_diff_inverse: "\<lbrakk>a:N; b:N; b - a = 0 : N\<rbrakk> \<Longrightarrow> b #+ (a-b) = a : N"
wenzelm@65447
   796
  apply (rule EqE)
wenzelm@65447
   797
  apply (rule add_diff_inverse_lemma [THEN ProdE, THEN ProdE])
wenzelm@65447
   798
    apply (assumption | rule EqI)+
wenzelm@65447
   799
  done
wenzelm@65447
   800
wenzelm@65447
   801
wenzelm@65447
   802
subsection \<open>Absolute difference\<close>
wenzelm@65447
   803
wenzelm@65447
   804
text \<open>Typing of absolute difference: short and long versions.\<close>
wenzelm@65447
   805
wenzelm@65447
   806
lemma absdiff_typing: "\<lbrakk>a:N; b:N\<rbrakk> \<Longrightarrow> a |-| b : N"
wenzelm@65447
   807
  unfolding arith_defs by typechk
wenzelm@65447
   808
wenzelm@65447
   809
lemma absdiff_typingL: "\<lbrakk>a = c:N; b = d:N\<rbrakk> \<Longrightarrow> a |-| b = c |-| d : N"
wenzelm@65447
   810
  unfolding arith_defs by equal
wenzelm@65447
   811
wenzelm@65447
   812
lemma absdiff_self_eq_0: "a:N \<Longrightarrow> a |-| a = 0 : N"
wenzelm@65447
   813
  unfolding absdiff_def by (arith_rew diff_self_eq_0)
wenzelm@65447
   814
wenzelm@65447
   815
lemma absdiffC0: "a:N \<Longrightarrow> 0 |-| a = a : N"
wenzelm@65447
   816
  unfolding absdiff_def by hyp_arith_rew
wenzelm@65447
   817
wenzelm@65447
   818
lemma absdiff_succ_succ: "\<lbrakk>a:N; b:N\<rbrakk> \<Longrightarrow> succ(a) |-| succ(b)  =  a |-| b : N"
wenzelm@65447
   819
  unfolding absdiff_def by hyp_arith_rew
wenzelm@65447
   820
wenzelm@65447
   821
text \<open>Note how easy using commutative laws can be?  ...not always...\<close>
wenzelm@65447
   822
lemma absdiff_commute: "\<lbrakk>a:N; b:N\<rbrakk> \<Longrightarrow> a |-| b = b |-| a : N"
wenzelm@65447
   823
  unfolding absdiff_def
wenzelm@65447
   824
  apply (rule add_commute)
wenzelm@65447
   825
   apply (typechk diff_typing)
wenzelm@65447
   826
  done
wenzelm@65447
   827
wenzelm@65447
   828
text \<open>If \<open>a + b = 0\<close> then \<open>a = 0\<close>. Surprisingly tedious.\<close>
wenzelm@65447
   829
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
   830
  apply (NE a)
wenzelm@65447
   831
    apply (rule_tac [3] replace_type)
wenzelm@65447
   832
     apply arith_rew
wenzelm@65447
   833
  apply intr  \<comment> \<open>strips remaining \<open>\<Prod>\<close>s\<close>
wenzelm@65447
   834
   apply (rule_tac [2] zero_ne_succ [THEN FE])
wenzelm@65447
   835
     apply (erule_tac [3] EqE [THEN sym_elem])
wenzelm@65447
   836
    apply (typechk add_typing)
wenzelm@65447
   837
  done
wenzelm@65447
   838
wenzelm@65447
   839
text \<open>
wenzelm@65447
   840
  Version of above with the premise \<open>a + b = 0\<close>.
wenzelm@65447
   841
  Again, resolution instantiates variables in @{thm ProdE}.
wenzelm@65447
   842
\<close>
wenzelm@65447
   843
lemma add_eq0: "\<lbrakk>a:N; b:N; a #+ b = 0 : N\<rbrakk> \<Longrightarrow> a = 0 : N"
wenzelm@65447
   844
  apply (rule EqE)
wenzelm@65447
   845
  apply (rule add_eq0_lemma [THEN ProdE])
wenzelm@65447
   846
    apply (rule_tac [3] EqI)
wenzelm@65447
   847
    apply typechk
wenzelm@65447
   848
  done
wenzelm@65447
   849
wenzelm@65447
   850
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
   851
schematic_goal absdiff_eq0_lem:
wenzelm@65447
   852
  "\<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
   853
  apply (unfold absdiff_def)
wenzelm@65447
   854
  apply intr
wenzelm@65447
   855
   apply eqintr
wenzelm@65447
   856
   apply (rule_tac [2] add_eq0)
wenzelm@65447
   857
     apply (rule add_eq0)
wenzelm@65447
   858
       apply (rule_tac [6] add_commute [THEN trans_elem])
wenzelm@65447
   859
         apply (typechk diff_typing)
wenzelm@65447
   860
  done
wenzelm@65447
   861
wenzelm@65447
   862
text \<open>If \<open>a |-| b = 0\<close> then \<open>a = b\<close>
wenzelm@65447
   863
  proof: \<open>a - b = 0\<close> and \<open>b - a = 0\<close>, so \<open>b = a + (b - a) = a + 0 = a\<close>.
wenzelm@65447
   864
\<close>
wenzelm@65447
   865
lemma absdiff_eq0: "\<lbrakk>a |-| b = 0 : N; a:N; b:N\<rbrakk> \<Longrightarrow> a = b : N"
wenzelm@65447
   866
  apply (rule EqE)
wenzelm@65447
   867
  apply (rule absdiff_eq0_lem [THEN SumE])
wenzelm@65447
   868
     apply eqintr
wenzelm@65447
   869
  apply (rule add_diff_inverse [THEN sym_elem, THEN trans_elem])
wenzelm@65447
   870
     apply (erule_tac [3] EqE)
wenzelm@65447
   871
    apply (hyp_arith_rew add_0_right)
wenzelm@65447
   872
  done
wenzelm@65447
   873
wenzelm@65447
   874
wenzelm@65447
   875
subsection \<open>Remainder and Quotient\<close>
wenzelm@65447
   876
wenzelm@65447
   877
text \<open>Typing of remainder: short and long versions.\<close>
wenzelm@65447
   878
wenzelm@65447
   879
lemma mod_typing: "\<lbrakk>a:N; b:N\<rbrakk> \<Longrightarrow> a mod b : N"
wenzelm@65447
   880
  unfolding mod_def by (typechk absdiff_typing)
wenzelm@65447
   881
wenzelm@65447
   882
lemma mod_typingL: "\<lbrakk>a = c:N; b = d:N\<rbrakk> \<Longrightarrow> a mod b = c mod d : N"
wenzelm@65447
   883
  unfolding mod_def by (equal absdiff_typingL)
wenzelm@65447
   884
wenzelm@65447
   885
wenzelm@65447
   886
text \<open>Computation for \<open>mod\<close>: 0 and successor cases.\<close>
wenzelm@65447
   887
wenzelm@65447
   888
lemma modC0: "b:N \<Longrightarrow> 0 mod b = 0 : N"
wenzelm@65447
   889
  unfolding mod_def by (rew absdiff_typing)
wenzelm@65447
   890
wenzelm@65447
   891
lemma modC_succ: "\<lbrakk>a:N; b:N\<rbrakk> \<Longrightarrow>
wenzelm@65447
   892
  succ(a) mod b = rec(succ(a mod b) |-| b, 0, \<lambda>x y. succ(a mod b)) : N"
wenzelm@65447
   893
  unfolding mod_def by (rew absdiff_typing)
wenzelm@65447
   894
wenzelm@65447
   895
wenzelm@65447
   896
text \<open>Typing of quotient: short and long versions.\<close>
wenzelm@65447
   897
wenzelm@65447
   898
lemma div_typing: "\<lbrakk>a:N; b:N\<rbrakk> \<Longrightarrow> a div b : N"
wenzelm@65447
   899
  unfolding div_def by (typechk absdiff_typing mod_typing)
wenzelm@65447
   900
wenzelm@65447
   901
lemma div_typingL: "\<lbrakk>a = c:N; b = d:N\<rbrakk> \<Longrightarrow> a div b = c div d : N"
wenzelm@65447
   902
  unfolding div_def by (equal absdiff_typingL mod_typingL)
wenzelm@65447
   903
wenzelm@65447
   904
lemmas div_typing_rls = mod_typing div_typing absdiff_typing
wenzelm@65447
   905
wenzelm@65447
   906
wenzelm@65447
   907
text \<open>Computation for quotient: 0 and successor cases.\<close>
wenzelm@65447
   908
wenzelm@65447
   909
lemma divC0: "b:N \<Longrightarrow> 0 div b = 0 : N"
wenzelm@65447
   910
  unfolding div_def by (rew mod_typing absdiff_typing)
wenzelm@65447
   911
wenzelm@65447
   912
lemma divC_succ: "\<lbrakk>a:N; b:N\<rbrakk> \<Longrightarrow>
wenzelm@65447
   913
  succ(a) div b = rec(succ(a) mod b, succ(a div b), \<lambda>x y. a div b) : N"
wenzelm@65447
   914
  unfolding div_def by (rew mod_typing)
wenzelm@65447
   915
wenzelm@65447
   916
wenzelm@65447
   917
text \<open>Version of above with same condition as the \<open>mod\<close> one.\<close>
wenzelm@65447
   918
lemma divC_succ2: "\<lbrakk>a:N; b:N\<rbrakk> \<Longrightarrow>
wenzelm@65447
   919
  succ(a) div b =rec(succ(a mod b) |-| b, succ(a div b), \<lambda>x y. a div b) : N"
wenzelm@65447
   920
  apply (rule divC_succ [THEN trans_elem])
wenzelm@65447
   921
    apply (rew div_typing_rls modC_succ)
wenzelm@65447
   922
  apply (NE "succ (a mod b) |-|b")
wenzelm@65447
   923
    apply (rew mod_typing div_typing absdiff_typing)
wenzelm@65447
   924
  done
wenzelm@65447
   925
wenzelm@65447
   926
text \<open>For case analysis on whether a number is 0 or a successor.\<close>
wenzelm@65447
   927
lemma iszero_decidable: "a:N \<Longrightarrow> rec(a, inl(eq), \<lambda>ka kb. inr(<ka, eq>)) :
wenzelm@65447
   928
  Eq(N,a,0) + (\<Sum>x:N. Eq(N,a, succ(x)))"
wenzelm@65447
   929
  apply (NE a)
wenzelm@65447
   930
    apply (rule_tac [3] PlusI_inr)
wenzelm@65447
   931
     apply (rule_tac [2] PlusI_inl)
wenzelm@65447
   932
      apply eqintr
wenzelm@65447
   933
     apply equal
wenzelm@65447
   934
  done
wenzelm@65447
   935
wenzelm@65447
   936
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
   937
lemma mod_div_equality: "\<lbrakk>a:N; b:N\<rbrakk> \<Longrightarrow> a mod b #+ (a div b) #* b = a : N"
wenzelm@65447
   938
  apply (NE a)
wenzelm@65447
   939
    apply (arith_rew div_typing_rls modC0 modC_succ divC0 divC_succ2)
wenzelm@65447
   940
  apply (rule EqE)
wenzelm@65447
   941
    \<comment> \<open>case analysis on \<open>succ(u mod b) |-| b\<close>\<close>
wenzelm@65447
   942
  apply (rule_tac a1 = "succ (u mod b) |-| b" in iszero_decidable [THEN PlusE])
wenzelm@65447
   943
    apply (erule_tac [3] SumE)
wenzelm@65447
   944
    apply (hyp_arith_rew div_typing_rls modC0 modC_succ divC0 divC_succ2)
wenzelm@65447
   945
    \<comment> \<open>Replace one occurrence of \<open>b\<close> by \<open>succ(u mod b)\<close>. Clumsy!\<close>
wenzelm@65447
   946
  apply (rule add_typingL [THEN trans_elem])
wenzelm@65447
   947
    apply (erule EqE [THEN absdiff_eq0, THEN sym_elem])
wenzelm@65447
   948
     apply (rule_tac [3] refl_elem)
wenzelm@65447
   949
     apply (hyp_arith_rew div_typing_rls)
wenzelm@65447
   950
  done
wenzelm@65447
   951
wenzelm@19761
   952
end