-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathOne.agda
361 lines (271 loc) · 12.1 KB
/
One.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
{-# OPTIONS --allow-unsolved-metas #-}
module Exercise.One where
open import Lib.Basics
open import Lib.Nat
------------------------------------------------------------------------------
-- ORDER-PRESERVING EMBEDDINGS (or "thinnings" for short)
------------------------------------------------------------------------------
-- The type xs <: ys represents the possible order-preserving
-- embeddings from xs to ys. That means ys is generated by
-- inserting more stuff anywhere in xs, i.e. "thinning" xs.
data _<:_ {X : Set} : List X -> List X -> Set where
o' : forall {x ys zs} -> ys <: zs -> ys <: x ,- zs -- insert new
os : forall {x ys zs} -> ys <: zs -> x ,- ys <: x ,- zs -- keep old
oz : [] <: [] -- done!
infix 50 _<:_
-- You can also think of xs <: ys as the ways of selecting
-- elements xs from ys, with
-- o' meaning "drop the head",
-- os meaning "take the head",
-- oz meaning "end of list".
-- You can also see a thinning in xs <: ys as a vector of bits
-- telling whether each element position in ys is connected
-- from an element position in xs.
-- 2 ,- 4 ,- []
-- o' (o' (os (o' (os oz))))
-- 0 ,- 1 ,- 2 ,- 3 ,- 4 ,- []
------------------------------------------------------------------------------
-- Exploration (for comprehension rather than credit)
------------------------------------------------------------------------------
-- Lists of elements of One are a lot like numbers
num : Nat -> List One
num zero = []
num (suc n) = <> ,- num n
-- Using C-c C-a with -l and -s options, generate exhaustive lists of
-- thinnings with the following types.
pick0from4 : List (num 0 <: num 4)
pick0from4 = {!!}
pick1from4 : List (num 1 <: num 4)
pick1from4 = {!!}
pick2from4 : List (num 2 <: num 4)
pick2from4 = {!!}
pick3from4 : List (num 3 <: num 4)
pick3from4 = {!!}
pick4from4 : List (num 4 <: num 4)
pick4from4 = {!!}
-- But with more interesting elements, we have fewer options, sometimes.
thinOdds : List (1 ,- 3 ,- 5 ,- [] <: 0 ,- 1 ,- 2 ,- 3 ,- 4 ,- 5 ,- 6 ,- [])
thinOdds = {!!}
------------------------------------------------------------------------------
-- 1.1 Categorical Structure
------------------------------------------------------------------------------
-- Construct the identity thinning from any list to itself.
oi : forall {X}{xs : List X} -> xs <: xs
oi = {!!}
-- Give composition for thinnings. Minimize the number of cases.
_-<-_ : forall {X}{xs ys zs : List X} -> xs <: ys -> ys <: zs -> xs <: zs
th -<- ph = {!!}
infixl 40 _-<-_
-- Prove the following laws. Minimize the number of cases (which will
-- depend on your definition of _-<-_).
oi-<- : forall {X}{xs ys : List X}(ph : xs <: ys) -> oi -<- ph == ph
oi-<- ph = {!!}
_-<-oi : forall {X}{xs ys : List X}(th : xs <: ys) -> th -<- oi == th
th -<-oi = {!!}
assoc-<- : forall {X}{ws xs ys zs : List X}
(th0 : ws <: xs)(th1 : xs <: ys)(th2 : ys <: zs) ->
(th0 -<- th1) -<- th2 == th0 -<- (th1 -<- th2)
assoc-<- th0 th1 th2 = {!!}
------------------------------------------------------------------------------
-- 1.2 Emptiness
------------------------------------------------------------------------------
-- Show that the empty list embeds into all lists in a unique way.
oe : forall {X}{xs : List X} -> [] <: xs
oe = {!!}
oe-unique : forall {X}{xs : List X}(th : [] <: xs) -> th == oe
oe-unique th = {!!}
------------------------------------------------------------------------------
-- 1.3 Antisymmetry
------------------------------------------------------------------------------
-- Show that if two lists are mutually embeddable, they are equal
-- and the embeddings are the identity.
antisym : forall {X}{xs ys : List X}
(th : xs <: ys)(ph : ys <: xs) ->
Sg (xs == ys) \
{ refl -> th == oi * ph == oi }
antisym th ph = {!!}
-- Deduce that oi is unique.
oi-unique : forall {X}{xs : List X}(th : xs <: xs) -> th == oi
oi-unique th = {!!}
------------------------------------------------------------------------------
-- 1.4 Thinnings as selections
------------------------------------------------------------------------------
-- We can use the "selection" interpretation of thinnings to act
-- on data indexed by lists.
-- The type All P ys has elements of type P y for each y in ys.
-- If xs <: ys, show that we can get P x for each x in xs.
select : forall {X}{xs ys : List X}{P : X -> Set} ->
xs <: ys -> All P ys -> All P xs
select th pys = {!!}
-- Now prove the following laws relating to selecting by the
-- identity and composition.
select-oi : forall {X}{xs : List X}{P : X -> Set} -> (pxs : All P xs) ->
select oi pxs == pxs
select-oi pxs = {!!}
select-<- : forall {X}{xs ys zs : List X}{P : X -> Set} ->
(th : xs <: ys)(ph : ys <: zs) -> (pzs : All P zs) ->
select (th -<- ph) pzs == select th (select ph pzs)
select-<- th ph pzs = {!!}
------------------------------------------------------------------------------
-- 1.5 Splittings
------------------------------------------------------------------------------
-- If we have two thinnings,
-- th : xs <: zs
-- ph : ys <: zs
-- we can say what it means for th and ph to *split* zs:
-- every element position in zs is connected from either
-- a position in xs or from a position in ys, but *not both*.
data Splitting {X : Set} : {xs ys zs : List X}
(th : xs <: zs)(ph : ys <: zs)
-> Set where
split's : forall {w xs ys zs}{th : xs <: zs}{ph : ys <: zs} ->
Splitting th ph ->
Splitting {zs = w ,- _} (o' th) (os ph)
splits' : forall {w xs ys zs}{th : xs <: zs}{ph : ys <: zs} ->
Splitting th ph ->
Splitting {zs = w ,- _} (os th) (o' ph)
splitzz : Splitting oz oz
-- Show that if we know how xs <: zs, we can find a splitting of zs by
-- computing...
thinSplit : {X : Set}{xs zs : List X}(th : xs <: zs) ->
Sg (List X) \ ys -> -- ...what wasn't from xs...
Sg (ys <: zs) \ ph -> -- ...but was in zs...
Splitting th ph -- ...hence forms a splitting.
thinSplit th = {!!}
-- Given a splitting, show that we can "riffle" together a bunch
-- of "All P"-s for each selection to get an "All P" for the whole.
riffle : forall {X : Set}{xs ys zs : List X}
{th : xs <: zs}{ph : ys <: zs}
{P : X -> Set} ->
All P xs -> Splitting th ph -> All P ys ->
All P zs
riffle pxs s pys = {!!}
-- Moreover, we can use a splitting to invert "riffle", dealing
-- out an "All P" for the whole list into the parts for each
-- selection in the splitting, and making sure that the parts
-- riffle back together to make the whole.
data Deal {X : Set}{xs ys zs : List X}
{th : xs <: zs}{ph : ys <: zs}(s : Splitting th ph)
{P : X -> Set} :
All P zs -> Set where
dealt : (pxs : All P xs)(pys : All P ys) -> Deal s (riffle pxs s pys)
deal : {X : Set}{xs ys zs : List X}
{th : xs <: zs}{ph : ys <: zs}(s : Splitting th ph)
{P : X -> Set}(pzs : All P zs) -> Deal s pzs
deal s pzs = {!!}
------------------------------------------------------------------------------
-- 1.6 Composability as a relation
------------------------------------------------------------------------------
-- We have the composition *operator*, but it is sometimes more
-- convenient to work with the *call graph* of the composition operator,
-- giving the explanations for why an output comes from some input.
-- For example, the call graph of our boolean <= operator from Lecture.One
-- _<=_ : Nat -> Nat -> Two
-- zero <= y = tt
-- suc x <= zero = ff
-- suc x <= suc y = x <= y
-- would be
-- data Graph<= : Nat -> Nat -> Two -> Set where
-- le-z-y : forall {y} -> Graph<= zero y tt
-- le-s-z : forall {x} -> Graph<= (suc x) zero ff
-- le-s-s : forall {x y b} -> Graph<= x y b -> Graph<= (suc x) (suc y) b
-- so that we can always show
-- graph<= : (x y : Nat) -> Graph<= x y (x <= y)
-- Define the inductive composability relation on three thinnings.
-- This should correspond to your composition function, with one
-- constructor per line of your function, and one recursive substructure
-- per recursive call. We've written the type declaration, but you need
-- to add the constructors.
-- No defined function symbols should appear in any of the type indices,
-- just variables and constructors. That means dependent pattern matching
-- will play nice.
data Composable-<- {X : Set}
: {xs ys zs : List X}
(th : xs <: ys)(ph : ys <: zs)(thph : xs <: zs)
-> Set where
-- your constructors here!
-- Show that your definition really captures composability by
-- proving the following.
composable-<- : forall {X : Set}{xs ys zs : List X}
(th : xs <: ys)(ph : ys <: zs) ->
Composable-<- th ph (th -<- ph)
-- i.e., we have *at least* composition...
composable-<- th ph = {!!}
composable-unique : forall {X : Set}{xs ys zs : List X}
{th : xs <: ys}{ph : ys <: zs}
{thph thph' : xs <: zs} ->
Composable-<- th ph thph ->
Composable-<- th ph thph' ->
thph == thph'
-- ...and nothing but composition.
composable-unique c d = {!!}
-- Your prize for establishing the graph representation is to have a nice time
-- showing that thinnings really are *embeddings* (or "monomorphisms").
-- If you have two thinnings, th and th' that compose with some ph to get
-- equal results, th and th' must have been equal in the first place. That
-- tells you something important about ph, namely that it maps all its source
-- positions to distinct target positions.
composable-mono : forall {X}{xs ys zs : List X}
{th th' : xs <: ys}{ph : ys <: zs}{ps : xs <: zs} ->
Composable-<- th ph ps -> Composable-<- th' ph ps ->
th == th'
composable-mono c d = {!!}
-- Now use composable-<- and composable-mono to get a cheap proof of the
-- following.
mono-<- : forall {X}{xs ys zs : List X}(th th' : xs <: ys)(ph : ys <: zs) ->
th -<- ph == th' -<- ph ->
th == th'
mono-<- th th' ph q = {!!}
------------------------------------------------------------------------------
-- 1.7 Pullbacks (pointwise "and")
------------------------------------------------------------------------------
-- If we have a situation like this
--
-- ys
-- |
-- | ph
-- v
-- xs ------> zs
-- th
-- we say a "BackSquare" extends the situation to a square
-- side1
-- corner ------> ys
-- | |
-- side0| | ph
-- v v
-- xs ------> zs
-- th
-- where the *same* diagonal is both side0 -<- th and side1 -<- ph,
-- so either path around the square gives the same thinning.
record BackSquare {X}{xs ys zs : List X}
(th : xs <: zs)(ph : ys <: zs) : Set where
constructor backSquare
field
{corner} : List X
{side0} : corner <: xs
{side1} : corner <: ys
{diagonal} : corner <: zs
triangle0 : Composable-<- side0 th diagonal
triangle1 : Composable-<- side1 ph diagonal
open BackSquare
-- The corner of the "best" BackSquare is called a *pullback*,
-- (and the square is called a "pullback square"). What's best
-- about it is that the corner of every other BackSquare embeds
-- in it. That is, it has all the things that both th and ph
-- select from zs.
-- First, construct the pullback square.
pullback-<- : forall {X}{xs ys zs : List X} ->
(th : xs <: zs)(ph : ys <: zs) ->
BackSquare th ph
pullback-<- th ph = {!!}
-- Then show that every other BackSquare has a corner
-- which embeds in the pullback, and that the resulting
-- triangles commute.
pullback-best : forall {X}{xs ys zs : List X} ->
{th : xs <: zs}{ph : ys <: zs} ->
let bs = pullback-<- th ph in
(bs' : BackSquare th ph) ->
Sg (corner bs' <: corner bs) \ ps ->
Composable-<- ps (side0 bs) (side0 bs') *
Composable-<- ps (side1 bs) (side1 bs')
pullback-best bs' = {!!}