2014-12-05 23:47:04 +00:00
|
|
|
/-
|
|
|
|
Copyright (c) 2014 Microsoft Corporation. All rights reserved.
|
|
|
|
Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
|
2015-02-28 00:02:18 +00:00
|
|
|
Module: init.num
|
2014-12-05 23:47:04 +00:00
|
|
|
Authors: Leonardo de Moura
|
|
|
|
-/
|
2015-02-26 18:19:54 +00:00
|
|
|
|
2014-12-05 23:47:04 +00:00
|
|
|
prelude
|
|
|
|
import init.logic init.bool
|
|
|
|
open bool
|
|
|
|
|
|
|
|
definition pos_num.is_inhabited [instance] : inhabited pos_num :=
|
|
|
|
inhabited.mk pos_num.one
|
|
|
|
|
|
|
|
namespace pos_num
|
|
|
|
definition is_one (a : pos_num) : bool :=
|
2015-02-11 20:49:27 +00:00
|
|
|
pos_num.rec_on a tt (λn r, ff) (λn r, ff)
|
2014-12-05 23:47:04 +00:00
|
|
|
|
|
|
|
definition pred (a : pos_num) : pos_num :=
|
2015-02-11 20:49:27 +00:00
|
|
|
pos_num.rec_on a one (λn r, bit0 n) (λn r, cond (is_one n) one (bit1 r))
|
2014-12-05 23:47:04 +00:00
|
|
|
|
|
|
|
definition size (a : pos_num) : pos_num :=
|
2015-02-11 20:49:27 +00:00
|
|
|
pos_num.rec_on a one (λn r, succ r) (λn r, succ r)
|
2014-12-05 23:47:04 +00:00
|
|
|
|
|
|
|
definition add (a b : pos_num) : pos_num :=
|
2015-02-11 20:49:27 +00:00
|
|
|
pos_num.rec_on a
|
2014-12-05 23:47:04 +00:00
|
|
|
succ
|
2015-02-11 20:49:27 +00:00
|
|
|
(λn f b, pos_num.rec_on b
|
2014-12-05 23:47:04 +00:00
|
|
|
(succ (bit1 n))
|
|
|
|
(λm r, succ (bit1 (f m)))
|
|
|
|
(λm r, bit1 (f m)))
|
2015-02-11 20:49:27 +00:00
|
|
|
(λn f b, pos_num.rec_on b
|
2014-12-05 23:47:04 +00:00
|
|
|
(bit1 n)
|
|
|
|
(λm r, bit1 (f m))
|
|
|
|
(λm r, bit0 (f m)))
|
|
|
|
b
|
|
|
|
|
|
|
|
notation a + b := add a b
|
|
|
|
|
|
|
|
definition mul (a b : pos_num) : pos_num :=
|
2015-02-11 20:49:27 +00:00
|
|
|
pos_num.rec_on a
|
2014-12-05 23:47:04 +00:00
|
|
|
b
|
|
|
|
(λn r, bit0 r + b)
|
|
|
|
(λn r, bit0 r)
|
|
|
|
|
|
|
|
notation a * b := mul a b
|
|
|
|
|
|
|
|
end pos_num
|
|
|
|
|
|
|
|
definition num.is_inhabited [instance] : inhabited num :=
|
|
|
|
inhabited.mk num.zero
|
|
|
|
|
|
|
|
namespace num
|
|
|
|
open pos_num
|
|
|
|
definition pred (a : num) : num :=
|
2015-02-11 20:49:27 +00:00
|
|
|
num.rec_on a zero (λp, cond (is_one p) zero (pos (pred p)))
|
2014-12-05 23:47:04 +00:00
|
|
|
|
|
|
|
definition size (a : num) : num :=
|
2015-02-11 20:49:27 +00:00
|
|
|
num.rec_on a (pos one) (λp, pos (size p))
|
2014-12-05 23:47:04 +00:00
|
|
|
|
|
|
|
definition add (a b : num) : num :=
|
2015-02-11 20:49:27 +00:00
|
|
|
num.rec_on a b (λp_a, num.rec_on b (pos p_a) (λp_b, pos (pos_num.add p_a p_b)))
|
2014-12-05 23:47:04 +00:00
|
|
|
|
|
|
|
definition mul (a b : num) : num :=
|
2015-02-11 20:49:27 +00:00
|
|
|
num.rec_on a zero (λp_a, num.rec_on b zero (λp_b, pos (pos_num.mul p_a p_b)))
|
2014-12-05 23:47:04 +00:00
|
|
|
|
|
|
|
notation a + b := add a b
|
|
|
|
notation a * b := mul a b
|
|
|
|
end num
|
2015-02-23 20:33:35 +00:00
|
|
|
|
|
|
|
-- the coercion from num to nat is defined here, so that it can already be used in init.trunc
|
|
|
|
namespace nat
|
|
|
|
|
|
|
|
definition add (a b : nat) : nat :=
|
|
|
|
nat.rec_on b a (λ b₁ r, succ r)
|
|
|
|
|
|
|
|
notation a + b := add a b
|
|
|
|
|
|
|
|
definition of_num [coercion] (n : num) : nat :=
|
|
|
|
num.rec zero
|
|
|
|
(λ n, pos_num.rec (succ zero) (λ n r, r + r + (succ zero)) (λ n r, r + r) n) n
|
|
|
|
end nat
|
|
|
|
attribute nat.of_num [reducible] -- of_num is also reducible if namespace "nat" is not opened
|