1 16-035 Even More Ada Constructs 9 Oct. 2002. 2 16-035 Today’s Constructs Generics Private types Child library units Tasking Pragmas Elaboration.

Slides:



Advertisements
Similar presentations
Modules Program is built out of components. Each component defines a set of logically related entities (strong internal coupling) A component has a public.
Advertisements

MATH 224 – Discrete Mathematics
Programming Languages and Paradigms
CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
Abstract Data Types Data abstraction, or abstract data types, is a programming methodology where one defines not only the data structure to be used, but.
1 Languages and Compilers (SProg og Oversættere) Sequence control and Subprogram Control.
CPS 506 Comparative Programming Languages Abstract Data Type and Encapsulation.
Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Concurrency in Ada Programming Languages 1 Robert Dewar.
Concurrency in Ada What concurrency is all about Relation to operating systems Language facilities vs library packages POSIX threads Ada concurrency Real.
© Alan Burns and Andy Wellings, 2001 Programming in the Small n Aim: to remind you how to read/write programs in Ada 95, Java and ANSI C n Practicals will.
Scheduler Activations Effective Kernel Support for the User-Level Management of Parallelism.
More Ada Constructs 7 Oct Today’s Constructs Derived types Record types Array types Visibility and scope Subprogram arguments.
Lecture 9 Concepts of Programming Languages
© Alan Burns and Andy Wellings, 2001 Programming in the Small Aim: to remind you how to read/write programs in Ada 95, Java and ANSI C.
Concurrency - 1 Tasking Concurrent Programming Declaration, creation, activation, termination Synchronization and communication Time and delays conditional.
Abstract Data Types and Encapsulation Concepts
Chapter 15: Operator Overloading
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "methods" in Java Purpose Reuse code Modularize the program This.
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Recursion.
Porting Implementation of Packet Utilization Standard from ADA to JAVA Annelie Hultman (TEC-EME) Donata Pedrazzani (TEC-EMS) ESA/ESTEC 2004 JPUS de-briefing.
1 CS Programming Languages Class 15 October 17, 2000.
Major objective of this course is: Design and analysis of modern algorithms Different variants Accuracy Efficiency Comparing efficiencies Motivation thinking.
Slide: 1 Copyright © AdaCore Subprograms Presented by Quentin Ochem university.adacore.com.
CPS120: Introduction to Computer Science Decision Making in Programs.
Useful Ada Information Help for programming assignment.
ICS 313: Programming Language Theory Chapter 13: Concurrency.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Ch Ch jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes, notes, notes) Dr. Carter Tiernan.
What is a Package? A package is an Oracle object, which holds other objects within it. Objects commonly held within a package are procedures, functions,
I Power Higher Computing Software Development High Level Language Constructs.
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
1 © AdaCore under the GNU Free Documentation License Franco Gasperoni
ADT data abstraction. Abstraction  representation of concepts by their relevant features only  programming has two major categories of abstraction process.
CS533 – Spring Jeanie M. Schwenk Experiences and Processes and Monitors with Mesa What is Mesa? “Mesa is a strongly typed, block structured programming.
VHDL Discussion Subprograms IAY 0600 Digital Systems Design Alexander Sudnitson Tallinn University of Technology 1.
1 Chapter 11 © 1998 by Addison Wesley Longman, Inc The Concept of Abstraction - The concept of abstraction is fundamental in programming - Nearly.
1 CS Programming Languages Class 22 November 14, 2000.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 10 Abstraction - The concept of abstraction is fundamental in programming - Nearly all programming.
Winter 2006CISC121 - Prof. McLeod1 Stuff No stuff today!
Ada Constructs Revisited 21 Oct Constructs to be Expanded Generics Tasking Elaboration.
3/12/2013Computer Engg, IIT(BHU)1 OpenMP-1. OpenMP is a portable, multiprocessing API for shared memory computers OpenMP is not a “language” Instead,
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
Ada, Scheme, R Emory Wingard. Ada History Department of Defense in search of high level language around Requirements drafted for the language.
CPE779: Shared Memory and OpenMP Based on slides by Laxmikant V. Kale and David Padua of the University of Illinois.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
Def: A control structure is a control statement and
Abstract Data Types and Encapsulation Concepts
Computer Engg, IIT(BHU)
Chapter 3: Using Methods, Classes, and Objects
11.1 The Concept of Abstraction
Lecture 9 Concepts of Programming Languages
Abstract Data Types and Encapsulation Concepts
VHDL Discussion Subprograms
VHDL Discussion Subprograms
Subject : T0152 – Programming Language Concept
Languages and Compilers (SProg og Oversættere)
Java Programming Language
Arrays.
Threads CSE 2431: Introduction to Operating Systems
11.1 The Concept of Abstraction
Lecture 9 Concepts of Programming Languages
Chapter 11 Abstraction - The concept of abstraction is fundamental in
Presentation transcript:

