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? • Why are certain PLs popular? • What goes into the design of a PL? CS251 Programming Languages – What features must/should it contain? Spring 2018, 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

• 2012 – current: member of App Inventor development team CommonLisp

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

Big ideas 5 Big ideas 6

Programming Languages: LinguisEc 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 pracEcally impossible to teach good programming to students that medium for expressing ideas about have had a prior exposure to BASIC: as potenEal programmers they are methodology, not just a way to get a computer mentally muElated beyond hope of regeneraEon. – Edsger Dijstra You're introducing your students to programming in C? You might as well to perform operaEons. Programs are wriIen 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 starEng them off with plasEc toy tools and telling them to go sculpt sand on the beach. - Alfred Thompson, 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? EssenEals

CS111 Big idea #1: Abstraction

Function & Function & Contract / API Data Abstraction PrimiEves Data Abstraction Implementer User / Client Means of CombinaEon

Means of AbstracEon

Think of the languages you know. What means of abstracEon do they have?

Programming Language Designer Big ideas 9 Big ideas 10

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 11 Big ideas 12

Abstract Syntax Tree (AST): This AST abstracts over the Concrete Syntax: Absolute Value FuncEon concrete syntax for the Logo, Absolute Value FuncEon JavaScript, and Python Logo: to abs :n ifelse :n < 0 [output (0 - :n)] [output :n] end definiEons. The other definiEons funcEonDeclaraEon would have different ASTs. Javascript: function abs (n) {if (n < 0) return -n; else return n;} body funcDonName params Java: public static int abs (int n) {if (n < 0) return -n; else return n;} abs condiEonalStatement Python: App Inventor: n test def abs(n): then if n < 0: relaEonalOperaEon return -n return return else: value rand1 value return n lessThan varref intlit arithmeEcOperaEon varref Scheme: (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 13 Big ideas 14

Dynamic SemanEcs Example 1 Dynamic SemanEcs Example 2

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

a = 1; (1 + 11) * 10 b = a + 20; print(b); a = 300 print(b); count = 0; fun inc() { count = count + 1; return count; } fun dbl(ignore, x) { return x + x; } print(dbl(inc(), inc())

Big ideas 15 Big ideas 16 Dynamic SemanEcs Example 3 StaEc SemanEcs Example 1: Type Checking 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 in the following languages. What is the meaning of the following expressions/ checker)? How do you know? What assumpEons are you making? statements in various languages (the syntax might differ from what’s shown).

A 2 * (3 + 4) a[1] a[3] a[2] = "foo" a[3] = 17 F if (a) { G public boolean f(int i, boolean b) { Java c = a + b; return b && (i > 0); B 2 < (3 + 4) } else { } C c = a * b; Python C 2 < True } H public int g(int i, boolean b) { return i * (b ? 1 : -1); JavaScript D if (a < b) { } Pascal c = a + b; } else { I public int p(int w) { App Inventor c = a * b; if (w > 0) { return 2*w; } } } How do you determine the answers??? J public int q(int x) { return x > 0; } E if (a < b) { c = a + b; public int r(int y) { return g(y, y>0); } } else { K c = a > b; public boolean s(int z) { return f(z); } } L Big ideas 17 Big ideas 18

Static Semantics Example 2: Static Semantics and Uncomputability Detecting Loops It is generally impossible to answer any interesEng quesEon about Which of these Python programs has staEc program analysis! inputs for which it loops forever? This is a consequence of Rice’s Theorem (see CS235). def f(x): return x+1 For example, will this program ever: • halt on certain inputs def g(x): def g2(x): while True: return g2(x) • encounter an array index out of bounds error? pass def collatz(x): • throw a NullPointerException? return x while x != 1: if (x % 2) == 0: • access a given object again? x = x/2 • send sensitive information over the network? def h(x): def h2(x): else: • divide by 0? while x > 0: x = 3*x + 1 if x <= 0: • run out of memory, starting with a given amount available? x = x+1 return x return 1 return x else: • try to treat an integer as an array?

return h(x+1) Big ideas 19 Big ideas 20 The Church-Turing Thesis and Turing-Completeness Expressiveness and Power • About: • Church-Turing Thesis: Computability is the common spirit embodied by – ease this collecEon of formalisms. – elegance • This thesis is a claim that is widely believed about the intuiEve noEons of – and effecEve computaEon. It is not a theorem that can be clarity proved. – modularity • Because of their similarity to later computer hardware, Turing machines – abstracEon (CS235) have become the gold standard for effecEvely computable. – ... • Well see in CS251 that Church’s lambda-calculus formalism is the • Not about: computability foundaEon of modern programming languages. • Different problems, different languages • A consequence: programming languages all have the same – Facebook or web browser in ? computaEonal power in term of what they can express. All such languages are said to be Turing-complete.

Big ideas 21 Big ideas 22

Pragmatics: Raffle App In App Inventor Pragmatics: Metaprogramming hIp://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). ImplementaEon strategies: • Interpreta(on: interpret a program P in a source language S in terms of an implementaEon 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 wriIen in To enter the raffle, text me now with implementaEon 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 tradiEonal structures and funcEons in implementaEon language I. development environments for Android/ iOS? Big ideas 23 Big ideas 24 Metaprogramming: InterpretaEon Metaprogramming: TranslaEon

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 25 on machine M Big ideas 26

Metaprogramming: Embedding Metaprogramming: Bootstrapping Puzzles In what language is the gcc C implemented? How can we write a Java-to-x86 compiler in Java? How can we write a Racket interpreter in Racket?

Program in Interpreter Machine M language A for language B embedded in on machine M language B

We’ll learn how to understand such puzzles! Big ideas 27 Big ideas 28 Metaprogramming: Programming Language Layers PL Dimensions PLs differ based on decisions language designers make in many dimensions. E.g.: • First-class values: what values can be named, passed as arguments to funcEons, returned as values from funcEons, stored in data structures. Which of these are first-class in your favorite PL: arrays, funcEons, variables? • Naming: Do variables/parameters name expressions, the values resulEng kernel from evaluaEng expressions, or mutable slots holding the values from evaluaEng expressions? How are names declared and referenced? What primiEve determines their scope? values/datatypes • State: What is mutable and immutable; i.e., what enEEes in the language (variables, data structures, objects) can change over Eme. syntacEc sugar • Control: What constructs are there for control flow in the language, e.g. condiEonals, loops, non-local exits, excepEon handling, conEnuaEons? system libraries • Data: What kinds of data structures are supported in the language, including products (arrays, tuples, records, dicEonaries), sums (opEons, oneofs, user libraries variants), sum-of-products, and objects. • Types: Are programs staEcally or dynamically typed? What types are

Big ideas 29 expressible? Big ideas 30

Programming Paradigms Paradigm Example: Quicksort

void qsort(int a[], int lo, int hi) { quicksort :: Ord a => [a] -> [a] • Impera(ve (e.g. C, Python): ComputaEon is step-by-step execuEon on a int h, l, p, t; quicksort [] = [] stateful abstract machine involving memory slots and mutable data quicksort (p:xs) = if (lo < hi) { structures. l = lo; (quicksort lesser) h = hi; ++ [p] • Func(onal, func(on-oriented (e.g Racket, ML, Haskell): ComputaEon is p = a[hi]; ++ (quicksort greater) expressed by composing funcEons that manipulate immutable data. where do { lesser = filter (< p) xs • Object-oriented (e.g. Simula, Smalltalk, Java): ComputaEon is expressed in while ((l < h) && (a[l] <= p)) greater = filter (>= p) xs l = l+1; terms of stateful objects that communicate by passing messages to one while ((h > l) && (a[h] >= p)) another. h = h-1; if (l < h) { • Logic-oriented (e.g. Prolog): ComputaEon is expressed in terms of declaraEve t = a[l]; relaEonships. a[l] = a[h]; a[h] = t; FuncEonal Style (in Haskell) } Note: In pracEce, most PLs involve mulEple paradigms. E.g. } while (l < h);

• Python supports funcEonal features (map, filter, list comprehensions) and a[hi] = a[l]; objects a[l] = p; ImperaEve Style • Racket and ML have imperaEve features. qsort( a, lo, l-1 ); qsort( a, l+1, hi ); (in C; Java would be similar) } Big ideas 31 } Big ideas 32 Why? Who? When? Where? Why study PL? Design and ApplicaEon • Crossroads of CS • Approach problems as a language designer. • Historical context – "A good programming language is a conceptual universe for thinking about • Motivating applications programming” -- Alan Perlis – Lisp: symbolic computation, logic, AI, experimental programming – Evaluate, compare, and choose languages – ML: theorem-proving, case analysis, – Become beIer at learning new languages – Become a beIer programmer by leveraging powerful features – C: Unix operating system (first-class funcEons, tree , sum-of-product datatypes, paIern – Simula: simulation of physical phenomena, operations, objects matching) – : communicating objects, user-programmer, – You probably won’t design a general-purpose PL, but might design a DSL pervasiveness – view API design as language design • Design goals, implementation constraints • Ask: – performance, productivity, reliability, modularity, abstraction, – Why are PLs are the way they are? extensibility, strong guarantees, … – How could they (or couldn't they) be beIer? • Well-suited to what sorts of problems? – What is the cost-convenience trade-off for feature X? Big ideas 33 Big ideas 34

Administrivia

• Schedule, psets, quizzes, lateness policy, etc.: see http://cs.wellesley.edu/~cs251/. • Do PS0 tonight – Fill out “get to know you” form – Review course syllabus and policies (we’ll go over these tomorrow) – Read Wed slides on “big-step semantics” of Racket – Install Dr. Racket • PS1 is available; due next Friday • Visit me in office hours before next Friday!

1-35