overview introductory remarks equational programming lambda terms 2020 10 26 lecture 1 towards beta reduction overview functional programming a functional program is an expression, and is executed by evaluating the expression introductory remarks (use definitions from left to right) lambda terms focus on what and not so much on how towards beta reduction the functions are pure (or, mathematical) an input always gives the same output the course is about the foundations of functional programming
example functional programming style taste of Haskell definition of sum: in Haskell: applying functions to arguments sum [] = 0 sum (n:ns) = n + sum ns sum [1 .. 100] type of sum: in Java: changing stored values Num a => [a] -> a total = 0; that is: for (i = 1; i < 100; ++i) for any type a of numbers, sum maps a list of elements of a to a total = total + i; use of sum: application of the function sum to the argument [1,2,3] sum [1,2,3] evaluation by equational reasoning functional programming: properties definition: double x = x + x evaluation: double 2 high level of abstraction = { unfold definition double } 2 + 2 concise programs = { applying + } 4 more confidence in correctness double (double 2) (read, check, prove correct) = { unfold definition inner double } double (2 + 2) higher-order functions = {unfold definition double } (2 + 2) + (2 + 2) foundations: equational reasoning and λ -calculus = {apply first +} 4 + (2+2) = {apply last +} 4 + 4 = {apply +} 8
Haskell: properties functional programming: some history lazy evaluation strategy Lisp John McCarthy (1927–2011), Turing Award 1971 static typing FP John Backus (1924–2007), Turing Award 1977 type inference system ML Robin Milner (1934-2010), Turing Award 1991, et al purely functional ] Miranda David Turner (born 1946) Haskell functional programming languages typed untyped strict ML Lisp lazy Haskell Haskell See also F# (Microsoft), Erlang (Ericsson), Scala (Java plus ML) a group containing ao Philip Wadler and Simon Peyton Jones
functional programming and lambda calculus course equational programming (EP) Based on the lambda calculus, Lisp rapidly became ... (from: wikipedia page John McCarthy) lambda calculus Haskell is based on the lambda calculus, hence the lambda we use as a logo. (from: the Haskell website) equational specifications Historically, ML stands for metalanguage: it was conceived to develop proof exercises functional programming: Haskell tactics in the LCF theorem prover (whose language, pplambda, a combination of the first-order predicate calculus and the simply typed polymorphic lambda calculus, had ML as its metalanguage). (from: wikipedia page of ML) overview lambda calculus introductory remarks inventor: Alonzo Church (1936) lambda terms a language expressing functions or algorithms towards beta reduction concept of computability and basis of functional programming a language expressing proofs untyped and typed
historical note: notation for functions language and theory Frege defined the graph of a function (1893) alphabet Σ: finite set of symbols Russell and Whitehead and Russell (1910) language L ⊆ Σ ∗ : set of sentences over the alphabet Sch¨ onfinkel defined function calculus (1920) theory T ⊆ L : set of correct sentences Curry defined combinary logic (1920) equational theory sentences of the form l = r with l and r ‘terms’ in Σ ∗ notation for (anonymous) functions lambda terms: intuition mathematical notation: f : N → N abstraction: f ( x ) = x 2 λ x . M is the function mapping x to M or also: f : N → N λ x . x is the function mapping x to x f : x �→ x 2 λ x . x 2 is the function mapping x to x 2 lambda notation for functions: application: λ x . x 2 F M is the application of the function F to its argument M and notation for application of a function to an argument: (not the result of applying) ( λ x . x 2 ) 7
from alphabet to langage: inductive definition of lambda famous terms terms I = ( λ x . x ) we assume a countably infinite set of variables ( x , y , z . . . ) K = ( λ x . ( λ y . x )) sometimes we in addition assume a set of contstants S = ( λ x . ( λ y . ( λ z . (( x z ) ( y z ))))) Ω = (( λ x . ( x x )) ( λ x . ( x x ))) the set of λ -terms is defined inductively by the following clauses: a variable x is a λ -term omit outermost parentheses a constant c is a λ -term abstraction is associative to the right if M is a λ -term, then ( λ x . M ) is a λ -term, called an abstraction application is associative to the left if F and M are λ -terms, then ( F M ) is a λ -term, called an application lambda extends to the right as far as possible terms as trees: example terms as trees: general @ � ❅ x @ λ x @ � ❅ � ❅ y λ x x M F M x a subterm corresponds to a subtree subterms of λ x . y are λ x . y and y
parentheses inductive definition of terms application is associative to the left ( M N P ) instead of (( M N ) P ) outermost parentheses are omitted definitions recursively on the definition of terms M N P instead of ( M N P ) example: definition of the free variables of a term lambda extends to the right as far as possible λ x . M N instead of λ x . ( M N ) proofs by induction on the definition of terms sometimes we combine lambdas λ x 1 . . . x n . M instead of λ x 1 . . . . λ x n . M example: every term has finitely many free variables ( λ x . λ y . M ) instead of ( λ x . ( λ y . M )) ( M λ x . N ) instead of ( M ( λ x . N )) λ xy . M instead of λ x . λ y . M bound variables: definition free variables: definition a variable that is not bound is free x is bound by the first λ x above it in the term tree alternatively: define recursively the set FV( M ) of free variables of M : examples: the underlined x is bound in λ x . x FV( x ) = { x } λ x . x x FV( c ) = ∅ ( λ x . x ) x FV( λ x . M ) = FV( M ) \{ x } λ x . y x FV( F P ) = FV( F ) ∪ FV( P ) λ x . λ x . x a term is closed if it has no free variables
overview from language to theory β -axiom: ( λ x . M ) N = M [ x := N ] ( what is this? ) introductory remarks reflexivity, symmetry, transitivity: lambda terms M = M if M = N then N = M towards beta reduction if M = N and N = P then M = P compatible with term formation: if M = N then L M = L N if M = N then M R = N R if M = N then λ x . M = λ x . N examples theory about computation: use the axiom from left to right ( λ x . M ) N = M [ x := N ] M [ x := N ] is the result of substituting N for the free occurrences of x in M β -axiom: ( λ x . M ) N → β M [ x := N ] ( λ x . x 2 ) 7 = 7 2 = 49 compatible with term formation: ( λ x . x + 3) 7 = 7 + 3 = 10 if M → β N then L M → β L N ( λ x . x + x ) 7 = 7 + 7 = 14 if M → β N then M R → β N R if M → β N then λ x . M → β λ x . N ( λ x . 5) 7 = 5 and also in a ‘context’: λ y . ( λ x . 5) 7 = λ y . 5, and (( λ x . 5) 7) z = 5 z , and z (( λ x . 5) 7) = z 5
examples the definition of substitution needs more care ( λ x . M ) N → β M [ x := N ] M [ x := N ] is the result of substituting N for the free occurrences of x in M (( λ x . λ y . x ) y ) z → β ( λ y . y ) z → β z ( λ x . x 2 ) 7 → β 7 2 = 49 This is incorrect! It should be: ( λ x . x + 3) 7 → β 7 + 3 = 10 (( λ x . λ y ′ . x ) y ) z → β ( λ y ′ . y ) z → β y ( λ x . x + x ) 7 → β 7 + 7 = 14 ( λ x . 5) 7 → β 5 and also in a ‘context’: λ y . ( λ x . 5) 7 → β λ y . 5, and (( λ x . 5) 7) z → β 5 z , and z (( λ x . 5) 7) → β z 5 intuitive approach to solution substitution: examples we work ‘modulo renaming of bound variables’, or α -conversion we rename (use another representative of the same α -equivalence class) whenever necessary to avoid capturing of free variables ( λ x . x )[ x := c] = λ x . x we define substitution recursively: ( λ x . y )[ y := c] = λ x . c x [ x := N ] = N ( λ x . y )[ y := x ] = λ z . x a [ x := N ] = a with a � = x a variable or a constant ( λ y . x ( λ w . v w x ))[ x := u v ] = λ y . u v ( λ w . v w ( u v )) ( P Q )[ x := N ] = ( P [ x := N ]) ( Q [ x := N ]) ( λ y . x ( λ x . x ))[ x := λ y . x y ] = λ y . ( λ y . x y ) ( λ x . x ) ( λ x . P )[ x := N ] = λ x . P ( λ y . P )[ x := N ] = λ y . ( P [ x := N ]) (so: we assume that λ y does not capture free occurrences of y in N )
Recommend
More recommend