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 โ
|