2023-03-07 22:15:08 -05:00
|
|
|
||| This module defines tensor products, which are later used to define
|
|
|
|
||| the concept of profunctor strength. The two primary tensor products
|
|
|
|
||| in `Idr` are the product (`Pair`) and the coproduct (`Either`).
|
2023-03-06 10:57:36 -05:00
|
|
|
module Data.Tensor
|
|
|
|
|
|
|
|
%default total
|
|
|
|
|
|
|
|
|
2023-03-07 22:15:08 -05:00
|
|
|
------------------------------------------------------------------------------
|
|
|
|
-- Tensor products
|
|
|
|
------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
||| A bifunctor that admits an *associator*, i.e. a bifunctor that is
|
|
|
|
||| associative up to isomorphism.
|
|
|
|
|||
|
|
|
|
||| Laws:
|
2023-03-07 22:21:54 -05:00
|
|
|
||| * `mapFst assocl . assocl . assocl = assocl . mapSnd assocl`
|
2023-03-06 10:57:36 -05:00
|
|
|
public export
|
|
|
|
interface Bifunctor ten => Associative ten where
|
2023-03-07 22:21:54 -05:00
|
|
|
assocl : a `ten` (b `ten` c) -> (a `ten` b) `ten` c
|
|
|
|
assocl = assoc.leftToRight
|
|
|
|
|
|
|
|
assocr : (a `ten` b) `ten` c -> a `ten` (b `ten` c)
|
|
|
|
assocr = assoc.rightToLeft
|
|
|
|
|
2023-03-06 16:53:56 -05:00
|
|
|
assoc : a `ten` (b `ten` c) <=> (a `ten` b) `ten` c
|
2023-03-07 22:21:54 -05:00
|
|
|
assoc = MkEquivalence assocl assocr
|
|
|
|
|
2023-03-06 10:57:36 -05:00
|
|
|
|
2023-03-07 22:15:08 -05:00
|
|
|
||| A bifunctor that admits a swap map, i.e. a bifunctor that is
|
|
|
|
||| symmetric up to isomorphism.
|
|
|
|
|||
|
|
|
|
||| The bifunctor `ten` is generally also associative.
|
2023-03-06 10:57:36 -05:00
|
|
|
public export
|
|
|
|
interface Bifunctor ten => Symmetric ten where
|
|
|
|
swap : a `ten` b -> b `ten` a
|
2023-03-06 16:53:56 -05:00
|
|
|
swap = symmetric.leftToRight
|
|
|
|
|
|
|
|
symmetric : a `ten` b <=> b `ten` a
|
|
|
|
symmetric = MkEquivalence swap swap
|
|
|
|
|
2023-03-06 10:57:36 -05:00
|
|
|
|
2023-03-07 22:15:08 -05:00
|
|
|
||| A tensor product is an associative bifunctor that has an identity element
|
|
|
|
||| up to isomorphism. Tensor products constitute the monoidal structure of a
|
|
|
|
||| monoidal category.
|
|
|
|
|||
|
|
|
|
||| Laws:
|
2023-03-07 22:21:54 -05:00
|
|
|
||| * `mapSnd unitl.leftToRight = mapFst unitr.leftToRight . assocl`
|
2023-03-06 10:57:36 -05:00
|
|
|
public export
|
|
|
|
interface Associative ten => Tensor ten i | ten where
|
2023-03-06 16:53:56 -05:00
|
|
|
unitl : i `ten` a <=> a
|
|
|
|
unitr : a `ten` i <=> a
|
2023-03-06 10:57:36 -05:00
|
|
|
|
|
|
|
|
2023-03-07 22:15:08 -05:00
|
|
|
------------------------------------------------------------------------------
|
|
|
|
-- Cartesian monoidal structure
|
|
|
|
------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
2023-03-06 10:57:36 -05:00
|
|
|
export
|
|
|
|
Associative Pair where
|
2023-03-07 22:21:54 -05:00
|
|
|
assocl (x,(y,z)) = ((x,y),z)
|
|
|
|
assocr ((x,y),z) = (x,(y,z))
|
2023-03-06 10:57:36 -05:00
|
|
|
|
|
|
|
export
|
|
|
|
Symmetric Pair where
|
|
|
|
swap = Builtin.swap
|
|
|
|
|
|
|
|
export
|
|
|
|
Tensor Pair () where
|
2023-03-06 16:53:56 -05:00
|
|
|
unitl = MkEquivalence snd ((),)
|
|
|
|
unitr = MkEquivalence fst (,())
|
2023-03-06 10:57:36 -05:00
|
|
|
|
|
|
|
|
2023-03-07 22:15:08 -05:00
|
|
|
------------------------------------------------------------------------------
|
|
|
|
-- Cocartesian monoidal structure
|
|
|
|
------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
2023-03-06 10:57:36 -05:00
|
|
|
export
|
|
|
|
Associative Either where
|
2023-03-07 22:21:54 -05:00
|
|
|
assocl (Left x) = Left (Left x)
|
|
|
|
assocl (Right (Left x)) = Left (Right x)
|
|
|
|
assocl (Right (Right x)) = Right x
|
|
|
|
|
|
|
|
assocr (Left (Left x)) = Left x
|
|
|
|
assocr (Left (Right x)) = Right (Left x)
|
|
|
|
assocr (Right x) = Right (Right x)
|
2023-03-06 10:57:36 -05:00
|
|
|
|
|
|
|
export
|
|
|
|
Symmetric Either where
|
|
|
|
swap = either Right Left
|
|
|
|
|
2023-03-06 10:59:05 -05:00
|
|
|
export
|
2023-03-06 10:57:36 -05:00
|
|
|
Tensor Either Void where
|
2023-03-06 16:53:56 -05:00
|
|
|
unitl = MkEquivalence (either absurd id) Right
|
|
|
|
unitr = MkEquivalence (either id absurd) Left
|