2014-08-01 00:48:51 +00:00
|
|
|
----------------------------------------------------------------------------------------------------
|
2014-07-02 15:08:35 +00:00
|
|
|
-- Copyright (c) 2014 Microsoft Corporation. All rights reserved.
|
|
|
|
-- Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
-- Author: Leonardo de Moura
|
2014-08-01 00:48:51 +00:00
|
|
|
----------------------------------------------------------------------------------------------------
|
2014-10-05 17:50:13 +00:00
|
|
|
import logic.inhabited data.bool general_notation
|
2014-09-15 17:31:03 +00:00
|
|
|
open bool
|
2014-08-01 00:48:51 +00:00
|
|
|
|
2014-07-02 15:08:35 +00:00
|
|
|
-- pos_num and num are two auxiliary datatypes used when parsing numerals such as 13, 0, 26.
|
|
|
|
-- The parser will generate the terms (pos (bit1 (bit1 (bit0 one)))), zero, and (pos (bit0 (bit1 (bit1 one)))).
|
|
|
|
-- This representation can be coerced in whatever we want (e.g., naturals, integers, reals, etc).
|
|
|
|
inductive pos_num : Type :=
|
2014-08-22 22:46:10 +00:00
|
|
|
one : pos_num,
|
|
|
|
bit1 : pos_num → pos_num,
|
|
|
|
bit0 : pos_num → pos_num
|
2014-07-02 15:08:35 +00:00
|
|
|
|
2014-10-02 16:00:34 +00:00
|
|
|
definition pos_num.is_inhabited [instance] : inhabited pos_num :=
|
2014-09-15 17:31:03 +00:00
|
|
|
inhabited.mk pos_num.one
|
|
|
|
|
2014-09-05 05:31:52 +00:00
|
|
|
namespace pos_num
|
2014-09-19 22:04:52 +00:00
|
|
|
protected theorem induction_on {P : pos_num → Prop} (a : pos_num)
|
2014-09-15 17:31:03 +00:00
|
|
|
(H₁ : P one) (H₂ : ∀ (n : pos_num), P n → P (bit1 n)) (H₃ : ∀ (n : pos_num), P n → P (bit0 n)) : P a :=
|
|
|
|
rec H₁ H₂ H₃ a
|
|
|
|
|
2014-09-19 22:04:52 +00:00
|
|
|
protected definition rec_on {P : pos_num → Type} (a : pos_num)
|
2014-09-15 17:31:03 +00:00
|
|
|
(H₁ : P one) (H₂ : ∀ (n : pos_num), P n → P (bit1 n)) (H₃ : ∀ (n : pos_num), P n → P (bit0 n)) : P a :=
|
|
|
|
rec H₁ H₂ H₃ a
|
|
|
|
|
2014-09-17 21:39:05 +00:00
|
|
|
definition succ (a : pos_num) : pos_num :=
|
2014-09-15 17:31:03 +00:00
|
|
|
rec_on a (bit0 one) (λn r, bit0 r) (λn r, bit1 n)
|
|
|
|
|
2014-09-17 21:39:05 +00:00
|
|
|
definition is_one (a : pos_num) : bool :=
|
2014-09-15 17:31:03 +00:00
|
|
|
rec_on a tt (λn r, ff) (λn r, ff)
|
|
|
|
|
2014-09-17 21:39:05 +00:00
|
|
|
definition pred (a : pos_num) : pos_num :=
|
2014-09-15 17:31:03 +00:00
|
|
|
rec_on a one (λn r, bit0 n) (λn r, cond (is_one n) one (bit1 r))
|
|
|
|
|
2014-09-17 21:39:05 +00:00
|
|
|
definition size (a : pos_num) : pos_num :=
|
2014-09-15 17:31:03 +00:00
|
|
|
rec_on a one (λn r, succ r) (λn r, succ r)
|
|
|
|
|
2014-10-05 20:47:51 +00:00
|
|
|
theorem succ_not_is_one (a : pos_num) : is_one (succ a) = ff :=
|
2014-09-15 17:31:03 +00:00
|
|
|
induction_on a rfl (take n iH, rfl) (take n iH, rfl)
|
|
|
|
|
2014-10-05 20:47:51 +00:00
|
|
|
theorem pred.succ (a : pos_num) : pred (succ a) = a :=
|
2014-09-15 17:31:03 +00:00
|
|
|
rec_on a
|
|
|
|
rfl
|
|
|
|
(take (n : pos_num) (iH : pred (succ n) = n),
|
|
|
|
calc
|
2014-10-05 20:47:51 +00:00
|
|
|
pred (succ (bit1 n)) = cond ff one (bit1 (pred (succ n))) : {!succ_not_is_one}
|
2014-09-15 17:31:03 +00:00
|
|
|
... = bit1 (pred (succ n)) : rfl
|
|
|
|
... = bit1 n : {iH})
|
|
|
|
(take (n : pos_num) (iH : pred (succ n) = n), rfl)
|
2014-09-16 00:23:55 +00:00
|
|
|
|
2014-09-17 21:39:05 +00:00
|
|
|
definition add (a b : pos_num) : pos_num :=
|
2014-09-16 00:23:55 +00:00
|
|
|
rec_on a
|
|
|
|
succ
|
|
|
|
(λn f b, rec_on b
|
|
|
|
(succ (bit1 n))
|
|
|
|
(λm r, succ (bit1 (f m)))
|
|
|
|
(λm r, bit1 (f m)))
|
|
|
|
(λn f b, rec_on b
|
|
|
|
(bit1 n)
|
|
|
|
(λm r, bit1 (f m))
|
|
|
|
(λm r, bit0 (f m)))
|
|
|
|
b
|
|
|
|
|
2014-10-21 21:08:07 +00:00
|
|
|
notation a + b := add a b
|
2014-09-16 00:23:55 +00:00
|
|
|
|
2014-10-05 20:47:51 +00:00
|
|
|
section
|
|
|
|
variables (a b : pos_num)
|
|
|
|
|
|
|
|
theorem add.one_one : one + one = bit0 one :=
|
2014-09-16 00:23:55 +00:00
|
|
|
rfl
|
|
|
|
|
2014-10-05 20:47:51 +00:00
|
|
|
theorem add.one_bit0 : one + (bit0 a) = bit1 a :=
|
2014-09-16 00:23:55 +00:00
|
|
|
rfl
|
|
|
|
|
2014-10-05 20:47:51 +00:00
|
|
|
theorem add.one_bit1 : one + (bit1 a) = succ (bit1 a) :=
|
2014-09-16 00:23:55 +00:00
|
|
|
rfl
|
|
|
|
|
2014-10-05 20:47:51 +00:00
|
|
|
theorem add.bit0_one : (bit0 a) + one = bit1 a :=
|
2014-09-16 00:23:55 +00:00
|
|
|
rfl
|
|
|
|
|
2014-10-05 20:47:51 +00:00
|
|
|
theorem add.bit1_one : (bit1 a) + one = succ (bit1 a) :=
|
2014-09-16 00:23:55 +00:00
|
|
|
rfl
|
|
|
|
|
2014-10-05 20:47:51 +00:00
|
|
|
theorem add.bit0_bit0 : (bit0 a) + (bit0 b) = bit0 (a + b) :=
|
2014-09-16 00:23:55 +00:00
|
|
|
rfl
|
|
|
|
|
2014-10-05 20:47:51 +00:00
|
|
|
theorem add.bit0_bit1 : (bit0 a) + (bit1 b) = bit1 (a + b) :=
|
2014-09-16 00:23:55 +00:00
|
|
|
rfl
|
|
|
|
|
2014-10-05 20:47:51 +00:00
|
|
|
theorem add.bit1_bit0 : (bit1 a) + (bit0 b) = bit1 (a + b) :=
|
2014-09-16 00:23:55 +00:00
|
|
|
rfl
|
|
|
|
|
2014-10-05 20:47:51 +00:00
|
|
|
theorem add.bit1_bit1 : (bit1 a) + (bit1 b) = succ (bit1 (a + b)) :=
|
2014-09-16 00:23:55 +00:00
|
|
|
rfl
|
2014-10-05 20:47:51 +00:00
|
|
|
end
|
2014-09-16 00:23:55 +00:00
|
|
|
|
2014-09-17 21:39:05 +00:00
|
|
|
definition mul (a b : pos_num) : pos_num :=
|
2014-09-16 00:23:55 +00:00
|
|
|
rec_on a
|
|
|
|
b
|
|
|
|
(λn r, bit0 r + b)
|
|
|
|
(λn r, bit0 r)
|
|
|
|
|
2014-10-21 21:08:07 +00:00
|
|
|
notation a * b := mul a b
|
2014-09-16 00:23:55 +00:00
|
|
|
|
2014-10-05 20:47:51 +00:00
|
|
|
theorem mul.one_left (a : pos_num) : one * a = a :=
|
2014-09-16 00:23:55 +00:00
|
|
|
rfl
|
|
|
|
|
2014-10-05 20:47:51 +00:00
|
|
|
theorem mul.one_right (a : pos_num) : a * one = a :=
|
2014-09-16 00:23:55 +00:00
|
|
|
induction_on a
|
|
|
|
rfl
|
|
|
|
(take (n : pos_num) (iH : n * one = n),
|
|
|
|
calc bit1 n * one = bit0 (n * one) + one : rfl
|
|
|
|
... = bit0 n + one : {iH}
|
2014-10-05 20:47:51 +00:00
|
|
|
... = bit1 n : !add.bit0_one)
|
2014-09-16 00:23:55 +00:00
|
|
|
(take (n : pos_num) (iH : n * one = n),
|
|
|
|
calc bit0 n * one = bit0 (n * one) : rfl
|
|
|
|
... = bit0 n : {iH})
|
2014-09-05 05:31:52 +00:00
|
|
|
end pos_num
|
|
|
|
|
2014-07-02 15:08:35 +00:00
|
|
|
inductive num : Type :=
|
2014-08-22 22:46:10 +00:00
|
|
|
zero : num,
|
|
|
|
pos : pos_num → num
|
2014-07-02 15:08:35 +00:00
|
|
|
|
2014-10-02 16:00:34 +00:00
|
|
|
definition num.is_inhabited [instance] : inhabited num :=
|
2014-09-04 23:36:06 +00:00
|
|
|
inhabited.mk num.zero
|
2014-09-15 17:31:03 +00:00
|
|
|
|
|
|
|
namespace num
|
|
|
|
open pos_num
|
2014-09-19 22:04:52 +00:00
|
|
|
protected theorem induction_on {P : num → Prop} (a : num)
|
2014-09-15 17:31:03 +00:00
|
|
|
(H₁ : P zero) (H₂ : ∀ (p : pos_num), P (pos p)) : P a :=
|
|
|
|
rec H₁ H₂ a
|
|
|
|
|
2014-09-19 22:04:52 +00:00
|
|
|
protected definition rec_on {P : num → Type} (a : num)
|
2014-09-15 17:31:03 +00:00
|
|
|
(H₁ : P zero) (H₂ : ∀ (p : pos_num), P (pos p)) : P a :=
|
|
|
|
rec H₁ H₂ a
|
|
|
|
|
2014-09-17 21:39:05 +00:00
|
|
|
definition succ (a : num) : num :=
|
2014-09-15 17:31:03 +00:00
|
|
|
rec_on a (pos one) (λp, pos (succ p))
|
|
|
|
|
2014-09-17 21:39:05 +00:00
|
|
|
definition pred (a : num) : num :=
|
2014-09-15 17:31:03 +00:00
|
|
|
rec_on a zero (λp, cond (is_one p) zero (pos (pred p)))
|
|
|
|
|
2014-09-17 21:39:05 +00:00
|
|
|
definition size (a : num) : num :=
|
2014-09-15 17:31:03 +00:00
|
|
|
rec_on a (pos one) (λp, pos (size p))
|
|
|
|
|
2014-10-05 20:47:51 +00:00
|
|
|
theorem pred.succ (a : num) : pred (succ a) = a :=
|
2014-09-15 17:31:03 +00:00
|
|
|
rec_on a
|
|
|
|
rfl
|
|
|
|
(λp, calc
|
|
|
|
pred (succ (pos p)) = pred (pos (pos_num.succ p)) : rfl
|
2014-10-05 20:47:51 +00:00
|
|
|
... = cond ff zero (pos (pos_num.pred (pos_num.succ p))) : {!succ_not_is_one}
|
2014-10-05 16:50:55 +00:00
|
|
|
... = pos (pos_num.pred (pos_num.succ p)) : !cond.ff
|
2014-10-05 20:47:51 +00:00
|
|
|
... = pos p : {!pos_num.pred.succ})
|
2014-09-16 00:23:55 +00:00
|
|
|
|
2014-09-17 21:39:05 +00:00
|
|
|
definition add (a b : num) : num :=
|
2014-09-16 00:23:55 +00:00
|
|
|
rec_on a b (λp_a, rec_on b (pos p_a) (λp_b, pos (pos_num.add p_a p_b)))
|
|
|
|
|
2014-09-17 21:39:05 +00:00
|
|
|
definition mul (a b : num) : num :=
|
2014-09-16 00:23:55 +00:00
|
|
|
rec_on a zero (λp_a, rec_on b zero (λp_b, pos (pos_num.mul p_a p_b)))
|
|
|
|
|
2014-10-21 21:08:07 +00:00
|
|
|
notation a + b := add a b
|
|
|
|
notation a * b := mul a b
|
2014-09-15 17:31:03 +00:00
|
|
|
end num
|