{-# OPTIONS --without-K --safe #-} open import Categories.Category using (Category) open import Category.Dagger.Semiadditive using (SemiadditiveDagger) open import Level using (Level) module Data.WiringDiagram.Core {o โ„“ e : Level} {๐’ž : Category o โ„“ e} (S : SemiadditiveDagger ๐’ž) where open import Relation.Binary using (IsEquivalence) open Category ๐’ž using (Obj; _โˆ˜_; _โ‡’_; id; _โ‰ˆ_; module Equiv) open SemiadditiveDagger S using (_โŠ•_; _ร—โ‚_; ฯ€โ‚; ฯ€โ‚‚; ฮ”; โˆ‡; _โ€ ; โŸจ_,_โŸฉ) -- A "Box" is a pair of objects from the underlying category, -- representing input and output ports -- +-----------+ -- | | -- Aแตข A Aโ‚’ -- | | -- +-----------+ record Box : Set o where constructor _โ–ก_ field แตข : Obj โ‚’ : Obj infix 4 _โ–ก_ -- A Wiring Diagram between two boxes -- is a pair of morphisms from the underlying category -- (which can be thought of as generalized relations): -- 1. A relation from the output of the inner box -- plus the input of the outer box to the input of -- the inner box -- 2. A relation from the output of the inner box -- to the output of the outer box -- This choice of definition gives a way to represent -- feedback from output to input. -- -- outer box (B) -- +--------------------------------+ -- | | -- | /-------------------\ | -- | | | | -- | | +-------+ | | -- | \----| | | | -- | Aโ‚’ โŠ• Bแตข | inner |------+------| -- | โ‡’ Aแตข | box | Aโ‚’ โ‡’ Bโ‚’ | -- |----------| (A) | output | -- | input | | relation | -- | relation +-------+ | -- | | -- +--------------------------------+ record WiringDiagram (A B : Box) : Set โ„“ where constructor _โงˆ_ private module A = Box A module B = Box B field input : A.โ‚’ โŠ• B.แตข โ‡’ A.แตข output : A.โ‚’ โ‡’ B.โ‚’ infix 4 _โงˆ_ -- Two wiring diagrams are equivalent when their -- input and output relations are equivalent as -- morphism in the underlying category. record _โ‰ˆ-โงˆ_ {A B : Box} (f g : WiringDiagram A B) : Set e where constructor _โŒธ_ private module f = WiringDiagram f module g = WiringDiagram g field โ‰ˆi : f.input โ‰ˆ g.input โ‰ˆo : f.output โ‰ˆ g.output infix 4 _โ‰ˆ-โงˆ_ -- Equivalence of boxes is a legitimate equivalence relation module _ {A B : Box} where open Equiv โ‰ˆ-refl : {x : WiringDiagram A B} โ†’ x โ‰ˆ-โงˆ x โ‰ˆ-refl = refl โŒธ refl โ‰ˆ-sym : {x y : WiringDiagram A B} โ†’ x โ‰ˆ-โงˆ y โ†’ y โ‰ˆ-โงˆ x โ‰ˆ-sym (โ‰ˆi โŒธ โ‰ˆo) = sym โ‰ˆi โŒธ sym โ‰ˆo โ‰ˆ-trans : {x y z : WiringDiagram A B} โ†’ x โ‰ˆ-โงˆ y โ†’ y โ‰ˆ-โงˆ z โ†’ x โ‰ˆ-โงˆ z โ‰ˆ-trans (โ‰ˆiโ‚ โŒธ โ‰ˆoโ‚) (โ‰ˆiโ‚‚ โŒธ โ‰ˆoโ‚‚) = trans โ‰ˆiโ‚ โ‰ˆiโ‚‚ โŒธ trans โ‰ˆoโ‚ โ‰ˆoโ‚‚ โ‰ˆ-isEquiv : IsEquivalence (_โ‰ˆ-โงˆ_ {A} {B}) โ‰ˆ-isEquiv = record { refl = โ‰ˆ-refl ; sym = โ‰ˆ-sym ; trans = โ‰ˆ-trans } -- The identity wiring diagram id-โงˆ : {A : Box} โ†’ WiringDiagram A A id-โงˆ = ฯ€โ‚‚ โงˆ id -- Composition of wiring diagrams _โŒป_ : {A B C : Box} โ†’ WiringDiagram B C โ†’ WiringDiagram A B โ†’ WiringDiagram A C _โŒป_ {Aแตข โ–ก Aโ‚’} {Bแตข โ–ก Bโ‚’} {Cแตข โ–ก Cโ‚’} (f โงˆ g) (h โงˆ i) = hfi โงˆ g โˆ˜ i where hfi : Aโ‚’ โŠ• Cแตข โ‡’ Aแตข hfi = h โˆ˜ โŸจ ฯ€โ‚ , f โˆ˜ i ร—โ‚ id โŸฉ infixr 9 _โŒป_ -- Special wiring diagrams loop : {A : Obj} โ†’ WiringDiagram (A โ–ก A) (A โ–ก A) loop = โˆ‡ โงˆ id pulsh : {A B C D : Obj} โ†’ A โ‡’ B โ†’ C โ‡’ D โ†’ WiringDiagram (B โ–ก C) (A โ–ก D) pulsh f g = f โˆ˜ ฯ€โ‚‚ โงˆ g push : {A B : Obj} โ†’ A โ‡’ B โ†’ WiringDiagram (A โ–ก A) (B โ–ก B) push f = pulsh (f โ€ ) f pull : {A B : Obj} โ†’ A โ‡’ B โ†’ WiringDiagram (B โ–ก B) (A โ–ก A) pull f = pulsh f (f โ€ ) merge : {A B : Obj} โ†’ A โ‡’ B โ†’ WiringDiagram (A โ–ก A) (B โ–ก B) merge f = f โ€  โˆ˜ โˆ‡ โˆ˜ f ร—โ‚ id โงˆ f split : {A B : Obj} โ†’ A โ‡’ B โ†’ WiringDiagram (B โ–ก B) (A โ–ก A) split f = โˆ‡ โˆ˜ id ร—โ‚ f โงˆ f โ€