1 / 25

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, SS2000 RISC-Linz. Universal quantification yields generic types. Generic subprogram

maeve
Télécharger la présentation

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

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Universal quantification yields generic types.

  3. 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.(ELEMELEM)(ELEMELEM)value EXCHANGE : Generic_ EXCHANGE = all[ELEM]fun(UV : ELEMELEM) exchange(U,V) Generic Types 1.

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

  5. 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.

  6. 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.

  7. Existential quantification yields abstract data types.

  8. 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 : UnitP, m : RRP}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.

  9. 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.

  10. Combination of universal and existential quantification yields parametric data abstractions.

  11. 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 : IntQueue, Append : (QueueEltType)Queue} // EltType is a free type variabletype Generic_Queue_Package = EltType. Queue. Generic_Queue_PackageWRT[EltType][Queue]type Generic_Queu_PackageWRT[EltType][Queue] = { Init : IntQueue, Append : (QueueEltType)Queue} Parametric Data Abstraction 1.

  12. 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.

  13. Bounded universal quantification yields subtypes.

  14. 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 = ELEMDiscrete_types.(ELEMELEM)(ELEMELEM)value EXCHANGE : Generic_ EXCHANGE = all[ELEM]fun(UV : ELEMELEM) exchange(U,V) Subtypes 1.

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

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

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

  18. Bounded existential quantification yields partial abstraction.

  19. 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, --T2T1end PA;

  20. 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

  21. 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

  22. 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.

  23. 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

  24. Thank You for your attention!

  25. 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

More Related