simulating codata types using coalgebras
play

Simulating Codata Types using Coalgebras Anton Setzer Swansea - PowerPoint PPT Presentation

Simulating Codata Types using Coalgebras Anton Setzer Swansea University, Swansea UK Types 2018, Braga, Portugal 18 June 2018 Coalgebras in Type Theory Musical Notation In Agda Reduced to Coalgebras Suggested Extension Conclusion Anton


  1. Simulating Codata Types using Coalgebras Anton Setzer Swansea University, Swansea UK Types 2018, Braga, Portugal 18 June 2018

  2. Coalgebras in Type Theory Musical Notation In Agda Reduced to Coalgebras Suggested Extension Conclusion Anton Setzer Simulating Codata Types using Coalgebras 2/ 25

  3. Coalgebras in Type Theory Coalgebras in Type Theory Musical Notation In Agda Reduced to Coalgebras Suggested Extension Conclusion Anton Setzer Simulating Codata Types using Coalgebras 3/ 25

  4. Coalgebras in Type Theory Codata Types ◮ Original way of defining infinite (in general non-well-founded) structures in functional programming are codata types: codata coList : Set where : nil coList cons : N → coList → coList ◮ Define from : N → coList from n = cons n ( from ( n + 1 )) ◮ Problem ◮ Literally taken non-normalising . ◮ Restriction of evaluation to lazy evaluation or similar led to subject reduction problem in Coq and early versions of Agda. ◮ Proof in [4] that there is no decidable equality on codata types which would allow pattern matching . Anton Setzer Simulating Codata Types using Coalgebras 4/ 25

  5. Coalgebras in Type Theory Coalgebras ◮ Solution define infinite structures by elimination rules or by their observations. ◮ Replace Pattern matching by copattern matching [2]. ◮ Example Streams (syntax is desired syntax – Agda uses record types instead): coalg Stream : Set where : Stream → N head tail : Stream → Stream ◮ Define the stream n , n + 1 , n + 2 , . . . by copattern matching from : N → Stream head ( from n ) = n ( from n ) = from ( n + 1 ) tail Anton Setzer Simulating Codata Types using Coalgebras 5/ 25

  6. Coalgebras in Type Theory Colists ◮ When applying the above to colists one need to have an observation which determines for every colist whether it is nil or cons . ◮ Most easily done by using a simultaneous inductive-coinductive definition. mutual coalg ∞ coList : Set where ♭ : ∞ coList → coList data coList : Setwhere : nil coList cons : N → ∞ coList → coList Anton Setzer Simulating Codata Types using Coalgebras 6/ 25

  7. Musical Notation In Agda Reduced to Coalgebras Coalgebras in Type Theory Musical Notation In Agda Reduced to Coalgebras Suggested Extension Conclusion Anton Setzer Simulating Codata Types using Coalgebras 7/ 25

  8. Musical Notation In Agda Reduced to Coalgebras Examples of Coalgebras in Agda ◮ We have developed lots of coalgebras in Agda: ◮ IO monad in Agda. ◮ Formalisation of CSP in Agda. ◮ Objects (as in object-based programming) in Agda ◮ GUIs in Agda. ◮ Business processes in Agda ◮ Many variants of the above ◮ In some examples definition by several eliminators is the right thing. ◮ In some example one has the pattern as above but needs to add to the type corresponding to ∞ coList extra components. ◮ But most examples follow exactly the same pattern as above. ◮ Musical notation modified by Danielsson [5] was an abbreviation mechanism in Agda for having the above. (Currently abandoned). ◮ Suggestion: Reduce musical notation to coalgebras. ◮ Musical notation as a syntactic sugar for coalgebra approach. Anton Setzer Simulating Codata Types using Coalgebras 8/ 25

  9. Musical Notation In Agda Reduced to Coalgebras Functions for Introduction Codata like Coalgebras ◮ When defining functions into a codata like coalgebra one defines mutually two functions: ♯ f : A → ∞ coList ♭ ( ♯ f a ) = f a f : A → coList f a = · · · ( referring to ♯ f ) ◮ Example from musical notation documentation in Agda: ♯ map : ( N → N ) → coList → ∞ coList ♭ ( ♯ map f l ) = map f l map : ( N → N ) → coList → coList map f nil = nil map f ( cons n l ) = cons ( f n ) ( ♯ map f l ) Anton Setzer Simulating Codata Types using Coalgebras 9/ 25

  10. Suggested Extension Coalgebras in Type Theory Musical Notation In Agda Reduced to Coalgebras Suggested Extension Conclusion Anton Setzer Simulating Codata Types using Coalgebras 10/ 25

  11. Suggested Extension ∞ as Syntactic Sugar ◮ Whenever one defines a new constant C : ( x 1 : A 1 ) ( x 2 : A 2 ) · · · ( x n : A n ) → Set one defines simultaneously with any definition involving C coalg ∞ C ( x 1 : A 1 ) ( x 2 : A 2 ) · · · ( x n : A n ) : Set where ♭ : ∞ C x 1 · · · x n → C x 1 · · · x n ◮ Whenever one defines a new constant f : ( x 1 : A 1 ) ( x 2 : A 2 ) · · · ( x n : A n ) → C t where C is a constant one defines ♯ f : ( x 1 : A 1 ) ( x 2 : A 2 ) · · · ( x n : A n ) → ∞ C t ♭ ( ♯ f x 1 · · · x n ) = f x 1 · · · x n Anton Setzer Simulating Codata Types using Coalgebras 11/ 25

  12. Suggested Extension ∞ as Syntactic Sugar ◮ Whether the following is a good notation needs to be discussed. ◮ However it allows to give an interpretation of Altenkirch et. al.’s boxed operator [3], where ♯ t is the type of delayed computations. ◮ If C s 1 · · · s n is an expression where C is a constant define ∞ ( C s 1 · · · s n ) := ∞ C s 1 · · · s n ◮ If f s 1 · · · s n is an expression where f is a constant define ♯ ( f s 1 · · · s n ) := ♯ f s 1 · · · s n ◮ In the above C s 1 · · · s n and f s 1 · · · s n are not evaluated, the right hand side is evaluated. ◮ Note that ∞ x and ♯ x for variables x is not defined. Anton Setzer Simulating Codata Types using Coalgebras 12/ 25

  13. Suggested Extension Syntactic Sugar ◮ Note that the above is just syntactic sugar. ◮ Type checking and Term checking relies on type and termination checking of the desugared version. ◮ Should the above definitions not be suitable, the user has access to the standard coalgebra definitions. Anton Setzer Simulating Codata Types using Coalgebras 13/ 25

  14. Suggested Extension Example: coList, map and from data coList : Setwhere : nil coList cons : N → ∞ coList → coList map : ( N → N ) → coList → coList = map f nil nil map f ( cons n l ) = cons ( f n ) ( ♯ ( map f l )) from : N → coList = cons n ( ♯ ( from ( n + 1 ))) from n Anton Setzer Simulating Codata Types using Coalgebras 14/ 25

  15. Suggested Extension Sized Types ◮ For coalgebras one needs except for very simple examples sized types. ◮ In case of coList the definition is as follows: mutual coalg ∞ coList { i : Size } : Set where ♭ : { j : Size < i } → ∞ coList { i } → coList { j } data coList { i : Size } : Set where nil : coList { i } : N → ∞ coList { i } → coList { i } cons Anton Setzer Simulating Codata Types using Coalgebras 15/ 25

  16. Suggested Extension ∞ , ♯ with Sizes ◮ Whenever one defines a new constant C : { i : Size } ( x 1 : A 1 ) ( x 2 : A 2 ) · · · ( x n : A n ) → Set one defines simultaneously with any definition involving C coalg ∞ C { i : Size } ( x 1 : A 1 ) ( x 2 : A 2 ) · · · ( x n : A n ) : Set where ♭ : { j : Size < i } → ∞ C { i } x 1 · · · x n → C { j } x 1 · · · x n ◮ Whenever one defines a new constant f : { i : Size } ( x 1 : A 1 ) ( x 2 : A 2 ) · · · ( x n : A n ) → C t where C is a constant one defines ♯ f : { i : Size } ( x 1 : A 1 ) ( x 2 : A 2 ) · · · ( x n : A n ) → ∞ C t ♭ ( ♯ f { i } x 1 · · · x n ) { j } = f { j } x 1 · · · x n Anton Setzer Simulating Codata Types using Coalgebras 16/ 25

  17. Suggested Extension IO Interface data Command : Set where : getStr Command putStr : String → Command Response : Command → Set = String Response getStr ( putStr s ) = ⊤ Response Anton Setzer Simulating Codata Types using Coalgebras 17/ 25

  18. Suggested Extension Example IO data IO { i : Size } ( A : Set ) : Set where : A → IO { i } A return exec : ( c : Command ) ( p : Response c → ∞ ( IO { i } A )) → IO { i } A copycat : { i : Size } → IO { i } ⊥ copycat = exec getStr λ s → ♯ ( exec ( putStr s ) λ → ♯ copycat ) Anton Setzer Simulating Codata Types using Coalgebras 18/ 25

  19. Suggested Extension Object Interface for Cell data Method : Set where : get Method put : N → Method Result : Method → Set = Result get N ( put n ) = ⊤ Result Anton Setzer Simulating Codata Types using Coalgebras 19/ 25

  20. Suggested Extension Example Object [1] Object : { i : Size } → Set Object { i } = ( m : Method ) → IO ∞ ( Result m × ♯ ( Object { i } )) cell : { i : Size } → N → Object { i } = exec ( putStr “get invoked” ) λ → cell n get ♯ ( return ( n , ♯ ( cell n ))) cell n ( put m ) = exec ( putStr “put invoked” ) λ → ♯ ( return ( , ♯ ( cell m ))) Anton Setzer Simulating Codata Types using Coalgebras 20/ 25

  21. Conclusion Coalgebras in Type Theory Musical Notation In Agda Reduced to Coalgebras Suggested Extension Conclusion Anton Setzer Simulating Codata Types using Coalgebras 21/ 25

  22. Conclusion Conclusion ◮ Definition of coalgebras by their elimination as a clean approach for defining “infinite data types” (more precisely well-founded). ◮ Musical notation as syntactic sugar which reduces to coalgebras. ◮ Delayed computations can be interpreted in this setting. ◮ Resulting code is very close to codata types. Anton Setzer Simulating Codata Types using Coalgebras 22/ 25

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