Vectors are records, too! Jesper Cockx 1 etan Gilbert 2 Ga¨ Nicolas Tabareau 2 Matthieu Sozeau 2 1 Gothenburg University, Sweden 2 INRIA, France 21 June 2018
types ’ most popular example 1 data V ( A : Set) : ( n : N ) → Set where nil : V A zero cons : ( m : N )( x : A )( xs : V A m ) → V A (suc m ) 1 Disclaimer: I did not actually count all examples since 1990. 1 / 15
types ’ most popular example 1 V : ( A : Set)( n : N ) → Set V A zero = ⊤ V A (suc n ) = A × V A n 1 Disclaimer: I did not actually count all examples since 1990. 1 / 15
types ’ most popular example 1 data V ( A : Set) : ( n : N ) → Set where nil : V A zero cons : ( m : N )( x : A )( xs : V A m ) → V A (suc m ) vs. V : ( A : Set)( n : N ) → Set V A zero = ⊤ V A (suc n ) = A × V A n 1 Disclaimer: I did not actually count all examples since 1990. 1 / 15
Presenting. . . A common representation of indexed datatypes and recursive types as case-splitting datatypes . An elaboration algorithm to automatically transform an indexed datatype into a case-splitting datatype. 2 / 15
Inductive types vs. recursive types Case-splitting datatypes Elaborating indexed datatypes
Inductive types vs. recursive types Case-splitting datatypes Elaborating indexed datatypes
Inductive type Recursive type 3 / 15
Inductive type I Recursive type • Intuitive notation data V ( A : Set) : ( n : N ) → Set where nil : V A zero cons : ( m : N )( x : A )( xs : V A m ) → V A (suc m ) 3 / 15
Inductive type I Recursive type I • Intuitive notation • Eta equality x : V A zero ⊢ x ≡ tt x : V A (suc m ) ⊢ x ≡ ( x .π 1 , x .π 2 ) 3 / 15
Inductive type II Recursive type I • Intuitive notation • Eta equality • Pattern matching tail : ( m : N )( xs : V A (suc m )) → V A m tail m (cons ⌊ m ⌋ x xs ) = xs 3 / 15
Inductive type II Recursive type II • Intuitive notation • Eta equality • Pattern matching • Forcing & detagging for free cons : ( m : Nat )( x : A )( xs : V A m ) → V A (suc m ) cons m x xs = ( x , xs ) 3 / 15
Inductive type III Recursive type II • Intuitive notation • Eta equality • Pattern matching • Forcing & detagging for free • Structural recursion 3 / 15
Inductive type III Recursive type III • Intuitive notation • Eta equality • Pattern matching • Forcing & detagging for free • Structural recursion • Large indices ≤ : N → N → Prop zero ≤ n = ⊤ (suc m ) ≤ zero = ⊥ (suc m ) ≤ (suc n ) = m ≤ n 3 / 15
Inductive type IIII Recursive type III • Intuitive notation • Eta equality • Pattern matching • Forcing & detagging for free • Structural recursion • Large indices • Non-indexed / non-stratified types N = ⊤ ⊎ N is not a valid definition! 3 / 15
Inductive type IIII Recursive type IIII • Intuitive notation • Eta equality • Pattern matching • Forcing & detagging for free • Structural recursion • Large indices • Non-indexed / non-stratified types • Non-positive types data U : Set where ⇒ : U → U → U | = : U → Set | =( t 1 ⇒ t 2 ) = | = t 1 → | = t 2 3 / 15
Inductive types vs. recursive types Case-splitting datatypes Elaborating indexed datatypes
data V A : N → Set where Indexed datatype . . . V A zero = . . . Recursive type V A (suc m ) = . . . 4 / 15
data V A : N → Set where Indexed datatype . . . Case-splitting V A n = case n { . . . } datatype V A zero = . . . Recursive type V A (suc m ) = . . . 4 / 15
data V A : N → Set where Indexed datatype . . . ⇑ Case-splitting V A n = case n { . . . } datatype ⇓ V A zero = . . . Recursive type V A (suc m ) = . . . 4 / 15
data V A : N → Set where Indexed datatype . . . ⇑ ⇓ Case-splitting V A n = case n { . . . } datatype ⇓ V A zero = . . . Recursive type V A (suc m ) = . . . 4 / 15
General syntax for case-splitting datatypes Q ::= c 1 ∆ 1 | . . . | c k ∆ k c 1 ˆ ∆ 1 �→ τ 1 Q 1 . . | case x . c n ˆ ∆ n �→ τ n Q n 5 / 15
Case tree for V A n zero �→ nil case n suc m �→ cons ( x : A )( xs : V A m ) 6 / 15
Case tree for m ≤ n zero �→ lz suc m ′ �→ case n case m zero �→ suc n ′ �→ ls ( p : m ′ ≤ n ′ ) 7 / 15
From case tree to a datatype: Ignore case splits; gather all constructors in a flat list. From case tree to a recursive definition: Translate case splits with tools from ‘eliminating dependent pattern matching’. 8 / 15
Inductive types vs. recursive types Case-splitting datatypes Elaborating indexed datatypes
Problem: We don’t want to write case trees, we want to write datatypes! 9 / 15
Problem: We don’t want to write case trees, we want to write datatypes! Solution: Elaborate datatypes to case trees automatically. 9 / 15
State of elaborating a datatype c 1 ∆ 1 [Φ 1 ] . . ∆ ⊢ . c k ∆ k [Φ k ] • ∆ is ‘outer’ telescope of datatype indices • c 1 , . . . , c k are the constructor names • ∆ i is ‘inner’ telescope of arguments of c i • Φ i is a set of constraints { v j / ? p j } 10 / 15
Initial elaboration state ( A : Set)( n : N ) ⊢ [zero / ? n ] { } nil cons ( m : N )( x : A )( xs : V A m ) [suc m / ? n ] 11 / 15
Elaboration step: case split on index ( A : Set)( n : N ) ⊢ [zero / ? n ] { } nil cons ( m : N )( x : A )( xs : V A m ) [suc m / ? n ] ⇓ nil [zero / ? zero] { } ( A : Set) ⊢ ( A : Set)( n ′ : N ) ⊢ cons ( m : N )( x : A )( xs : V A m ) [suc m / ? suc n ′ ] { } 11 / 15
Elaboration step: solve constraint ( A : Set)( n : N ) ⊢ cons ( m : N )( x : A )( xs : V A m ) [suc m / ? suc n ′ ] { } ⇓ { } ( A : Set)( n : N ) ⊢ cons ( x : A )( xs : V A n ′ ) 12 / 15
Elaboration step: finish splitting ( A : Set)( n : N ) ⊢ cons ( m : N )( x : A )( xs : V A m ) [suc m / ? suc n ′ ] { } ⇓ { } ( A : Set)( n : N ) ⊢ cons ( x : A )( xs : V A n ′ ) ⇓ cons ( x : A )( xs : V A n ′ ) 12 / 15
Elaboration step: introduce equality proof image ( x : A ) [ f x / ? y ] { } ( A B : Set)( f : A → B )( y : B ) ⊢ ⇓ { } ( A B : Set)( f : A → B )( y : B ) ⊢ image ( x : A ) ( e : f x ≡ B y ) 13 / 15
Ongoing & future work • Implement translation in Coq (WIP) • Generate constructors & eliminator • Generate case trees for Agda datatypes • User syntax to control splitting? 14 / 15
Conclusion Datatypes have long been denied features of record types such as η -equality. 15 / 15
Conclusion Datatypes have long been denied features of record types such as η -equality. We can automatically transform a datatype into an equivalent definition with η -laws. 15 / 15
Conclusion Datatypes have long been denied features of record types such as η -equality. We can automatically transform a datatype into an equivalent definition with η -laws. Now you can both have the cake and eat it: vectors are records, too! 15 / 15
Recommend
More recommend