SLIDE 6 LR Nondeterminism Syntax NO-Reduction Contextual Equivalence Diagrams
Labeled Reduction Rules
(lbeta) C[((λx.e1)sub e2)] → C[(letrec x = e2 in e1)] (cp-in) (letrec x1 = (λx.e)sub, {xi = xi−1}m
i=2, Env in C[xvis m ])
→ (letrec x1 = (λx.e), {xi = xi−1}m
i=2, Env in C[(λx.e)])
(cp-e) (letrec x1 = (λx.e1)sub, {xi = xi−1}m
i=2, Env, y = C[xvis m ] in e2)
→ (letrec x1 = (λx.e1), {xi = xi−1}m
i=2, Env, y = C[(λx.e1)] in e2)
(llet-in) (letrec Env1 in (letrec Env2 in e)sub) → (letrec Env1, Env2 in e) (llet-e) (letrec Env1, x = (letrec Env2 in ex)sub in e) → (letrec Env1, Env2, x = ex in e) (lapp) C[((letrec Env in e1)sub e2)] → C[(letrec Env in (e1 e2))] (lcase) C[(caseT (letrec Env in e)sub of alts)] → C[(letrec Env in (caseT e of alts))] (lseq) C[(seq (letrec Env in e1)sub e2)] → C[(letrec Env in (seq e1 e2))] (seq-c) C[(seq vsub e)] → C[e] if v is a value (seq-in) (letrec x1 = (c − → e )sub, {xi = xi−1}m
i=2, Env in C[(seq xvis m e′)])
→ (letrec x1 = (c − → e ), {xi = xi−1}m
i=2, Env in C[e′])
(seq-e) (letrec x1 = (c − → e )sub, {xi = xi−1}m
i=2, Env, y = C[(seq xvis m e′)] in e′′)
→ (letrec x1 = (c − → e ), {xi = xi−1}m
i=2, Env, y = C[e′] in e′′)
(case-c) C[caseT (cT,i − → e )sub of {. . . ; (cT,i − → y ) → e′; . . .}] → C[(letrec {yi = ei}n
i=1 in e′)]
if n = ar(cT,i) ≥ 1 (case-c) C[caseT csub
T,iof{. . . ; cT,i → e; . . .}] → C[e]
if ar(cT,i) = 0 (case-in) letrec x1 = (cT,i − → e )sub, {xi = xi−1}m
i=2, Env in C[caseT xvis m of{. . . ; (cT,i −
→ z ) → e′; . . .}] → letrec x1 = (cT,i − → y ), {yi = ei}n
i=1, {xi = xi−1}m i=2, Env in C[(letrec {zi = yi}n i=1 in e′)]
where n = ar(cT,i) ≥ 1 and yi are fresh variables (case-in) letrec x1 = csub
T,i, {xi = xi−1}m i=2, Env in C[caseT xvis m of{. . . ; (ci → e); . . .}]
→ letrec x1 = cT,i, {xi = xi−1}m
i=2, Env in C[e], if ar(cT,i) = 0
(case-e) letrec x1 = (cT,i − → e )sub, {xi = xi−1}m
i=2, u = C[caseT xvis m of{. . . ; (cT,i −
→ z ) → e′; . . . }], Env in e′′ → letrec x1 = (cT,i − → y ), {yi = ei}n
i=1, {xi = xi−1}m i=2, u = C[(letrec {zi = yi}n i=1 in e′)], Env in e′′
where n = ar(cT,i) ≥ 1 and yi are fresh variables (case-e) letrec x1 = csub
T,i, {xi = xi−1}m i=2, u = C[caseT xvis m of {. . . ; (cT,i → e′); . . .}], Env in e′′
→ letrec x1 = cT,i, {xi = xi−1}m
i=2, . . . , u = C[e′], Env in e′′
if ar(cT,i) = 0
- D. Sabel & M. Schmidt-Schauß · ISR 2015 · Rewriting Techniques for Correctness of Program Transformations
21/57 LR Nondeterminism Syntax NO-Reduction Contextual Equivalence Diagrams
Example
Labeling of (letrec w = λx.x, y = w, z = (y y) in z) yields (letrec w = (λx.x)sub, y = wnontarg, z = (yvis y)vis in zvis)vis The matching reduction rule is
(cp-e) (letrec x1 = (λx.e1)sub, {xi = xi−1}m
i=2, Env, y = C[xvis m ] in e2)
→ (letrec x1 = (λx.e1), {xi = xi−1}m
i=2, Env, y = C[(λx.e1)] in e2)
thus the normal order reduction is: (letrec w = λx.x, y = w, z = (y y) in z)
no
− → (letrec w = λx.x, y = w, z = ((λx′.x′) y) in z)
- D. Sabel & M. Schmidt-Schauß · ISR 2015 · Rewriting Techniques for Correctness of Program Transformations
22/57 LR Nondeterminism Syntax NO-Reduction Contextual Equivalence Diagrams
Another Example
(letrec x = (Cons True Nil), y = ((λw.w) x) in (caseList y of {Nil → Nil; (Cons z zs → z)})) (letrec x = (Cons True Nil), y = ((λw.w) x) in (caseList y of {Nil → Nil; (Cons z zs → z)}))top (letrec x = (Cons True Nil), y = ((λw.w) x) in (caseList y of {Nil → Nil; (Cons z zs → z)}
no,lbeta
− − − − − → (letrec x = (Cons True Nil), y = (letrec w = x in w) in (caseList y of {Nil → Nil; (Cons z zs → z)})) (letrec x = (Cons True Nil), y = (letrec w = x in w) in (caseList y of {Nil → Nil; (Cons z zs → z)}))top (letrec x = (Cons True Nil), y = (letrec w in (caseList y of {Nil → Nil; (Cons z zs →
no,llet−e
− − − − − → (letrec x = (Cons True Nil), y = w, w = x in (caseList y of {Nil → Nil; (Cons z zs → z)})) (letrec x = (Cons True Nil), y = w, w = x in (caseList y of {Nil → Nil; (Cons z zs → z)}))top (letrec x = (Cons True Nil), y = w, w = x in (caseList ysub of {Nil → Nil; (Cons z zs →
no,case−in
− − − − − − − → (letrec x = (Cons u us), u = True, us = Nil, y = w, w = x in (letrec z = u, zs = us in z)) (letrec x = (Cons u us), u = True, us = Nil, y = w, w = x in (letrec z = u, zs = us in z))top (letrec x = (Cons u us), u = True, us in (letrec z = u, zs = us in z)sub)vis
no,llet−in
− − − − − − → (letrec x = (Cons u us), u = True, us = Nil, y = w, w = x, z = u, zs = us in z)
- D. Sabel & M. Schmidt-Schauß · ISR 2015 · Rewriting Techniques for Correctness of Program Transformations
23/57 LR Nondeterminism Syntax NO-Reduction Contextual Equivalence Diagrams
Weak Head Normal Forms and Convergence
Weak head normal form (WHNF) a value v,
- r an expression (letrec Env in v), where v is a value,
- r an expression
(letrec x1 = (c − → e ), {xi = xi−1}m
i=2, Env in xm).
Convergence: e⇓ ⇐ ⇒ ∃ WHNF e′ : e
no,∗
− − → e′ Divergence: e⇑ ⇐ ⇒ ¬(e⇓)
- D. Sabel & M. Schmidt-Schauß · ISR 2015 · Rewriting Techniques for Correctness of Program Transformations
24/57