formal language more formally
play

Formal language More formally L ( G ) is the set of all sentences (a - PowerPoint PPT Presentation

Announcements CSCI 334: Principles of Programming Languages How did Lab 2 go? Lab 3 posted (pset) Small errors in book fi gures Lecture 4: PL Fundamentals II (thanks, Edwin!) Instructor: Dan Barowy Why couldnt you understand the


  1. Announcements CSCI 334: Principles of Programming Languages • How did Lab 2 go? • Lab 3 posted (pset) • Small errors in book fi gures Lecture 4: PL Fundamentals II (thanks, Edwin!) Instructor: Dan Barowy Why couldn’t you understand the script? It’s written in English, after all! We don’t know the “ground rules” for the document as it is written: • Surface appearance (“syntax”) • What is the set of valid symbols? • What combinations of symbols are permissible? • Deeper meaning (“semantics”) The York Plays (late 15th century) comprise one of the four complete surviving medieval play cycles • How does a given arrangement of sometimes known as ‘mystery cycles’. They are a series of short plays, known as ‘pageants’, which were symbols correspond to meaning? performed by members of di ff erent craft guilds (groups of people practicing the same trade who formed a club) at locations throughout the city of York. —British Library

  2. Formal language More formally L ( G ) is the set of all sentences (a “language”) de fi ned by the grammar, G . A formal language is the set of permissible sentences whose symbols are taken from an alphabet and G = ( N , Σ , P , S ) where whose word order is determined by a speci fi c set of N is a set of nonterminal symbols. rules . Σ is a set of terminal symbols. Intuition: a language that can be de fi ned mathematically, P is a set of production rules of the form 
 using a grammar . N ::= ( Σ ⋃ N )* where * means “zero or more” (Kleene star) and English is not a formal language. where ⋃ means set union Java is a formal language. S ∈ N denotes the “start symbol.” Backus-Naur Form (BNF) Backus-Naur Form (BNF) More concretely, for programming languages, we conventionally write G in a form called BNF. You should read the following BNF expression: Nonterminals, N , are in brackets: <expression> <num> ::= <digit> Terminals, Σ , are “bare”: x | <num><digit> A production rule, P , consists of the ::= operator, a as nonterminal on the left hand side, and a sequence of one or more symbols from N and Σ on “ num is de fi ned as a digit or as a num followed by a the right hand side. digit .” < variable> ::= x The | symbol means “alternatively”: <num> ::= 1 | 2 We use ε to denote the empty string nonterminal.

  3. Backus-Naur Form (BNF) Lambda calculus grammar The following de fi nition should look familiar: <expr> ::= <var> <expr> ::= <num> | <expr> + <expr> | <abs> 
 | <expr> - <expr> | <app> <num> ::= <digit> | <num><digit> <var> ::= x 
 <digit> ::= 0|1|2|3|4|5|6|7|8|9 <abs> ::= λ <var>.<expr> <expr> is the start symbol. <app> ::= <expr><expr> Conventionally, we ignore whitespace, but if it matters, <expr> is the start symbol. use the ␣ symbol. E.g., <expr> ␣ + ␣ <expr> Pro tip Parse Trees Don’t try to “understand” the lambda calculus. There are at least two forms of trees that we might refer to “parse trees” Aside from “variables,” “functions,” and “application,” it has no more meaning than regular algebra. We ascribe meanings to it later (as we do with algebra). The lambda calculus is simply a tool for reasoning by using the logic of computation.

  4. Derivation Tree Abstract Syntax Tree Describes exactly how input was parsed Abstracts over representation details e ::= n | e+e | e-e e ::= n | e+e | e-e n ::= d | nd n ::= d | nd d ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 d ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 1+2+3 1+2+3 e + e+e + 1 2 3 n e+e d n n 1 d d 2 3 Parse tree Abiguity We can create a “parse tree” by following the You might have noticed that there is an rules of a grammar as we interpret a alternative parse tree. sentence of a language. λ x.xx λ x.xx <expression> <expression> <abstraction> <application> <variable> <expression> <expression> <expression> x <application> <abstraction> <variable> <expression> <expression> <variable> <expression> x <variable> <variable> x <variable> x x x

  5. Parentheses disambiguate grammar Lambda calculus grammar <expr> ::= <var> <expr> = (<expr>) | <abs> 
 | <app> Axiom of equivalence for parens | <parens> <var> ::= x 
 <abs> ::= λ <var>.<expr> Let’s modify our grammar <app> ::= <expr><expr> <parens> ::= (<expr>) While we’re at it… Also… <expr> ::= <var> <expr> ::= <value> | <abs> 
 | <abs> 
 | <app> | <app> | <parens> | <parens> <var> ::= α ∈ { a ... z } 
 <var> ::= α ∈ { a ... z } 
 <abs> ::= λ <var>.<expr> <abs> ::= λ <var>.<expr> <app> ::= <expr><expr> <app> ::= <expr><expr> <parens> ::= (<expr>) <parens> ::= (<expr>) <value> ::= v ∈ ℕ | <var>

  6. This expression is now unambiguous Free vs bound variables ( λ x.x)x <expression> <application> ( λ x.x)x <expression> <parens> <variable> <expression> bound free x <abstraction> <variable> <expression> x <variable> x Evaluation: Lambda calculus is like algebra α -Reduction ( λ x.x)x ( λ x.x)x This expression has two di ff erent x variables Evaluation consists of simplifying an expression using text substitution. Which should we rename? Only two simpli fi cation rules: Rule: α -reduction λ x.<expr> = α λ y.[y/x]<expr> β -reduction [y/x] means “substitute y for x in <expr> ”

  7. α -Reduction β -Reduction ( λ x.x)x ( λ x.x)y ( λ y.[y/x]x)x How we “call” or apply a function to an argument ( λ y.y)x Rule: ( λ x.<expr>)y = β [y/x]<expr> Reduce this How far do we go? We keep going until there is nothing left to do done x done xx ( λ x.x)x λ x.y done ( λ x.xy)z not done That “most simpli fi ed” expression is called a normal form . An expression that can be simpli fi ed is a called a redex .

  8. Sometimes multiple simpli fi cations Example Order (mostly) does not matter M ( λ a. λ b.(- a b)) 2 1 If M → M 1 and M → M 2 then M 1 → * N and M 2 → * N M 1 M 2 for some N N “con fl uence” Activity Activity Leftmost reduction: Rightmost reduction: ( λ f. λ x.f(f x))( λ z.(+ x z))2 ( λ f. λ x.f(f x))( λ z.(+ x z))2

  9. Recap & Next Class Today we covered: Lambda calculus Next class: Lambda calculus Computability

Recommend


More recommend