lean2/library/init/num.lean
2015-11-08 14:04:55 -08:00

114 lines
3 KiB
Text

/-
Copyright (c) 2014 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
prelude
import init.logic init.bool init.priority
open bool
definition pos_num.is_inhabited [instance] : inhabited pos_num :=
inhabited.mk pos_num.one
namespace pos_num
protected definition mul (a b : pos_num) : pos_num :=
pos_num.rec_on a
b
(λn r, bit0 r + b)
(λn r, bit0 r)
definition lt (a b : pos_num) : bool :=
pos_num.rec_on a
(λ b, pos_num.cases_on b
ff
(λm, tt)
(λm, tt))
(λn f b, pos_num.cases_on b
ff
(λm, f m)
(λm, f m))
(λn f b, pos_num.cases_on b
ff
(λm, f (succ m))
(λm, f m))
b
definition le (a b : pos_num) : bool :=
pos_num.lt a (succ b)
end pos_num
definition pos_num_has_mul [instance] [reducible] : has_mul pos_num :=
has_mul.mk pos_num.mul
definition num.is_inhabited [instance] : inhabited num :=
inhabited.mk num.zero
namespace num
open pos_num
definition pred (a : num) : num :=
num.rec_on a zero (λp, cond (is_one p) zero (pos (pred p)))
definition size (a : num) : num :=
num.rec_on a (pos one) (λp, pos (size p))
protected definition mul (a b : num) : num :=
num.rec_on a zero (λpa, num.rec_on b zero (λpb, pos (pos_num.mul pa pb)))
end num
definition num_has_mul [instance] [reducible] : has_mul num :=
has_mul.mk num.mul
namespace num
protected definition le (a b : num) : bool :=
num.rec_on a tt (λpa, num.rec_on b ff (λpb, pos_num.le pa pb))
private definition psub (a b : pos_num) : num :=
pos_num.rec_on a
(λb, zero)
(λn f b,
cond (pos_num.le (bit1 n) b)
zero
(pos_num.cases_on b
(pos (bit0 n))
(λm, 2 * f m)
(λm, 2 * f m + 1)))
(λn f b,
cond (pos_num.le (bit0 n) b)
zero
(pos_num.cases_on b
(pos (pos_num.pred (bit0 n)))
(λm, pred (2 * f m))
(λm, 2 * f m)))
b
protected definition sub (a b : num) : num :=
num.rec_on a zero (λpa, num.rec_on b a (λpb, psub pa pb))
end num
definition num_has_sub [instance] [reducible] : has_sub num :=
has_sub.mk num.sub
-- the coercion from num to nat is defined here,
-- so that it can already be used in init.tactic
namespace nat
protected definition prio := num.add std.priority.default 100
protected definition add (a b : nat) : nat :=
nat.rec_on b a (λ b₁ r, succ r)
definition nat_has_zero [reducible] [instance] : has_zero nat :=
has_zero.mk nat.zero
definition nat_has_one [reducible] [instance] : has_one nat :=
has_one.mk (nat.succ (nat.zero))
definition nat_has_add [reducible] [instance] [priority nat.prio] : has_add nat :=
has_add.mk nat.add
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