Total (Co)Programming with Guarded Recursion Andrea Vezzosi - - PowerPoint PPT Presentation

total co programming with guarded recursion
SMART_READER_LITE
LIVE PREVIEW

Total (Co)Programming with Guarded Recursion Andrea Vezzosi - - PowerPoint PPT Presentation

Total (Co)Programming with Guarded Recursion Andrea Vezzosi Department of Computer Science and Engineering Chalmers University of Technology, Gothenburg, Sweden Types for Proofs and Programs Annual Meeting 2015 Tallinn, Estonia 18 May 2015


slide-1
SLIDE 1

Total (Co)Programming with Guarded Recursion

Andrea Vezzosi

Department of Computer Science and Engineering Chalmers University of Technology, Gothenburg, Sweden

Types for Proofs and Programs Annual Meeting 2015 Tallinn, Estonia 18 May 2015

Andrea Vezzosi ( Department of Computer Science and Engineering Chalmers University of Technology, Gothenburg, Sweden [1ex] ) Total (Co)Programming with Guarded Recursion TYPES 2015 1 / 26

slide-2
SLIDE 2

Guarded Recursion

Guarded coinductive types Coinductive types Guarded fixed point operator as only source of recursion Recursive types as fixed points on the universe What about Induction?

Andrea Vezzosi ( Department of Computer Science and Engineering Chalmers University of Technology, Gothenburg, Sweden [1ex] ) Total (Co)Programming with Guarded Recursion TYPES 2015 2 / 26

slide-3
SLIDE 3

Main Combinators

⊲ A, ”later A”, modality as an applicative functor: next : A → ⊲ A ⊛ : ⊲ (A → B) → ⊲ A → ⊲ B Guarded fixpoint combinator: fix : (⊲ A → A) → A fix f = f (next (fix f ))

Andrea Vezzosi ( Department of Computer Science and Engineering Chalmers University of Technology, Gothenburg, Sweden [1ex] ) Total (Co)Programming with Guarded Recursion TYPES 2015 3 / 26

slide-4
SLIDE 4

Corecursion Example

gStr A ∼ = A × ⊲ gStr A ghead : gStr A → A ghead = fst gtail : gStr A → ⊲ gStr A gtail = snd map : (A → B) → gStr A → gStr B map f = fix (λ map′ . λ xs . ghead xs, map′ ⊛ gtail xs)

Andrea Vezzosi ( Department of Computer Science and Engineering Chalmers University of Technology, Gothenburg, Sweden [1ex] ) Total (Co)Programming with Guarded Recursion TYPES 2015 4 / 26

slide-5
SLIDE 5

Recursion Example?

gList A ∼ = ⊤ + A × gList A all : (A → Bool) → gList A → Bool all p = fix (λ (all′ : ⊲ (gList A → Bool)). λ xs . case xs of [ ] → True (x :: xs) → p x ∧ ? We need a way to call all′ with xs as argument and obtain Bool.

Andrea Vezzosi ( Department of Computer Science and Engineering Chalmers University of Technology, Gothenburg, Sweden [1ex] ) Total (Co)Programming with Guarded Recursion TYPES 2015 5 / 26

slide-6
SLIDE 6

Recursion Example, take 2, with diamonds

gList A ∼ = ⊤ + A × ♦ gList A extract : ♦ Bool → Bool ⋆ : ⊲ (A → B) → ♦ A → ♦ B all : (A → Bool) → gList A → Bool all p = fix (λ (all′ : ⊲ (gList A → Bool)). λ xs . case xs of [ ] → True (x :: xs) → p x ∧ extract (all′ ⋆ xs)

Andrea Vezzosi ( Department of Computer Science and Engineering Chalmers University of Technology, Gothenburg, Sweden [1ex] ) Total (Co)Programming with Guarded Recursion TYPES 2015 6 / 26

slide-7
SLIDE 7

Problem: we lose next

For ♦ A we cannot have next, e.g.: next : ♦ ⊤ → ⊲ (♦ ⊤) ♦ ⊤ → ⊲ (♦ ⊤) means ”if there is time left now, there will be time left later too”

Andrea Vezzosi ( Department of Computer Science and Engineering Chalmers University of Technology, Gothenburg, Sweden [1ex] ) Total (Co)Programming with Guarded Recursion TYPES 2015 7 / 26

slide-8
SLIDE 8

Semantics

The standard model for Guarded Recursion is the topos of trees i.e. functors ωop → Set A : N → Set A (n m) : A m → A n (⊲ A) 0 = ⊤ (⊲ A) (suc n) = A n next0 = ! nextsuc n = A (n suc n) next uses the functoriality of A

Andrea Vezzosi ( Department of Computer Science and Engineering Chalmers University of Technology, Gothenburg, Sweden [1ex] ) Total (Co)Programming with Guarded Recursion TYPES 2015 8 / 26

slide-9
SLIDE 9

Alternative Semantics: Relators

A : N → Set A (n m) : A m → A n → Set A (n n) ∼ = =A n Any functor A : ωop → Set is also a relator: A (n m) an am = an =A n A (n m) am

Andrea Vezzosi ( Department of Computer Science and Engineering Chalmers University of Technology, Gothenburg, Sweden [1ex] ) Total (Co)Programming with Guarded Recursion TYPES 2015 9 / 26

slide-10
SLIDE 10

ala Sized Types

⊲, ♦ : (Time → Set) → (Time → Set) ⊲ A i = ∀ j < i. A j ♦ A i = ∃ j < i. A j ⋆ : ∀ i. (∀ j < i. A j → B j) → (∃ j < i. A j) → ∃ j < i. B j f ⋆ (j, a) = (j, f j a)

Andrea Vezzosi ( Department of Computer Science and Engineering Chalmers University of Technology, Gothenburg, Sweden [1ex] ) Total (Co)Programming with Guarded Recursion TYPES 2015 10 / 26

slide-11
SLIDE 11

ala Sized Types (contd.)

fix : (∀ i. (∀ j < i. A j) → A i) → ∀ i. A i unfold : (∀ i . S i → ⊤ + (A × ∃ j < i. S j)) → ∀ i . S i → List A unfold f = fix λ i unfold′ s . case f i s of inl → [ ] inr (a, (j, s′)) → a :: unfold′ j s′

Andrea Vezzosi ( Department of Computer Science and Engineering Chalmers University of Technology, Gothenburg, Sweden [1ex] ) Total (Co)Programming with Guarded Recursion TYPES 2015 11 / 26

slide-12
SLIDE 12

Recursive Types through fixed points

ˆ ⊲ : ⊲ U → U gStr A = fix λ X . A × ˆ ⊲ X gStr A = fix λ i (X : ∀ j < i. U ). A × ∀ j < i. X j

Andrea Vezzosi ( Department of Computer Science and Engineering Chalmers University of Technology, Gothenburg, Sweden [1ex] ) Total (Co)Programming with Guarded Recursion TYPES 2015 12 / 26

slide-13
SLIDE 13

Coinductive Types with ⊲

gStrκ A ∼ = A × ⊲κ gStrκ A Str A ∼ = ∀ κ. gStrκ A force : (∀ κ. ⊲κ A) ∼ = (∀ κ. A) tail : Str A → Str A tail xs = force (λ κ. gtail (xs κ))

Andrea Vezzosi ( Department of Computer Science and Engineering Chalmers University of Technology, Gothenburg, Sweden [1ex] ) Total (Co)Programming with Guarded Recursion TYPES 2015 13 / 26

slide-14
SLIDE 14

Coinductive Types with ∀ j < i

gStr A i ∼ = A × ∀ j < i. gStr A j Str A ∼ = ∀ i. gStr A i force⊲ : (∀ i. ∀ j < i. A j) → ∀ i. A i force⊲ f i = f (suc i) i guard⊲ : (∀ i. A i) → ∀ i. ∀ j < i. A j guard⊲ f i j = f j guard⊲ (force⊲ f ) i j = f (suc j) j

Andrea Vezzosi ( Department of Computer Science and Engineering Chalmers University of Technology, Gothenburg, Sweden [1ex] ) Total (Co)Programming with Guarded Recursion TYPES 2015 14 / 26

slide-15
SLIDE 15

Inductive Types with ∃ j < i

gNat i ∼ = ⊤ + ∃ j < i. A j Nat ∼ = ∃ i. gNat i force♦ : (∃ i. ∃ j < i. A j) → ∃ i. A i force♦ (i, j, a) = (j, a) guard♦ : (∀ i. A i) → ∀ i. ∀ j < i. A j guard♦ (j, a) = (suc j, j, a) guard♦ (force♦ (i, j, a)) = suc j, j, a

Andrea Vezzosi ( Department of Computer Science and Engineering Chalmers University of Technology, Gothenburg, Sweden [1ex] ) Total (Co)Programming with Guarded Recursion TYPES 2015 15 / 26

slide-16
SLIDE 16

∃ i as a weak existential

gNat i ∼ = ⊤ + ∃ j < i. A j Nat ∼ = ∃ i. gNat i Want all ”zeros” to be equal: (i, inl tt) = (j, inl tt) We cannot project times out: fst : (∃ i. A i) → Time fst (i, a) = i i = fst (i, inl tt) = fst (j, inl tt) = j

Andrea Vezzosi ( Department of Computer Science and Engineering Chalmers University of Technology, Gothenburg, Sweden [1ex] ) Total (Co)Programming with Guarded Recursion TYPES 2015 16 / 26

slide-17
SLIDE 17

∃ i as a weak existential

P : (∃ i. A i) → U f : (∀ i. (a : A i) → P (i, a)) uncurry f : (x : ∃ i . A i) → P x where U is a type theoretic universe such that Time / ∈ U

Andrea Vezzosi ( Department of Computer Science and Engineering Chalmers University of Technology, Gothenburg, Sweden [1ex] ) Total (Co)Programming with Guarded Recursion TYPES 2015 17 / 26

slide-18
SLIDE 18

Summary

Ordered type Time : Type which supports well-founded induction A universe U : Type such that Time / ∈ U Parametric time quantifiers ∀ i. A i and ∃ i. A i

Andrea Vezzosi ( Department of Computer Science and Engineering Chalmers University of Technology, Gothenburg, Sweden [1ex] ) Total (Co)Programming with Guarded Recursion TYPES 2015 18 / 26

slide-19
SLIDE 19

Reflexive Graph Model of Martin L¨

  • f Type Theory

ΓO : Set ΓR : ΓO → ΓO → Set Γrefl : (γO : ΓO) → ΓR γO γO

Andrea Vezzosi ( Department of Computer Science and Engineering Chalmers University of Technology, Gothenburg, Sweden [1ex] ) Total (Co)Programming with Guarded Recursion TYPES 2015 19 / 26

slide-20
SLIDE 20

Time

TimeO = N TimeR i j = ⊤ Any two time values are related.

Andrea Vezzosi ( Department of Computer Science and Engineering Chalmers University of Technology, Gothenburg, Sweden [1ex] ) Total (Co)Programming with Guarded Recursion TYPES 2015 20 / 26

slide-21
SLIDE 21

Types depending on Time

i : Time ⊢ A : Type AO : N → Set AR : (n m : N) → A n → A m → Set Arefl : (n : N) → (a : A n) → AR n n a a AR n n =? =AO

Andrea Vezzosi ( Department of Computer Science and Engineering Chalmers University of Technology, Gothenburg, Sweden [1ex] ) Total (Co)Programming with Guarded Recursion TYPES 2015 21 / 26

slide-22
SLIDE 22

Universe of Small Discrete Reflexive Graphs

UO = {(AO, AR) | AO small set, AR ∼ = eqAo} UR A B = {Rel | Rel small proof irrelevant relation between AO and BO } Urefl (AO, AR) = AR Γ ⊢ A : U Γ ⊢ El A : Type (El A)R (Γrefl γ) ∼ = =ElAO

Andrea Vezzosi ( Department of Computer Science and Engineering Chalmers University of Technology, Gothenburg, Sweden [1ex] ) Total (Co)Programming with Guarded Recursion TYPES 2015 22 / 26

slide-23
SLIDE 23

Time dependency for discrete reflexive graphs

Given A such that i / ∈ fv A i : Time ⊢ t : El A tO : N → (El A)O tR : (n m : N) → tO n =ElAO tO m ∀ i . ∀ j < i. El (A j) ∼ = ∀ i . El (A i)

Andrea Vezzosi ( Department of Computer Science and Engineering Chalmers University of Technology, Gothenburg, Sweden [1ex] ) Total (Co)Programming with Guarded Recursion TYPES 2015 23 / 26

slide-24
SLIDE 24

Discretization

Given any small reflxive graph A we can form its free discrete reflexive graph

  • A : U

(El (

  • A))O = AO / symmetric transitive closure of AR

(El (

  • A))R ∼

= =(

  • A)

O Andrea Vezzosi ( Department of Computer Science and Engineering Chalmers University of Technology, Gothenburg, Sweden [1ex] ) Total (Co)Programming with Guarded Recursion TYPES 2015 24 / 26

slide-25
SLIDE 25

Discretization, Universal property

(El (

  • A) → El B) ∼

= (A → El B) P : El (

  • A) → U

f : (a : A) → P (

  • a)

elim f : (x : El (

  • A)) → El (P x)

∃ i . A i =

  • (Σ (i : Time). A i)

Andrea Vezzosi ( Department of Computer Science and Engineering Chalmers University of Technology, Gothenburg, Sweden [1ex] ) Total (Co)Programming with Guarded Recursion TYPES 2015 25 / 26

slide-26
SLIDE 26

Future work

How to internalize the parametricity properties of ∀ i and ∃ i ? Very interested in the talks about parametricity in the following days! Cohesive Homotopy Type Theory has something like discretization

  • How to preserve strong normalization?

fix f i = f i (fix f ) = f i (λ j. fix f j) = f i (λ j. f j (fix f )) = ...

Andrea Vezzosi ( Department of Computer Science and Engineering Chalmers University of Technology, Gothenburg, Sweden [1ex] ) Total (Co)Programming with Guarded Recursion TYPES 2015 26 / 26