To prove Theorem 4 we shall need an additional lemma formalizing type preservation into evaluation contexts E.
Lemma 14 (Type preservation into E). If Γ ` E[e] : T then ∃T0such thatΓ ` e : T0, and∀e0| Γ ` e0: T0 ⇒ Γ ` E[e0] : T .
Proof. We prove this lemma by on the definition of evaluation contexts given in Sec- tion 6.3.
• E = []. This case is trivial: [e] = e, thus Γ ` e : T implies trivially T0 = T . The second conclusion follows immediately.
• E = balance(E0), and E[e] = balance(e). In this case Γ ` balance(e) : T can be derived by hypothesis. By Case 11 of Lemma 2, T = uint and Γ ` e : address, which is part of what we aimed to prove (setting T0 = address). Let e0 be any expression such that Γ ` e0 : address, and consider rule BAL in Section 6.3: Γ ` e0 : address satisfies its premise, and thus we can derive Γ ` balance(e0) : uint, which is the same as Γ ` balance[e0] : uint.
• E = address(E0), and E[e] = address(e). In this case Γ ` address(e) : T can be derived by hypothesis. By Case 12 of Lemma 2, T = address and Γ ` e : C, which is part of what we aimed to prove (setting T0 = C). Let e0be any expression such that Γ ` e0 : C, and consider rule ADDRin Section 6.3: Γ ` e0 : C satisfies its premise, and thus we can derive Γ ` address(e0) : address, which is the same as Γ ` address[e0] : address.
• E = E0.s, and E[e] = e.s. In this case Γ ` e.s : T can be derived by hypothesis. By Case 14 of Lemma 2, Γ ` e : C, which is part of what we aimed to prove (setting T0 = C). Let e0 be any expression such that Γ ` e0 : C, and consider rule STATESEL in Section 6.3: Γ ` e0 : C, together with the conclusions of Case 14 of Lemma 2 satisfy its premises, and thus we can derive Γ ` e0.s : Ti, which is the same as Γ ` E[e0] : uint.
• E = E0.transfer(e1), and E[e] = e.transfer(e1). In this case Γ ` e.transfer(e1) : T can be derived by hypothesis. By Case 15 of Lemma 2, T = unit, Γ ` e : address, and Γ ` e1 : uint. Setting T0 = address, Γ ` e : address proves the first con- clusion of this lemma. Let e0be any expression such that Γ ` e0 : address, and consider rule TRANSFERin Section 6.3: Γ ` e0 : address and Γ ` e1 : uint
satisfy its premises, and thus we can derive Γ ` e0.transfer(e1) : unit, which is what we aimed to prove since (E.transfer(e1))[e0] = e0.transfer(e1).
• E = a.transfer(E0), and E[e] = a.transfer(e). In this case Γ ` a.transfer(e) : T can be derived by hypothesis. By Case 15 of Lemma 2, T = unit, Γ ` a : address, and Γ ` e : uint. Setting T0 = uint, Γ ` e : uint proves the first conclusion of this lemma. Let e0 be any expression such that Γ ` e0 : uint, and consider rule TRANSFERin Section 6.3: Γ ` a : address and Γ ` e0 : uint satisfy its premises, and thus we can derive Γ ` a.transfer(e0) : unit, which is the same as Γ ` E[e0] : unit.
• E = new C.value(E0)(˜e), and E[e] = new C.value(e)(˜e). In this case Γ ` new C.value(e)(˜e) : T can be derived by hypothesis. By Case 16 of Lemma 2, T = C, Γ ` e : uint, and Γ ` ˜e : ˜T , where sv(C) = ˜T s. Setting T0= uint, Γ ` e : uint proves the first conclusion of this lemma. Let e0be any expression such that Γ ` e0 : uint, and consider rule NEWin Section 6.3: Γ ` e0 : uint, together with the conclusions of Case 16 of Lemma 2, satisfy its premises, and thus we can derive Γ ` new C.value(e0)(˜e) : C, which is the same as Γ ` E[e0] : C. • E = new C.value(n)(˜v, E0, ˜e), and E[e] = new C.value(a)(˜v, e, ˜e). In this case
Γ ` new C.value(a)(˜v, e, ˜e) : T can be derived by hypothesis. By Case 16 of Lemma 2, T = C, Γ ` n : uint, and Γ ` vj{0≤j<i}, ei, e{i<j≤m}j : ˜T , where sv(C) = ˜T s. Setting T0 = Ti, Γ ` ei : Ti proves the first conclusion of this lemma. Let e0be any expression such that Γ ` e0: Ti, and consider rule NEWin Section 6.3: Γ ` a : uint, together with Γ ` vj{0≤j<i}, e0, e{i<j≤m}j : ˜T , satisfy its premises, and thus we can derive Γ ` new C.value(a)(˜v, e, ˜e) : C, which is the same as Γ ` E[e0] : C.
• E = C(E0), and E[e] = C(e). In this case Γ ` C(e) : T can be derived by hypothesis. By Case 17 of Lemma 2, T = C and Γ ` e : address, which is part of what we aimed to prove (setting T0= address). Let e0be any expression such that Γ ` e0 : address, and consider rule CONTRRETRin Section 6.3: Γ ` e0 : address satisfies its premise, and thus we can derive Γ ` C(e0) : C, which is the same as Γ ` C[e0] : C, which is indeed the same as Γ ` E[e0] : C.
• E = E0; e1, and E[e] = e; e1. In this case Γ ` e; e1 : T can be derived by hypothesis. By Case 18 of Lemma 2, ∃T1such that Γ ` e : T1and Γ ` e1 : T . Setting T0 = T1, Γ ` e : T1proves the first conclusion of this lemma. Let e0be any expression such that Γ ` e0 : T1, and consider rule SEQin Section 6.3: Γ ` e0 : T1and Γ ` e1: T satisfy its premises, and thus we can derive Γ ` e0; e : T , which is what we aimed to prove since (E; e1)[e0] = e0; e1.
• E = E.f.value(e1)(˜e), and E[e] = e.f.value(e1)(˜e). In this case Γ ` e.f.value(e1)(˜e) : T can be derived by hypothesis. By Case 24 of Lemma 2, ∃ ˜T1, T2 such that T = ˜T1 → T2, ftype(C, f ) = ˜T1 → T2, Γ ` e : C, Γ ` e1: uint, and Γ ` ˜e : ˜T . Setting T0= C, Γ ` e : C proves the first conclu- sion of this lemma. Let e0 be any expression such that Γ ` e0 : C, and consider rule CALLin Section 6.3: Γ ` e0: C, together with the conclusions of Case 24 of Lemma 2, satisfy its premises, and thus we can derive Γ ` e.f.value(e1)(˜e) : T2, which is the same as Γ ` E[e0] : T2.
• E = c.f.value(E)(˜e), and E[e] = c.f.value(e)(˜e). In this case Γ ` c.f.value(e)(˜e) : T can be derived by hypothesis. By Case 24 of Lemma 2, ∃ ˜T1, T2 such that T = ˜T1 → T2, ftype(C, f ) = ˜T1 → T2, Γ ` c : C, Γ ` e : uint, and Γ ` ˜e : ˜T . Setting T0 = uint, Γ ` e : uint proves the first conclusion of this lemma. Let e0be any expression such that Γ ` e0 : uint, and consider rule CALLin Section 6.3: Γ ` e0 : uint, together with the con- clusions of Case 24 of Lemma 2, satisfy its premises, and thus we can derive Γ ` c.f.value(e0)(˜e) : T2, which is the same as Γ ` E[e0] : T2.
• E = c.f.value(n)(˜v, E, ˜e), and E[e] = c.f.value(n)(˜v, e, ˜e). In this case Γ ` c.f.value(n)(˜v, e, ˜e) : T can be derived by hypothesis. By Case 24 of Lemma 2, ∃ ˜T1, T2such that T = ˜T1 → T2, ftype(C, f ) = ˜T1 → T2, Γ ` c : C, Γ ` n : uint, and Γ ` c.f.value(n)(v{0≤j<i}j , ei, e
{i<j≤m}
j ) : ˜T . Setting T 0 = Ti, Γ ` ei : Tiproves the first conclusion of this lemma. Let e0 be any expression such that Γ ` e0 : Ti, and consider rule CALLin Section 6.3: we know Γ ` e0 : Ti, and hence we can replace ei in vj{0≤j<i}, ei, e{i<j≤m}j with e0, in such a way that we obtain Γ ` vj{0≤j<i}, e0, e{i<j≤m}j : ˜T . This, together with the other conclusions of Case 24 of Lemma 2, satisfy the premises of CALL, and thus we can derive Γ ` c.f.value(n)(vj{0≤j<i}, e0, e{i<j≤m}j ) : T2, which is the same as Γ ` E[e0] : T2.
• E = E.value(e1)(˜e), and E[e] = e.value(e1)(˜e). In this case Γ ` e.value(e1)(˜e) : T can be derived by hypothesis. By Case 25 of Lemma 2, ∃ ˜T1, T2such that T = ˜T1→ T2, Γ ` e : ˜T1→ T2, Γ ` e1: uint, and Γ ` ˜e : ˜T . Setting T0 = ˜T1 → T2, Γ ` e : ˜T1 → T2 proves the first conclusion of this lemma. Let e0 be any expression such that Γ ` e0 : ˜T1 → T2, and consider rule CALLVALUE in Section 6.3: Γ ` e0 : ˜T1 → T2, together with the con- clusions of Case 25 of Lemma 2, satisfy its premises, and thus we can derive Γ ` e0.value(e1)(˜e) : T2, which is the same as Γ ` E[e0] : T2.
• The cases with E = E0.f.value(e1).sender(e2)(˜e),
E = c.f.value(E0).sender(e1)(˜e), and E = E.a.value(c).sender(n)(˜v, E0, ˜e) are the same as discussed above for call. The only difference relies on the ad- ditional hypothesis about the sender. For what concerns these three cases, the sender’s expression (respectively e2, e1and a) is just an hypothesis used to de- rive the second conclusion of this lemma. The remaining case, where the sender is actually the target expression, is discussed below.
• E = c.f.value(n).sender(E0)(˜e), and E[e] = c.f.value(n).sender(e)(˜e). In this case Γ ` c.f.value(n).sender(E)(˜e) : T can be derived by hypothesis. By Case 26 of Lemma 2, ∃ ˜T1, T2such that T = ˜T1→ T2, ftype(C, f ) = ˜T1→ T2, Γ ` c : C, Γ ` n : uint, Γ ` e : address, and Γ ` ˜e : ˜T . Setting T0 = address, Γ ` e : address proves the first conclusion of this lemma. Let e0 be any expression such that Γ ` e0 : address, and consider rule CALLTOPLEVEL in Section 6.3: Γ ` e0 : address satisfies its first premise. The second one refers back to CALL, whose premises are satisfied by the conclusions of Case 24 of Lemma 2. Hence, we can derive Γ ` c.f.value(n).sender(e)(˜e) : T2.
• E = (T1x = E0; e1), and E[e] = (T1 x = e; e1). In this case Γ ` T1x = e; e1: T can be derived by hypothesis. By Case 19 of Lemma 2, Γ ` e : T1and Γ, x : T1 ` e1 : T . Setting T0 = T1, Γ ` e : T1proves the first conclusion of
this lemma. Let e0 be any expression such that Γ ` e0 : T1, and consider rule DECLin Section 6.3: Γ ` e0: T1and Γ, x : T1` e1: T satisfy its premises, and thus we can derive Γ ` T1x = e0; e1 : T , which is what we wanted to prove, since (T1x = E; e1)[e0] = T1x = e0; e1
• E = (x = E0), and E[e] = (x = e). In this case Γ ` x = e : T can be derived by hypothesis. By Case 20 of Lemma 2, Γ ` x : T and Γ ` e : T . Setting T0 = T , Γ ` e : T proves the first conclusion of this lemma. Let e0 be any expression such that Γ ` e0 : T , and consider rule ASSin Section 6.3: its premises are satisfied by Γ ` x : T and Γ ` e0 : T ; we can then derive Γ ` x = e0 : T , which is what we aimed to prove since (x = E0)[e0] = (x = e0). • E = (E0.s = e1), and E[e] = (e.s = e1). In this case Γ ` e.s = e1 : T can be derived by hypothesis. By Case 21 of Lemma 2, Γ ` e : C, Γ ` e.s : T , and Γ ` e1: T . Setting T0= C, Γ ` e : C proves the first conclusion of this lemma. Let e0 be any expression such that Γ ` e0 : C, and consider rule STATEASS in Section 6.3. The second premise is satisfied by Γ ` e1 : T ; the first one is also satisfied, since Γ ` e0 : C makes it possible to derive Γ ` e0.s : T . Hence, we can derive Γ ` e0.s = e1: T , which is what we aimed to prove since (E.s = e1)[e0] = (e0.s = e1).
• E = (c.s = E0), and E[e] = (c.s = e). In this case Γ ` c.s = e : T can be derived by hypothesis. By Case 21 of Lemma 2, Γ ` c : C, Γ ` c.s : T , and Γ ` e : T . Setting T0 = T , Γ ` e : T proves the first conclusion of this lemma. Let e0be any expression such that Γ ` e0 : T , and consider rule STATEASSin Section 6.3. The second premise is satisfied by Γ ` e0 : T , whereas the first one is so by Γ ` c.s : T . Hence, we can derive Γ ` c.s = e0 : T , which is what we aimed to prove since (c.s = E)[e0] = (c.s = e0).
• E = E0[e1], and E[e] = e[e1]. In this case Γ ` e[e1] : T can be derived by hypothesis. By Case 22 of Lemma 2, ∃T1, T2 such that T = T2, Γ ` e : mapping(T1⇒ T2), and Γ ` e1: T1. Setting T0= mapping(T1⇒ T2), Γ ` e : mapping(T1⇒ T2) proves the first conclusion of this lemma. Let e0be any expression such that Γ ` e0 : mapping(T1⇒ T2), and consider rule MAPPSEL in Section 6.3. The first premise is satisfied by Γ ` e0 : mapping(T1 ⇒ T2), whereas the second one is so by Γ ` e1 : T1. Hence, we can derive Γ ` e0[e1] : T2, which is what we aimed to prove since (E[e1])[e0] = e0[e1].
• E = M [E0], and E[e] = M [e]. In this case Γ ` M [e] : T can be de- rived by hypothesis. By Case 22 of Lemma 2, ∃T1, T2 such that T = T2, Γ ` M : mapping(T1 ⇒ T2), and Γ ` e : T1. Setting T0 = T1, Γ ` e : T1 proves the first conclusion of this lemma. Let e0 be any expression such that Γ ` e0 : T1, and consider rule MAPPSEL in Section 6.3. The first premise is satisfied by Γ ` M : mapping(T1 ⇒ T2), whereas the second one is so by Γ ` e0 : T1. Hence, we can derive Γ ` M [e0] : T2, which is what we aimed to prove since (M [E])[e0] = M [e0].
• E = E0[e1 → e2], and E[e] = e[e1 → e2]. In this case Γ ` e[e1 → e2] : T can be derived by hypothesis. By Case 23 of Lemma 2, ∃T1, T2such that T = mapping(T1 ⇒ T2), Γ ` e : mapping(T1 ⇒ T2), Γ ` e1 : T1, and Γ ` e2 : T2. Setting T0 = mapping(T1 ⇒ T2), Γ ` e : mapping(T1 ⇒ T2) proves the first conclusion of this lemma. Let e0be any expression such that Γ `
e0 : mapping(T1 ⇒ T2), and consider rule MAPPASSin Section 6.3. The first premise is satisfied by Γ ` e0: mapping(T1⇒ T2), whereas the others are is so by Case 23 of Lemma 2. Hence, we can derive Γ ` e0[e1→ e2] : mapping(T1⇒ T2), which is what we aimed to prove since (E[e1→ e2])[e0] = e0[e1→ e2]. • E = M [e → e1], and E[e] = M [e → e1]. In this case Γ ` M [e → e1] : T
can be derived by hypothesis. By Case 23 of Lemma 2, ∃T1, T2such that T = mapping(T1⇒ T2), Γ ` M : mapping(T1⇒ T2), Γ ` e : T1, and Γ ` e1: T2. Setting T0= T1, Γ ` e : T1proves the first conclusion of this lemma. Let e0 be any expression such that Γ ` e0: T1, and consider rule MAPPASSin Section 6.3. The second premise is satisfied by Γ ` e0 : T1, whereas the others are is so by Case 23 of Lemma 2. Hence, we can derive Γ ` M [e0 → e1] : mapping(T1 ⇒ T2), which is what we aimed to prove since (M [E → e1])[e0] = M [e0→ e1]. • E = M [v → e], and E[e] = M [v → e]. In this case Γ ` M [v → e0] : T
can be derived by hypothesis. By Case 23 of Lemma 2, ∃T1, T2such that T = mapping(T1 ⇒ T2), Γ ` M : mapping(T1⇒ T2), Γ ` v : T1, and Γ ` e : T2. Setting T0= T2, Γ ` e : T2proves the first conclusion of this lemma. Let e0 be any expression such that Γ ` e0: T2, and consider rule MAPPASSin Section 6.3. The third premise is satisfied by Γ ` e0 : T2, whereas the others are is so by Case 23 of Lemma 2. Hence, we can derive Γ ` M [v → e0] : mapping(T1 ⇒ T2), which is what we aimed to prove since (M [v → E])[e0] = M [v → e0]. • E = if E0 then e2else e3, and E[e] = if e then e2 else e3. In this case Γ `
if e then e2else e3: T can be derived by hypothesis. By Case 27 of Lemma 2, Γ ` e : bool and Γ ` e2, e3 : T . Setting T0 = bool, Γ ` e : bool proves the first conclusion of this lemma. Let e0be any expression such that Γ ` e0 : bool, and consider rule IFin Section 6.3: Γ ` e0 : bool and Γ ` e2, e3 : T satisfy its premises, and thus we can derive Γ ` if e0then e2else e3: T , which is what we aimed to prove since (if E then e2else e3)[e0] = if e0then e2else e3
• E = return E0, and E[e] = return e. In this case Γ ` return e : T can be derived by hypothesis. By Case 13 of Lemma 2, Γ ` e : T can be derived, which is part of what we aimed to prove (setting T0= T ). Let e0be any expression such that Γ ` e0 : T , and consider rule RETURNin Section 6.3: Γ ` e0 : T satisfies its premise, and thus we can derive Γ ` e0 : T , which is what we aimed to prove since (return E)[e0] = return e0.
We can now prove Theorem 4: Theorem 4 (Subject Reduction).
IfΓ ` hβ, σ, ei : T with hβ, σ, ei −→ hβ0, σ0, e0i then ∃∆ such that Γ0 = Γ · ∆ and Γ0 ` hβ0, σ0, e0i : T .
Proof. We prove this theorem by induction on the height of the derivation of one re- duction step hβ, σ, ei −→ hβ, σ, e0i.
Base cases These cases correspond to the axioms in Section 5.5, where the height of the derivation corresponding to the computational step is 0.
• IF-TRUE. In this case hβ, σ, if true then e1else e2i −→ hβ, σ, e1i. By hypoth- esis, Γ ` hβ, σ, if true then e1else e2i : T . This implies, by rule CONFIGURA- TIONin Section 6.3, Γ ` if true then e1else e2: T , and by Case 27 of Lemma 2 we know Γ ` e1: T , which makes it derivable Γ0 ` hβ, σ, e1i : T , since β and σ are well-typed by hypothesis, setting ∆ = ∅.
• IF-FALSE. This case is dual to IF-TRUE.
• SEQ-C. In this case hβ, σ, v; e1i −→ hβ, β, e1i. By hypothesis, Γ ` hβ, σ, v; e1i : T . This implies, by rule CONFIGURATION in Section 6.3, Γ ` v; e1 : T , and by Case 18 of Lemma 2 we know Γ ` e1 : T , which makes it derivable Γ0` hβ, β, e1i : T , since β is well-typed by hypothesis, setting ∆ = ∅.
• SEQ-R. In this case hβ, σ, revert; e1i −→ hβ0, σ, reverti, where σ = β0. By hypothesis, Γ ` hβ, σ, revert; e1i : T . This implies, by rule CONFIGURATION in Section 6.3, Γ ` σ (and consequently Γ ` β0). Hence, bearing in mind that revert is well-typed regardless of the actual T (as stated by rule REVERTin Section 6.3), we have Γ ` revert : T , which implies Γ0 ` hβ0, σ, reverti : T , since σ = β0is well-typed by hypothesis, setting ∆ = ∅.
• SEQ. In this case hβ, σ, v; e1i −→ hβ, σ, e1i, where Top(σ) = a. By hypothesis, Γ ` hβ, σ, v; e1i : T . This implies, by rule CONFIGURATIONin Section 6.3, Γ ` v; e1: T , and by Case 18 of Lemma 2 we know Γ ` e1: T , which makes it derivable Γ0` hβ, σ, e1i : T , since β and σ are well-typed by hypothesis, setting ∆ = ∅.
• STATESEL. In this case hβ, σ, c.si −→ hβ, σ, vi, where β(c) = (C, s˜:v, n) and s ∈ ˜s. By hypothesis, Γ ` hβ, σ, c.si : T . This implies, by rule CONFIGU- RATIONin Section 6.3, Γ ` c.s : T . Furthermore, v has same type T ; that is Γ ` v : T . Hence, Γ0` hβ, σ, vi : T , since β and σ are well-typed by hypothesis and ∆ = ∅.
• DECL. In this case hβ, σ, T1 x = v; e1i −→ hβ · [x 7→ v], σ, v; e1i, where x /∈ dom(β). By hypothesis, Γ ` hβ, σ, T x = v; e1i : T2. This implies, by rule CONFIGURATION in Section 6.3, Γ ` T x = v; e1 : T2, and by Case 19 of Lemma 2 we know Γ ` e1 : T2 and Γ ` v : T1. Let ∆ = x : T1 and Γ0 = Γ · ∆, and consider Γ0 ` β · [x 7→ v]. Rule VARIABLErequires Γ0 ` β, which is derivable by applying Lemma 10 to the hypothesis Γ ` β, x /∈ dom(β), which is true by hypothesis of DECL, Γ0 ` x : T1, and Γ0` v : T1. The former is true by rule VAR in Section 6.3, since ∆ = x : T1. The latter is true by Lemma 12 applied to Γ ` v : T1. Hence, by rule VARIABLE, Γ0 ` β · [x 7→ v] is true. Furthermore, by Lemma 11, given Γ ` σ we know that also Γ0 ` σ can be derived. Hence, by rule CONFIGURATION, we can derive Γ0 ` hβ · [x 7→ v], σ, v; e1i : T2, which is what we wanted to prove.
• VAR. In this case hβ, σ, xi −→ hβ, σ, β(x)i. By hypothesis, Γ ` hβ, σ, xi : T . This implies, by rule CONFIGURATIONin Section 6.3, Γ ` x : T , and by Case 10 of Lemma 2 x : T ∈ Γ. By rule VARIABLE, each value v pointed to by a variable identifier x has the same type of x itself. This means that Γ ` β(x) : T . Hence, Γ0 ` hβ, σ, β(x)i : T , since β and σ are well-typed by hypothesis and setting ∆ = ∅.
• BALANCE. In this case hβ, σ, balance(a)i −→ hβ, σ, ni, where β(a) = (C, s˜:v, n). By hypothesis, Γ ` hβ, σ, balance(a)i : T . This implies, by rule CONFIGURA- TIONin Section 6.3, Γ ` balance(a) : T , and by Case 11 of Lemma 2 we know T = uint and Γ ` a : address. From rule CONTRACT in Section 6.3 follows Γ ` n : uint, which makes it derivable Γ0 ` hβ, σ, ni : uint, since β and σ are well-typed by hypothesis, setting ∆ = ∅.
• ADDRESS. In this case hβ, σ, address(c)i −→ hβ, σ, ai, where ˆβ(c) = a. By hypothesis, Γ ` hβ, σ, address(c)i : T . This implies, by rule CONFIGURATION in Section 6.3, Γ ` address(c) : T , and by Case 12 of Lemma 2 we know T = address and Γ ` c : C. We defined ˆβ(c) = a if (c, a) ∈ dom(β). Γ ` β implies, by rule CONTRACT in Section 6.3, that every pair (c, a) ∈ dom(β) is well-formed, and hence, by Lemma 13, Γ ` a : address. From this follows Γ0 ` hβ, σ, ai : address, since β and σ are well-typed by hypothesis, setting ∆ = ∅.
• ASS. In this case hβ, σ, x = vi −→ hβ[x 7→ v], σ, vi, where x ∈ dom(β). By hypothesis, Γ ` hβ, σ, x = vi : T . This implies, by rule CONFIGURATION in Section 6.3, Γ ` x = v : T , and by Case 20 of Lemma 2, Γ ` x : T and Γ ` v : T . Provided that the type of x and v is T , β remains well-typed. In fact VARIABLE(Section 6.3) applies and makes it derivable Γ0 ` hβ[x 7→ v], σ, vi : T , setting ∆ = ∅.
• STATEASSIn this case hβ, σ, c.s = v0i −→ hβ[c.s 7→ v0], σ, v0i, where β(c) = (C, s˜:v, n) and s ∈ ˜s. By hypothesis, Γ ` hβ, σ, c.s = v0i : T . This implies, by rule CONFIGURATIONin Section 6.3, Γ ` c.s = v0 : T , and by Case 21 of Lemma 2 we know Γ ` c.s : T and Γ ` v0 : T . Provided that the type of c.s and v0is T , β remains well-typed. CONTRACT(Section 6.3) applies and makes it derivable Γ0 ` hβ, σ, c.s = v0i : T , setting ∆ = ∅.
• MAPPSEL. In this case hβ, σ, M [v1]i −→ hβ, σ, M (v1)i. By hypothesis, Γ ` hβ, σ, M [v1]i : T . This implies, by rule CONFIGURATION in Section 6.3, Γ ` M [v1] : T , and by Case 22 of Lemma 2, ∃T1, T2such that Γ ` M : mapping(T1⇒ T2), T = T2, and Γ ` v1 : T1. Since a mapping is a total function T1 → T2, M [v1] is always well-defined and has type T2, that is Γ0 ` hβ, σ, M (v1)i : T2, where β and σ are well-typed by hypothesis and ∆ = ∅.
• MAPPASS. In this case hβ, σ, M [v1 → v2]i −→ hβ, σ, M0i, where M0 = M \{(v1, M (v1))} ∪ {(v1, v2)}. By hypothesis, Γ ` hβ, σ, M [v1 → v2]i : T . This implies, by rule CONFIGURATION in Section 6.3, Γ ` M [v1 → v2] : T , and by Case 23 of Lemma 2, ∃T1, T2 such that Γ ` M : mapping(T1 ⇒ T2), T = mapping(T1⇒ T2), Γ ` v1: T1, and Γ ` v2: T2. Looking at M0, we note it is obtained from M substituting the pair (v1, M (v1)) with (v1, v2). The types of both v1and v2are correct with respect to mapping(T1⇒ T2), and so we can derive Γ0 ` hβ, σ, M0i : mapping(T1 ⇒ T2), since β and σ are well-typed by hypothesis and ∆ = ∅.
• NEW-1. In this case hβ, σ, new C.value(n)(˜v)i −→ huptbal(β, Top(σ), −n) · [(c, a) 7→ (C, s˜:v, n)], σ, ci, where (c, a) /∈ dom(β), sv(C) = ˜T s, and |˜v| = |˜s|. By hypothesis, Γ ` hβ, σ, new C.value(n)(˜v)i : T . This implies, by rule CONFIGURATIONin Section 6.3, Γ ` new C.value(n)(˜v) : T , and by Case 16 of Lemma 2 we know T = C, Γ ` n : uint, and Γ ` ˜v : ˜T . Let ∆ =
c : C, a : address and Γ0 = Γ · ∆, and consider Γ0 ` β · [(c, a) 7→ (C, s˜:v, n)]. Rule CONTRACTrequires Γ0 ` β, which is derivable by applying Lemma 10 to the hypothesis Γ ` β, (c, a) /∈ dom(β), which is true by hypothesis of NEW- 1, Γ0 ` c : C, Γ0 ` a : address, Γ0 ` ˜v : ˜T (where sv(C) = T s), and˜ Γ0 ` n : uint. Γ0 ` c : C and Γ0 ` a : address() are derivable by rules REF and ADDRESS, respectively. Γ0 ` ˜v : ˜T and Γ0 ` n : uint are, on the other hand, derivable by Lemma 10 applied to the hypotheses Γ ` n : uint and Γ ` ˜v : ˜T . Furthermore, uptbal only modifies β incrementing or decrementing the balance n, thus preserving its well-formedness. We can here suppose that such operation is successful, since the case where uptbal returns ⊥ is dealt with by NEW-R (see below). Hence, by rule CONTRACT in Section 6.3 it follows Γ0 ` uptbal(β, Top(σ), −n) · [(c, a) 7→ (C, s˜:v, n)]. By Lemma 11, given Γ ` σ also Γ0 ` σ is true. This makes it derivable, by rule CONFIGURATION, Γ0 ` huptbal(β, Top(σ), −n) · [(c, a) 7→ (C, s˜:v, n)], σ, ci : C, which is what we wanted to prove.
• NEW-2. This case is very similar to NEW-1, with the only difference that no balance updates are made on β.
• NEW-R. In this case hβ, σ, new C.value(n)(˜v)i −→ hβ, σ, reverti, with uptbal(β, Top(σ), −n) = ⊥. By rule REVERTin Section 6.3 Γ ` revert : T , which implies Γ0 ` hβ, σ, reverti : T , where β and σ are well-typed by hypoth- esis and ∆ = ∅.
• CALL. In this case hβ, σ, c.f.value(n)(˜v)i −→ hβ0, σ · a, e{this := c,
msg.sender := Top(σ), msg.value := n}i, where ˆβ(c) = a, βC(c) = C, fbody(C, f, ˜v) = (˜x, e), (xi, a)xi∈˜x∈ dom(β), and/
β0 = uptbal(uptbal(β, a, n), Top(σ), −n) · [x
i 7→ vixi∈˜x,vi∈˜v]. By hypoth- esis, Γ ` hβ, σ, c.f.value(n)(˜v)i : T . This implies, by rule CONFIGURA- TIONin Section 6.3, Γ ` c.f.value(n)(˜v) : T , and by Case 24 of Lemma 2 ∃ ˜T1, T2 such that T = ˜T1 → T2, ftype(C, f ) = ˜T1 → T2, Γ ` c : C, Γ ` n : uint, and Γ ` ˜v : ˜T1. Since ˆβ(c) = a is well-defined by hypothe- sis, ∃(c, a) ∈ dom(β) such that Γ ` a : address: from it follows Γ ` σ · a. Also note that Γ ` σ ⇒ Γ ` Top(σ) : address, where Top(σ) 6= ∅ (otherwise CALLTOPLEVELwould apply). From rule F OKIN C (Section 6.3) we know this : C, msg.sender : address, msg.value : uint, ˜x : ˜T1` e : T2. By Lemma 5, considering the judgments Γ ` c : C, Γ ` Top(σ) : address, and Γ ` n : uint (which we have already proven as true), follows Γ ` e{this := c, msg.sender := Top(σ), msg.value := n} : T2. The last thing we have to deal with is β0. First, as we already pointed out, uptbal only modifies the balances via arithmetical operations, thus preserving the well-formedness of β. Again, we can suppose that such operation is successful, since the case where uptbal returns ⊥ is dealt with by CALL-R (see below). Secondly, we append to β a list of fresh pairs of local variables, together with the address of the contract identifying the invoked function. From Γ ` ˜v : ˜T1follows that each value vihas the same type of the for- mal parameter xiit refers to, and we have already proven Γ ` Top(σ) : address. Hence, by rule VARIABLE in Section 6.3, Γ ` β0, and we can finally derive Γ0 ` hβ0, σ · a, e{this := c, msg.sender := Top(σ), msg.value := n}i : T2, where ∆ = ∅.
• CALLTOPLEVEL. In this case
a0, msg.value := n}; e0i, where ˆβ(c) = a, βC(c) = C, fbody(C, f, ˜v) = (˜x, e), (xi, a)xi∈˜x∈ dom(β), and/
β0 = uptbal(uptbal(β, a, n), Top(σ), −n) · [xi 7→ vi xi∈˜x,vi∈˜v]. This case is very similar to CALL, the only difference relying on the sender, which is here ex- plicitly set to a0(since Top(σ) = ∅). We shall prove only the correctness of such sender, referring to the previous case for the rest of the proof. By hypothesis, Γ ` hβ, σ, c.f.value(n).sender(a0)(˜v); e0i : T . This implies, by rule CONFIGU- RATIONin Section 6.3, Γ ` c.f.value(n).sender(a0)(˜v); e0 : T , and by Case 26 of Lemma 2, Γ ` a0 : address. Setting ∆ = ∅, we obtain Γ0 ` a0: address. The proof is now the same as before, using a0instead of Top(σ).
• CALL-R. This case is the same as NEW-R: hβ, σ, c.f.value(n)(˜v)i −→ hβ, σ, reverti, with
uptbal(β, Top(σ), −n) = ⊥. By rule REVERTin Section 6.3 Γ ` revert : T , which implies Γ0` hβ, σ, reverti : T , since β and σ are well-typed by hypothesis and ∆ = ∅.
• CALLTOPLEVEL-R. This case is the same as CALL-R.
• TRANSFER. This case is the same as CALL, the only difference relying on the absence of formal parameters. Furthermore, the return type is unit. Hence, β0 is well-formed for what we said before, and so are σ · Top(σ) and e{this := c, msg.sender := Top(σ), msg.value := n}. Also note that Top(σ) 6= ∅, and hence using it to enlarge σ and to form β0is safe.
• TRANSFER-R. This case is the same as CALL-R.
• RETURN. In this case hβ, σ · a, return vi −→ hβ, σ, vi. Γ ` σ · a ⇒ Γ ` σ by rule CALLSTACKin Section 6.3. By hypothesis, Γ ` hβ, σ, return vi : T ; this implies, by rule CONFIGURATIONin Section 6.3, Γ ` return v : T . Hence, by Case 13 of Lemma 2, Γ ` v : T and we can derive Γ0 ` hβ, σ, vi : T , where ∆ = ∅.
• RETURN-R. In this case hβ, σ · a, return reverti −→ hβ, σ, reverti. By rule CONFIGURATIONin Section 6.3, Γ ` revert : T is derivable, and by Case 5 of lemma 2, we obtain Γ ` revert : T . Furthermore β and σ are well-typed for what we previously said for RETURN, and thus we can derive Γ0` hβ, σ, reverti : T , where ∆ = ∅.
• CONTRRETR. In this case hβ, σ, C(a)i −→ hβ, σ, ci, where βC(a) = C and ˆ
β(a) = c. By hypothesis, Γ ` hβ, σ, C(a)i : T . This implies, by rule CONFIG- URATIONin Section 6.3, Γ ` C(a) : T , and by Case 17 of Lemma 2 we know T = C and Γ ` a : address. Since ˆβ(a) = c is well-defined by hypothesis, ∃(c, a) ∈ dom(β), and since Γ ` β we get Γ ` c : C. Hence, Γ0 ` hβ, σ, ci : C, considering that also σ is well-typed by hypothesis and setting ∆ = ∅.
• CONTRRETR-R. In this case hβ, σ, C(a)i −→ hβ, σ, reverti, where βC(a) = C0 and C0 6= C. By rule REVERTin Section 6.3 Γ ` revert : T , which implies Γ0 ` hβ, σ, reverti : T , where β and σ are well-typed by hypothesis, setting