slides

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
Total (Co)Programming
and Engineering Chalmers
with Guarded
University
Recursion
of Technology, Gothenburg,
TYPES 2015
Sweden [1ex]
1 / 26
)
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
Total (Co)Programming
and Engineering Chalmers
with Guarded
University
Recursion
of Technology, Gothenburg,
TYPES 2015
Sweden [1ex]
2 / 26
)
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
Total (Co)Programming
and Engineering Chalmers
with Guarded
University
Recursion
of Technology, Gothenburg,
TYPES 2015
Sweden [1ex]
3 / 26
)
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 0 . λ xs . ghead xs, map 0 ~ gtail xs)
Andrea Vezzosi ( Department of Computer Science
Total (Co)Programming
and Engineering Chalmers
with Guarded
University
Recursion
of Technology, Gothenburg,
TYPES 2015
Sweden [1ex]
4 / 26
)
Recursion Example?
gList A ∼
= > + A × gList A
all : (A → Bool ) → gList A → Bool
all p = fix (λ (all 0 : . (gList A → Bool )) . λ xs .
case xs of
[]
→ True
(x :: xs) → p x ∧ ?
We need a way to call all 0 with xs as argument and obtain Bool .
Andrea Vezzosi ( Department of Computer Science
Total (Co)Programming
and Engineering Chalmers
with Guarded
University
Recursion
of Technology, Gothenburg,
TYPES 2015
Sweden [1ex]
5 / 26
)
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 0 : . (gList A → Bool )) . λ xs .
case xs of
[]
→ True
(x :: xs) → p x ∧ extract (all 0 ? xs)
Andrea Vezzosi ( Department of Computer Science
Total (Co)Programming
and Engineering Chalmers
with Guarded
University
Recursion
of Technology, Gothenburg,
TYPES 2015
Sweden [1ex]
6 / 26
)
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
Total (Co)Programming
and Engineering Chalmers
with Guarded
University
Recursion
of Technology, Gothenburg,
TYPES 2015
Sweden [1ex]
7 / 26
)
Semantics
The standard model for Guarded Recursion is the topos of trees
i.e. functors ω op → Set
A : N → Set
A (n 6 m) : A m → A n
(. A) 0
= >
(. A) (suc n) = A n
next 0
= !
next suc n = A (n 6 suc n)
next uses the functoriality of A
Andrea Vezzosi ( Department of Computer Science
Total (Co)Programming
and Engineering Chalmers
with Guarded
University
Recursion
of Technology, Gothenburg,
TYPES 2015
Sweden [1ex]
8 / 26
)
Alternative Semantics: Relators
A : N → Set
A (n 6 m) : A m → A n → Set
A (n 6 n) ∼
= =A n
Any functor A : ω op → Set is also a relator:
A (n 6 m) an am = an =A n A (n 6 m) am
Andrea Vezzosi ( Department of Computer Science
Total (Co)Programming
and Engineering Chalmers
with Guarded
University
Recursion
of Technology, Gothenburg,
TYPES 2015
Sweden [1ex]
9 / 26
)
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
Total (Co)Programming
and Engineering Chalmers
with Guarded
University
Recursion
of Technology, Gothenburg,
TYPES 2015
Sweden 10
[1ex]
/ 26
)
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 0 s . case f i s of
→ []
inl
inr (a, (j , s 0 )) → a :: unfold 0 j s 0
Andrea Vezzosi ( Department of Computer Science
Total (Co)Programming
and Engineering Chalmers
with Guarded
University
Recursion
of Technology, Gothenburg,
TYPES 2015
Sweden 11
[1ex]
/ 26
)
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
Total (Co)Programming
and Engineering Chalmers
with Guarded
University
Recursion
of Technology, Gothenburg,
TYPES 2015
Sweden 12
[1ex]
/ 26
)
Coinductive Types with .
gStr κ A ∼
= A × .κ gStr κ A
∼ ∀ κ . gStr κ A
Str A =
force : (∀ κ . .κ A) ∼
= (∀ κ . A)
tail : Str A → Str A
tail xs = force (λ κ . gtail (xs κ))
Andrea Vezzosi ( Department of Computer Science
Total (Co)Programming
and Engineering Chalmers
with Guarded
University
Recursion
of Technology, Gothenburg,
TYPES 2015
Sweden 13
[1ex]
/ 26
)
Coinductive Types with ∀ j < i
gStr A i ∼
= A × ∀ j < i . gStr A j
Str A ∼
= ∀ i . gStr A i
force . :
force . f
guard . :
guard . f
(∀ i . ∀ j < i . A j ) → ∀ i . A i
i = f (suc i ) i
(∀ i . A i ) → ∀ i . ∀ j < i . A j
ij = f j
guard . (force . f ) i j = f (suc j ) j
Andrea Vezzosi ( Department of Computer Science
Total (Co)Programming
and Engineering Chalmers
with Guarded
University
Recursion
of Technology, Gothenburg,
TYPES 2015
Sweden 14
[1ex]
/ 26
)
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
Total (Co)Programming
and Engineering Chalmers
with Guarded
University
Recursion
of Technology, Gothenburg,
TYPES 2015
Sweden 15
[1ex]
/ 26
)
∃ 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
Total (Co)Programming
and Engineering Chalmers
with Guarded
University
Recursion
of Technology, Gothenburg,
TYPES 2015
Sweden 16
[1ex]
/ 26
)
∃ 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
Total (Co)Programming
and Engineering Chalmers
with Guarded
University
Recursion
of Technology, Gothenburg,
TYPES 2015
Sweden 17
[1ex]
/ 26
)
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
Total (Co)Programming
and Engineering Chalmers
with Guarded
University
Recursion
of Technology, Gothenburg,
TYPES 2015
Sweden 18
[1ex]
/ 26
)
Reflexive Graph Model of Martin L¨of Type Theory
Γ O : Set
Γ R : Γ O → Γ O → Set
Γ ref l : (γO : Γ O ) → Γ R γO γO
Andrea Vezzosi ( Department of Computer Science
Total (Co)Programming
and Engineering Chalmers
with Guarded
University
Recursion
of Technology, Gothenburg,
TYPES 2015
Sweden 19
[1ex]
/ 26
)
Time
TimeO
= N
TimeR i j = >
Any two time values are related.
Andrea Vezzosi ( Department of Computer Science
Total (Co)Programming
and Engineering Chalmers
with Guarded
University
Recursion
of Technology, Gothenburg,
TYPES 2015
Sweden 20
[1ex]
/ 26
)
Types depending on Time
i : Time ` A : Type
AO : N → Set
AR : (n m : N) → A n → A m → Set
Aref l : (n : N) → (a : A n) → AR n n a a
AR n n =? =AO
Andrea Vezzosi ( Department of Computer Science
Total (Co)Programming
and Engineering Chalmers
with Guarded
University
Recursion
of Technology, Gothenburg,
TYPES 2015
Sweden 21
[1ex]
/ 26
)
Universe of Small Discrete Reflexive Graphs
UO
= {(AO , AR ) | AO small set, AR ∼
= eqAo }
U R A B = {Rel | Rel small proof irrelevant relation
between AO and B O }
U ref l (AO , AR ) = AR
Γ `A: U
Γ ` El A : Type
(El A)R (Γ ref l γ) ∼
= =ElAO
Andrea Vezzosi ( Department of Computer Science
Total (Co)Programming
and Engineering Chalmers
with Guarded
University
Recursion
of Technology, Gothenburg,
TYPES 2015
Sweden 22
[1ex]
/ 26
)
Time dependency for discrete reflexive graphs
Given A such that i ∈
/ fv A
i : Time ` t : El A
t O : N → (El A)O
t R : (n m : N) → t O n =ElAO t O m
J ∀ i . ∀ j < i . El (A j ) K ∼
= J ∀ i . El (A i ) K
Andrea Vezzosi ( Department of Computer Science
Total (Co)Programming
and Engineering Chalmers
with Guarded
University
Recursion
of Technology, Gothenburg,
TYPES 2015
Sweden 23
[1ex]
/ 26
)
Discretization
Given any small reflxive graph A
R
we can form its free discrete reflexive graph A : U
R
(El (R A))O = AO / symmetric transitive closure of AR
(El ( A))R ∼
= =(R A)O
Andrea Vezzosi ( Department of Computer Science
Total (Co)Programming
and Engineering Chalmers
with Guarded
University
Recursion
of Technology, Gothenburg,
TYPES 2015
Sweden 24
[1ex]
/ 26
)
Discretization, Universal property
R
(El ( A) → El B ) ∼
= (A → El B )
Z
Z
P : El ( A) → U
f : (a : A) → P ( a)
Z
elim f : (x : El ( A)) → El (P x )
∃ i. A i =
R
(Σ (i : Time) . A i )
Andrea Vezzosi ( Department of Computer Science
Total (Co)Programming
and Engineering Chalmers
with Guarded
University
Recursion
of Technology, Gothenburg,
TYPES 2015
Sweden 25
[1ex]
/ 26
)
Future work
How to internalize the parametricity properties of ∀ i and ∃ i ?
Very interested in the talks about parametricity in the following days!
R
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
Total (Co)Programming
and Engineering Chalmers
with Guarded
University
Recursion
of Technology, Gothenburg,
TYPES 2015
Sweden 26
[1ex]
/ 26
)