Programming Languages

Big Ideas for CS 251 • What is a PL? Theory of Programming Languages • Why are new PLs created? Principles of Programming Languages – What are they used for? – Why are there so many? SOLUTIONS • Why are certain PLs popular? • What goes into the design of a PL? CS251 Programming Languages – What features must/should it contain? Spring 2019, Lyn Turbak – What are the design dimensions? – What are design decisions that must be made? Department of Science Wellesley College • Why should you take this course? What will you learn?

Big ideas 2

PL is my passion! General Purpose PLs

• First PL project in 1982 as intern at Xerox PARC Java • Created visual PL for 1986 MIT Python masters thesis

• 1994 MIT PhD on PL feature (synchronized lazy aggregates) ML JavaScript • 1996 – 2006: worked on types Racket as member of Church project Haskell • 1988 – 2008: Design Concepts in Programming Languages /C++ Ruby • 2011 – current: lead TinkerBlocks research team at Wellesley C# • 2012 – current: member of App Inventor development team Scala CommonLisp

Big ideas 3 Big ideas 4 Domain Specific PLs (DSLs) Programming Languages: Mechanical View Excel HTML A computer is a machine. Our aim is to make CSS the machine perform some specified acGons. With some machines we might express our OpenGL LaTeX intenGons by depressing keys, pushing buKons, rotaGng knobs, etc. For a computer, Matlab we construct a sequence of instrucGons (this Digital Amati is a ``program'') and present this sequence to JINJA the machine. IDL Swift PostScript – Laurence Atkinson, Pascal Programming

Big ideas 5 Big ideas 6

Programming Languages: LinguisGc View Religious Views The use of COBOL cripples the mind; its teaching should, therefore, be A computer language … is a novel formal regarded as a criminal offense. – Edsger Dijkstra It is pracGcally impossible to teach good programming to students that medium for expressing ideas about have had a prior exposure to BASIC: as potenGal programmers they are methodology, not just a way to get a computer mentally muGlated beyond hope of regeneraGon. – Edsger Dijstra You're introducing your students to programming in C? You might as well to perform operaGons. Programs are wriKen for give them a frontal lobotomy! – A colleague of mine people to read, and only incidentally for A LISP programmer knows the value of everything, but the cost of nothing. - Alan Perlis machines to execute. I have never met a student who cut their teeth in any of these languages – Harold Abelson and Gerald J. Sussman and did not come away profoundly damaged and unable to cope. … I mean this reads to me very similarly to teaching someone to be a carpenter by starGng them off with plasGc toy tools and telling them to go sculpt sand on the beach. - John Haugeland, on blocks languages A language that doesn't affect the way you think about programming, is not worth knowing. - Alan Perlis

Big ideas 7 Big ideas 8 Which Programming/PL Hat do You Wear? Programming Paradigms

• Impera've (e.g. C, Python): ComputaGon is step-by-step execuGon on a CS111 Big idea #1: Abstraction stateful abstract machine involving memory slots and mutable data structures.

Function & • Func'onal, func'on-oriented (e.g Racket, ML, Haskell): ComputaGon is Function & Contract / API Data Abstraction expressed by composing funcGons that manipulate immutable data. Data Abstraction Implementer • Object-oriented (e.g. Simula, , Java): ComputaGon is expressed in User / Client terms of stateful objects that communicate by passing messages to one another. • Logic-oriented (e.g. ): ComputaGon is expressed in terms of declaraGve relaGonships. Note: In pracGce, most PLs involve mulGple paradigms. E.g. • Python supports funcGonal features (map, filter, list comprehensions) and objects • Racket and ML have imperaGve features. Designer Big ideas 9 Big ideas 10

