λ λ CS 251 Fall 2019 CS 251 Fall 2019 PL = P rogramming L anguage Principles of Programming Languages Principles of Programming Languages Ben Wood Ben Wood 1. What is a PL? 2. What goes into PL design? The Plan 3. How is a PL defined? 4. Why study PLs? What will you learn? https://cs.wellesley.edu/~cs251/f19/ Plan 1 Plan 2 PL = Procedural Lever A computer is a machine. Our aim is to make the machine perform some specified actions. With some machines we might express our intentions by depressing keys, pushing buttons, rotating knobs, etc. For a computer, we construct a sequence of instructions (this is a "program") and present this What is a P rogramming L anguage? sequence to the machine. – Laurence Atkinson, Pascal Programming Plan 3 Plan 4
PL = Presentation of Logic PL = Problem-solving Lens … a computer language is not just a way of getting a computer A good programming language is a conceptual universe for to perform operations but rather that it is a novel formal thinking about programming. medium for expressing ideas about methodology. Thus, programs must be written for people to read, and only A language that doesn't affect the way you think about incidentally for machines to execute. programming is not worth knowing. – Harold Abelson and Gerald J. Sussman, – Alan Perlis Structure and Interpretation of Computer Programs Plan 5 Plan 6 PL = Precise Laws PL Big idea #1: Abstraction Im Implementer / / Us User / Client Co Contract / A / API Des Designer ner PL PL PL What goes into PL design? PL PL PL PL PL PL PL PL PL PL PL PL -- Wellesley CS 111 Determine what and how abstractions Enable precise manual and automated can be expressed and manipulated. reasoning about properties of programs. Plan 7 Plan 8
PL design: application / purpose Computability Turing-co Tu comple lete = equivalent to key models of computation General computation Perl Java – Tu Turing machine (CS 235) Python ) λ -ca – (L (Lambda) calcu culus (CS 251) FORTRAN ML JavaScript – … Racket Rac Haskell Ruby C/C ++ Church-Tu Ch Turing thesis: Turing-complete = computable C# C# Rust Scala Sc CommonLisp Domain-specific computation ⇒ All Turing-complete PLs (roughly, general-purpose PLs or just "PLs") Excel HT HTML CSS D3.js D3 OpenGL – have "same" computational "power"; and LaTeX Digital Amati R Matlab – can express all possible computations; but JINJA IDL PostScript jQuery jQ Motivating application • the ease, concision, elegance, clarity, modularity, abstractness, efficiency, style, of these computations may vary radically across such languages. Plan 9 Plan 10 PL design: goals/values "Programming paradigms" • Im Imperative : execute step-by-step statements to PL design affects goals/values for programs: change mutable state. Lens: statements, execution, mutation, side effects. – Correctness, Reliability, Security • Fu Functional : compose functions over immutable data. – Clarity, Explainability, Learnability, Analyzability, Audibility Lens: expressions, evaluation, results, composition. – Fairness, Privacy • Ob Object-or oriented : pass (typically imperative) messages between objects. – Maintainability, Extensibility Lens: behaviors, methods, encapsulation, extension. – Efficiency (of programs, programmers), Optimizability • De Deduc ductive : query over declarative relationships. Lens: relations, implications, constraints, satisfiability. – Creativity, Expressivity, Flexibility • Plenty ty more… – … Imprecisely defined, overlapping. Most PLs blend a few. Plan 11 Plan 12
Quicksort PL design: dimensions void qsort(int a[], int lo, int hi) { int h, l, p, t; if (lo < hi) { • Fi First-class va values : What can be named, passed as an argument, l = lo; h = hi; returned as a result, stored in a data structure? Imperative Style p = a[hi]; (C; Java would be similar) • Naming : Do variables/parameters name expressions, values, or Na do { storage cells? How are names declared, referenced, scoped? while ((l < h) && (a[l] <= p)) l = l+1; while ((h > l) && (a[h] >= p)) Functional Style (SML) Fu • State : What is mutable or immutable? St h = h-1; if (l < h) { • Control : Conditionals, pattern matching, loops, exception Co t = a[l]; fun qsort [] = [] handling, continuations, parallelism, concurrency? a[l] = a[h]; a[h] = t; | qsort (x::xs) = } • Data : Products (arrays, tuples, records, maps), sums (options, Da let } while (l < h); (lt, ge) = List.partition (fn n => n < x) xs one-ofs, variants), objects with behavior? a[hi] = a[l]; in Types : Static? Dynamic? Polymorphic? Abstract? First-class? Ty • a[l] = p; (qsort lt) @ (x :: (qsort ge)) end • … … qsort( a, lo, l-1 ); qsort( a, l+1, hi ); } Plan 13 Plan 14 } Defining a programming language Sy Syntax : fo form of a PL – Structure of programs: symbols and grammar – Concrete syntax vs. abstract syntax trees (ASTs) Se Semantics : me meaning of a PL How is a PL defined? – Dy Dynamic Semantics : Behavior, actions, results of programs wh when evaluated. • Ev Evaluati tion rules es: What is the result or effect of evaluating each language construct? How are these composed? – St Static Semantics: Properties of programs determined wi without evaluation. Scope rules: to which declaration may a variable reference refer? • Sc Type rules: is a program well-typed (and therefore legal)? • Ty Plan 15 Plan 16
Concrete syntax: absolute value function Syntax (form) vs. Semantics (meaning) Logo : Lo to abs :n ifelse :n < 0 [output (0 - :n)] [output :n] end JS: JS function abs(n) {if (n<0) return -n; else return n;} Ja Java: static int abs(int n) {if (n<0) return -n; else return n;} Fu Furiously sleep ideas green colorless. Python: Ap Py App Invent ntor: def abs(n): Colorle Color less g green i ideas s sle leep f furiou ously ly. if n < 0: return -n else: Little br Li brown rabbi abbits sleep soundly. return n Racket: (define abs (lambda (n) (if (< n 0) (- n) n))) Ra Po PostScript: /abs {dup 0 lt {0 swap sub} if} def Fo Forth: : abs dup 0 < if 0 swap - then ; Plan 17 Plan 18 This AST abstracts the concrete Abstract Syntax Tree (AST): Dynamic semantics examples syntax for the Logo, JavaScript, absolute value function and Python definitions. The other definitions would have functionDeclaration different ASTs. What is the meaning of the following expression? body name params (1 + 11) * 10 conditionalStatement abs n What is printed by the following program? condition else then a = 1; b = a + 20; relationalExpression return return r operand2 print(b); o t a r operand1 e p value o value a = 300; lessThan varref intlit arithmeticExpression print(b); varref operand2 r o t operand1 a name r count = 0; e p o name fun inc() { count = count + 1; return count; } n 0 n subtract intlit varref fun dbl(ignore, x) { return x + x; } value name print(dbl(inc(), inc()); 0 n Plan 19 Plan 20
Recommend
More recommend