Experience implementing a performant category-theory library in Coq Jason Gross, Adam Chlipala, David I. Spivak Massachusetts Institute of Technology
How should theorem provers work? 2
How theorem provers should work: 1 = 0 1 = 0 No; hereโs a Coq, is this proof of correct? 1 = 0 โ False 3
How theorem provers should work: Th Theo eorem rem (curr rryi ying ng) ) : ๐ซ ๐ โ ๐ซ ๐ โ ๐ฌ โ (๐ซ ๐ ร ๐ซ ๐ โ ๐ฌ) Proof: : homewo ework k โ Coq, is this Yes; hereโs a correct? proof โฆ 4
How theorem provers should work: Th Theo eorem rem (curr rryi ying ng) ) : ๐ซ ๐ โ ๐ซ ๐ โ ๐ฌ โ (๐ซ ๐ ร ๐ซ ๐ โ ๐ฌ) Proof: : homewo ework k โ Theorem currying : ๐ท 1 โ ๐ท 2 โ ๐ธ โ ๐ท 1 ร ๐ท 2 โ ๐ธ . Proof. trivial. Qed. 5
How theorem provers should work: Theo Th eorem rem (curr rryi ying ng) ) : ๐ซ ๐ โ ๐ซ ๐ โ ๐ฌ โ (๐ซ ๐ ร ๐ซ ๐ โ ๐ฌ) Proof: โ : : ๐ฎ โฆ ๐ ๐ ๐ , ๐ ๐ . ๐ฎ ๐ ๐ ๐ ๐ ; morph phisms isms simila larly ly โ : : ๐ฎ โฆ ๐ ๐ ๐ . ๐ ๐ ๐ . ๐ฎ(๐ ๐ , ๐ ๐ ) ; morphis hisms ms simila larly ly Functoriality oriality, , naturality rality, , and congrue uence: nce: straig ightfo htforward. rward. โ Theorem currying : ๐ท 1 โ ๐ท 2 โ ๐ธ โ ๐ท 1 ร ๐ท 2 โ ๐ธ . Proof. esplit. { by refine ( ๐ F ( ๐บ โฆ ( ๐ F ( ๐ โฆ ๐บ o ๐ 1 ๐ 2 )))). } { by refine ( ๐ F ( ๐บ โฆ ( ๐ F ( ๐ 1 โฆ ( ๐ F ( ๐ 2 โฆ ๐บ o (๐ 1 , ๐ 2 )) ))))). } all: trivial. Qed. 6
How theorem provers should work: Th Theo eorem rem (curr rryi ying ng) ) : ๐ซ ๐ โ ๐ซ ๐ โ ๐ฌ โ (๐ซ ๐ ร ๐ซ ๐ โ ๐ฌ) Proof: โ : : ๐ฎ โฆ ๐ ๐ ๐ , ๐ ๐ . ๐ฎ ๐ ๐ ๐ ๐ ; morph phisms isms simila larly ly โ : : ๐ฎ โฆ ๐ ๐ ๐ . ๐ ๐ ๐ . ๐ฎ(๐ ๐ , ๐ ๐ ) ; morphis hisms ms simila larly ly Functoriality oriality, , naturality rality, , and congrue uence: nce: straig ightfo htforward. rward. โ Theorem currying : ๐ท 1 โ ๐ท 2 โ ๐ธ โ ๐ท 1 ร ๐ท 2 โ ๐ธ . Proof. esplit. { by refine ( ๐ F ( ๐บ โฆ ( ๐ F ( ๐ โฆ ๐บ o ๐ 1 ๐ 2 ) ( ๐ก ๐ ๐ โฆ ๐บ o ๐ 1 m ๐ 2 โ ๐บ m ๐ 1 o ๐ก 2 )) ( ๐บ ๐ป ๐ โฆ ( ๐ T ( ๐ โฆ ๐ ๐ 1 ๐ 2 )))). } { by refine ( ๐ F ( ๐บ โฆ ( ๐ F ( ๐ 1 โฆ ( ๐ F ( ๐ 2 โฆ ๐บ o (๐ 1 , ๐ 2 )) ( ๐ก ๐ ๐ โฆ ๐บ m (1, ๐)) )) ( ๐บ ๐ป ๐ โฆ ( ๐ T ( ๐ 1 โฆ ( ๐ T ( ๐ 2 โฆ ๐ (๐ 1 , ๐ 2 ) ))))). } all: trivial. Qed. 7
How theorem provers do work: Th Theo eorem rem (curr rryi ying ng) ) : ๐ซ ๐ โ ๐ซ ๐ โ ๐ฌ โ (๐ซ ๐ ร ๐ซ ๐ โ ๐ฌ) Proof: โ : : ๐ฎ โฆ ๐ ๐ ๐ , ๐ ๐ . ๐ฎ ๐ ๐ ๐ ๐ ; morph phisms isms simila larly ly โ 0 s โ : : ๐ฎ โฆ ๐ ๐ ๐ . ๐ ๐ ๐ . ๐ฎ(๐ ๐ , ๐ ๐ ) ; morphis hisms ms simila larly ly Functoriality oriality, , naturality rality, , and congrue uence: nce: straig ightfo htforward. rward. โ 17 s 2m 46 s !!! (5 s, if we use UIP) Theorem currying : ๐ท 1 โ ๐ท 2 โ ๐ธ โ ๐ท 1 ร ๐ท 2 โ ๐ธ . Proof. esplit. { by refine ( ๐ F ( ๐บ โฆ ( ๐ F ( ๐ โฆ ๐บ o ๐ 1 ๐ 2 ) ( ๐ก ๐ ๐ โฆ ๐บ o ๐ 1 m ๐ 2 โ ๐บ m ๐ 1 o ๐ก 2 )) ( ๐บ ๐ป ๐ โฆ ( ๐ T ( ๐ โฆ ๐ ๐ 1 ๐ 2 )))). } { by refine ( ๐ F ( ๐บ โฆ ( ๐ F ( ๐ 1 โฆ ( ๐ F ( ๐ 2 โฆ ๐บ o (๐ 1 , ๐ 2 )) ( ๐ก ๐ ๐ โฆ ๐บ m (1, ๐)) )) ( ๐บ ๐ป ๐ โฆ ( ๐ T ( ๐ 1 โฆ ( ๐ T ( ๐ 2 โฆ ๐ (๐ 1 , ๐ 2 ) ))))). } all: trivial. Qed. 8
Performance is important! If weโre not careful, obvious or trivial things can be very, very slow. 9
Why you should listen to me Theorem : You should listen to me. Proof. by experience. Qed. 10
Why you should listen to me Category theory in Coq: https://github.com/HoTT/HoTT (subdirectory theories/categories): ๐ท 1 โ ๐ท ; 1 ๐ท โ 1 โข Concepts Formalized: ๐ท ๐ต+๐ถ โ ๐ท ๐ต ร ๐ท ๐ถ โข โข 1-precategories (in the sense of the HoTT Book) (๐ต ร ๐ถ) ๐ท โ ๐ต ๐ท ร ๐ถ ๐ท โข โข univalent/saturated categories (or just categories, in the HoTT Book) โข โข (๐ต ๐ถ ) ๐ท โ ๐ต ๐ถร๐ท functor precategories ๐ท โ ๐ธ dual functor isomorphisms Cat โ Cat ; and ๐ท โ ๐ธ op โ (๐ท op โ ๐ธ op ) โข โข Product laws โข โข the category Prop of (U-small) hProps ๐ท ร ๐ธ โ ๐ธ ร ๐ท โข โข the category Set of (U-small) hSets ๐ท ร 0 โ 0 ร ๐ท โ 0 โข โข the category Cat of (U-small) strict (pre)categories (strict in the sense of the ๐ท ร 1 โ 1 ร ๐ท โ ๐ท โข objects being hSets) Grothendieck construction (oplax colimit) of a pseudofunctor to Cat โข โข pseudofunctors Category of sections (gives rise to oplax limit of a pseudofunctor to Cat when โข profunctors applied to Grothendieck construction identity profunction (the hom functor ๐ท op ร ๐ท โ Set ) โข โข functor composition is functorial (there's a functor ฮ: ๐ท โ ๐ธ โ (๐ธ โ โข adjoints โข equivalences between a number of definitions: โข unit-counit + zig-zag definition โข unit + UMP definition โข counit + UMP definition โข universal morphism definition โข hom-set definition (porting from old version in progress) โข composition, identity, dual pointwise adjunctions in the library, ๐ป ๐น โฃ ๐บ ๐ท and ๐น ๐บ โฃ ๐ท ๐ป from an โข adjunction ๐บ โฃ ๐ป for functors ๐บ: ๐ท โ ๐ธ: ๐ป and ๐น a precategory (still too slow to be merged into the library proper; code here) โข Yoneda lemma โข Exponential laws 11 ๐ท 0 โ 1 ; 0 ๐ท โ 0 given an object in ๐ท โข
Presentation is not mainly about: 12
Presentation is not mainly about: โข category theory or diagram chasing Cartoon from xkcd, adapted by Alan Huang 13
Presentation is not mainly about: โข category theory or diagram chasing Cartoon from xkcd, adapted by Alan Huang โข my library 14
Presentation is not mainly about: โข category theory or diagram chasing Cartoon from xkcd, adapted by Alan Huang โข my library โข Coq 15
Presentation is not mainly about: โข category theory or diagram chasing Cartoon from xkcd, adapted by Alan Huang โข my library โข Coq (though what I say might not always generalize nicely) 16
Presentation is is about: โข performance โข the design of proof assistants and type theories to assist with performance โข the kind of performance issues I encountered 17
Presentation is is for: โข Users of proof assistants (and Coq in particular) โข Who want to make their code faster โข Designers of (type-theoretic) proof assistants โข Who want to know where to focus their optimization efforts 18
Outline โข Why should we care about performance? โข What makes theorem provers (mainly Coq) slow? โข Examples of particular slowness โข For users (workarounds) โข Arguments vs. fields and packed records โข Proof by duality as proof by unification โข Abstraction barriers โข Proof by reflection โข For developers (features) โข Primitive projections โข Higher inductive types โข Universe Polymorphism Universes image from Abell NGC2218 hst big, NASA, โข More judgmental rules http://en.wikipedia.org/wiki/Abell_2218#mediaviewer/File:A bell_NGC2218_hst_big.jpg, released in Public Domain; Bubble from http://pixabay.com/en/blue-bubble-shiny- 157652/, released in Public Domain CC0, combined in โข Hashconsing Photoshop by Jason Gross 19 Dam image from http://www.flickr.com/photos/gammaman/7803829282/ by Eli Christman, CC by 2.0 Fence image from http://www.picgifs.com/clip-art/playing-children/clip-art-playing-children-362018-689955/
Performance โข Question: What makes programs, particularly theorem provers or proof scripts, slow? 20
Performance โข Question: What makes programs, particularly theorem provers or proof scripts, slow? โข Answer: Doing too much stuff! 21
Performance โข Question: What makes programs, particularly theorem provers or proof scripts, slow? โข Answer: Doing too much stuff! โข doing the same things repeatedly 22 Snail from http://naolito.deviantart.com/art/Repetitive-task-258126598
Performance โข Question: What makes programs, particularly theorem provers or proof scripts, slow? โข Answer: Doing too much stuff! โข doing the same things repeatedly โข doing lots of stuff for no good reason 23 Running rooster from http://d.wapday.com:8080/animation/ccontennt/15545-f/mr_rooster_running.gif
Performance โข Question: What makes programs, particularly theorem provers or proof scripts, slow? โข Answer: Doing too much stuff! โข doing the same things repeatedly โข doing lots of stuff for no good reason โข using a slow language when you could be using a quicker one 24
Proof assistant performance โข What kinds of things does Coq do? โข Type checking โข Term building โข Unification โข Normalization 25
Proof assistant performance (pain) โข When are these slow? โข when you duplicate work โข when you do work on a part of a term you end up not caring about โข when you do them too many times โข when your term is large 26
Proof assistant performance (size) โข How large is slow? 27
Recommend
More recommend