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:
|
|
|
|
||| * `mapFst assoc.rightToLeft . assoc.leftToRight . assoc.leftToRight = assoc.leftToRight . mapSnd assoc.leftToRight`
|
2023-03-06 10:57:36 -05:00
|
|
|
public export
|
|
|
|
interface Bifunctor ten => Associative ten where
|
2023-03-06 16:53:56 -05:00
|
|
|
assoc : a `ten` (b `ten` c) <=> (a `ten` b) `ten` c
|
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:
|
|
|
|
||| * `mapSnd unitl.leftToRight = mapFst unitr.leftToRight . assoc.leftToRight`
|
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-06 16:53:56 -05:00
|
|
|
assoc = MkEquivalence (\(x,(y,z)) => ((x,y),z)) (\((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-06 16:53:56 -05:00
|
|
|
assoc = MkEquivalence f b
|
2023-03-06 10:57:36 -05:00
|
|
|
where
|
|
|
|
f : forall a,b,c. Either a (Either b c) -> Either (Either a b) c
|
|
|
|
f (Left x) = Left (Left x)
|
|
|
|
f (Right (Left x)) = Left (Right x)
|
|
|
|
f (Right (Right x)) = Right x
|
|
|
|
|
|
|
|
b : forall a,b,c. Either (Either a b) c -> Either a (Either b c)
|
|
|
|
b (Left (Left x)) = Left x
|
|
|
|
b (Left (Right x)) = Right (Left x)
|
|
|
|
b (Right x) = Right (Right x)
|
|
|
|
|
|
|
|
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
|