(************************************************************************) (* * The Rocq Prover / The Rocq Development Team *) (* v * Copyright INRIA, CNRS and contributors *) (* <O___,, * (see version control and CREDITS file for authors & dates) *) (* \VV/ **************************************************************) (* // * This file is distributed under the terms of the *) (* * GNU Lesser General Public License Version 2.1 *) (* * (see LICENSE file for the text of the license) *) (************************************************************************)
Fixpoint compare n m : comparison := match n, m with
| 0, 0 => Eq
| 0, S _ => Lt
| S _, 0 => Gt
| S n', S m' => compare n' m' end.
Infix"?=" := compare (at level 70) : nat_scope.
(** ** Minimum, maximum *)
Fixpoint max n m := match n, m with
| 0, _ => m
| S n', 0 => n
| S n', S m' => S (max n' m') end.
Fixpoint min n m := match n, m with
| 0, _ => 0
| S n', 0 => 0
| S n', S m' => S (min n' m') end.
(** ** Parity tests *)
Fixpoint even n : bool := match n with
| 0 => true
| 1 => false
| S (S n') => even n' end.
Definition odd n := negb (even n).
(** ** Power *)
Fixpoint pow n m := match m with
| 0 => 1
| S m => n * (n^m) end
where"n ^ m" := (pow n m) : nat_scope.
(** ** Tail-recursive versions of [add] and [mul] *)
Fixpoint tail_add n m := match n with
| O => m
| S n => tail_add n (S m) end.
(** [tail_addmul r n m] is [r + n * m]. *)
Fixpoint tail_addmul r n m := match n with
| O => r
| S n => tail_addmul (tail_add m r) n m end.
Definition tail_mul n m := tail_addmul 0 n m.
(** ** Conversion with a decimal representation for printing/parsing *)
LocalNotation ten := (S (S (S (S (S (S (S (S (S (S O)))))))))).
Fixpoint of_uint_acc (d:Decimal.uint)(acc:nat) := match d with
| Decimal.Nil => acc
| Decimal.D0 d => of_uint_acc d (tail_mul ten acc)
| Decimal.D1 d => of_uint_acc d (S (tail_mul ten acc))
| Decimal.D2 d => of_uint_acc d (S (S (tail_mul ten acc)))
| Decimal.D3 d => of_uint_acc d (S (S (S (tail_mul ten acc))))
| Decimal.D4 d => of_uint_acc d (S (S (S (S (tail_mul ten acc)))))
| Decimal.D5 d => of_uint_acc d (S (S (S (S (S (tail_mul ten acc))))))
| Decimal.D6 d => of_uint_acc d (S (S (S (S (S (S (tail_mul ten acc)))))))
| Decimal.D7 d => of_uint_acc d (S (S (S (S (S (S (S (tail_mul ten acc))))))))
| Decimal.D8 d => of_uint_acc d (S (S (S (S (S (S (S (S (tail_mul ten acc)))))))))
| Decimal.D9 d => of_uint_acc d (S (S (S (S (S (S (S (S (S (tail_mul ten acc)))))))))) end.
Definition of_uint (d:Decimal.uint) := of_uint_acc d O.
LocalNotation sixteen := (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S O)))))))))))))))).
Fixpoint of_hex_uint_acc (d:Hexadecimal.uint)(acc:nat) := match d with
| Hexadecimal.Nil => acc
| Hexadecimal.D0 d => of_hex_uint_acc d (tail_mul sixteen acc)
| Hexadecimal.D1 d => of_hex_uint_acc d (S (tail_mul sixteen acc))
| Hexadecimal.D2 d => of_hex_uint_acc d (S (S (tail_mul sixteen acc)))
| Hexadecimal.D3 d => of_hex_uint_acc d (S (S (S (tail_mul sixteen acc))))
| Hexadecimal.D4 d => of_hex_uint_acc d (S (S (S (S (tail_mul sixteen acc)))))
| Hexadecimal.D5 d => of_hex_uint_acc d (S (S (S (S (S (tail_mul sixteen acc))))))
| Hexadecimal.D6 d => of_hex_uint_acc d (S (S (S (S (S (S (tail_mul sixteen acc)))))))
| Hexadecimal.D7 d => of_hex_uint_acc d (S (S (S (S (S (S (S (tail_mul sixteen acc))))))))
| Hexadecimal.D8 d => of_hex_uint_acc d (S (S (S (S (S (S (S (S (tail_mul sixteen acc)))))))))
| Hexadecimal.D9 d => of_hex_uint_acc d (S (S (S (S (S (S (S (S (S (tail_mul sixteen acc))))))))))
| Hexadecimal.Da d => of_hex_uint_acc d (S (S (S (S (S (S (S (S (S (S (tail_mul sixteen acc)))))))))))
| Hexadecimal.Db d => of_hex_uint_acc d (S (S (S (S (S (S (S (S (S (S (S (tail_mul sixteen acc))))))))))))
| Hexadecimal.Dc d => of_hex_uint_acc d (S (S (S (S (S (S (S (S (S (S (S (S (tail_mul sixteen acc)))))))))))))
| Hexadecimal.Dd d => of_hex_uint_acc d (S (S (S (S (S (S (S (S (S (S (S (S (S (tail_mul sixteen acc))))))))))))))
| Hexadecimal.De d => of_hex_uint_acc d (S (S (S (S (S (S (S (S (S (S (S (S (S (S (tail_mul sixteen acc)))))))))))))))
| Hexadecimal.Df d => of_hex_uint_acc d (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (tail_mul sixteen acc)))))))))))))))) end.
Definition of_hex_uint (d:Hexadecimal.uint) := of_hex_uint_acc d O.
Definition of_num_uint (d:Number.uint) := match d with
| Number.UIntDecimal d => of_uint d
| Number.UIntHexadecimal d => of_hex_uint d end.
Fixpoint to_little_uint n acc := match n with
| O => acc
| S n => to_little_uint n (Decimal.Little.succ acc) end.
Definition to_uint n :=
Decimal.rev (to_little_uint n Decimal.zero).
Fixpoint to_little_hex_uint n acc := match n with
| O => acc
| S n => to_little_hex_uint n (Hexadecimal.Little.succ acc) end.
Definition to_hex_uint n :=
Hexadecimal.rev (to_little_hex_uint n Hexadecimal.zero).
Definition to_num_uint n := Number.UIntDecimal (to_uint n).
Definition to_num_hex_uint n := Number.UIntHexadecimal (to_hex_uint n).
Definition of_int (d:Decimal.int) : option nat := match Decimal.norm d with
| Decimal.Pos u => Some (of_uint u)
| _ => None end.
Definition of_hex_int (d:Hexadecimal.int) : option nat := match Hexadecimal.norm d with
| Hexadecimal.Pos u => Some (of_hex_uint u)
| _ => None end.
Definition of_num_int (d:Number.int) : option nat := match d with
| Number.IntDecimal d => of_int d
| Number.IntHexadecimal d => of_hex_int d end.
Definition to_int n := Decimal.Pos (to_uint n).
Definition to_hex_int n := Hexadecimal.Pos (to_hex_uint n).
Definition to_num_int n := Number.IntDecimal (to_int n).
(** ** Euclidean division *)
(** This division is linear and tail-recursive. In[divmod],[y]isthepredecessoroftheactualdivisor, and[u]is[y]minustherealremainder
*)
Fixpoint divmod x y q u := match x with
| 0 => (q,u)
| S x' => match u with
| 0 => divmod x' y (S q) y
| S u' => divmod x' y q u' end end.
Definition div x y := match y with
| 0 => y
| S y' => fst (divmod x y' 0 y') end.
Definition modulo x y := match y with
| 0 => x
| S y' => y' - snd (divmod x y' 0 y') end.
Infix"/" := div : nat_scope. Infix"mod" := modulo (at level 40, no associativity) : nat_scope.
(** ** Greatest common divisor *)
(** We use Euclid algorithm, which is normally not structural, butRocqisnowcleverenoughtoacceptthis(behindmodulo thereisasubtraction,whichnowpreservesbeingasubterm)
*)
Fixpoint gcd a b := match a with
| O => b
| S a' => gcd (b mod (S a')) (S a') end.
(** ** Square *)
Definition square n := n * n.
(** ** Square root *)
(** The following square root function is linear (and tail-recursive). WithPeanorepresentation,wecan'tdobetter.Forfasteralgorithm, seePsqrt/Zsqrt/Nsqrt...
Fixpoint sqrt_iter k p q r := match k with
| O => p
| S k' => match r with
| O => sqrt_iter k' (S p) (S (S q)) (S (S q))
| S r' => sqrt_iter k' p q r' end end.
Definition sqrt n := sqrt_iter n 000.
(** ** Log2 *)
(** This base-2 logarithm is linear and tail-recursive.
Fixpoint log2_iter k p q r := match k with
| O => p
| S k' => match r with
| O => log2_iter k' (S p) (S q) q
| S r' => log2_iter k' p (S q) r' end end.
Definition log2 n := log2_iter (pred n) 010.
(** Iterator on natural numbers *)
Definition iter (n:nat) {A} (f:A->A) (x:A) : A :=
nat_rect (fun _ => A) x (fun _ => f) n.
(** Bitwise operations *)
(** We provide here some bitwise operations for unary numbers. Somemightbereallynaive,theyarejustthereforfulfilling thesameinterfaceasotherfornaturalrepresentations.As soonasbinaryrepresentationssuchasNArithareavailable, itisclearlybettertoconvertto/fromthemandusetheirops.
*)
Fixpoint div2 n := match n with
| 0 => 0
| S 0 => 0
| S (S n') => S (div2 n') end.
Fixpoint testbit a n : bool := match n with
| 0 => odd a
| S n => testbit (div2 a) n end.
Definition shiftl a := nat_rect _ a (fun _ => double). Definition shiftr a := nat_rect _ a (fun _ => div2).
Fixpoint bitwise (op:bool->bool->bool) n a b := match n with
| 0 => 0
| S n' =>
(if op (odd a) (odd b) then1else0) + 2*(bitwise op n' (div2 a) (div2 b)) end.
Definition land a b := bitwise andb a a b. Definition lor a b := bitwise orb (max a b) a b. Definition ldiff a b := bitwise (fun b b' => andb b (negb b')) a a b. Definition lxor a b := bitwise xorb (max a b) a b.
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.13 Sekunden
(vorverarbeitet am 2026-06-10)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.