Python C/C++ Java Perl Ruby
Total Page:16
File Type:pdf, Size:1020Kb
Discussion: Programming Languages Big Ideas for CS 251 Your experience: Theory of Programming Languages • What PLs have you used? Principles of Programming Languages • Which 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 Computer 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 Java Perl • Created visual PL for 1986 MIT Python masters thesis • 1994 MIT PhD on PL feature Fortran (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/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 make Excel CSS the machine perform some specifieD acEons. With some machines we might express our intenEons by Depressing keys, pushing OpenGL R 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 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 1-7 1-8 Programming Language EssenEals PL Parts Syntax: form of a PL • What a P in a given L look like as symbols? • Concrete syntax vs abstract syntax trees (ASTs) PrimiEves Semantics: meaning of a PL • Static Semantics: What can we tell about P before running it? Means of CombinaEon – Scope 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 test 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 b = 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 c = a + b; if (x % 2) == 0: 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 assembly language? How harD is this to Do in more tradiEonal Development environments for AnDroiD/ iOS? 2-21 22 Pragmatics: Metaprogramming 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. Interpreter 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: Programming Language Layers How can we write a Java-to-x86 compiler 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.