1 1 © AdaCore under the GNU Free Documentation License Franco Gasperoni

Slides:



Advertisements
Similar presentations
1 © AdaCore under the GNU Free Documentation License Franco Gasperoni
Advertisements

Free Beer and Free Speech Thomas Krichel
EPD-EN-01© Crown copyright 2009 Going for gold Literacy Plus unit R1 Resources R1.1 and R1.2 Image of Gold Medal © IOC/Olympic Museums Collections.
Template. 2 Using templates, it is possible to create generic functions and classes. In a generic function or class, the type of data upon which the function.
Slide: 1 Copyright © AdaCore Packages Presented by Quentin Ochem university.adacore.com.
Lecture 16 Subroutine Calls and Parameter Passing Semantics Dragon: Sec. 7.5 Fischer: Sec Procedure declaration procedure p( a, b : integer, f :
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.
The Importance of Open Source Software Networking 2002 Washington, D.C. April 18, 2002 Carol A. Kunze Napa, California.
Generic programming Define software components with type parameters –A sorting algorithm has the same structure, regardless of the types being sorted –Stack.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
1 Lab Session-3 CSIT221 Spring 2003 b Group Worksheet 3 Exercise (Demo Required) b No new lab demo will be assigned to allow you to focus on HW#1.
Wednesday, 10/9/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 10/9/02  QUESTIONS ??  Today:  Discuss HW #02  Discuss test question types  Review 
1) More on parameter passing: a) Parameter association b) Default parameters c) Procedures as parameters 2) Modules: Ada packages, C modules, C header.
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
Generic programming Define software components with type parameters –A sorting algorithm has the same structure, regardless of the types being sorted –Stack.
Open Source/Free Software Source code is available Extensible Can be changed, modified Freely distributed Copies Modified versions Alternatives to commercial/proprietary.
Structure of Ada Programs Building a program from components Programming Languages Spring 2004.
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
Generic programming Define software components with type parameters –A sorting algorithm has the same structure, regardless of the types being sorted –Stack.
Abstract Data Types and Encapsulation Concepts
Screen Clipping. Screen 3 rd Party Software Print Screen Copying images from the internet is easy. But if can also violate the copyright laws of.
ITEC 320 Lecture 16 Packages (1). Review Questions? –HW –Exam Nested records –Benefits –Downsides.
Operating Systems AOIT Principles of Information Technology.
Chapter Nine: Subprograms Lesson 09. What are they  Modularized code  Might return a value  Functions  Or not  Procedures  Subroutines  In object.
Copyright 2007 © The OWASP Foundation Permission is granted to copy, distribute and/or modify this document under the terms of the OWASP License. The OWASP.
CS 106 Introduction to Computer Science I 04 / 20 / 2007 Instructor: Michael Eckmann.
PlanetSim Release 3.0 Candidate in depth Jordi Pujol Ahulló Universitat Rovira i Virgili
Chapter 9: MuPAD Programming II Procedures MATLAB for Scientist and Engineers Using Symbolic Toolbox.
/* Documentations */ Pre process / Linking statements Global declarations; main( ) { Local Declarations; Program statements / Executable statements; }
Copyright © 2002, Systems and Computer Engineering, Carleton University a-JavaReview.ppt * Object-Oriented Software Development Unit.
Copyright © The OWASP Foundation Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation.
ITEC 320 Lecture 10 Packages (2). Review Packages –What parts do they have? –Syntax?
Slide: 1 Copyright © AdaCore Subprograms Presented by Quentin Ochem university.adacore.com.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
© ACT Europe under the GNU Free Documentation License 1 Franco Gasperoni
Introduction to Programming in MATLAB Intro. MATLAB Peer Instruction Lecture Slides by Dr. Cynthia Lee, UCSD is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike.
Open Source Software This permits users to use, change, and improve the software, and to redistribute it in modified or unmodified forms. It is very often.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Abstraction: Procedures as Parameters CMSC Introduction to Computer Programming October 14, 2002.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
1 © AdaCore under the GNU Free Documentation License Franco Gasperoni
© ACT Europe under the GNU Free Documentation License 1 Franco Gasperoni
MIS 3200 – Unit 5.1 Iteration (looping) – while loops – for loops Working with List Items.
(1) ICS 313: Programming Language Theory Chapter 11: Abstract Data Types (Data Abstraction)
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
How to Use The Creative Commons Licenses. [formats]
CMSC 104, Version 8/061L14AssignmentOps.ppt Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips Reading Section.
CSC 221 Computer Organization and Assembly Language Lecture 16: Procedures.
Unified Modeling Language (UML)
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
1 © AdaCore under the GNU Free Documentation License Franco Gasperoni
Localization by TDOA ©Thomas Haenselmann – Department of Computer Science IV – University of Mannheim Lecture on Sensor Networks Historical Development.
Screen Profiles byobu Adding Bling To GnuScreen Presented by: Chuck Frain Ubuntu Maryland Loco Team To CALUG May 13, 2009.
<draft-bradner-rfc-extracts-00.txt>
User-Written Functions
Open Source software Licensing
Intro To Classes Review
A Lecture for the c++ Course
Software Development with uMPS
Chapter 4: Writing Classes
Nuffield Free-Standing Mathematics Activity
APACHE LICENSE HISTORICAL EVOLUTION
Presentation transcript:

