P rogramming L anguages 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? – What features must/should it contain? CS251 Programming Languages Spring 2019, Lyn Turbak – What are the design dimensions? – What are design decisions that must be made? Department of Computer Science • Why should you take this course? What will you learn? Wellesley College Big ideas 2 PL is my passion! General Purpose PLs First PL project in 1982 as intern • Perl Java at Xerox PARC Python • Created visual PL for 1986 MIT masters thesis Fortran • 1994 MIT PhD on PL feature Racket ML JavaScript (synchronized lazy aggregates) • 1996 – 2006: worked on types Haskell as member of Church project Ruby C/C ++ 1988 – 2008: Design Concepts in Programming Languages • C# C# • 2011 – current: lead TinkerBlocks research team at Wellesley Scala CommonLisp 2012 – current: member of App Inventor development team • 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 R is a ``program'') and present this sequence to JINJA the machine. Swift IDL – Laurence Atkinson, Pascal Programming PostScript � Big ideas 5 Big ideas 6 � Religious � Views Programming Languages: LinguisGc View The use of COBOL cripples the mind; its teaching should, therefore, be regarded as a criminal offense. – Edsger Dijkstra A computer language … is a novel formal 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 mentally muGlated beyond hope of regeneraGon. – Edsger Dijstra methodology, not just a way to get a computer 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
Programming Paradigms Which Programming/PL Hat do You Wear? • 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. Func'onal , func'on-oriented (e.g Racket, ML, Haskell) : ComputaGon is • Function & � expressed by composing funcGons that manipulate immutable data. Contract / API Function & � Data Abstraction � Data Abstraction � Implementer Object-oriented (e.g. Simula, Smalltalk, Java) : ComputaGon is expressed in • User / Client terms of stateful objects that communicate by passing messages to one another. • Logic-oriented (e.g. Prolog) : 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. Programming Language Designer Big ideas 9 Big ideas 10 PL Dimensions Paradigm Example: Quicksort 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; First-class values : what values can be named, passed as arguments to • quicksort [] = [] quicksort (p:xs) = funcGons, returned as values from funcGons, stored in data structures. if (lo < hi) { (quicksort lesser) Which of these are first-class in your favorite PL: arrays, funcGons, variables? l = lo; ++ [p] h = hi; ++ (quicksort greater) Naming : Do variables/parameters name expressions, the values resulGng p = a[hi]; • where from evaluaGng expressions, or mutable slots holding the values from do { lesser = filter ( < p) xs evaluaGng expressions? How are names declared and referenced? What while ((l < h) && (a[l] <= p)) greater = filter ( >= p) xs l = l+1; determines their scope? while ((h > l) && (a[h] >= p)) h = h-1; • State : What is mutable and immutable; i.e., what enGGes in the language if (l < h) { (variables, data structures, objects) can change over Gme. t = a[l]; a[l] = a[h]; Control : What constructs are there for control flow in the language, e.g. FuncGonal Style (in Haskell) • a[h] = t; } condiGonals, loops, non-local exits, excepGon handling, conGnuaGons? } while (l < h); • Data : What kinds of data structures are supported in the language, including a[hi] = a[l]; products (arrays, tuples, records, dicGonaries), sums (opGons, oneofs, a[l] = p; variants), sum-of-products, and objects. ImperaGve Style qsort( a, lo, l-1 ); (in C; Java would be similar) • Types : Are programs staGcally or dynamically typed? What types are qsort( a, l+1, hi ); } expressible? Big ideas 11 Big ideas 12 }
Why study PL? Administrivia • Crossroads of CS • Approach problems as a language designer. • Schedule, psets, quizzes, lateness policy, etc.: see http://cs.wellesley.edu/~cs251/. – "A good programming language is a conceptual universe for thinking about 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 recursion, 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 • Credit/non is a bad idea for 251. Talk to me first! • Ask: • Visit me in o ffi ce hours before next Friday! – Why are PLs are the way they are? – 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 • Colorless green ideas sleep furiously. perform? What values does it produce? – Evaluation rules: what is the result or e ff ect 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): Concrete Syntax: Absolute Value FuncGon This AST abstracts over the concrete syntax for the Logo, Absolute Value FuncGon JavaScript, and Python definiGons. The other definiGons Logo : to abs :n ifelse :n < 0 [output (0 - :n)] [output :n] end funcGonDeclaraGon would have different ASTs. funcLonName body Javascript: function abs (n) {if (n < 0) return -n; else return n;} params Java: public static int abs (int n) {if (n < 0) return -n; else return n;} condiGonalStatement abs n Python: App Inventor: test def abs(n): then if n < 0: relaGonalOperaGon return return -n return else: value value rand1 return n lessThan varref intlit arithmeGcOperaGon varref Scheme/Racket: (define abs (lambda (n) (if (< n 0) (- n) n))) name name rand1 PostScript: /abs {dup 0 lt {0 swap sub} if} def 0 n 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
Recommend
More recommend