λ λ λ λ CS 251 Fall 2019 CS 251 Fall 2019 CS 251 Fall 2019 CS 251 Fall 2019 Principles of Programming Languages Principles of Programming Languages Principles of Programming Languages Principles of Programming Languages Ben Wood Ben Wood Ben Wood Ben Wood CS 251 Part 1: Defining Racket: How to Program Expressions and Bindings via the meta-language of PL definitions https://cs.wellesley.edu/~cs251/f19/ https://cs.wellesley.edu/~cs251/f19/ 0 1 Expressions, Bindings, Meta-language Expressions, Bindings, Meta-language Topics / Goals From AI to language-oriented programming LISP: List Processing language, 1950s-60s, MIT AI Lab. LI 1. Basic language forms and evaluation model. Advice Taker: represent logic as data, not just as a program. Metaprogramming and programs as data: 2. Foundations of defining syntax and semantics. • Symbolic computation (not just number crunching) • Programs that manipulate logic (and run it too) – Informal descriptions (English) Scheme : child of Lisp, 1970s, MIT AI Lab. Sc – Formal descriptions (meta-language): Still motivated by AI applications, became more "functional" than Lisp. • Grammars for syntax. Important design changes/additions/cleanup: • simpler naming and function treatment • Judgments, inference rules, and derivations • lexical scope for big-step operational semantics. • first-class continuations • tail-call optimization, … 3. Learn Racket. (an opinionated subset) Ra Racket : child of Scheme, 1990s-2010s, PLT group. – Not always idiomatic or the full story. Setup for transition to Standard ML. Revisions to Scheme for: • Rapid implementation of new languages. • Education. Became Racket in 2010. Expressions, Bindings, Meta-language 2 Expressions, Bindings, Meta-language 3
Defining Racket PL design/implementation: layers To define each new language feature: – Define its sy synta tax . How is it written? – Define its dy dynamic semantics as ev evaluation rules . kernel ke How is it evaluated? primitive pr e values es , data types Features 1. Ex 1. Expr pression ons syntactic sugar • A few today, more to come. 2. Bindings standard libraries 3. That's all! • A couple more advanced features later. user libraries Expressions, Bindings, Meta-language 4 Expressions, Bindings, Meta-language 5 Values Addition expression Expressions that cannot be evaluated further. Synta Sy ntax: : (+ e1 e2 ) – Parentheses required: no extras, no omissions. Sy Synta ntax: – e1 and e2 stand in for any expressions. Numbers: 251 240 301 – Note prefix notation. Note No e re recursiv ive Booleans: #t #f str struc uctur ture! … Examples: (+ 251 240) (+ (+ 251 240) 301) Evaluation: (+ #t 251) Values evaluate to themselves. Expressions, Bindings, Meta-language 6 Expressions, Bindings, Meta-language 7
Addition expression Addition expression Not quite! No e! Syntax: (+ e1 e2 ) Syntax: (+ e1 e2 ) No Note e re recursiv ive str struc uctur ture! Evaluation: Ev Evaluation: Ev Dyna Dy nami mic t type pe c checking ng 1. Evaluate e1 to a value v1 . 1. Evaluate e1 to a value v1 . 2. Evaluate e2 to a value v2 . 2. Evaluate e2 to a value v2 . 3. Return the ar arithmetic su sum of v1 + v2 . 3. 3. If If v1 and and v2 ar are nu numbers then return the ar arithmetic su sum of v1 + v2 . Otherwise there is a type error. Ot Expressions, Bindings, Meta-language 8 Expressions, Bindings, Meta-language 9 ! The l anguage o f l anguages A grammar formalizes syntax. Because it pays to be precise. no non-te terminal sy symbols Sy Synta ntax: e ::= v – Formal grammar notation Non-terminal e has 2 pr productions , | ( + e e ) – Conventions for writing syntax patterns separated by "|". Grammar meta-syntax. terminal symbols "An expression e is one of: – Any value v – Any addition expression (+ e e ) of any two expressions" Expressions, Bindings, Meta-language 10 Expressions, Bindings, Meta-language 11
Racket syntax so far Notation conventions Expressions Outside the grammar: e ::= v • Use of a non-terminal symbol, such as e , in syntax | ( + e e ) examples and evaluation rules means any expression matching one of the productions of e in the grammar. Literal Values • Two uses of e in the same context are aliases; they mean v ::= #f | #t | n the same expression. Number values • Subscripts (or suffixes) distinguish separate instances of a n ::= 0 | 1 | 2 | … single non-terminal, e.g. , e 1 , e 2 , …, e n or e1 , e2 , …, en. Expressions, Bindings, Meta-language 12 Expressions, Bindings, Meta-language 13 ! The l anguage o f l anguages Judgments and rules formalize semantics. Because it pays to be precise. Judgment e ↓ v Ju Syntax: means "expression e evaluates to value v." – Formal grammar notation – Conventions for writing syntax patterns It is implemented by in rules for different cases: infere rence ru Semantics Se cs: value rule : [value] v ↓ v – Judgments: addition rule : • formal assertions, like functions if e1 e1 ↓ n1 n1 e1 ↓ n1 – Inference rules: and e2 e2 ↓ n2 n2 e2 ↓ n2 and n is the arithmetic sum • implications between judgments, like cases of functions of n1 n1 and n2 n2 n = n1 + n2 [add] – Derivations: then (+ (+ e1 e1 e2 e2) ↓ n (+ e1 e2) ↓ n • deductions based on rules, like applying functions … Expressions, Bindings, Meta-language 14 Expressions, Bindings, Meta-language 15
If If al all pr premises hold ho Inference rules Evaluation derivations th then th the c conclusio ion ho holds . Inference rule no notatio ion and me mean anin ing Axiom (no premises) Bar is optional for axioms. An evaluation de derivation on is a "proof" that an expression [value] evaluates to a value using the evaluation rules. Conclusion v ↓ v Rule name (+ 3 (+ 5 4)) ↓ 12 by the addition rule because: – 3 ↓ 3 by the value rule, where 3 is a number Number values, not just any values. Models dynamic type checking. – and (+ 5 4) ↓ 9 by the addition rule , where 9 is a number, because: e1 ↓ n1 " v v is the arithmetic sum • 5 ↓ 5 by the value rule, where 5 is a number of the numbers n1 n1 and n2 n2 ." ." e2 ↓ n2 Premises (not Racket syntax) • and 4 ↓ 4 by the value rule, where 4 is a number n = n1 + n2 [add] Conclusion • and 9 is the sum of 5 and 4 (+ e1 e2) ↓ n Rule name – and 12 is the sum of 3 and 9 . Expressions, Bindings, Meta-language 16 Expressions, Bindings, Meta-language 17 Evaluation derivations Errors are modeled by “stuck” derivations. Rules defining the evaluation judgment How to evaluate How to evaluate [value] Adding vertical bars e ↓ v (+ (+ 1 2) (+ 5 #f)) ? (+ #t (+ 5 4)) ? v ↓ v helps clarify nesting. #t ↓ n 1 ↓ 1 [value] e1 ↓ n1 3 ↓ 3 [value] 2 ↓ 2 [value] e2 ↓ n2 5 ↓ 5 [value] 3 = 1 + 2 n = n1 + n2 4 ↓ 4 [value] 5 ↓ 5 [value] [add] [add] (+ 1 2) ↓ 3 (+ e1 e2) ↓ n 9 = 5 + 4 4 ↓ 4 [value] [add] 5 ↓ 5 [value] (+ 5 4) ↓ 9 9 = 5 + 4 #f ↓ n [add] [add] (+ 5 4) ↓ 9 [add] Stuck. Can’t apply the [add] rule 12 = 3 + 9 [add] because there is no rule that Stuck. Can’t apply the [add] rule allows #t to evaluate to a number. because there is no rule that (+ 3 (+ 5 4)) ↓ 12 allows #t to evaluate to a number. Expressions, Bindings, Meta-language 18 Expressions, Bindings, Meta-language 19
Conditional if expressions Other number expressions Sy Synta ntax: (if e1 e2 e3 ) Similar syntax and evaluation for: + - * / quotient < > <= >= = Some small differences. Ev Evaluation: Build syntax and evaluation rules for: 1. Evaluate e1 to a value v1 . quotient and > 2. If v1 is not the value #f then evaluate e2 and return the result otherwise evaluate e3 and return the result Expressions, Bindings, Meta-language 20 Expressions, Bindings, Meta-language 21 if ex expressions Evaluation rules for if expressions. if expressions are ex expressions . e1 ↓ v1 Racket has no "statements!" e3 is is n not e t evaluated! e2 ↓ v2 v1 is not #f [if nonfalse] (if (< 9 (- 251 240)) (if e1 e2 e3) ↓ v2 (+ 4 (* 3 2)) (+ 4 (* 3 3))) (+ 4 (* 3 (if (< 9 (- 251 240)) 2 3))) e1 ↓ #f e2 is is n not e t evaluated! e3 ↓ v3 (if (if (< 1 2) (> 4 3) (> 5 6)) (if e1 e2 e3) ↓ v3 [if false] (+ 7 8) (* 9 10) Notice: at most one of these rules can have its premises satisfied! Expressions, Bindings, Meta-language 22 Expressions, Bindings, Meta-language 23
Recommend
More recommend