| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT RUN-TIME COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- ADA.NUMERICS.BIG_NUMBERS.BIG_INTEGERS_GHOST -- |
| 6 |
-- -- |
| 7 |
-- S p e c -- |
| 8 |
-- -- |
| 9 |
-- This specification is derived from the Ada Reference Manual for use with -- |
| 10 |
-- GNAT. In accordance with the copyright of that document, you can freely -- |
| 11 |
-- copy and modify this specification, provided that if you redistribute a -- |
| 12 |
-- modified version, any changes that you have made are clearly indicated. -- |
| 13 |
-- -- |
| 14 |
------------------------------------------------------------------------------ |
| 15 |
|
| 16 |
-- This package provides a reduced and non-executable implementation of the |
| 17 |
-- ARM A.5.6 defined ``Ada.Numerics.Big_Numbers.Big_Integers`` for use in |
| 18 |
-- SPARK proofs in the runtime. As it is only intended for SPARK proofs, this |
| 19 |
-- package is marked as a Ghost package and consequently does not have a |
| 20 |
-- runtime footprint. |
| 21 |
|
| 22 |
-- Contrary to Ada.Numerics.Big_Numbers.Big_Integers, this unit does not |
| 23 |
-- depend on System or Ada.Finalization, which makes it more convenient for |
| 24 |
-- use in run-time units. Note, since it is a ghost unit, all subprograms are |
| 25 |
-- marked as imported. |
| 26 |
|
| 27 |
-- Ghost code in this unit is meant for analysis only, not for run-time |
| 28 |
-- checking. This is enforced by setting the assertion policy to Ignore. |
| 29 |
|
| 30 |
pragma Assertion_Policy (Ghost => Ignore); |
| 31 |
|
| 32 |
package Ada.Numerics.Big_Numbers.Big_Integers_Ghost with |
| 33 |
SPARK_Mode, |
| 34 |
Ghost, |
| 35 |
Pure, |
| 36 |
Always_Terminates |
| 37 |
is |
| 38 |
|
| 39 |
type Big_Integer is private |
| 40 |
with Integer_Literal => From_Universal_Image; |
| 41 |
-- Private type that holds the integer value |
| 42 |
|
| 43 |
function Is_Valid (Arg : Big_Integer) return Boolean |
| 44 |
with |
| 45 |
Import, |
| 46 |
Global => null; |
| 47 |
-- Return whether a passed big integer is valid |
| 48 |
|
| 49 |
subtype Valid_Big_Integer is Big_Integer |
| 50 |
with Dynamic_Predicate => Is_Valid (Valid_Big_Integer), |
| 51 |
Predicate_Failure => raise Program_Error; |
| 52 |
-- Holds a valid Big_Integer |
| 53 |
|
| 54 |
-- Comparison operators defined for valid Big_Integer values |
| 55 |
function "=" (L, R : Valid_Big_Integer) return Boolean with |
| 56 |
Import, |
| 57 |
Global => null; |
| 58 |
|
| 59 |
function "<" (L, R : Valid_Big_Integer) return Boolean with |
| 60 |
Import, |
| 61 |
Global => null; |
| 62 |
|
| 63 |
function "<=" (L, R : Valid_Big_Integer) return Boolean with |
| 64 |
Import, |
| 65 |
Global => null; |
| 66 |
|
| 67 |
function ">" (L, R : Valid_Big_Integer) return Boolean with |
| 68 |
Import, |
| 69 |
Global => null; |
| 70 |
|
| 71 |
function ">=" (L, R : Valid_Big_Integer) return Boolean with |
| 72 |
Import, |
| 73 |
Global => null; |
| 74 |
|
| 75 |
function To_Big_Integer (Arg : Integer) return Valid_Big_Integer |
| 76 |
with |
| 77 |
Import, |
| 78 |
Global => null; |
| 79 |
-- Create a Big_Integer from an Integer value |
| 80 |
|
| 81 |
subtype Big_Positive is Big_Integer |
| 82 |
with Dynamic_Predicate => |
| 83 |
(if Is_Valid (Big_Positive) |
| 84 |
then Big_Positive > To_Big_Integer (0)), |
| 85 |
Predicate_Failure => raise Constraint_Error; |
| 86 |
-- Positive subtype of Big_Integers, analogous to Positive and Integer |
| 87 |
|
| 88 |
subtype Big_Natural is Big_Integer |
| 89 |
with Dynamic_Predicate => |
| 90 |
(if Is_Valid (Big_Natural) |
| 91 |
then Big_Natural >= To_Big_Integer (0)), |
| 92 |
Predicate_Failure => raise Constraint_Error; |
| 93 |
-- Natural subtype of Big_Integers, analogous to Natural and Integer |
| 94 |
|
| 95 |
function In_Range |
| 96 |
(Arg : Valid_Big_Integer; Low, High : Big_Integer) return Boolean |
| 97 |
is (Low <= Arg and Arg <= High) |
| 98 |
with |
| 99 |
Import, |
| 100 |
Global => null; |
| 101 |
-- Check whether Arg is in the range Low .. High |
| 102 |
|
| 103 |
function To_Integer (Arg : Valid_Big_Integer) return Integer |
| 104 |
with |
| 105 |
Import, |
| 106 |
Pre => In_Range (Arg, |
| 107 |
Low => To_Big_Integer (Integer'First), |
| 108 |
High => To_Big_Integer (Integer'Last)) |
| 109 |
or else raise Constraint_Error, |
| 110 |
Global => null; |
| 111 |
-- Convert a valid Big_Integer into an Integer |
| 112 |
|
| 113 |
generic |
| 114 |
type Int is range <>; |
| 115 |
package Signed_Conversions is |
| 116 |
-- Generic package to implement conversion functions for |
| 117 |
-- arbitrary ranged types. |
| 118 |
|
| 119 |
function To_Big_Integer (Arg : Int) return Valid_Big_Integer |
| 120 |
with |
| 121 |
Global => null; |
| 122 |
-- Convert a ranged type into a valid Big_Integer |
| 123 |
|
| 124 |
function From_Big_Integer (Arg : Valid_Big_Integer) return Int |
| 125 |
with |
| 126 |
Pre => In_Range (Arg, |
| 127 |
Low => To_Big_Integer (Int'First), |
| 128 |
High => To_Big_Integer (Int'Last)) |
| 129 |
or else raise Constraint_Error, |
| 130 |
Global => null; |
| 131 |
-- Convert a valid Big_Integer into a ranged type |
| 132 |
end Signed_Conversions; |
| 133 |
|
| 134 |
generic |
| 135 |
type Int is mod <>; |
| 136 |
package Unsigned_Conversions is |
| 137 |
-- Generic package to implement conversion functions for |
| 138 |
-- arbitrary modular types. |
| 139 |
|
| 140 |
function To_Big_Integer (Arg : Int) return Valid_Big_Integer |
| 141 |
with |
| 142 |
Global => null; |
| 143 |
-- Convert a modular type into a valid Big_Integer |
| 144 |
|
| 145 |
function From_Big_Integer (Arg : Valid_Big_Integer) return Int |
| 146 |
with |
| 147 |
Pre => In_Range (Arg, |
| 148 |
Low => To_Big_Integer (Int'First), |
| 149 |
High => To_Big_Integer (Int'Last)) |
| 150 |
or else raise Constraint_Error, |
| 151 |
Global => null; |
| 152 |
-- Convert a valid Big_Integer into a modular type |
| 153 |
|
| 154 |
end Unsigned_Conversions; |
| 155 |
|
| 156 |
function From_String (Arg : String) return Valid_Big_Integer |
| 157 |
with |
| 158 |
Import, |
| 159 |
Global => null; |
| 160 |
-- Create a valid Big_Integer from a String |
| 161 |
|
| 162 |
function From_Universal_Image (Arg : String) return Valid_Big_Integer |
| 163 |
renames From_String; |
| 164 |
|
| 165 |
-- Mathematical operators defined for valid Big_Integer values |
| 166 |
function "+" (L : Valid_Big_Integer) return Valid_Big_Integer |
| 167 |
with |
| 168 |
Import, |
| 169 |
Global => null; |
| 170 |
|
| 171 |
function "-" (L : Valid_Big_Integer) return Valid_Big_Integer |
| 172 |
with |
| 173 |
Import, |
| 174 |
Global => null; |
| 175 |
|
| 176 |
function "abs" (L : Valid_Big_Integer) return Valid_Big_Integer |
| 177 |
with |
| 178 |
Import, |
| 179 |
Global => null; |
| 180 |
|
| 181 |
function "+" (L, R : Valid_Big_Integer) return Valid_Big_Integer |
| 182 |
with |
| 183 |
Import, |
| 184 |
Global => null; |
| 185 |
|
| 186 |
function "-" (L, R : Valid_Big_Integer) return Valid_Big_Integer |
| 187 |
with |
| 188 |
Import, |
| 189 |
Global => null; |
| 190 |
|
| 191 |
function "*" (L, R : Valid_Big_Integer) return Valid_Big_Integer |
| 192 |
with |
| 193 |
Import, |
| 194 |
Global => null; |
| 195 |
|
| 196 |
function "/" (L, R : Valid_Big_Integer) return Valid_Big_Integer |
| 197 |
with |
| 198 |
Import, |
| 199 |
Global => null; |
| 200 |
|
| 201 |
function "mod" (L, R : Valid_Big_Integer) return Valid_Big_Integer |
| 202 |
with |
| 203 |
Import, |
| 204 |
Global => null; |
| 205 |
|
| 206 |
function "rem" (L, R : Valid_Big_Integer) return Valid_Big_Integer |
| 207 |
with |
| 208 |
Import, |
| 209 |
Global => null; |
| 210 |
|
| 211 |
function "**" (L : Valid_Big_Integer; R : Natural) return Valid_Big_Integer |
| 212 |
with |
| 213 |
Import, |
| 214 |
Global => null; |
| 215 |
|
| 216 |
function Min (L, R : Valid_Big_Integer) return Valid_Big_Integer |
| 217 |
with |
| 218 |
Import, |
| 219 |
Global => null; |
| 220 |
|
| 221 |
function Max (L, R : Valid_Big_Integer) return Valid_Big_Integer |
| 222 |
with |
| 223 |
Import, |
| 224 |
Global => null; |
| 225 |
|
| 226 |
function Greatest_Common_Divisor |
| 227 |
(L, R : Valid_Big_Integer) return Big_Positive |
| 228 |
with |
| 229 |
Import, |
| 230 |
Pre => (L /= To_Big_Integer (0) and R /= To_Big_Integer (0)) |
| 231 |
or else raise Constraint_Error, |
| 232 |
Global => null; |
| 233 |
-- Calculate the greatest common divisor for two Big_Integer values |
| 234 |
|
| 235 |
private |
| 236 |
pragma SPARK_Mode (Off); |
| 237 |
|
| 238 |
type Big_Integer is null record; |
| 239 |
-- Solely consists of Ghost code |
| 240 |
|
| 241 |
end Ada.Numerics.Big_Numbers.Big_Integers_Ghost; |