src/ZF/Arith.thy
author wenzelm
Tue, 18 Mar 2008 20:33:29 +0100
changeset 26315 cb3badaa192e
parent 24893 b8ef7afe3a6b
child 35762 af3ff2ba4c54
permissions -rw-r--r--
removed redundant less_trans, less_linear, le_imp_less_or_eq, le_less_trans, less_le_trans (cf. Orderings.thy); renamed less_imp_le to less_imp_le_nat;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9654
9754ba005b64 X-symbols for ordinal, cardinal, integer arithmetic
paulson
parents: 9492
diff changeset
     1
(*  Title:      ZF/Arith.thy
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     2
    ID:         $Id$
1478
2b8c2a7547ab expanded tabs
clasohm
parents: 1401
diff changeset
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     4
    Copyright   1992  University of Cambridge
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     5
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     6
*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     7
13163
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
     8
(*"Difference" is subtraction of natural numbers.
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
     9
  There are no negative numbers; we have
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
    10
     m #- n = 0  iff  m<=n   and     m #- n = succ(k) iff m>n.
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
    11
  Also, rec(m, 0, %z w.z) is pred(m).   
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
    12
*)
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
    13
13328
703de709a64b better document preparation
paulson
parents: 13185
diff changeset
    14
header{*Arithmetic Operators and Their Definitions*} 
703de709a64b better document preparation
paulson
parents: 13185
diff changeset
    15
16417
9bc16273c2d4 migrated theory headers to new format
haftmann
parents: 15201
diff changeset
    16
theory Arith imports Univ begin
6070
032babd0120b ZF: the natural numbers as a datatype
paulson
parents: 3840
diff changeset
    17
13328
703de709a64b better document preparation
paulson
parents: 13185
diff changeset
    18
text{*Proofs about elementary arithmetic: addition, multiplication, etc.*}
703de709a64b better document preparation
paulson
parents: 13185
diff changeset
    19
24893
b8ef7afe3a6b modernized specifications;
wenzelm
parents: 16417
diff changeset
    20
definition
b8ef7afe3a6b modernized specifications;
wenzelm
parents: 16417
diff changeset
    21
  pred   :: "i=>i"    (*inverse of succ*)  where
13361
5005d34425bb new lemmas
paulson
parents: 13356
diff changeset
    22
    "pred(y) == nat_case(0, %x. x, y)"
6070
032babd0120b ZF: the natural numbers as a datatype
paulson
parents: 3840
diff changeset
    23
24893
b8ef7afe3a6b modernized specifications;
wenzelm
parents: 16417
diff changeset
    24
definition
b8ef7afe3a6b modernized specifications;
wenzelm
parents: 16417
diff changeset
    25
  natify :: "i=>i"    (*coerces non-nats to nats*)  where
9491
1a36151ee2fc natify, a coercion to reduce the number of type constraints in arithmetic
paulson
parents: 6070
diff changeset
    26
    "natify == Vrecursor(%f a. if a = succ(pred(a)) then succ(f`pred(a))
1a36151ee2fc natify, a coercion to reduce the number of type constraints in arithmetic
paulson
parents: 6070
diff changeset
    27
                                                    else 0)"
6070
032babd0120b ZF: the natural numbers as a datatype
paulson
parents: 3840
diff changeset
    28
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    29
consts
13163
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
    30
  raw_add  :: "[i,i]=>i"
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
    31
  raw_diff  :: "[i,i]=>i"
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
    32
  raw_mult  :: "[i,i]=>i"
9492
72e429c66608 used natify with div and mod; also put in the divide-by-zero trick
paulson
parents: 9491
diff changeset
    33
72e429c66608 used natify with div and mod; also put in the divide-by-zero trick
paulson
parents: 9491
diff changeset
    34
primrec
72e429c66608 used natify with div and mod; also put in the divide-by-zero trick
paulson
parents: 9491
diff changeset
    35
  "raw_add (0, n) = n"
72e429c66608 used natify with div and mod; also put in the divide-by-zero trick
paulson
parents: 9491
diff changeset
    36
  "raw_add (succ(m), n) = succ(raw_add(m, n))"
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    37
6070
032babd0120b ZF: the natural numbers as a datatype
paulson
parents: 3840
diff changeset
    38
primrec
13163
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
    39
  raw_diff_0:     "raw_diff(m, 0) = m"
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
    40
  raw_diff_succ:  "raw_diff(m, succ(n)) =
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
    41
                     nat_case(0, %x. x, raw_diff(m, n))"
9491
1a36151ee2fc natify, a coercion to reduce the number of type constraints in arithmetic
paulson
parents: 6070
diff changeset
    42
1a36151ee2fc natify, a coercion to reduce the number of type constraints in arithmetic
paulson
parents: 6070
diff changeset
    43
primrec
9492
72e429c66608 used natify with div and mod; also put in the divide-by-zero trick
paulson
parents: 9491
diff changeset
    44
  "raw_mult(0, n) = 0"
72e429c66608 used natify with div and mod; also put in the divide-by-zero trick
paulson
parents: 9491
diff changeset
    45
  "raw_mult(succ(m), n) = raw_add (n, raw_mult(m, n))"
