1 Initial Algebra Semantics for Cyclic Sharing Structures Makoto Hamana Department of Computer Science, Gunma University, Japan August 2009, GoI Workshop, Kyoto http://www.cs.gunma-u.ac.jp/ ˜ hamana/
2 This Work ⊲ How to inductively capture cylces and sharing ⊲ Intended to apply it to functional programming
3 Introduction ⊲ Terms are a representation of tree structures (i) Reasoning: structural induction (ii) Functional programming: pattern matching, structural recursion (iii) Type: inductive type (iv) Initial algebra property
4 Introduction ⊲ What about tree-like structures? ⊲ How can we represent this data in functional programming? ⊲ Maybe: vertices and edges set, adjacency lists, etc. ⊲ Give up to use pattern matching, structural induction ⊲ Not inductive
5 Introduction Are really no inductive structures in tree-like structures?
6 This Work ⊲ Gives an initial algebra characterisation of cyclic sharing structures Aim ⊲ To derive the following from ↑ : [I] A simple term syntax that admits structural induction / recursion [II] To give an inductive type that represents cyclic sharing structures uniquely in functional languages and proof assistants
7 Variations of Initial Algebra Semantics ⊲ Various computational structures are formulated as initial algebras by varying the base category Abstract syntax Set ADJ 1975 Set S S -sorted abstract syntax Robinson 1994 Set F Abstract syntax with binding Fiore,Plotkin,Turi 1999 Recursive path ordring LO R. Hasegawa 2002 ↓ S ) S ( Set F S -sorted 2nd-order abs. syn. Fiore 2003 Pre F 2nd-order rewriting systems Hamana 2005 Explicit substitutions [ Set , Set ] f 2006 Ghani,Uustalu,Hamana ( Set T ∗ ) T Cyclic sharing structures Hamana 2009
8 Basic Idea
9 Basic Idea: Graph Algorithmic View ⊲ Traverse a graph in a depth-first search manner: Depth-First Search tree ⊲ DFS tree consists of 3 kinds of edges: (i) Tree edge (ii) Back edge (iii) Right-to-left cross edge ⊲ Characterise pointers for back and cross edges
10 Formulation: Cycles by µ -terms Idea ⊲ Binders as pointers ⊲ Back edges = bound variables Cycles µx. bin ( µy 1 . bin ( lf (5) , lf (6)) , µy 2 . bin ( x, lf (7) ))
11 Formulation: Sharing via ? Idea ⊲ Binders as pointers ⊲ Back edges = bound variables ⊲ Right-to-left Cross edges = ? Sharing µx. bin ( µy 1 . bin ( lf (5) , lf (6)) , µy 2 . bin ( , lf (7))) . Can we fill the blank to refer the node 5 by a bound variable?
12 Formulation: Sharing via Pointer ⊲ Cross edges = pointers by a new notation µx. bin ( µy 1 . bin ( lf (5) , lf (6)) , µy 2 . bin ( ւ 11 ↑ x , lf (7))) Pointer ւ 11 ↑ x means ⊲ going back to the node x , then ⊲ going down through the left child twice (by position 11 )
13 Formulation: Sharing via Pointer ⊲ Cross edges = pointers by a new notation µx. bin ( µy 1 . bin ( lf (5) , lf (6)) , µy 2 . bin ( ւ 11 ↑ x , lf (7))) Pointer ւ 11 ↑ x means Need to ensure a correct pointer only!! ⊲ going back to the node x , then ⊲ going down through the left child twice (by position 11 )
14 Typed Abstract Syntax for Cyclic Sharing Structures
15 Shape Trees ⊲ Skeltons of cyclic sharing trees Shape trees τ ::= E | P | L | B( τ 1 , τ 2 ) ⊲ Used as types ⊲ Blue nodes represent possible positions for sharing pointers.
16 Syntax and Types Typing rules p ∈ P os ( σ ) k ∈ Z Γ , x : σ, Γ ′ ⊢ ւ p ↑ x : P Γ ⊢ lf ( k ) : L x : B(E , E) , Γ ⊢ ℓ : σ x : B( σ, E) , Γ ⊢ r : τ Γ ⊢ µx. bin ( ℓ, r ) : B( σ, τ ) ⊲ A type declaration x : σ means: “ σ is the shape of a subtree headed by µx ”. ⊲ Taking a position p ∈ P os ( σ ) safely refers to a position in the subtree.
17 Example: making bin-node x : B(E , E) ⊢ x : B(B(L , L) , E) ⊢ µy 1 . bin (5 , 6) : B(L , L) µy 2 . bin ( ւ 11 ↑ x, 7) : B(P , L) ⊢ µx. bin ( µy 1 . bin (5 , 6) , µy 2 . bin ( ւ 11 ↑ x, 7)) : B(B(L , L) , B(P , L))
18 Syntax and Types Typing rules (de Bruijn version) | Γ | = i − 1 p ∈ P os ( σ ) k ∈ Z Γ , σ, Γ ′ ⊢ ւ p ↑ i : P Γ ⊢ lf ( k ) : L B(E , E) , Γ ⊢ s : σ B( σ, E) , Γ ⊢ t : τ Γ ⊢ bin ( s, t ) : B( σ, τ ) Thm. Given rooted, connected and edge-ordered graph, the term representation in de Bruijn is unique.
19 Cyclic Sharing Data Structures ⊲ Sharing via cross edge ⊲ Term bin ( bin ( bin ( ↑ 3 , lf (6)) , ւ 1 ↑ 1) , lf (9))
20 Initial Algebra Semantics ⊲ Cyclic sharing trees are all well-typed terms: T τ (Γ) = { t | Γ ⊢ t : τ } ⊲ Need: sets indexed by contexts T ∗ and shape trees T Consider algebras in ( Set T ∗ ) T
21 Initial Algebra Semantics ⊲ Algebra of an endofunctor Σ : Σ -algebra ( A, α : Σ A → A ) ✲ ( Set T ∗ ) T for ⊲ Functor Σ : ( Set T ∗ ) T cyclic sharing trees is defined by (Σ A ) E = 0 (Σ A ) P = PO (Σ A ) L = K Z (Σ A ) B( σ,τ ) = δ B(E , E) A σ × δ B( σ, E) A τ
22 Initial Algebra Semantics ⊲ Σ -algebra ( A, α : Σ A → A ) ✲ ( Set T ∗ ) T for ⊲ Functor Σ : ( Set T ∗ ) T cyclic sharing trees is given by ptr A : PO → A P lf A : K Z → A L bin σ,τ A : δ B(E , E) A σ × δ B( σ, E) A τ → A B( σ,τ ) Typing rules (de Bruijn version) | Γ | = i − 1 p ∈ P os ( σ ) k ∈ Z Γ , σ, Γ ′ ⊢ ւ p ↑ i : P Γ ⊢ lf ( k ) : L B(E , E) , Γ ⊢ s : σ B( σ, E) , Γ ⊢ t : τ Γ ⊢ bin ( s, t ) : B( σ, τ )
23 Initial Algebra ⊲ All cyclic sharing trees T τ (Γ) = { t | Γ ⊢ t : τ } Thm. T forms an initial Σ -algebra. [Proof] ⊲ Smith-Plotkin construction of an initial algebra
24 Principles The initial algebra characterisation derives (i) Structural recursion by the unique homomorphism (ii) Structural induction by [Hermida,Jacobs I&C’98] (iii) Inductive type (in Haskell)
25 Structural Recursion Principle ✲ A is: Thm. The unique homomorphism φ : T φ P (Γ)( ւ p ↑ i ) = ptr A (Γ)( ւ p ↑ i ) φ L (Γ)( lf ( k )) = lf A (Γ)( k ) φ B( σ,τ ) (Γ)( bin ( s, t )) = bin A (Γ)( φ σ (B(E , E) , Γ)( s ) , φ τ (B( σ, E) , Γ)( t )) ⊲ “fold” in Haskell
26 Structural Induction Principle Thm. Let P be a predicate on T . To prove that P Γ τ ( t ) holds for all t ∈ T τ (Γ) , it suffices to show (i) P Γ P ( ւ p ↑ i ) holds for all ւ p ↑ i ∈ PO(Γ) , (ii) P Γ L ( lf ( k )) holds for all k ∈ Z , (iii) If P B(E , E) , Γ ( s ) & P B( σ, E) , Γ ( t ) holds, then σ τ P Γ B( σ,τ ) ( bin ( s, t )) holds.
27 Inductive Type for Cyclic Sharing Structures Constructors of the initial algebra T ∈ ( Set T ∗ ) T ptr T (Γ) : PO(Γ) → T P (Γ); ւ p ↑ i �→ ւ p ↑ i. lf T (Γ) : Z → T L (Γ); k �→ lf ( k ) . bin σ,τ T (Γ) : T σ (B(E , E) , Γ) × T τ (B( σ, E) , Γ) → T B( σ,τ ) (Γ) GADT in Haskell data T :: * -> * -> * where Ptr :: Ctx n => n -> T n P Lf :: Ctx n => Int -> T n L Bin :: (Ctx n, Shape s, Shape t) => T (TyCtx (B E E) n) s -> T (TyCtx (B s E) n) t -> T n (B s t) ⊲ Dependent type def. in Agda is more straightforward
28 Summary ⊲ An initial algebra characterisation Goals ⊲ To derive the following from ↑ : [I] A simple term syntax [II] An inductive type for cyclic sharing structures Reference M. Hamana. Initial Algebra Semantics for Cyclic Sharing Structures, TLCA’09.
29 Connections to Other Works There are interpretations: ✲ S ! ✲ Equational Term Graphs T where S is any of (i) Coalgebraic (ii) Domain-theoretic (iii) Categorical semantics: Traced sym. monoidal categories [Hasegawa’97] – (Equational) term graphs [Barendregt et al.’87][Ariola,Klop’96]
30 Connection to Traced Categorical Semantics ⊲ Interpretations ! ✲ Equational Term Graphs ∼ ✲ ( F : C → M ) T = letrec-Exprs ⊲ Cartesian-center symmetric traced monoidal category = identity-on-object functor F : C → M – Cartesian C – Symmetric traced monoidal M
31 Cyclic Sharing Data Structures ⊲ Sharing via cross edge ⊲ Term µx. bin ( µy 1 . bin ( µz. bin ( ↑ x, lf (6)) , ւ 1 ↑ y 1 ) , lf (9))
32 µx. bin ( µy 1 . bin ( µz. bin ( ↑ x, lf (6)) , ւ 1 ↑ y 1 ) , lf (9)) de Br. = bin ( bin ( bin ( ↑ 3 , lf (6)) , ւ 1 ↑ 1) , lf (9)) �→ bin ǫ ( bin 1 ( bin 11 ( ↑ 111 3 , lf 112 (6)) , ւ 1 ↑ 12 1) , lf 2 (9)) { ǫ | = bin (1 , 2) ǫ = bin (11 , 12) 1 11 = bin (111 , 112) �→ 12 = 11 111 = ǫ 112 = lf (6) 2 = lf (9) } �→ letrec ( ǫ, 1 , 11 , 12 , 111 , 112 , 2) = ( bin (1 , 2) , bin (1 , 12) , bin (111 , 112) , 11 , ǫ, lf (6) , lf (9)) in ǫ F (∆); (id ⊗ Tr ( F ∆ 7 ; ( [ [ ǫ, 1 , . . . ⊢ bin (1 , 2)] ] ⊗ Hasegawa �→ [ ǫ, 1 , . . . ⊢ bin (11 , 12)] [ ] ⊗ · · · ); F ∆)); F π 1
33 Connection to Traced Categorical Semantics ⊲ How useful? ⊲ Application: Haskell’s “arrows” [Hughes’00][Paterson’01] – Arrow-type in Haskell (or, Freyd category) is a cartesian-center premonoidal category [Heunen, Jacobs, Hasuo’06] – Arrow with loop is a cartesian-center traced premonoidal category [Benton, Hyland’03] – Cyclic sharing theory is interpreted in a cartesian-center traced monoidal category [Hasegawa’97] ⊲ What impact for functional progrmming?
Recommend
More recommend