Exercise 23.1.
The body 𝑀′ writes 𝑠0 +1 and returns 7. The continuation 𝑘′ is pure and returns 8. Thus the scope boundary is not observed: 𝗍𝗋𝖺𝗇𝗌𝖺𝖼𝗍𝗂𝗈𝗇(𝑀′)≫=𝑘′⇝commit 𝑠0+1,⇝then return 8,𝗍𝗋𝖺𝗇𝗌𝖺𝖼𝗍𝗂𝗈𝗇(𝑀′≫=𝑘′)⇝return 8 inside the transaction,⇝then commit 𝑠0+1. The original 𝑘 raises. On the left of (23.1) that raise occurs after the transaction has committed; on the right it occurs before the transaction closes and therefore triggers rollback. Algebraicity is a universal equation in the continuation. Finding one continuation 𝑘′ for which the two sides agree proves nothing about the failing continuation 𝑘, which is already a counterexample to the universal law.
Exercise 23.2.
Take ordinary operation symbols O ={𝖿𝖺𝗂𝗅,𝗈𝗋} and one scoped symbol S ={𝗈𝗇𝖼𝖾}. One convenient polynomial presentation is 𝑜𝑃𝑜𝑅𝑜𝖿𝖺𝗂𝗅10𝗈𝗋1{𝐿,𝑅}𝑠𝑃𝑠𝑄𝑠𝗈𝗇𝖼𝖾11. Hence their elementwise formation data are 𝖮𝗉𝖿𝖺𝗂𝗅(∗,𝑘):𝑇𝐴(𝑘:0→𝑇𝐴),𝖮𝗉𝗈𝗋(∗,𝑘):𝑇𝐴(𝑘:{𝐿,𝑅}→𝑇𝐴),𝖲𝖼𝗈𝗉𝖾𝗈𝗇𝖼𝖾(∗;𝑋;𝑚;𝑘):𝑇𝐴(𝑋:𝐒𝐞𝐭, 𝑚:1→𝑇𝑋, 𝑘:𝑋→𝑇𝐴). The last constructor is taken modulo the reindexing equation (23.12). The first two polynomial summands give 1 +𝑋 ×𝑋, and the scoped polynomial summand gives the unary functor Γ𝑋 ≅𝑋.
Exercise 23.3.
For raise, take one ordinary operation family parameterized by the exception: 𝑃𝗋𝖺𝗂𝗌𝖾 =𝐸 and 𝑅𝗋𝖺𝗂𝗌𝖾 =0. Its polynomial summand is therefore 𝐸 ×𝑋0 ≅𝐸.
For catch, take one scoped symbol with 𝑃𝖼𝖺𝗍𝖼𝗁 =1 and 𝑄𝖼𝖺𝗍𝖼𝗁=1+𝐸. Then 𝑋1+𝐸≅𝑋×𝑋𝐸, so the distinguished left position stores the protected computation and the 𝑒-position stores the recovery computation for exception 𝑒. Every position has type 𝑇𝑋 for one common intermediate set 𝑋. In particular, the protected computation and all recovery computations must return the same intermediate result type before the outside continuation is entered.
Exercise 23.4.
The canonical representative is [𝑇𝐴,𝑚′,𝗂𝖽𝑇𝐴],𝑚′(𝐿)=𝑇𝑘(𝑚(𝐿)),𝑚′(𝑅)=𝑇𝑘(𝑚(𝑅)). Now suppose 𝑘 =𝑘′ ∘ℎ. Packing the original representative gives 𝗉𝖺𝖼𝗄[𝑋,𝑚,𝑘](𝑞)(23.13)=𝑇(𝑘′∘ℎ)(𝑚(𝑞))functoriality of 𝑇=𝑇𝑘′(𝑇ℎ(𝑚(𝑞))). Packing the reindexed representative gives exactly 𝗉𝖺𝖼𝗄[𝑌,𝜆𝑞.𝑇ℎ(𝑚(𝑞)),𝑘′](𝑞)(23.13)=𝑇𝑘′(𝑇ℎ(𝑚(𝑞))). The two packed families agree pointwise at 𝐿 and 𝑅.
Exercise 23.5.
By the ordinary clause of explicit substitution, 𝗈𝗋(𝖵𝖺𝗋(1),𝖵𝖺𝗋(5))[𝑓](23.17)=𝗈𝗋(𝑓(1),𝑓(5))definition of 𝑓=𝗈𝗋(𝗈𝗋(𝖵𝖺𝗋(10),𝖵𝖺𝗋(11)),𝖵𝖺𝗋(50)). There are two 𝗈𝗋-nodes. The outer node is the original choice; the inner left node came from the replacement computation 𝑓(1).
Exercise 23.6.
The correct calculation is 𝗈𝗇𝖼𝖾(𝑡)[𝑓](23.20)=𝗈𝗇𝖼𝖾(𝑡;𝑓)definition of once=𝖲𝖼𝗈𝗉𝖾𝗈𝗇𝖼𝖾(∗;𝐴;𝜆_.𝗈𝗋(𝖵𝖺𝗋(1),𝖵𝖺𝗋(5));𝑓). The original scoped choice tree is unchanged; 𝑓 is stored as the outside continuation.
Both bad candidates transform the body to 𝑡[𝑓](23.17)=𝗈𝗋(𝗈𝗋(𝖵𝖺𝗋(1),𝖵𝖺𝗋(2)),𝗈𝗋(𝖵𝖺𝗋(5),𝖵𝖺𝗋(6))). Those two new inner 𝗈𝗋-nodes have been moved under 𝗈𝗇𝖼𝖾. False algebraicity produces 𝗈𝗇𝖼𝖾(𝑡[𝑓];𝖵𝖺𝗋), so it loses 𝑓 from the outside continuation. The all-fields traversal (23.16) instead produces 𝗈𝗇𝖼𝖾(𝑡[𝑓];𝑓): it transforms the body and also composes after the scope. These two erroneous trees are therefore distinct.
Exercise 23.7.
Definition 23.9 gives 𝗅𝗈𝖼𝖺𝗅(𝑛,𝑠,𝑡;𝑘)[𝑓](23.21)=𝗅𝗈𝖼𝖺𝗅(𝑛,𝑠,𝑡;𝜆𝑥.𝑘(𝑥)[𝑓]). Writing the first three fields of the displayed result as (𝑛𝑓,𝑠𝑓,𝑡𝑓), constructor injectivity gives (𝑛𝑓,𝑠𝑓,𝑡𝑓)=(𝑛,𝑠,𝑡). An implementation which changed an ordinary parameter would violate the first two components; one which descended into the scoped body would violate the third. Neither conclusion depends on an interpretation of local state.
Exercise 23.8.
By (23.25) and right identity, 𝑇𝗂𝖽(𝑡)(23.25)=𝑡[𝖵𝖺𝗋∘𝗂𝖽]function identity=𝑡[𝖵𝖺𝗋](23.22𝑏)=𝑡. For 𝑓 :𝐴 →𝐵 and 𝑔 :𝐵 →𝐶, 𝑇𝑔(𝑇𝑓(𝑡))(23.25)=𝑡[𝖵𝖺𝗋∘𝑓][𝖵𝖺𝗋∘𝑔]substitution composition=𝑡[𝜆𝑎.𝖵𝖺𝗋(𝑓(𝑎))[𝖵𝖺𝗋∘𝑔]]left unit=𝑡[𝜆𝑎.𝖵𝖺𝗋(𝑔(𝑓(𝑎)))](23.25)=𝑇(𝑔∘𝑓)(𝑡). Left unit is used exactly in the third line, where substitution into the returned variable 𝖵𝖺𝗋(𝑓(𝑎)) collapses to 𝖵𝖺𝗋(𝑔(𝑓(𝑎))).
Exercise 23.9.
By the definition of 𝖿𝗂𝗋𝗌𝗍, 𝖿𝗂𝗋𝗌𝗍(𝐹)(𝑎,𝑐)(23.29)=𝐹(𝑎)≫=𝜆𝑏.𝗋𝖾𝗍𝗎𝗋𝗇(𝑏,𝑐)definition of 𝐹=𝗈𝗇𝖼𝖾(𝑡𝑎;𝑘𝑎)≫=𝜆𝑏.𝗋𝖾𝗍𝗎𝗋𝗇(𝑏,𝑐)(23.20)=𝗈𝗇𝖼𝖾(𝑡𝑎;𝜆𝑥.𝑘𝑎(𝑥)≫=𝜆𝑏.𝗋𝖾𝗍𝗎𝗋𝗇(𝑏,𝑐)). The owned computation remains exactly 𝑡𝑎 :𝑇𝑋. The outside continuation changes from 𝑘𝑎 to 𝑥⟼𝑘𝑎(𝑥)≫=𝜆𝑏.𝗋𝖾𝗍𝗎𝗋𝗇(𝑏,𝑐), which pairs the final result with the untouched component 𝑐 only after the scope has closed.
Exercise 23.10.
Take a single scoped symbol 𝗍𝗋𝖺𝗇𝗌𝖺𝖼𝗍𝗂𝗈𝗇 with 𝑃𝗍𝗋𝖺𝗇𝗌𝖺𝖼𝗍𝗂𝗈𝗇 =1 and 𝑄𝗍𝗋𝖺𝗇𝗌𝖺𝖼𝗍𝗂𝗈𝗇 =1. Its scoped functor is therefore the identity. For 𝑀 :𝑇𝑋 and 𝑘 :𝑋 →𝑇𝐴, write 𝗍𝗋𝖺𝗇𝗌𝖺𝖼𝗍𝗂𝗈𝗇(𝑀;𝑘):=𝖲𝖼𝗈𝗉𝖾𝗍𝗋𝖺𝗇𝗌𝖺𝖼𝗍𝗂𝗈𝗇(∗;𝑋;𝜆_.𝑀;𝑘). Correct substitution gives 𝗍𝗋𝖺𝗇𝗌𝖺𝖼𝗍𝗂𝗈𝗇(𝑀;𝑘)≫=𝑓(23.20)=𝗍𝗋𝖺𝗇𝗌𝖺𝖼𝗍𝗂𝗈𝗇(𝑀;𝜆𝑥.𝑘(𝑥)≫=𝑓). The body 𝑀 is unchanged.
The false-algebraicity candidate is 𝗍𝗋𝖺𝗇𝗌𝖺𝖼𝗍𝗂𝗈𝗇(𝑀≫=(𝜆𝑥.𝑘(𝑥)≫=𝑓);𝖵𝖺𝗋). It is body-only: it moves the stored continuation and the new bind into the rollback region, then loses them from the outside continuation. Specializing 𝑘 =𝖵𝖺𝗋 gives exactly the failure exhibited by (23.1).
By contrast, the all-fields traversal (23.16) tries to transform both 𝑀 :𝑇𝑋 and the outside continuation. It is ill typed unless 𝑋 =𝐴 =𝐵. In that endomorphic homogeneous case it duplicates the post-computation: it moves 𝑓 into the body and also retains it after the scope. Thus the type failure belongs to the generic all-fields clause, whereas false algebraicity is typed but has the wrong transactional boundary.
Exercise 23.11.
For 𝑄𝑠 ={1,2}, equation (23.12) with 𝑚(1) =𝑚1 and 𝑚(2) =𝑚2 is exactly [𝑋,(𝑚1,𝑚2),𝑘∘ℎ](23.12)=[𝑌,(𝑇ℎ(𝑚1),𝑇ℎ(𝑚2)),𝑘]. Let 𝑓 :𝐴 →𝑇𝐵 and put 𝑘𝑓(𝑦) =𝑘(𝑦)[𝑓]. Substitution on the left produces [𝑋,(𝑚1,𝑚2),𝑘𝑓∘ℎ], while substitution on the right produces [𝑌,(𝑇ℎ(𝑚1),𝑇ℎ(𝑚2)),𝑘𝑓]. These are related by the same generator (23.12). The two scoped computations remain untouched throughout; only the outside continuation changes.
Exercise 23.12.
Write 𝑄 =1 +𝐸, with protected position ⋆ and recovery positions 𝑒 :𝐸. Define the family 𝑚(⋆):=𝑀,𝑚(𝑒):=𝐻(𝑒). The catch node is 𝐶:=𝖲𝖼𝗈𝗉𝖾𝖼𝖺𝗍𝖼𝗁(∗;𝑋;𝑚;𝑘). For 𝑓 :𝐴 →𝑇𝐵, 𝐶≫=𝑓(23.20)=𝖲𝖼𝗈𝗉𝖾𝖼𝖺𝗍𝖼𝗁(∗;𝑋;𝑚;𝜆𝑥.𝑘(𝑥)≫=𝑓). Neither 𝑀 nor any 𝐻(𝑒) changes. A second bind by 𝑔 :𝐵 →𝑇𝐶 gives the calculation 𝖲𝖼𝗈𝗉𝖾𝖼𝖺𝗍𝖼𝗁(∗;𝑋;𝑚;𝜆𝑥.(𝑘(𝑥)≫=𝑓)≫=𝑔)associativity in every branch=𝖲𝖼𝗈𝗉𝖾𝖼𝖺𝗍𝖼𝗁(∗;𝑋;𝑚;𝜆𝑥.𝑘(𝑥)≫=(𝜆𝑎.𝑓(𝑎)≫=𝑔)). The last term is the one-step substitution by the composite continuation. The proof is syntactic; no exception interpretation is used.
Exercise 23.13.
The elementwise smart constructor is 𝗈𝗇𝖼𝖾(𝑡):=[𝐴,𝜆_.𝑡,𝖵𝖺𝗋]. Packing gives the canonical body 𝗉𝖺𝖼𝗄[𝐴,𝜆_.𝑡,𝖵𝖺𝗋](23.13)=𝜆_.𝑇𝖵𝖺𝗋(𝑡):1→𝑇(𝑇𝐴). Thus the nested node is 𝖲𝖼𝗈𝗉𝖾(𝖮𝗇𝖼𝖾(𝑇𝖵𝖺𝗋(𝑡))), exactly the source’s 𝖿𝗆𝖺𝗉 𝗋𝖾𝗍𝗎𝗋𝗇 construction.
After substitution by 𝑓 :𝐴 →𝑇𝐵, the elementwise node is [𝐴,𝜆_.𝑡,𝑓]. Reindex it along 𝑓 :𝐴 →𝑇𝐵, with final continuation 𝗂𝖽𝑇𝐵, to obtain [𝑇𝐵,𝜆_.𝑇𝑓(𝑡),𝗂𝖽𝑇𝐵]. For a general canonical scope whose body is 𝑢 :𝑇(𝑇𝐴), the same reindexing uses the function 𝑏𝑓 :𝑇𝐴 →𝑇𝐵, 𝑏𝑓(𝑣) =𝑣≫=𝑓, and yields body 𝑇𝑏𝑓(𝑢). Applying the scoped functor therefore gives 𝖲𝖼𝗈𝗉𝖾(𝑣)≫=𝑓(23.18)=𝖲𝖼𝗈𝗉𝖾(Γ(𝑇𝑏𝑓)(𝑣)), which is (23.18). The first canonicalization uses functorial renaming 𝑇𝖵𝖺𝗋; the second uses reindexing (23.12) followed by the functor action 𝑇𝑏𝑓.
Exercise 23.14.
Let 𝜏𝑍 :𝐻𝑍 →𝑇𝑍 be the assumed sortwise translation. Put ̂𝜓𝐿:=𝜏ℕ(𝜓𝐿):𝑇ℕ,̂𝜓𝑅:=𝜏𝟐(𝜓𝑅):𝑇𝟐, and translate the continuation pointwise by ̂𝜅(𝑟):=𝜏𝐴(𝜅(𝑟)):𝑇𝐴. With 𝑋:=ℕ +𝟐, functorial renaming gives 𝑇𝗂𝗇𝗅(̂𝜓𝐿):𝑇𝑋,𝑇𝗂𝗇𝗋(̂𝜓𝑅):𝑇𝑋. Thus differing fork-result types alone are not an obstruction.
Suppose first that 𝜌 :𝑋 →𝑅 is supplied. For every set 𝑌, define Φ𝑌(ℓ):=ℓ∘𝜌(ℓ:𝑅→𝑌). For 𝑢 :𝑌 →𝑍, associativity of composition gives 𝑢∘Φ𝑌(ℓ)𝑡𝑟𝑎𝑛𝑠𝑙𝑎𝑡𝑖𝑜𝑛𝑑𝑒𝑓𝑖𝑛𝑖𝑡𝑖𝑜𝑛=𝑢∘(ℓ∘𝜌)associativity=(𝑢∘ℓ)∘𝜌𝑡𝑟𝑎𝑛𝑠𝑙𝑎𝑡𝑖𝑜𝑛𝑑𝑒𝑓𝑖𝑛𝑖𝑡𝑖𝑜𝑛=Φ𝑍(𝑢∘ℓ), so the family is natural in 𝑌.
Conversely, suppose Φ is natural, and define 𝜌:=Φ𝑅(𝗂𝖽𝑅):𝑋→𝑅. For any ℓ :𝑅 →𝑌, naturality with post-map ℓ :𝑅 →𝑌 gives ℓ∘𝜌𝑎𝑑𝑎𝑝𝑡𝑒𝑟𝑑𝑒𝑓𝑖𝑛𝑖𝑡𝑖𝑜𝑛=ℓ∘Φ𝑅(𝗂𝖽𝑅)𝑛𝑎𝑡𝑢𝑟𝑎𝑙𝑖𝑡𝑦=Φ𝑌(ℓ∘𝗂𝖽𝑅)identity law=Φ𝑌(ℓ). Thus every natural family is precomposition by the recovered adapter. The two constructions are inverse by the same equations.
Now take 𝑅 =∅. The carrier 𝑋 =ℕ +𝟐 is inhabited by 𝗂𝗇𝗅(0). If 𝜌 :𝑋 →∅ existed, then 𝜌(𝗂𝗇𝗅(0)) would be an element of the empty set, a contradiction. Hence the fork can be homogenized, but its continuation domain cannot be structurally converted to the scoped carrier.
For the restricted positive case, take 𝑅 =𝑋 and 𝜌 =𝗂𝖽𝑋. Then Φ𝑌(ℓ)𝑡𝑟𝑎𝑛𝑠𝑙𝑎𝑡𝑖𝑜𝑛𝑑𝑒𝑓𝑖𝑛𝑖𝑡𝑖𝑜𝑛=ℓ∘𝗂𝖽𝑋identity law=ℓ, so the continuation already has the scoped domain. For a scoped symbol with the two fork positions and a matching parameter 𝑝 :𝑃𝑠, the complete translated node is 𝖲𝖼𝗈𝗉𝖾𝑠(𝑝;𝑋;(𝑇𝗂𝗇𝗅(̂𝜓𝐿),𝑇𝗂𝗇𝗋(̂𝜓𝑅));̂𝜅). The response identification, equivalently the adapter 𝜌 :𝑋 →𝑅, is additional operation-specific signature data. An arbitrary higher-order signature does not contain it, so this conditional bridge is not an equality or a general inclusion of the two calculi.