Download presentation
Presentation is loading. Please wait.
1
Abstract machines & interpreters
2
(Term rewriting semantics) recap
3
(→*) (→) = (→β) ∪ (→α) ∪ (→η)
(→) = (→β) ∪ (→α) ∪ (→η) (→*) A non-deterministic pre-order (i.e., reflexive, transitive)
4
α α α α α e0 e1 e2 e3 e4 e5
5
α α α α α e0 e1 e2 e3 e4 e5 β η e6
6
α α α α α e0 e1 e2 e3 e4 e5 β η e6 β β β
7
α α α α α e0 e1 e2 e3 e4 e5 β β e10 e7 β β e11 e8 β e9
8
* * α α α α α e0 e1 e2 e3 e4 e5 β β e10 e7 β β e11 e8 β e9 e12
Confluence (i.e., Church-Rosser Theorem) α α α α α e0 e1 e2 e3 e4 e5 β β e10 e7 β β e11 e8 β * e9 * e12
9
{ { ((λ (x) E0) E1) →β E0[x ← E1] redex
10
(E0 E1)[x ← E] = (E0[x ← E] E1[x ← E])
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)
11
(... (λ (x) ((λ (a) (λ (x) a)) (λ (b) x))) ...) ... (λ (x) (λ (x) (λ (b) x)))
12
(λ (y) E0)[x ← E] = (λ (y) E0[x ← E])
(... (λ (x) ((λ (a) (λ (x) a)) (λ (b) x))) ...) (λ (y) E0)[x ← E] = (λ (y) E0[x ← E]) where y ≠ x and y ∉ FV(E)
13
Evaluation contexts ℰ ::= (ℰ e) | (v ℰ) | □ v ::= (λ (x) e)
e ::= (λ (x) e) | (e e) | x ℰ ::= (ℰ e) | (v ℰ) | □
14
(((λ (x) ((λ (y) y) x)) (λ (z) z)) (λ (w) w))
Context and redex { { r ℰ[(v v)] = (((λ (x) ((λ (y) y) x)) (λ (z) z)) (λ (w) w)) ℰ = (□ (λ (w) w)) r = ((λ (x) ((λ (y) y) x)) (λ (z) z))
15
Abstract Machines
16
(e0, env) ⇓ ((λ (x) e2), env’) (e1, env) ⇓ v1 (e2, env’[x ↦ v1]) ⇓ v2
((e0 e1), env) ⇓ v2 ((λ (x) e), env) ⇓ ((λ (x) e), env) (x, env) ⇓ env(x)
17
Big-step (Natural) Semantics
18
(e0 e1), env e’, env’
19
e0 e1 (e0 e1), env e’, env’
20
(define (interp e env) (match e [(? symbol? x) (hash-ref env x)] [`(λ (,x) ,e0) `(clo (λ (,x) ,e0) ,env)] [`(,e0 ,e1) (define v0 (interp e0 env)) (define v1 (interp e1 env)) (match v0 [`(clo (λ (,x) ,e2) ,env) (interp e2 (hash-set env x v1))])]))
21
(define (interp e env) (match e [(? symbol? x) (hash-ref env x)] [`(λ (,x) ,e0) `(clo (λ (,x) ,e0) ,env)] [`(,e0 ,e1) (define v0 (interp e0 env)) (define v1 (interp e1 env)) (match v0 [`(clo (λ (,x) ,e2) ,env) (interp e2 (hash-set env x v1))])]))
22
(e0, env) ⇓ ((λ (x) e2), env’) (e1, env) ⇓ v1 (e2, env’[x ↦ v1]) ⇓ v2
((e0 e1), env) ⇓ v2 ((λ (x) e), env) ⇓ ((λ (x) e), env) (x, env) ⇓ env(x)
23
(((λ (x) …) (λ (z) z)), ∅) ⇓ …
( , ∅) ⇓ … (((λ (x) ((λ (y) y) x)) (λ (z) z)) (λ (w) w))
24
((λ (x) …), ∅) ⇓ ((λ (x) …), ∅)
(((λ (x) …) (λ (z) z)), ∅) ⇓ … ( , ∅) ⇓ … (((λ (x) ((λ (y) y) x)) (λ (z) z)) (λ (w) w))
25
((λ (z) z), ∅) ⇓ ((λ (z) z), ∅)
… ⇓ ((λ (x) …), ∅) ((λ (z) z), ∅) ⇓ ((λ (z) z), ∅) (((λ (x) …) (λ (z) z)), ∅) ⇓ … ( , ∅) ⇓ … (((λ (x) ((λ (y) y) x)) (λ (z) z)) (λ (w) w))
26
(e0, env) ⇓ ((λ (x) e2), env’) (e1, env) ⇓ v1 (e2, env’[x ↦ v1]) ⇓ v2
((e0 e1), env) ⇓ v2 ((λ (x) e), env) ⇓ ((λ (x) e), env) (x, env) ⇓ env(x)
27
(((λ (y) y) x)), [x ↦ ((λ (z) z), ∅)]) ⇓ …
( , ∅) ⇓ … (((λ (x) ((λ (y) y) x)) (λ (z) z)) (λ (w) w))
28
… ⇓ ((λ (y) y), [x ↦ ((λ (z) z), ∅)])
(((λ (y) y) x)), [x ↦ ((λ (z) z), ∅)]) ⇓ … (((λ (x) …) (λ (z) z)), ∅) ⇓ … ( , ∅) ⇓ … (((λ (x) ((λ (y) y) x)) (λ (z) z)) (λ (w) w))
29
(x, [x ↦ ((λ (z) z), ∅)]) ⇓ ((λ (z) z), ∅)
… ⇓ ((λ (y) y), [x ↦ ((λ (z) z), ∅)]) … ⇓ ((λ (z) z), ∅) (((λ (y) y) x)), [x ↦ ((λ (z) z), ∅)]) ⇓ … (((λ (x) …) (λ (z) z)), ∅) ⇓ … ( , ∅) ⇓ … (((λ (x) ((λ (y) y) x)) (λ (z) z)) (λ (w) w))
30
(y, [y ↦ ((λ (z) z), ∅), x ↦ ((λ (z) z), ∅)]) ⇓ ((λ (z) z), ∅)
(x, [x ↦ ((λ (z) z), ∅)]) ⇓ ((λ (z) z), ∅) … ⇓ ((λ (y) y), [x ↦ ((λ (z) z), ∅)]) … ⇓ ((λ (z) z), ∅) (((λ (y) y) x)), [x ↦ ((λ (z) z), ∅)]) ⇓ … (((λ (x) …) (λ (z) z)), ∅) ⇓ … ( , ∅) ⇓ … (((λ (x) ((λ (y) y) x)) (λ (z) z)) (λ (w) w))
31
(((λ (x) …) (λ (z) z)), ∅) ⇓ ((λ (z) z), ∅)
((λ (w) w), ∅) ⇓ ((λ (w) w), ∅) (((λ (x) …) (λ (z) z)), ∅) ⇓ ((λ (z) z), ∅) ( , ∅) ⇓ … (((λ (x) ((λ (y) y) x)) (λ (z) z)) (λ (w) w))
32
(((λ (x) …) (λ (z) z)), ∅) ⇓ ((λ (z) z), ∅)
(z, [z ↦ ((λ (w) w), ∅)]) ⇓ ((λ (w) w), ∅) ((λ (w) w), ∅) ⇓ ((λ (w) w), ∅) (((λ (x) …) (λ (z) z)), ∅) ⇓ ((λ (z) z), ∅) ( , ∅) ⇓ … (((λ (x) ((λ (y) y) x)) (λ (z) z)) (λ (w) w))
33
Review Meta-circular interpreters (reuse all features from host)
Term-rewriting (non-deterministic but consistent) outermost and innermost beta-reductions => Lazy (CBN) and Eager (CBV) when stop at λ Using a grammar for evaluation contexts, we force CBV Substitution environments and big-step reductions Equivalent to a closure-creating interpreter
34
Try evaluating using context&redex and then big-step
Exercises Try evaluating using context&redex and then big-step (((λ (z) z) (λ (u) (u u))) (λ (x) x)) (((λ (f) (λ (g) ((f g) f))) (λ (z) z)) (λ (u) (u u)))
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.