An Introduction to Ada - PowerPoint PPT Presentation

1 / 47
About This Presentation
Title:

An Introduction to Ada

Description:

Are the bounds of an array answer part of the properties of the array type? ... VV : Vstr := (5, 'hello'); Other Types. Derived types (copying a type) ... – PowerPoint PPT presentation

Number of Views:64
Avg rating:3.0/5.0
Slides: 48
Provided by: robert948
Learn more at: https://cs.nyu.edu
Category:

less

Transcript and Presenter's Notes

Title: An Introduction to Ada


1
An Introduction to Ada
  • Programming Languages
  • Fall 2002

2
Basic Structure of a Program
  • A program is a collection of units
  • Packages
  • Functions
  • Procedures
  • Bound together to form a program
  • Typically a unit is stored in a file
  • Units reference other units

3
Procedure
  • procedure H (M Integer) is
    declarationsbegin statements returnend
    H
  • Typical use is procedure Main is which defines
    the main program

4
Function
  • function Max (A Integer B Integer) return
    Integeris Result Integerbegin if A gt
    B then Result A else Result
    B end if return Resultend Max

5
Packages
  • Packages define a related collection of types,
    and subprograms (procedure and functions)
  • A package provides a set of services to a client
  • A package may be a client of another package

6
Package Spec (Declaration)
  • package X is declarations types
    subprogram specs (but not subprogram
    bodies)end X

7
Subprogram Specs
  • procedure Print_In_Hex (X Integer)
  • function Max (A, B Integer) return Integer
  • Note the semicolon instead of is
  • A subprogram spec has everything you need to know
    to use the subprogram
  • A client needs only the spec

8
Package Bodies
  • package body X is declarations subprograms
    local to body variables/constants local to
    body subprogram bodies for subprogram
    specs appearing in the package specbegin
    initialization statementsend X

9
How to be A Client
  • To access a package, use a WITHwith
    Calendarprocedure Main is Today
    Calendar.Time end Main

10
The Use Clause
  • Accessing Stuff in Calendar without dots
  • with Calendaruse Calendarprocedure Main is
    Today Time end Main

11
Package Bodies as Clients
  • with Calendarpackage body Julian_Calendar_Stuff
    is end Julian_Calendar_Stuff
  • Here we have the implementation of a package done
    using stuff in another package

12
Package Specs as Clients
  • A package spec can build on another spec
  • with Calendaruse Calendarpackage To_Do_List
    is procedure Enter (T Time M
    String) -- Enter new item in todo list. Time
    is -- deadline. M is description.end
    To_Do_List

13
The Idea of a Package Spec
  • Write the package spec
  • It is like a contract between client and body of
    the spec
  • Write the body
  • Write the client
  • Last two activities are completely independent
    (and should not talk to one another except via
    the spec)

14
Integer Type Declarations
  • Type Integer is built in
  • But you dont want to use it
  • Because its range is implementation defined
  • Because it is defined to match the machine not
    your problem
  • Because it does not take advantage of strong
    typing to prevent errors

15
Defining Integer Types
  • Define type according to use
  • type Day_In_Year is range 1 .. 366type Age is
    range 0 .. 130type Temperature is range -20 ..
    180
  • Now we can define variables of the type
  • Today_Day Day_In_YearEmployee_Age
    AgeMachine_Room_Temp Temperature

16
Why Define Integer Types
  • No dependence on implementation
  • Unlike type int in C
  • Range of types matches problem
  • Get an error or warning at compile time
  • Age 200
  • Or an exception at runtime
  • Age Age 1000

17
Strong Typing
  • Cannot mix integer types
  • Current_Temp TemperatureCurrent_Pressure
    Pressure
  • Current_Temp Current_Pressure 1
  • Error, cannot assign pressure to temperature
  • Current_Temp Current_Temp
    Current_Pressure
  • Error, cannot add temperature to pressure

