CVS difference for ai12s/ai12-0208-1.txt

Differences between 1.5 and version 1.6
Log of other versions for file ai12s/ai12-0208-1.txt

--- ai12s/ai12-0208-1.txt	2018/02/27 07:03:53	1.5
+++ ai12s/ai12-0208-1.txt	2018/03/02 03:50:56	1.6
@@ -1,4 +1,4 @@
-!standard A.20(0)                                   16-12-19  AI12-0208-1/00
+!standard A.20(0)                                   18-03-01  AI12-0208-1/01
 !class Amendment 16-12-19
 !status work item 16-12-19
 !status received 16-09-27
@@ -26,9 +26,261 @@
 
 !wording
 
-** TBD **.
+package Big_Numbers is
+   type Number is interface;
 
+   function "=" (L, R : Number) return Boolean is abstract;
+   function "<" (L, R : Number) return Boolean is abstract;
+   function "<=" (L, R : Number) return Boolean is abstract;
+   function ">" (L, R : Number) return Boolean is abstract;
+   function ">=" (L, R : Number) return Boolean is abstract;
+
+   function To_String (Arg : Number) return String is abstract;
+   function From_String (Arg : String) return Number is abstract;
+   function "+" (Arg : Integer) return Number is abstract;
+
+   function "-" (L : Number) return Number is abstract;
+   function "abs" (L : Number) return Number is abstract;
+   function "+" (L, R : Number) return Number is abstract;
+   function "-" (L, R : Number) return Number is abstract;
+   function "*" (L, R : Number) return Number is abstract;
+   function "/" (L, R : Number) return Number is abstract;
+   function Min (L, R : Number) return Number is abstract;
+   function Max (L, R : Number) return Number is abstract;
+end Big_Numbers;
+
+----------------------------------------------------------------
+
+package Big_Numbers.Big_Integers is
+   type Big_Integer is new Number with private;
+   -- Defaults to zero. It would be nice to state this other than in a
+   -- comment but we don't have SPARK's Default_Initial_Condition aspect.
+
+   overriding function "=" (L, R : Big_Integer) return Boolean;
+   overriding function "<" (L, R : Big_Integer) return Boolean;
+   overriding function "<=" (L, R : Big_Integer) return Boolean;
+   overriding function ">" (L, R : Big_Integer) return Boolean;
+   overriding function ">=" (L, R : Big_Integer) return Boolean;
+
+   overriding function "+" (Arg : Integer) return Big_Integer;
+   function To_Big_Integer (Arg : Integer) return Big_Integer renames "+";
+
+   subtype Big_Positive is Big_Integer with
+        Dynamic_Predicate => Big_Positive > +0,
+        Predicate_Failure => (raise Constraint_Error);
+
+   subtype Big_Natural is Big_Integer with
+        Dynamic_Predicate => Big_Natural >= +0,
+        Predicate_Failure => (raise Constraint_Error);
+
+   function In_Range (Arg, Lo, Hi : Big_Integer) return Boolean is
+     ((Lo <= Arg) and (Arg <= Hi));
+
+   function To_Integer (Arg : Big_Integer) return Integer with
+      Pre => In_Range (Arg, Lo => +Integer'First, Hi => +Integer'Last)
+      or else (raise Constraint_Error);
+
+   generic
+      type Int is range <>;
+   package Signed_Conversions is
+      function To_Big_Integer (Arg : Int) return Big_Integer;
+      function From_Big_Integer (Arg : Big_Integer) return Int with
+         Pre => In_Range
+           (Arg, Lo => To_Big_Integer (Int'First),
+            Hi      => To_Big_Integer (Int'Last))
+         or else (raise Constraint_Error);
+   end Signed_Conversions;
+
+   generic
+      type Int is mod <>;
+   package Unsigned_Conversions is
+      function To_Big_Integer (Arg : Int) return Big_Integer;
+      function From_Big_Integer (Arg : Big_Integer) return Int with
+         Pre => In_Range
+           (Arg, Lo => To_Big_Integer (Int'First),
+            Hi      => To_Big_Integer (Int'Last))
+         or else (raise Constraint_Error);
+   end Unsigned_Conversions;
+
+   overriding function To_String (Arg : Big_Integer) return String;
+   overriding function From_String (Arg : String) return Big_Integer;
+   -- Accepts same syntax as Integer'Value (although accepts more digits);
+   -- progagates Constraint_Error if input doesn't meet this requirement.
+
+   overriding function "-" (L : Big_Integer) return Big_Integer;
+   overriding function "abs" (L : Big_Integer) return Big_Integer;
+   overriding function "+" (L, R : Big_Integer) return Big_Integer;
+   overriding function "-" (L, R : Big_Integer) return Big_Integer;
+   overriding function "*" (L, R : Big_Integer) return Big_Integer;
+   overriding function "/" (L, R : Big_Integer) return Big_Integer;
+   function "mod" (L, R : Big_Integer) return Big_Integer;
+   function "rem" (L, R : Big_Integer) return Big_Integer;
+   function "**" (L : Big_Integer; R : Natural) return Big_Integer;
+   overriding function Min (L, R : Big_Integer) return Big_Integer;
+   overriding function Max (L, R : Big_Integer) return Big_Integer;
 
+private
+   type Big_Integer is new Number with null record; -- bogus completion
+end Big_Numbers.Big_Integers;
+
+----------------------------------------------------------------
+
+generic
+   Capacity : Natural;
+package Big_Numbers.Bounded_Big_Integers is
+   -- Unlike the expected implementation of Big_Numbers.Big_Integers,
+   -- the expected implementation of (an instance of) Bounded_Big_Integers
+   -- is not expected to require use of allocators or controlled types.
+   -- The only difference between the specs of the bounded and unbounded
+   -- versions is the addition of the First and Last functions.
+
+   type Big_Integer is new Number with private;
+   -- Defaults to zero.
+   --
+   -- Type_Invariant for Bounded_Big_Integer is effectively
+   --   In_Range (Bounded_Big_Integer, First, Last)
+   --   or else (raise Constraint_Error)
+   -- although we don't specify that here in visible part because
+   -- the operators called in the type invariant itself have
+   -- to be special in order to avoid infinite recursion.
+   -- Note that First and Last are defined below.
+
+   overriding function "=" (L, R : Big_Integer) return Boolean;
+   overriding function "<" (L, R : Big_Integer) return Boolean;
+   overriding function "<=" (L, R : Big_Integer) return Boolean;
+   overriding function ">" (L, R : Big_Integer) return Boolean;
+   overriding function ">=" (L, R : Big_Integer) return Boolean;
+
+   overriding function "+" (Arg : Integer) return Big_Integer;
+   function To_Big_Integer (Arg : Integer) return Big_Integer renames "+";
+   subtype Big_Positive is Big_Integer with
+        Dynamic_Predicate => Big_Positive > +0,
+        Predicate_Failure => (raise Constraint_Error);
+
+   subtype Big_Natural is Big_Integer with
+        Dynamic_Predicate => Big_Natural >= +0,
+        Predicate_Failure => (raise Constraint_Error);
+
+   function In_Range (Arg, Lo, Hi : Big_Integer) return Boolean is
+     ((Lo <= Arg) and (Arg <= Hi));
+
+   function To_Integer (Arg : Big_Integer) return Integer with
+      Pre => In_Range (Arg, Lo => +Integer'First, Hi => +Integer'Last)
+      or else (raise Constraint_Error);
+
+   generic
+      type Int is range <>;
+   package Signed_Conversions is
+      function To_Big_Integer (Arg : Int) return Big_Integer;
+      function From_Big_Integer (Arg : Big_Integer) return Int with
+         Pre => In_Range
+           (Arg, Lo => To_Big_Integer (Int'First),
+            Hi      => To_Big_Integer (Int'Last))
+         or else (raise Constraint_Error);
+   end Signed_Conversions;
+
+   generic
+      type Int is mod <>;
+   package Unsigned_Conversions is
+      function To_Big_Integer (Arg : Int) return Big_Integer;
+      function From_Big_Integer (Arg : Big_Integer) return Int with
+         Pre => In_Range
+           (Arg, Lo => To_Big_Integer (Int'First),
+            Hi      => To_Big_Integer (Int'Last))
+         or else (raise Constraint_Error);
+   end Unsigned_Conversions;
+
+   overriding function To_String (Arg : Big_Integer) return String;
+   overriding function From_String (Arg : String) return Big_Integer;
+   -- Accepts same syntax as Integer'Value (although accepts more digits);
+   -- progagates Constraint_Error if input doesn't meet this requirement.
+
+   overriding function "-" (L : Big_Integer) return Big_Integer;
+   overriding function "abs" (L : Big_Integer) return Big_Integer;
+   overriding function "+" (L, R : Big_Integer) return Big_Integer;
+   overriding function "-" (L, R : Big_Integer) return Big_Integer;
+   overriding function "*" (L, R : Big_Integer) return Big_Integer;
+   overriding function "/" (L, R : Big_Integer) return Big_Integer;
+   function "mod" (L, R : Big_Integer) return Big_Integer;
+   function "rem" (L, R : Big_Integer) return Big_Integer;
+   function "**" (L : Big_Integer; R : Natural) return Big_Integer;
+   overriding function Min (L, R : Big_Integer) return Big_Integer;
+   overriding function Max (L, R : Big_Integer) return Big_Integer;
+
+   function Last return Big_Integer is ((+256)**Capacity);
+   function First return Big_Integer is (-Last);
+private
+   type Big_Integer is new Number with null record; -- bogus completion
+end Big_Numbers.Bounded_Big_Integers;
+
+----------------------------------------------------------------
+
+with Big_Numbers.Big_Integers;
+generic
+package Big_Numbers.Bounded_Big_Integers.Conversions is
+   -- This unit is a child unit because we don't want
+   -- Big_Integers to be in the closure of Bounded_Big_Integers.
+
+   function From_Unbounded (Arg : Big_Integers.Big_Integer) return Big_Integer;
+   function To_Unbounded (Arg : Big_Integer) return Big_Integers.Big_Integer;
+end Big_Numbers.Bounded_Big_Integers.Conversions;
+
+----------------------------------------------------------------
+
+with Big_Numbers.Big_Integers;
+package Big_Numbers.Big_Rationals is
+   use Big_Integers;
+
+   type Big_Rational is new Number with private;
+   -- Defaults to zero.
+   --
+   -- Type_Invariant for Big_Rational is effectively
+   --   (Big_Rational = +0) or else
+   --   (GCD (abs Numerator (Big_Rational),
+   --         Denominator (Big_Rational)) = +1) or else
+   --   (raise Constraint_Error)
+   -- although we don't specify that here in visible part because
+   -- the operators called in the type invariant itself have
+   -- to be special in order to avoid infinite recursion.
+
+   function "/" (Num, Den : Big_Integer) return Big_Rational with
+      Pre => (Den /= +0) or else (raise Constraint_Error);
+
+   function Numerator (Arg : Big_Rational) return Big_Integer;
+   function Denominator (Arg : Big_Rational) return Big_Positive;
+
+   overriding function "+" (Arg : Integer) return Big_Rational is
+     ((+Arg) / Big_Integer'(+1));
+
+   function To_Big_Rational (Arg : Integer) return Big_Rational renames "+";
+
+   overriding function "=" (L, R : Big_Rational) return Boolean;
+   overriding function "<" (L, R : Big_Rational) return Boolean;
+   overriding function "<=" (L, R : Big_Rational) return Boolean;
+   overriding function ">" (L, R : Big_Rational) return Boolean;
+   overriding function ">=" (L, R : Big_Rational) return Boolean;
+
+   overriding function To_String (Arg : Big_Rational) return String is
+     (To_String (Numerator (Arg)) & " /" & To_String (Denominator (Arg)));
+   overriding function From_String (Arg : String) return Big_Rational;
+   -- inverse of To_String; propagates Constraint_Error in error cases.
+
+   overriding function "-" (L : Big_Rational) return Big_Rational;
+   overriding function "abs" (L : Big_Rational) return Big_Rational;
+   overriding function "+" (L, R : Big_Rational) return Big_Rational;
+   overriding function "-" (L, R : Big_Rational) return Big_Rational;
+   overriding function "*" (L, R : Big_Rational) return Big_Rational;
+   overriding function "/" (L, R : Big_Rational) return Big_Rational;
+   function "**" (L : Big_Rational; R : Integer) return Big_Rational;
+   overriding function Min (L, R : Big_Rational) return Big_Rational;
+   overriding function Max (L, R : Big_Rational) return Big_Rational;
+private
+   type Big_Rational is new Number with null record; -- bogus completion
+end Big_Numbers.Big_Rationals;
+
+
+** Semantics rules TBD.
+
 !discussion
 
 !ASIS
@@ -2663,5 +2915,65 @@
 several non-favorite compilers as well).
 
 So don't look for the non-existent part 3.
+
+****************************************************************
+
+From: Steve Baird
+Sent: Wednesday, February 28, 2018  8:03 PM
+
+I'm attaching some preliminary specs that reflect the feedback from the last
+discussion of this set of predefined packages. [This is version /01 of the
+AI - ED.]
+
+There is still some polishing to do with respect to, for example, pre/post
+conditions for the various operations, but this should give us a more concrete
+framework for discussion.
+
+I gave up on making Bounded_Integer a discriminated type and instead defined
+the enclosing package Bounded_Integers as a generic package. That eliminates
+questions such as "what is the discriminant of the result of adding together
+two values whose discriminants differ?".
+
+It is intended that the bounded and unbounded Big_Integer types should be
+streaming-compatible as with Vectors and Bounded_Vectors
+
+As discussed last meeting, there is no Bounded_Big_Rationals package (or
+generic package).
+
+The types are tagged now and descended from a common interface type.
+This means that most parameter subtypes have to be first named subtypes.
+This should not be considered an endorsement of this idea; we might (as Bob 
+suggests) want these types to be untagged. It's just easier to see what this
+looks like and then have to imagine what the untagged version might be than
+the other way around. I agree with Randy's comments that taggedness by itself
+doesn't imply performance problems.
+
+Currently no declaration of a GCD function. Do we want that? If so, then in
+which package? If we declare it in Big_Reals then it is not a primitive and so
+parameters can be Big_Positives instead of Big_Integers. If we really want it
+in Big_Integers package(s) then it would either need Big_Integer parameter
+subtypes or it would need to be declared in a nested package to avoid
+primitive status.
+
+Comments?
+
+****************************************************************
+
+From: Randy Brukardt
+Sent: Thursday, March  1, 2018  9:49 PM
+
+> I'm attaching some preliminary specs that reflect the feedback from 
+> the last discussion of this set of predefined packages.
+...
+> Comments?
+
+Shouldn't this be some set of children of Ada.Numerics? These kinda seem like
+numbers. ;-)
+
+You don't have the numeric literal definitions (see AI12-0249-1) -- that seems
+necessary for usability. (One wonders if the literals should be defined on the
+interface, which suggests that AI12-0249-1 needs a bit of extension.)
+
+Otherwise, I didn't notice anything that I would change.
 
 ****************************************************************

Questions? Ask the ACAA Technical Agent