cs 251 fall 2019 cs 251 fall 2019 principles of
play

CS 251 Fall 2019 CS 251 Fall 2019 Principles of Programming - PowerPoint PPT Presentation

CS 251 Fall 2019 CS 251 Fall 2019 Principles of Programming Languages Principles of Programming Languages Ben Wood Ben Wood The Plan https://cs.wellesley.edu/~cs251/f19/ Plan 1 PL = P rogramming L anguage 1. What is a PL? 2. What


  1. λ λ CS 251 Fall 2019 CS 251 Fall 2019 Principles of Programming Languages Principles of Programming Languages Ben Wood Ben Wood The Plan https://cs.wellesley.edu/~cs251/f19/ Plan 1

  2. PL = P rogramming L anguage 1. What is a PL? 2. What goes into PL design? 3. How is a PL defined? 4. Why study PLs? What will you learn? Plan 2

  3. What is a P rogramming L anguage? Plan 3

  4. 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 sequence to the machine. – Laurence Atkinson, Pascal Programming Plan 4

  5. PL = Presentation of Logic … a computer language is not just a way of getting a computer to perform operations but rather that it is a novel formal medium for expressing ideas about methodology. Thus, programs must be written for people to read, and only incidentally for machines to execute. – Harold Abelson and Gerald J. Sussman, Structure and Interpretation of Computer Programs Plan 5

  6. PL = Problem-solving Lens A good programming language is a conceptual universe for thinking about programming. A language that doesn't affect the way you think about programming is not worth knowing. – Alan Perlis Plan 6

  7. PL = Precise Laws PL Big idea #1: Abstraction Im Implementer / / User / Client Us Contract / A Co / API Designer Des ner PL PL PL 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

  8. What goes into PL design? Plan 8

  9. PL design: application / purpose General computation Perl Java Python FORTRAN JavaScript ML Racket Rac Haskell Ruby C/C ++ C# C# Rust Sc Scala CommonLisp Domain-specific computation Excel HT HTML CSS D3.js D3 OpenGL LaTeX Digital Amati R Matlab JINJA IDL PostScript jQ jQuery Motivating application Plan 9

  10. Computability lete = equivalent to key models of computation Tu Turing-co comple – Tu Turing machine (CS 235) – (L (Lambda) ) λ -ca calcu culus (CS 251) – … Ch Church-Tu Turing thesis: Turing-complete = computable ⇒ All Turing-complete PLs (roughly, general-purpose PLs or just "PLs") – have "same" computational "power"; and – can express all possible computations; but • the ease, concision, elegance, clarity, modularity, abstractness, efficiency, style, of these computations may vary radically across such languages. Plan 10

  11. PL design: goals/values PL design affects goals/values for programs: – Correctness, Reliability, Security – Clarity, Explainability, Learnability, Analyzability, Audibility – Fairness, Privacy – Maintainability, Extensibility – Efficiency (of programs, programmers), Optimizability – Creativity, Expressivity, Flexibility – … Plan 11

  12. "Programming paradigms" • Im Imperative : execute step-by-step statements to change mutable state. Lens: statements, execution, mutation, side effects. • Fu Functional : compose functions over immutable data. Lens: expressions, evaluation, results, composition. • Ob Object-or oriented : pass (typically imperative) messages between objects. Lens: behaviors, methods, encapsulation, extension. • De Deduc ductive : query over declarative relationships. Lens: relations, implications, constraints, satisfiability. • Plenty ty more… Imprecisely defined, overlapping. Most PLs blend a few. Plan 12

  13. Quicksort void qsort(int a[], int lo, int hi) { int h, l, p, t; if (lo < hi) { l = lo; h = hi; Imperative Style p = a[hi]; (C; Java would be similar) do { while ((l < h) && (a[l] <= p)) l = l+1; while ((h > l) && (a[h] >= p)) Functional Style (SML) Fu h = h-1; if (l < h) { t = a[l]; fun qsort [] = [] a[l] = a[h]; a[h] = t; | qsort (x::xs) = } let } while (l < h); (lt, ge) = List.partition (fn n => n < x) xs a[hi] = a[l]; in a[l] = p; (qsort lt) @ (x :: (qsort ge)) end qsort( a, lo, l-1 ); qsort( a, l+1, hi ); } Plan 13 }

  14. PL design: dimensions • Fi First-cl class values es : What can be named, passed as an argument, returned as a result, stored in a data structure? • Na Naming : Do variables/parameters name expressions, values, or storage cells? How are names declared, referenced, scoped? State : What is mutable or immutable? • St • Co Control : Conditionals, pattern matching, loops, exception handling, continuations, parallelism, concurrency? • Da Data : Products (arrays, tuples, records, maps), sums (options, one-ofs, variants), objects with behavior? Types : Static? Dynamic? Polymorphic? Abstract? First-class? • Ty • … … Plan 14

  15. How is a PL defined? Plan 15

  16. 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 – 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. • Sc Scope rules: to which declaration may a variable reference refer? • Ty Type rules: is a program well-typed (and therefore legal)? Plan 16

  17. Syntax (form) vs. Semantics (meaning) Fu Furiously sleep ideas green colorless. Color Colorle less g green i ideas s sle leep f furiou ously ly. Li Little br brown rabbi abbits sleep soundly. Plan 17

  18. Concrete syntax: absolute value function Lo Logo : 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;} Py Python: Ap App Invent ntor: def abs(n): if n < 0: return -n else: return n Ra Racket: (define abs (lambda (n) (if (< n 0) (- n) n))) Po PostScript: /abs {dup 0 lt {0 swap sub} if} def Fo Forth: : abs dup 0 < if 0 swap - then ; Plan 18

  19. This AST abstracts the concrete Abstract Syntax Tree (AST): syntax for the Logo, JavaScript, absolute value function and Python definitions. The other definitions would have functionDeclaration different ASTs. body e m a n params conditionalStatement abs n n o i t i else d n o c then relationalExpression return return operand2 operator operand1 value value lessThan varref intlit arithmeticExpression varref operand2 operator o name p name e r a n n 0 n d subtract intlit 1 varref value name n 0 Plan 19

  20. Dynamic semantics examples What is the meaning of the following expression? (1 + 11) * 10 What is printed by the following program? 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()); Plan 20

  21. Static semantics example: type checking Which of the following Java examples can be well-typed (i.e., pass the type checker)? How do you know? What assumptions are you making? 2 * (3 + 4) A public boolean f(int i, boolean b) { F if (a) { G c = a + b; return b && (i > 0); B 2 < (3 + 4) } } else { c = a * b; } public int g(int i, boolean b) { C 2 < True H return i * (b ? 1 : -1); } D if (a < b) { c = a + b; I public int p(int w) { } else { if (w > 0) { return 2*w; } c = a * b; } } public int q(int x) { return x > 0; } J if (a < b) { E c = a + b; public int r(int y) { return g(y, y>0); } K } else { c = a > b; public boolean s(int z) { return f(z); } L } Plan 21

  22. Static semantics example: termination checking Which of these Python programs has inputs def f(x): for which it does not terminate (runs forever)? return x+1 def g(x): def g2(x): while True: return g2(x) def collatz(x): pass while x != 1: return x if ( x % 2) == 0: x = x/2 else : def h(x): def h2(x): x = 3*x + 1 while x > 0: if x <= 0: return 1 x = x+1 return x return x else : return h2(x+1) Plan 22

  23. Static semantics Properties of programs determined wi without evaluation. – Sc Scope: To which declarations do variable references refer? – Ty Types: What are the types of entities in the program? – … Go Goal: Accept only (and all) sa safe programs free of various problems. Will any evaluation of this program ever: – reference a nonexistent variable? – index outside an array's bounds? dereference null? divide by zero? – apply an array operation to an integer? – coordinate concurrency unsafely? – access a given object again? surpass a given memory budget? – leak sensitive information over the network? – ... not terminate (run forever)? reach a given point in the program? – … Reality: Most useful static semantics questions for Turing-complete Re able! (Rice's Theorem, CS 235) languages are unco uncomput utab Plan 23

Recommend


More recommend