-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTarski.agda
367 lines (252 loc) · 8.71 KB
/
Tarski.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
module Tarski where
import Relation.Binary.PropositionalEquality as Eqq
open import Data.Nat using (ℕ; zero; suc; _+_;_∸_; _*_)
open import Data.Product using (_×_;_,_)
open import Data.List
open import Data.Maybe
open import Function.Base using (flip)
open import Data.List.Relation.Unary.Any
open import Data.Bool using (true; false; _∧_; Bool; if_then_else_)
open Eqq using (_≡_; refl; cong; cong₂; sym ; trans)
open Eqq.≡-Reasoning using (begin_; _≡⟨⟩_; step-≡; _∎)
open import Data.Product
open import Data.String using (String; _≟_)
open import Relation.Nullary using (yes; no)
open import Data.Sum.Base
open import Level using (Level; _⊔_) renaming (suc to lsuc; zero to lzero)
open import Relation.Nullary using (¬_)
private
variable
a b c ℓ ℓ₁ ℓ₂ ℓ₃ : Level
-- Heterogeneous binary relations
REL : Set a → Set b → (ℓ : Level) → Set (a ⊔ b ⊔ lsuc ℓ)
REL A B ℓ = A → B → Set ℓ
-- Homogeneous binary relations
Rel : Set a → (ℓ : Level) → Set (a ⊔ lsuc ℓ)
Rel A ℓ = REL A A ℓ
-- A unary relation is a predicate
Pred : Set a → (ℓ : Level) → Set (a ⊔ lsuc ℓ)
Pred A ℓ = A → Set ℓ
-- Set type as in lean
SET : Set a → (ℓ : Level) → Set (a ⊔ lsuc ℓ)
SET A ℓ = Pred A ℓ
_∈_ : {A : Set a} → A → SET A ℓ → Set _
x ∈ P = P x
-- In set theory: for a set A and P a predicate over A (i.e. P ⊆ A), S = {x ∈ A | P x }
-- In type theory: A : Set, P : A → Set and S = Σ A P
-- Id = {(x, y) | x , y ∈ A ∧ x ≡ y }
Id : {A : Set a} → A → A → Set a
Id x y = x ≡ y
-- R ∪ S = {(x, y) | (x, y) ∈ R v (x, y) ∈ S}
_∪_ : {A : Set a} {B : Set b}
→ REL A B ℓ₁
→ REL A B ℓ₂
-------------------
→ REL A B (ℓ₁ ⊔ ℓ₂)
(R ∪ S) x y = R x y ⊎ S x y
-- R ◯ S = {(x, y) | ∃z: (x,z) ∈ R ∧ (z,y) ∈ S}
_◯_ : {A : Set a} {B : Set b} {C : Set c}
→ REL A B ℓ₁
→ REL B C ℓ₂
-------------------------
→ REL A C (b ⊔ ℓ₁ ⊔ ℓ₂ )
(R ◯ S) x y = ∃ λ z → R x z × S z y
-- it's the same as Σ _ (λ z → R x z × S z y)
-- R ⇃ P = {(x, y) | (x, y) ∈ R ∧ P x}
_⇃_ : {A : Set a} {B : Set b}
→ REL A B ℓ₁
→ Pred A ℓ₂
---------------
→ REL A B (ℓ₁ ⊔ ℓ₂)
(R ⇃ P) x y = R x y × P x
private
variable
_≈_ : {A : Set a} → Rel A ℓ -- The underlying equality relation
Monotonic : {A : Set a} {B : Set b}
→ Rel A ℓ₁
→ Rel B ℓ₂
→ (A → B)
---------------
→ Set (a ⊔ ℓ₁ ⊔ ℓ₂)
Monotonic _R_ _R'_ f = ∀ {x y}
→ x R y
---------------
→ f x R' f y
_⊆_ : {A : Set a}
→ SET A ℓ₁
→ SET A ℓ₂
------------
→ Set (a ⊔ ℓ₁ ⊔ ℓ₂)
X ⊆ Y = ∀ {x}
→ x ∈ X
---------
→ x ∈ Y
-- 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
Reflex : {A : Set a}
→ Rel A ℓ₁
---------------
→ Set (a ⊔ ℓ₁)
Reflex _R_ = ∀ {x} → x R x
Sym : {A : Set a}
→ Rel A ℓ₁
---------------
→ Set _
Sym _R_ = ∀ {x y}
→ x R y
--------
→ y R x
Antisymmetric : {A : Set a} → Rel A ℓ₁ → Rel A ℓ₂ → Set _
Antisymmetric _E_ _R_ = ∀ {x y}
→ x R y
→ y R x
--------
→ x E y
Trans : {A : Set a}
→ Rel A ℓ₁
---------------
→ Set _
Trans _R_ = ∀ {x y z}
→ x R y
→ y R z
--------
→ x R z
module Structures
{a ℓ ℓ₃} {A : Set a} -- The underlying set
(_≈_ : Rel A ℓ) -- The underlying equality relation
where
record IsPartialOrder (_≤_ : Rel A ℓ₂ ) : Set (a ⊔ ℓ ⊔ ℓ₂) where
field
reflex : Reflex _≤_
transitive : Trans _≤_
antisym : Antisymmetric _≈_ _≤_
record IsCompleteLattice (_≤_ : Rel A ℓ₂) (Π : SET A ℓ₂ → A) : Set (a ⊔ ℓ ⊔ lsuc ℓ₂) where
field
isPartialOrder : IsPartialOrder _≤_
glb : ∀ {X} {x} → x ∈ X → Π X ≤ x
gtGlb : ∀ {X} {y} → (∀ {x} → x ∈ X → y ≤ x) → y ≤ Π X
lfp :
(_≤_ : Rel A ℓ₃ )
→ (Π : SET A ℓ₃ → A)
→ (IsCompleteLattice _≤_ Π)
→ (A → A)
--------------------------------
→ A
lfp _≤_ Π cl f = Π (λ x → f x ≤ x)
lfpLe :
(_≤_ : Rel A ℓ₃ )
→ (Π : SET A ℓ₃ → A)
→ (cl : IsCompleteLattice _≤_ Π)
→ (f : A → A)
→ (x : A)
→ (h : f x ≤ x)
---------------------
→ lfp _≤_ Π cl f ≤ x
lfpLe _≤_ Π cl f x h = IsCompleteLattice.glb cl h
Lelfp :
(_≤_ : Rel A ℓ₃ )
→ (Π : SET A ℓ₃ → A)
→ (cl : IsCompleteLattice _≤_ Π)
→ (f : A → A)
→ (x : A)
→ (h : ∀ {x'} → f x' ≤ x' → x ≤ x')
-------------------------------------
→ x ≤ lfp _≤_ Π cl f
Lelfp _≤_ Π cl f x h = IsCompleteLattice.gtGlb cl h
isFixpoint :
(_≤_ : Rel A ℓ₃ )
→ (Π : SET A ℓ₃ → A)
→ (cl : IsCompleteLattice _≤_ Π)
→ (f : A → A)
→ (Monotonic _≤_ _≤_ f)
--------------------------------------------
→ (f (lfp _≤_ Π cl f)) ≈ (lfp _≤_ Π cl f)
isFixpoint _≤_ Π cl f f-monotone = fx≈x
where
x = f (lfp _≤_ Π cl f)
x≤fx : lfp _≤_ Π cl f ≤ f (lfp _≤_ Π cl f)
x≤fx = lfpLe _≤_ Π cl f x (f-monotone
(IsCompleteLattice.gtGlb cl
(λ z → IsPartialOrder.transitive (IsCompleteLattice.isPartialOrder cl)
(f-monotone (IsCompleteLattice.glb cl z)) z)))
fx≤x : f (lfp _≤_ Π cl f) ≤ lfp _≤_ Π cl f
fx≤x = Lelfp _≤_ Π cl f x λ z →
IsPartialOrder.transitive (IsCompleteLattice.isPartialOrder cl)
(f-monotone (IsCompleteLattice.glb cl z)) z
antisim = IsPartialOrder.antisym (IsCompleteLattice.isPartialOrder cl)
-- f x ≤ x ∧ x ≤ f x ⇒ f x ≈ x
fx≈x : f (lfp _≤_ Π cl f) ≈ (lfp _≤_ Π cl f)
fx≈x = antisim fx≤x x≤fx
record Sigma (A : Set) (B : A → Set) : Set where
constructor _,_
field fst : A
snd : B fst
syntax Sigma A (λ x → P) = [ x ∈ A :: P ]
Sucs = [ (x , y) ∈ ℕ × ℕ :: y ≡ x + 1 ]
m : Sucs
m = ( (2 , 3) , refl )
Var = String
Σ' = Var → ℕ
data Arith : Set where
CONST : ℕ → Arith
VAR : Var → Arith
_PLUS_ : Arith → Arith → Arith
_TIMES_ : Arith → Arith → Arith
_MINUS_ : Arith → Arith → Arith
data Boolean : Set where
TRUE : Boolean
FALSE : Boolean
B⟦_⟧ : Boolean → Σ' → Bool
B⟦ TRUE ⟧ _ = true
B⟦ FALSE ⟧ _ = false
data Cmd : Set where
SKIP : Cmd
_::=_ : Var → Arith → Cmd
_::_ : Cmd → Cmd → Cmd
WHILE_DO_DONE : Boolean → Cmd → Cmd
_[_/_] : Arith → Arith → Var → Arith
CONST n [ e / x ] = CONST n
VAR y [ e / x ] with y ≟ x
... | yes _ = e
... | no _ = VAR y
(n PLUS m) [ e / x ] = (n [ e / x ]) PLUS (m [ e / x ])
(n TIMES m) [ e / x ] = (n [ e / x ]) TIMES (m [ e / x ])
(n MINUS m) [ e / x ] = (n [ e / x ]) MINUS (m [ e / x ])
_[_↦_] : Σ' → Var → ℕ → Σ'
(σ [ X ↦ n ]) Y with Y ≟ X
... | yes _ = n
... | no _ = σ Y
ℕ⟦_⟧ : Arith → Σ' → ℕ
ℕ⟦ CONST n ⟧ σ = n
ℕ⟦ VAR x ⟧ σ = σ x
ℕ⟦ n PLUS m ⟧ σ = ℕ⟦ n ⟧ σ + ℕ⟦ m ⟧ σ
ℕ⟦ n TIMES m ⟧ σ = ℕ⟦ n ⟧ σ * ℕ⟦ m ⟧ σ
ℕ⟦ n MINUS m ⟧ σ = ℕ⟦ n ⟧ σ ∸ ℕ⟦ m ⟧ σ
T = Rel Σ' lzero
⋂ : ∀ (I : SET T lzero) → {X : T} → T
⋂ I {X} = λ σ σ' → ( I X → X σ σ')
_⊆'_ : Rel T lzero
X ⊆' Y = ∀ {σ σ'} → X σ σ' → Y σ σ'
denote : ∀ {X : T} → Cmd → Σ' → Σ' → Set
denote {X} (SKIP) σ σ' = σ ≡ σ'
denote {X} (x ::= e) σ σ' = σ' ≡ (σ [ x ↦ ℕ⟦ e ⟧ σ ])
denote {X} (cmd :: cmd') = (denote {X} cmd ) ◯ (denote {X} cmd')
denote {X} (WHILE b DO cmd DONE) = least-fixpoint {X} (W B⟦ b ⟧ (denote {X} cmd) )
where
W : (Σ' → Bool) → T → ( T → T )
W cond d d' = λ σ σ' →
if cond σ then
(d ◯ d') σ σ'
else
σ ≡ σ'
least-fixpoint : ∀ {X : T} → (T → T) → T
least-fixpoint {X} f = ⋂ (λ x → f x ⊆' x) {X}