-
Notifications
You must be signed in to change notification settings - Fork 6
/
FuncLec.agda
387 lines (295 loc) · 9.67 KB
/
FuncLec.agda
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
module FuncLec where
open import Ex1Prelude
_=^=_ : {S T : Set}(f g : S -> T) -> Set
f =^= g = (s : _) -> f s == g s
infixl 2 _=^=_
map : {S T : Set} -> (S -> T) -> (List S -> List T)
map f [] = []
map f (s :> ss) = f s :> map f ss
mapId : {S : Set} -> map (id {S}) =^= id {List S}
mapId [] = refl
mapId (x :> ss) rewrite mapId ss = refl
mapComp : {R S T : Set}(f : S -> T)(g : R -> S) ->
map f o map g =^= map (f o g)
mapComp f g [] = refl
mapComp f g (x :> ss) rewrite mapComp f g ss = refl
_>=_ : Nat -> Nat -> Set
m >= zero = One
zero >= suc n = Zero
suc m >= suc n = m >= n
geRefl : (n : Nat) -> n >= n
geRefl zero = <>
geRefl (suc x) = geRefl x
geTrans : (l m n : Nat) -> m >= n -> l >= m -> l >= n
geTrans l zero zero mn lm = <>
geTrans zero zero (suc x) mn lm = mn
geTrans (suc x) zero (suc zero) mn lm = <>
geTrans (suc x) zero (suc (suc x₁)) mn lm = geTrans x zero (suc x₁) mn <>
geTrans l (suc x) zero mn lm = <>
geTrans zero (suc x) (suc x₁) mn lm = lm
geTrans (suc x) (suc x₁) (suc x₂) mn lm = geTrans x x₁ x₂ mn lm
data Vec (X : Set) : Nat -> Set where
[] : Vec X zero
_,_ : forall {n} -> X -> Vec X n -> Vec X (suc n)
take : {X : Set}(m n : Nat) -> m >= n -> (Vec X m -> Vec X n)
take m zero mn xs = []
take .0 (suc n) () []
take ._ (suc n) mn (x , xs) = x , take _ n mn xs
data Tree (X : Set) : Set where
leaf : Tree X
node : Tree X -> X -> Tree X -> Tree X
treeMap : {S T : Set} -> (S -> T) -> (Tree S -> Tree T)
treeMap f leaf = leaf
treeMap f (node l s r) = node (treeMap f l) (f s) (treeMap f r)
From : Set -> Set -> Set
From A X = A -> X
fromMap : {A S T : Set} -> (S -> T) -> ((From A) S -> (From A) T)
fromMap f g = f o g
{-
To : Set -> Set -> Set
To B X = X -> B
toMap : {B S T : Set} -> (S -> T) -> ((To B) S -> (To B) T)
toMap {B}{S}{T} f g = {!!}
-}
NotNot : Set -> Set
NotNot X = (X -> Zero) -> Zero
nnMap : {S T : Set} -> (S -> T) -> (NotNot S -> NotNot T)
nnMap f nns = \ nt -> nns (\ s -> nt (f s))
good : Zero -> One
good ()
{-
bad : One -> Zero
bad = toMap good id
-}
Id : Set -> Set
Id X = X
idMap : {S T : Set} -> (S -> T) -> (Id S -> Id T)
idMap {S}{T} = id
Product : (F G : Set -> Set) -> Set -> Set
Product F G X = F X /*/ G X
prodMap : {F G : Set -> Set} ->
({S T : Set} -> (S -> T) -> (F S -> F T)) ->
({S T : Set} -> (S -> T) -> (G S -> G T)) ->
({S T : Set} -> (S -> T) -> ((Product F G) S -> (Product F G) T))
prodMap fmap gmap h (fs , gs) = fmap h fs , gmap h gs
ex1 : Product Id Id Nat
ex1 = 6 , 7
ex2 : Product Id Id Two
ex2 = prodMap idMap idMap (\ n -> n <= 6) ex1
Sum : (F G : Set -> Set) -> Set -> Set
Sum F G X = F X /+/ G X
sumMap : {F G : Set -> Set} ->
({S T : Set} -> (S -> T) -> (F S -> F T)) ->
({S T : Set} -> (S -> T) -> (G S -> G T)) ->
({S T : Set} -> (S -> T) -> ((Sum F G) S -> (Sum F G) T))
sumMap fmap gmap h (inl fs) = inl (fmap h fs)
sumMap fmap gmap h (inr gs) = inr (gmap h gs)
ex3 : Sum (Product Id Id) Id Two
ex3 = inl (tt , ff)
K : Set -> Set -> Set
K A X = A
kMap : {A S T : Set} -> (S -> T) -> (K A S -> K A T)
kMap f a = a
Mystery : Set -> Set
Mystery = Sum (K One) Id
mystery : Mystery Two
mystery = inl <>
data Kit : Set1 where
kK : Set -> Kit
kId : Kit
_k+_ : Kit -> Kit -> Kit
_k*_ : Kit -> Kit -> Kit
kFun : Kit -> (Set -> Set)
kFun (kK A) X = A
kFun kId X = X
kFun (f k+ g) X = kFun f X /+/ kFun g X
kFun (f k* g) X = kFun f X /*/ kFun g X
kitMap : (k : Kit){S T : Set} -> (S -> T) -> kFun k S -> kFun k T
kitMap (kK A) h a = a
kitMap kId h s = h s
kitMap (f k+ g) h (inl fs) = inl (kitMap f h fs)
kitMap (f k+ g) h (inr gs) = inr (kitMap g h gs)
kitMap (f k* g) h (fs , gs) = kitMap f h fs , kitMap g h gs
data Data (k : Kit) : Set where
[_] : kFun k (Data k) -> Data k
fold : (k : Kit){X : Set} -> (kFun k X -> X) -> Data k -> X
fold k {X} f [ kd ] = f (kitMapFold k kd) where
kitMapFold : (j : Kit) -> kFun j (Data k) -> kFun j X
kitMapFold (kK A) a = a
kitMapFold kId s = fold k f s
kitMapFold (f k+ g) (inl fs) = inl (kitMapFold f fs)
kitMapFold (f k+ g) (inr gs) = inr (kitMapFold g gs)
kitMapFold (f k* g) (fs , gs) = kitMapFold f fs , kitMapFold g gs
kMaybe : Kit
kMaybe = kK One k+ kId
pattern ze = inl <>
pattern su n = inr n
NAT = Data kMaybe
exNat : NAT
exNat = [ su [ su [ ze ] ] ]
_+'_ : NAT -> NAT -> NAT
x +' y = fold kMaybe p x where
p : kFun kMaybe NAT -> NAT
p ze = y
p (su x+y) = [ su x+y ]
{-
[ ze ] +' y = y
[ su x ] +' y = [ su (x +' y) ]
-}
kT : Kit
kT = kK One k+ (kId k* kId)
exTree : Data kT
exTree = [ inl <> ]
FreeMo : Kit -> Set -> Set
FreeMo k X = Data (k k+ kK X)
return : (k : Kit) -> {A : Set} -> A -> FreeMo k A
return k a = [ inr a ]
bind : (k : Kit){A B : Set} ->
FreeMo k A ->
(A -> FreeMo k B) ->
FreeMo k B
bind k {A} ma a2mb = fold (k k+ kK A)
(\ { (inl kb) -> [ inl kb ]
; (inr a) -> a2mb a
}) ma
Error : Set -> Set -> Set
Error E = FreeMo (kK E)
kBitWrite : Kit
kBitWrite = kK Two k* kId
kBitRead : Kit
kBitRead = kId k* kId
RWBit : Set -> Set
RWBit = FreeMo (kBitWrite k+ kBitRead)
run : {X : Set} -> RWBit X ->
List Two -> List Two /*/ Error One X
run [ inl (inl (b , p)) ] bs with run p bs
run [ inl (inl (b , p)) ] bs | bs' , ex
= (b :> bs') , ex
run [ inl (inr (tp , fp)) ] [] = [] , [ inl <> ]
run [ inl (inr (tp , fp)) ] (tt :> bs)
= run tp bs
run [ inl (inr (tp , fp)) ] (ff :> bs)
= run fp bs
run [ inr x ] bs = [] , [ inr x ]
record Container : Set1 where
constructor _<!_
field
Command : Set -- aka Shape
Response : Command -> Set -- aka Position
open Container public
record Sigma (S : Set)(T : S -> Set) : Set where
constructor _,_
field
fst : S
snd : T fst
open Sigma public
_*'_ : Set -> Set -> Set
S *' T = Sigma S \ _ -> T
_+s_ : Set -> Set -> Set
S +s T = Sigma Two \ { tt -> S ; ff -> T }
[[_]] : Container -> Set -> Set
[[ C <! R ]] Y = Sigma C (\ c -> R c -> Y)
cmap : (F : Container) -> {S T : Set} -> (S -> T) -> [[ F ]] S -> [[ F ]] T
cmap F f (c , k) = c , (\ r -> f (k r))
data GenMo (F : Container)
(X : Set) : Set where
ret : X -> GenMo F X
<_> : [[ F ]] (GenMo F X) -> GenMo F X
_>>=_ : {F : Container}
{A B : Set} -> GenMo F A -> (A -> GenMo F B)
-> GenMo F B
ret a >>= f = f a
< c , k > >>= f = < c , (\ r -> k r >>= f) >
data Fail : Set where
fail : Fail
FailR : Fail -> Set
FailR fail = Zero
Maybe : Container
Maybe = Fail <! FailR
! : {F : Container} ->
(c : Command F) -> GenMo F (Response F c)
! c = < c , ret >
_c+_ : Container -> Container -> Container
(C0 <! R0) c+ (C1 <! R1) = (C0 /+/ C1) <! (\ { (inl c0) -> R0 c0 ; (inr c1) -> R1 c1 })
inC : {F G : Container}{X : Set} -> [[ F ]] X /+/ [[ G ]] X -> [[ F c+ G ]] X
inC (inl (fc , fk)) = inl fc , fk
inC (inr (gc , gk)) = inr gc , gk
_c*_ : Container -> Container -> Container
(C0 <! R0) c* (C1 <! R1) = (C0 /*/ C1) <! (\ { (c0 , c1) -> R0 c0 /+/ R1 c1 })
pairC : {F G : Container}{X : Set} -> [[ F ]] X /*/ [[ G ]] X -> [[ F c* G ]] X
pairC ((fc , fk) , (gc , gk))
= (fc , gc) , (\ { (inl fr) -> fk fr ; (inr gr) -> gk gr })
kContainer : Kit -> Container
kContainer (kK A) = A <! (\ _ -> Zero)
kContainer kId = One <! (\ _ -> One)
kContainer (k k+ k') = kContainer k c+ kContainer k'
kContainer (k k* k') = kContainer k c* kContainer k'
tail : {X : Set} -> List X -> GenMo Maybe (List X)
tail [] = ! fail >>= (\ ())
tail (x :> xs) = ret xs
try : {X : Set} -> GenMo Maybe X -> X -> X
try (ret x) _ = x
try < fail , k > x = x
data State (S : Set) : Set where
get : State S
put : S -> State S
StateR : {S : Set} -> State S -> Set
StateR {S} get = S
StateR (put x) = One
StateC : Set -> Container
StateC S = State S <! StateR
c++ : GenMo (StateC Nat) Nat
c++ = ! get >>= \ c -> ! (put (suc c)) >>= \ _ -> ret c
runState : {S X : Set} -> GenMo (StateC S) X ->
S -> X /*/ S
runState (ret x) s = x , s
runState < get , k > s = runState (k s) s
runState < put s' , k > s = runState (k <>) s'
test : (Nat /*/ Nat) /*/ Nat
test = runState (c++ >>= \ x -> c++ >>= \ y -> ret (x , y)) 41
data Choose : Set where
choose : Choose
ChooseR : Choose -> Set
ChooseR choose = Two
ChooseC : Container
ChooseC = Choose <! ChooseR
truey : {X : Set} -> GenMo ChooseC X -> X
truey (ret x) = x
truey < choose , k > = truey (k tt)
streamy : {X : Set} ->
GenMo ChooseC X -> (Nat -> Two) -> X
streamy (ret x) s = x
streamy < choose , k > s = streamy (k (s zero)) (s o suc)
_++_ : {X : Set} -> List X -> List X -> List X
[] ++ ys = ys
x :> xs ++ ys = x :> (xs ++ ys)
infixr 3 _++_
pokemon : {X : Set} -> GenMo ChooseC X -> List X
pokemon (ret x) = x :> []
pokemon < choose , k > = pokemon (k tt) ++ pokemon (k ff)
FailOrChoose : Container
FailOrChoose = Maybe c+ ChooseC
answers : {X : Set} -> GenMo FailOrChoose X -> List X
answers (ret x) = x :> []
answers < inl fail , k > = []
answers < inr choose , k > = answers (k tt) ++ answers (k ff)
data Sender (X : Set) : Set where
send : X -> Sender X
SenderC : Set -> Container
SenderC X = Sender X <! \ _ -> One
data Receiver : Set where
receive : Receiver
ReceiverC : Set -> Container
ReceiverC X = Receiver <! \ _ -> X
TransducerC : Set -> Container
TransducerC X = ReceiverC X c+ SenderC X
FlakyTransducerC : Set -> Container
FlakyTransducerC X = Maybe c+ (ReceiverC X c+ SenderC X)
pipe : {X Y : Set} -> GenMo (TransducerC X) One -> GenMo (TransducerC X) Y ->
GenMo (FlakyTransducerC X) Y
pipe (ret <>) (ret y) = ret y
pipe (ret <>) < inl receive , k > = < inl fail , (\ ()) >
pipe < inl receive , k > q = < inr (inl receive) , (\ x -> pipe (k x) q) >
pipe < inr (send x) , k > (ret y) = ret y
pipe < inr (send x) , k > < inl receive , j > = pipe (k <>) (j x)
pipe p < inr (send x) , k > = < inr (inr (send x)) , (\ _ -> pipe p (k <>)) >