numeric-bases.rst 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. .. SPDX-License-Identifier: GPL-2.0 or GPL-3.0
  2. .. Copyright © 2019 Ariadne Devos
  3. Parsing base-N numbers
  4. ======================
  5. For now, only ASCII decimal is supported,
  6. Some logical constructs are defined here,
  7. see <sHT/lex/nat.h> for the C API.
  8. decimal := [0-9]
  9. The set of ASCII decimal digits
  10. decimal->nat (c : decimal) : { k : nat | 0 ≤ k < 10 } := c - ascii->nat '0'
  11. Convert an ASCII decimal digit into its numeric value.
  12. (In ASCII, '0', '1' .. '9' are contiguous, mapping to 0 .. 9)
  13. string->nat (length : nat) (d : { k : nat | 0 ≤ k < length } → { k : nat | 0 ≤ k < 10 }) → nat := sum (k <- 0 \to length) (d(k) * exp(10, length - k - 1))
  14. Convert a decimal ASCII string `d` into its numeric value,
  15. leading zeroes are allowed
  16. Theorem digit_minus_zero_k (k : { k : ℕ | 0 ⩽ k ⩽ 9 }) (a : uint8_t) : a - '0' ⩽ k ↔ '0' ⩽ a ⩽ '0' + k
  17. Proof.
  18. ⇔ 0 ⩽ a - '0' ⩽ k ↔ '0' ⩽ a ⩽ '0' + k
  19. ⇔ (retype a : ℕ) 0 ⩽ wrap(uint8_t, a - '0') ⩽ k ↔ '0' ⩽ a ⩽ '0' + k
  20. ⇔ 0 + '0' ⩽ wrap(uint8_t, a - '0') + '0' ⩽ k + '0' ↔ '0' ⩽ a ⩽ '0' + k
  21. ⇔ 0 + '0' ⩽ wrap(uint8_t, a - '0' + '0') + '0' ⩽ k + '0' ↔ '0' ⩽ a ⩽ '0' + k
  22. ⇔ '0' ⩽ wrap(uint8_t, a) ⩽ '0' + k ↔ '0' ⩽ a ⩽ '0' + k (eval, reorder)
  23. ⇔ '0' ⩽ a ⩽ '0' + k ↔ '0' ⩽ a ⩽ '0' + k
  24. ⇔ ⊤
  25. Qed.
  26. Theorem digit_minus_zero (a : uint8_t) := digit_minus_zero_k 9.