Exercise 104.1.
Let 𝑃 be the successful postcondition on (𝗎𝗇𝗂𝗍,𝑠1) and 𝑄 the exceptional postcondition on (𝗇𝖾𝗀𝖺𝗍𝗂𝗏𝖾,𝑠1). The program reads 𝑠0; if 𝑠0 <0 it raises, and otherwise it writes 𝑠0 +1. Expanding get, bind, raise, and put gives 𝑤(𝑃)(𝑄)(𝑠0)={𝑄(𝗇𝖾𝗀𝖺𝗍𝗂𝗏𝖾,𝑠0),𝑠0<0,𝑃(𝗎𝗇𝗂𝗍,𝑠0+1),𝑠0≥0. Thus the negative branch requires exactly 𝑄(𝗇𝖾𝗀𝖺𝗍𝗂𝗏𝖾,𝑠0), retaining the failing state, while the nonnegative branch requires exactly 𝑃(𝗎𝗇𝗂𝗍,𝑠0 +1).
Exercise 104.2.
Let 𝗂𝗇𝖼𝗋𝟤 sequence 𝗂𝗇𝖼𝗋 twice. Two applications of WP-Bind, followed by the get and put clauses, calculate 𝑤2(𝑝)(𝑠0)𝑊𝑃−𝐵𝑖𝑛𝑑=𝑤𝑖(𝜆(𝗎𝗇𝗂𝗍,𝑠1).𝑤𝑖(𝑝)(𝑠1))(𝑠0)first increment=𝑤𝑖(𝑝)(𝑠0+1)second increment=𝑝(𝗎𝗇𝗂𝗍,𝑠0+2). Advertise 𝑤𝑎(𝑝)(𝑠0)=∀𝑠2.𝑠2=𝑠0+2⇒𝑝(𝗎𝗇𝗂𝗍,𝑠2). Rule WP-Sub asks for ∀𝑝,𝑠0. 𝑤𝑎(𝑝)(𝑠0) ⇒𝑤2(𝑝)(𝑠0). Given its antecedent, instantiate 𝑠2 with 𝑠0 +2, use reflexivity of equality, and obtain 𝑝(𝗎𝗇𝗂𝗍,𝑠0 +2), which is the calculated consequent.
Exercise 104.3.
Fix a result type 𝐴, a state 𝑠0 :𝑆, and a postcondition 𝑝 :𝐴 ×𝑆 →𝖴. For a state transformer 𝑤, right identity is 𝖻𝗂𝗇𝖽𝗐𝗉𝖲𝗍(𝑤,𝜆𝑎.𝗋𝖾𝗍𝗎𝗋𝗇𝗐𝗉𝖲𝗍𝑎)(𝑝)(𝑠0)𝑊𝑃−𝐵𝑖𝑛𝑑=𝑤(𝜆(𝑎,𝑠1).𝗋𝖾𝗍𝗎𝗋𝗇𝗐𝗉𝖲𝗍𝑎(𝑝)(𝑠1))(𝑠0)𝑊𝑃−𝑅𝑒𝑡𝑢𝑟𝑛=𝑤(𝜆(𝑎,𝑠1).𝑝(𝑎,𝑠1))(𝑠0)=𝑤(𝑝)(𝑠0). For 𝑤 :𝖶𝖯𝖲𝗍(𝐴), 𝑘 :𝐴 →𝖶𝖯𝖲𝗍(𝐵), and ℎ :𝐵 →𝖶𝖯𝖲𝗍(𝐶), fix 𝑞 :𝐶 ×𝑆 →𝖴. Then 𝖻𝗂𝗇𝖽𝗐𝗉𝖲𝗍(𝖻𝗂𝗇𝖽𝗐𝗉𝖲𝗍(𝑤,𝑘),ℎ)(𝑞)(𝑠0)𝑊𝑃−𝐵𝑖𝑛𝑑=𝑤(𝜆(𝑎,𝑠1).𝑘(𝑎)(𝜆(𝑏,𝑠2).ℎ(𝑏)(𝑞)(𝑠2))(𝑠1))(𝑠0)𝑊𝑃−𝐵𝑖𝑛𝑑=𝖻𝗂𝗇𝖽𝗐𝗉𝖲𝗍(𝑤,𝜆𝑎.𝖻𝗂𝗇𝖽𝗐𝗉𝖲𝗍(𝑘(𝑎),ℎ))(𝑞)(𝑠0). Extensionality in 𝑞 and 𝑠0 yields the two transformer equations.
Exercise 104.4.
In the state-preserving semantics, 𝗋𝖺𝗂𝗌𝖾(𝑒)(𝑝)(𝑞)(𝑠1) =𝑞(𝑒,𝑠1). In the rollback semantics a computation also receives its entry state 𝑠0, and failure uses 𝑞(𝑒,𝑠0), discarding the state at the point of failure. Start at zero and run 𝗉𝗎𝗍(1);𝗋𝖺𝗂𝗌𝖾(𝑒). The preserving transformer produces 𝑞(𝑒,1), while the rollback transformer produces 𝑞(𝑒,0). Choose 𝑞(𝑒′,𝑠):=(𝑒′ =𝑒 ∧𝑠 =1). The preserving precondition is true and the rollback precondition is false. Therefore the transformers are not extensionally equal, and the generated bind/catch laws of one semantics cannot be transferred without replacing the failure rule.