Other Agda modules in this section (76)

Complete compiler-highlighted source. Follow a symbol to its declaration.

Corresponding book passages
# Composition and application

`mapComp` is the composition functor from `Map D E Γ— Map C D` to
`Map C E`, constructed by currying double evaluation. `composeTerm g f`
applies this functor to two mapping terms with a common parameter.
Thus `mapComp` names the operation itself, while `composeTerm` applies it.

Similarly, `applyTerm f x` evaluates a mapping term on an object term by
pairing them and applying evaluation. Both constructions accept terms
with any common parameter category.

```agda
{-# OPTIONS --safe --without-K #-}
open import Agda.Primitive using (Level)
open import SCT.VolumeI.Chapter01.Theory using (Theory)
import SCT.VolumeI.Chapter01.Section04.Setup as Setup
import SCT.VolumeI.Chapter01.Section04.MappingAnimae as MappingAnimae

module SCT.VolumeI.Chapter01.Section04.Composition
  {c m a : Level} (𝒯 : Theory c m a)
  (M : MappingAnimae.MappingAnimae 𝒯) where

open Setup 𝒯
open MappingAnimae.MappingAnimae M

applyTerm : {Ξ“ C D : CAT} β†’ MAP Ξ“ (Map C D) β†’ MAP Ξ“ C β†’ MAP Ξ“ D
applyTerm f x = mapEval ∘ pair f x

doubleEvaluation : {C D E : CAT} β†’ MAP ((Map D E Γ— Map C D) Γ— C) E
doubleEvaluation = applyTerm (pr₁ ∘ pr₁) (applyTerm (prβ‚‚ ∘ pr₁) prβ‚‚)

mapComp : {C D E : CAT} β†’ MAP (Map D E Γ— Map C D) (Map C E)
mapComp {C} {D} {E} =
  mapCurry (product-isAn (map-isAn D E) (map-isAn C D)) doubleEvaluation

mapComp-Ξ² : {C D E : CAT} β†’ (mapUncurry (mapComp {C} {D} {E})) =₁ doubleEvaluation
mapComp-Ξ² {C} {D} {E} =
  mapCurry-Ξ² (product-isAn (map-isAn D E) (map-isAn C D)) doubleEvaluation

mapId : (C : CAT) β†’ Obj-abs (Map C C)
mapId C = mapCurry one-isAn prβ‚‚

mapId-Ξ² : (C : CAT) β†’ (mapUncurry (mapId C)) =₁ prβ‚‚
mapId-Ξ² C = mapCurry-Ξ² one-isAn prβ‚‚

composeTerm : {Ξ“ C D E : CAT}
  β†’ MAP Ξ“ (Map D E) β†’ MAP Ξ“ (Map C D) β†’ MAP Ξ“ (Map C E)
composeTerm g f = mapComp ∘ pair g f

identityTerm : {Ξ“ : CAT} (C : CAT) β†’ MAP Ξ“ (Map C C)
identityTerm C = const (mapId C)

applyTerm-cong : {Ξ“ C D : CAT} {f g : MAP Ξ“ (Map C D)} {x y : MAP Ξ“ C}
  β†’ f =₁ g β†’ x =₁ y β†’ (applyTerm f x) =₁ (applyTerm g y)
applyTerm-cong Ξ± Ξ² = mapEval ◁ pair-cong Ξ± Ξ²

applyTerm-pre : {Ξ“ Ξ” C D : CAT} (f : MAP Ξ“ (Map C D)) (x : MAP Ξ“ C) (Οƒ : MAP Ξ” Ξ“)
  β†’ (applyTerm f x ∘ Οƒ) =₁ (applyTerm (f ∘ Οƒ) (x ∘ Οƒ))
applyTerm-pre f x Οƒ = (mapEval ◁ pair-pre f x Οƒ) βˆ™ comp-assoc Οƒ (pair f x) mapEval

composeTerm-cong : {Ξ“ C D E : CAT}
  {g gβ€² : MAP Ξ“ (Map D E)} {f fβ€² : MAP Ξ“ (Map C D)}
  β†’ g =₁ gβ€² β†’ f =₁ fβ€² β†’ (composeTerm g f) =₁ (composeTerm gβ€² fβ€²)
composeTerm-cong Ξ± Ξ² = mapComp ◁ pair-cong Ξ± Ξ²

composeTerm-pre : {Ξ“ Ξ” C D E : CAT}
  (g : MAP Ξ“ (Map D E)) (f : MAP Ξ“ (Map C D)) (Οƒ : MAP Ξ” Ξ“)
  β†’ (composeTerm g f ∘ Οƒ) =₁ (composeTerm (g ∘ Οƒ) (f ∘ Οƒ))
composeTerm-pre g f Οƒ = (mapComp ◁ pair-pre g f Οƒ) βˆ™ comp-assoc Οƒ (pair g f) mapComp
```

