Categories of Containers Michael Abbott Thorsten Altenkirch Neil Ghani APPSEM-II 26 th –27 th March 2003 1
Strictly Positive Types If X is a type variable, K a constant type and F , G are strictly positive types then so are µ X . F ν X . F F K F + G F × G X K Each strictly positive type in n variables can be interpreted as a functor Set n → Set nor 2 2 X Note that neither X X are strictly positive. 2
Functors SP types Set n → Set + polymorphic + natural functions transformations Containers + container morphisms All inclusions are full and faithful and preserve limits and coproducts (but not coequalisers). The category of containers is complete (and cocomplete). Set can be replaced by a locally cartesian closed and locally presentable category. 3
Example types: pairs, trees, lists. A pair consists of a choice of two elements of X X x 1 · · x 2 A binary tree. X • • x 3 x 3 ∼ • • · = x 2 x 1 x 2 x 1 · · An element σ ∈ List X is σ = σ 1 ... σ n , ie X σ 1 σ 2 � � ... � n 1 2 σ n σ : { 1 .. n } → X n : N 4
A “container” is generated by a set of “shapes” S and for each shape s ∈ S a set of “positions” P s thus: ( S ⊲ P ) ∈ G is ( P s ) s ∈ S . An element of the associated container type, the “extension” of ( S ⊲ P ) , is a choice of shape s ∈ S and a function P s → X , defining a functor Set → Set thus X �→ ∑ s ∈ S X P s . 5
What is a morphism of containers? Eg: flatten : Tree X → List X • • �→ tail : List X → MaybeList X �→ �→ � � In general, each shape is mapped to a new shape and each position is mapped back to the position that generates it. Thus a morphism of containers ( u , f ) : ( S ⊲ P ) → ( R ⊲ Q ) is � � u : S → R f s : Q us → P s s ∈ S 6
We can now give a semantics for the the ( K , X , + , × , − K ) fragment of strictly positive types ( 1 ⊲ 1 ) X ( K ⊲ 0 ) K ( S ⊲ P ) × ( R ⊲ Q ) ( s : S , r : R ⊲ P s + Q r ) ( S + R ⊲ P ◦ ( S ⊲ P )+( R ⊲ Q ) + Q ) f : S K ⊲ ∑ k ∈ K P fk ( S ⊲ P ) K � � For x ∈ S + R define ( P ◦ + Q ) x ≡ case x of ( s ∈ S : P s � r ∈ R : Q r ) . The remaining types X i , µ X i . F , ν X i . F need a little more work. 7
Least Fixed Points Let F ≡ ( S ⊲ P , Q ) be given with extension F ( X , Y ) = ∑ s ∈ S X P s × Y Q s . Can we write the fixed point of F as a container = ∑ s ∈ S ′ X P ′ µ Y . F ( X , Y ) ∼ s ? We know from Dybjer that for each X we can use W -types to compute µ Y . F ( X , Y ) . However, nested W -types are more problematic. 8
F ( X , Y ) = ∑ s ∈ S X P s × Y Q s = ∑ s ∈ S ′ X P ′ µ Y . F ( X , Y ) ∼ s ? If so we can compute S ′ : = µ Y . F ( 1 , Y ) = µ Y . ∑ s ∈ S Y Q s ∼ S ′ ∼ = W ( S , Q ) . The family of positions can be described as an inductive construction: = P s + ∑ q ∈ Q s P ′ s ∈ S , f : Q s → S ′ ⊢ P ′ in ( s , f ) ∼ fq . 9
Lists as Fixed Points = ∑ n ∈ N X [ n ] List X ∼ = µ Y .1 + X × Y ∼ = X 0 × Y 0 + X 1 × Y 1 1 + X × Y ∼ ( S ⊲ P , Q ) ≡ ( 2 ⊲ ( 0 ◦ + 1 ) , ( 0 ◦ + 1 )) S ′ = µ Y .1 + Y ∼ = N P ′ 0 = 0 P ′ n + 1 = 1 + P ′ n This yields, as expected List = ( S ′ ⊲ P ′ ) ∼ = ([ n ]) n ∈ N 10
Joyal, Foncteurs Analytique et Esp´ eces de Structures Conor McBride, The Derivative of a Regular Type is its Type of One-Hole Contexts Abbott, Altenkirch, Ghani, McBride, Derivatives of Containers A generic construction ∂ together with a universal one place substitution s : ∂ F × X − → F The operator ∂ satisfies the isomorphisms expected of a derivative. ∂ ( F + G ) ∼ ∂ X ∼ ∂ K ∼ = ∂ F + ∂ G = 1 = 0 ∂ ( F × G ) ∼ = ∂ F × G + F ∂ × G ∂ ( F ◦ G ) ∼ = ( ∂ F ◦ G ) × ∂ G Differentiation of least and greatest fixed points also works. 11
Recommend
More recommend