Lectures onType Theory
ch:typestate: ch:typestate
appendix sectionsolutions

ch:typestate: ch:typestate

exercise 52.1.

The branch derivations have the common output : drain:(x:File[open])=PDΩf:File[open]drain(f)TSCall and eof{open,eof}XreturnTSReturnf:File[eof]close f;returnTSClose Rule TS-Read places these two derivations over the displayed body PD, deriving the declared input state and common empty output.

exercise 52.2.

After the first close the context is empty. The second command would require a decomposition containing f:File[q] for q{open,eof}. No such binding exists, so TS-Close cannot apply.

exercise 52.3.

Let η(f)=η(g)=, H()=open, and give both variables type File[open]. Closing f removes H(), while a noninjective static account could retain g:File[open]. The successor maps fail agreement at g; therefore the first conclusion of lemma 52.2 is false.

exercise 52.4.

Add Δ,f:File[suspended]PΔΔ,f:File[open]suspend f;PΔTSSuspend and the corresponding rule from suspended to open for resume. The dynamic rules update H(η(f)) to the target state. In each preservation case, agreement gives the source state, the heap update establishes the target state, and the typing premise supplies the continuation derivation.

exercise 52.5.

The only rule concluding a term headed by read is TS-Read. Its input context contains f:File[open]. Finite-map equality cannot identify this binding with f:File[eof], so inversion yields no premise and no derivation.

exercise 52.6.

Read returns File[open]File[eof]. Sum elimination checks the left branch under the first summand and the right branch under the second, requiring both to produce the same output context Δ. The scrutinee and each branch must consume its file component affinely; otherwise one branch can retain a stale copy. No term introduces a dual endpoint or a blocking communication, so session fidelity and deadlock freedom are absent.

Search the book

Type to search the local edition.