program ing finger trees in coq or how to morph endo
play

Program -ing Finger Trees In Coq or How To Morph Endo Using Type - PowerPoint PPT Presentation

Program -ing Finger Trees In Coq or How To Morph Endo Using Type Theory Matthieu Sozeau LRI , Univ. Paris-Sud - D emons Team & INRIA Saclay - ProVal Project ICFP07 October 13 2007 Freiburg, Germany The Curry-Howard isomorphism


  1. Program -ing Finger Trees In Coq or How To Morph Endo Using Type Theory Matthieu Sozeau LRI , Univ. Paris-Sud - D´ emons Team & INRIA Saclay - ProVal Project ICFP’07 October 1–3 2007 Freiburg, Germany

  2. The Curry-Howard isomorphism Programming language = Proof system

  3. The Curry-Howard isomorphism Programming language = Proof system Program extends the Coq proof-assistant into a dependently-typed programming environment.

  4. The Curry-Howard isomorphism Programming language = Proof system Program extends the Coq proof-assistant into a dependently-typed programming environment. Epigram PVS DML Ω mega

  5. The Curry-Howard isomorphism Programming language = Proof system Program extends the Coq proof-assistant into a dependently-typed programming environment. ◮ Logical Framework Type Theory. Epigram PVS DML Ω mega

  6. The Curry-Howard isomorphism Programming language = Proof system Program extends the Coq proof-assistant into a dependently-typed programming environment. ◮ Logical Framework Type Theory. Separates proofs and programs using sorts. Epigram PVS DML Ω mega

  7. The Curry-Howard isomorphism Programming language = Proof system Program extends the Coq proof-assistant into a dependently-typed programming environment. ◮ Logical Framework Type Theory. Separates proofs and programs using sorts. ◮ Paradigm Purely functional. . Epigram PVS DML Ω mega

  8. The Curry-Howard isomorphism Programming language = Proof system Program extends the Coq proof-assistant into a dependently-typed programming environment. ◮ Logical Framework Type Theory. Separates proofs and programs using sorts. ◮ Paradigm Purely functional. No separation of terms and types. Epigram PVS DML Ω mega

  9. The Curry-Howard isomorphism Programming language = Proof system Program extends the Coq proof-assistant into a dependently-typed programming environment. ◮ Logical Framework Type Theory. Separates proofs and programs using sorts. ◮ Paradigm Purely functional. No separation of terms and types. ◮ Proof automation Semi-automatic, using tactics. Epigram PVS DML Ω mega

  10. The Curry-Howard isomorphism Programming language = Proof system Program extends the Coq proof-assistant into a dependently-typed programming environment. ◮ Logical Framework Type Theory. Separates proofs and programs using sorts. ◮ Paradigm Purely functional. No separation of terms and types. ◮ Proof automation Semi-automatic, using tactics. ◮ Phase distinction none Epigram PVS DML Ω mega

  11. The Curry-Howard isomorphism Programming language = Proof system Program extends the Coq proof-assistant into a dependently-typed programming environment. ◮ Logical Framework Type Theory. Separates proofs and programs using sorts. ◮ Paradigm Purely functional. No separation of terms and types. ◮ Proof automation Semi-automatic, using tactics. ◮ Phase distinction ⇒ in Program Epigram PVS DML Ω mega

  12. Program -ing with subsets Fixpoint div ( a : nat ) ( b : nat | b � = 0) { wf lt } : { ( q , r ) : nat × nat | a = b × q + r ∧ r < b } := if less than a ( proj b ) then ((0, a ), ?) else dest div ( a - proj b ) b as ( q’ , r ) in ((S q’ , r ), ?). where : less than : ∀ x y : nat , { x < y } + { x ≥ y }

  13. Program -ing with subsets Program Fixpoint div ( a : nat ) ( b : nat | b � = 0) { wf lt } : { ( q , r ) : nat × nat | a = b × q + r ∧ r < b } := if less than a b then (0, a ) else dest div ( a - b ) b as ( q’ , r ) in (S q’ , r ). where : less than : ∀ x y : nat , { x < y } + { x ≥ y } Enriched type equality Γ , x : U ⊢ P : Prop Γ ⊢ { x : U | P } � U : Type Γ , x : U ⊢ P : Prop Γ ⊢ U � { x : U | P } : Type

  14. Outline 1 Program 2 Finger Trees 3 In Coq 4 A Rope for Endo M. Sozeau (LRI) Program -ing Finger Trees In Coq ICFP’07 4 / 19

  15. A quick tour of Finger Trees ◮ A Simple General Purpose Data Structure (Hinze & Paterson, JFP 2006) ◮ Purely functional, nested datatype ◮ Parameterized data structure ◮ Efficient deque operations, concatenation and splitting ◮ Comparable to Kaplan & Tarjan’s catenable deques M. Sozeau (LRI) Program -ing Finger Trees In Coq ICFP’07 5 / 19

  16. The Big Finger Tree Picture data Digit a = One a | Two a a | Three a a a | Four a a a a M. Sozeau (LRI) Program -ing Finger Trees In Coq ICFP’07 6 / 19

  17. The Big Finger Tree Picture data Digit a = One a | Two a a | Three a a a | Four a a a a data Node a = Node2 a a | Node3 a a a M. Sozeau (LRI) Program -ing Finger Trees In Coq ICFP’07 6 / 19

  18. The Big Finger Tree Picture data Digit a = One a | Two a a | Three a a a | Four a a a a data Node a = Node2 a a | Node3 a a a Deep data FingerTree a = | Empty | Single a Two Three | Deep ( Digit a ) Deep ( FingerTree ( Node a )) ( Digit a ) Empty Two One Node2 Node3 Node2 M. Sozeau (LRI) Program -ing Finger Trees In Coq ICFP’07 6 / 19

  19. Operating on a Finger Tree add left :: a → FingerTree a → FingerTree a add left a Empty = Single a add left a (Single b ) = Deep (One a ) Empty (One b ) add left a (Deep pr m sf ) = . . . Deep Empty Three Three C D E F G H M. Sozeau (LRI) Program -ing Finger Trees In Coq ICFP’07 7 / 19

  20. Operating on a Finger Tree add left :: a → FingerTree a → FingerTree a add left a Empty = Single a add left a (Single b ) = Deep (One a ) Empty (One b ) add left a (Deep pr m sf ) = . . . Deep Empty Four Three B C D E F G H M. Sozeau (LRI) Program -ing Finger Trees In Coq ICFP’07 7 / 19

  21. Operating on a Finger Tree add left :: a → FingerTree a → FingerTree a add left a Empty = Single a add left a (Single b ) = Deep (One a ) Empty (One b ) add left a (Deep pr m sf ) = . . . Deep Single Two Three A B F G H Node3 C D E M. Sozeau (LRI) Program -ing Finger Trees In Coq ICFP’07 7 / 19

  22. Adding cached measures class Monoid v ⇒ Measured v a where � � :: a → v M. Sozeau (LRI) Program -ing Finger Trees In Coq ICFP’07 8 / 19

  23. Adding cached measures class Monoid v ⇒ Measured v a where � � :: a → v instance ( Measured v a ) ⇒ Measured v ( Digit a ) where · · · M. Sozeau (LRI) Program -ing Finger Trees In Coq ICFP’07 8 / 19

  24. Adding cached measures class Monoid v ⇒ Measured v a where � � :: a → v instance ( Measured v a ) ⇒ Measured v ( Digit a ) where · · · Deep data Node v a = � a �···� g � Node2 v a a | Node3 v a a a data FingerTree v a = | Empty Empty Two One | Single a ε | Deep v ( Digit a ) ( FingerTree v ( Node v a )) Node2 Node3 Node2 ( Digit a ) � a �·� b � � c �·� d �·� e � � f �·� g � g a c e b d f M. Sozeau (LRI) Program -ing Finger Trees In Coq ICFP’07 8 / 19

  25. Outline 1 Program 2 Finger Trees 3 In Coq 4 A Rope for Endo M. Sozeau (LRI) Program -ing Finger Trees In Coq ICFP’07 9 / 19

  26. Why do this ? ◮ Generally useful, non-trivial structure M. Sozeau (LRI) Program -ing Finger Trees In Coq ICFP’07 10 / 19

  27. Why do this ? ◮ Generally useful, non-trivial structure ◮ Abstraction power needed to ensure coherence of measures M. Sozeau (LRI) Program -ing Finger Trees In Coq ICFP’07 10 / 19

  28. Why do this ? ◮ Generally useful, non-trivial structure ◮ Abstraction power needed to ensure coherence of measures ◮ Makes dependent types (subsets and indexed datatypes) shine M. Sozeau (LRI) Program -ing Finger Trees In Coq ICFP’07 10 / 19

  29. Why do this ? ◮ Generally useful, non-trivial structure ◮ Abstraction power needed to ensure coherence of measures ◮ Makes dependent types (subsets and indexed datatypes) shine ◮ Fun ! Helps solve the ICFP contest using Coq M. Sozeau (LRI) Program -ing Finger Trees In Coq ICFP’07 10 / 19

  30. Digits Variable A : Type . Inductive digit : Type := | One : A → digit | Two : A → A → digit | Three : A → A → A → digit | Four : A → A → A → A → digit . Definition full x := match x with Four ⇒ True | ⇒ False end . M. Sozeau (LRI) Program -ing Finger Trees In Coq ICFP’07 11 / 19

  31. Digits cont’d Program Definition add digit left ( a : A ) ( d : digit | ¬ full d ) : digit := match d with | One x ⇒ Two a x | Two x y ⇒ Three a x y | Three x y z ⇒ Four a x y z | Four ⇒ ! end . Next Obligation . intros ; simpl in n ; auto . Qed . M. Sozeau (LRI) Program -ing Finger Trees In Coq ICFP’07 12 / 19

  32. Nodes Variables ( v : Type ) ( mono : monoid v ). Variables ( A : Type ) ( measure : A → v ). M. Sozeau (LRI) Program -ing Finger Trees In Coq ICFP’07 13 / 19

  33. Nodes Variables ( v : Type ) ( mono : monoid v ). Variables ( A : Type ) ( measure : A → v ). Inductive node : Type := | Node2 : ∀ x y , { s : v | s = � x � · � y � } → node | Node3 : ∀ x y z , { s : v | s = � x � · � y � · � z � } → node . M. Sozeau (LRI) Program -ing Finger Trees In Coq ICFP’07 13 / 19

  34. Nodes Variables ( v : Type ) ( mono : monoid v ). Variables ( A : Type ) ( measure : A → v ). Inductive node : Type := | Node2 : ∀ x y , { s : v | s = � x � · � y � } → node | Node3 : ∀ x y z , { s : v | s = � x � · � y � · � z � } → node . Program Definition node2 ( x y : A ) : node := Node2 x y ( � x � · � y � ). Program Definition node measure ( n : node ) : v := match n with Node2 s ⇒ s | Node3 s ⇒ s end . M. Sozeau (LRI) Program -ing Finger Trees In Coq ICFP’07 13 / 19

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