<<

Discussion: Programming Languages

Big Ideas for CS 251 Your experience: Theory of Programming Languages • What PLs have you used? Principles of Programming Languages • PLs/PL features do you like/dislike. Why? More generally: • What is a PL? • Why are new PLs created? CS251 Programming Languages – What are they used for? Spring 2017, Lyn Turbak – Why are there so many?

Department of Science • Why are certain PLs popular? Wellesley College • What goes into the design of a PL?

1-2

PL is my passion! General Purpose PLs

• First PL project in 1982 as intern at Xerox PARC Perl • 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

1-3 1-4 Domain Specific PLs Programming Languages: Mechanical View HTML A computer is a machine. Our aim is to 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

1-5 1-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 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 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

1-7 1-8

Programming Language EssenEals PL Parts Syntax: of a PL • What a P in a given L look like as symbols? • Concrete syntax vs abstract syntax trees (ASTs) PrimiEves : meaning of a PL • Static Semantics: What can we tell about P before running it? Means of CombinaEon – rules: to which declaration does a variable reference refer? – Type rules: which programs are well-typed (and therefore legal)? • Dynamic Semantics: What is the behavior of P? What actions does it Means of AbstracEon perform? What values does it produce? – Evaluation rules: what is the result or effect of evaluating each language fragment and how are these composed? Think of the languages you know. What means of abstracEon do they have? Pragmatics: implementation of a PL (and PL environment) • How can we evaluate programs in the language on a computer?

1-9 • How can we optimize the performance of program execution? 1-10

Syntax (Form) vs. Semantics (Meaning) Concrete Syntax: Absolute Value FuncEon

in Natural Language Logo: to abs :n ifelse :n < 0 [output (0 - :n)] [output :n] end Javascript: function abs (n) {if (n < 0) return -n; else return n;} Furiously sleep ideas green colorless. Java: public static int abs (int n) {if (n < 0) return -n; else return n;} Python: App Inventor: Colorless green ideas sleep furiously. def abs(n): if n < 0: Little white rabbits sleep soundly. return -n else: return n Scheme: (define abs (lambda (n) (if (< n 0) (- n) n)))

PostScript: /abs {dup 0 lt {0 swap sub} if} def

1-11 1-12

Abstract Syntax Tree (AST): This AST abstracts over the concrete syntax for the Logo, Dynamic SemanEcs Example 1 Absolute Value FuncEon JavaScript, and Python definiEons. The other definiEons What is the meaning of the following expression? funcEonDeclaraEon would have different ASTs. body funcDonName params abs condiEonalStatement (1 + 11) * 10 n then

relaEonalOperaEon return return value rand1 value lessThan varref intlit arithmeEcOperaEon varref name rand1 name n 0 n subtract intlit varref value name 0 n 1-13 1-14