1 1 © AdaCore under the GNU Free Documentation License Franco Gasperoni

2 2 © AdaCore under the GNU Free Documentation License Copyright Notice © AdaCore under the GNU Free Documentation License Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; provided its original author is mentioned and the link to is kept at the bottom of every non-title slide. A copy of the license is available at:

3 3 © AdaCore under the GNU Free Documentation License

4 4 © AdaCore under the GNU Free Documentation License We often write similar code... Integer procedure Swap (X, Y : in out Integer) is Integer Tmp : Integer := X; begin X := Y; Y := Tmp; end Integer procedure Swap (X, Y : in out Integer) is Integer Tmp : Integer := X; begin X := Y; Y := Tmp; end Float procedure Swap (X, Y : in out Float) is Float Tmp : Float := X; begin X := Y; Y := Tmp; end Float procedure Swap (X, Y : in out Float) is Float Tmp : Float := X; begin X := Y; Y := Tmp; end

5 5 © AdaCore under the GNU Free Documentation License This is... Time consuming Error prone (cut & paste) Maintenance hazard

6 6 © AdaCore under the GNU Free Documentation License Genericity allows you to parameterize your code generic Some_Type type Some_Type is private; Some_Type procedure Gen_Swap (X, Y : in out Some_Type); generic Some_Type type Some_Type is private; Some_Type procedure Gen_Swap (X, Y : in out Some_Type); Any non limited type without discriminants gen_swap.ads Some_Type procedure Gen_Swap (X, Y : in out Some_Type) is Some_Type Tmp : Some_Type := X; begin X := Y; Y := Tmp; end Gen_Swap; Some_Type procedure Gen_Swap (X, Y : in out Some_Type) is Some_Type Tmp : Some_Type := X; begin X := Y; Y := Tmp; end Gen_Swap; gen_swap.adb

7 7 © AdaCore under the GNU Free Documentation License Instantiating a Generic with Gen_Swap; procedure Client is procedure Swap is new Gen_Swap (Some_Type => Integer); procedure Swap is new Gen_Swap (Some_Type => Float); A, B : Integer := …; P, Q : Float := …; begin Swap (A, B); Swap (P, Q); end Swap; with Gen_Swap; procedure Client is procedure Swap is new Gen_Swap (Some_Type => Integer); procedure Swap is new Gen_Swap (Some_Type => Float); A, B : Integer := …; P, Q : Float := …; begin Swap (A, B); Swap (P, Q); end Swap; Generic instantiation

8 8 © AdaCore under the GNU Free Documentation License Type of Generic Units generic … formal parameters... procedure Proc (…); generic … formal parameters... procedure Proc (…); generic … formal parameters... function Func (…) return …; generic … formal parameters... function Func (…) return …; generic … formal parameters... package Pack is … end Pack; generic … formal parameters... package Pack is … end Pack;

9 9 © AdaCore under the GNU Free Documentation License … formal parameters... Types Objects Subprograms Packages

10 © AdaCore under the GNU Free Documentation License Another example Write a generic function that computes where L & H are integer bounds F is some function returning some type An addition operation is available for this type

11 © AdaCore under the GNU Free Documentation License generic type Res_Type is private; Zero : in Res_Type; with function Add (X, Y : Res_Type) return Res_Type; with function F (I : Integer) return Res_Type; function Sum (L, H : Integer) return Res_Type; generic type Res_Type is private; Zero : in Res_Type; with function Add (X, Y : Res_Type) return Res_Type; with function F (I : Integer) return Res_Type; function Sum (L, H : Integer) return Res_Type; Spec...

