Prerequisites. Direct starred prerequisites: Chapter 23. No later core chapter depends on this route.
Theorem 23.11 proved that the scoped syntax 𝑇𝐴=𝜇𝑋.(𝐴+Σ𝑋+Γ(𝑇𝑋)) of definition 23.2 carries a monad structure, by displaying the substitution operation and checking its three laws on each constructor. The proof is a computation with the clauses of (23.17), and it says nothing about where the structure comes from. Three questions are left open by it. The nesting 𝑇(𝑇𝐴) in the scope constructor is not the shape of an algebra for an endofunctor, so 𝑇 is not visibly a free monad on anything. Interpreting a scoped term needs a carrier for each depth of nesting (example 23.8 used lists of lists), and the elementwise account gives no reason for that. And the monad laws were verified rather than derived, so a second construction of the same monad could not be recognized as the same.
This chapter answers all three at once. The nesting is replaced by two adjoint shift functors on indexed families; the scoped syntax becomes an ordinary free monad for an ordinary endofunctor on the indexed category; and the two presentations are compared by a pair of maps proved mutually inverse. Nothing about chapter 23 changes: the elementwise monad is the object of study, and the categorical monad is a second description of it.
The signature and the elementwise monad, restated
Fix the scoped signature (Σ,Γ) of definition 23.1: two endofunctors on 𝐒𝐞𝐭, presented elementwise by (23.4) as Σ𝑋 =∐𝑜𝑃𝑜 ×𝑋𝑅𝑜 and Γ𝑋 =∐𝑠𝑃𝑠 ×𝑋𝑄𝑠. Write 𝐸 for the scoped syntax monad 𝑇 of definition 23.2, with constructors 𝖵𝖺𝗋, 𝖮𝗉, 𝖲𝖼𝗈𝗉𝖾 as in (23.6) and with unit and multiplication as in (23.23). The hypothesis of definition 23.2 on the initial chain remains in force, and the base category is 𝐒𝐞𝐭 throughout; where a general C would do, the argument is the same and is not stated more generally.
Referenced from 4 locations
The letter 𝐸 is used for the elementwise monad so that the comparison in section 149.4 has two names to compare.
Indexed families and the two shifts
Let |ℕ| be the discrete category on the natural numbers and let 𝐒𝐞𝐭|ℕ| be the functor category: an object is a family 𝐴 =(𝐴𝑛)𝑛∈ℕ of sets and an arrow 𝐴 ⟶𝐵 is a family (𝑓𝑛 :𝐴𝑛 →𝐵𝑛), with no condition relating different indices. Coproducts are computed indexwise, (𝐴 +𝐵)𝑛 =𝐴𝑛 +𝐵𝑛, and an endofunctor Σ on 𝐒𝐞𝐭 lifts indexwise by (Σ𝐴)𝑛:=Σ(𝐴𝑛).
Referenced from 3 locations
The index 𝑛 will be the number of scopes enclosing a subterm. Two operations move a family along that count.
On 𝐒𝐞𝐭|ℕ| put (◃𝐴)𝑖:=𝐴𝑖+1,(▹𝐴)0:=∅,(▹𝐴)𝑖+1:=𝐴𝑖, with the evident action on arrows.
Referenced from 3 locations
▹ is left adjoint to ◃. Consequently arrows 𝐴 ⟶ ◃𝐵 correspond bijectively and naturally to arrows ▹𝐴 ⟶𝐵.
Referenced from 3 locations
Proof of Lemma 149.4
Proof. An arrow ▹𝐴 ⟶𝐵 is a family (𝑔𝑖 :( ▹𝐴)𝑖 →𝐵𝑖). At 𝑖 =0 the domain is ∅, so 𝑔0 is the unique empty map and carries no data; at 𝑖 +1 the domain is 𝐴𝑖, so the remaining data is a family (𝑔𝑖+1 :𝐴𝑖 →𝐵𝑖+1), which is exactly an arrow 𝐴 ⟶ ◃𝐵. The two assignments are mutually inverse by construction, and both are natural in 𝐴 and 𝐵 because they only reindex the components. The two equations of (142.5) are these reindexings. ◻
A scoped algebra for (Σ,Γ) is a quadruple ⟨𝐴,𝑎:Σ𝐴⟶𝐴,𝑑:Γ◃𝐴⟶𝐴,𝑝:𝐴⟶◃𝐴⟩ in 𝐒𝐞𝐭|ℕ|. The arrow 𝑑 is the demotion interpreting a scope creator and 𝑝 is the promotion performed on entering a scope.
Referenced from 5 locations
Scoped algebras for (Σ,Γ) are exactly the algebras for the endofunctor Σ +Γ ◃ + ▹ on 𝐒𝐞𝐭|ℕ|, and the two notions of morphism agree.
Referenced from 4 locations
Proof of Proposition 149.6 — Scoped algebras are ordinary algebras
Proof. By lemma 149.4 the promotion arrows 𝑝 :𝐴 ⟶ ◃𝐴 correspond bijectively and naturally to arrows ▹𝐴 ⟶𝐴. An algebra for a coproduct of endofunctors is a triple of arrows out of the three summands, so the data (𝑎,𝑑,𝑝) is exactly one arrow (Σ +Γ ◃ + ▹)𝐴 ⟶𝐴. A homomorphism condition for the coproduct algebra is the conjunction of the three componentwise conditions, and the third of these is transported by the bijection because the bijection is natural. ◻
Proposition 149.6 is the whole gain of the indexed category: the nesting 𝑇(𝑇𝐴) of (23.5), which is not the shape 𝐴 +𝐹𝑋 of an algebra, has been replaced by an ordinary signature, at the cost of carrying one carrier per scope depth.
Take Σ𝑋 =𝑋 ×𝑋 +1 with injections 𝗈𝗋 and 𝖿𝖺𝗂𝗅, and Γ𝑋 =𝑋 for the scoped operation 𝗈𝗇𝖼𝖾, as in (23.7). For a set 𝑋 put 𝐴𝑛:=List𝑛+1𝑋, the (𝑛 +1)-fold application of the list functor. Interpret 𝑎𝑛(𝗈𝗋(𝑥,𝑥′)):=𝑥++𝑥′,𝑎𝑛(𝖿𝖺𝗂𝗅):=[],𝑝𝑛(𝑥):=[𝑥], and let 𝑑𝑛 select the first element of a list of lists, with 𝑑𝑛([ ]):=[ ]. Every component is a function 𝐴𝑛 →𝐴𝑛, 𝐴𝑛 →𝐴𝑛+1 or 𝐴𝑛+1 →𝐴𝑛 of the right type, so this is a scoped algebra. The list-of-lists carrier that example 23.8 produced by hand is here forced: the promotion 𝑝 must land in 𝐴𝑛+1, and List applied once more is the smallest such choice for this interpretation.
Referenced from 6 locations
The free monad and the represented monad
Assume the forgetful functor 𝑈 :(Σ +Γ ◃ + ▹)-Alg →𝐒𝐞𝐭|ℕ| has a left adjoint 𝐹; this holds under the initial-chain hypothesis of convention 149.1. Put 𝑀:=𝑈𝐹=(Σ+Γ◃+▹)∗:𝐒𝐞𝐭|ℕ|→𝐒𝐞𝐭|ℕ|, the free monad on that endofunctor, with structure map in =[𝜂,𝖼Σ,𝖼Γ,𝖼▹].
Referenced from 2 locations
For every family 𝐴, (𝑀𝐴)0≅𝐴0+Σ(𝑀𝐴)0+Γ(𝑀𝐴)1,(𝑀𝐴)𝑛+1≅𝐴𝑛+1+Σ(𝑀𝐴)𝑛+1+Γ(𝑀𝐴)𝑛+2+(𝑀𝐴)𝑛.
Referenced from 8 locations
Proof of Lemma 149.9 — Reading the free monad indexwise
Proof. By Lambek’s lemma the structure map of the initial algebra is an isomorphism, so 𝑀𝐴 ≅𝐴 +Σ𝑀𝐴 +Γ ◃𝑀𝐴 + ▹𝑀𝐴. Evaluate at 𝑛: (Γ ◃𝑀𝐴)𝑛 =Γ((𝑀𝐴)𝑛+1) by definition 149.3 and the indexwise lifting, and ( ▹𝑀𝐴)0 =∅ while ( ▹𝑀𝐴)𝑛+1 =(𝑀𝐴)𝑛. A coproduct with ∅ is the other summand. ◻
The four summands read as four constructors. A variable at index 𝑛; an ordinary operation whose arguments sit under the same 𝑛 scopes; a scope creator, an opening bracket, whose arguments sit under 𝑛 +1 scopes; and a closing bracket whose argument is the continuation, outside the innermost scope, hence under 𝑛 −1. The absence of the fourth summand at index 0 is the statement that every closing bracket matches an opening one.
The functor ⇂ :𝐒𝐞𝐭|ℕ| →𝐒𝐞𝐭 with ⇂𝐴:=𝐴0 has a left adjoint ↾ given by ( ↾𝑋)0:=𝑋 and ( ↾𝑋)𝑛+1:=∅. Consequently ⇂𝑀 ↾ is a monad on 𝐒𝐞𝐭.
Referenced from 4 locations
Proof of Theorem 149.10 — The projection adjunction
Proof. An arrow ↾𝑋 ⟶𝐴 is a family whose component at 0 is a function 𝑋 →𝐴0 and whose components at 𝑛 +1 are the unique maps out of ∅; so it is exactly a function 𝑋 → ⇂𝐴, naturally in 𝑋 and 𝐴. This is the hom-set bijection of definition 142.17, and the two equations (142.5) are again reindexings. Composing ↾ ⊣ ⇂ with 𝐹 ⊣𝑈 by lemma 142.37 gives 𝐹 ↾ ⊣ ⇂𝑈, whose induced monad on 𝐒𝐞𝐭 has underlying functor ⇂𝑈𝐹 ↾ = ⇂𝑀 ↾. ◻
Sandwiching in this way is what removes variables from inside scopes: since ( ↾𝑋)𝑛+1 =∅, lemma 149.9 leaves no variable summand at a positive index, which matches the elementwise fact that substitution acts only on the outermost continuation ((23.17)).
The comparison
Two monads on 𝐒𝐞𝐭 are now in play: the elementwise 𝐸 of convention 149.1 and the represented ⇂𝑀 ↾ of theorem 149.10. The comparison is built from a distributive law that puts 𝑀-structure inside brackets.
Let 𝜆 :𝑀 ◃ ⟶ ◃𝑀 be the unique arrow induced by the algebra structure on ◃𝑀𝐴 whose four components are 𝜆var:=◃𝜂:◃𝐴⟶◃𝑀𝐴,𝜆Σ:=◃𝖼:Σ◃𝑀𝐴=◃Σ𝑀𝐴⟶◃𝑀𝐴,𝜆◃:=◃𝖼:Γ◃◃𝑀𝐴=◃Γ◃𝑀𝐴⟶◃𝑀𝐴,𝜆▹:=◃𝖼∘𝜀:▹◃𝑀𝐴⟶𝑀𝐴=◃▹𝑀𝐴⟶◃𝑀𝐴, with 𝜀 the counit of ▹ ⊣ ◃. The bracketing of 𝑀 is 𝑀 =𝑀 ◃ ▹𝜆→ ◃𝑀 ▹.
Referenced from 3 locations
The equalities Σ ◃ = ◃Σ and Γ ◃ = ◃Γ used above hold because Σ and Γ act indexwise and ◃ only renumbers indices.
The monads 𝐸 and ⇂𝑀 ↾ are isomorphic in the category of monads on 𝐒𝐞𝐭 and monad morphisms.
Referenced from 7 locations
Proof of Theorem 149.12 — The two monads agree
Proof. Both directions are defined by initiality, and each is then checked to be a monad morphism; the two composites are identities by a second appeal to initiality.
The arrow 𝑖 :𝐸 ⟶ ⇂𝑀 ↾. By definition 23.2, 𝐸 is the initial algebra of 𝑋 ↦Id +Σ𝑋 +Γ𝑋𝑋 in the sense that 𝐸𝐴 ≅𝐴 +Σ(𝐸𝐴) +Γ(𝐸(𝐸𝐴)), so an arrow out of 𝐸 is determined by three components. Take 𝑖var:=(Id 𝜂 ⟶⇂↾ ⇂𝜂 ←←←←←←→⇂𝑀↾),𝑖Σ:=(Σ⇂𝑀↾=⇂Σ𝑀↾ ⇂𝖼 ←←←←←←→⇂𝑀↾),𝑖Γ:=(Γ⇂𝑀↾⇂𝑀↾Γ⇂𝑀𝜀←←←←←←←←←→Γ⇂𝑀𝑀↾=Γ⇂𝑀◃▹𝑀↾Γ⇂𝜆←←←←←←←→Γ⇂◃𝑀▹𝑀↾=⇂Γ◃𝑀▹𝑀↾⇂𝖼𝖼←←←←←←←→⇂𝑀𝑀↾ ⇂𝜇 ←←←←←←→⇂𝑀↾), and let 𝑖 be the induced arrow. The middle composite is where the bracketing is used: a scoped operation of 𝐸 owns a term whose own variables are terms, and the bracketing converts that nesting into an opening and a closing bracket in 𝑀.
The arrow 𝑘 and the inverse. For an endofunctor 𝐺 on 𝐒𝐞𝐭 define 𝐺+ :𝐒𝐞𝐭 →𝐒𝐞𝐭|ℕ| by (𝐺+𝐴)𝑛:=𝐺𝑛+1𝐴. Using 𝑀𝐴 =𝜇𝑋. (𝐴 +Σ𝑋 +Γ ◃𝑋 + ▹𝑋), define 𝑘 :𝑀 ↾𝐴 ⟶𝐸+𝐴 by initiality from the four components: the variable component is 𝜂 :𝐴 →𝐸𝐴 =(𝐸+𝐴)0; the Σ and Γ components are the corresponding constructors of 𝐸 applied indexwise; and the ▹ component at index 𝑛 +1 is ( ▹𝐸+𝐴)𝑛+1 =(𝐸+𝐴)𝑛 =𝐸𝑛+1𝐴𝜂→𝐸 𝐸𝑛+1𝐴 =(𝐸+𝐴)𝑛+1. Put 𝑖−1:= ⇂𝑘 : ⇂𝑀 ↾𝐴 → ⇂𝐸+𝐴 =(𝐸+𝐴)0 =𝐸𝐴.
Mutually inverse. Both 𝑖−1 ∘𝑖 and the identity are algebra morphisms out of the initial algebra defining 𝐸, so they are equal; both 𝑖 ∘𝑖−1 and the identity are algebra morphisms out of the initial algebra defining 𝑀 ↾ after projecting with ⇂, so they are equal.
Monad morphism. Compatibility with the units is the equation 𝑖 ∘𝜂𝐸 =𝜂⇂𝑀↾, which is the variable component 𝑖var by construction. Compatibility with the multiplications, 𝑖 ∘𝜇𝐸 =𝜇⇂𝑀↾ ∘𝑖𝑖, is proved by initiality: both sides are algebra morphisms from the initial algebra presenting 𝐸𝐸, and they agree on each of the three components, the Γ component by naturality of 𝜆 and the two monad laws of 𝑀. ◻
Theorem 149.12 is Theorem 5.1 of Piróg, Schrijvers, Wu and Jaskelioff, Syntax and semantics for operations with scopes, LICS 2018, physical page 6; definition 149.5 is their Definition 4.1 and theorem 149.10 their Theorem 4.3, both on physical page 5. The proof above follows their sketch and supplies the initiality arguments they leave implicit.
Exercise 23.1 exhibited the term whose two readings differ, and (23.2) fixed the intended one. Take the term 𝗈𝗇𝖼𝖾(𝗈𝗋(𝖵𝖺𝗋1,𝖵𝖺𝗋5))≫=𝜆𝑥.𝗈𝗋(𝖵𝖺𝗋𝑥,𝖵𝖺𝗋(𝑥+1)). Under 𝑖 its image in ⇂𝑀 ↾ is the term whose root is the opening bracket for 𝗈𝗇𝖼𝖾, whose two children are 𝗈𝗋-nodes at index 1, and whose closing brackets return the continuation to index 0. Interpreting in the scoped algebra of example 149.7, the 𝗈𝗋 nodes at index 1 produce [[1],[2]] and [[5],[6]], the demotion 𝑑 selects the first, and the result is [1,2]: the same answer that (23.2) required, now obtained from the algebra rather than by hand.
Referenced from 3 locations
★☆☆ Compute the unit and counit of ▹ ⊣ ◃ componentwise, and check the two triangle identities (142.6) at indices 0 and 𝑛 +1 separately.
Referenced from 2 locations
★★☆ Prove from lemma 149.9 that for 𝐴 = ↾𝑋 every element of (𝑀𝐴)𝑛 with 𝑛 >0 contains at least one closing-bracket constructor on the path to each variable. Which summand of the lemma is empty, and why?
Referenced from 2 locations
★★☆ Take Σ𝑋:=𝑆 and Γ𝑋:=𝑋 ×𝑋𝑆 for a set 𝑆 of exceptions, so that the second component of Γ is the handler. Exhibit a scoped algebra with carrier 𝐴𝑛:=𝐻𝑛+1𝑋 for a suitable 𝐻, and identify the demotion morphism as the handler application.
Referenced from 2 locations
What the representation does not cover
Suggested first pass.
Begin with exercise 149.4 and exercise 149.5, then complete exercise 149.7.
★★☆ Write out the isomorphism of lemma 149.9 at 𝑛 =0,1,2 for the signature of example 149.7, and count the elements of (𝑀 ↾𝑋)1 for 𝑋 a two-element set and terms of depth at most two.
Referenced from 3 locations
★★★ Verify that 𝜆 of definition 149.11 is a distributive law of the monad 𝑀 over the endofunctor ◃: check the two equations 𝜆 ∘𝜂 ◃ = ◃𝜂 and 𝜆 ∘𝜇 ◃ = ◃𝜇 ∘𝜆𝑀 ∘𝑀𝜆 on each of the four constructors of lemma 149.9.
Referenced from 3 locations
★★☆ Give a signature (Σ,Γ) and a family 𝐴 for which the promotion 𝑝 of definition 149.5 cannot be chosen to be a family of bijections, and explain what that says about recovering the enclosing computation after leaving a scope.
Referenced from 2 locations
★★★ Practical project.scoped-monad-comparison Implement both presentations and check the isomorphism on named inputs. The program represents elementwise terms of 𝐸 over the signature of example 149.7 as trees with the three constructors of (23.6), and represents indexed terms of 𝑀 ↾ as trees with the four constructors of lemma 149.9, each node carrying its index. It implements 𝑖 and 𝑖−1 of theorem 149.12 and the scoped-algebra interpreter of example 149.7.
Invariant. Every indexed term is well indexed: a variable occurs only at index 0, an opening bracket at index 𝑛 has children at index 𝑛 +1, a closing bracket at index 𝑛 +1 has its child at index 𝑛, and no closing bracket occurs at index 0. The program checks this invariant after every construction and after every step of 𝑖 and 𝑖−1, and aborts naming the offending node rather than continuing.
Concrete result. For each input term the program prints the image under 𝑖, the image of that under 𝑖−1, and either round trip: equal or round trip: differs at followed by the first differing subterm. It also prints the value computed by the scoped-algebra interpreter on both representations.
Acceptance test. Run it on the term of example 149.13. The round trip must print equal; the interpreter must print [1,2] on both representations; and the printed indexed term must contain exactly one opening bracket at index 0 and two closing brackets at index 1. Run it on the term of exercise 23.1 that exposed the obstruction: the two representations must agree, and the printed value must be the one (23.2) requires and not the one (23.3) would give. A run in which the closing bracket is emitted at index 0 has implemented ▹ without its empty component, which is the defect this test detects.
Referenced from 3 locations
Sources. Definition 149.2 through theorem 149.12 follow M. Piróg, T. Schrijvers, N. Wu and M. Jaskelioff, Syntax and semantics for operations with scopes, LICS 2018 [PSWJ18]: the indexed category and the two shifts on physical page 5, the definition of scoped algebra as their Definition 4.1 and the reformulation of proposition 149.6 on the same page, the projection adjunction as their Theorem 4.3 and the indexwise reading of lemma 149.9 on physical pages 5–6, the bracketing distributive law and the comparison morphisms on physical page 6, and the monad isomorphism as their Theorem 5.1 on physical page 6. The elementwise construction that this chapter compares against is chapter 23, following Wu, Schrijvers and Hinze [WSH14]. The structured handler-based semantics with its own theorems, and the operational calculus with a type-safety statement, are Yang et al. [Y^+22] and Bosman et al. [BvdBTS24]; neither is used above. The intrinsically typed elaboration mentioned in remark 149.14 is the subject of the following chapter of the effects route and is not imported here.