simply typed lambda calculus, progress and preservation

45
Lambda Calculus and Haskell Review, cont.

Upload: others

Post on 18-May-2022

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Simply Typed Lambda Calculus, Progress and Preservation

Lambda Calculus and Haskell Review, cont.

Page 2: Simply Typed Lambda Calculus, Progress and Preservation

Outline

n Pure lambda calculus, a reviewn Syntax and semantics (last class)n Free and bound variables (last class)n Substitution (last class)n Rulesn Normal formsn Reduction strategies

n Interpreters for the Lambda calculusn Coding them in HaskellProgram Analysis CSCI 4450/6450, A Milanova 2

Page 3: Simply Typed Lambda Calculus, Progress and Preservation

3

Substitution, formally

n (lx.E) M à E[M/x] replaces all free occurrences of x in E by M

n E[M/x] is defined by cases on E:n Var: y[M/x] =

y[M/x] =n App: (E1 E2)[M/x] =n Abs: (ly.E1)[M/x] =

(ly.E1)[M/x] =

M if x = yy otherwise

(E1[M/x] E2[M/x])(ly.E1) if x = ylz.((E1[z/y])[M/x]) otherwise,

where z NOT in free(E1) U free(M) U {x}

Page 4: Simply Typed Lambda Calculus, Progress and Preservation

Substitution, formally

(lx.ly. x y) (y w) à (ly. x y)[(y w)/x]à l1_. ( ((x y)[1_/y])[(y w)/x] ) à l1_. ( (x 1_)[(y w)/x] )à l1_. ( (y w) 1_ )à l1_. y w 1_You will have to implement precisely this substitution algorithm in HaskellProgram Analysis CSCI 4450/6450, A Milanova 4

Page 5: Simply Typed Lambda Calculus, Progress and Preservation

Program Analysis CSCI 4450/6450, A Milanova 5

Rules (Axioms) of Lambda Calculus

n a rule (a-conversion): renaming of parameter (choice of parameter name does not matter)n lx. E àa lz. (E[z/x]) provided z is not free in En e.g., lx. x x is the same as lz. z z

n b rule (b-reduction): function application (substitutes argument for parameter)n (lx.E) M àb E[M/x]Note: E[M/x] as defined on previous slide!n e.g., (lx. x) z àb z

Page 6: Simply Typed Lambda Calculus, Progress and Preservation

Program Analysis CSCI 4450/6450, A Milanova 6

Rules of Lambda Calculus: Exercises

n Reduce

(lx. x) y à ?

(lx. x) (ly. y) à ?

(lx.ly.lz. x z (y z)) (lu. u) (lv. v) à ?

Page 7: Simply Typed Lambda Calculus, Progress and Preservation

Program Analysis CSCI 4450/6450, A Milanova 7

Rules of Lambda Calculus: Exercises

(lx.ly.lz. x z (y z)) (lu. u) (lv. v) àab

Page 8: Simply Typed Lambda Calculus, Progress and Preservation

Program Analysis CSCI 4450/6450, A Milanova 8

Reductions

n An expression ( lx.E ) M is called a redex(for reducible expression)

n An expression is in normal form if it cannot be β-reduced

n The normal form is the meaning of the term, the “answer”

Page 9: Simply Typed Lambda Calculus, Progress and Preservation

Definitions of Normal Form

n Normal form (NF): a term without redexesn Head normal form (HNF)

n x is in HNFn (lx. E) is in HNF if E is in HNF n (x E1 E2 … En) is in HNF

n Weak head normal form (WHNF)n x is in WHNFn (lx. E) is in WHNFn (x E1 E2 … En) is in WHNF

Program Analysis CSCI 4450/6450, A Milanova (from MIT’s 2015 Program Analysis OCW) 9

Page 10: Simply Typed Lambda Calculus, Progress and Preservation

Questions

n lz. z z is in NF, HNF, or WHNF?n (lz. z z) (lx. x) is in?n lx.ly.lz. x z (y (lu. u)) is in?

n (lx.ly. x) z ((lx. z x) (lx. z x)) is in?n z ((lx. z x) (lx. z x)) is in?n (lz.(lx.ly. x) z ((lx. z x) (lx. z x))) is in?

Program Analysis CSCI 4450/6450, A Milanova 10

Page 11: Simply Typed Lambda Calculus, Progress and Preservation

Simple Reduction Exercise

n C = lx.ly.lf. f x yn H = lf. f (lx.ly. x) T = lf. f (lx.ly. y)n What is H (C a b)?à (lf. f (lx.ly. x)) (C a b)à (C a b) (lx.ly. x)à ((lx.ly.lf. f x y) a b) (lx.ly. x)à (lf. f a b) (lx.ly. x)à (lx.ly. x) a b à a CSCI 4450/6450, A Milanova (from MIT 2015 Program Analysis OCW) 11

Page 12: Simply Typed Lambda Calculus, Progress and Preservation

Program Analysis CSCI 4450/6450, A Milanova 12

Exercise

n S = lx.ly.lz. x z (y z)n I = lx. xn What is S I I I?( lx.ly.lz. x z (y z) ) I I Ià (ly.lz. I z (y z) ) I Ià (lz. I z (I z) ) Ià I I (I I) = (lx. x) I (I I)à I (I I) = (lx. x) (I I)à I I = (lx. x) I à I

An expression with no free variables is called combinator.S, I, C, H, T are combinators.

Reducible expression is underlinedat each step.

Page 13: Simply Typed Lambda Calculus, Progress and Preservation

Aside: Trace Semantics

n Models a trace of program executionn In the imperative world

n Basic operation: assignment statementn Execution (transition system) is a sequence of

state transitionsn Assignment: lj : x = E; li : …(lj,σ) à (li, σ[xß|[E]|(σ)])n Assignment: lj : x = E1 Op E2 ; li : …(lj,σ) à (li, σ[xß|[E1]|(σ) Op |[E2]|(σ)])

Program Analysis CSCI 4450/6450, A Milanova 13

Page 14: Simply Typed Lambda Calculus, Progress and Preservation

Aside: Trace Semantics

n In the functional world n Basic operation is function applicationn Execution (transition system) is a sequence of

β-reductions( lx.ly.lz. x z (y z) ) I I Ià (ly.lz. I z (y z) ) I Ià (lz. I z (I z) ) I…lx.x

14

Page 15: Simply Typed Lambda Calculus, Progress and Preservation

Outline

n Pure lambda calculus, a reviewn Syntax and semantics (last class)n Free and bound variables (last class)n Substitution (last class)n Rulesn Normal formn Reduction strategies

n Lambda calculus interpretersn Coding them in HaskellProgram Analysis CSCI 4450/6450, A Milanova 15

Page 16: Simply Typed Lambda Calculus, Progress and Preservation

16

Reduction Strategyn Let us look at (lx.ly.lz. x z (y z)) (lu. u) (lv. v)

n Actually, there are (at least) two “reduction paths”:Path 1: (lx.ly.lz. x z (y z)) (lu. u) (lv. v) àβ

(ly.lz. (lu. u) z (y z)) (lv. v) àβ

Path 2: (lx.ly.lz. x z (y z)) (lu. u) (lv. v) àβ(ly.lz. (lu. u) z (y z)) (lv. v) àβ

Page 17: Simply Typed Lambda Calculus, Progress and Preservation

17

Reduction Strategyn Let us look at (lx.ly.lz. x z (y z)) (lu. u) (lv. v)

n Actually, there are (at least) two “reduction paths”:Path 1: (lx.ly.lz. x z (y z)) (lu. u) (lv. v) àβ

(ly.lz. (lu. u) z (y z)) (lv. v) àβ(lz. (lu. u) z ((lv. v) z)) àβ (lz. z ((lv. v) z)) àβlz. z z

