total co programming with guarded recursion
play

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


  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

  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

  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

  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

  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

  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

  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

  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 next 0 = ! next suc 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

  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 ) a n a m = a n = A n A ( n � m ) a m 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

  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

  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

  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

  13. 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 and Engineering Chalmers University of Technology, Gothenburg, Sweden [1ex] ) Total (Co)Programming with Guarded Recursion TYPES 2015 13 / 26

  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 = f ( suc i ) i force ⊲ f 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

  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

  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

  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

  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

  19. Reflexive Graph Model of Martin L¨ of 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

  20. Time Time O = N Time R 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

  21. Types depending on Time i : Time ⊢ A : Type A O : N → Set A R : ( n m : N ) → A n → A m → Set A refl : ( n : N ) → ( a : A n ) → A R n n a a A R n n =? = 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 21 / 26

  22. Universe of Small Discrete Reflexive Graphs = { ( A O , A R ) | A O small set , A R ∼ = eqAo } U O U R A B = { Rel | Rel small proof irrelevant relation between A O and B O } U refl ( A O , A R ) = A R Γ ⊢ A : U Γ ⊢ El A : Type ( El A ) R ( Γ refl γ ) ∼ = = El 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 22 / 26

Recommend


More recommend