--- ai05s/ai05-0213-1.txt 2011/05/12 22:21:37 1.5 +++ ai05s/ai05-0213-1.txt 2011/05/14 06:03:29 1.6 @@ -1,4 +1,4 @@ -!standard 3.10.1(8/2) 11-05-11 AI05-0213-1/02 +!standard 3.10.1(8/2) 11-05-14 AI05-0213-1/03 !standard 12.5(2) !standard 12.5.1(1/2) !standard 12.5.1(6) @@ -21,62 +21,12 @@ !problem -Ada 95 provides formal package parameters. One way of using these parameters -is to define a "signature" for a class of abstractions, such as all set -abstractions, or all physical unit abstractions, and then build a new -generic abstraction on top of the original class of abstractions using this -signature as the template for a formal package parameter. - -Unfortunately, it is difficult to use signatures because of the fact that an -instantiation freezes all of its actual parameters. - -For example: - -Given the signature for a set abstraction: - -generic - type Element is private; - type Set is private; - with function Size(Of_Set : Set) return Natural is <>; - with function Union(Left, Right : Set) return Set is <>; - with function Intersection(Left, Right : Set) return Set is <>; - with function Empty return Set is <>; - with function Unit_Set(With_Item : Element) return Set is <>; - with function Nth_Element(Of_Set : Set) return Element is <>; -package Set_Signature is end; -... - -we could define a generic that required some set abstraction, but it -didn't care which one so long as it implemented the above signature: - -generic - with package Base_Set is new Set_Signature(<>); -package Layered_Abstraction is - type Cool_Type(Set : access Base_Set.Set) is limited_private; - procedure Massage(CT : in out Cool_Type; New_Set : Base_Set.Set); - ... -end Layered_Abstraction; - -Now if we want to define a set type and provide the pre-instantiated -signature for it, we run into trouble: - -generic - type Elem is private; - with function Hash(El : Elem) return Integer; -package Hashed_Sets is - - type Set is private; - function Union(Left, Right : Set) return Set; - ... - - package Signature is new Set_Signature(Elem, Set); -private - type Set is record ... end record; -end Hashed_Sets; - -The problem is that we can't do the instantiation of Set_Signature where we -would want to do so, because the instantiation freezes the type "Set" -prematurely. +There are situations where it is desirable to instantiate a generic +with a private type before it has been completely defined. Unfortunately +a generic instantiation freezes all of its actual parameters. In cases +where the formal type is not used in a way that requires a completely +defined type, it would be valuable if there were a kind of formal type +that did not freeze the actual upon instantiation. !proposal @@ -155,26 +105,65 @@ !discussion -This generic formal is used in the definition of the iterator generic (see -AI05-0139-2) in order to allow that generic to be visibly instantiated in -containers packages (see AI05-0212-1). +A formal incomplete type which does not cause freezing of the actual +upon instantiation nicely solves the challenge of allowing instantiations +of certain kinds of simple generics in the same visible part +where a private type is declared. The Iterator_Interfaces generic is +such a generic (see AI05-0139-2); it is instantiated in the containers +packages with a private type (see AI05-0212-1). + +Another important example of such a generic is a "signature" generic, such +as the following: + + generic + type Element; + type Set; + with function Union(Left, Right : Set) return Set is <>; + with function Intersection(Left, Right : Set) return Set is <>; + with function Unit_Set(Elem : Element) return Set is <>; + with procedure Add(Elem : Element; To_Set : in out Set) is <>; + ... + package Set_Signature is end; + +Such a signature generic can be instantiated with a private "Set" type +and then the instance can be passed into other generics that have +a formal package such as: + + generic + type VN is private; + with package Sets is Set_Signature(Element => VN, others => <>); + ... + package Flow_Analysis is ... + +This allows the construction of a generic that needs a "Set" +abstraction, such as a flow analysis package. This is not something +that would be easy to do with just an Set "interface," since a "Set" +abstraction needs to be parameterized by the element type. !example -The set signature shown in the !problem could be written as: +The set generic given above could be included in a set container +package: -generic - type Element is private; - type Set; - with function Size(Of_Set : Set) return Natural is <>; - with function Union(Left, Right : Set) return Set is <>; - with function Intersection(Left, Right : Set) return Set is <>; - with function Empty return Set is <>; - with function Unit_Set(With_Item : Element) return Set is <>; - with function Nth_Element(Of_Set : Set) return Element is <>; -package Set_Signature is end; + generic + type Element is private; + package My_Sets is + type Set is tagged private; + + function Union (Left, Right : Set) return Set; + function Intersection (Left, Right : Set) return Set; + function Unit_Set (Elem : Element) return Set; + procedure Add (Elem : Element; To_Set : in out Set); + ... + + package My_Set_Signature is new Set_Signature (Element, Set); + + private + ... + end My_Sets; -Now the instance shown in the !problem is legal. +This makes the signature immediately available for use (as in the Flow_Analysis +package above). !corrigendum 3.10.1(8/2) @@ -986,3 +975,57 @@ Fine by me. **************************************************************** + +From: Tucker Taft +Sent: Monday, May 2, 2011 9:44 PM + +[Part of this message that pertains to this AI.] + +AI05-0213-1/01 Formal incomplete types + [This is unchanged (but previously No Action); we need this to enable + the solution for the next two AIs.] + Approve __X____ Disapprove ______ Abstain _______ + Comments: + Needs a problem and a discussion. How about: + Problem: + There are situations where it is desirable to instantiate a generic + with a private type before it has been completely defined. Unfortunately + a generic instantiation freezes all of its actual parameters. In cases + where the formal type is not used in a way that requires a completely + defined type, it would be valuable if there were a kind of formal type + that did not freeze the actual upon instantiation. + Discussion: + A formal incomplete type which does not cause freezing of the actual + upon instantiation nicely solves the challenge of allowing instantiations + of certain kinds of simple generics in the same visible part + where a private type is declared. The Iterator_Interfaces generic is + such a generic (see AI-139). Another important example of such a + generic is a "signature" generic, such as the following: + + generic + type Element; + type Set; + with function Union(Left, Right : Set) return Set is <>; + with function Intersection(Left, Right : Set) return Set is <>; + with function Unit_Set(Elem : Element) return Set is <>; + with procedure Add(Elem : Element; To_Set : in out Set) is <>; + ... + package Set_Signature is end; + + Such a signature generic can be instantiated with a private "Set" type + and then the instance can be passed into other generics that have + a formal package such as: + + generic + type VN is private; + with package Sets is Set_Signature(Element => VN, others => <>); + ... + package Flow_Analysis is ... + + This allows the construction of a generic that needs a "Set" + abstraction, such as a flow analysis package. This is not something + that would be easy to do with just an Set "interface," since a "Set" + abstraction needs to be parameterized by the element type. + +**************************************************************** +

Questions? Ask the ACAA Technical Agent