Path 2: (lx.ly.lz. x z (y z)) (lu. u) (lv. v) àβ(ly.lz. (lu. u) z (y z)) (lv. v) àβ(ly.lz. z (y z)) (lv. v) àβ (lz. z ((lv. v) z)) àβlz. z z

Page 18: Simply Typed Lambda Calculus, Progress and Preservation

Program Analysis CSCI 4450/6450, A Milanova 18

Reduction Strategy

n A reduction strategy (also called evaluation order) is a strategy for choosing redexesn How do we arrive at the normal form (answer)?

n Applicative order reduction chooses the leftmost-innermost redex in an expressionn Also referred to as call-by-value reduction

n Normal order reduction chooses the leftmost-outermost redex in an expressionn Also referred to as call-by-name reduction

Page 19: Simply Typed Lambda Calculus, Progress and Preservation

Reduction Strategy

Program Analysis CSCI 4450/6450, A Milanova 19

Page 20: Simply Typed Lambda Calculus, Progress and Preservation

20

Reduction Strategy: Examples

n Evaluate (lx. x x) ( (ly. y) (lz. z) )n Using applicative order reduction:

n Using normal order reduction

Page 21: Simply Typed Lambda Calculus, Progress and Preservation

Program Analysis CSCI 4450/6450, A Milanova 21

Reduction Strategy

n In our examples, both strategies produced the same result. This is not always the casen First, look at expression (lx. x x) (lx. x x). What

happens when we apply β-reduction to this expression?

n Then look at (lz.y) ((lx. x x) (lx. x x))n Applicative order reduction – what happens?n Normal order reduction – what happens?

Page 22: Simply Typed Lambda Calculus, Progress and Preservation

Program Analysis CSCI 4450/6450, A Milanova 22

Church-Rosser Theorem

n Normal form implies that there are no more reductions possible

n Church-Rosser Theorem, informallyn If normal form exists, then it is unique (i.e., result

of computation does not depend on the order that reductions are applied; i.e., no expression can have two distinct normal forms)

n If normal form exists, then normal order will find it

Page 23: Simply Typed Lambda Calculus, Progress and Preservation

Program Analysis CSCI 4450/6450, A Milanova 23

Reduction Strategy

n Intuitively:

n Applicative order (call-by-value) is an eager evaluation strategy. Also known as strict

n Normal order (call-by-name) is a lazyevaluation strategy

n What order of evaluation do most PLs use?

Page 24: Simply Typed Lambda Calculus, Progress and Preservation

Exercises

n Evaluate (lx.ly. x y) ((lz. z) w) n Using applicative order reduction

n Using normal order reduction

24Program Analysis CSCI 4450/6450, A Milanova

Page 25: Simply Typed Lambda Calculus, Progress and Preservation

Interpreters

n An interpreter for the lambda calculus is a program that reduces lambda expressions to “answers”

n We must specifyn The definition of “answer”. Which normal form?n The reduction strategy. How do we choose

redexes in an expression?

Program Analysis CSCI 4450/6450, A Milanova 25

Page 26: Simply Typed Lambda Calculus, Progress and Preservation

An Interpreter

n Definition by cases on E ::= x | lx. E1 | E1 E2interpret(x) = xinterpret(lx.E1) = lx.E1interpret(E1 E2) = let f = interpret(E1)

in case f oflx.E3 -> interpret(E3[E2/x])

- -> f E2

n What normal form: Weak head normal formn What strategy: Normal order

26

Haskell syntax:let …. in case f of ->

Program Analysis CSCI 4450/6450, A Milanova (modified from MIT 2015 Program Analysis OCW)

Page 27: Simply Typed Lambda Calculus, Progress and Preservation

Another Interpreter

n Definition by cases on E ::= x | lx. E1 | E1 E2interpret(x) = xinterpret(lx.E1) = lx.E1interpret(E1 E2) = let f = interpret(E1)

a = interpret(E2)in case f of

lx.E3 à interpret(E3[a/x])- à f a

n What normal form: Weak head normal formn What strategy: Applicative order 27

