250 likes | 338 Views
Universal and Existential Type Quantification in Type System of Ada and OOP with Ada. G ábor Kusper Type Systems, SS2000 RISC-Linz. Universal quantification yields generic types. Generic subprogram
E N D
Universal and Existential Type Quantification in Type System of Ada and OOP with Ada Gábor Kusper Type Systems, SS2000 RISC-Linz
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 typebegin 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 1.
Generic subprogram Value assignment:procedure SWAP is new EXCHANGE(ELEM => INTEGER); Generic function Value assignment:value SWAP : (IntInt) (IntInt) = Generic_ EXCHANGE [Int] Generic Types 2.
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 3.
Generic package Instanciate:package P is new PairRecord_Package(INTEGER)subtype IntPair is P.PairRecord; Parametric Type Instanciate:type IntPair = PairRecord[Int] Generic Types 4.
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 1.
Ada Package Value assignment:declare p : point2.P = point2.m(1.0,2.0); Package Value assigment:open point2 as x[b] invalue p : b = x.m(1.0,2.0) Abstract Data Types 2.
Combination of universal and existential quantification yields parametric data abstractions.
Generic Package Type declarationgeneric 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 declarationtype 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 1.
Generic Package Value assignmentpackage Q is new Queue_Package(Integer);declare queue : Q.Queue(100); Generic Package Value assignmentvalue 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] invalue queue : b = x.Init(100) Parametric Data Abstraction 2.
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 typebegin 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 1.
Generic subprogram Value assignment:procedure SWAP is new EXCHANGE(ELEM => INTEGER); Generic function Value assignment:value SWAP : (IntInt) (IntInt) = Generic_ EXCHANGE [Int] Subtypes 2.
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;
Ada tagged record Type declarationtype 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 declarationclass 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; OOP wirh ADA
Ada tagged record Type declarationtype 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 declarationclass 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; Inheritance
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.
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 fileend; OO pseudo code class File method View() end File;class Directory isa File method View() end Directory;class Ada_File isa Filemethod 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() Override & Method Dispatching
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 recordqueue : 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)) Universal Type QuantificationParametric Types