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