18
Integer Subtypes
  • A subtype creates a limited range
  • But is still the same type
  • subtype OK_Operating_Range is Temperature range
    70 .. 80Room_Temp TemperatureMachine_Room_Te
    mp OK_Operating_RangeMachine_Room_Temp
    Room_Temp
  • Raises exception if Room_Temp out of range

19
Catching Exceptions
  • You can catch an exception at run time
  • begin Machine_Room_Temp Room_Temp
    exception when Constraint_Error gt
    recovery stuffend

20
Unsigned (Modular) Types
  • Modular types have wrap around
  • type M is mod 7 -- values are 0,1,2,3,4,5,6q
    m 6 -- initializationq q 2 -- result
    is 1
  • Most common use, conventional unsigned
  • type Uns_32 is mod 2 32
  • Remember that twos complement arithmetic is
    equivalent to arithmetic mod 2wordsize

21
Real Types
  • Float types (control relative accuracy)
  • type My_Float is digits 7type Xfloat is digits
    7 range 1.0 .. 10.0subtype F1 is My_Float range
    1.0 .. 5.0
  • Digits is decimal digits of relative precision
  • There is a formal model for fpt in Ada
  • Target independent (parametrized)
  • Guarantees minimal accuracy
  • Operations defined in terms of model numbers
  • Results fall in defined model interval

22
Fixed-Point Types
  • Fixed-point types are real types where you
    control the absolute accuracy.
  • Typically implemented as scaled integers
  • type Velocity is delta 0.125 range 0.0 .. 10.0
  • Decimal fixed-point types
  • Decimal small
  • Typical use in financial programming
  • type Money is digits 10 delta 0.01 range 0.00
    .. 999_999_999.00

23
Character Types
  • Built in types
  • Character (8-bit Latin-1)
  • Wide_Character (16-bit Unicode/ISO 10646)
  • Good enough for most purposes, but you can define
    your own types
  • type My_Character is (A, B, C, .)
  • Note on standard types
  • Standard types are in package Standard that is
    automatically visible in every unit.

24
Enumeration Types
  • An enumeration type is a sequence of ordered
    enumeration literals
  • type State is (Off, Powering_Up, On)
  • No arithmetic defined
  • S1, S2 StateS1 S1 S2 -- Illegal
  • Can add/subtract one
  • StatePred (S1)StateSucc (S2)
  • These are examples of attributes

25
Boolean Types
  • Predefined enumeration type
  • type Boolean is (False, True)
  • Expressions of type boolean used in
  • if statements
  • while loops
  • exit statements
  • Etc.

26
Access Types
  • Access types function like pointers
  • But are not necessarily implemented that way
  • type r is access integertype s is access all
    integer
  • The all allows the access value to reference any
    item at all. Without all, you can only reference
    objects specifically allocated for the pool in
    question.

27
Using Access Types
  • Allocate an object using new
  • type AI is access all integerPtr AIPtr
    new Integer -- uninitializedPtr new
    Integer(12) -- initialized
  • To obtain value dereference
  • V IntegerV Ptr.all

28
Array Types
  • Arrays can have 1 or more subscripts
  • type Vector is
  • array (Integer range 1 .. 10) of
    Integertype Matrix is array (Integer range 0
    .. 10, Character range A .. Z)
    of Vector
  • VV Vector (others gt 10) -- aggregateMM
    MatrixMM (5, C) VVVV (1 .. 5) VV (2
    .. 6) -- slicing (one dim only)

29
Array Bounds and Types
  • Are the bounds of an array answer part of the
    properties of the array type?
  • Things are much cleaner if we answer yes, as in
    Pascal, but this is very limiting
  • For example, a sort routine cannot take a vector
    of any size to sort.
  • Instead we consider the bounds to be like the
    range of an integer type

30
Unconstrained Arrays
  • Unconstrained array type has no bounds
  • type UA is array (Int range ltgt) of Int--
    cannot use UA to declare a variable-- instead
    must build a subtypesubtype UA5 is UA (1 ..
    5)UAV5 UA5 (6,5,4,3,2)-- can also set
    bounds for a variableUAV2 UA (1 .. 2)