The next comparisons spell out evaluation at a specified parameter and
argument. They use the product beta comparisons and the external associator.

```agda
productMap-pair : {Ξ“ A B C D : CAT}
  (f : MAP A B) (g : MAP C D) (p : MAP Ξ“ A) (q : MAP Ξ“ C)
  β†’ (productMap f g ∘ pair p q) =₁ (pair (f ∘ p) (g ∘ q))
productMap-pair f g p q = pair-cong
  ((f ◁ pair-β₁ p q) βˆ™ comp-assoc (pair p q) pr₁ f)
  ((g ◁ pair-Ξ²β‚‚ p q) βˆ™ comp-assoc (pair p q) prβ‚‚ g)
  βˆ™ pair-pre (f ∘ pr₁) (g ∘ prβ‚‚) (pair p q)

mapUncurry-at : {Ξ“ X C D : CAT}
  (f : MAP X (Map C D)) (p : MAP Ξ“ X) (x : MAP Ξ“ C)
  β†’ (mapUncurry f ∘ pair p x) =₁ (applyTerm (f ∘ p) x)
mapUncurry-at {C = C} f p x =
  (mapEval ◁ (pair-cong (idIso (f ∘ p)) (comp-unitΛ‘ x) βˆ™
    productMap-pair f (id C) p x)) βˆ™
    comp-assoc (pair p x) (productMap f (id C)) mapEval

mapUncurry-as-apply : {Ξ“ C D : CAT} (f : MAP Ξ“ (Map C D))
  β†’ (mapUncurry f) =₁ (applyTerm (f ∘ pr₁) prβ‚‚)
mapUncurry-as-apply f = mapEval ◁ pair-cong (idIso (f ∘ pr₁)) (comp-unitΛ‘ prβ‚‚)

apply-compose : {Ξ“ C D E : CAT}
  (g : MAP Ξ“ (Map D E)) (f : MAP Ξ“ (Map C D)) (x : MAP Ξ“ C)
  β†’ (applyTerm (composeTerm g f) x) =₁ (applyTerm g (applyTerm f x))
apply-compose g f x =
  let point = pair (pair g f) x
      first = pair-β₁ g f βˆ™
        ((pr₁ ◁ pair-β₁ (pair g f) x) βˆ™ comp-assoc point pr₁ pr₁)
      second = pair-Ξ²β‚‚ g f βˆ™
        ((prβ‚‚ ◁ pair-β₁ (pair g f) x) βˆ™ comp-assoc point pr₁ prβ‚‚)
      third = pair-Ξ²β‚‚ (pair g f) x
      inner = applyTerm-cong second third βˆ™ applyTerm-pre (prβ‚‚ ∘ pr₁) prβ‚‚ point
      outer = applyTerm-cong first inner βˆ™
        applyTerm-pre (pr₁ ∘ pr₁) (applyTerm (prβ‚‚ ∘ pr₁) prβ‚‚) point
  in outer βˆ™ ((mapComp-Ξ² β–· point) βˆ™ (mapUncurry-at mapComp (pair g f) x) ⁻¹)

apply-identity : {Ξ“ C : CAT} (x : MAP Ξ“ C)
  β†’ (applyTerm (identityTerm C) x) =₁ x
apply-identity {Ξ“} {C} x = pair-Ξ²β‚‚ (terminate Ξ“) x βˆ™
  ((mapId-Ξ² C β–· pair (terminate Ξ“) x) βˆ™
    (mapUncurry-at (mapId C) (terminate Ξ“) x) ⁻¹)
```