Vectors are records, too! Jesper Cockx 1 etan Gilbert 2 Ga Nicolas - - PowerPoint PPT Presentation

vectors are records too
SMART_READER_LITE
LIVE PREVIEW

Vectors are records, too! Jesper Cockx 1 etan Gilbert 2 Ga Nicolas - - PowerPoint PPT Presentation

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


slide-1
SLIDE 1

Vectors are records, too!

Jesper Cockx1 Ga¨ etan Gilbert2 Nicolas Tabareau2 Matthieu Sozeau2

1 Gothenburg University, Sweden 2 INRIA, France

21 June 2018

slide-2
SLIDE 2

types’ most popular example1

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)

1Disclaimer: I did not actually count all examples since 1990. 1 / 15

slide-3
SLIDE 3

types’ most popular example1

V : (A : Set)(n : N) → Set V A zero = ⊤ V A (suc n) = A × V A n

1Disclaimer: I did not actually count all examples since 1990. 1 / 15

slide-4
SLIDE 4

types’ most popular example1

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

1Disclaimer: I did not actually count all examples since 1990. 1 / 15

slide-5
SLIDE 5
  • 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

slide-6
SLIDE 6

Inductive types vs. recursive types Case-splitting datatypes Elaborating indexed datatypes

slide-7
SLIDE 7

Inductive types vs. recursive types Case-splitting datatypes Elaborating indexed datatypes

slide-8
SLIDE 8

Inductive type Recursive type

3 / 15

slide-9
SLIDE 9

Inductive type I

  • Intuitive notation

Recursive type

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

slide-10
SLIDE 10

Inductive type I

  • Intuitive notation

Recursive type I

  • Eta equality

x : V A zero ⊢ x ≡ tt x : V A (suc m) ⊢ x ≡ (x .π1, x .π2)

3 / 15

slide-11
SLIDE 11

Inductive type II

  • Intuitive notation
  • Pattern matching

Recursive type I

  • Eta equality

tail : (m : N)(xs : V A (suc m)) → V A m tail m (cons ⌊m⌋ x xs) = xs

3 / 15

slide-12
SLIDE 12

Inductive type II

  • Intuitive notation
  • Pattern matching

Recursive type II

  • Eta equality
  • 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

slide-13
SLIDE 13

Inductive type III

  • Intuitive notation
  • Pattern matching
  • Structural recursion

Recursive type II

  • Eta equality
  • Forcing &

detagging for free

3 / 15

slide-14
SLIDE 14

Inductive type III

  • Intuitive notation
  • Pattern matching
  • Structural recursion

Recursive type III

  • Eta equality
  • Forcing &

detagging for free

  • Large indices

≤ : N → N → Prop zero ≤ n = ⊤ (suc m) ≤ zero = ⊥ (suc m) ≤ (suc n) = m ≤ n

3 / 15

slide-15
SLIDE 15

Inductive type IIII

  • Intuitive notation
  • Pattern matching
  • Structural recursion
  • Non-indexed /

non-stratified types

Recursive type III

  • Eta equality
  • Forcing &

detagging for free

  • Large indices

N = ⊤ ⊎ N is not a valid definition!

3 / 15

slide-16
SLIDE 16

Inductive type IIII

  • Intuitive notation
  • Pattern matching
  • Structural recursion
  • Non-indexed /

non-stratified types

Recursive type IIII

  • Eta equality
  • Forcing &

detagging for free

  • Large indices
  • Non-positive types

data U : Set where ⇒ : U → U → U | = : U → Set | =(t1 ⇒ t2) = | = t1 → | = t2

3 / 15

slide-17
SLIDE 17

Inductive types vs. recursive types Case-splitting datatypes Elaborating indexed datatypes

slide-18
SLIDE 18

Indexed datatype data V A : N → Set where . . . Recursive type V A zero = . . . V A (suc m) = . . .

4 / 15

slide-19
SLIDE 19

Indexed datatype data V A : N → Set where . . . Case-splitting datatype V A n = casen {. . .} Recursive type V A zero = . . . V A (suc m) = . . .

4 / 15

slide-20
SLIDE 20

Indexed datatype data V A : N → Set where . . . ⇑ Case-splitting datatype V A n = casen {. . .} ⇓ Recursive type V A zero = . . . V A (suc m) = . . .

4 / 15

slide-21
SLIDE 21

Indexed datatype data V A : N → Set where . . . ⇑ ⇓ Case-splitting datatype V A n = casen {. . .} ⇓ Recursive type V A zero = . . . V A (suc m) = . . .

4 / 15

slide-22
SLIDE 22

General syntax for case-splitting datatypes

Q ::= c1 ∆1 | . . . | ck ∆k | casex

            

c1 ˆ ∆1 →τ1 Q1 . . . cn ˆ ∆n →τn Qn

            

5 / 15

slide-23
SLIDE 23

Case tree for V A n

casen

    

zero → nil suc m → cons (x : A)(xs : V A m)

    

6 / 15

slide-24
SLIDE 24

Case tree for m ≤ n

casem

                  

zero → lz suc m′ → casen

    

zero → suc n′ → ls (p : m′ ≤ n′)

                       

7 / 15

slide-25
SLIDE 25

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

slide-26
SLIDE 26

Inductive types vs. recursive types Case-splitting datatypes Elaborating indexed datatypes

slide-27
SLIDE 27

Problem: We don’t want to write case trees, we want to write datatypes!

9 / 15

slide-28
SLIDE 28

Problem: We don’t want to write case trees, we want to write datatypes! Solution: Elaborate datatypes to case trees automatically.

9 / 15

slide-29
SLIDE 29

State of elaborating a datatype

∆ ⊢

              

c1 ∆1 [Φ1] . . . ck ∆k [Φk]

              

  • ∆ is ‘outer’ telescope of datatype indices
  • c1, . . . , ck are the constructor names
  • ∆i is ‘inner’ telescope of arguments of ci
  • Φi is a set of constraints {vj /? pj}

10 / 15

slide-30
SLIDE 30

Initial elaboration state

(A : Set)(n : N) ⊢

{

nil [zero /? n] cons (m : N)(x : A)(xs : V A m) [suc m /? n]

}

11 / 15

slide-31
SLIDE 31

Elaboration step: case split on index

(A : Set)(n : N) ⊢

{

nil [zero /? n] cons (m : N)(x : A)(xs : V A m) [suc m /? n]

}

(A : Set) ⊢

{

nil [zero /? zero]

}

(A : Set)(n′ : N) ⊢

{

cons (m : N)(x : A)(xs : V A m) [suc m /? suc n′]

}

11 / 15

slide-32
SLIDE 32

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

slide-33
SLIDE 33

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

slide-34
SLIDE 34

Elaboration step: introduce equality proof

(A B : Set)(f : A → B)(y : B) ⊢

{

image (x : A) [f x /? y]

}

(A B : Set)(f : A → B)(y : B) ⊢

{

image (x : A) (e : f x ≡B y)

}

13 / 15

slide-35
SLIDE 35

Ongoing & future work

  • Implement translation in Coq (WIP)
  • Generate constructors & eliminator
  • Generate case trees for Agda datatypes
  • User syntax to control splitting?

14 / 15

slide-36
SLIDE 36

Conclusion

Datatypes have long been denied features of record types such as η-equality.

15 / 15

slide-37
SLIDE 37

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

slide-38
SLIDE 38

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