31
String Types
  • A string type is an array whose elements are a
    character type.
  • Two standard built in string types
  • type String is array (Natural range ltgt) of
    Charactertype Wide_String is array (Natural
    range ltgt) of Wide_CharacterS String (1 .. 5)
    Hello
  • Note Natural is a predefined subtype of Integer
    with bounds 0 .. IntegerLast

32
Record Types
  • Like struct in C
  • type Date is record Year Year_Number
    2002 -- default Month Month_Number Day
    Day_Numberend recordDD DateEE Date
    (2001, 8, Day gt 27)DD.Month EE.Month 1

33
Arrays/Records and Access Types
  • Access types and records/arrays
  • type A is array (Int range 0 .. 10) of Inttype
    AP is access AAV APAV.all (3) AV (4)
    -- can omit .all here
  • Similarly do not need .all for fields of records
  • DP.Month DP.all.Month 1

34
What about Union Types?
  • The union type in C is fundamentally unsafe, and
    therefore unacceptable
  • union (int, float) puzzle
  • Now puzzle has either an int or a float
  • But at runtime, cannot tell which
  • So we have to trust the programer
  • In Ada we are short on trust ?

35
Instead, Discriminated Types
  • A record can have discriminants
  • type IF is (Int, Float)type Int_Float (V IF
    Int) is record case V is when Int gt
    Int_Val Integer when Float gt Float_Val
    Float end caseend record
  • Now the value of the discriminant V shows what
    type is currently present

36
More on Discriminated Records
  • Referencing a discriminanted type
  • Puzzle Int_FloatPuzzle (Float, 1.0)F
    Puzzle.Float_Val -- OKI Puzzle.Int_Val
    -- raise exceptionPuzzle.V Int -- not
    allowed!

37
More on Discriminated Records
  • Can make subtypes
  • subtype PuzzleI is puzzle (Int)-- this type
    only holds int values
  • Can dimension arrays from discriminant
  • Subtype Vlen is Integer range 1 .. 10type Vstr
    (Vlen Integer 0) is record Data String
    (1 .. Vlen)end recordVV Vstr (5,
    hello)

38
Other Types
  • Derived types (copying a type)
  • Extended types (object oriented stuff)
  • Task types (active threads)
  • Protected types (passive synchronization)
  • More on all these later!

39
Statement Forms
  • Assignment statement (is not an expression)
  • Variable expression
  • If statements
  • if condition then statementselsif condition
    then statementselse statementsend if

40
Statement Forms (cont)
  • Loops
  • for J in Integer range 1 .. 10 loop
    statementsend loop
  • while condition loop statementsend loop
  • loop statementsend loop

41
Statements (cont)
  • Exit statement
  • exitexit when condition
  • Can only be used in a loop
  • Can use labels
  • Outer loop Inner loop exit Inner when
    Done end loop Innerend loop Outer

42
Statements (cont)
  • Case statements
  • case expression is when 0 gt statements
    when 1 10 gt statements when 2 .. 9 gt
    statementsend case
  • All values in when branches must be static
  • All possible values of expression must be
    included exactly once
  • Can use when others gt to cover rest

43
Statements (cont)
  • Return statement
  • return -- procedurereturn expression --
    function
  • Only in procedure/function
  • Function must have at least one return
  • Raise statement
  • raise exception-name

44
Declaring and Handling Exceptions
  • Declaring an exception
  • Error, Disaster exception
  • Raising an exception
  • raise Disaster-- strips stack frames till a
    handler is found
  • Handling an exception
  • exception when Disaster gt statements

45
More on Exception Handling
  • Anywhere we have begin end, we can do
  • begin statementsexception when handler gt
    statements when handler gt statementsend

46
Block Statement
  • Block statement can be used anywhere
  • declare -- declare section optional
    declarationsbegin statementsexception
    -- exception section optional handlersend

47
Summary
  • Thats enough to get you on your way!
  • First assignment will be to do one of the ACM
    programming competition problems
  • You do the program and make test data
  • Your choice of examples
  • See website for URL
Write a Comment
User Comments (0)
About PowerShow.com