Universal and Existential Type Quantification in Type System of Ada and OOP with Ada
description
Transcript of Universal and Existential Type Quantification in Type System of Ada and OOP with Ada
Universal and Existential Type Quantification in Type System of
Ada and OOP with Ada
Gábor KusperType Systems, SS2000
RISC-Linz
Universal quantification yields generic types.
Generic Types 1.
• Generic subprogram• Type:
generic type ELEM is private;procedure EXCHANGE(U, V : in out ELEM);procedure EXCHANGE(U, V : in out ELEM) is T : ELEM; --the generic formal type
begin T:=U; U:=V; V:=T;end EXCHANGE;
• Generic function• Type:
type Generic_ EXCHANGE = ELEM.(ELEMELEM)(ELEMELEM)
value EXCHANGE : Generic_ EXCHANGE = all[ELEM]fun(UV : ELEMELEM) exchange(U,V)
Generic Types 2.
• Generic subprogram• Value assignment:
procedure SWAP is new EXCHANGE(ELEM => INTEGER);
• Generic function• Value assignment:
value SWAP : (IntInt) (IntInt) = Generic_ EXCHANGE [Int]
Generic Types 3.
• Generic package• Type:
generic type T is private;package PairRecord_Package is type PairRecord is record A,B : T; end record;end PairRecord;
• Parametric Type• Type:
type PairRecord[T] = { A : T, B : T}
• PairRecord is a type operator.
Generic Types 4.
• Generic package• Instanciate:
package P is new PairRecord_Package(INTEGER)subtype IntPair is P.PairRecord;
• Parametric Type• Instanciate:
type IntPair = PairRecord[Int]
Existential quantification yields abstract data types.
Abstract Data Types 1.
• Ada Package• Type:
package point2 is type P is private; function m(x,y:R) return P;private type P is array(0..1) of R;end point2;package body point2 is function m(x, y:R) return P begin ... end m;end point2;
• Package• Type:
type Point2 =P.Point2WRT[P]type Point2WRT[P] = { Init : UnitP, m : RRP}value point2 : Point2 = pack[P = Array[R](2) in Point2WRT[P]] { Init = fun() init(), m = fun(x : R, y : R) makepoint(x,y)}
Abstract Data Types 2.
• Ada Package• Value assignment:
declare p : point2.P = point2.m(1.0,2.0);
• Package• Value assigment:
open point2 as x[b] in value p : b = x.m(1.0,2.0)
Combination of universal and existential quantification yields
parametric data abstractions.
Parametric Data Abstraction 1.• Generic Package• Type declaration
generic type EltType is private;package Queue_Package is type Queue(MaxElts:Natural) is limited private; procedure Append(Q: in out Queue; E in EltType);private subtype Non_Negative is Integer range 0..Integer’LAST; type Queue(MaxElts:Natural) is record First,Last:Non_Negative := 0; Elements:array(0..MaxElts) of EltType; end record;end Queue_Package;
• Generic Package• Type declaration
type Queue_Package = Queue. Queue_PackageWRT[Queue]type Queu_PackageWRT[Queue] = { Init : IntQueue, Append : (QueueEltType)Queue} // EltType is a free type variabletype Generic_Queue_Package = EltType. Queue. Generic_Queue_PackageWRT[EltType][Queue]type Generic_Queu_PackageWRT[EltType][Queue] = { Init : IntQueue, Append : (QueueEltType)Queue}
Parametric Data Abstraction 2.• Generic Package• Value assignment
package Q is new Queue_Package(Integer);
declare queue : Q.Queue(100);
• Generic Package• Value assignment
value newQP : Generic_Queue_Package = all[EltType] pack[Queue = { First,Last,CurSize : Non_Negative, MaxElts : Natural, Elements : Array[EltType]} in Generic_Queue_PackageWRT[EltType][Queue]] { Init = fun(Max:Int) (0,0,0,Max,Array[EltType](Max), Append = fun(Q:Queue;E:EltType) append(Q,E)}value Q : Queue_Package= newQP[Int]open Q as x[b] in value queue : b = x.Init(100)
Bounded universal quantification yields subtypes.
Subtypes 1.
• Generic subprogram• Type:
generic type ELEM is (<>);procedure EXCHANGE(U, V : in out ELEM);procedure EXCHANGE(U, V : in out ELEM) is T : ELEM; --the generic formal type
begin T:=U; U:=V; V:=T;end EXCHANGE;
• Generic function• Type:
type Generic_ EXCHANGE = ELEMDiscrete_types.(ELEMELEM)(ELEMELEM)
value EXCHANGE : Generic_ EXCHANGE = all[ELEM]fun(UV : ELEMELEM) exchange(U,V)
Subtypes 2.
• Generic subprogram• Value assignment:
procedure SWAP is new EXCHANGE(ELEM => INTEGER);
• Generic function• Value assignment:
value SWAP : (IntInt) (IntInt) = Generic_ EXCHANGE [Int]
Subtypes 3.
• Generic Formal Types• Discrete types: (<>)• Integer types: range <>• Floating point types: digits <>• Fixed point types: delta <>
Subtypes 4.
• Ada subtype notion• Examples:
subtype RAINDOW is COLOR range RED .. BLUE;subtype RED_BLUE is RAINBOW;subtype INT is INTEGER;subtype UP_TO_K is INTEGER range -10 .. 10;subtype MALE is PERSON(SEX => M);
Bounded existential quantification yields partial
abstraction.
Partial Abstraction 1.
• Ada Package with tagged private typepackage PA is type T1 is tagged private; --T1 is hidden type T2 is new T1 with private; --T2 is hidden, --T2T1end PA;
OOP wirh ADA• Ada tagged record• Type declaration
type Account_With_Interest is tagged record Identity : Account_Number:= None; Balance : Money := 0.00; Rate : Interest_Rate := 0.05; Interest : Money := 0.00; end record;procedure Accure_Interest( On_Account: in out Account_With_Interest; Over_Time : in Integer); procedure Deduct_Charges( From: in out Account_With_Interest);
• OO pseudo code• Class declaration
class Account_With_Interest method Accure_Interest(Over_Time : Integer) method Deduct_Charges()
attribute Identity : Account_Number:= None; attribute Balance : Money := 0.00; attribute Rate : Interest_Rate := 0.05; attribute Interest : Money := 0.00;end Account_With_Interest;
Inheritance• Ada tagged record• Type declaration
type Free_Checking_Account is new Account_With_Interest with record Min_Balance : Money := 500.00; Transactions : Natural := 0; end record;procedure Withdraw( From: in out Free_Checking_Account; Amount : in Money);
• OO pseudo code• Class declaration
class Free_Checking_Account isa Account_With_Interest method Withdraw(Amount: Money)
attribute Min_Balance : Money := 500.00; attribute Transactions : Natural := 0; end Free_Checking_Account;
Class-wide type
• For each tagged type T, there is an associated class-wide type T'Class. The set of values of T'Class is the discriminated union of the sets of values of T and all types derived directly or indirectly from T. Discrimination between the different specific types is with a type tag. This tag, associated with each value of a class-wide type, is the basis for run-time polymorphism in Ada 95.
Override & Method Dispatching• Ada tagged record• type File is tagged private;
procedure View(F: File);• type Directory is new File with private;
procedure View(D: Directory);• type Ada_File is new File with private;
procedure View(A: Ada_File);• type Ada_Library is new Directory with
private;procedure View(L: Ada_Library);
• declare A_File: File'Class := Get_File_From_User;begin View(A_File);--dispatches according to specific type of file
end;
• OO pseudo code• class File
method View() end File;class Directory isa File method View() end Directory;class Ada_File isa File method View() end Ada_File;class Ada_Library isa Directory method View() end Ada_Library;
//Get_File_From_User() return File A_File = Get_File_From_User();A_File.View()
Thank You for your attention!
Universal Type QuantificationParametric Types
• Discriminant notion of Ada• Example 1:
type Queue(Max : Natural) is record First, Last : Natural := 0; CurSize : Natural :=0; Elements : array(0..Max) of EltType; end record
queue : Queue(100);
• translation• Example 1 is:
type Queue = { First : Natural, Last : Natural, CurSize : Natural, Max : Natural, Elements :Array[EltType]}
value queue : Queue = (0,0,100,Array[EltType](100))