2014-12-01 04:34:12 +00:00
|
|
|
/-
|
|
|
|
Copyright (c) 2014 Microsoft Corporation. All rights reserved.
|
|
|
|
Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
Authors: Leonardo de Moura
|
|
|
|
-/
|
2015-03-03 21:37:38 +00:00
|
|
|
|
2014-12-01 04:34:12 +00:00
|
|
|
prelude
|
2015-10-07 23:44:47 +00:00
|
|
|
import init.logic init.bool init.priority
|
2014-11-07 16:21:42 +00:00
|
|
|
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-11-07 16:21:42 +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-11-07 16:21:42 +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-11-07 16:21:42 +00:00
|
|
|
|
|
|
|
definition add (a b : pos_num) : pos_num :=
|
2015-02-11 20:49:27 +00:00
|
|
|
pos_num.rec_on a
|
2014-11-07 16:21:42 +00:00
|
|
|
succ
|
2015-02-11 20:49:27 +00:00
|
|
|
(λn f b, pos_num.rec_on b
|
2014-11-07 16:21:42 +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-11-07 16:21:42 +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-11-07 16:21:42 +00:00
|
|
|
b
|
|
|
|
(λn r, bit0 r + b)
|
|
|
|
(λn r, bit0 r)
|
|
|
|
|
|
|
|
notation a * b := mul a b
|
|
|
|
|
2015-03-03 23:52:14 +00:00
|
|
|
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 :=
|
|
|
|
lt a (succ b)
|
2014-11-07 16:21:42 +00:00
|
|
|
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-11-07 16:21:42 +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-11-07 16:21:42 +00:00
|
|
|
|
|
|
|
definition add (a b : num) : num :=
|
2015-03-03 23:52:14 +00:00
|
|
|
num.rec_on a b (λpa, num.rec_on b (pos pa) (λpb, pos (pos_num.add pa pb)))
|
2014-11-07 16:21:42 +00:00
|
|
|
|
|
|
|
definition mul (a b : num) : num :=
|
2015-03-03 23:52:14 +00:00
|
|
|
num.rec_on a zero (λpa, num.rec_on b zero (λpb, pos (pos_num.mul pa pb)))
|
2014-11-07 16:21:42 +00:00
|
|
|
|
|
|
|
notation a + b := add a b
|
|
|
|
notation a * b := mul a b
|
2015-03-03 23:52:14 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
definition sub (a b : num) : num :=
|
|
|
|
num.rec_on a zero (λpa, num.rec_on b a (λpb, psub pa pb))
|
|
|
|
|
|
|
|
notation a ≤ b := le a b
|
|
|
|
notation a - b := sub a b
|
2014-11-07 16:21:42 +00:00
|
|
|
end num
|
2015-03-03 21:37:38 +00:00
|
|
|
|
|
|
|
-- the coercion from num to nat is defined here,
|
|
|
|
-- so that it can already be used in init.tactic
|
|
|
|
namespace nat
|
2015-10-07 23:44:47 +00:00
|
|
|
protected definition prio := num.add std.priority.default 100
|
2015-03-03 21:37:38 +00:00
|
|
|
|
2015-10-07 23:44:47 +00:00
|
|
|
protected definition add (a b : nat) : nat :=
|
2015-03-03 21:37:38 +00:00
|
|
|
nat.rec_on b a (λ b₁ r, succ r)
|
|
|
|
|
2015-10-07 23:44:47 +00:00
|
|
|
definition nat_has_add [reducible] [instance] [priority nat.prio] : has_add nat := has_add.mk nat.add
|
2015-03-03 21:37:38 +00:00
|
|
|
|
|
|
|
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
|
2015-08-04 11:20:13 +00:00
|
|
|
attribute nat.of_num [reducible] -- of_num is also reducible if namespace "nat" is not opened
|