Debian Codegraph @

theories/Corelib/BinNums/NatDef.v

raw
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 From Corelib Require Export BinNums PosDef.
12
13 (**********************************************************************)
14 (** * Binary natural numbers, definitions of operations *)
15 (**********************************************************************)
16
17 Module N.
18
19 (** ** Operation [x -> 2*x+1] *)
20
21 Definition succ_double x :=
22 match x with
23 | N0 => Npos 1
24 | Npos p => Npos p~1
25 end.
26
27 (** ** Operation [x -> 2*x] *)
28
29 Definition double n :=
30 match n with
31 | N0 => N0
32 | Npos p => Npos p~0
33 end.
34
35 (** ** The successor of a [N] can be seen as a [positive] *)
36
37 Definition succ_pos (n : N) : positive :=
38 match n with
39 | N0 => xH
40 | Npos p => Pos.succ p
41 end.
42
43 (** Subtraction *)
44
45 Definition sub n m :=
46 match n, m with
47 | N0, _ => N0
48 | n, N0 => n
49 | Npos n', Npos m' =>
50 match Pos.sub_mask n' m' with
51 | Pos.IsPos p => Npos p
52 | _ => N0
53 end
54 end.
55
56 (** Order *)
57
58 Definition compare n m :=
59 match n, m with
60 | N0, N0 => Eq
61 | N0, Npos m' => Lt
62 | Npos n', N0 => Gt
63 | Npos n', Npos m' => Pos.compare n' m'
64 end.
65
66 (** Boolean equality and comparison *)
67
68 Definition leb x y :=
69 match compare x y with Gt => false | _ => true end.
70
71 (** Euclidean division *)
72
73 Fixpoint pos_div_eucl (a:positive)(b:N) : N * N :=
74 match a with
75 | xH =>
76 match b with Npos 1 => (Npos 1, N0) | _ => (N0, Npos 1) end
77 | xO a' =>
78 let (q, r) := pos_div_eucl a' b in
79 let r' := double r in
80 if leb b r' then (succ_double q, sub r' b)
81 else (double q, r')
82 | xI a' =>
83 let (q, r) := pos_div_eucl a' b in
84 let r' := succ_double r in
85 if leb b r' then (succ_double q, sub r' b)
86 else (double q, r')
87 end.
88
89 (** Operation over bits of a [N] number. *)
90
91 (** Logical [or] *)
92
93 Definition lor n m :=
94 match n, m with
95 | N0, _ => m
96 | _, N0 => n
97 | Npos p, Npos q => Npos (Pos.lor p q)
98 end.
99
100 (** Logical [and] *)
101
102 Definition land n m :=
103 match n, m with
104 | N0, _ => N0
105 | _, N0 => N0
106 | Npos p, Npos q => Pos.land p q
107 end.
108
109 (** Logical [diff] *)
110
111 Definition ldiff n m :=
112 match n, m with
113 | N0, _ => N0
114 | _, N0 => n
115 | Npos p, Npos q => Pos.ldiff p q
116 end.
117
118 (** [xor] *)
119
120 Definition lxor n m :=
121 match n, m with
122 | N0, _ => m
123 | _, N0 => n
124 | Npos p, Npos q => Pos.lxor p q
125 end.
126
127 End N.