Universal and Existential Type Quantification in Type System

  • Slides: 25
Download presentation
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 Kusper Type Systems, SS 2000 RISC-Linz

Universal quantification yields generic types.

Universal quantification yields generic types.

Generic Types 1. • Generic subprogram • Generic function • Type: generic type ELEM

Generic Types 1. • Generic subprogram • Generic function • Type: generic type ELEM is private; procedure EXCHANGE(U, V : in out ELEM) is T : ELEM; --the generic formal type begin T: =U; U: =V; V: =T; end EXCHANGE; • Type: type Generic_ EXCHANGE = ELEM. (ELEM) (E LEM ELEM) value EXCHANGE : Generic_ EXCHANGE = all[ELEM]fun(U V : ELEM) exchange(U, V)

Generic Types 2. • Generic subprogram • Generic function • Value assignment: procedure SWAP

Generic Types 2. • Generic subprogram • Generic function • Value assignment: procedure SWAP is new EXCHANGE(ELEM => INTEGER); • Value assignment: value SWAP : (Int Int) = Generic_ EXCHANGE [Int]

Generic Types 3. • Generic package • Parametric Type • Type: generic type T

Generic Types 3. • Generic package • Parametric Type • Type: generic type T is private; package Pair. Record_Package is type Pair. Record is record A, B : T; end record; end Pair. Record; • Type: type Pair. Record[T] = { A : T, B: T } • Pair. Record is a type operator.

Generic Types 4. • Generic package • Parametric Type • Instanciate: package P is

Generic Types 4. • Generic package • Parametric Type • Instanciate: package P is new Pair. Record_Package(INTEGER ) subtype Int. Pair is P. Pair. Record; • Instanciate: type Int. Pair = Pair. Record[Int]

Existential quantification yields abstract data types.

Existential quantification yields abstract data types.

Abstract Data Types 1. • Ada Package • Type: package point 2 is type

Abstract Data Types 1. • Ada Package • Type: package point 2 is type P is private; function m(x, y: R) return P; private type P is array(0. . 1) of R; end point 2; package body point 2 is function m(x, y: R) return P begin. . . end m; end point 2; • Type: type Point 2 = P. Point 2 WRT[P] type Point 2 WRT[P] = { Init : Unit P, m : R R P } value point 2 : Point 2 = pack[P = Array[R](2) in Point 2 WRT[P]] { Init = fun() init(), m = fun(x : R, y : R) makepoint(x, y) }

Abstract Data Types 2. • Ada Package • Value assignment: declare p : point

Abstract Data Types 2. • Ada Package • Value assignment: declare p : point 2. P = point 2. m(1. 0, 2. 0); • Value assigment: open point 2 as x[b] in value p : b = x. m(1. 0, 2. 0)

Combination of universal and existential quantification yields parametric data abstractions.

Combination of universal and existential quantification yields parametric data abstractions.

Parametric Data Abstraction 1. • Generic Package • • Type declaration generic type Elt.

Parametric Data Abstraction 1. • Generic Package • • Type declaration generic type Elt. Type is private; package Queue_Package is type Queue(Max. Elts: Natural) is limited private; procedure Append(Q: in out Queue; E in Elt. Type); private subtype Non_Negative is Integer range 0. . Integer’LAST; type Queue(Max. Elts: Natural) is record First, Last: Non_Negative : = 0; Elements: array(0. . Max. Elts) of Elt. Type; end record; end Queue_Package; Type declaration type Queue_Package = Queue_Package. WRT[Queue] type Queu_Package. WRT[Queue] = { Init : Int Queue, Append : (Queue Elt. Type) Queue } // Elt. Type is a free type variable type Generic_Queue_Package = Elt. Type. Queue. Generic_Queue_Package. WRT[Elt. Type ][Queue] type Generic_Queu_Package. WRT[Elt. Type][ Queue] = { Init : Int Queue, Append : (Queue Elt. Type) Queue }

Parametric Data Abstraction 2. • Generic Package • • Value assignment package Q is

Parametric Data Abstraction 2. • Generic Package • • Value assignment package Q is new Queue_Package(Integer); declare queue : Q. Queue(100); Value assignment value new. QP : Generic_Queue_Package = all[Elt. Type] pack[Queue = { First, Last, Cur. Size : Non_Negative, Max. Elts : Natural, Elements : Array[Elt. Type]} in Generic_Queue_Package. WRT[Elt. Type ][Queue]] { Init = fun(Max: Int) (0, 0, 0, Max, Array[Elt. Type](Max), Append = fun(Q: Queue; E: Elt. Type) append(Q, E) } value Q : Queue_Package= new. QP[Int] open Q as x[b] in value queue : b = x. Init(100)

Bounded universal quantification yields subtypes.

Bounded universal quantification yields subtypes.

Subtypes 1. • Generic subprogram • Generic function • Type: generic type ELEM is

Subtypes 1. • Generic subprogram • Generic function • Type: generic type ELEM is (<>); procedure EXCHANGE(U, V : in out ELEM) is T : ELEM; --the generic formal type begin T: =U; U: =V; V: =T; end EXCHANGE; • Type: type Generic_ EXCHANGE = ELEM Discrete_types. (E LEM ELEM) (ELEM ELE M) value EXCHANGE : Generic_ EXCHANGE = all[ELEM]fun(U V : ELEM) exchange(U, V)

Subtypes 2. • Generic subprogram • Generic function • Value assignment: procedure SWAP is

Subtypes 2. • Generic subprogram • Generic function • Value assignment: procedure SWAP is new EXCHANGE(ELEM => INTEGER); • Value assignment: value SWAP : (Int Int) = Generic_ EXCHANGE [Int]

Subtypes 3. • Generic Formal Types • • Discrete types: (<>) Integer types: range

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.

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.

Bounded existential quantification yields partial abstraction.

Partial Abstraction 1. • Ada Package with tagged private type package PA is type

Partial Abstraction 1. • Ada Package with tagged private type package PA is type T 1 is tagged private; --T 1 is hidden type T 2 is new T 1 with private; --T 2 is hidden, --T 2 T 1 end PA;

OOP wirh ADA • Ada tagged record • OO pseudo code • • Type

OOP wirh ADA • Ada tagged record • OO pseudo code • • 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); 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 • OO pseudo code • • Type declaration type

Inheritance • Ada tagged record • OO pseudo code • • 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); 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

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 • OO pseudo code • •

Override & Method Dispatching • Ada tagged record • OO pseudo code • • • 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; 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!

Thank You for your attention!

Universal Type Quantification Parametric Types • Discriminant notion of Ada • Example 1: type

Universal Type Quantification Parametric Types • Discriminant notion of Ada • Example 1: type Queue(Max : Natural) is record First, Last : Natural : = 0; Cur. Size : Natural : =0; Elements : array(0. . Max) of Elt. Type; end record queue : Queue(100); • translation • Example 1 is: type Queue = { First : Natural, Last : Natural, Cur. Size : Natural, Max : Natural, Elements : Array[Elt. Type] } value queue : Queue = (0, 0, 100, Array[Elt. Type](100))