13163
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
    46
24893
b8ef7afe3a6b modernized specifications;
wenzelm
parents: 16417
diff changeset
    47
definition
b8ef7afe3a6b modernized specifications;
wenzelm
parents: 16417
diff changeset
    48
  add :: "[i,i]=>i"                    (infixl "#+" 65)  where
9492
72e429c66608 used natify with div and mod; also put in the divide-by-zero trick
paulson
parents: 9491
diff changeset
    49
    "m #+ n == raw_add (natify(m), natify(n))"
9491
1a36151ee2fc natify, a coercion to reduce the number of type constraints in arithmetic
paulson
parents: 6070
diff changeset
    50
24893
b8ef7afe3a6b modernized specifications;
wenzelm
parents: 16417
diff changeset
    51
definition
b8ef7afe3a6b modernized specifications;
wenzelm
parents: 16417
diff changeset
    52
  diff :: "[i,i]=>i"                    (infixl "#-" 65)  where
9492
72e429c66608 used natify with div and mod; also put in the divide-by-zero trick
paulson
parents: 9491
diff changeset
    53
    "m #- n == raw_diff (natify(m), natify(n))"
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    54
24893
b8ef7afe3a6b modernized specifications;
wenzelm
parents: 16417
diff changeset
    55
definition
b8ef7afe3a6b modernized specifications;
wenzelm
parents: 16417
diff changeset
    56
  mult :: "[i,i]=>i"                    (infixl "#*" 70)  where
9492
72e429c66608 used natify with div and mod; also put in the divide-by-zero trick
paulson
parents: 9491
diff changeset
    57
    "m #* n == raw_mult (natify(m), natify(n))"
72e429c66608 used natify with div and mod; also put in the divide-by-zero trick
paulson
parents: 9491
diff changeset
    58
24893
b8ef7afe3a6b modernized specifications;
wenzelm
parents: 16417
diff changeset
    59
definition
b8ef7afe3a6b modernized specifications;
wenzelm
parents: 16417
diff changeset
    60
  raw_div  :: "[i,i]=>i"  where
13163
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
    61
    "raw_div (m, n) ==
