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.
|
|
|
|
Authors: Leonardo de Moura
|
|
|
|
-/
|
|
|
|
prelude
|
2015-12-08 17:57:55 +00:00
|
|
|
import init.bool
|
|
|
|
open bool algebra
|
2014-12-05 23:47:04 +00:00
|
|
|
|
|
|
|
namespace pos_num
|
2015-12-08 17:57:55 +00:00
|
|
|
protected 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)
|
|
|
|
|
2015-03-04 00:22:59 +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 :=
|
2015-12-08 17:57:55 +00:00
|
|
|
pos_num.lt a (succ b)
|
2014-12-05 23:47:04 +00:00
|
|
|
end pos_num
|
|
|
|
|
2015-12-08 17:57:55 +00:00
|
|
|
definition pos_num_has_mul [instance] [reducible] : has_mul pos_num :=
|
|
|
|
has_mul.mk pos_num.mul
|
2014-12-05 23:47:04 +00:00
|
|
|
|
|
|
|
namespace num
|
|
|
|
open pos_num
|
2015-03-04 00:22:59 +00:00
|
|
|
|
2014-12-05 23:47:04 +00:00
|
|
|
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
|
|
|
|
2015-12-08 17:57:55 +00:00
|
|
|
protected definition mul (a b : num) : num :=
|
2015-03-04 00:22:59 +00:00
|
|
|
num.rec_on a zero (λpa, num.rec_on b zero (λpb, pos (pos_num.mul pa pb)))
|
2015-12-08 17:57:55 +00:00
|
|
|
end num
|
2014-12-05 23:47:04 +00:00
|
|
|
|
2015-12-08 17:57:55 +00:00
|
|
|
definition num_has_mul [instance] [reducible] : has_mul num :=
|
|
|
|
has_mul.mk num.mul
|
2015-02-23 20:33:35 +00:00
|
|
|
|
2015-12-08 17:57:55 +00:00
|
|
|
namespace num
|
|
|
|
protected definition le (a b : num) : bool :=
|
2015-03-04 00:22:59 +00:00
|
|
|
num.rec_on a tt (λpa, num.rec_on b ff (λpb, pos_num.le pa pb))
|
2015-02-23 20:33:35 +00:00
|
|
|
|
2015-03-04 00:22:59 +00:00
|
|
|
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
|
2015-02-23 20:33:35 +00:00
|
|
|
|
2015-12-08 17:57:55 +00:00
|
|
|
protected definition sub (a b : num) : num :=
|
2015-03-04 00:22:59 +00:00
|
|
|
num.rec_on a zero (λpa, num.rec_on b a (λpb, psub pa pb))
|
|
|
|
end num
|
|
|
|
|
2015-12-08 17:57:55 +00:00
|
|
|
definition num_has_sub [instance] [reducible] : has_sub num :=
|
|
|
|
has_sub.mk num.sub
|