Dynamic SemanEcs Example 2 Dynamic SemanEcs Example 3 Suppose a is an array (or list) containing the three integer values 10, 20, and 30 What is printed by the following program? in the following languages. What is the meaning of the following expressions/ statements in various languages (the syntax might differ from what’s shown). a = 1; a[1] a[3] a[2] = "foo" a[3] = 17 = a + 20; Java C print(b); Python a = 300 JavaScript print(b); Pascal count = 0; App Inventor fun inc() { count = count + 1; return count; } fun dbl(ignore, x) { return x + x; } print(dbl(inc(), inc())

1-15 1-16 StaEc SemanEcs Example 1 Static Semantics Example 2: Detecting Loops

Which of the following Java examples is well-typed (i.e., passes the type checker)? Which of these following Python programs has inputs How do you know? What assumpEons are you making? for which it loops forever?

A 2 * (3 + 4) F public boolean f(int i, boolean b) { if (a) { G A def f(x): c = a + b; return b && (i > 0); B 2 < (3 + 4) } else { } return x+1 c = a * b; C 2 < True } H public int g(int i, boolean b) { B def g(x): return i * (b ? 1 : -1); C def g2(x): G def k(x): while True: return g2(x) while x != 1: D if (a < b) { } pass if (x % 2) == 0: c = a + b; return x } else { I public int p(int w) { x = x/2 c = a * b; if (w > 0) { return 2*w; } else: } } x = 3*x + 1 E def h(x): F def h2(x): return 1 J public int q(int x) { return x > 0; } while x > 0: if x <= 0: E if (a < b) { x = x+1 return x c = a + b; } else { K public int r(int y) { return g(y, y>0); } return x else: c = a > b; return h(x+1) public boolean s(int z) { return f(z); } } L 1-17 2-18

Static Semantics and Uncomputability The Church-Turing Thesis

It is generally impossible to answer any interesEng quesEon about and Turing-Completeness staEc program analysis!

This is a consequence of Rice’s Theorem (see CS235). • Church-Turing Thesis: Computability is the common spirit embodied by this collecEon of formalisms. For example, will this program ever: • This thesis is a claim that is widely believed about the intuiEve noEons of algorithm and effecEve computaEon. 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 effecEvely computable. • access a given object again? • Well see in CS251 that Church’s lambda-calculus formalism is the • send sensitive information over the network? foundaEon 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? computaEonal 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.

2-19 2-20

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

• About: Designer Window Blocks Editor – ease – elegance – clarity – modularity – abstracEon – ... • 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 tradiEonal development environments for Android/ iOS? 2-21 22

Pragmatics: Metaprogramming: InterpretaEon 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). ImplementaEon strategies: • Interpreta(on: interpret a program P in a source language S in terms of an Program in implementaEon 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 wriIen in implementaEon language I. on machine M • Embedding: express program P in source language S in terms of data structures and funcEons in implementaEon language I.

1-23 1-24 Metaprogramming: TranslaEon Metaprogramming: Embedding

Program in Program in language A A to B translator language B Program in Interpreter Machine M language A for language B embedded in on machine M language B Interpreter Machine M for language B on machine M 1-25 1-26

Metaprogramming: Bootstrapping Puzzles Metaprogramming: Layers

How can we write a Java-to-x86 in Java?

kernel

primiEve values/datatypes

syntacEc sugar

system libraries

We’ll learn how to user libraries understand such puzzles!

1-27 1-28 PL Dimensions Programming Paradigms PLs differ based on decisions language designers make in many dimensions. E.g.: • Impera(ve (e.g. C, Python): ComputaEon is step-by-step execuEon on a • First-class values: what values can be named, passed as arguments to funcEons, returned as values from funcEons, stored in data structures. stateful abstract machine involving memory slots and mutable data Which of these are first-class in your favorite PL: arrays, funcEons, variables? structures. • • Naming: Do variables/parameters name expressions, the values resulEng Func(onal, func(on-oriented (e.g Racket, ML, Haskell): ComputaEon is expressed by composing funcEons that manipulate immutable data. from evaluaEng expressions, or mutable slots holding the values from evaluaEng expressions? How are names declared and referenced? What • Object-oriented (e.g. , , Java): ComputaEon is expressed in determines their scope? terms of stateful objects that communicate by passing messages to one another. • State: What is mutable and immutable; i.e., what enEEes in the language (variables, data structures, objects) can change over Eme. • Logic-oriented (e.g. ): ComputaEon is expressed in terms of declaraEve relaEonships. • Control: What constructs are there for control flow in the language, e.g. condiEonals, loops, non-local exits, excepEon handling, conEnuaEons? Note: In pracEce, most PLs involve mulEple paradigms. E.g. • Data: What kinds of data structures are supported in the language, including • Python supports funcEonal features (map, filter, list comprehensions) and products (arrays, tuples, records, dicEonaries), sums (opEons, oneofs, objects variants), sum-of-products, and objects. • Racket and ML have imperaEve features. • Types: Are programs staEcally or dynamically typed? What types are expressible? 1-29 1-30

Paradigm Example: Quicksort Why? Who? When? Where? void qsort(int a[], int lo, int hi) { quicksort :: Ord a => [a] -> [a] int h, l, p, t; quicksort [] = [] Design and ApplicaEon quicksort (p:) = if (lo < hi) { l = lo; (quicksort lesser) • Historical context h = hi; ++ [p] p = a[hi]; ++ (quicksort greater) • Motivating applications where do { lesser = (< p) xs – Lisp: symbolic computation, logic, AI, experimental programming while ((l < h) && (a[l] <= p)) greater = filter (>= p) xs l = l+1; – ML: theorem-proving, case analysis, while ((h > l) && (a[h] >= p)) h = h-1; – C: if (l < h) { t = a[l]; – Simula: simulation of physical phenomena, operations, objects a[l] = a[h]; a[h] = t; FuncEonal Style (in Haskell) – Smalltalk: communicating objects, user-programmer, } pervasiveness } while (l < h); • Design goals, implementation constraints a[hi] = a[l]; a[l] = p; – performance, productivity, reliability, modularity, abstraction, extensibility, strong guarantees, … qsort( a, lo, l-1 ); ImperaEve Style qsort( a, l+1, hi ); (in C; Java would be similar) • Well-suited to what sorts of problems? } 1-31 1-32 } Why study PL? Administrivia • Crossroads of CS • Approach problems as a language designer. • Schedule, psets, lateness policy, etc.: – "A good programming language is a conceptual universe for thinking about programming" see http://cs.wellesley.edu/~cs251/ -- Alan Perlis • PS0 (introductions) will be posted this afternoon; – Evaluate, compare, and choose languages due tomorrow – Become beIer at learning new languages • PS1 will be posted tomorrow; due next Friday – become a beIer problem-solver – view API design as language design • install Dr. Racket for tomorrow • Ask: • visit me in office hours! – Why are PLs are the way they are? – How could they (or couldn't they) be beIer? – What is the cost-convenience trade-off for feature X? 1-33 1-34