Page 28: Simply Typed Lambda Calculus, Progress and Preservation

Outline

n Pure lambda calculus, a reviewn Syntax and semantics (last class)n Free and bound variables (last class)n Substitution (last class)n Rulesn Reduction strategiesn Normal form

n Lambda calculus interpretersn Coding them in HaskellProgram Analysis CSCI 4450/6450, A Milanova 28

Page 29: Simply Typed Lambda Calculus, Progress and Preservation

Coding them in Haskell

n In HW5 you will code an interpreter in Haskelln Haskell

n A functional programming language

n Key ideasn Lazy evaluationn Static typing and polymorphic type inferencen Algebraic data types and pattern matchingn Monads … and more

Program Analysis CSCI 4450/6450, A Milanova 29

Page 30: Simply Typed Lambda Calculus, Progress and Preservation

Lazy Evaluation

n Unlike Scheme (and most programming languages) Haskell does lazy evaluation, i.e., normal order reductionn It won’t evaluate an argument expr. until it is needed

> f x = [] // f takes x and returns the empty list> f (repeat 1) // returns? > []> head (tail [1..]) // returns? > 2 // [1..] is infinite list of integersn Lazy evaluation allows us to work with infinite

structures! 30

Page 31: Simply Typed Lambda Calculus, Progress and Preservation

Static Typing and Type Inference

n Unlike Scheme, which is dynamically typed, Haskell is statically typed!

n Unlike Java/C++ we don’t always have to write type annotations. Haskell infers types!n A lot more on type inference later!

> f x = head x // f returns the head of list x> f True // returns?• Couldn't match expected type ‘[a]’ with actual type ‘Bool’• In the first argument of ‘f’, namely ‘True’

In the expression: f True … 31

Page 32: Simply Typed Lambda Calculus, Progress and Preservation

Algebraic Data Types

n Algebraic data types are tagged unions (aka sums) of products (aka records)

data Shape = Line Point Point| Triangle Point Point Point| Quad Point Point Point Point

Program Analysis CSCI 4450/6450, A Milanova (from MIT 2015 Program Analysis OCW) 32

union

Haskell keyword

the new type

new constructors (a.k.a. tags, disjuncts, summands)Line is a binary constructor, Triangle is a ternary …

Page 33: Simply Typed Lambda Calculus, Progress and Preservation

Algebraic Data Types in HW5

n Constructors create new valuesn Defining a lambda expression

type Name = String data Expr = Var Name

| Lambda Name Expr| App Expr Expr

> e1 = Var “x” // Lambda term x> e2 = Lambda “x” e1 // Lambda term lx.x 33

Page 34: Simply Typed Lambda Calculus, Progress and Preservation

Examples of Algebraic Data Types

data Bool = True | Falsedata Day = Mon | Tue | Wed | Thu | Fri | Sat | Sun

data List a = Nil | Cons a (List a)data Tree a = Leaf a | Node (Tree a) (Tree a)

data Maybe a = Nothing | Just aMaybe type denotes that result of computation can be a or Nothing. Maybe is a monad.Program Analysis CSCI 4450/6450, A Milanova (from MIT 2015 Program Analysis OCW) 34

Polymorphic types.a is a type parameter!

Page 35: Simply Typed Lambda Calculus, Progress and Preservation

Data Constructors vs Type Constructors

n Data constructor constructs a “program object”n E.g., Var, Lambda, and App are data constructs

n Type constructor constructs a “type object”n E.g., True is a nullary type constructorn Maybe is a unary type constructor

Program Analysis CSCI 4450/6450, A Milanova 35

Page 36: Simply Typed Lambda Calculus, Progress and Preservation

Pattern Matching

n Examine values of an algebraic data typeanchorPnt :: Shape à PntanchorPnt s = case s of

Line p1 p2 à p1Triangle p3 p4 p5 à p3Quad p6 p7 p8 p9 à p6

n Two pointsn Test: does the given value match this pattern?n Binding: if value matches, bind corresponding

