| 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 | (** Numbers in different forms: signed or unsigned, possibly with |
| 12 | fractional part and exponent. |
| 13 | |
| 14 | Numbers are represented using raw strings of (hexa)decimal |
| 15 | literals and a separate sign flag. |
| 16 | |
| 17 | Note that this representation is not unique, due to possible |
| 18 | multiple leading or trailing zeros, and -0 = +0, for instances. |
| 19 | The reason to keep the number exactly as it was parsed is that |
| 20 | specific notations can be declared for specific numbers |
| 21 | (e.g. [Notation "0" := False], or [Notation "00" := (nil,nil)], or |
| 22 | [Notation "2e1" := ...]). Those notations override the generic |
| 23 | interpretation as number. So, one has to record the form of the |
| 24 | number which exactly matches the notation. *) |
| 25 | |
| 26 | type sign = SPlus | SMinus |
| 27 | |
| 28 | type num_class = CDec | CHex |
| 29 | |
| 30 | type 'a exp = EDec of 'a | EBin of 'a |
| 31 | |
| 32 | (** {6 String representation of a natural number } *) |
| 33 | |
| 34 | module UnsignedNat : |
| 35 | sig |
| 36 | type t |
| 37 | val of_string : string -> t |
| 38 | (** Convert from a non-empty sequence of digits (which may contain "_") |
| 39 | (or hexdigits when starting with "0x" or "0X") *) |
| 40 | |
| 41 | val to_string : t -> string |
| 42 | (** Convert to a non-empty sequence of digit that does not contain "_" |
| 43 | (or hexdigits, starting with "0x", all hexdigits are lower case) *) |
| 44 | |
| 45 | val sprint : t -> string |
| 46 | val print : t -> Pp.t |
| 47 | (** [sprint] and [print] returns the number as it was parsed, for printing *) |
| 48 | |
| 49 | val classify : t -> num_class |
| 50 | |
| 51 | val compare : t -> t -> int |
| 52 | end |
| 53 | |
| 54 | (** {6 String representation of a signed natural number } *) |
| 55 | |
| 56 | module SignedNat : |
| 57 | sig |
| 58 | type t = sign * UnsignedNat.t |
| 59 | val of_string : string -> t |
| 60 | (** Convert from a non-empty sequence of (hex)digits which may contain "_" *) |
| 61 | |
| 62 | val to_string : t -> string |
| 63 | (** Convert to a non-empty sequence of (hex)digit that does not contain "_" |
| 64 | (hexadecimals start with "0x" and all hexdigits are lower case) *) |
| 65 | |
| 66 | val classify : t -> num_class |
| 67 | |
| 68 | val of_bigint : num_class -> Z.t -> t |
| 69 | val to_bigint : t -> Z.t |
| 70 | end |
| 71 | |
| 72 | (** {6 Unsigned decimal numbers } *) |
| 73 | |
| 74 | module Unsigned : |
| 75 | sig |
| 76 | type t |
| 77 | val equal : t -> t -> bool |
| 78 | val is_nat : t -> bool |
| 79 | val to_nat : t -> string option |
| 80 | |
| 81 | val sprint : t -> string |
| 82 | val print : t -> Pp.t |
| 83 | (** [sprint] and [print] returns the number as it was parsed, for printing *) |
| 84 | |
| 85 | val parse : (unit,char) Gramlib.Stream.t -> t |
| 86 | (** Parse a positive Rocq number. |
| 87 | Precondition: the first char on the stream is already known to be a digit (\[0-9\]). |
| 88 | Precondition: at least two extra chars after the number to parse. |
| 89 | |
| 90 | The recognized syntax is: |
| 91 | - integer part: \[0-9\]\[0-9_\]* |
| 92 | - fractional part: empty or .\[0-9_\]+ |
| 93 | - exponent part: empty or \[eE\]\[+-\]?\[0-9\]\[0-9_\]* |
| 94 | or |
| 95 | - integer part: 0\[xX\]\[0-9a-fA-F\]\[0-9a-fA-F_\]* |
| 96 | - fractional part: empty or .\[0-9a-fA-F_\]+ |
| 97 | - exponent part: empty or \[pP\]\[+-\]?\[0-9\]\[0-9_\]* *) |
| 98 | |
| 99 | val parse_string : string -> t option |
| 100 | (** Parse the string as a non negative Rocq number, if possible *) |
| 101 | |
| 102 | val classify : t -> num_class |
| 103 | |
| 104 | end |
| 105 | |
| 106 | (** {6 Signed decimal numbers } *) |
| 107 | |
| 108 | module Signed : |
| 109 | sig |
| 110 | type t = sign * Unsigned.t |
| 111 | val equal : t -> t -> bool |
| 112 | val is_zero : t -> bool |
| 113 | val of_nat : UnsignedNat.t -> t |
| 114 | val of_int : SignedNat.t -> t |
| 115 | val to_int : t -> SignedNat.t option |
| 116 | val is_int : t -> bool |
| 117 | |
| 118 | val sprint : t -> string |
| 119 | val print : t -> Pp.t |
| 120 | (** [sprint] and [print] returns the number as it was parsed, for printing *) |
| 121 | |
| 122 | val parse_string : string -> t option |
| 123 | (** Parse the string as a signed Rocq number, if possible *) |
| 124 | |
| 125 | val of_int_string : string -> t |
| 126 | (** Convert from a string in the syntax of OCaml's int/int64 *) |
| 127 | |
| 128 | val of_string : string -> t |
| 129 | (** Convert from a string in the syntax of OCaml's string_of_float *) |
| 130 | |
| 131 | val to_string : t -> string |
| 132 | (** Returns a string in the syntax of OCaml's float_of_string *) |
| 133 | |
| 134 | val of_bigint : num_class -> Z.t -> t |
| 135 | val to_bigint : t -> Z.t option |
| 136 | (** Convert from and to bigint when the denotation of a bigint *) |
| 137 | |
| 138 | val of_int_frac_and_exponent : SignedNat.t -> UnsignedNat.t option -> SignedNat.t option -> t |
| 139 | val to_int_frac_and_exponent : t -> SignedNat.t * UnsignedNat.t option * SignedNat.t option |
| 140 | (** n, p and q such that the number is n.p*10^q or n.p*2^q |
| 141 | pre/postcondition: classify n = classify p, classify q = CDec *) |
| 142 | |
| 143 | val of_bigint_and_exponent : Z.t -> Z.t exp -> t |
| 144 | val to_bigint_and_exponent : t -> Z.t * Z.t exp |
| 145 | (** n and p such that the number is n*10^p or n*2^p *) |
| 146 | |
| 147 | val classify : t -> num_class |
| 148 | |
| 149 | val is_bigger_int_than : t -> UnsignedNat.t -> bool |
| 150 | (** Test if an integer whose absolute value is bounded *) |
| 151 | |
| 152 | end |