9492
72e429c66608 used natify with div and mod; also put in the divide-by-zero trick
paulson
parents: 9491
diff changeset
    62
       transrec(m, %j f. if j<n | n=0 then 0 else succ(f`(j#-n)))"
72e429c66608 used natify with div and mod; also put in the divide-by-zero trick
paulson
parents: 9491
diff changeset
    63
24893
b8ef7afe3a6b modernized specifications;
wenzelm
parents: 16417
diff changeset
    64
definition
b8ef7afe3a6b modernized specifications;
wenzelm
parents: 16417
diff changeset
    65
  raw_mod  :: "[i,i]=>i"  where
13163
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
    66
    "raw_mod (m, n) ==
9492
72e429c66608 used natify with div and mod; also put in the divide-by-zero trick
paulson
parents: 9491
diff changeset
    67
       transrec(m, %j f. if j<n | n=0 then j else f`(j#-n))"
9491
1a36151ee2fc natify, a coercion to reduce the number of type constraints in arithmetic
paulson
parents: 6070
diff changeset
    68
24893
b8ef7afe3a6b modernized specifications;
wenzelm
parents: 16417
diff changeset
    69
definition
b8ef7afe3a6b modernized specifications;
wenzelm
parents: 16417
diff changeset
    70
  div  :: "[i,i]=>i"                    (infixl "div" 70)  where
9492
72e429c66608 used natify with div and mod; also put in the divide-by-zero trick
paulson
parents: 9491
diff changeset
    71
    "m div n == raw_div (natify(m), natify(n))"
9491
1a36151ee2fc natify, a coercion to reduce the number of type constraints in arithmetic
paulson
parents: 6070
diff changeset
    72
24893
b8ef7afe3a6b modernized specifications;
wenzelm
parents: 16417
diff changeset
    73
definition
b8ef7afe3a6b modernized specifications;
wenzelm
parents: 16417
diff changeset
    74
  mod  :: "[i,i]=>i"                    (infixl "mod" 70)  where
9492
72e429c66608 used natify with div and mod; also put in the divide-by-zero trick
paulson
parents: 9491
diff changeset
    75
    "m mod n == raw_mod (natify(m), natify(n))"
9491
1a36151ee2fc natify, a coercion to reduce the number of type constraints in arithmetic
paulson
parents: 6070
diff changeset
    76
24893
b8ef7afe3a6b modernized specifications;
wenzelm
parents: 16417
diff changeset
    77
notation (xsymbols)
b8ef7afe3a6b modernized specifications;
wenzelm
parents: 16417
diff changeset
    78
  mult  (infixr "#\<times>" 70)
9964
7966a2902266 tuned symbols;
wenzelm
parents: 9683
diff changeset
    79
24893
b8ef7afe3a6b modernized specifications;
wenzelm
parents: 16417
diff changeset
    80
notation (HTML output)
b8ef7afe3a6b modernized specifications;
wenzelm
parents: 16417
diff changeset
    81
  mult  (infixr "#\<times>" 70)
13163
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
    82
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
    83
declare rec_type [simp]
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
    84
        nat_0_le [simp]
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
    85
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
    86
14060
c0c4af41fa3b Adding the theory UNITY/AllocImpl.thy, with supporting lemmas
paulson
parents: 13784
diff changeset
    87
lemma zero_lt_lemma: "[| 0<k; k \<in> nat |] ==> \<exists>j\<in>nat. k = succ(j)"
13163
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
    88
apply (erule rev_mp)
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
    89
apply (induct_tac "k", auto)
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
    90
done
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
    91
14060
c0c4af41fa3b Adding the theory UNITY/AllocImpl.thy, with supporting lemmas
paulson
parents: 13784
diff changeset
    92
(* [| 0 < k; k \<in> nat; !!j. [| j \<in> nat; k = succ(j) |] ==> Q |] ==> Q *)
13163
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
    93
lemmas zero_lt_natE = zero_lt_lemma [THEN bexE, standard]
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
    94
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
    95
13356
c9cfe1638bf2 improved presentation markup
paulson
parents: 13328
diff changeset
    96
subsection{*@{text natify}, the Coercion to @{term nat}*}
13163
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
    97
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
    98
lemma pred_succ_eq [simp]: "pred(succ(y)) = y"
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
    99
by (unfold pred_def, auto)
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
   100
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
   101
lemma natify_succ: "natify(succ(x)) = succ(natify(x))"
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
   102
by (rule natify_def [THEN def_Vrecursor, THEN trans], auto)
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
   103
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
   104
lemma natify_0 [simp]: "natify(0) = 0"
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
   105
by (rule natify_def [THEN def_Vrecursor, THEN trans], auto)
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
   106
14060
c0c4af41fa3b Adding the theory UNITY/AllocImpl.thy, with supporting lemmas
paulson
parents: 13784
diff changeset
   107
lemma natify_non_succ: "\<forall>z. x ~= succ(z) ==> natify(x) = 0"
13163
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
   108
by (rule natify_def [THEN def_Vrecursor, THEN trans], auto)
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
   109
14060
c0c4af41fa3b Adding the theory UNITY/AllocImpl.thy, with supporting lemmas
paulson
parents: 13784
diff changeset
   110
lemma natify_in_nat [iff,TC]: "natify(x) \<in> nat"
13163
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
   111
apply (rule_tac a=x in eps_induct)
14060
c0c4af41fa3b Adding the theory UNITY/AllocImpl.thy, with supporting lemmas
paulson
parents: 13784
diff changeset
   112
apply (case_tac "\<exists>z. x = succ(z)")
13163
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
   113
apply (auto simp add: natify_succ natify_non_succ)
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
   114
done
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
   115
14060
c0c4af41fa3b Adding the theory UNITY/AllocImpl.thy, with supporting lemmas
paulson
parents: 13784
diff changeset
   116
lemma natify_ident [simp]: "n \<in> nat ==> natify(n) = n"
13163
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
   117
apply (induct_tac "n")
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
   118
apply (auto simp add: natify_succ)
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
   119
done
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
   120
14060
c0c4af41fa3b Adding the theory UNITY/AllocImpl.thy, with supporting lemmas
paulson
parents: 13784
diff changeset
   121
lemma natify_eqE: "[|natify(x) = y;  x \<in> nat|] ==> x=y"
13163
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
   122
by auto
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
   123
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
   124
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
   125
(*** Collapsing rules: to remove natify from arithmetic expressions ***)
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
   126
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
   127
lemma natify_idem [simp]: "natify(natify(x)) = natify(x)"
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
   128
by simp
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
   129
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
   130
(** Addition **)
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
   131
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
   132
lemma add_natify1 [simp]: "natify(m) #+ n = m #+ n"
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
   133
by (simp add: add_def)
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
   134
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
   135
lemma add_natify2 [simp]: "m #+ natify(n) = m #+ n"
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
   136
by (simp add: add_def)
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
   137
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
   138
(** Multiplication **)
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
   139
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
   140
lemma mult_natify1 [simp]: "natify(m) #* n = m #* n"
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
   141
by (simp add: mult_def)
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
   142
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
   143
lemma mult_natify2 [simp]: "m #* natify(n) = m #* n"
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
   144
by (simp add: mult_def)
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
   145
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
   146
(** Difference **)
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
   147
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
   148
lemma diff_natify1 [simp]: "natify(m) #- n = m #- n"
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
   149
by (simp add: diff_def)
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
   150
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
   151
lemma diff_natify2 [simp]: "m #- natify(n) = m #- n"
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
   152
by (simp add: diff_def)
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
   153
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
   154
(** Remainder **)
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
   155
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
   156
lemma mod_natify1 [simp]: "natify(m) mod n = m mod n"
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
   157
by (simp add: mod_def)
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
   158
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
   159
lemma mod_natify2 [simp]: "m mod natify(n) = m mod n"
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
   160
by (simp add: mod_def)
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
   161
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
   162
e320a52ff711 converted Arith, Univ, func to Isar format!
paulson
parents: 12114
diff changeset
   163
(** Quotient **)