Even More Ada Constructs 9 Oct. 2002

Today’s Constructs Generics Private types Child library units Tasking Pragmas Elaboration

Generics Generic units are used to make Ada’s strong types less painful —Possible to create general unit once, but use it for many different declared types —Has types, variables, subprograms, and packages as parameters —When each instance is made of a generic unit, it might be a separate copy, or it might share code

Generic Example* generic type My_Float is digits <>; package Generic_Complex_Numbers is type Complex is private;... function “+” (X, Y: Complex) return Complex;... end; generic type index is (<>); with package Complex_Numbers is new Generic_Complex_Numbers (<>); Package Generic_Complex_Vectors is use Complex_Numbers; type Vector is array (Index range <>) of Complex;... end;

Private Types Private types allow designers to hide information (sort of) Why is that good? —Coupling is bad; Cohesion is good —Coupling: Complex systems need simple boundaries among its constituent parts; otherwise, a change in one place can affect many others, even those that don’t need to be affected —Cohesion: Put everything having to do with some aspect of the system in a single place; that makes it easy to make changes, easy to understand the implementation, and easy to find things Items declared in the private part of a package specification can not be used anywhere else except —In the package itself —In child packages Taken from “Programming in Ada95”, 2 nd Edition, John Barnes

Private Parts Example with MATHEMATICS_EXCEPTIONS; with GENERIC_LINEAR_ALGEBRA_MATHEMATICS; generic type FLOAT_TYPE is digits <>; type INDEX_TYPE is (<>); type COLUMN_VECTOR_TYPE is array(INDEX_TYPE range <>) of FLOAT_TYPE; type MATRIX_TYPE is array(INDEX_TYPE range <>, INDEX_TYPE range <>) of FLOAT_TYPE; package GENERIC_QUATERNION_MATHEMATICS is type QUATERNION is private; type TRANSPOSE_SUPERSCRIPT_TYPE is (T); function SCALAR_PART_OF(X : QUATERNION) return FLOAT_TYPE; function QUATERNION_FROM_PARTS(SCALAR_PART : FLOAT_TYPE; VECTOR_PART : COLUMN_VECTOR_TYPE) return QUATERNION; function "**" (LEFT : QUATERNION; RIGHT : TRANSPOSE_SUPERSCRIPT_TYPE) return QUATERNION;... private... – Define Scalar_Type and Vector_Type type QUATERNION is record SCALAR : SCALAR_TYPE; VECTOR : VECTOR_TYPE; end record; end GENERIC_LINEAR_ALGEBRA_MATHEMATICS;

Operations Available for Private Types The only operations available to users of private types are —Assignment —Test for equality and inequality (i.e. if X = Y) —Procedures and functions that have an argument of the type —Attributes defined for all types (such as Size) Limited types are even more restrictive —Assignment, equality tests not defined —Note that assignment cannot be redefined, so it is never possible to use assignment for limited types type My_Limited_type is limited private;

