Ada Ada: Gen : Generi erics cs Neil Mitchell Aga gain, aga - - PowerPoint PPT Presentation

ada ada gen generi erics cs
SMART_READER_LITE
LIVE PREVIEW

Ada Ada: Gen : Generi erics cs Neil Mitchell Aga gain, aga - - PowerPoint PPT Presentation

Want to follow along? http://www.cs.york.ac.uk/~ndm/ads.pdf Ada Ada: Gen : Generi erics cs Neil Mitchell Aga gain, aga in, again, ag in, again ain Dave writes a linked list package for characters Sue writes a linked list package


slide-1
SLIDE 1

Ada Ada: Gen : Generi erics cs

Neil Mitchell

http://www.cs.york.ac.uk/~ndm/ads.pdf

Want to follow along?

slide-2
SLIDE 2

Aga gain, aga in, again, ag in, again ain

 Dave writes a linked list package for

characters

 Sue writes a linked list package for integers  Ed writes a linked list package for booleans  What if Dave had written a linked list package

for <anything>?

 Sue and Ed could have gone to the pub!  Generics

Generics allows Dave to do this

slide-3
SLIDE 3

Gen Generic? eric?

Value Value

Function

Type Package

Generic Package

Value Action

Procedure

Type Function

Generic Function

slide-4
SLIDE 4

procedur procedure e Swap_Float(X,Y: in out : in out Float) is is T : Float; begin begin T := X; X := Y; Y := T; end; end;

Sw Swap_ ap_Flo loat at

slide-5
SLIDE 5

generic generic type type Item is private is private; procedur procedure Swap(X,Y: in out in out Item); procedur procedure e Swap(X,Y: in out in out Item) is is T: Item; begin begin T := X; X := Y; Y:= T; end end Swap;

  • - an instantiation, which we use

procedur procedure e Swap_Float is new is new Swap(Float);

Sw Swap ( ap (gener generic ic proc proced edure) ure)

slide-6
SLIDE 6

generic generic type type Element is private is private; package package List is is type type List is private is private; Nil : constant constant List; function function Null_Query(L : List) return return Boolean; function function Cons(Head : Element; Tail : List) return return List; ... -- other useful methods private private ... -- as before end end List;

Gen Generic p eric pack ackage sp age spec ecific ificat ation ion

Not Note: Save as “list.ads”

slide-7
SLIDE 7

package body package body List is is function function Null_Query (L : List) return return Boolean is is begin return begin return L = Nil; end end Null_Query; function function Cons(Head : Element; Tail : List) return return List is is begin begin return new return new Cell'(Content => Head ,Next => Tail); end end Cons; end end List;

Gen Generic p eric pack ackage b age bod

  • dy

Not Note: Save as “list.adb”

slide-8
SLIDE 8

with with List; -- import procedure procedure Test is is

  • - instantiate

package package List_Integer is new is new List(Integer);

  • - use

Ns : List_Integer.List := List_Integer.Nil; begin begin Ns := List_Integer.Cons(Head => 6, Tail => Ns); ...; end end Test;

Us Using ing a ge a gener neric pac ic packa kage ge

Not Note: Save as “test.adb”

slide-9
SLIDE 9

generic generic type type Element is <somet is <something> hing>; package package List is is

 limi

mited private ted private = use as parameter type,

declare variables

 pri

priva vate te = l = limited pri imited priva vate te + assign and test

for equality

 (<>) = pri

(<>) = priva vate te + treat as discrete type (T’First, T’Range, etc)

Gen Generic typ eric type pa e paramet rameters ers

slide-10
SLIDE 10

 Requ

Request: est: print a list

 procedure Put(L : List);

 Impossible!  An item in list is generic  We don’t know how to write it to the screen  Solu

Solution ion: the user tells us how

 with procedure Element_Put(E : in Element);

Pro Proced cedures as ures as Pa Param rameters eters

slide-11
SLIDE 11

generic generic type type Element is private is private; with procedure with procedure Element_Put(E : in Element); package package List is is procedure procedure Put(L : in in List); ... -- as before end end List; procedure procedure Put(L : in in List) is is begin begin if not if not Null_Query(L) then then Element_Put(Head(L));

Im Implement lementing ing Put Put

slide-12
SLIDE 12

with with Ada.Text_IO, List; procedure procedure Test is is package package List_Char is new is new List (Element => Character ,Element_Put => Ada.Text_IO.Put); Hi : List_Char.List := ...; begin begin List_Char.Put(Hi); end end Test;

Us Using ing Put Put

slide-13
SLIDE 13

 Each List package provides an unlimited

number of values:

  • Every operation takes a List parameter
  • Which data structure to operate on
  • Abstract Data Type (ADT)

 An alternative is to have one value in one

package

  • No more saying which data structure
  • Can sometimes be simpler
  • Abstract Object

One p ne pack ackage, on ge, one e val value? ue?

slide-14
SLIDE 14

gener neric ic type type Element is is p priv ivate te; with proc rocedu edure re Put(E : in in Element); packa ckage ge One_List is is

  • - Note: no exported type or constants!
  • - All the state is in the body
  • - A new facility (replaces constant Nil)

procedure ure Reset; function

  • n Null_Query return

return Boolean; procedure ure Cons(Head : in in Element); ...

  • - Note: no private section!

end d One_List;

Abst bstract ract Ob Object ject: Spe : Specific ificat ation ion

slide-15
SLIDE 15

with th List; packa ckage ge body body One_List is is pa pack ckag age List_Element is is n new ew List (Element => Element, Put => Put); The_List : List_Element.List; function

  • n Null_Query return

return Boolean is is begi egin ret n return urn List_Element.Null_Query(The_List); end end Null_Query; procedure ure Cons(Head : in Element) is is begin begin The_List := List_Element.Cons(Head, The_List); end end Cons; begin gin Reset; end d One_List;

Abst bstract ract Ob Object ject: Bod : Body

slide-16
SLIDE 16

with th One_List; pr proce cedur ure Classify is is package e Marks is new is new One_List(Integer, Num_Out); package e Age is new is new One_List(Natural, Num_Out); begin Marks.Cons(99); -- cheated, not caught Marks.Cons(70); -- revised hard Marks.Cons(-5); -- cheated, caught, expelled Marks.Put; Marks.Reset; -- get rid of last years marks if Age.Null_Query then Age.Cons(21);

Abst bstract ract Ob Object ject: Use : Use

slide-17
SLIDE 17

 Work through the exercises

  • No more of us talking at the start of practicals
  • Go from where you are
  • Get as far as you can

 If you are struggling

  • Stick your hand up, get some help now
  • You will be expected to be able to program Ada for

the open assessment

  • You will not get Ada help during that time

What t hat to

  • do

do no now? w?