Docsity

Problems on Understanding Programming Analysis - Assignment 5 | CMSC 631, Assignments of Computer Science

Toggle sidebar
bg1
CMSC 631 – Homework 5
Mark Daly <[email protected]>
April 27, 2009
Problem 4
Lemma 1 (Progress) If A` hs, ei:tand eis a closed term (no free variables, though it may contain
locations `) then either eis a value or there exist s0and e0such that hs, ei→hs0, e0i.
Proof : by induction on e.
•x: By assumption (eis a closed term, and so no free variables can exist).
•n:nis a value.
•λx :t.e :λx :t.e is a value.
•`:`is a value.
•e1e2
–If e1is not a value, then by the IH it evaluates to some e0
1, and we can take a step hs, e1e2i →
hs0, e0
1e2iby C-App-Left.
–If e2is not a value, then (similarly) by the IH it evaluates to some e0
2, and we can take a step
hs, e1e2i→hs0, e1e0
2iby C-App-Right.
–Otherwise, e1and e2are values. Assuming A`e1e2is well typed, we know by inspection of the
type rules (by T-App) that A`e1:t→t0and A`e2:t, and that (by T-Lam)λx :t.exis
the only value that can take type t→t0;e2can be any value v. As such, we can take a step to
e0=ex[x7→ v], s0=sby S-App.
•ref e
–If eis not a value, then by the IH it evaluates to some e0, and we can take a step hs,ref ei →
hs0,ref e0iby C-Ref.
–Otherwise, eis a value v. We can select a “fresh” `(i.e. ` /∈dom(s)) to represent the location of
vin state sand expand sto include this new `(with value v), creating s0. Given this `and s0,
we can take a step to e0=`,s0=s[`7→ v] by S-Ref.
•!e
–If eis not a value, then by the IH it evaluates to some e0, and we can take a step hs, !ei→hs0,!(e0)i
by C-Deref.
–Otherwise eis a value. Assuming A` hs, !eiis well typed, we know by inspection of the type
rules (T-Ref) that A`e:tref , and that (by T-Loc), `|`∈dom(A) is the only value that can
take type tref . By compatibility (again stemming from the fact that A` hs, !eiis well typed), we
know that dom(A) = dom(s), and so `∈dom(A)⇒`∈dom (s). Therefore, e=`,`∈dom(s),
and so we can take a step to e0=s(`), s0=sby S-Deref.
1
pf3
pf4
pf5

Partial preview of the text

Download Problems on Understanding Programming Analysis - Assignment 5 | CMSC 631 and more Assignments Computer Science in PDF only on Docsity!

CMSC 631 – Homework 5

Mark Daly

April 27, 2009

Problem 4

Lemma 1 (Progress) If A 〈s, e〉 : t and e is a closed term (no free variables, though it may contain locations) then either e is a value or there exist s′^ and e′^ such that 〈s, e〉 → 〈s′, e′〉.

Proof : by induction on e.

  • x : By assumption (e is a closed term, and so no free variables can exist).
  • n : n is a value.
  • λx : t.e : λx : t.e is a value.
  • : is a value.
  • e 1 e 2
    • If e 1 is not a value, then by the IH it evaluates to some e′ 1 , and we can take a step 〈s, e 1 e 2 〉 → 〈s′, e′ 1 e 2 〉 by C-App-Left.
    • If e 2 is not a value, then (similarly) by the IH it evaluates to some e′ 2 , and we can take a step 〈s, e 1 e 2 〉 → 〈s′, e 1 e′ 2 〉 by C-App-Right.
    • Otherwise, e 1 and e 2 are values. Assuming A e 1 e 2 is well typed, we know by inspection of the type rules (by T-App) that A e 1 : t → t′^ and A ` e 2 : t, and that (by T-Lam) λx : t.ex is the only value that can take type t → t′; e 2 can be any value v. As such, we can take a step to e′^ = ex[x 7 → v], s′^ = s by S-App.
  • ref e
    • If e is not a value, then by the IH it evaluates to some e′, and we can take a step 〈s, ref e〉 → 〈s′, ref e′〉 by C-Ref.
    • Otherwise, e is a value v. We can select a “fresh” (i.e. /∈ dom(s)) to represent the location of v in state s and expand s to include this new (with value v), creating s′. Given this and s′, we can take a step to e′^ = , s′^ = s[ 7 → v] by S-Ref.
  • !e
    • If e is not a value, then by the IH it evaluates to some e′, and we can take a step 〈s, !e〉 → 〈s′, !(e′)〉 by C-Deref.
    • Otherwise e is a value. Assuming A 〈s, !e〉 is well typed, we know by inspection of the type rules (T-Ref) that A e : t ref , and that (by T-Loc), | ∈ dom(A) is the only value that can take type t ref. By compatibility (again stemming from the fact that A 〈s, !e〉 is well typed), we know that dom(A) = dom(s), and so ∈ dom(A) ⇒ ∈ dom(s). Therefore, e =, ∈ dom(s), and so we can take a step to e′^ = s(), s′^ = s by S-Deref.

e ::= x | n | λx : t.e | | e e | ref e |!e | e := e t ::= int | t → t | t ref v ::= n | λx : t.e | A ::= ∅ | (A, x : t) | (A, ` : t)

Figure 1: Grammar definition for λ-calculus with references

S-App 〈s, (λx.e) v〉 → 〈s, e[x 7 → v]〉

C-App-Left 〈s, e 1 〉 → 〈s′, e′ 1 〉 〈s, e 1 e 2 〉 → 〈s′, e′ 1 e 2 〉

C-App-Right 〈s, e 2 〉 → 〈s′, e′ 2 〉 〈s, v e 2 〉 → 〈s′, v e′ 2 〉

S-Ref 6 ∈ dom(s) 〈s, ref v〉 → 〈s[ 7 → v], `〉

C-Ref 〈s, e〉 → 〈s′, e′〉 〈s, ref e〉 → 〈s′, ref e′〉

S-Deref ∈ dom(s) 〈s, !〉 → 〈s, s(`)〉

C-Deref 〈s, e〉 → 〈s′, e′〉 〈s, !e〉 → 〈s′, !(e′)〉

S-Assign ∈ dom(s) 〈s, := v〉 → 〈s[` 7 → v], v〉

C-Assign-Left 〈s, e 1 〉 → 〈s′, e′ 1 〉 〈s, e 1 := e 2 〉 → 〈s′, e′ 1 := e 2 〉

C-Assign-Right 〈s, e 2 〉 → 〈s′, e′ 2 〉 〈s, := e 2 〉 → 〈s′, := e′ 2 〉

Figure 2: (Small-step) Operational semantics with stores

T-Var x ∈ dom(A) A ` x : A(x)

T-Loc ∈ dom(A) A : A()

T-Int A ` n : int

T-Lam A, x : t e : t′ A λx : t.e : t → t′

T-App A e 1 : t → t′^ A e 2 : t A ` e 1 e 2 : t′

T-Ref A e : t A ref e : t ref

T-Deref A e : t ref A!e : t

T-Assign A e 1 : t ref A e 2 : t A ` e 1 := e 2 : t

Figure 3: Type rules with references

  • Otherwise, both e 1 and e 2 are values. We know by inspection of the type rules (by T-Assign) that A e 1 : t ref and A e 2 : t, and that (by T-Loc) | ∈ dom(A) is the only value that can take type t ref ; as such, e 1 must be , and e 2 can be any value v. As in the previous case, we know from configuration typing that A s() : t. From the type rules (T-Assign), A v : t; given that both v and s() have type t under state s and environment A, we know by lemma 6 that replacing in s with v will not require an addition to A, nor will it change the type of s(), and so will preserve configuration typing. Using S-Assign, we choose A′^ = A, s′^ = s[ 7 → v], e′^ = v, and so preservation holds for this case.

Lemma 3 (Substitution) If A v : t and A, x : t e : t′, then A ` e[x 7 → v] : t′.

Proof sketch: by straightforward induction on the structure of e. Substitute any v for any x in A ` 〈s, e〉 where A(x) = A(v), and show that e is still well typed.

Lemma 4 (State expansion) Given an /∈ dom(s), and some v, if s′^ = s[ 7 → v], then s′^ |doms= s.

Proof : by induction over the derivation of states. By the IH, assume a well formed s; select /∈ dom(s) and derive s′^ by adding 7 → v to s (i.e. s′^ = s[7 → v]). Because /∈ dom(s), ∀′^ ∈ dom(s).s(′) = s′(′) – that is, the newly added cannot change the values of any ′^ ∈ dom(s), since ∀′^ ∈ dom(s)./∈ dom(s) ⇒ 6 = `′. Therefore, s′^ |doms= s.

Lemma 5 (Safety of environment expansion) Given A ∼ s, some /∈ dom(s), and some A v : t, if A′^ = A, : t ref and s′^ = s[ 7 → v], then A′^ |domA= A and A′^ ∼ s′.

Proof : by induction over the derivation of compatible type environments. To prove this lemma, we must show that both of the conditions of compatibility hold for A′^ and s′.

  • dom(A′) = dom(s′). By the induction hypothesis, assume A and s were constructed as outlined in this lemma’s definition and that A ∼ s. Let A′^ = A, : t ref (which is allowed by our language definition), and let s′^ = s[ 7 → v], where ∈/ dom(s). By A ∼ s, dom(A) = dom(s); therefore, /∈ dom(s) ∧ dom(s) = dom(A) ⇒ /∈ dom(A). Since is being added both to dom(A) to produce A′^ and to dom(s) to produce s′, we have dom(A′) = dom(A) ∪ , dom(s′) = dom(s) ∪, and so dom(A′) = dom(s′).
  • ∀∈ dom(s).∃t.A() = t ref ∧ A s() : t Derive A′^ and s′^ from A and s where A ∼ s using the IH as above. By lemma 4, we know that the newly added cannot change the values of any′^ ∈ dom(s). Since /∈ dom(s) and dom(s) = dom(A), also cannot conflict any of the values in A. By this and the derivation of A′, we find that ∀′dom(s′).∃t.A′(′) = t ref ∧ A′^ s′(′) : t.

Therefore, compatibility holds for A′^ and s′. From this, we know that, because values added to A to derive A′^ are not in dom(A), new values cannot conflict with′^ ∈ dom(A). Therefore, ∀′^ ∈ dom(A).A(′) = A′(`′), and thus A′^ |domA= A.

Lemma 6 (Safety of state substitution) Given A ∼ s, some ∈ dom(s) where A : t ref and A s() : t, and some A v : t, if s′^ = s[` 7 → v], then compatibility holds.

Proof : by inspection.

  • dom(A) = dom(s). Because A ∼ s, dom(A) = dom(s). Since ∈ dom(s), s[ 7 → v] does not change the domain of s, and so dom(A) = dom(s′).
  • ∀∈ dom(s).∃t.A() = t ref ∧ A s() : t Knowing that there already exists in A a term : t ref , and there alreay exists in s a term such that A s() : t, given A v : t, let s′^ = s[ 7 → v]. We must now show that A(`) = t ref

and A s′() : t. However, this is immediate from the type of v, as A has not changed, and, because s′() = v, A s′() : t. In other words, because we only admit updates to in s that have types t that are equal to that of s() under environment A, A s[7 → v]() : t. Therefore, ∀dom(s′).∃t.A() = t ref ∧ A s′() : t.

Therefore, compatibility holds for A and s′.