Semantics interp IR 0 2 compile / desugar = interp IR 1 2 - - PowerPoint PPT Presentation

semantics
SMART_READER_LITE
LIVE PREVIEW

Semantics interp IR 0 2 compile / desugar = interp IR 1 2 - - PowerPoint PPT Presentation

Semantics interp IR 0 2 compile / desugar = interp IR 1 2 Assignment 1 interp Scheme IR 2 church-encode church->nat interp ( (f) ( (x) -calculus (f (f x)))) Formal semantics Axiomatic Semantics Gives axioms for


slide-1
SLIDE 1

Semantics

slide-2
SLIDE 2

IR0

interp interp compile / desugar

2 IR1 2

=

slide-3
SLIDE 3

Assignment 1

slide-4
SLIDE 4

Scheme IR λ-calculus

interp interp church-encode

2 (λ (f) (λ (x) (f (f x))))

church->nat

slide-5
SLIDE 5

Formal semantics

slide-6
SLIDE 6

Axiomatic Semantics Denotational Semantics Operational Semantics

Gives axioms for constructing sound proofs about programs (typically using Hoare logic). Provides a function that maps language forms into their denotations in a known domain. Provides a step-by-step reduction of the program to a value in terms of program terms or an abstract machine.

slide-7
SLIDE 7

((λ (f) (f (f (λ (x) x)))) (λ (x) x)) ((λ (x) x) ((λ (x) x) (λ (x) x))) ((λ (x) x) (λ (x) x)) (λ (x) x) β β β

slide-8
SLIDE 8

→β

((λ (x) E0) E1) E0[x ← E1]

{

redex

slide-9
SLIDE 9

Capture-avoiding substitution

E0[x ← E1]

slide-10
SLIDE 10

FV(x) = {x} FV((λ (x) E0)) = FV(E0) \ {x} FV((E0 E1)) = FV(E0) ∪ FV(E1)

slide-11
SLIDE 11

x[x ← E] = E y[x ← E] = y where y ≠ x (E0 E1)[x ← E] = (E0[x ← E] E1[x ← E]) (λ (x) E0)[x ← E] = (λ (x) E0) (λ (y) E0)[x ← E] = (λ (y) E0[x ← E])

where y ≠ x and y ∉ FV(E)

β-reduction cannot occur when y ∈ FV(E)

slide-12
SLIDE 12

α - renaming

(λ (x) (λ (y) x)) (λ (a) (λ (b) a))

slide-13
SLIDE 13

→α

(λ (x) E0) (λ (y) E0[x ← y])

α - renaming

slide-14
SLIDE 14

(λ (x) (E0 x)) E0 where x ∉ FV(E0)

η - reduction

→η

slide-15
SLIDE 15

Reduction

(→) = (→β) ∪ (→α) ∪ (→η)

(→*)

reflexive/transitive closure

slide-16
SLIDE 16

Evaluation

E0

*

E1

*

?

slide-17
SLIDE 17

Evaluation to normal form

E0

*

(λ (x) …)

slide-18
SLIDE 18

Evaluation to normal form

E0

*

(λ (x) … (λ (z) ((a …) …)))

function position must be a variable

slide-19
SLIDE 19

Evaluation Strategy

E0

* *

E1 E2

slide-20
SLIDE 20

Evaluation Strategy

((λ (x) ((λ (y) y) x)) (λ (z) z))

→η ((λ (y) y) (λ (z) z)) →β (λ (z) z)

slide-21
SLIDE 21

Evaluation Strategy

((λ (x) ((λ (y) y) x)) (λ (z) z))

→β ((λ (y) y) (λ (z) z)) →β (λ (z) z)

slide-22
SLIDE 22

Evaluation Strategy

((λ (x) ((λ (y) y) x)) (λ (z) z))

→β ((λ (x) x) (λ (z) z)) →β (λ (z) z)

slide-23
SLIDE 23

Confluence

E0

* *

E1 E2

Church-Rosser Theorem

* *

E3

slide-24
SLIDE 24

Applicative evaluation order Normal evaluation order

Always evaluates the innermost leftmost redex first. Always evaluates the outermost leftmost redex first.

slide-25
SLIDE 25

Applicative evaluation order Normal evaluation order

((λ (x) ((λ (y) y) x)) (λ (z) z)) (((λ (x) ((λ (y) y) x)) (λ (z) z)) (λ (w) w))

slide-26
SLIDE 26

Call-by-name semantics Call-by-value semantics

Normal evaluation order, but not under lambdas. Applicative evaluation order, but not under lambdas.

slide-27
SLIDE 27

ℰ ::= (ℰ e)


| (v ℰ)
 | □

Evaluation contexts

v ::= (λ (x) e)
 e ::= (λ (x) e)
 | (e e) | x

slide-28
SLIDE 28

Context and redex

(((λ (x) ((λ (y) y) x)) (λ (z) z)) (λ (w) w))

ℰ = (□ (λ (w) w)) ℰ[(v v)] =

r = ((λ (x) ((λ (y) y) x)) (λ (z) z))

{ {

r

slide-29
SLIDE 29

Context and redex

(((λ (x) ((λ (y) y) x)) (λ (z) z)) (λ (w) w))

ℰ = (□ (λ (w) w)) ℰ[r] =

r = ((λ (x) ((λ (y) y) x)) (λ (z) z))

→β ((λ (y) y) (λ (z) z))

slide-30
SLIDE 30

Put it back together:

ℰ = (□ (λ (w) w))

r = ((λ (x) ((λ (y) y) x)) (λ (z) z))

→β ((λ (y) y) (λ (z) z))

(((λ (y) y) (λ (z) z)) (λ (w) w))

ℰ[r]

slide-31
SLIDE 31

Some exercises

1) (((λ (y) y) (λ (z) z)) (λ (w) w)) 2) ((λ (u) (u u)) (λ (x) (λ (x) x))) 3) (((λ (x) x) (λ (y) y)) ((λ (u) (u u)) (λ (z) (z z))))