Introduction and Scheme
Total Page:16
File Type:pdf, Size:1020Kb
Principles of Programming Languages (S) Matteo Pradella September 17, 2019 Matteo Pradella Principles of Programming Languages (S) September 17, 2019 1 / 105 Overview 1 Introduction 2 Basic concepts of programming languages, using Scheme 3 More advanced Scheme concepts 4 Object-oriented programming Matteo Pradella Principles of Programming Languages (S) September 17, 2019 2 / 105 Main coordinates email [email protected] office DEIB, Politecnico di Milano via Golgi 42, building 22, floor 3, room 322 phone 3495 web-page http://home.deib.polimi.it/pradella/PL.html twitter @bzoto, tag #corsopl Matteo Pradella Principles of Programming Languages (S) September 17, 2019 3 / 105 Motivation and preliminaries programming languages are tools for writing software i.e. tools for talking to the machine but not only to the machine: often code is created also to be read by human beings (debugging, pair programming, modifications/extensions) Matteo Pradella Principles of Programming Languages (S) September 17, 2019 4 / 105 Motivation and preliminaries (cont.) several levels of abstraction in a computer: hw, operating system, network, applications, daemons, virtual machines, frameworks, web, clouds, . no "holy grail" language different languages are to be used for programming at different levels close to human: abstract, hard to "control" close to machine: too many details, hard to understand what is going on various (often conflicting) aspects: expressiveness and conciseness; ease of use; ease to control; efficiency of compiled code . Matteo Pradella Principles of Programming Languages (S) September 17, 2019 5 / 105 Motivation and preliminaries (cont.) why is a language successful? sometimes right tool at the right time; often hype, good marketing, good tools, luck, who knows. e.g. often is better a so-so language with great compilers, than a very nice language with a partial implementation (thanks especially to the Internet and open source software:) many new languages, strong evolution in recent years but often the concepts are those introduced 30+ years ago! Matteo Pradella Principles of Programming Languages (S) September 17, 2019 6 / 105 Motivation and preliminaries (cont.) Recent, competitive technologies are based on new languages, especially w.r.t. the Web e.g. Ruby on Rails, Node.js more and more new technologies and language-based frameworks emerge (or re-emerge, think about Objective-C and then Swift) Hence, we need to know and understand not particular languages, but their main principles and concepts Matteo Pradella Principles of Programming Languages (S) September 17, 2019 7 / 105 An incomplete timeline of PLs 1957 Fortran (Formula Translator) 1958 LISP (LISt Processor) 1959 COBOL (Common Business Oriented Language) 1960 ALGOL 60 (Algorithmic Language) 1964 BASIC (Beginner’s All-purpose Symbolic Instruction Code) 1967 Simula (first object-oriented lang.) 1970 Pascal, Forth 1972 C, Prolog, Smalltalk 1975 Scheme (Lisp + Algol) 1978 ML (Meta-Language) 1980 Ada Matteo Pradella Principles of Programming Languages (S) September 17, 2019 8 / 105 An incomplete timeline of PLs (cont.) 1983 C++, Objective-C 1984 Common Lisp (Lisp + OO) 1986 Erlang 1987 Perl 1990 Haskell 1991 Python 1995 Java, JavaScript, Ruby, PHP 2001 C# 2002 F# 2003 Scala 2007 Clojure 2009 Go; ’11 Dart, ’12 Rust, ’14 Swift . Matteo Pradella Principles of Programming Languages (S) September 17, 2019 9 / 105 Matteo Pradella Principles of Programming Languages (S) September 17, 2019 10 / 105 Pre-(and post-)requisites Good knowledge of procedural and object-oriented programming (I assume at least with C and Java, respectively) Exam: written exercises, small programs, translations from different paradigms. Emphasis on concepts and elegance of the chosen approach; no code obfuscation contest! Matteo Pradella Principles of Programming Languages (S) September 17, 2019 11 / 105 Map of used languages Scheme, for basics, memory management, introduction to functional programming and object orientation, meta-stuff Haskell, for static type systems and algebraic data types, functional “purity” Erlang, for concurrency-oriented programming Some of them are “academic” languages (but with reference with more mainstream ones): they are simpler, more orthogonal and with less "cruft" We need to understand the concepts and to be able to adapt and learn to new languages with little effort. (It is useless to focus on one particular, temporarily successful language.) Matteo Pradella Principles of Programming Languages (S) September 17, 2019 12 / 105 Map of concepts dynamic (S) Type discipline Procedural programming (S) parametric polymorphism Abstract Data Types static (H) call by value (S)(H*) ad-hoc polymorphism Evaluation type inference call by reference (S) call by need (S*) 1st class functions (S) PL Concepts call by name (H) memoizaion (S*) (tail) recursion (S)(H) Functional programming Continuations (S) purity (H) monads (H)(S*) Exceptions (S)(H) quoting + macros (S) general principles DSL (S) Meta programming Object-oriented programming (S) prototype-based Actor model communication and synchronization Concurrent Programming (E) data sharing Matteo Pradella Principles of Programming Languages (S) September 17, 2019 13 / 105 Scheme: Why? Scheme is a language of the ancient and glorious Lisp Family [Scheme is intended to] allow researchers to use the language to explore the design, implementation, and semantics of programming languages. (From the R6RS standard) It is unique because it has extremely simple and flexible syntax and semantics, very few basic ideas Good to understand and experiment new concepts/constructs We will build new constructs and an OO language with it Matteo Pradella Principles of Programming Languages (S) September 17, 2019 14 / 105 A fast and incomplete introduction to Scheme Main (and free) material: We will use the Racket dialect of Scheme, which has a very good implementation and environment: http://racket-lang.org/ The last standard is "The Revised7 Report on the Algorithmic Language Scheme" (aka R7RS). A good book: http://www.scheme.com/tspl4/ (R6RS) #lang directive at the beginning of the file to choose the Scheme dialect that we are using (in our case #lang racket) Matteo Pradella Principles of Programming Languages (S) September 17, 2019 15 / 105 The obligatory quotes on Lisp Anyone could learn Lisp in one day, except that if they already knew Fortran, it would take three days. – Marvin Minsky If you give someone Fortran, he has Fortran. If you give someone Lisp, he has any language he pleases. – Guy Steele A Lisp programmer knows the value of everything, but the cost of nothing. – Alan Perlis Matteo Pradella Principles of Programming Languages (S) September 17, 2019 16 / 105 Syntax (I hope you really like parentheses) The typical procedure call f (x; y); (C/Java) is written like this: (f x y) No special syntax for expressions, no infix operators, no precedence rules. E.g. x == y + 3*x + z; is written (= x (+ y (* 3 x) z)) Such expressions are called s-expressions Matteo Pradella Principles of Programming Languages (S) September 17, 2019 17 / 105 Basic types Booleans: #t, #f Numbers: 132897132989731289713, 1.23e+33, 23/169, 12+4i Characters: #\a, #\Z Symbols: a-symbol, another-symbol?, indeed! Vectors: #(1 2 3 4) Strings: "this is a string" Pairs and Lists: (1 2 #\a), (a . b), () (we’ll see later) Matteo Pradella Principles of Programming Languages (S) September 17, 2019 18 / 105 Expressions Scheme is mainly a functional language, so every program is an expression, and computation is based on evaluating expressions (no statements). Evaluation of an expression produces a value (if it terminates) Evaluation of an expression (e1 e2 e3 :::) is based on the evaluation of e1, which identifies an operation f (e.g. is the name of a procedure). The other sub-expressions (i.e. ei , i > 1) are evaluated, and their values are passed to f . The evaluation order of ei , i ≥ 1 is unspecified, e.g. e4 could be evaluated before e2. Matteo Pradella Principles of Programming Languages (S) September 17, 2019 19 / 105 Procedures and the λ-calculus ancestry lambdas are unnamed procedures: (lambda (x y); this isa comment (+ (* x x) (* y y))) example usage ((lambda (x y) (+ (* x x) (* y y))) 2 3) =) 13 i.e. (λ(x; y) := x 2 + y 2)(2; 3) Lambdas are called blocks in Smalltalk, Ruby, Objective-C, and are present in many languages (e.g. in C++11, Java 8). Procedures are values in Scheme, hence are first class objects. λ-calculus introduced by Alonzo Church in the ’30s, theory of computable functions based on recursion (i.e. recursive functions) Matteo Pradella Principles of Programming Languages (S) September 17, 2019 20 / 105 Variables and binding let is used for binding variables: (let ((x 2); in Scheme (y 3)) ...) { intx=2,y=3;// inC ...} Scoping rules are static (or lexical): traditional/old Lisps are dynamic (e.g. Emacs Lisp). Scheme was the first Lisp taking static scoping from Algol. Matteo Pradella Principles of Programming Languages (S) September 17, 2019 21 / 105 With static scoping rules, it prints 1; with dynamic scoping rules, 2. A few interpreted languages are still based on dynamic scoping. Some languages can optionally support it (e.g. Common Lisp, Perl). In Perl “my” variables are static, while “local” are dynamic. Static vs Dynamic scoping Consider this code: (let ((a 1)) (let ((f (lambda () (display a)))) (let ((a 2)) (f )))) Matteo Pradella Principles of Programming Languages (S) September 17, 2019 22 / 105 Static vs Dynamic scoping Consider this code: (let ((a 1)) (let ((f (lambda () (display a)))) (let ((a 2)) (f )))) With static scoping rules, it prints 1; with dynamic scoping rules, 2. A few interpreted languages are still based on dynamic scoping. Some languages can optionally support it (e.g. Common Lisp, Perl). In Perl “my” variables are static, while “local” are dynamic. Matteo Pradella Principles of Programming Languages (S) September 17, 2019 22 / 105 let, again let binds variables “in parallel”, e.g.: (let ((x 1) (y 2)) (let ((x y); swapx andy (y x)) ..