Modularising inductive families Josh Ko & Jeremy Gibbons - - PowerPoint PPT Presentation

modularising inductive families
SMART_READER_LITE
LIVE PREVIEW

Modularising inductive families Josh Ko & Jeremy Gibbons - - PowerPoint PPT Presentation

Modularising inductive families Josh Ko & Jeremy Gibbons Department of Computer Science University of Oxford Workshop on Generic Programming 18 September 2011, Tokyo, Japan Internalism Externalism Internalism Constraints internalised


slide-1
SLIDE 1

Modularising inductive families

Josh Ko & Jeremy Gibbons

Department of Computer Science University of Oxford

Workshop on Generic Programming 18 September 2011, Tokyo, Japan

slide-2
SLIDE 2

Internalism Externalism

slide-3
SLIDE 3

Internalism

Constraints internalised in datatypes

data Vec (A : Set) : Nat Set where [] : Vec A 0 _∷_ : (x : A) {n : Nat} (xs : Vec A n) Vec A (suc n) Vec A 3 x ∷ y ∷ z ∷ [] :

slide-4
SLIDE 4

Internalist clarity

Constraints cleanly expressed and managed

zipWith3 : (f : A B C D) Vec A n Vec B n Vec C n Vec D n zipWith3 f [] [] [] = [] zipWith3 f (x ∷ xs) (y ∷ ys) (z ∷ zs) = f x y z ∷ zipWith3 f xs ys zs

slide-5
SLIDE 5

Internalist libraries

dreadful reusability/composability

insert : Nat List Nat List Nat vinsert : Nat Vec Nat n Vec Nat (suc n) sinsert : (x : Nat) SList b SList (b ⊓ x) data SList : Nat Set where nil : ∀ {b} SList b cons : (x : Nat) ∀ {b} b ≤ x (xs : SList x) SList b

slide-6
SLIDE 6

Externalism

Predicates imposed on existing datatypes

data Length : Nat List A Set where nil : Length 0 [] cons : ∀ {x n xs} Length n xs Length (suc n) (x ∷ xs) (xs : List Nat) × Length n xs

slide-7
SLIDE 7

Externalist composability

Easy to impose multiple constraints

(xs : List Nat) × Length n xs × Sorted b xs data Sorted : Nat List Nat Set where nil : ∀ {b} Sorted b [] cons : ∀ {x b} b ≤ x ∀ {xs} Sorted x xs Sorted b (x ∷ xs)

slide-8
SLIDE 8

Externalist composability

Ideal for structuring libraries

insert : Nat List Nat List Nat insert-length : Length n xs Length (suc n) (insert x xs) insert-sorted : Sorted b xs Sorted (b ⊓ x) (insert x xs)

slide-9
SLIDE 9

Is it possible to import

externalist composability internalist libraries

into

?

Or: Can we get sorted vectors and insert on sorted vectors for free?

slide-10
SLIDE 10

Constraints Multiple constraints Internalism ?? ?? Externalism Predicates Pointwise conjunction

  • f predicates
slide-11
SLIDE 11

Constraints Multiple constraints Internalism ?? Externalism Predicates Pointwise conjunction

  • f predicates

Conor McBride Ornaments!

slide-12
SLIDE 12

Ornaments

Information added to a datatype to get a fancier one

