ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP...
Transcript of ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP...
![Page 1: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/1.jpg)
ELTE - PPKE informatika tananyagfejlesztési projekt
TÁMOP-4.1.2.A/1-11/1-2011-0052
![Page 2: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/2.jpg)
Advanced Programming Languages
Zoltán Porkoláb, PhD.
Associate Professor
Eötvös Loránd University, Faculty of Informatics
Dept. Of Programming Languages and Compilers
2013.
![Page 3: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/3.jpg)
The book
• The book: Advanced Programming Languages• To be published in 2013• Editor: Judit Nyéky-Gaizler, PhD• Almost 20 authors, mostly from Eötvös LorándUniversity, Faculty of Informatics• 1080 pages, 17 chapters + Appendix
• The predecessor book: • 2003, in Hungarian
•
![Page 4: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/4.jpg)
The purpose of this training
• The book• Generic concepts in programming languages• Unified terminology• Cross-reference between chapters
• The training• Summarize most important language features• Recap key concepts• Base of programming language class/training
![Page 5: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/5.jpg)
Content (1)
• Language design• Lexical elements• Control structures• Scope and Life• Data types• Composite types• Subprograms• Exception handling
![Page 6: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/6.jpg)
Content (2)
• Abstract data types• Object-oriented programming• Type parameters (Generics)• (Correctness in practice)• Concurrency• Program libraries• Elements of functional programming languages• Logic programming and Prolog• (Aspect-oriented programming)
![Page 7: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/7.jpg)
Language design
![Page 8: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/8.jpg)
Language design
• Key concepts:• Syntax, Semantics, Pragmatics• Implementation• Programming language evolution • Language categories• Language design• Standardization
![Page 9: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/9.jpg)
Syntax, Semantic, Pragmatics
• Syntax• The correct grammar of the language
• Semantic• The meaning of a syntactically correct phrase
• Pragmatics• How to use the given phrase for a useful purpose
![Page 10: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/10.jpg)
Implementation
•Compilation• Phases: (Preprocessing), Compiling, Linking• Static or dynamic linking• Generates HW and OS-specific executable • Effective optimizations
• Interpretation• Faster developing process • Less correctness-checking possibilities
![Page 11: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/11.jpg)
Implementation
• Hybrid model• Compiler generates platform independent intermediate code• Intermediate code executed by “virtual machine”• Fair correctness checking and optimization • More optimization: Just-in-time compilers
• Samples• Pascal P-code, Java virtual machine, MS IL
![Page 12: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/12.jpg)
Evolution of the programming languages
• Early attempts• Computation of Bernoulli Numbers for the Analytical Engine – notes from Ada Lovelace • Plankalkül (Zuse, 1943-45) – relational algebra• Hard-wired machines (1940 - )
• Raising the abstraction level• Machine code (1945-50)• Assembly (1950-)
![Page 13: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/13.jpg)
Evolution of the programming languages
• Early high level languages• FORTRAN (1956) – Math expressions• LISP (1957) – Functional• ALGOL (1958-60) – First block structure• COBOL (1960-) – Detailed data description• PL/1 – Union of all existing features• Basic – (Kemény), Simplification for education
![Page 14: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/14.jpg)
Evolution of the programming languages
• New directions for better abstraction• Simula 67 (1967) – First OO language• Algol 68 (1968) – More precise specification • Pascal (1970) – Educational purposes• C (1971) – HW abstraction for system programming• Smalltalk (1971-) – Pure OO language• Prolog – First Logic programming language• ML – Statically typed functional language
![Page 15: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/15.jpg)
Evolution of the programming languages
• Towards better modularization• Modula-2 (1978) – (Pascal) Better modularization• ADA (1977-) – Programming safety critical systems • C++ (1980) – C + Simula 67 + Algol 68• Oberon (1986) – Modula 2++• Objective C – Object based, dynamic
![Page 16: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/16.jpg)
Evolution of the programming languages
• Towards faster development – Scripting languages• Perl, Python, Ruby, PHP (1985-)
• … and hybrid languages• Java - easy to use and deploy (virtual machine)• C#• Scala
• Just now: towards many-core & multicore systems• OpenCL, Go, ...
![Page 17: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/17.jpg)
Programming language classification
• Imperative (procedural) • Applicative (functional)• Rule-based or logic• Object-oriented
• Object-based, class-based• Concurrent• Scripting (dynamic)
![Page 18: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/18.jpg)
Language design
concepts• Well-defined syntax and semantics• Expressivity• Orthogonality• Generality• Modularity• Portability • Easy to learn • Performance
![Page 19: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/19.jpg)
Lexical elements
![Page 20: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/20.jpg)
Lexical elements
• Key concepts:• Compilation units• Lexical elements, character sets• Delimeters, strict and non-strict format languages• Identifiers• Keywords, reserved words• Literals (number, character, string, …)• Comments
![Page 21: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/21.jpg)
Control structures
![Page 22: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/22.jpg)
Control structures
• Key concepts:• Sequences• Transfer of control
• Conditional • Unconfitional
• Subprogram (function, subroutine) call• Return from subprogram • (Exceptions)
![Page 23: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/23.jpg)
Representing the control structure
• Sentence-like descriptions• Flow diagrams• D diagramss• Block diagrams• Structograms
![Page 24: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/24.jpg)
Imperative (procedural) languages
Abstraction of von Neumann computer• Variables representing the program state
• Assignment = change state• Execution is a sequence of state transitions• Procedures: nesting state + state transition
![Page 25: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/25.jpg)
Declarative and Functional languages
No method of execution is specified• Specification of the problem to solve
• SQL, Prolog• In functional languages
• The problem specification is to solve a (pure) function• Input/output is considered as „side effect”
![Page 26: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/26.jpg)
Assignment
• Statement in earlier languages• Expression in modern languages
• Can be nested• Has return value• User may redefine semantics (C++ operator=)
• Some languages (CLU) has multiply assignment• Implicit conversions are involved
![Page 27: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/27.jpg)
Unconditional transfer of control
• Go To statement • …is considered harmful (Edsger W. Dijkstra, 1968)• Sometimes still used in modern languages
• Break and continue in C• Return from the middle of a subprogram
• Call of subprogram and return • Recursion
![Page 28: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/28.jpg)
Conditional transfer of control
• Arithmetic GOTO in Fortran• Branching structures
• If, elif, else • Dangling if
• Switch/case• Default case
![Page 29: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/29.jpg)
Loops
• Loops on condition expression• While, do-while, for(expr1;expr2;expr3) in C
• Iteration over an integer range• Iteration over a value range
• Foreach in C#, for (expr) in C++• Iteratiors
• Abstraction over control structure
![Page 30: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/30.jpg)
Scope and Life
![Page 31: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/31.jpg)
Scope and Life
• Key concepts:• Scope
• Identifyer, declaration, definition• Block structure, visibility
• Life (or lifetime)• Construction, destruction• Garbage collector, Memory leak• RAII, Smart pointers
![Page 32: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/32.jpg)
Scope and Life
• Scope• Static feature – compilation time• The mapping between names (identifiers) and program objects (types, functions, variables)
• Life (or lifetime, life span)• Dynamic feature – during runtime• The time between the creation and destruction of the object
• Related, but not identical features
![Page 33: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/33.jpg)
Scope types
• The entire program • perhaps more compilation units
• One compilation unit• A type or class• A namespace • A subprogram• A block of code
![Page 34: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/34.jpg)
Scope rules
• Scopes may overlap• Internal scope hides external one• In some languages: syntax error
• Important difference between• Hiding (of a name in external scope)• Overriding (of virtual function)• Overloading (between functions with same name but different signature)
![Page 35: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/35.jpg)
Life, lifetime, life span
• Static• Memory allocated at the beginning of program • Memory deallocated at the end of the program
• Automatic• Memory allocated when control enters the block • Memory deallocated when leaving the block
• Dynamic• Allocation controlled by the programmer• Deallocating manually or by garbage controller
![Page 36: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/36.jpg)
Static life
• Memory allocated at the beginning of the program• Life keeps until the end of the program• Nasty details:
• Java: construction when loading the class• C++: no creation order between compilation units• C++: Static initialization issue, when constructors of static variables refers to each other. Use Singleton pattern!
![Page 37: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/37.jpg)
Automatic life
• To reuse memory between disjunct subprograms• Objects are allocated in the stack• Mostly used for local variables and temporaries• Objects constructed at entering the code block where the object is declared (in declaration order)• Objects are destructed when leaving the block• Nasty details:
• Sometimes we have reference to variable after automatic life finished (e.g. return pointer to it)
![Page 38: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/38.jpg)
Dynamic life
• Construction and destruction (mostly) controlled by the programmer• Objects are allocated in the heap/free memory on programmer request• In some languages deallocation is on request• In other languages: garbage collection
• Difference between destruction and finalization• Heap operations are very slow in-memory activities
![Page 39: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/39.jpg)
Dynamic life
• Memory leak: when allocations and deallocations do not match
• Can happen even with garbage collection• Usually happen when no garbage collection• Throwing exceptions is a typical source of issue
• RAII – Resource allocation is initialization• C++ smart pointers using RAII
• Ownership or reference counting strategy
![Page 40: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/40.jpg)
Data types
![Page 41: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/41.jpg)
Data types
• Key concepts:• What is a data type• Specification and realization
• Invariants • Type system
• Strongly typed, graduate typed, typeless • Type inference
• Type conversions
![Page 42: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/42.jpg)
Data type categories
• Primitive/built-in types• Scalar types
• Integral types • Floating point types• Characters• Enumerations
• Pointers• Pointers to objects, pointers to subprograms
![Page 43: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/43.jpg)
Composite types
![Page 44: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/44.jpg)
Composite types
• Key concepts:• Abstract constructions
• Cartesian product types• Union types• Iterated types
• Type equivalence• Name equivalence• Structure equivalence• Declaration equivalence
![Page 45: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/45.jpg)
Cartesian product
• Type-value set: T1 ᵡ T2 ᵡ T3 ᵡ … ᵡ Tn• Widely supported in languages: record, struct, ...
• Operations• Type/Field selection• Assignment• Equality check
• Language specific• Variadic record• Default values
![Page 46: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/46.jpg)
Union
• Type-value set: T1 ᴗ T2 ᴗ T3 ᴗ … ᴗ Tn• Less support in languages: union, variant• Some OO languages use inheritance instead• Tagged or free union
• Operations• Type/Field selection• Assignment• Type selection (in some languages)
![Page 47: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/47.jpg)
Iterated types: Array
• Type-value set: T ᵡ T ᵡ T ᵡ … ᵡ T• Full support in languages: array, ... • Length may variadic (given at runtime) or static• In some languages arrays „know” their lenght
• Operations• Selection based on index value• Assignment is not fully supported• In C special relation between pointers and arrays
![Page 48: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/48.jpg)
Iterated types: Set
• Type-value set: 2T • Partial support in (mostly Pascal-like) languages• Otherwise implemented as library type
• Operations• Assignment• Equality check• Set operations (push, pop, has, ...)
![Page 49: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/49.jpg)
Iterated types: Set
• Type-value set: 2T • Partial support in (mostly Pascal-like) languages• Otherwise implemented as library type
• Operations• Assignment• Equality check• Set operations (push, pop, has, ...)
![Page 50: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/50.jpg)
Other iterated types
• Hashtables• Key-value pairs• Mostly in script languages (Perl)• Otherwise implemented as library type (C++, Java)
• Multisets/bags• Key-counter pairs• Usually implemented as library type
![Page 51: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/51.jpg)
Subprograms
![Page 52: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/52.jpg)
Subprograms
• Key concepts:• Formal and actual parameters • Parameter passing methods• In, out, and in-out parameters • Overloading• Subprograms as parameters• Corutines
![Page 53: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/53.jpg)
Subprograms
• Reusing existing code parts (since Babbage!)• Positive effect on code quality
• Reusablility• Readability • Changeability• Maintainability
• Procedures and functions
![Page 54: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/54.jpg)
Subprogram structure
• Function signature• Name• Parameter list
• Formal parameters: at subprogram definition• Actual parameters: at subprogram call
• Const, volatile modifiers are part of the signature• Return value (for functions)• Usually a single entry point (exceptions, like PL/I)• Potentially multiply return points
![Page 55: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/55.jpg)
Calling subprograms
• Explicit call statement with keyword, like CALL f()• Just write a call expression, like x = f()• Actual parameters match with formal parameters
• Either prameters matching by name• Or parameters are passed using formal name
• Default parameters (if any) are evaluated at calling site• In some languages () can be omitted at calls with no actual parameter
![Page 56: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/56.jpg)
Parameter list
• Sometimes we have variadic parameter list• printf( const char *fmt … )
• Sometimes we have default parameters• void f(int x = 1)
• Sometimes we overload functions on parameters• void f(double x) and void f(int x)
• Sometimes we overload on modifiers:• void F( int* x) and void F(const int* x)
• In OO languages we pass hidden parameter “this”
![Page 57: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/57.jpg)
Parameter passing
by value • Actual parameters are copied into the subprogram• Formal parameters acting like local variables• Best separation of caller and callee
• Formal parameter identifies different memory area than the actual parameter, changing them has no effect on caller • Parameters transfer information only into callee• Only return value transfer information to caller
• Out parameters can be simulated by passing pointers•
![Page 58: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/58.jpg)
Parameter passing
by reference (address) • Actual parameter addresses are used in subprogram• Formal parameters acting like global variables• Weak separation of caller and callee
• Formal parameter identifies the same memory area than the actual parameter, changing them has permanent effect on caller • Parameters may transfer information in and out
• Issues when actual parameter is an expression not identifying a memory area, like: CALL F(k+1)•
![Page 59: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/59.jpg)
Parameter passing
by result• Modification of pass by value for implementing output parameters• Actual parameters are copied into the subprogram• Formal parameters acting like local variables• When the subprogram returns, value of formal parameters are copyed back to actual parameters• Good separation of caller and callee
• Modification of formal parameters has no effect to caller until subprogram returns
![Page 60: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/60.jpg)
Parameter passing
by name• Actual parameters not identifying any memory region (expressions, like 3+4) are passed by value• Actual parameters referring to memory region (expressions, like 3+t[i]) are passed by address
• Every time a formal parameter is referred during subprogram execution, the expression specified as actual parameter is re-evaluated.
• Weak separation of caller and callee• Modification of actual parameters has immediate effect to the subprogram execution
![Page 61: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/61.jpg)
Textual substritution of parameters
• Used mainly in simple scripting languages and macros• Weak separation of caller and callee
• No separation of caller and callee• Dangerous side-effects may happen, like
• Multiply evaluation • Precedence hijacking
![Page 62: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/62.jpg)
Overloading
• The same name can denote multiply subprograms• Compiler selects the appropriate subprogram
• Overload resolution happens in compile-time• Compiler flags compile error if unable to select
• Overloading happens on • Number of parameters• Types of parameters • Modifyers (like const and volatile)
![Page 63: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/63.jpg)
Operator overloading
• Operators – when programmer can define them – are acting like functions with special names• ADA, C++, C# allows operator overloading, Java not
• Arity and precedence of operators are usually fixed•
![Page 64: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/64.jpg)
Passing subprograms as parameters
• Subprograms are first calss citizens in functional programming languages
• They can be passed as parameters• In imperative and OO languages this is not general
• Funtion pointers in C/C++• Function objects and lambda functions in C++• Modula-3: closure carrying the environment where the subprogram will be executed
![Page 65: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/65.jpg)
Corutines
• Corutines are subprograms executed in a symmetric control model instead of caller-callee distinction
• The corutin can pass back the control without finishing its task – dispatch • Local variables are kept alive• Corutin can resume its execution from the place where it was last dispatced
• They mimic parallelism – sometimes decreaseing program complexity
![Page 66: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/66.jpg)
Exception handling
![Page 67: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/67.jpg)
Exception handling
• Key concepts:• Handling runtime errors• Separation of error handling concepts• Exception safety• Checked and unchecked exceptions• Grouping exceptions• Polymorphic exceptions
![Page 68: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/68.jpg)
Basic concepts
• Runtime errors break normal execution flow• Error handling code crosscuts normal execution• Separation of error handling is welcome
• Exception may raised (throw, signal) by SW or HW• Exceptions are catched by specific handlers• Exceptions can carry information from the site of error to the place where we can handle it
• In modern languages exceptions are objects of arbitrary type – we can define our type too
![Page 69: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/69.jpg)
Exception safety
• As exceptions break the normal flow of execution• Object can be left in undefined state• Resources may not be deallocated• This can cause inconsistency, like memory leak
• In Java and C# finally blocks are executed before the control leaves a block• In C++ RAII is used to avoid memory leak
• Nothrow, strong, and basic garantees
![Page 70: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/70.jpg)
Checked exceptions
• An exception throable from a subprogram or class is part of the interface of that module• Checked exceptions in Java
• Static (compile-time) checking whether the exception has been properly handled• There are non-checked run time exceptions too
• In C++ the compiler has less possibility for checks• In C++11 there is a __nothrow attribute and operator which is evaluated in compile time
![Page 71: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/71.jpg)
Grouping exceptions
• In modern OO languages exceptions are common objects from arbitrary types • Custom types can be defined for custom exceptions• Inheritance hierarchy can be used to group exceptions
• The handler of base class catch derived exceptions• Always throw the most derived type of exception• Exception objects can be polymorphic
![Page 72: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/72.jpg)
Abstract Data Types
![Page 73: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/73.jpg)
Abstract Data Types
• Key concepts:• Modular design• Language support for modularity• Representation hiding• Separation of specification and implementation• Generalized program schemes
![Page 74: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/74.jpg)
Modular design
• In modern programming languages modular design is a key concept.• Criteria of modular design:
• Modular decomposition• Modular composition• Modular intelligibility• Modular continuity• Modular protection
![Page 75: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/75.jpg)
Modularity
• Language support of modules• Few interconnections• Weak interconnections• Explicit interfaces• Information hiding• Open and closed modules
• Open: extendable (like C++ namespaces) • Closed: reachable via interface, used unchanged
![Page 76: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/76.jpg)
Modularity
• Reusability • Variety of types• Variety of data structures and algorithms• One type – one module• Representation independence
![Page 77: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/77.jpg)
Language Support for Modularity
• Procedural languages• C: #include • Modula-3: modules, export, import, generic• Ada: package – open modules support
• Functional languages• Signature description in ML, can be reused
• By embedding• By specialization
![Page 78: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/78.jpg)
Object-oriented languages
• Address open/closed modules with inheritance • Package (Java as example)
• Package visibility• Import, imported names should be fully qualified
• Single-type import• On-demand import
•
![Page 79: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/79.jpg)
Representation hiding
• Opaque type• Handlers in C, Pascal• ADA: private type• CLU: abstract data types
• Visibility levels• Private• Protected• Public
•
![Page 80: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/80.jpg)
Specification and Implementation
• Enables the modules to be developed separately• Supports modification in implementation• Implementation can be delivered in binary • Supports separation:
•C and C++ header files / source files separation• Mapping to pointers / C++ PIMPL strategy
• Not supports physical separation• Eiffel, Java
![Page 81: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/81.jpg)
Generalized program schemes
• Data parameters• Type parameters: Generic, Template
• Type erasure• Instantiation
• Subprograms as parameters• Function pointers, functors, lambdas
• Higher level structures as parameters• Scala
![Page 82: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/82.jpg)
Object-oriented programming
![Page 83: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/83.jpg)
Object-oriented programming
• Key concepts:• Class and Object• Constructing and destructing objects• Encapsulation• Data hiding, interfaces• Class (static) data and method• Inheritance• Polymorphism
![Page 84: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/84.jpg)
Class and Object
• Object• Independent units of the reality we model• Inner state (represented by attributes)• Response for the messages received (behavior)• Each object has its identity
• Class• Group of objects with similar attributes and behavior • Each object is an instances of a class
![Page 85: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/85.jpg)
Construction, destruction
• Life cycle of objects: born, live, die• Construction
• Set the initial state to fulfill type invariant • Constructor: usually a public method
• Destruction• Sometimes need to clean-up resources • Destructor (C++)• Finalize method (called by Garbage Collector)
![Page 86: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/86.jpg)
Encapsulation
• Considering data structure and the operations on it as a single unit and hide them from outer word• Specification gives the outer description
• Value-set of the type • Behavior: methods
• Implementation• Data representation • Method implementation
![Page 87: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/87.jpg)
Data hiding, interfaces
• Encapsulation means objects hides implementation• To communicate with the outside word: interface
• Interface: previously defined set of messages• Object can be touched only using the interface • Interface should be minimal
• Visibility• Public, protected, private• (C++) friends, (Eiffel) selective access
![Page 88: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/88.jpg)
Class (static) data and methods
• Normally we work with objects and their methods • For such instance methods we pass Self/This
• Sometime we use data and methods not connected to any objects – called class (static) data and method
• Cannot access instance members • Cannot call instance methods (not receiving Self)
• C++, C#, Java: static members, static methods• Scala: object – a singleton
![Page 89: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/89.jpg)
Inheritance
• Generalization and specialization• Inheritance
• Super-class (base) and sub-class (derived)• Derived inherits base attributes and methods• New attributes and methods can be added• Cannot access instance members • Cannot call instance methods (not receiving Self)
• Inheritance can be single and multiply• Some lang multiply inheritance only for interfaces
![Page 90: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/90.jpg)
Inheritance types
• Derived class access public and protected of base • Inheritance normally extend base interface
• Java keyword: extend• In C++ there are three kind of inheritances
• Public: extend interface as Java • Protected: convert base interface to protected• Private (default): Hide the interface of base
![Page 91: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/91.jpg)
Polymorfism
• Extending base interface: substitutability• Liskov Substitution Principle
• Subclass objects can appear in place of super• Static and dynamic (run-time) type of object
• Polymorphism: in OO = subtype polymorphism • Methods can be redefined in subclass
• Static redefinition (based on static type)• Dynamic redefinition (based on run-time type):
• Virtual functions, dynamic dispatch
![Page 92: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/92.jpg)
Abstract class, interface
• Representing common abstractions• No objects are instantiated from abstract class
• Common data for all subclasses• Protocol: for redefine in subclasses
• C++ abstract class• Pure virtual
• Java • Interface: no data • Abstract class
![Page 93: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/93.jpg)
Multiple inheritance
• Mostly for unifying multiply interfaces• In many languages only for interfaces
• Java, C#• In C++ works by default
• Resolving name conflict with scope operator• Diamond-shape inheritance
• Common base class inherited multiply times • Scala traits
![Page 94: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/94.jpg)
Type parameters
![Page 95: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/95.jpg)
Type parameters
• Key concepts:• Control and data abstraction• Taxonomy of Polymorphism • Generic contract model• Instantiation• Type erasure
![Page 96: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/96.jpg)
Abstraction
• Control Abstraction• Procedural (C): function pointers• Object-oriented (Java): classes with “doit” func.• Generic (C++): functors – function objects
• Data abstraction• Type parameters• Opaque types
![Page 97: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/97.jpg)
Taxonomy of Polymorphism
• Universal• Parametric• Inclusion
• Ad-hoc• Overloading• Coercion
![Page 98: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/98.jpg)
Generic Contract Model
• Constraining type parameters for data or method• Constrained generics
• Java – supertype, subtype relationships• Ada – with clause
• Unconstrained generics• C++ templates – no restriction on type parameter• C++14 Concepts (light)
• Type class in functional languages
![Page 99: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/99.jpg)
Instantiation
• Actual type parameter substitues formal parameter• New code generated – instantiation
• On demand (C++)• Manually (ADA)
• User defined specialization in C++ • Template metaprogramming – executing algorithm in compile-time
![Page 100: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/100.jpg)
Type erasure
• Actual type parameter substitued by common heir• Most famously used in Java • Only one code serves all type parameters• Compiler ensures type safety on back-conversion• Auto-boxing unboxing for built-in types• No specialization
![Page 101: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/101.jpg)
Concurrency
![Page 102: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/102.jpg)
Concurrency
• Key concepts:• ”The free lunch is over”• Why to write concurrent applications?• Amdahl's law• Syncronization• Process vs. Thread• Sample: MPI and Java
![Page 103: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/103.jpg)
The free lunch is over
• Moore's law• Number of transistors • Processing speed
doubles in every 24 month• Free lunch: the same program will run faster by time• Trend on speed has broken:
• The free lunch is over (Herb Sutter, 2004)• Make programs faster should utilize concurrency
![Page 104: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/104.jpg)
Area of concurrent programming
• Responsive user interfaces• Multi-user server solutions incl. databases• Web servers, and web browsers• Multicore systems to utilize all resources• Scalable applications• More intuitive programming schema
![Page 105: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/105.jpg)
Popular fallacies on Concurrency
• If it is concurrent it is quicker.• Program structure does not matter.• Easier to write a sequential prototype and then rewrite it as a parallel version.• I do not need to care concurrency.• Concurrency is easy. At least easy to debug if I make mistakes.
![Page 106: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/106.jpg)
Amdahl's law
The performance gain by paralellizing an application is heavily bounded by the ration of concurrently executable parts that are independent of each other
![Page 107: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/107.jpg)
Data and Instruction
• Single instruction• Single Data Stream SISD• Multiply Data Stream SIMD
• Multiply Instruction• Single Data Stream MISD• Multiply Data Stream MIMD
![Page 108: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/108.jpg)
Syncronization
• Deadlocks• happen if all these conditions state (Coffman conditions):
• Mutual exclusion• Hold-and-wait locking• No preemption• Circular dependencies
• Starvasion
![Page 109: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/109.jpg)
Syncronization techniques
• Critical section• Set of instructions where the execution is restricted to a single thread/process.• Entry protocol, exit protocol
• Busy waiting• Semaphore• Monitor• Conditional critical section
![Page 110: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/110.jpg)
Concurrent execution units
• Process vs thread• Process
• Own address space • Expensive to start and switch
• Thread• Own execution thread, shared address space• Thread local memory - own stack• User / system thread
![Page 111: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/111.jpg)
Sample: MPI
• Message Passing Interface – language independent • Inter-thread point-to-point communication• Handle tasks in groups
• Creating tasks• Communication methods• Communication in group
![Page 112: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/112.jpg)
Sample: Java
• Runnable interface and Thread class• Thread groups• Concurrent API• Concurrent collections• Executor framework• Thread pools• Syncronized
![Page 113: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/113.jpg)
Program libraries
![Page 114: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/114.jpg)
Program libraries
• Key concepts:• Requirements against libraries• Procedural programming• Object-oriented programming• Generic programming
![Page 115: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/115.jpg)
Library requirements
• Correctness• Efficiency• Reliability• Extensibility and maintainability• Reusability• Portability
![Page 116: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/116.jpg)
Procedural library design
• Set of type definitions and functions• Separation of interface and implementation• Most frequently tasks are covered:
• Standard I/O: file handling• Memory manipulation• String library• Mathematical functions
![Page 117: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/117.jpg)
Object-oriented library design
• Class hierarchy• Optimal service size < 80• Abstract types with interface
• Fat vs. narrow interfaces• Handle classes, proxy classes
• Expression problem: • Easy to extend with new data (class)• Hard to extend with new service • Visitor pattern
![Page 118: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/118.jpg)
Generic programming library design
• First introduced in C++ as STL (Stepanov&Musser)• Orthogonal separation of data and algorithm• Containers (parametrized by types)• Algorithms without knowing data representation• Iterators connecting containers to algorithms
• Hierarchy of iterators• Functors • Adaptors
![Page 119: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/119.jpg)
Functional Programming
![Page 120: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/120.jpg)
Functional Programming
• Key concepts:• Mathematical foundation: Lambda calculus• Structure: function definitions, starts expression • Referential transparency• Pattern matching
![Page 121: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/121.jpg)
Mathematical foundation
• Lambda calculus (Church 1932-33)• Equivalent to Turing Machine
• Evaluation of (mathematical) functions• First implemented: LISP (1957)
![Page 122: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/122.jpg)
Functional program structure
• Type, type class and function declarations• Program execution = evaluate start expression • Rewriting system: rewriting start expression determined with the computation model• Pure (side-effect free) functions• Referential transparency (no assignment) • Strong static typing• Pattern matching
![Page 123: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/123.jpg)
Functional program structure
• Recursive function application• Currying, partial function application• Higher order functions• Evaluation strategy
• Lazy evaluation• Eager, strict evaluation
• List comprehension
![Page 124: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/124.jpg)
Logic Programming
![Page 125: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/125.jpg)
Logic Programming
• Key concepts:• Algorithm = Logic + Control• Logic programs• Prolog
![Page 126: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/126.jpg)
Logic programming
• Roots: Automated theorem prover (suggested by Hilbert) has two components:
• Logical (declarative) description of problem• Control component of deduction or computation
• Axiom:• Fact• Rule (incl. recursive rules)
• Search trees
![Page 127: ELTE - PPKE informatika tananyagfejlesztési projekt TÁMOP ...aszt.inf.elte.hu/~gsd/programming_languages.pdf · • Static feature – compilation time • The mapping between names](https://reader033.fdocuments.in/reader033/viewer/2022041619/5e3df49921726a38a96b9caf/html5/thumbnails/127.jpg)
Prolog
• Robert Kowalski (~1970): Theoretical foundations• Alain Colmerauer (1972): PROLOG
• Implemented first as an interpreter• David Warren (~1976): First effective compiler• Data structure: terms
• Set of predicates and a query or goal• The prolog machine