{"id":13442891,"url":"https://github.com/jwiegley/category-theory","last_synced_at":"2025-05-15T04:03:43.752Z","repository":{"id":18311406,"uuid":"21490182","full_name":"jwiegley/category-theory","owner":"jwiegley","description":"An axiom-free formalization of category theory in Coq for personal study and practical work","archived":false,"fork":false,"pushed_at":"2025-03-24T07:31:18.000Z","size":3426,"stargazers_count":767,"open_issues_count":7,"forks_count":72,"subscribers_count":19,"default_branch":"master","last_synced_at":"2025-04-14T01:59:05.602Z","etag":null,"topics":["cartesian","cartesian-closed-category","categories","category","category-theory","comonads","construction","coq","functor","monad","monoid","profunctor","profunctor-composition"],"latest_commit_sha":null,"homepage":"","language":"Coq","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jwiegley.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2014-07-04T08:13:37.000Z","updated_at":"2025-04-04T23:20:16.000Z","dependencies_parsed_at":"2024-03-28T19:34:09.227Z","dependency_job_id":"82ef5b08-5b33-403e-9b4c-cab80f7d3ee9","html_url":"https://github.com/jwiegley/category-theory","commit_stats":{"total_commits":1424,"total_committers":19,"mean_commits":74.94736842105263,"dds":"0.053370786516853896","last_synced_commit":"3e06aa30b32d19af12a6774ddaa8fda593accabc"},"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwiegley%2Fcategory-theory","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwiegley%2Fcategory-theory/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwiegley%2Fcategory-theory/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwiegley%2Fcategory-theory/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jwiegley","download_url":"https://codeload.github.com/jwiegley/category-theory/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254270640,"owners_count":22042858,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cartesian","cartesian-closed-category","categories","category","category-theory","comonads","construction","coq","functor","monad","monoid","profunctor","profunctor-composition"],"created_at":"2024-07-31T03:01:52.788Z","updated_at":"2025-05-15T04:03:43.716Z","avatar_url":"https://github.com/jwiegley.png","language":"Coq","funding_links":[],"categories":["Coq","Projects","Rocq Prover","Software Libraries"],"sub_categories":["Type Theory and Mathematics"],"readme":"# Category Theory in Coq\n\nThis development encodes category theory in Coq, with the primary aim being to\nallow representation and manipulation of categorical terms, as well\nrealization of those terms in various target categories.\n\n- Versions used: [Coq](https://github.com/coq/coq/) 8.14.1, 8.15.2, 8.16+rc1.\n- Some parts depend on [Coq-Equations](https://github.com/mattam82/Coq-Equations) 1.2.4, 1.3.\n\n## Usage\n\nIt is recommended to include this library in your developments by adding the\nfollowing to your `_CoqProject` file:\n\n    -R \u003cpath to this library\u003e Category\n\nThen include the primary elements of the library using:\n\n    Require Import Category.Theory.\n\n## Library structure\n\nThis library is broken up into several major areas:\n\n  - Core `Theory`, covering topics such as categories, functors, natural\n    transformations, adjunctions, kan extensions, etc.\n\n  - Categorical `Structure`, which reveals internal structure of a category by\n    way of morphisms related to some universal property.\n\n  - Categorical `Construction`, which introduces external structure by\n    building new categories out of existing ones.\n\n  - Species of different kinds of `Functor`, `Natural.Transformation`,\n    `Adjunction` and `Kan.Extension`; for example: categories with monoidal\n    structure give rise to monoidal functors preserving this structure, which\n    in turn leads to monoidal transformations that transform functors while\n    preserving their monoidal mapping property.\n\n  - The `Instance` directory defines various categories; some of these are\n    fairly general, such as the category of preorders, applicable to any\n    `PreOrder` relation, but in general these are either not defined in terms\n    of other categories, or are sufficiently specific.\n\n  - When a concept, such as limits, can be defined using more fundamental\n    terms, that version of limits can be found in a subdirectory of the\n    derived concept, for example there is `Category.Structure.Limit` and\n    `Category.Limit.Kan.Extension`. This is done to demonstrate the\n    relationship of ideas; for example:\n    `Category.Construction.Comma.Natural.Transformation`. As a result, files\n    with the same name occur often, with the parent directory establishing\n    intent.\n\n### Programming sub-library\n\nWithin `Theory.Coq` there is now a sub-library that continues work started in\nthe [coq-haskell](https://github.com/jwiegley/coq-haskell/) library. This\nsub-library is specifically aimed at \"applied category theory\" for programmers\nin the category of Coq types and functions. The aim is to mimic the utility of\nHaskell's monad hierarchy -- but for Coq users, similar to what ext-lib\nachieves. I've also adjusted a few of my notations to more closely match\next-lib.\n\nWhere this library differs, and what is considered the main contribution of\nthis work, is that laws are not defined for these structures in the\nsub-library. Rather, the programmer establishs that her Monad is lawful by\nproving that a mapping exists from that definition into the general definition\nof monads (found in `Theory.Monad`) specialized to the category Coq. In this\nway the rest of the category-theory library is leveraged to discharge these\nproof obligations, while keeping the programmatic side as simple as possible.\n\nFor example, it is trivial to define the composition of two Applicatives. What\nis not so trivial is proving that this is truly an Applicative, based on that\nsimple definition. While this proof was done in coq-haskell, it requires a bit\nof Ltac magic to keep the size down:\n\n```\nProgram Instance Compose_ApplicativeLaws\n  `{ApplicativeLaws F} `{ApplicativeLaws G} : ApplicativeLaws (F \\o G).\nObligation 2. (* ap_composition *)\n  (* Discharge w *)\n  rewrite \u003c- ap_comp; f_equal.\n  (* Discharge v *)\n  rewrite \u003c- !ap_fmap, \u003c- ap_comp.\n  symmetry.\n  rewrite \u003c- ap_comp; f_equal.\n  (* Discharge u *)\n  apply_applicative_laws.\n  f_equal.\n  extensionality y.\n  extensionality x.\n  extensionality x0.\n  rewrite \u003c- ap_comp, ap_fmap.\n  reflexivity.\nQed.\n```\n\nWhat's ill-gotten about this proof is that it's confined to the very specific\ncase of Coq applicative endo-functors. However, there is a more general truth,\nwhich is that any two lax monoidal functors in any monoidal category also\ncompose. So why not appeal to that proof to establish that our programmatic\napplicative in Coq is lawful by construction?\n\nThis is what the new sub-library does. Since the more general proof is already\ncompleted (and is too large to paste here), one may appeal to it directly to\nestablish the desired fact:\n\n```\n(* The composition of two applicatives is itself applicative. We establish\n   this by appeal to the general proofs that:\n\n   1. every Coq functor has strength;\n   2. (also, but not needed: any two strong functors compose to a strong\n      functor; if it is a Coq functor it is known to have strength); and\n   3. any two lax monoidal functors compose to a lax monoidal functor. *)\n\nTheorem Compose_IsApplicative\n  `(HF : EndoFunctor F)\n  `(AF : @Functor.Applicative.Applicative _ _ (FromAFunctor HF))\n  `(HG : EndoFunctor G)\n  `(AG : @Functor.Applicative.Applicative _ _ (FromAFunctor HG)) :\n  IsApplicative (Compose_IsFunctor HF HG)\n    (@Compose_Applicative\n       F HF (EndoApplicative_Applicative HF AF)\n       G HG (EndoApplicative_Applicative HG AG)).\nProof.\n  construct.\n  - apply (@Compose_LaxMonoidalFunctor _ _ _ _ _ _ _ _ AF AG).\n  - apply Coq_StrongFunctor.\nQed.\n```\n\nThis proof pulls in several instances to establish that the category Coq is\ncartesian, closed, and thus closed monoidal, etc.\n\nThe hope is this will become a happy marriage of simple, useful computational\nconstructions for Coq programmers, with relevant proof results from what\ncategory theory tells us about these structures in general, such as the above\nfact concerning composition of monoidal functors.\n\n## Duality\n\nThe core theory is defined in such a way that \"the dual of the dual\" is\nevident to Coq by mere simplification (that is, \"C^op^op = C\" follows by\nreflexivity for the opposite of the opposite of categories, functors, natural\ntransformation, adjunctions, isomorphisms, etc.).\n\nFor this to be true, certain artificial constructions are necessary, such as\nrepeating the associativity law for categories in symmetric form, and likewise\nthe naturality condition of natural transformations. This repeats proof\nobligations when constructing these objects, but pays off in the ability to\navoid repetition when stating the dual of whole structures.\n\nAs a result, the definition of comonads, for example, is reduced to one line:\n\n    Definition Comonad `{M : C ⟶ C} := @Monad (C^op) (M^op).\n\nMost dual constructions are similarly defined, with the exception of `Initial`\nand `Cocartesian` structures. Although the core classes are indeed defined in\nterms of their dual construction, an alternate surface syntax and set of\ntheorems is provided (for example, using `a + b` to indicate coproducts) to\nmake life is a little less confusing for the reader. For instance, it follows\nfrom duality that `0 + X ≅ X` is just `1 × X ≅ X` in the opposite category,\nbut using separate notations makes it easier to see that these two\nisomorphisms in the *same* category are not identical. This is especially\nimportant because Coq hides implicit parameters that would usually let you\nknow duality is involved.\n\n## Design decisions\n\nSome features and choices made in this library:\n\n  - Type classes are used throughout to present concepts. When a type class\n    instance would be too general -- and thus overlap with other instances --\n    it is presented as a definition that can later be added to instance\n    resolution with `Existing Instance`.\n\n  - All definitions are in Type, so that `Prop` is not used anywhere except\n    specific category instances defined over `Prop`, such as the category\n    `Rel` with heterogeneous relations as arrows.\n\n  - No axioms are used anywhere in the core theory; they appear only at times\n    when defining specific category instances, mostly for the `Coq` category.\n\n  - Homsets are defined as computationally-relevant homsetoids (that is, using\n    `crelation`). This is done using a variant of the `Setoid` type class\n    defined for this library; likewise, the category of `Sets` is actually the\n    category of such setoids. This increases the proof work necessary to\n    establish definitions -- since preservation of the equivalence relation is\n    required at all levels -- but allows categories to be flexible in what it\n    means for two arrows to be equivalent.\n\n## Notations\n\nThere are many notations used through the library, which are chosen in an\nattempt to make complex constructions appear familiar to those reading modern\ntexts on category theory. Some of the key notations are:\n\n - `≈` is equivalence; equality is almost never used.\n - `≈[Cat]` is equivalence between arrows of some category, here `Cat`; this\n   is only needed when type inference fails because it tries to find both the\n   type of the arguments, and the type class instance for the equivalence\n - `≅` is isomorphism; apply it with `to` or `from`\n - `≊` is used specifically for isomorphisms between homsets in `Sets`\n - `iso⁻¹` also indicates the reverse direction of an isomorphism\n - `X ~\u003e Y`: a squiggly arrow between objects is a morphism\n - `X ~{C}~\u003e Y`: squiggly arrows may also specify the intended category\n - `id[C]`: many known morphisms allow specifying the intended category;\n   sometimes this is even used in the printing format\n - `C ⟶ D`: a long right arrow between categories is a functor\n - `F ⟹ G`: a long right double arrow between functors is a natural\n   transformation\n - `f ∘ g`: a small centered circle is composition of morphisms, or horizontal\n   composition generally\n - `f ∘[Cat] g`: composition can specify the intended category, as an aid to\n   type inference\n   composition generally\n - `f ○ g`: a larger hollow circle is composition of functors\n - `f ⊙ g`: a larger circle with a dot is composition of isomorphisms\n - `f ∙ g`: a solid composition dot is composition of natural\n   transformations, or vertical composition generally\n - `f ⊚ g`: a larger circle with a smaller circle is composition of\n   adjunctions\n - `([C, D])`: A pair of categories in square brackets is another way to give\n   the type of a functor, being an object of the category `Fun(C, D)`\n - `F ~{[C, D]}~\u003e G`: An arrow in a functor category is a natural\n   transformation\n - `F ⊣ G`: the turnstile is used for adjunctions\n - Cartesian categories use `△` as the `fork` operation and `×` for products\n - Cocartesian categories use `▽` as the `merge` operation and `+` for\n   coproducts\n - Closed categories use `^` for exponents and `≈\u003e` for the internal hom\n - As objects, the numbers `0` and `1` refer to initial and terminal objects\n - As categories, the numbers `0` and `1` refer to the initial and terminal\n   objects of `Cat`\n - Products of categories can be specified using `∏`, which does not require\n   pulling in the cartesian definition of `Cat`\n - Coproducts of categories can be specified using `∐`, which does not require\n   pulling in the cocartesian definition of `Cat`\n - Products of functors are given with `F ∏⟶ G`, combining product and functor\n   notations; the same for `∐⟶`\n - Comma categories of two functors are given by `(F ↓ G)`\n - Likewise, the arrow category of `C⃗`\n - Slice categories use a unicode forward slash `C̸c`, since the normal forward\n   slash is not considered an operator\n - Coslice categories use `c ̸co C`, to avoid ambiguity\n\n## Future directions\n\n### Computational Solver\n\nThere are some equivalences in category-theory that are very easily expressed\nand proven, but slow to establish in Coq using only symbolic term rewriting.\nFor example:\n\n    (f ∘ g) △ (h ∘ i) ≈ split f h ∘ g △ i\n\nThis is solved by unfolding the definition of split, and then rewriting so\nthat the fork operation (here given by `△`) absorbs the terms to its left,\nfollowed by observing the associativity of composition, and then simplify\nbased on the universal properties of products. This is repeated several times\nuntil the prove is trivially completed.\n\nAlthough this is easy to state, and even to write a tactic for, it can be\nextremely slow, especially when the types of the terms involved becomes large.\nA single rewrite can take several seconds to complete for some terms, in my\nexperience.\n\nThe goal of this solver is to reify the above equivalence in terms of its\nfundamental operations, and then, using what we know about the laws of\ncategory theory, to compute the equivalence down to an equation on indices\nbetween the reduced terms. This is called *computational reflection*, and\nencodes the fact that our solution only depends on the categorical structure\nof the terms, and not their type.\n\nThat is, an incorrectly-built structure will simply fail to solve; but since\nwe're reflecting over well-typed expressions to build the structure we pass to\nthe solver, combined with a proof of soundness for that solver, we can know\nthat solvable, well-typed, terms always give correct solutions. In this way,\nwe transfer the problem to a domain without types, only indices, solve the\nstructural problem there, and then bring the solution back to the domain of\nfull types by way of the soundness proof.\n\n### Compiling to categories\n\nWork has started in `Tools/Abstraction` for compiling monomorphic Gallina\nfunctions into \"categorical terms\" that can then be instantiated in any\nsupporting target category using Coq's type class instance resolution.\n\nThis is as a Coq implementation of an idea developed by Conal Elliott, which\nhe first implemented in and for Haskell. It is hoped that the medium of\ncategories may provide a sound means for transporting Gallina terms into other\nsyntactic domains, without relying on Coq's extraction mechanism.\n\n## License\n\nThis library is made available under the BSD-3-Clause license, a copy of which is\nincluded in the file [`LICENSE`](LICENSE). Basically: you are free to use it for any\npurpose, personal or commercial (including proprietary derivates), so long as\na copy of the license file is maintained in the derived work. Further, any\nacknowledgement referring back to this repository, while not necessary, is\ncertainly appreciated.\n\nJohn Wiegley\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjwiegley%2Fcategory-theory","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjwiegley%2Fcategory-theory","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjwiegley%2Fcategory-theory/lists"}