Paradigm Example: Quicksort PL Dimensions PLs differ based on decisions language designers make in many dimensions. E.g.: void qsort(int a[], int lo, int hi) { quicksort :: Ord a => [a] -> [a] int h, l, p, t; quicksort [] = [] • First-class values: what values can be named, passed as arguments to quicksort (p:xs) = if (lo < hi) { funcGons, returned as values from funcGons, stored in data structures. l = lo; (quicksort lesser) Which of these are first-class in your favorite PL: arrays, funcGons, variables? h = hi; ++ [p] p = a[hi]; ++ (quicksort greater) • Naming: Do variables/parameters name expressions, the values resulGng where do { lesser = filter (< p) xs from evaluaGng expressions, or mutable slots holding the values from while ((l < h) && (a[l] <= p)) greater = filter (>= p) xs evaluaGng expressions? How are names declared and referenced? What l = l+1; while ((h > l) && (a[h] >= p)) determines their scope? h = h-1; if (l < h) { • State: What is mutable and immutable; i.e., what enGGes in the language t = a[l]; (variables, data structures, objects) can change over Gme. a[l] = a[h]; a[h] = t; FuncGonal Style (in Haskell) • Control: What constructs are there for control flow in the language, e.g. } } while (l < h); condiGonals, loops, non-local exits, excepGon handling, conGnuaGons?

a[hi] = a[l]; • Data: What kinds of data structures are supported in the language, including a[l] = p; products (arrays, tuples, records, dicGonaries), sums (opGons, oneofs, variants), sum-of-products, and objects. qsort( a, lo, l-1 ); ImperaGve Style qsort( a, l+1, hi ); (in C; Java would be similar) • Types: Are programs staGcally or dynamically typed? What types are } } Big ideas 11 expressible? Big ideas 12 Why study PL? Administrivia • Crossroads of CS • Approach problems as a language designer. • Schedule, psets, quizzes, lateness policy, etc.: – "A good programming language is a conceptual universe for thinking about see http://cs.wellesley.edu/~cs251/. programming” -- Alan Perlis • Do (most of) PS0 tonight – Evaluate, compare, and choose languages – Fill out “get to know you” Introze introduction. – Become beKer at learning new languages – Review course syllabus and policies – Become a beKer programmer by leveraging powerful features (we’ll go over these tomorrow) (first-class funcGons, tree , sum-of-product datatypes, paKern – Read Wed slides on “big-step semantics” of Racket matching) – Install Dr. Racket – You probably won’t design a general-purpose PL, but might design a DSL • PS1 is available; due next Friday. Start it this week! – view API design as language design • Ask: • Credit/non is a bad idea for 251. Talk to me first! – Why are PLs are the way they are? • Visit me in office hours before next Friday! – How could they (or couldn't they) be beKer? – What is the cost-convenience trade-off for feature X? Big ideas 13 Big ideas 14

PL Parts Syntax (Form) vs. Semantics (Meaning) Syntax: form of a PL in Natural Language • What a P in a given L look like as symbols? • Concrete syntax vs abstract syntax trees (ASTs) Furiously sleep ideas green colorless. Semantics: meaning of a PL • Dynamic Semantics: What is the behavior of P? What actions does it perform? What values does it produce? Colorless green ideas sleep furiously. – Evaluation rules: what is the result or effect of evaluating each language fragment and how are these composed? Little white rabbits sleep soundly. • Static Semantics: What can we tell about P before running it? – Scope rules: to which declaration does a variable reference refer? – Type rules: which programs are well-typed (and therefore legal)?

Pragmatics: implementation of a PL (and PL environment) • How can we evaluate programs in the language on a computer? • How can we optimize the performance of program execution? Big ideas 15 Big ideas 16

Abstract Syntax Tree (AST): This AST abstracts over the Concrete Syntax: Absolute Value FuncGon concrete syntax for the Logo, Absolute Value FuncGon JavaScript, and Python Logo: to abs :n ifelse :n < 0 [output (0 - :n)] [output :n] end definiGons. The other definiGons funcGonDeclaraGon would have different ASTs. Javascript: function abs (n) {if (n < 0) return -n; else return n;} body funcLonName params Java: public static int abs (int n) {if (n < 0) return -n; else return n;} abs condiGonalStatement Python: App Inventor: n test def abs(n): then if n < 0: relaGonalOperaGon return -n return return else: value rand1 value return n lessThan varref intlit arithmeGcOperaGon varref Scheme/Racket: (define abs (lambda (n) (if (< n 0) (- n) n))) name rand1 name PostScript: /abs {dup 0 lt {0 swap sub} if} def n 0 n subtract intlit varref value name 0 n Big ideas 17 Big ideas 18

