Library HoTT.Basics.Numerals.Hexadecimal

(************************************************************************)
(*         *   The Coq Proof Assistant / The Coq 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)         *)
(************************************************************************)

(************************************************************************)
(* This file has been modified for use in the HoTT library              *)
(************************************************************************)

Require Import Basics.Overture Basics.Numerals.Decimal.

Hexadecimal numbers

These numbers coded in base 16 will be used for parsing and printing other Coq numeral datatypes in an human-readable way. See the Numeral Notation command. We represent numbers in base 16 as lists of hexadecimal digits, in big-endian order (most significant digit comes first).
Unsigned integers are just lists of digits. For instance, sixteen is (D1 (D0 Nil))

Inductive uint : Type0 :=
 | Nil
 | D0 (_:uint)
 | D1 (_:uint)
 | D2 (_:uint)
 | D3 (_:uint)
 | D4 (_:uint)
 | D5 (_:uint)
 | D6 (_:uint)
 | D7 (_:uint)
 | D8 (_:uint)
 | D9 (_:uint)
 | Da (_:uint)
 | Db (_:uint)
 | Dc (_:uint)
 | Dd (_:uint)
 | De (_:uint)
 | Df (_:uint).

Nil is the number terminator. Taken alone, it behaves as zero, but rather use D0 Nil instead, since this form will be denoted as 0, while Nil will be printed as Nil.

Notation zero := (D0 Nil).

For signed integers, we use two constructors Pos and Neg.

Variant int : Type0 := Pos (d:uint) | Neg (d:uint).

For decimal numbers, we use two constructors Hexadecimal and HexadecimalExp, depending on whether or not they are given with an exponent (e.g., 0x1.a2p+01). i is the integral part while f is the fractional part (beware that leading zeroes do matter).

Variant hexadecimal : Type0 :=
 | Hexadecimal (i:int) (f:uint)
 | HexadecimalExp (i:int) (f:uint) (e:Decimal.int).

Declare Scope hex_uint_scope.
Delimit Scope hex_uint_scope with huint.
Bind Scope hex_uint_scope with uint.

Declare Scope hex_int_scope.
Delimit Scope hex_int_scope with hint.
Bind Scope hex_int_scope with int.

Register uint as num.hexadecimal_uint.type.
Register int as num.hexadecimal_int.type.
Register hexadecimal as num.hexadecimal.type.

Fixpoint nb_digits d :=
  match d with
  | NilO
  | D0 d | D1 d | D2 d | D3 d | D4 d | D5 d | D6 d | D7 d | D8 d | D9 d
  | Da d | Db d | Dc d | Dd d | De d | Df d
    S (nb_digits d)
  end.

This representation favors simplicity over canonicity. For normalizing numbers, we need to remove head zero digits, and choose our canonical representation of 0 (here D0 Nil for unsigned numbers and Pos (D0 Nil) for signed numbers).
nzhead removes all head zero digits

Fixpoint nzhead d :=
  match d with
  | D0 dnzhead d
  | _d
  end.

unorm : normalization of unsigned integers

Definition unorm d :=
  match nzhead d with
  | Nilzero
  | dd
  end.

norm : normalization of signed integers

Definition norm d :=
  match d with
  | Pos dPos (unorm d)
  | Neg d
    match nzhead d with
    | NilPos zero
    | dNeg d
    end
  end.

A few easy operations. For more advanced computations, use the conversions with other Coq numeral datatypes (e.g. Z) and the operations on them.

Definition opp (d:int) :=
  match d with
  | Pos dNeg d
  | Neg dPos d
  end.

For conversions with binary numbers, it is easier to operate on little-endian numbers.

Fixpoint revapp (d d' : uint) :=
  match d with
  | Nild'
  | D0 drevapp d (D0 d')
  | D1 drevapp d (D1 d')
  | D2 drevapp d (D2 d')
  | D3 drevapp d (D3 d')
  | D4 drevapp d (D4 d')
  | D5 drevapp d (D5 d')
  | D6 drevapp d (D6 d')
  | D7 drevapp d (D7 d')
  | D8 drevapp d (D8 d')
  | D9 drevapp d (D9 d')
  | Da drevapp d (Da d')
  | Db drevapp d (Db d')
  | Dc drevapp d (Dc d')
  | Dd drevapp d (Dd d')
  | De drevapp d (De d')
  | Df drevapp d (Df d')
  end.

Definition rev d := revapp d Nil.

Definition app d d' := revapp (rev d) d'.

Definition app_int d1 d2 :=
  match d1 with Pos d1Pos (app d1 d2) | Neg d1Neg (app d1 d2) end.

nztail removes all trailing zero digits and return both the result and the number of removed digits.

Definition nztail d :=
  let fix aux d_rev :=
    match d_rev with
    | D0 d_revlet (r, n) := aux d_rev in pair r (S n)
    | _pair d_rev O
    end in
  let (r, n) := aux (rev d) in pair (rev r) n.

Definition nztail_int d :=
  match d with
  | Pos dlet (r, n) := nztail d in pair (Pos r) n
  | Neg dlet (r, n) := nztail d in pair (Neg r) n
  end.

Module Little.

Successor of little-endian numbers

Fixpoint succ d :=
  match d with
  | NilD1 Nil
  | D0 dD1 d
  | D1 dD2 d
  | D2 dD3 d
  | D3 dD4 d
  | D4 dD5 d
  | D5 dD6 d
  | D6 dD7 d
  | D7 dD8 d
  | D8 dD9 d
  | D9 dDa d
  | Da dDb d
  | Db dDc d
  | Dc dDd d
  | Dd dDe d
  | De dDf d
  | Df dD0 (succ d)
  end.

Doubling little-endian numbers

Fixpoint double d :=
  match d with
  | NilNil
  | D0 dD0 (double d)
  | D1 dD2 (double d)
  | D2 dD4 (double d)
  | D3 dD6 (double d)
  | D4 dD8 (double d)
  | D5 dDa (double d)
  | D6 dDc (double d)
  | D7 dDe (double d)
  | D8 dD0 (succ_double d)
  | D9 dD2 (succ_double d)
  | Da dD4 (succ_double d)
  | Db dD6 (succ_double d)
  | Dc dD8 (succ_double d)
  | Dd dDa (succ_double d)
  | De dDc (succ_double d)
  | Df dDe (succ_double d)
  end

with succ_double d :=
  match d with
  | NilD1 Nil
  | D0 dD1 (double d)
  | D1 dD3 (double d)
  | D2 dD5 (double d)
  | D3 dD7 (double d)
  | D4 dD9 (double d)
  | D5 dDb (double d)
  | D6 dDd (double d)
  | D7 dDf (double d)
  | D8 dD1 (succ_double d)
  | D9 dD3 (succ_double d)
  | Da dD5 (succ_double d)
  | Db dD7 (succ_double d)
  | Dc dD9 (succ_double d)
  | Dd dDb (succ_double d)
  | De dDd (succ_double d)
  | Df dDf (succ_double d)
  end.

End Little.