rewrite semantics for guarded recursion in type theory
play

Rewrite Semantics for Guarded Recursion in Type Theory Patrick Bahr - PowerPoint PPT Presentation

Rewrite Semantics for Guarded Recursion in Type Theory Patrick Bahr IT University of Copenhagen joint work with Rasmus Mgelberg and Hans Bugge Grathwohl Overview Guarded Recursive Types Dependent Types Reduction Semantics 2 / 21 Guarded


  1. Rewrite Semantics for Guarded Recursion in Type Theory Patrick Bahr IT University of Copenhagen joint work with Rasmus Møgelberg and Hans Bugge Grathwohl

  2. Overview Guarded Recursive Types Dependent Types Reduction Semantics 2 / 21

  3. Guarded Recursive Types H. Nakano. A modality for recursion. In: LICS, 2000 3 / 21

  4. Guarded Recursion ◮ type modality ⊲ (pronounced “later”) ◮ ⊲ is an applicative functor next : A → ⊲ A ⊛ : ⊲ ( A → B ) → ⊲ A → ⊲ B ◮ fixed-point operator fix: ( ⊲ A → A ) → A ◮ guarded recursive types: µ X . A 4 / 21

  5. Example Str = µ X . Nat × ⊲ X cons: Nat → ⊲ Str → Str cons = λ x .λ y . � x , y � nats: Nat → Str nats = fix( λ f n . cons n ( f ⊛ (next( n + 1)))) inter: Str → ⊲ Str → Str inter = fix( λ f s t . cons ( π 1 s ) ( f ⊛ t ⊛ (next( π 2 s )))) foo: Str foo = fix( λ x . inter (nats 0) , x ) 5 / 21

  6. Motivation ◮ functional reactive programming ◮ productive coprogramming (clocks & clock quantification) ◮ solving recursive domain equations ( → synthetic domain theory) 6 / 21

  7. Dependent Types A. Bizjak, H. B. Grathwohl, R. Clouston, R. E. Møgelberg, and L. Birkedal. Guarded dependent type theory with coinductive types. In FoSSaCS, 2016. 7 / 21

  8. Combining Π and ⊲ Γ ⊢ s : Π x : A . B Γ ⊢ t : A Γ ⊢ s t : B [ t / x ] 8 / 21

  9. Combining Π and ⊲ Γ ⊢ s : Π x : A . B Γ ⊢ t : A Γ ⊢ s t : B [ t / x ] Γ ⊢ s : ⊲ (Π x : A . B ) Γ ⊢ t : ⊲ A Γ ⊢ s ⊛ t : ??? 8 / 21

  10. Combining Π and ⊲ Γ ⊢ s : Π x : A . B Γ ⊢ t : A Γ ⊢ s t : B [ t / x ] Γ ⊢ s : ⊲ (Π x : A . B ) Γ ⊢ t : ⊲ A Γ ⊢ s ⊛ t : ⊲ B [ t / x ] 8 / 21

  11. Combining Π and ⊲ Γ ⊢ s : Π x : A . B Γ ⊢ t : A Γ ⊢ s t : B [ t / x ] Γ ⊢ s : ⊲ (Π x : A . B ) Γ ⊢ t : ⊲ A Γ ⊢ s ⊛ t : ⊲ B [ t / x ] ◮ Problem: t : ⊲ A , but x : A 8 / 21

  12. Combining Π and ⊲ Γ ⊢ s : Π x : A . B Γ ⊢ t : A Γ ⊢ s t : B [ t / x ] Γ ⊢ s : ⊲ (Π x : A . B ) Γ ⊢ t : ⊲ A Γ ⊢ s ⊛ t : ⊲ B [ t / x ] ◮ Problem: t : ⊲ A , but x : A ◮ needed: getting rid of ⊲ in a controlled way 8 / 21

  13. Delayed Substitutions Instead of Γ ⊢ s : ⊲ (Π x : A . B ) Γ ⊢ t : ⊲ A Γ ⊢ s ⊛ t : ⊲ B [ t / x ] we have Γ ⊢ s : ⊲ (Π x : A . B ) Γ ⊢ t : ⊲ A Γ ⊢ s ⊛ t : ⊲ [ x ← t ] . B 9 / 21

  14. Delayed Substitutions Instead of Γ ⊢ s : ⊲ (Π x : A . B ) Γ ⊢ t : ⊲ A Γ ⊢ s ⊛ t : ⊲ B [ t / x ] we have Γ ⊢ s : ⊲ (Π x : A . B ) Γ ⊢ t : ⊲ A Γ ⊢ s ⊛ t : ⊲ [ x ← t ] . B In general ⊲ [ x 1 ← t 1 , . . . x n ← t n ] . A 9 / 21

  15. Delayed Substitutions Instead of Γ ⊢ s : ⊲ (Π x : A . B ) Γ ⊢ t : ⊲ A Γ ⊢ s ⊛ t : ⊲ B [ t / x ] we have Γ ⊢ s : ⊲ (Π x : A . B ) Γ ⊢ t : ⊲ A Γ ⊢ s ⊛ t : ⊲ [ x ← t ] . B In general ⊲ [ x 1 ← t 1 , . . . x n ← t n ] . A next [ x 1 ← t 1 , . . . x n ← t n ] . t 9 / 21

  16. Equalities ⊲ [ x ← next u ] . A = ⊲ A [ u / x ] 10 / 21

  17. Equalities ⊲ξ [ x ← next ξ. u ] . A = ⊲ξ. A [ u / x ] 10 / 21

  18. Equalities ⊲ξ [ x ← next ξ. u ] . A = ⊲ξ. A [ u / x ] ⊲ ξ [ x ← u ] . A = ⊲ξ. A if x �∈ fv( A ) ⊲ ξ [ x ← u , y ← v ] ξ ′ . A = ⊲ξ [ y ← v , x ← u ] ξ ′ . A if . . . 10 / 21

  19. Equalities ⊲ξ [ x ← next ξ. u ] . A = ⊲ξ. A [ u / x ] ⊲ ξ [ x ← u ] . A = ⊲ξ. A if x �∈ fv( A ) ⊲ ξ [ x ← u , y ← v ] ξ ′ . A = ⊲ξ [ y ← v , x ← u ] ξ ′ . A if . . . next ξ [ x ← next ξ. u ] . t = next ξ. t [ u / x ] next ξ [ x ← u ] . t = next ξ. t if x �∈ fv( t ) next ξ [ x ← u , y ← v ] ξ ′ . t = next ξ [ y ← v , x ← u ] ξ ′ . t if . . . 10 / 21

  20. Typing rule Simple Case Γ , x : A ⊢ t : B Γ ⊢ u : ⊲ A Γ ⊢ next [ x ← u ] . t : ⊲ [ x ← u ] . B 11 / 21

  21. Typing rule Simple Case Γ , x : A ⊢ t : B Γ ⊢ u : ⊲ A Γ ⊢ next [ x ← u ] . t : ⊲ [ x ← u ] . B In General Γ , x 1 : A 1 , . . . , x n : A n ⊢ t : B Γ ⊢ t i : ⊲ [ x 1 ← t 1 , . . . , x i − 1 ← t i − 1 ] . A i for all 1 ≤ i ≤ n Γ ⊢ next [ x 1 ← t 1 , . . . , x n ← t n ] . t : ⊲ [ x 1 ← t 1 , . . . , x n ← t n ] . B 11 / 21

  22. Applicative Structure Applicative structure can be defined in terms of delayed substitutions: s ⊛ t = next [ x ← s , y ← t ] . x y 12 / 21

  23. Applicative Structure Applicative structure can be defined in terms of delayed substitutions: s ⊛ t = next [ x ← s , y ← t ] . x y next u ⊛ next v = next [ x ← next u , y ← next v ] . x y = next [ x ← next u ] . x v = next( u v ) 12 / 21

  24. Applicative Functor Laws We need to add the following equality next ξ [ x ← t ] . x = t 13 / 21

  25. Applicative Functor Laws We need to add the following equality next ξ [ x ← t ] . x = t We can then derive the applicative functor laws: next( λ x . x ) ⊛ t = t next( λ f .λ g .λ x . f ( g x )) ⊛ s ⊛ t ⊛ u = s ⊛ ( t ⊛ u ) next s ⊛ next t = next ( s t ) s ⊛ next t = next( λ f . f t ) ⊛ s 13 / 21

  26. Reduction Semantics 14 / 21

  27. Motivation ◮ we want to implement a type checker for dependent type theory with guarded recursion ◮ we need to decide the equality theory ◮ possible approach: reduction relation that is ◮ strongly normalising ◮ confluent 15 / 21

  28. Problems with Normalisation ◮ Fixed-point combinator! fix t = t (next(fix t )) ◮ We cannot turn this equation into a normalising rewrite rule: next ξ [ x ← u , y ← v ] ξ ′ . A = next ξ [ y ← v , x ← u ] ξ ′ . A 16 / 21

  29. Problems with Confluence next ξ [ x ← next ξ. s ] . t = next ξ. t [ s / x ] 17 / 21

  30. Problems with Confluence next ξ [ x ← next ξ. s ] . t → next ξ. t [ s / x ] 17 / 21

  31. Problems with Confluence next ξ [ x ← next ξ. s ] . t → next ξ. t [ s / x ] 17 / 21

  32. Problems with Confluence next ξ [ x ← next ξ. s ] . t → next ξ. t [ s / x ] t = [ x 1 ← y , x 2 ← [ x 1 ← y ] . 0] . x 1 x 2 → next [ x 1 ← y ] . x 1 0 t t → next [ x 1 ← y , x 2 ← next . 0] . x 1 x 2 17 / 21

  33. Alternative Calculus without Delayed Substitutions Idea ◮ controlled conversion prev : ⊲ A → A . 18 / 21

  34. Alternative Calculus without Delayed Substitutions Idea ◮ controlled conversion prev : ⊲ A → A . ◮ next [ x ← t ] . u next u [prev t / x ] � ◮ ⊲ [ x ← t ] . A ⊲ A [prev t / x ] � 18 / 21

  35. Alternative Calculus without Delayed Substitutions Idea ◮ controlled conversion prev : ⊲ A → A . ◮ next [ x ← t ] . u next l . u [prev l t / x ] � ◮ ⊲ [ x ← t ] . A ⊲ l . A [prev l t / x ] � 18 / 21

  36. Alternative Calculus without Delayed Substitutions Idea ◮ controlled conversion prev : ⊲ A → A . ◮ next [ x ← t ] . u next l . u [prev l t / x ] � ◮ ⊲ [ x ← t ] . A ⊲ l . A [prev l t / x ] � Γ ⊢ L t : I ⊲ l . A Γ ⊢ L , l t : I , l A Γ ⊢ L l ∈ L Γ ⊢ L prev l t : I , l A Γ ⊢ L next l . t : I ⊲ l . A 18 / 21

  37. Alternative Calculus without Delayed Substitutions Idea ◮ controlled conversion prev : ⊲ A → A . ◮ next [ x ← t ] . u next l . u [prev l t / x ] � ◮ ⊲ [ x ← t ] . A ⊲ l . A [prev l t / x ] � Γ ⊢ L t : I ⊲ l . A Γ ⊢ L , l t : I , l A Γ ⊢ L l ∈ L Γ ⊢ L prev l t : I , l A Γ ⊢ L next l . t : I ⊲ l . A Γ , x : I A ⊢ L t : I B J ⊆ I Γ , x : J A , Γ ′ ⊢ L x : I A Γ ⊢ L λ x . t : I A → B 18 / 21

  38. Reduction rules prev l ′ (next l . t ) → t [ l ′ / l ] next l . (prev l t ) → t l �∈ fl( t ) 19 / 21

  39. Reduction rules prev l ′ (next l . t ) → t [ l ′ / l ] next ξ [ x ← next ξ. u ] . A = next ξ. A [ u / x ] next l . (prev l t ) → t l �∈ fl( t ) next ξ [ x ← t ] . x = t 19 / 21

  40. η -rule for ⊲ next [ x ← t ] ξ. u [next x / y ] = next [ x ← t ] ξ. u [ t / y ] 20 / 21

  41. η -rule for ⊲ next [ x ← t ] ξ. u [next x / y ] = next [ x ← t ] ξ. u [ t / y ] next l . (prev l t ) → t 20 / 21

  42. η -rule for ⊲ next [ x ← t ] ξ. u [next x / y ] = next [ x ← t ] ξ. u [ t / y ] next l . (prev l ′ t ) → t l �∈ fl( t ) 20 / 21

  43. η -rule for ⊲ next [ x ← t ] ξ. u [next x / y ] = next [ x ← t ] ξ. u [ t / y ] next l . (prev l ′ t ) → t l �∈ fl( t ) This rule breaks confluence! 20 / 21

  44. Future Work What we have ◮ confluence proof ◮ strong normalisation without dependent types ◮ completeness w.r.t. delayed substitution calculus 21 / 21

  45. Future Work What we have ◮ confluence proof ◮ strong normalisation without dependent types ◮ completeness w.r.t. delayed substitution calculus What is missing ◮ strong normalisation of dependently typed calculus ◮ soundness w.r.t. delayed substitution calculus 21 / 21

Recommend


More recommend