Dynamic SemanGcs Example 1 Dynamic SemanGcs Example 2

What is the meaning of the following expression? What is printed by the following program?

(1 + 11) * 10 a = 1; Here are some possible answers. What execuGon models give rise to these answers? b = a + 20; Some possible answers: print(b); 21 21 21 21 • 120 (regular interpretaGon of numbers, operators) a = 300 21 21 320 320 • 1000 (binary numbers, regular operators) print(b); 4 2 3 2 • 0 (“+” means “minus”, “*” means “plus”) • 111…1 (30 1s; “+” means “convert to string and concatenate”; count = 0; * means “repeated concatenaGon”) fun inc() { count = count + 1; return count; } • 13 (number of characters in string) fun dbl(ignore, x) { return x + x; } • 5 (number of nodes in AST) print(dbl(inc(), inc()) • 3 (number of leaves in AST)

Big ideas 19 Big ideas 20 SemanGcs Example 3 SoluFons StaGc SemanGcs Example 1: Type Checking SoluFons Suppose a is an array (or list) containing the three integer values 10, 20, and 30 Which of the following Java examples can be well-typed (i.e., pass the type checker)? How do you know? What assumpGons are you making? in the following languages. What is the meaning of the following expressions/ statements in various languages (the syntax might differ from what’s shown). Green indicates staFcally well-typed; Red indicates staFc type error. F A can’t both be G public boolean f(int i, boolean b) { A 2 * (3 + 4) a[1] a[3] a[2] = "foo" a[3] = 17 bool for test and return b && (i > 0); } Java 20 dynamic index out of staGc type error dynamic index out B 2 < (3 + 4) int for +/* if (a) { bounds error of bounds error H public int g(int i, boolean b) { C 2 < True c = a + b; C 20 returns value in staGc type error Stores 17 in memory } else { return i * (b ? 1 : -1); memory slot aoer a[2] slot aoer a[2] D Assume a,b,c are ints c = a * b; } if (a < b) { } I Python 20 dynamic list index out stores “foo” in dynamic list index public int p(int w) { c = a + b; if (w > 0) { return 2*w; } of range error third slot of a out of range error } else { } No else clause, so no return of in when w <= 0 JavaScript 20 “undefined” value stores “foo” in Stores 17 in a[3] c = a * b; third slot of a } J Return type needs to be boolean, not int public int q(int x) { return x > 0; } Pascal 20 staGc index out of staGc type error staGc index out of E Assume a,b are ints; bounds error bounds error can’t find type for c K Assume g has type from above: g : (int, boolean) -> int if (a < b) { public int r(int y) { return g(y, y>0); } App Inventor 10 30 stores “foo” in Stores 17 in third c = a + b; second slot of a slot of a } else { L If f has type (int, boolean) -> boolean, not well-typed; How do you determine the answers? Try in implementaFon; consult documentaFon c = a > b; But *would* be well typed if f : (int) -> boolean Big ideas 21 } public boolean s(int z) { return f(z); } Big ideas 22

Static Semantics Example 2: Static Semantics and Uncomputability Detecting Loops Solutions Which of these Python programs has inputs for It is generally impossible to answer any interesGng quesGon about which it loops forever? staGc program analysis! def f(x): def g2(x): return x+1 return g2(x) This is a consequence of Rice’s Theorem (see CS235). No ∞ loop on any input ∞ recursion on all inputs. In pracGce, runs out of stack def g(x): space in Python. Similar Racket while True: For example, will this program ever: program is true ∞ loop due pass to proper tail recursion. return x • halt on certain inputs ∞ loop on all inputs def collatz(x): def h2(x): while x != 1: • encounter an array index out of bounds error? if x <= 0: def h(x): if (x % 2) == 0: • throw a NullPointerException? while x > 0: return x x = x/2 x = x+1 else: else: • access a given object again? return h2(x+1) return x x = 3*x + 1 • send sensitive information over the network? No ∞ loop for x <= 0. No ∞ loop for x <= 0. return 1 ∞ loop for x > 0, assuming ∞ recursion for x > 0. Although this terminates for all x > 0 • divide by 0? arbitrarily large numbers. In pracGce, runs out of stack that have been tested, no one knows In pracGce, will either run space in Python. Similar Racket the answer for all x. This a famous • run out of memory, starting with a given amount available? out of memory when x gets program will loop, but run out open problem in mathemaGcs. • try to treat an integer as an array? memory when x gets too big. too big, or will wrap to Solve it and win a Fields medal! negaGve and halt. Big ideas 23 Big ideas 24 The Church-Turing Thesis and Turing-Completeness Expressiveness and Power • About: • Church-Turing Thesis: Computability is the common spirit embodied by – ease this collecGon of formalisms. – elegance • This thesis is a claim that is widely believed about the intuiGve noGons of – and effecGve computaGon. It is not a theorem that can be clarity proved. – modularity • Because of their similarity to later computer hardware, Turing machines – abstracGon (CS235) have become the gold standard for effecGvely computable. – ... • Well see in CS251 that Church’s lambda-calculus formalism is the • Not about: computability foundaGon of modern programming languages. • Different problems, different languages • A consequence: programming languages all have the same – Facebook or web browser in ? computaGonal power in term of what they can express. All such languages are said to be Turing-complete.

Big ideas 25 Big ideas 26

Pragmatics: Raffle App In App Inventor Pragmatics: Metaprogramming hKp://ai2.appinventor.mit.edu PLs are implemented in terms of metaprogams = programs that manipulate other programs. Designer Window Blocks Editor This may sound weird, but programs are just trees (ASTs), so a metaprogram is just a program that manipulates trees (think a more complex version of CS230 binary tree programs). ImplementaGon strategies: • Interpreta'on: interpret a program P in a source language S in terms of an implementaGon language I. • Transla'on (compila'on): translate a program P in a source language S to a program P’ in a target language T using a translator wriKen in To enter the raffle, text me now with implementaGon language I. an empty message: 339-225-0287 • Embedding: express program P in source language S in terms of data How hard is this to do in more tradiGonal structures and funcGons in implementaGon language I. development environments for Android/ iOS? Big ideas 27 Big ideas 28 Metaprogramming: InterpretaGon Metaprogramming: TranslaGon

Program in Program in language A A to B translator language B Program in Machine M language L for language L on machine M

Interpreter Machine M for language B Big ideas 29 on machine M Big ideas 30

Metaprogramming: Bootstrapping Puzzles Metaprogramming: Programming Language Layers How can a Racket interpreter be wriKen in Racket? How can a Java be wriKen in Java? How can gcc (a C-to-x86 compiler) be wriKen in C? kernel primiGve values/datatypes

syntacGc sugar

system libraries

user libraries

Big ideas 31 Big ideas 32 Metaprogramming: Embedding Programming Language EssenGals

PrimiGves

Means of CombinaGon Program in Interpreter Machine M language A for language B Means of AbstracGon embedded in on machine M language B Think of the languages you know. What means of abstracGon do they have?

Big ideas 33 Big ideas 34

Why? Who? When? Where? Design and ApplicaGon • Historical context • Motivating applications – Lisp: symbolic computation, logic, AI, experimental programming – ML: theorem-proving, case analysis, – C: Unix operating system – Simula: simulation of physical phenomena, operations, objects – Smalltalk: communicating objects, user-programmer, pervasiveness • Design goals, implementation constraints – performance, productivity, reliability, modularity, abstraction, extensibility, strong guarantees, … • Well-suited to what sorts of problems?

Big ideas 35