values of s and patternProgram Analysis CSCI 4450/6450, A Milanova (from MIT 2015 Program Analysis OCW) 36

Type signature of anchorPnt: takesa Shape and returns a Pnt.

Page 37: Simply Typed Lambda Calculus, Progress and Preservation

Pattern Matching in HW5

isFree::Name à Expr à BoolisFree v e =

case e ofVar n à if (n == v) then True else FalseLambda …

Program Analysis CSCI 4450/6450, A Milanova 37

Type signature of isFree. In Haskell, all signaturesare curried, i.e., they take just one argument.isFree takes a variable name, and returns a function that takes an expression and returns a boolean.

Of course, we can interpret isFree as a function that takes a variable name name and an expressione, and returns true if variable name is free in e.

Page 38: Simply Typed Lambda Calculus, Progress and Preservation

Monads

n A way to cleanly compose computationsn E.g., f may return a value of type a or NothingComposing computations becomes tedious:case (f s) of

Nothing à Nothing Just m à case (f m) …

n In Haskell, monads model IO and other imperative features

Program Analysis CSCI 4450/6450, A Milanova 38

Page 39: Simply Typed Lambda Calculus, Progress and Preservation

An Example: Cloned Sheep

type Sheep = …father :: Sheep à Maybe Sheepfather = ...mother :: Sheep à Maybe Sheepmother = …(Note: a sheep has both parents; a cloned sheep has one)maternalGrandfather :: Sheep à Maybe SheepmaternalGrandfather s = case (mother s) of

Nothing à NothingJust mà father m

Program Analysis CSCI 4450/6450, A Milanova (Example from All About Monads Tutorial) 39

Page 40: Simply Typed Lambda Calculus, Progress and Preservation

An Example

mothersPaternalGrandfather :: Sheep à Maybe SheepmothersPaternalGrandfather s = case (mother s) of

Nothing à NothingJust mà case (father m) of

Nothing à NothingJust gfà father gf

n Tedious, unreadable, difficult to maintainn Monads help!

40Program Analysis CSCI 4450/6450, A Milanova (Example from All About Monads Tutorial)

Page 41: Simply Typed Lambda Calculus, Progress and Preservation

The Monad Class

n Haskell’s Monad type class requires 2 operations, >>= (bind) and return

class Monad m where// >>= (the bind operation) takes a monad// m a, and a function that takes a and turns // it into a monad m b, and returns m b(>>=) :: m a à (a à m b) à m b// return encapsulates a value into the monadreturn :: a à m a

41

Page 42: Simply Typed Lambda Calculus, Progress and Preservation

The Maybe Monad

instance Monad Maybe where Nothing >>= f = Nothing(Just x) >>= f = f xreturn = Just

n Back to our example:mothersPaternalGrandfather s =

(return s) >>= mother >>= father >>= father(Note: if at any point, some function returns Nothing, it gets cleanly propagated.) 42

Page 43: Simply Typed Lambda Calculus, Progress and Preservation

The List Monad

n The List type constructor is a monadli >>= f = concat (map f li)return x = [x]Note: concat::[[a]] à [a]e.g., concat [[1,2],[3,4],[5,6]] yields [1,2,3,4,5,6]n Use any f s.t. f::aà[b]. f may return a list of

0,1,2,… elements of type b, e.g.,> f x = [x+1]> [1,2,3] >>= f // returns [2,3,4]

43

Page 44: Simply Typed Lambda Calculus, Progress and Preservation

The List Monad

parents :: Sheep à [Sheep]parents s = MaybeToList (mother s) ++

MaybeToList (father s)

grandParents :: Sheep à [Sheep]grandParents s = (parents s) >>= parents

Program Analysis CSCI 4450/6450, A Milanova 44

Page 45: Simply Typed Lambda Calculus, Progress and Preservation

Haskell Resources

n http://www.seas.upenn.edu/~cis194/spring13/

n https://www.haskell.org/

Program Analysis CSCI 4450/6450, A Milanova 45