12 © AdaCore under the GNU Free Documentation License function Sum (L, H : Integer) return Res_Type is Result : Res_Type := Zero; begin for I in L.. H loop Result := Add (Result, F (I)); end loop; return Result; end Sum; function Sum (L, H : Integer) return Res_Type is Result : Res_Type := Zero; begin for I in L.. H loop Result := Add (Result, F (I)); end loop; return Result; end Sum; Body...

13 © AdaCore under the GNU Free Documentation License with Sum; procedure Client is function Compute (X : Integer) return Integer is … end; function Compute (X : Integer) return Float is … end; function New_Sum is new Sum (Res_Type => Integer, Zero => 0, Add => “+”, F => Compute); function New_Sum is new Sum (Res_Type => Float, Zero => 0.0, Add => “+”, F => Compute); with Sum; procedure Client is function Compute (X : Integer) return Integer is … end; function Compute (X : Integer) return Float is … end; function New_Sum is new Sum (Res_Type => Integer, Zero => 0, Add => “+”, F => Compute); function New_Sum is new Sum (Res_Type => Float, Zero => 0.0, Add => “+”, F => Compute); Instantiating Sum

14 © AdaCore under the GNU Free Documentation License generic type Res_Type is private; Zero : in Res_Type; with function Add (X, Y : Res_Type) return Res_Type; with function F (I : Integer) return Res_Type is <> ; function Sum (L, H : Integer) return Res_Type; generic type Res_Type is private; Zero : in Res_Type; with function Add (X, Y : Res_Type) return Res_Type; with function F (I : Integer) return Res_Type is <> ; function Sum (L, H : Integer) return Res_Type; Default Generic Parameters

15 © AdaCore under the GNU Free Documentation License with Sum; procedure Client is function F (X : Integer) return Integer is … end; function F (X : Integer) return Float is … end; function New_Sum is new Sum (Res_Type => Integer, Zero => 0, Add => “+”); function New_Sum is new Sum (Res_Type => Float, Zero => 0.0, Add => “+”); with Sum; procedure Client is function F (X : Integer) return Integer is … end; function F (X : Integer) return Float is … end; function New_Sum is new Sum (Res_Type => Integer, Zero => 0, Add => “+”); function New_Sum is new Sum (Res_Type => Float, Zero => 0.0, Add => “+”); You can omit F => F You can omit parameter F if there is a visible routine called F with the right parameters

16 © AdaCore under the GNU Free Documentation License Allowed to use “+” “-” “ * ” etc. as function names package Sets is type Set is private; Empty : constant Set; function “+” (S1, S2 : Set) return Set; -- Set union function “*” (S1, S2 : Set) return Set; -- Set intersection function “-” (S1, S2 : Set) return Set; -- Set difference … private type Set is …; end Alerts; package Sets is type Set is private; Empty : constant Set; function “+” (S1, S2 : Set) return Set; -- Set union function “*” (S1, S2 : Set) return Set; -- Set intersection function “-” (S1, S2 : Set) return Set; -- Set difference … private type Set is …; end Alerts;

17 © AdaCore under the GNU Free Documentation License Back to our Generic Sum generic type Res_Type is private; Zero : in Res_Type; with function “+” (X, Y : Res_Type) return Res_Type is <>; with function F (I : Integer) return Res_Type is <> ; function Sum (L, H : Integer) return Res_Type; generic type Res_Type is private; Zero : in Res_Type; with function “+” (X, Y : Res_Type) return Res_Type is <>; with function F (I : Integer) return Res_Type is <> ; function Sum (L, H : Integer) return Res_Type;

18 © AdaCore under the GNU Free Documentation License function Sum (L, H : Integer) return Res_Type is Result : Res_Type := Zero; begin for I in L.. H loop Result := Result + F (I); end loop; return Result; end Sum; function Sum (L, H : Integer) return Res_Type is Result : Res_Type := Zero; begin for I in L.. H loop Result := Result + F (I); end loop; return Result; end Sum; Body...

19 © AdaCore under the GNU Free Documentation License with Sum; with Sets; use Sets; procedure Client is function F (X : Integer) return Integer is … end; function F (X : Integer) return Set is … end; function New_Sum is new Sum (Res_Type => Integer, Zero => 0); function New_Sum is new Sum (Res_Type => Set, Zero => Empty); with Sum; with Sets; use Sets; procedure Client is function F (X : Integer) return Integer is … end; function F (X : Integer) return Set is … end; function New_Sum is new Sum (Res_Type => Integer, Zero => 0); function New_Sum is new Sum (Res_Type => Set, Zero => Empty); You can omit F => F and “+” => “+”