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 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; 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 19 Big ideas 20 Dynamic SemanGcs Example 3 SemanGcs Example 3 Suppose a is an array (or list) containing the three integer values 10, 20, and 30 Suppose a is an array (or list) containing the three integer values 10, 20, and 30 in the following languages. What is the meaning of the following expressions/ in the following languages. What is the meaning of the following expressions/ statements in various languages (the syntax might differ from what’s shown). statements in various languages (the syntax might differ from what’s shown).

a[1] a[3] a[2] = "foo" a[3] = 17 a[1] a[3] a[2] = "foo" a[3] = 17 Java Java C Python C

JavaScript Python Pascal App Inventor JavaScript

How do you determine the answers??? Pascal

App Inventor

How do you determine the answers? Try in implementaCon; consult documentaCon Big ideas 21 Big ideas 22

StaGc SemanGcs Example 1: Type Checking Static Semantics Example 2: Detecting Loops 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? Which of these Python programs has inputs for which it loops forever? A 2 * (3 + 4) F if (a) { G public boolean f(int i, boolean b) { c = a + b; return b && (i > 0); B 2 < (3 + 4) } else { } def f(x): c = a * b; return x+1 C 2 < True } H public int g(int i, boolean b) { return i * (b ? 1 : -1); D if (a < b) { } def g(x): def g2(x): c = a + b; while True: return g2(x) } else { I public int p(int w) { pass def collatz(x): c = a * b; if (w > 0) { return 2*w; } return x while x != 1: } } if (x % 2) == 0: x = x/2 J public int q(int x) { return x > 0; } E if (a < b) { def h(x): def h2(x): else: c = a + b; while x > 0: if x <= 0: x = 3*x + 1 K public int r(int y) { return g(y, y>0); } } else { x = x+1 return x return 1 c = a > b; return x public boolean s(int z) { return f(z); } else: } L return h2(x+1) Big ideas 23 Big ideas 24 Static Semantics and Uncomputability The Church-Turing Thesis

It is generally impossible to answer any interesGng quesGon about and Turing-Completeness staGc program analysis!

This is a consequence of Rice’s Theorem (see CS235). • Church-Turing Thesis: Computability is the common spirit embodied by this collecGon of formalisms. For example, will this program ever: • 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 • halt on certain inputs proved. • encounter an array index out of bounds error? • Because of their similarity to later computer hardware, Turing machines • throw a NullPointerException? (CS235) have become the gold standard for effecGvely computable. • access a given object again? • Well see in CS251 that Church’s lambda-calculus formalism is the • send sensitive information over the network? foundaGon of modern programming languages. • divide by 0? • A consequence: programming languages all have the same • run out of memory, starting with a given amount available? computaGonal power in term of what they can express. All such • try to treat an integer as an array? languages are said to be Turing-complete.

Big ideas 25 Big ideas 26

Pragmatics: Raffle App In App Inventor Expressiveness and Power hKp://ai2.appinventor.mit.edu

• About: Designer Window Blocks Editor – ease – elegance – clarity – modularity – abstracGon – ... • Not about: computability To enter the raffle, text me now with • Different problems, different languages an empty message: 339-225-0287 – Facebook or web browser in ? How hard is this to do in more tradiGonal development environments for Android/ iOS? Big ideas 27 Big ideas 28 Pragmatics: Metaprogramming Metaprogramming: InterpretaGon PLs are implemented in terms of metaprogams = programs that manipulate other programs. 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 Program in implementaGon language I. Machine M • Transla'on (compila'on): translate a program P in a source language S to a language L for language L program P’ in a target language T using a translator wriKen in implementaGon language I. on machine M • Embedding: express program P in source language S in terms of data structures and funcGons in implementaGon language I.

Big ideas 29 Big ideas 30

Metaprogramming: TranslaGon Metaprogramming: Bootstrapping Puzzles How can a Racket interpreter be wriKen in Racket? How can a Java be wriKen in Java?

Program in How can gcc (a C-to-x86 compiler) be wriKen in C? Program in language A A to B translator language B

Interpreter Machine M for language B on machine M Big ideas 31 Big ideas 32 Metaprogramming: Programming Language Layers Metaprogramming: Embedding

kernel

primiGve values/datatypes Program in Interpreter Machine M syntacGc sugar language A for language B embedded in on machine M system libraries language B

user libraries

Big ideas 33 Big ideas 34

Programming Language EssenGals Why? Who? When? Where? Design and ApplicaGon • Historical context • Motivating applications PrimiGves – Lisp: symbolic computation, logic, AI, experimental programming – ML: theorem-proving, case analysis, Means of CombinaGon – C: Unix operating system – Simula: simulation of physical phenomena, operations, objects – Smalltalk: communicating objects, user-programmer, Means of AbstracGon pervasiveness • Design goals, implementation constraints Think of the languages you know. What means of abstracGon do they have? – performance, productivity, reliability, modularity, abstraction, extensibility, strong guarantees, … • Well-suited to what sorts of problems?

Big ideas 35 Big ideas 36