-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRelations.agda
277 lines (190 loc) · 5.76 KB
/
Relations.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
{-# OPTIONS --allow-exec #-}
{-# OPTIONS --guardedness #-}
module Relations where
import Relation.Binary.PropositionalEquality as Eq
open import Data.Nat using (ℕ; zero; suc; _+_;_∸_)
open import Data.Product using (_×_;_,_)
open import Data.List
open import Data.Maybe
open import Data.List.Relation.Unary.Any
open import Data.Bool
open Eq using (_≡_; refl; cong; cong₂; sym ; trans)
open Eq.≡-Reasoning using (begin_; _≡⟨⟩_; step-≡; _∎)
open import Data.Nat.Properties using (+-comm)
open import SMT.Theories.Nats as Nats
open import SMT.Backend.Z3 Nats.theory using (solveZ3)
-- relations
-- reflex: ∀ x . xRx
-- simmetric: ∀ x y . xRy ⇒ yRx
-- antisimmetric: ∀ x y . xRy ∧ yRx ⇒ x ≡ y
-- transistive: ∀ x y z . xRy ∧ yRz ⇒ xRz
-- total: ∀ x y . xRy v yRx
--
-- A relation R is
-- Preorder: reflexive and transitive.
-- Partial order: preorder and antisimmetric.
-- Total order: partial order and total
data ^ : ∀ {A : Set} (R : A → A → Set) → A → A → Set where
^-base : ∀ {A} {R : A → A → Set } {x y}
→ R x y
------------
→ ^ R x y
^-trans : ∀ {A} {R : A → A → Set} {x y z}
→ ^ R x y → ^ R y z
----------------------------------
→ ^ R x z
data ≺ : ℕ → ℕ → Set where
≺-cons : ∀ {x y : ℕ}
→ suc x ≡ y
-------------
→ ≺ x y
≺-inv : ∀ (x y : ℕ)
→ ≺ x y
→ suc x ≡ y
≺-inv x y (≺-cons x+1≡y) = x+1≡y
data ≤ : ℕ → ℕ → Set where
z≤n : ∀ {x : ℕ}
----------
→ ≤ zero x
s≤s : ∀ {x y : ℕ}
→ ≤ x y
--------------------
→ ≤ (suc x) (suc y)
x≤x : ∀ (x : ℕ) → ≤ x x
x≤x zero = z≤n
x≤x (suc x) = s≤s (x≤x x)
data < : ℕ → ℕ → Set where
z<s : ∀ {n : ℕ}
------------
→ < zero (suc n)
s<s : ∀ {m n : ℕ}
→ < m n
-------------
→ < (suc m) (suc n)
<-trans : ∀ {x y z : ℕ}
→ < x y → < y z
---------------------
→ < x z
<-trans z<s (s<s n<m) = z<s
<-trans (s<s m<n) (s<s n<p) = s<s (<-trans m<n n<p)
<xsx : ∀ {x : ℕ}
---------------------
→ < x (suc x)
<xsx {zero} = z<s
<xsx {suc x} = s<s <xsx
≺' : ℕ → ℕ → Set
≺' x y = ^ ≺ x y
≺⊆< : ∀ (x y : ℕ)
→ ≺ x y
-------------------
→ < x y
≺⊆< x y (≺-cons r)
rewrite (sym r) = <xsx
≺'⊆< : ∀ (x y : ℕ)
→ ≺' x y
-------------------
→ < x y
≺'⊆< x y (^-base r) = ≺⊆< x y r
≺'⊆< x y (^-trans {x = x} {y = z} {z = y} r1 r2) = <-trans h1 h2
where
h1 : < x z
h1 = ≺'⊆< x z r1
h2 : < z y
h2 = ≺'⊆< z y r2
x≺'1+k+x : ∀ (x k : ℕ)
----------------------
→ ≺' x (1 + k + x)
x≺'1+k+x x zero = ^-base (≺-cons refl)
x≺'1+k+x x (suc k) = ^-trans (x≺'1+k+x x k) (^-base (≺-cons refl))
-- Z3 help!
arith-1 : ∀ {x y : ℕ} → (1 + (y ∸ x ∸ 1) + x) ≡ y
arith-1 = solveZ3
<⊆≺' : ∀ (x y : ℕ)
→ < x y
-------------------
→ ≺' x y
<⊆≺' x y _ = part2
where
part1 : (≺' x (1 + (y ∸ x ∸ 1) + x) ) ≡ (≺' x y)
part1 =
begin
≺' x (1 + (y ∸ x ∸ 1) + x)
≡⟨ cong₂ ≺' refl (arith-1 {x} {y}) ⟩
≺' x y
∎
part2 : ≺' x y
part2
rewrite (sym part1) = x≺'1+k+x x (y ∸ x ∸ 1)
postulate
-- this axioms makes sense, (p ⇒ q) ∧ (q ⇒ p) is p ≡ q
relation-≡ : ∀ {A B : Set} {r1 r2 : A → B → Set} {x : A} {y : B}
→ (r1 x y → r2 x y) × (r2 x y → r1 x y)
-----------------------------------------------
→ r1 x y ≡ r2 x y
≺'≡< : ∀ (x y : ℕ)
--------------------
→ (≺' x y ≡ < x y)
≺'≡< x y =
relation-≡
{r1 = ≺'} {r2 = <}
( ≺'⊆< x y , <⊆≺' x y )
-- 𝜋
-- permutations of list
data π {A : Set} : List A → List A → Set where
π-empty :
------------
π [] []
π-add : ∀ {xs ys : List A} {x : A}
→ π xs ys
------------------------
→ π (x ∷ xs) ( x ∷ ys)
π-add2 : ∀ {xs : List A} {x y : A}
---------------------------------
→ π (x ∷ y ∷ xs) ( y ∷ x ∷ xs)
π-trans : ∀ {xs ys zs : List A}
→ π xs ys → π ys zs
---------------------------------------
→ π xs zs
-- example
data Fin : ℕ → Set where
zero : {n : ℕ} → Fin (suc n)
suc : {n : ℕ} → Fin n → Fin (suc n)
li1 : List ℕ
li1 = 2 ∷ 3 ∷ 10 ∷ 0 ∷ []
li2 : List ℕ
li2 = 10 ∷ 2 ∷ 3 ∷ 0 ∷ []
perm : π li1 li2
perm = π-trans (π-add π-add2) π-add2
-- relation min x xs when x is the min of xs
data min : ℕ → List ℕ → Set where
[] : ∀ {x} →
----------
min x []
_∷_ : ∀ {x y} {ys}
→ ≤ x y → min x ys
-------------------------------
→ min x (y ∷ ys)
-- Proof that a list in ascending order
data InOrder : List ℕ → Set where
[] : InOrder []
_∷_ : ∀ {x xs}
→ min x xs → InOrder xs
--------------------------------------
→ InOrder (x ∷ xs)
record Sorted (xs : List ℕ) : Set where
field
ys : List ℕ
inOrder : InOrder ys
isPerm : π ys xs
-- Skeleton for proving that a sorting function is correct
postulate
dumb-sort : List ℕ → List ℕ
dumb-order : ∀ {xs : List ℕ} → InOrder (dumb-sort xs)
dumb-perm : ∀ {xs : List ℕ} → π (dumb-sort xs) xs
correct-dumb-sort : ∀ (xs : List ℕ) → Sorted xs
correct-dumb-sort xs = record
{
ys = dumb-sort xs
; inOrder = dumb-order
; isPerm = dumb-perm
}