data [] : _∷_ : (x : A) Set where (A : Set) : ) A A A List List List (xs : List

slide-13
SLIDE 13

Ornaments

Information added to a datatype to get a fancier one

data [] : _∷_ : (x : A) Set where (A : Set) : ) List A A A List List (xs : List Nat {n : Nat} n (suc n)

slide-14
SLIDE 14

Ornaments

Information added to a datatype to get a fancier one

data [] : _∷_ : (x : A) Set where (A : Set) : ) A A A (xs : Nat {n : Nat} n (suc n)

  • Vec

Vec Vec Vec

slide-15
SLIDE 15

Constraints Multiple constraints Externalism Internalism Ornaments Predicates Pointwise conjunction

  • f predicates

?? induce

slide-16
SLIDE 16

Length VecO A

induces

:

Ornaments induce predicates

Nat List A Set List A Vec A

basic type fancier type predicate on basic type

slide-17
SLIDE 17

n ≅ (xs : ) × n xs List A Vec A Length SList b ≅ (xs : List Nat) × Sorted b xs

Ornaments induce predicates

and corresponding isomorphisms basic type fancier type induced predicate internalist externalist

slide-18
SLIDE 18

Function upgrade

with the help of the isomorphisms

Vec Nat n ≅ (xs : List Nat) × Length n xs insert : Nat List Nat List Nat insert-length : Length n xs Length (suc n) (insert x xs) ≅

xs : List Nat l : Length n xs insert x xs : List Nat insert-length l : Length (suc n) (insert x xs)

≅ ↦ ↦ vinsert : Nat

  • Vec Nat n

Vec Nat (suc n)

slide-19
SLIDE 19

Vec Nat n ≅ (xs : List Nat) × Length n xs

Function upgrade

with the help of the isomorphisms

insert : Nat List Nat List Nat insert-length : Length n xs Length (suc n) (insert x xs) vinsert : Nat Vec Nat n Vec Nat (suc n) insert-sorted : Sorted b xs Sorted (b ⊓ x) (insert x xs) sinsert : (x : Nat) SList b SList (b ⊓ x) SList b ≅ (xs : List Nat) × Sorted b xs

slide-20
SLIDE 20

data Vec Nat : Set where [] : Vec Nat _∷_ : Nat Vec Nat Vec Nat data SList : Set where nil : SList cons : (x : Nat) SList SList

Sorted vectors

∀ {b} b ∀ {b} b ≤ x x b Nat Nat ∀ {n} n (suc n)

slide-21
SLIDE 21

data SVec : Set where nil : SVec cons : (x : Nat) SVec SVec

Sorted vectors

= sorted lists + vectors!

∀ {b} b ∀ {b} b ≤ x x b Nat Nat ∀ {n} n (suc n)

slide-22
SLIDE 22

Constraints Multiple constraints Internalism Ornaments Externalism Predicates Pointwise conjunction

  • f predicates

Ornament fusion corresponds to induce

slide-23
SLIDE 23

Ornament fusion

corresponds to conjunction of induced predicates

SLen b n xs ≅ Sorted b xs × Length n xs List Nat SList b Vec Nat n SVec b n Sorted b Length n SLen b n

slide-24
SLIDE 24

Ornament fusion

corresponds to conjunction of induced predicates

≅ (xs : List Nat) × SLen b n xs ≅ SVec b n (xs : List Nat) × Sorted b xs × Length n xs

slide-25
SLIDE 25

Function upgrade

with the help of the isomorphisms

SVec b n ≅

xs : List Nat s : Sorted b xs insert x xs : List Nat insert-sorted s : Sorted (b ⊓ x) (insert x xs)

≅ ↦ ↦ svinsert : (x : Nat) SVec b n SVec (b ⊓ x) (suc n)

l : Length n xs insert-length l : Length (suc n) (insert x xs)

↦ ≅ (xs : List Nat) × Sorted b xs × Length n xs

slide-26
SLIDE 26

Modular library structure

data List ..., insert, ...

  • rn Vec inducing Length

..., insert-length, ...

  • rn SList inducing Sorted

..., insert-sorted, ...

slide-27
SLIDE 27

Constraints Multiple constraints Internalism Ornaments Ornament fusion Externalism Predicates Pointwise conjunction

  • f predicates

corresponds to induce

slide-28
SLIDE 28

Thanks!

slide-29
SLIDE 29

Descriptions

A “universe” datatype containing codes for datatypes

Vec : Set Desc Nat Vec A = σ Bool (false↦ say zero true↦ σ A λ x σ Nat λ n ask n * say (suc n))

μ : Desc I (I Set)

  • - μ (Vec A) : Nat Set
slide-30
SLIDE 30

Ornaments

A richer universe of relative descriptions

VecO : Set Orn Nat ... VecO A = σ Bool (false↦ say (ok zero) true↦ σ A λ x Δ Nat λ n ask (ok n) * say (ok (suc n)))

⌊_⌋ : Orn J ... Desc J

  • - ⌊ VecO A ⌋ ≈ Vec A