pattern and copattern matching
play

Pattern and Copattern matching Anton Setzer Swansea University, - PowerPoint PPT Presentation

Pattern and Copattern matching Anton Setzer Swansea University, Swansea UK Leeds Logic Seminar, 13 May 2015 Anton Setzer (Swansea) Pattern and Copattern matching 1/ 46 Iteration, Recursion, Induction Coiteration, Corecursion Bisimilarity


  1. Pattern and Copattern matching Anton Setzer Swansea University, Swansea UK Leeds Logic Seminar, 13 May 2015 Anton Setzer (Swansea) Pattern and Copattern matching 1/ 46

  2. Iteration, Recursion, Induction Coiteration, Corecursion Bisimilarity and Coinduction Proofs by Coinduction of Bisimilarity in Transition Systems Mixed Patterns and Copatterns Unnesting of Pattern/Copattern Matching Anton Setzer (Swansea) Pattern and Copattern matching 2/ 46

  3. Iteration, Recursion, Induction Iteration, Recursion, Induction Coiteration, Corecursion Bisimilarity and Coinduction Proofs by Coinduction of Bisimilarity in Transition Systems Mixed Patterns and Copatterns Unnesting of Pattern/Copattern Matching Anton Setzer (Swansea) Pattern and Copattern matching 3/ 46

  4. Iteration, Recursion, Induction N as an Initial Algebra ◮ N is initial algebra of the functor F ( X ) = 1 + X ◮ 0 + S ✲ N F ( N ) = 1 + N F ( g ) = 1 + g ∃ ! g f ′ ❄ ❄ ✲ A F ( A ) = 1 + A f ′ can be decomposed as f ′ = a + f Anton Setzer (Swansea) Pattern and Copattern matching 4/ 46

  5. Iteration, Recursion, Induction Unique Iteration 1 + N 0 + S ✲ N 1 + g ∃ ! g ❄ a + f ✲ A ❄ 1 + A Unique existence of g means unique iteration : Given a : A and f : A → A there exists a unique g : N → A g 0 = a g ( S n ) = f ( g n ) i.e g ( S n 0) f n a = Anton Setzer (Swansea) Pattern and Copattern matching 5/ 46

  6. Iteration, Recursion, Induction Unique Recursion ◮ From the principle of unique iteration we can prove the principle of unique (primitive) recursion: Given a : A and f : N → A → A there exists a unique g : N → A g 0 = a g ( S n ) = f n ( g n ) Anton Setzer (Swansea) Pattern and Copattern matching 6/ 46

  7. Iteration, Recursion, Induction Induction ◮ From the principle of unique iteration we can prove the principle of induction: Assume A : N → Set , a : A 0 and f : ( n : N ) → A n → A ( S n ) There exists a unique g : ( n : N ) → A n g 0 = a g ( S n ) = f n ( g n ) ◮ Using induction we can prove that if we have two solutions for a iteration or recursion principle, they are pointwise equal, i.e. uniqueness of iteration and recursion. Anton Setzer (Swansea) Pattern and Copattern matching 7/ 46

  8. Iteration, Recursion, Induction Pattern Matching ◮ The above means that we can define g : ( n : N ) → A n g 0 = for some a : A a for some a ′ : A depending on n a ′ g ( S n ) = where in the second case we can use the recursion hypothesis or induction hypothesis g n . ◮ This means we can define g n by pattern matching on n : N . Anton Setzer (Swansea) Pattern and Copattern matching 8/ 46

  9. Iteration, Recursion, Induction Iteration, Recursion, Induction Theorem Assume N : Set , 0 : N , S : N → N . The following are equivalent ◮ The principle of unique iteration. ◮ The principle of unique recursion. ◮ The principle of unique induction. ◮ The principle of induction. Anton Setzer (Swansea) Pattern and Copattern matching 9/ 46

  10. Coiteration, Corecursion Iteration, Recursion, Induction Coiteration, Corecursion Bisimilarity and Coinduction Proofs by Coinduction of Bisimilarity in Transition Systems Mixed Patterns and Copatterns Unnesting of Pattern/Copattern Matching Anton Setzer (Swansea) Pattern and Copattern matching 10/ 46

  11. Coiteration, Corecursion Streams as a Final Coalgebra ◮ Dual of + is × , so we use for clarity a functor using product rather than disjoint union: ◮ Stream is the final coalgebra of F ( X ) = N × X f ✲ N × X = F ( X ) X ∃ ! g id × g = F ( g ) ❄ head × tail ✲ N × Stream ❄ Stream = F ( Stream ) ◮ We can decompose f as = f 0 × f 1 f Anton Setzer (Swansea) Pattern and Copattern matching 11/ 46

  12. Coiteration, Corecursion Unique Coiteration f 0 × f 1 ✲ N × X X ∃ ! g id × g head × tail ❄ ❄ ✲ N × Stream Stream This corresponds to the principle of unique coiteration: There exists a unique g : A → Stream head ( g x ) = f 0 x ( g x ) = g ( f 1 x ) tail Anton Setzer (Swansea) Pattern and Copattern matching 12/ 46

  13. Coiteration, Corecursion Unique Coiteration ◮ We had: head ( g x )) = f 0 x ( g x ) = g ( f 1 x ) tail ◮ By choosing f 0 , f 1 we can define g : X → Stream s.t. head ( g x ) = n for some n : N depending on x for some x ′ : X depending on x ( g x ) = g x ′ tail Anton Setzer (Swansea) Pattern and Copattern matching 13/ 46

  14. Coiteration, Corecursion Unique Corecursion ◮ From unique coiteration we can derive unique corecursion : There exists a unique g : A → Stream ( g x ) = for some n : N depending on x head n for some x ′ : X depending on x ( g x ) = g x ′ tail or = for some s : Stream depending on x s ◮ This means we can define g x by copattern matching Anton Setzer (Swansea) Pattern and Copattern matching 14/ 46

  15. Coiteration, Corecursion Examples ◮ We can define : ( N × Stream ) → Stream cons head ( cons ( n , s )) = n ( cons ( n , s )) = tail s Note: cons not primitive but defined by corecursion : N → Stream inc head ( inc n ) = n ( inc n ) = inc ( n + 1) tail Anton Setzer (Swansea) Pattern and Copattern matching 15/ 46

  16. Coiteration, Corecursion Examples inc ′ : N → Stream ( inc ′ ( n )) head = n ( inc ′ ( n )) = inc ′′ ( n + 1) tail inc ′′ : N → Stream ( inc ′′ ( n )) head = n ( inc ′′ ( n )) = inc ′ ( n + 1) tail Anton Setzer (Swansea) Pattern and Copattern matching 16/ 46

  17. Bisimilarity and Coinduction Iteration, Recursion, Induction Coiteration, Corecursion Bisimilarity and Coinduction Proofs by Coinduction of Bisimilarity in Transition Systems Mixed Patterns and Copatterns Unnesting of Pattern/Copattern Matching Anton Setzer (Swansea) Pattern and Copattern matching 17/ 46

  18. Bisimilarity and Coinduction Bisimilarity ◮ Bisimilarity ∼ on Streams is an indexed final coalgebra . ◮ Consider the category Set Stream × Stream of binary relations ϕ : Stream × Stream → Set ◮ Let F ∼ : Set Stream × Stream → Set Stream × Stream F ∼ ( ϕ, ( s , s ′ )) = ( head s = head s ′ ) × ϕ ( tail s , tail s ′ ) Anton Setzer (Swansea) Pattern and Copattern matching 18/ 46

  19. Bisimilarity and Coinduction Bisimilarity ◮ That ∼ is a F ∼ coalgebra means there exist elim ∼ : ( s , s ′ : Stream ) → s ∼ s ′ → ( head s = head s ′ ) × ( tail s ∼ tail s ′ ) i.e. s ∼ s ′ → ( head s = head s ′ ) ∧ (( tail s ) ∼ ( tail s ′ )) ◮ Let elim 0 ∼ and elim 1 ∼ the two components of elim ∼ , ( s , s ′ : Stream ) → s ∼ s ′ → head s = head s ′ elim 0 : ∼ ( s , s ′ : Stream ) → s ∼ s ′ → tail s ∼ tail s ′ elim 1 : ∼ and hide the first two arguments of elim i ∼ . Anton Setzer (Swansea) Pattern and Copattern matching 19/ 46

  20. Bisimilarity and Coinduction Bisimilarity ◮ That ∼ is a final F ∼ -coalgebra means that it is the largest such relation: f ✲ head s = head s ′ ∧ ϕ ( tail s , tail s ′ ) ϕ ( s , s ′ ) ∃ ! g id ∧ g ❄ ❄ elim ∼ ✲ head s = head s ′ ∧ ( tail s ) ∼ ( tail s ′ ) s ∼ s ′ ◮ This means that ∀ s , s ′ .ϕ ( s , s ′ ) → head s = head s ′ ∧ ϕ ( tail s , tail s ′ ) then ∀ s , s ′ .ϕ ( s , s ′ ) → s ∼ s ′ Anton Setzer (Swansea) Pattern and Copattern matching 20/ 46

  21. Bisimilarity and Coinduction Bisimilarity ◮ So we have s ∼ s ′ → head s = head s ′ ∧ ( tail s ) ∼ ( tail s ′ ) and if ∀ s , s ′ .ϕ ( s , s ′ ) → head s = head s ′ ∧ ϕ ( tail s , tail s ′ ) then ∀ s , s ′ .ϕ ( s , s ′ ) → s ∼ s ′ Anton Setzer (Swansea) Pattern and Copattern matching 21/ 46

  22. Bisimilarity and Coinduction Corecursive Proof of Bisimilarity ◮ Because ∼ is a final coalgebra we can compute proofs of it by corecursion: ◮ We can define f : ( s , s ′ : Stream ) → ϕ s s ′ → s ∼ s ′ ∼ ( f s s ′ x ) elim 0 = an element of head s = head s ′ ∼ ( f s s ′ x ) elim 0 = an element of ( tail s ) ∼ ( tail s ′ ) where in the last line we can use ◮ either a proof of tail s ∼ tail s ′ defined before ◮ or use the corecursion hypothesis f ( tail s ) ( tail s ′ ) x ′ for some x ′ : ϕ ( tail s ) ( tail s ′ ) Anton Setzer (Swansea) Pattern and Copattern matching 22/ 46

  23. Bisimilarity and Coinduction Coinduction Theorem Assume Stream : Set , head : Stream → N , tail : Stream → Stream . The following are equivalent ◮ The principle of unique coiteration. ◮ The principle of unique corecursion. ◮ The principle of iteration together with the principle that bisimilarity ∼ implies equality ∀ s , s ′ : Stream . s ∼ s ′ → s = s ′ Because of the possibility of defining elements of s ∼ s ′ the latter can be considered as a principle of coinduction . Anton Setzer (Swansea) Pattern and Copattern matching 23/ 46

  24. Bisimilarity and Coinduction Principle of Coinduction ◮ Let ϕ : Stream → Stream → Set . ◮ We can prove ∀ s , s ′ : Stream .ϕ s s ′ → s = s ′ by showing ∀ s , s ′ : Stream .ϕ s s ′ → head s = head s ′ ∀ s , s ′ : Stream .ϕ s s ′ → tail s = tail s ′ where for proving tail s = tail s ′ we can use the coinduction hypothesis that ϕ ( tail s ) ( tail s ′ ) implies tail s = tail s ′ . Anton Setzer (Swansea) Pattern and Copattern matching 24/ 46

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend