aboutsummaryrefslogtreecommitdiff
path: root/Data/WiringDiagram/Core.agda
blob: 76ab70acf3c6cfbfdabc3beaf40ef4845bf31c0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
{-# 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 โ€