Indexed Containers based on joint work with Neil Ghani, Peter Hancock, Conor McBride, Peter Morris Thorsten Altenkirch School of Computer Science and IT University of Nottingham April 26, 2007 Thorsten Altenkirch tallinn
Inductive types Example: λ terms ala de Bruijn: Lam ∈ Set : ∈ N → Lam var ∈ Lam → Lam → Lam app ∈ Lam → Lam lam In Epigram syntax: t , u ∈ Lam i ∈ N t ∈ Lam app t u ∈ Lam lam t ∈ Lam var i ∈ Lam Natural numbers (ala Peano): n ∈ N 0 ∈ N succ n ∈ N Initial algebra semantics: F N X = 1 + X N + X × X + X F Lam X = Thorsten Altenkirch tallinn
Indexed inductive types λ -terms indexed by the number of free variables: Lam ∈ N → Set t , u ∈ Lam n i ∈ Fin n t ∈ Lam ( n + 1 ) app t u ∈ Lam n var i ∈ Lam n lam t ∈ Lam n Finite sets with n elements: Fin ∈ N → Set : n ∈ N i ∈ Fin n fzero ∈ Fin ( n + 1 ) fsucc i ∈ Fin ( n + 1 ) Initial algebra sematics: ∈ ( N → Set ) → N → Set F Lam , F Fin F Lam X n = Fin n + X n × X n + X ( n + 1 ) F Fin X n = Σ m ∈ N . m + 1 = n × ( 1 + X m ) Thorsten Altenkirch tallinn
Typed λ -terms ∈ Ty Set ∈ [ Ty ] → Ty → Set Var , Lam σ, τ ∈ Ty ty ∈ Ty arr σ τ ∈ Ty x ∈ Var Γ σ vzero ∈ Var ( σ : Γ) σ vsucc x ∈ Var ( τ : Γ) σ t ∈ Lam Γ ( arr σ τ ) u ∈ Lam Γ σ x ∈ Var Γ σ var x ∈ Lam Γ σ app t u ∈ Lam Γ τ t ∈ Lam ( σ : Γ) τ lam t ∈ Lam Γ ( arr σ τ ) Thorsten Altenkirch tallinn
Why container? Functorial semantics is too general: not every funtors has an initial algebra, e.g. F X = ( X → Bool ) → Bool Strictly positive functors? Too syntactic. Theory of Containers: semantic counterpart of strictly positive Useful for generic programming (e.g. derivatives of datatypes) Now: extend containers to indexed datatypes. Thorsten Altenkirch tallinn
Unary container A unary container S ⊳ P ∈ Cont is given by: S ∈ Set a set of shapes P ∈ S → Set a family of positions Extension of a container as a functor: S ⊳ P ∈ Cont � S ⊳ P � X = Σ s ∈ S . P s → X � S ⊳ P � ∈ Set → Set Example Lists: � N ⊳ Fin � X = [ X ] . Obs: � S ⊳ P � {∗} = S Thorsten Altenkirch tallinn
Container morphisms f ⊳ u ∈ Cont ( S ⊳ P ) ( T ⊳ Q ) is given by: f ∈ S → T ∈ Π s ∈ S . Q s → P s u Every morphism gives rise to a natural transformation: f ⊳ u ∈ Cont ( S ⊳ P ) ( T ⊳ Q ) � f ⊳ u � ∈ Π X ∈ Set . � S ⊳ P � X → � T ⊳ Q � X � f ⊳ u � X ( s , p ) = ( f s , p ◦ u s ) Example: rev ∈ Π X ∈ Set . List X → List X = � λ n . n ⊳ λ n , i . n − i � rev Theorem (Abbott, A., Ghani): The extension functor � − � is full and faithful. Thorsten Altenkirch tallinn
Constructions on containers Given container S ⊳ P , T ⊳ Q : Coproduct ( S ⊳ P ) + ( T ⊳ Q ) = S + T ⊳ λ Left s P s . Right t . Q t Product ( S ⊳ P ) × ( T ⊳ Q ) = S × T ⊳ λ ( s , t ) . P s + Q t Constant exponentiation K → ( S ⊳ P ) = K → S ⊳ λ f . Σ k ∈ K . P ( f k ) Thorsten Altenkirch tallinn
n -ary container An n -ary container S ⊳ P ∈ Cont n is given by: S ∈ Set a set of shapes P ∈ S → Fin n → Set n families of positions Extension of a container as a functor: S ⊳ P ∈ Cont n � S ⊳ P � ∈ ( Fin n → Set ) → Set � S ⊳ P � X = Σ s ∈ S . Π i ∈ Fin n . P s i → X i Example: leaf/node labelled trees Thorsten Altenkirch tallinn
Closure under µ S ⊳ P , Q ∈ Cont 2 µ ( S ⊳ P , Q ) ∈ Cont 1 µ ( S ⊳ P , Q ) = W S P ⊳ Path S P Q W-types: Given S ∈ Set , P ∈ S → Set : W S P ∈ Set . s ∈ S f ∈ P s → W S P sup s f ∈ W S P Paths, additionally given Q ∈ S → Set , Path S P Q ∈ W S P → Set q ∈ Q s p ∈ P s r ∈ Path S P Q ( f p ) top q ∈ Path S P Q ( sup s f ) down p r ∈ Path S P Q ( sup s f ) Lemma (Abbott,A.,Ghani): Path S P Q is definable using W-types. Thorsten Altenkirch tallinn
Strictly Positive Types n ∈ N i ∈ Fin n 0 T , 1 T ∈ SPT n η T i ∈ SPT n SPT n ∈ Type S , T ∈ SPT n S ∈ SPT n K ∈ Set S + T , S × T ∈ SPT n K → S ∈ SPT n S ∈ SPT ( n + 1 ) µ S ∈ SPT n Thorsten Altenkirch tallinn
Indexed container Given I ∈ Set , an I -indexed container S ⊳ P ∈ Cont I is S ∈ Set a set of shapes given by: P ∈ S → I → Set n families of positions Extension of a container as a functor: S ⊳ P ∈ Cont n � S ⊳ P � ∈ ( I → Set ) → Set � S ⊳ P � X = Σ s ∈ S . Π i ∈ I . P s i → X i Examples Fin , Lam , . . . Thorsten Altenkirch tallinn
Strictly positive indexed types I ∈ Set i ∈ I 0 T , 1 T ∈ SPT I η T i ∈ SPT I SPT I ∈ Type f ∈ I → J F ∈ I → SPT O F ∈ J → SPT ( I + J ) Π T f F ∈ J → SPT O µ F ∈ J → SPT I Σ T f F ∈ J → SPT O F Fin , F Lam ∈ N → SPT N Σ T ( λ n . 1 + n ) ( λ n . 1 + η T n ) F Fin = Fin , Lam ∈ N → SPT ∅ = µ F Fin Fin ( Fin ) + N + ( η T n ) 2 + η T ( n + 1 ) F Lam n = = Lam µ F Lam Thorsten Altenkirch tallinn
Summary We can extend the results for ordinary containers to indexed containers. The type of indexed strictly positive types is itself an indexed strictly positive type. We still only need W-types! Interpret inductive schemes in a very small core theory. Application: Small trusted code base for proof assistants. Generic programming for dependent types. Thorsten Altenkirch tallinn
Recommend
More recommend