Concepts in Programming Languages Practicalities Main Books
Total Page:16
File Type:pdf, Size:1020Kb
Concepts in Programming Languages Practicalities Alan Mycroft∗ Course web page: www.cl.cam.ac.uk/teaching/1112/ConceptsPL/ h i Computer Laboratory with lecture slides, exercise sheet and reading material. University of Cambridge One exam question. 2011–2012 (Easter Term) (*) I thank Marcelo Fiore for permission to use these slides 1 2 Topics Main books I. Introduction and motivation. J. C. Mitchell. Concepts in programming languages. II. The first procedural language: FORTRAN (1954–58). Cambridge University Press, 2003. III. The first declarative language: LISP (1958–62). T.W. Pratt and M. V.Zelkowitz. Programming Languages: IV. Block-structured procedural languages: Algol (1958–68), Design and implementation (3RD EDITION). Prentice Hall, Pascal (1970). 1999. V. Object-oriented languages — Concepts and origins: ⋆ M. L. Scott. Programming language pragmatics Simula (1964–67), Smalltalk (1971–80). (2ND EDITION). Elsevier, 2006. VI. Types in programming languages: ML (1973–1978). R. Sethi. Programming languages: Concepts & constructs VII. Data abstraction and modularity: SML Modules (1984–97). (2ND EDITION). Addison-Wesley, 1996. VIII. The state of the art: Scala (2007) 3 4 Topic I Goals Introduction and motivation Critical thinking about programming languages. References: g g ? What is a programming language!? Chapter 1 of Concepts in programming languages by Study programming languages. J. C. Mitchell. CUP, 2003. Be familiar with basic language concepts. Chapter 1 of Programming languages: Design and Appreciate trade-offs in language design. implementation (3RD EDITION) by T.W. Pratt and M. V.Zelkowitz. Prentice Hall, 1999. Trace history, appreciate evolution and diversity of ideas. Chapter 1 of Programming language pragmatics Be prepared for new programming methods, paradigms. (2ND EDITION) by M. L. Scott. Elsevier, 2006. 5 6 Why study programming languages? What makes a good language? To improve the ability to develop effective algorithms. Clarity, simplicity, and unity. To improve the use of familiar languages. Orthogonality. To increase the vocabulary of useful programming constructs. Naturalness for the application. To allow a better choice of programming language. Support of abstraction. To make it easier to learn a new language. Ease of program verification. To make it easier to design a new language. Programming environments. To simulate useful features in languages that lack them. Portability of programs. To make better use of language technology wherever it appears. 7 8 What makes a language successful? Expressive power. Cost of use. Cost of execution. Ease of use for the novice. Cost of program translation. Ease of implementation. Cost of program creation, testing, and use. Open source. Cost of program maintenance. Excellent compilers. Economics, patronage, and inertia. 9 10 Applications domains Influences Era Application Major languages Other languages 1960s Business COBOL Assembler Scientific FORTRAN ALGOL, BASIC, APL Computer capabilities. System Assembler JOVIAL, Forth AI LISP SNOBOL Applications. Today Business COBOL, SQL, spreadsheet C, PL/I, 4GLs Scientific FORTRAN, C, C++ BASIC, Pascal Programming methods. Maple, Mathematica System BCPL, C, C++ Pascal, Ada, BASIC, Implementation methods. MODULA AI LISP, Prolog Theoretical studies. Publishing TEX, Postscript, word processing Standardisation. Process UNIX shell, TCL, Perl Marvel, Esterel New paradigms Smalltalk, SML, Haskell, Java Eifell, C#, Scala Python, Ruby 11 12 ? Why are there so many languages? 89?>Motivating application in language design:;=< Evolution. A specific purpose provides focus for language designers; Special purposes. it helps to set criteria for making design decisions. Personal preference. A specific, motivating application also helps to solve one of the hardest problems in programming language design: deciding which features to leave out. 13 14 Examples: Good languages designed with a specific purpose Program execution model in mind. Good language design presents abstract machine. LISP: symbolic computation, automated reasoning FORTRAN: Flat register machine; memory arranged FP: functional programming, algebraic laws as linear array BCPL: compiler writing LISP: cons cells, read-eval-print loop Simula: simulation Algol family: stack of activation records; heap storage C: systems programming BCPL, C: underlying machine + abstractions ML: theorem proving Simula: Object references Smalltalk: Dynabook FP, ML: functions are basic control structure Clu, SML Modules: modular programming Smalltalk: objects and methods, communicating by C++: object orientation messages Java: Internet applications Java: Java virtual machine 15 16 89?>Classification of programming languages:;=< Theoretical foundations Imperative Examples: procedural C,Ada,Pascal,Algol,FORTRAN,... Formal-language theory. object oriented Scala, C#,Java, Smalltalk, SIMULA,... Automata theory. scripting Perl,Python,PHP,... Algorithmics. Declarative λ-calculus. functional Haskell, SML, Lisp, Scheme, . Semantics. logic Prolog Formal verification. dataflow Id, Val Type theory. constraint-based spreadsheets Complexity theory. template-based XSLT Logic. 17 18 Standardisation Language standardisation Consider: int i; i = (1 && 2) + 3 ; Proprietary standards. ? Is it valid C code? If so, what’s the value of i? Consensus standards. ? How do we answer such questions!? ANSI (American National Standards Institute) ! Read the reference manual. IEEE (Institute of Electrical and Electronics Engineers) BSI (British Standard Institute) ! Try it and see! ISO (International Standards Organisation) ! Read the ANSI C Standard. 19 20 Language-standards issues Language standards PL/1 Timeliness. When do we standardise a language? Conformance. What does it mean for a program to adhere to ? What does the following a standard and for a compiler to compile a standard? 9 + 8/3 Ambiguity and freedom to optimise — Machine mean? dependence — Undefined behaviour. − 11.666... ? Obsolescence. When does a standard age and how does it − Overflow ? get modified? − 1.666... ? Deprecated features. 21 22 For 9 + 8/3 we have: DEC(p,q) means p digits with q after the decimal point. DEC(1,0) + DEC(1,0)/DEC(1,0) => DEC(1,0) + DEC(15,15-((1-0)+0)) Type rules for DECIMAL in PL/1: => DEC(1,0) + DEC(15,14) => DEC(MIN(1+MAX(1-0,15-14)+MAX(0,14),15),MAX(0,14)) DEC(p1,q1) + DEC(p2,q2) => DEC(15,14) => DEC(MIN(1+MAX(p1-q1,p2-q2)+MAX(q1,q2),15),MAX(q1,q2)) DEC(p1,q1) / DEC(p2,q2) So the calculation is as follows => DEC(15,15-((p1-q1)+q2)) 9 + 8/3 = 9 + 2.66666666666666 = 11.66666666666666 - OVERFLOW = 1.66666666666666 - OVERFLOW disabled 23 24 History 1951–55: Experimental use of expression compilers. 1981–85: Smalltalk-80, Prolog, Ada 83. 1956–60: FORTRAN, COBOL, LISP, Algol 60. 1986–90: C++, SML, Haskell. 1961–65: APL notation, Algol 60 (revised), SNOBOL, CPL. 1991–95: Ada 95, TCL, Perl. 1966–70: APL, SNOBOL 4, FORTRAN 66, BASIC, SIMULA, 1996–2000: Java. Algol 68, Algol-W, BCPL. 2000–05: C#, Python, Ruby, Scala. 1971–75: Pascal, PL/1 (Standard), C, Scheme, Prolog. 1976–80: Smalltalk, Ada, FORTRAN 77, ML. 25 26 Language groups Multi-purpose languages Things to think about Scala, C#, Java, C++, C What makes a good language? Haskell, SML, Scheme, LISP The role of Perl, Python, Ruby 1. motivating applications, Special-purpose languages 2. program execution, UNIX shell 3. theoretical foundations SQL in language design. A LTEX Language standardisation. 27 28 FORTRAN = FORmula TRANslator (1957) Developed in the 1950s by an IBM team led by John Topic II Backus. FORTRAN : A simple procedural language The first high-level programming language to become widely used. References:g g At the time the utility of any high-level language was Chapter 10( 1) of Programming Languages: Design and open to question! § implementation (3RD EDITION) by T.W. Pratt and The main complain was the efficiency of compiled code. M. V.Zelkowitz. Prentice Hall, 1999. This heavily influenced the designed, orienting it towards The History of FORTRAN I, II, and III by J. Backus. In providing execution efficiency. History of Programming Languages by R. L. Wexelblat. Academic Press, 1981. Standards: 1966, 1977 (FORTRAN 77), 1990 (FORTRAN 90). 29 30 John Backus Overview As far as we were aware, we simply made up the Execution model language as we went along. We did not regard FORTRAN program = main program + subprograms language design as a difficult problem, merely a Each is compiled separate from all others. simple prelude to the real problem: designing a Translated programs are linked into final executable compiler which could produce efficient programs.a form during loading. All storage is allocated statically before program execution begins; no run-time storage management is provided. Flat register machine. No stacks, no recursion. Memory a In R.L.Wexelblat, History of Programming Languages, Academic Press, arranged as linear array. 1981, page 30. 31 32 Overview Overview Compilation Data types FORTRAN program Numeric data: Integer, real, complex, double-precision real. Compiler Boolean data. called logical Incomplete machine language Library routines U Arrays. of fixed declared length UUUU oo UUU ooo UUUU * ow o U Linker Character strings. of fixed declared length Files. Machine language program 33 34 Example PROGRAM MAIN Overview PARAMETER (MaXsIz=99) Control structures REAL A(mAxSiZ) FORTRAN 66 10 READ (5,100,END=999) K Relied heavily on statement labels and GOTO 100 FORMAT(I5)