CA341 - Comparative Programming Languages Introduction
Total Page:16
File Type:pdf, Size:1020Kb
Introduction CA341 - Comparative Programming Languages Introduction Dr. David Sinclair Introduction Overview This module will examine the essential concepts on which modern programming languages are based, to understand the design decisions in current languages and the design features that may be introduced into future languages. In this module we will cover: • Data Types and Scope • Pointers and Memory Management • Abstraction • Control • Data Abstraction • Functions, Parameter Matching and Parameter Passing • Assertions and Exceptions • Object-Oriented Programming Paradigm • Memory Models • Logic Programming Paradigm • Functional Programming Paradigm Introduction Texts Supplementary: • Robert V. Sebesta 2015, Concepts of Programming Languages, 11 Ed., Pearson [ISBN: 9780133943023] • Michael L. Scott 2015, Programming Language Pragmatics, 4 Ed., San Diego; Morgan Kaufmann [ISBN: 9780124104099] • David A. Watt 2004, Programming Language Design Concepts, John Wiley & Sons [ISBN: 978-047085320] • John C. Mitchell 2003, Concepts in programming language, Cambridge University Press New York [ISBN: 978-052178098] • M. Ben-Ari 1996, Understanding Programming Languages, John Wiley & Sons [ISBN: 978-047195846] Introduction Contact Details Lecturer: Dr. David Sinclair Office: L253 Phone: 5510 Email: [email protected] WWW: https://www.computing.dcu.ie/∼davids Course web page: https://www.computing.dcu.ie/∼davids/courses/CA341/CA341.html Introduction How do I successfully complete this module? The module mark is a straight weighted average of: • 30% continuous assessment • 2 assignments • first assignment: Object-Oriented Programming (15%) • second assignment: Language Comparison (15%) • 70% end-of-semester examination • Do 5 out of 6 questions. Introduction A Very Brief History of Programming Languages Prolog ’72 Scheme Common LISP ’75 ’84 Caml OCaml ’85 ’96 ML SML Lisp ’84 ’90 ’62 Haskell Smalltalk ’90 Snobol4 ’80 ’62 Java Scala Simula67 CLU ’95 ’04 APL ’67 ’77 ’60 C++ ’84 C# Algol68 C ’00 ’68 ’72 Objective-C Swift Algol60 ’84 ’14 ’60 CPL BCPL B ’63 ’66 ’69 Python Python 2 Python 3 Plankalkul scripting language ’91 ’00 ’08 ’48 ’77 Autocode Fortran Pascal Ada Perl Go ’52 ’57 PL/1 ’71 ’80 ’90 ’09 ’64 Concurrent Pascal Oberon BASIC ’75 ’87 B/0 Flowmatic COBOL ’64 Modula-2 Modula-3 ’57 ’58 ’60 ’78 ’89 Introduction Significant Features Autocode was developed for the Manchester One and was the first language with a compiler. Fortran was the first widely adopted high-level language. It introduced arrays, symbolic expressions and procedures with parameters. Algol60 introduced the concept of block structure where variables and procedures can be defined anywhere. It also introduced recursive procedures. Cobol was developed primarily for business data processing applications. It introduced the concept of data descriptions which, in later languages, evolved into data types. Introduction Significant Features (2) Children of Algol60: BASIC popularised programming with its simple structure and efficient implementations that made it the first programming language distributed with the initial PCs. Simula67 was designed for discrete-event simulation problems. It introduced a simple form of parallel execution and classes with hierarchies. Pascal was designed as a language to teach structured programming. The original language is characterised by simplicity. It has since been extended to include modularisation and object-orientation. Also featured an intermediate code, P-code. Algol68 is the successor to Algol60 and its design was driven by the principle of orthogonality (language features can be composed freely and uniformly with predictable effects). Introduction Significant Features (3) The “alternatives”: Lisp in its pure form, was the first functional language. It is based on the lambda calculus and the theory of recursive functions. It has only 1 data structure, the list, and Lisp programs are written as lists. ML is a functional language that demonstrated that a language could be computationally powerful while still retaining the ability to prove some properties about the program without executing it. It has a type inferencing system that guarantees that a well-types ML program will not cause runtime type errors. Haskell is a “pure” functional programming language with lazy evaluation. Prolog is a declarative language in which the programmer describes the properties of a solution and the runtime system searches for the answer (a binding of values to variables). Introduction Significant Features (4) The “modern” “imperative” languages: Ada was a language developed to replace over 450 different languages used by the U.S. Department of Defence covering the range from information systems to embedded systems. It includes support for concurrency and object-orientation. C was developed as part the development of UNIX. It is very efficient for systems programming. Smalltalk was first major implementation of object-orientation and reflective programming. It is dynamically typed. C++ is a general-purpose programming language adding object-oriented and generic programming features to C, while “retaining” low-level memory manipulation. It is biased toward system programming for both resource-constrained and large systems, with performance, efficiency and flexibility of use. Introduction Significant Features (5) Objective-C adds object-orientation and Smalltalk message passing to C. Version 2 added garbage collecting. Java is a portable general-purpose language that supports concurrency and object-orientation. It revived the use of intermediate code with bytecode and the JVM. C# is a multi-paradigm programming language that includes strong typing and supports imperative, declarative, functional, generic, object-oriented, and component-oriented programming. Swift is a general-purpose, compiled programming language based on Objective-C with functional aspects. Swift addresses common programming errors like null pointers, and promotes protocol-oriented programming. Introduction The Perfect Programming Language There are 2 types of Programming Languages: • those that people complain about; and • those that people do not used. Bjarne Stroustrup (C++ inventor) Introduction Syntax The syntax of a language defines how statements are form in a language. It is made up of: • lexical rules that define the alphabet of the language and how these characters are combined to form valid words; and • syntax rules that define how valid words are combined to form valid statements. Typically syntax is defined using Extended Backus-Naur Form (EBNF). EBNF is a context-free grammar that has: Terminals keywords, symbols and characters. Nonterminals are denoted by enclosing “<” and “>” and are composed of terminals and nonterminals. Sequences of terminals and nonterminals, e.g. < name >[< index >]. Introduction Syntax (2) Choice is denoted by “|”. < A > |b represents either the nonterminal A or the terminal b. Repetition is denoted by “*” or “+”. A* represents zero or more occurrences of A. B+ represents one or more occurrences of B. Recusion is where a rule is defined in terms of itself, e.g. < expr > ::= < expr >< operator >< expr >. The syntax of a simple programming language could be defined as follow. Lexical rules: < operator > ::= + | - | * | / | == | != | < | > | <= | >= < identifier > ::= < letter > | < Id >* < Id > ::= < letter > | < digit > < number > ::= < digit >* < letter > ::= a | b ... z Introduction Syntax (3) Syntax rules: < program > ::= { < statement >* } < stattement > ::= < assignment > | < conditional > | loop < assignment > ::= < identifier > = < expr > < conditional > ::= if < expr > { < statement >+ } | if < expr > { < statement >+ } else { < statement >+ } < loop > ::= while < expr > { < statement >+ } < expr > ::= < identifier > | < number > | < expr > | < expr > < operator > < expr > Introduction Semantics Semantics defines the meaning of a program. Not all syntactically correct programs have a valid meaning. For example the C program, if (a > b) max = a; else max = b; has a valid meaning if a and b are ints. However, if a is an identifier of an int and b is the identifier of a function, then the syntactically correct statement has no valid meaning. Additionally, if the meaning of a syntactically correct program can be verified before the program is executed, then these rules define the static semantics of the program. We will focus on dynamic semantics that describe the effects of executing the constructs of the programming language. A program can only be executed if the program is correct with respect to its syntax and static semantics. Introduction Semantics (2) A formal semantics defines a language in terms of mathematical concepts and provides a rigorous and unambiguous meaning of each element of the language. Two ways of formally specifying the semantics of a language are axiomatic semantics and denotational semantics. Axiomatic Semantics Axiomatic semantics defines the execution of a program in terms of a state machine. The state of a program is described by a set of first-order predicates that define the property of each value of the program’s variables in a state. The axiomatic semantics defines the meaning of each program construct by relating the state of the program before the execution of the construct to the state of the program after executing the construct. Introduction Semantics (3) A predicate P that is required to hold after the execution of a statement S is called a postcondition. A predicate Q that holds before the execution of a statement S, guarantees the termination