Child Library Units Suppose you have a nice, cohesive package, with private types Suppose you want to create another package that needs access to those private types, but is not quite relevant enough to put in the currently existing package package Complex_Number_Pack is type Complex_type is private function Construct (R, I, My_Float) return Complex_type;... private... end Complex_Number_pack; Now, you want to create a set of functions to work with polar coordinates package Complex_Number_Pack.Polar is type Angle_type is digits 6 range ; function Construct (R : My_Float, Theta : Angle_type) return Complex;... end Complex_Number_Pack.Polar; Then, if you want to use either polar or Cartesian versions, simply use with Complex_Number_Pack.Polar; -- Get both packages

Tasking Multitasking (what UNIX people call Threads) is built in to the Ada language —Model is parallel activities —Easily implemented to use multiple processors —Implementation can use any scheduling mechanism »Time slicing, where each task (of the same priority) gets a bit of time, then the next task runs »One task runs until it hits a blocking point, then the next task (of the same priority or lower) runs —Priority can be used (if supported) to force the system to run tasks in a certain order (if running on a single processor system)

Rendezvous Example with TASKING_PRIORITY; package FIVE_HZ_TASKING is FIVE_HZ_TASK_SIZE : integer := 30_000; task type FIVE_HZ_TASK_TYPE is -- start execution of cyclic iteration entry START_EXECUTION; pragma PRIORITY(TASKING_PRIORITY.FIVE_HZ_PRIORITY); pragma Storage_Size (FIVE_HZ_TASK_SIZE); end FIVE_HZ_TASK_TYPE; FIVE_HZ_TASK : FIVE_HZ_TASK_TYPE; end FIVE_HZ_TASKING;

Task Body package body Five_Hz_Tasking is task body Five_Hz_Task_type is Local declarations begin Initialize the task accept Start_Execution; -- Wait for signal to start loop Perform cyclic processing end loop; end Five_Hz_Tasking; Calling a task entry is just like calling a procedure Five_Hz_Task.Start_Execution;

Protected Object Example package Variable is type My_Type is range 0.. 9_000_000; protected P_Variable is function Read return My_Type; -- Many callers procedure Write ( -- One caller X : in My_Type ); private V : My_Type := 0; end P_Variable; end Variable; Any number of tasks may call protected functions at the same time Protected functions can not write to protected objects Not possible for a task to call Write while another calls Read (the second will block)

Pragmas Pragmas are compiler directives —Many defined; few should be needed for project —Pragmas Elaborate, Elaborate_All, Elaborate_Body might be needed —Example is pragma Priority, which sets the priority of a task —Another example is pragma Pack, which tells the compiler not to leave gaps between elements of an array or record

Elaboration Ada programs perform work before the first executable statement after the “begin” of the main procedure is run —“Elaboration” code is executed first »Gives data initial values (which might call functions) »Creates objects (if dynamic) »Runs package initialization code To be completely precise, the whole program is one big elaboration —Package specifications are elaborated before their bodies —Package bodies are elaborated before any of their subprograms can be called —Package bodies can have task bodies, which start running after the package body has been elaborated —When all packages have been elaborated, the main procedure runs

Elaboration Example with Ada.Text_IO; procedure Elab_Test is package Random_Pack is -- Elaboration of this is first type My_Type is digits 6 range ; function Random return My_Type; end Random_Pack; package body Random_Pack is -- Elaboration of this is second Seed : My_Type; function Random return My_Type is begin return Seed; -- For now, return a constant end Random; task My_Task; task body My_Task is X : My_Type := Random; begin Ada.Text_IO.Put ("Made it"); end My_Task; begin Seed := 0.5; -- Need this before Random can be called end Random_Pack; -- After last statement after "begin", My_Task can run begin null; end Elab_Test;

Watch Out for Circular Dependencies package My_Pack is type My_type is range 1_ _000_000; procedure Do_Something (Parameter : in out My_type); end My_Pack; package Other_Pack is type Other_type is range 0.. 1_000; function Do_Something_Else return Other_type; end Other_Pack; with Other_Pack; pragma Elaborate (Other_Pack); package body My_Pack is X : Other_Pack.Other_type := Do_Something_Else; -- Needs Other_Pack body elab... end My_Pack; with My_Pack; package body Other_Pack is X : My_Pack.My_type;... end Other_Pack;