Universal and Existential Type Quantification in Type System of Ada and OOP with Ada

25
Universal and Existential Type Quantification in Type System of Ada and OOP with Ada Gábor Kusper Type Systems, SS2000 RISC-Linz

description

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 - PowerPoint PPT Presentation

Transcript of Universal and Existential Type Quantification in Type System of Ada and OOP with Ada

Page 1: 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

Page 2: Universal and Existential  Type Quantification in Type System of Ada and OOP with Ada

Universal quantification yields generic types.

Page 3: Universal and Existential  Type Quantification in Type System of Ada and OOP with Ada

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)

Page 4: Universal and Existential  Type Quantification in Type System of Ada and OOP with Ada

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]

Page 5: Universal and Existential  Type Quantification in Type System of Ada and OOP with Ada

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.

Page 6: Universal and Existential  Type Quantification in Type System of Ada and OOP with Ada

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]

Page 7: Universal and Existential  Type Quantification in Type System of Ada and OOP with Ada

Existential quantification yields abstract data types.

Page 8: Universal and Existential  Type Quantification in Type System of Ada and OOP with Ada

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)}

Page 9: Universal and Existential  Type Quantification in Type System of Ada and OOP with Ada

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)

Page 10: Universal and Existential  Type Quantification in Type System of Ada and OOP with Ada

Combination of universal and existential quantification yields

parametric data abstractions.

Page 11: Universal and Existential  Type Quantification in Type System of Ada and OOP with Ada

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}

Page 12: Universal and Existential  Type Quantification in Type System of Ada and OOP with Ada

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)

Page 13: Universal and Existential  Type Quantification in Type System of Ada and OOP with Ada

Bounded universal quantification yields subtypes.

Page 14: Universal and Existential  Type Quantification in Type System of Ada and OOP with Ada

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)

Page 15: Universal and Existential  Type Quantification in Type System of Ada and OOP with Ada

Subtypes 2.

• Generic subprogram• Value assignment:

procedure SWAP is new EXCHANGE(ELEM => INTEGER);

• Generic function• Value assignment:

value SWAP : (IntInt) (IntInt) = Generic_ EXCHANGE [Int]

Page 16: Universal and Existential  Type Quantification in Type System of Ada and OOP with Ada

Subtypes 3.

• Generic Formal Types• Discrete types: (<>)• Integer types: range <>• Floating point types: digits <>• Fixed point types: delta <>

Page 17: Universal and Existential  Type Quantification in Type System of Ada and OOP with Ada

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);

Page 18: Universal and Existential  Type Quantification in Type System of Ada and OOP with Ada

Bounded existential quantification yields partial

abstraction.

Page 19: Universal and Existential  Type Quantification in Type System of Ada and OOP with Ada

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;

Page 20: Universal and Existential  Type Quantification in Type System of Ada and OOP with Ada

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;

Page 21: Universal and Existential  Type Quantification in Type System of Ada and OOP with Ada

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;

Page 22: Universal and Existential  Type Quantification in Type System of Ada and OOP with Ada

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.

Page 23: Universal and Existential  Type Quantification in Type System of Ada and OOP with Ada

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()

Page 24: Universal and Existential  Type Quantification in Type System of Ada and OOP with Ada

Thank You for your attention!

Page 25: Universal and Existential  Type Quantification in Type System of Ada and OOP with Ada

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))