Skip to content

Commit

Permalink
Add CoProduct and Either (#26)
Browse files Browse the repository at this point in the history
* add coproduct and either

* address cr issues

* stylistic improvements on either as coproduct
  • Loading branch information
mstn authored and marcosh committed Jun 10, 2019
1 parent 1b4159d commit 210141f
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/CoLimits/CoProduct.lidr
@@ -0,0 +1,59 @@
\iffalse
SPDX-License-Identifier: AGPL-3.0-only

This file is part of `idris-ct` Category Theory in Idris library.

Copyright (C) 2019 Stichting Statebox <https://statebox.nl>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
\fi

> module CoLimits.CoProduct
>
> import Basic.Category
>
> %access public export
> %default total
> %auto_implicits off
>
> record CommutingMorphism
> (cat : Category)
> (a : obj cat) (b : obj cat) (carrier : obj cat) (c : obj cat)
> (inl : mor cat a carrier) (inr : mor cat b carrier)
> (f : mor cat a c) (g : mor cat b c)
> where
> constructor MkCommutingMorphism
> challenger : mor cat carrier c
> commutativityLeft : compose cat a carrier c inl challenger = f
> commutativityRight : compose cat b carrier c inr challenger = g
>
> record CoProduct
> (cat : Category)
> (a : obj cat) (b : obj cat)
> where
> constructor MkCoProduct
> carrier: obj cat
> inl: mor cat a carrier
> inr: mor cat b carrier
> exists:
> (c : obj cat)
> -> (f : mor cat a c)
> -> (g : mor cat b c)
> -> CommutingMorphism cat a b carrier c inl inr f g
> unique:
> (c : obj cat)
> -> (f : mor cat a c)
> -> (g : mor cat b c)
> -> (h : CommutingMorphism cat a b carrier c inl inr f g)
> -> challenger h = challenger (exists c f g)
81 changes: 81 additions & 0 deletions src/Idris/EitherAsCoProduct.lidr
@@ -0,0 +1,81 @@
\iffalse
SPDX-License-Identifier: AGPL-3.0-only

This file is part of `idris-ct` Category Theory in Idris library.

Copyright (C) 2019 Stichting Statebox <https://statebox.nl>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
\fi

> module Idris.EitherAsCoProduct
>
> import Idris.TypesAsCategoryExtensional
>
> import Basic.Category
> import CoLimits.CoProduct
>
> applyLeftOrRight :
> (a, b, c : Type)
> -> (f : ExtensionalTypeMorphism a c)
> -> (g : ExtensionalTypeMorphism b c)
> -> ExtensionalTypeMorphism (Either a b) c
> applyLeftOrRight a b c (MkExtensionalTypeMorphism f) (MkExtensionalTypeMorphism g) =
> (MkExtensionalTypeMorphism (either f g))
>
> leftCompose :
> (a, b, c : Type)
> -> (f : ExtensionalTypeMorphism a c)
> -> (g : ExtensionalTypeMorphism b c)
> -> extCompose a (Either a b) c (MkExtensionalTypeMorphism Left) (applyLeftOrRight a b c f g) = f
> leftCompose a b c (MkExtensionalTypeMorphism f) (MkExtensionalTypeMorphism g) = Refl
>
> rightCompose :
> (a, b, c : Type)
> -> (f : ExtensionalTypeMorphism a c)
> -> (g : ExtensionalTypeMorphism b c)
> -> extCompose b (Either a b) c (MkExtensionalTypeMorphism Right) (applyLeftOrRight a b c f g) = g
> rightCompose a b c (MkExtensionalTypeMorphism f) (MkExtensionalTypeMorphism g) = Refl
>
> applyExtWith :
> (x : a)
> -> (f : ExtensionalTypeMorphism a b)
> -> b
> applyExtWith x f = apply (func f) x
>
> unique :
> (f : ExtensionalTypeMorphism a c)
> -> (g : ExtensionalTypeMorphism b c)
> -> (h : ExtensionalTypeMorphism (Either a b) c)
> -> (commutativityLeft : extCompose a (Either a b) c (MkExtensionalTypeMorphism Left) h = f)
> -> (commutativityRight: extCompose b (Either a b) c (MkExtensionalTypeMorphism Right) h = g)
> -> h = applyLeftOrRight a b c f g
> unique (MkExtensionalTypeMorphism f)
> (MkExtensionalTypeMorphism g)
> (MkExtensionalTypeMorphism h)
> commutativityLeft
> commutativityRight =
> funExt(\x =>
> case x of
> Left l => cong {f = applyExtWith l} commutativityLeft
> Right r => cong {f = applyExtWith r} commutativityRight
> )
>
> eitherToCoProduct : (a, b : Type) -> CoProduct Idris.TypesAsCategoryExtensional.typesAsCategoryExtensional a b
> eitherToCoProduct a b = MkCoProduct
> (Either a b)
> (MkExtensionalTypeMorphism Left)
> (MkExtensionalTypeMorphism Right)
> (\c, f, g => MkCommutingMorphism (applyLeftOrRight a b c f g) (leftCompose a b c f g) (rightCompose a b c f g))
> (\c, f, g, h => unique f g (challenger h) (commutativityLeft h) (commutativityRight h))

0 comments on commit 210141f

Please sign in to comment.