-
Notifications
You must be signed in to change notification settings - Fork 0
/
Machine.agda
324 lines (296 loc) · 14.9 KB
/
Machine.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
module Machine where
open import Relation.Binary.PropositionalEquality as Eq
open Eq.≡-Reasoning
open Eq using (_≡_; refl; sym; cong; subst; trans)
open import Data.List using (List; _∷_; []; _++_)
renaming (length to len)
open import Data.Vec using (Vec; lookup; _[_]≔_)
open import Data.Fin using (Fin)
open import Data.Product using (Σ-syntax; _×_; _,_)
open import Data.Star using (Star; ε; _◅_; _◅◅_)
open import Data.Empty using (⊥; ⊥-elim)
open import Relation.Nullary using (¬_)
open import Data.Nat using (ℕ; zero; suc; _+_)
open import Data.Nat.Properties using (+-identityʳ; +-cancelʳ-≡)
Offset : Set
Offset = ℕ
data Instr (∣V∣ : ℕ) : Set where
ADD : Instr ∣V∣
CONST : ℕ → Instr ∣V∣
LOAD : Fin ∣V∣ → Instr ∣V∣
STR : Fin ∣V∣ → Instr ∣V∣
HALT : Instr ∣V∣
BRZ⁺ : Offset → Instr ∣V∣
BRZ⁻ : Offset → Instr ∣V∣
MachineProgram : ℕ → Set
MachineProgram ∣V∣ = List (Instr ∣V∣)
Stack : Set
Stack = List ℕ
PrgCounter : Set
PrgCounter = ℕ
Memory : ℕ → Set
Memory = Vec ℕ
record Config (∣V∣ : ℕ) : Set where
constructor ⟨_,_,_⟩
field
pc : PrgCounter
stack : Stack
mem : Memory ∣V∣
init-config : ∀ {∣V∣ : ℕ} → Memory ∣V∣ → Config ∣V∣
init-config mem = ⟨ 0 , [] , mem ⟩
pc-increment : ∀ {∣V∣ : ℕ} → Config ∣V∣ → Config ∣V∣
pc-increment ⟨ pc , stack , mem ⟩ = ⟨ suc pc , stack , mem ⟩
pc-increase : ℕ → ∀ {∣V∣ : ℕ} → Config ∣V∣ → Config ∣V∣
pc-increase n ⟨ pc , stack , mem ⟩ = ⟨ n + pc , stack , mem ⟩
infix 3 [_,_]↝_
-- `is[pc] ≈ i` is inhabited iff the list of instructions `is` contains
-- instruction `i` at location `pc`.
-- TODO: this is probably defined in the standard library. Make use of that
-- instead.
data _[_]≈_ {∣V∣ : ℕ} : MachineProgram ∣V∣ → ℕ → Instr ∣V∣ → Set where
head : ∀ {i : Instr ∣V∣} (p : MachineProgram ∣V∣) → (i ∷ p) [ 0 ]≈ i
tail : ∀ {i i′ : Instr ∣V∣} {p : MachineProgram ∣V∣} {n}
→ p [ n ]≈ i′ → (i ∷ p) [ suc n ]≈ i′
[∙]≈-det : ∀ {∣V∣ : ℕ} {p : MachineProgram ∣V∣} {ind : ℕ} {i₀ i₁ : Instr ∣V∣}
→ p [ ind ]≈ i₀
→ p [ ind ]≈ i₁
→ i₀ ≡ i₁
[∙]≈-det (head is) (head is′) = refl
[∙]≈-det (tail φ) (tail ψ) = [∙]≈-det φ ψ
data [_,_]↝_ {∣V∣ : ℕ} : Config ∣V∣
→ MachineProgram ∣V∣
→ Config ∣V∣
→ Set where
R-const : ∀ {n S} {pc : PrgCounter} {P : MachineProgram ∣V∣} {M : Vec ℕ ∣V∣}
→ P [ pc ]≈ CONST n
→ [ ⟨ pc , S , M ⟩ , P ]↝ ⟨ suc pc , n ∷ S , M ⟩
R-add : ∀ {n₀ n₁ : ℕ}
{P : MachineProgram ∣V∣} {pc} {S} {M : Vec ℕ ∣V∣}
→ P [ pc ]≈ ADD
→ [ ⟨ pc , n₁ ∷ n₀ ∷ S , M ⟩ , P ]↝ ⟨ suc pc , n₀ + n₁ ∷ S , M ⟩
R-load : ∀ {ind : Fin ∣V∣}
{P : MachineProgram ∣V∣} {pc} {S} {M : Vec ℕ ∣V∣}
→ P [ pc ]≈ LOAD ind
→ [ ⟨ pc , S , M ⟩ , P ]↝ ⟨ suc pc , lookup M ind ∷ S , M ⟩
R-str : ∀ {v : ℕ} {ind : Fin ∣V∣}
{P : MachineProgram ∣V∣} {pc S} {M : Vec ℕ ∣V∣}
→ P [ pc ]≈ STR ind
→ [ ⟨ pc , v ∷ S , M ⟩ , P ]↝ ⟨ suc pc , S , M [ ind ]≔ v ⟩
R-br⁺Z : ∀ {P : MachineProgram ∣V∣} {pc S} {M : Vec ℕ ∣V∣} {o : Offset}
→ P [ pc ]≈ BRZ⁺ o
→ [ ⟨ pc , 0 ∷ S , M ⟩ , P ]↝ ⟨ (suc pc) + o , S , M ⟩
R-br⁺S : ∀ {P : MachineProgram ∣V∣} {pc S} {M : Vec ℕ ∣V∣}
{o : Offset} {n : ℕ}
→ P [ pc ]≈ BRZ⁺ o
→ [ ⟨ pc , suc n ∷ S , M ⟩ , P ]↝ ⟨ suc pc , S , M ⟩
R-br⁻Z : ∀ {P : MachineProgram ∣V∣} {pc pc′ S} {M : Vec ℕ ∣V∣} {o : Offset}
→ P [ pc ]≈ BRZ⁻ o
→ pc′ + o ≡ suc pc
→ [ ⟨ pc , 0 ∷ S , M ⟩ , P ]↝ ⟨ pc′ , S , M ⟩
R-br⁻S : ∀ {P : MachineProgram ∣V∣} {pc S} {M : Vec ℕ ∣V∣}
{o : Offset} {n : ℕ}
→ P [ pc ]≈ BRZ⁻ o
→ [ ⟨ pc , suc n ∷ S , M ⟩ , P ]↝ ⟨ suc pc , S , M ⟩
[_,_]↝⋆_ : ∀ {∣V∣ : ℕ}
→ Config ∣V∣
→ MachineProgram ∣V∣
→ Config ∣V∣
→ Set
[ C , p ]↝⋆ C₀ = Star (λ C′ C₀′ → [ C′ , p ]↝ C₀′) C C₀
[_,_]halts-with_ : ∀ {∣V∣} → Config ∣V∣ → MachineProgram ∣V∣ → Config ∣V∣ → Set
[_,_]halts-with_ {∣V∣} C₀ P C₁ =
[ C₀ , P ]↝⋆ C₁ × (∀ C₁′ → ¬ [ C₁ , P ]↝ C₁′)
index-append : ∀ {∣V∣ : ℕ} {p : MachineProgram ∣V∣} {is : List (Instr ∣V∣)} {i : Instr ∣V∣}
{k : ℕ}
→ p [ k ]≈ i
→ (p ++ is) [ k ]≈ i
index-append (head is) = head (is ++ _)
index-append (tail φ) = tail (index-append φ)
index-prepend : ∀ {∣V∣ : ℕ} {p : MachineProgram ∣V∣} {is : List (Instr ∣V∣)} {i : Instr ∣V∣}
{k : ℕ}
→ p [ k ]≈ i
→ (is ++ p) [ len is + k ]≈ i
index-prepend {is = []} ind = ind
index-prepend {is = _ ∷ _} ind = tail (index-prepend ind)
↝-append⋆ : ∀ {∣V∣ : ℕ} {p : MachineProgram ∣V∣} {is : List (Instr ∣V∣)}
{C C′ : Config ∣V∣}
→ [ C , p ]↝ C′
→ [ C , p ++ is ]↝ C′
↝-append⋆ (R-const φ) = R-const (index-append φ)
↝-append⋆ (R-add φ) = R-add (index-append φ)
↝-append⋆ (R-load φ) = R-load (index-append φ)
↝-append⋆ (R-str φ) = R-str (index-append φ)
↝-append⋆ (R-br⁺Z φ) = R-br⁺Z (index-append φ)
↝-append⋆ (R-br⁺S φ) = R-br⁺S (index-append φ)
↝-append⋆ (R-br⁻Z φ eq) = R-br⁻Z (index-append φ) eq
↝-append⋆ (R-br⁻S φ) = R-br⁻S (index-append φ)
↝⋆-append⋆ : ∀ {∣V∣ : ℕ} {p : MachineProgram ∣V∣} {is : List (Instr ∣V∣)}
{C C′ : Config ∣V∣}
→ [ C , p ]↝⋆ C′
→ [ C , p ++ is ]↝⋆ C′
↝⋆-append⋆ ε = ε
↝⋆-append⋆ (φ ◅ φ⋆) = ↝-append⋆ φ ◅ ↝⋆-append⋆ φ⋆
↝-prepend : ∀ {∣V∣ : ℕ} {i : Instr ∣V∣} {p : MachineProgram ∣V∣}
{C C′ : Config ∣V∣}
→ [ C , p ]↝ C′
→ [ pc-increment C , i ∷ p ]↝ pc-increment C′
↝-prepend (R-const φ) = R-const (tail φ)
↝-prepend (R-add φ) = R-add (tail φ)
↝-prepend (R-load φ) = R-load (tail φ)
↝-prepend (R-str φ) = R-str (tail φ)
↝-prepend (R-br⁺Z φ) = R-br⁺Z (tail φ)
↝-prepend (R-br⁺S φ) = R-br⁺S (tail φ)
↝-prepend (R-br⁻Z φ eq) = R-br⁻Z (tail φ) (cong suc eq)
↝-prepend (R-br⁻S φ) = R-br⁻S (tail φ)
↝⋆-prepend : ∀ {∣V∣ : ℕ} {i : Instr ∣V∣} {p : MachineProgram ∣V∣}
{C C′ : Config ∣V∣}
→ [ C , p ]↝⋆ C′
→ [ pc-increment C , i ∷ p ]↝⋆ pc-increment C′
↝⋆-prepend ε = ε
↝⋆-prepend (φ ◅ φ⋆) = ↝-prepend φ ◅ ↝⋆-prepend φ⋆
↝⋆-prepend⋆ : ∀ {∣V∣ : ℕ} {p : MachineProgram ∣V∣} {is : List (Instr ∣V∣)}
{C C′ : Config ∣V∣}
→ [ C , p ]↝⋆ C′
→ [ pc-increase (len is) C , is ++ p ]↝⋆ pc-increase (len is) C′
↝⋆-prepend⋆ {is = []} φ = φ
↝⋆-prepend⋆ {is = _ ∷ _ } φ = ↝⋆-prepend (↝⋆-prepend⋆ φ)
-- The composability proof for machine programs.
hauptsatz : ∀ {∣V∣ : ℕ} {p₀ p₁ : MachineProgram ∣V∣}
{pc pc′ : PrgCounter}
{stack stack′ stack′′ : Stack}
{mem mem′ mem′′ : Memory ∣V∣}
→ [ ⟨ pc , stack , mem ⟩ , p₀ ]↝⋆ ⟨ len p₀ , stack′′ , mem′′ ⟩
→ [ ⟨ zero , stack′′ , mem′′ ⟩ , p₁ ]↝⋆ ⟨ pc′ , stack′ , mem′ ⟩
→ [ ⟨ pc , stack , mem ⟩ , p₀ ++ p₁ ]↝⋆ ⟨ len p₀ + pc′ , stack′ , mem′ ⟩
hauptsatz {|V|} {p₀} {p₁} {_} {pc′} {stack′ = S′} {S′′} {mem} {mem′} {mem′′} φ ψ =
↝⋆-append⋆ φ ◅◅ ϑ
where
ϑ : [ ⟨ len p₀ , S′′ , mem′′ ⟩ , p₀ ++ p₁ ]↝⋆ ⟨ len p₀ + pc′ , S′ , mem′ ⟩
ϑ =
subst
(λ m → [ ⟨ m , _ , _ ⟩ , p₀ ++ p₁ ]↝⋆ ⟨ len p₀ + pc′ , _ , _ ⟩)
(+-identityʳ (len p₀))
(↝⋆-prepend⋆ ψ)
step-det : ∀ {∣V∣ : ℕ} {C₀ C₁ C₂ : Config ∣V∣} {P : MachineProgram ∣V∣}
→ [ C₀ , P ]↝ C₁
→ [ C₀ , P ]↝ C₂
→ C₁ ≡ C₂
step-det (R-const p) (R-const q) with [∙]≈-det p q
step-det (R-const p) (R-const q) | refl = refl
step-det (R-const p) (R-add q) with [∙]≈-det p q
step-det (R-const p) (R-add q) | ()
step-det (R-const p) (R-load q) with [∙]≈-det p q
step-det (R-const p) (R-load q) | ()
step-det (R-const p) (R-str q) with [∙]≈-det p q
step-det (R-const p) (R-str q) | ()
step-det (R-const p) (R-br⁺Z q) with [∙]≈-det p q
step-det (R-const p) (R-br⁺Z q) | ()
step-det (R-const p) (R-br⁺S q) with [∙]≈-det p q
step-det (R-const p) (R-br⁺S q) | ()
step-det (R-const p) (R-br⁻Z q eq) with [∙]≈-det p q
step-det (R-const p) (R-br⁻Z q eq) | ()
step-det (R-const p) (R-br⁻S q) with [∙]≈-det p q
step-det (R-const p) (R-br⁻S q) | ()
step-det (R-add _) (R-add _) = refl
step-det (R-add p) (R-const q) with [∙]≈-det p q
step-det (R-add p) (R-const q) | ()
step-det (R-add p) (R-load q) with [∙]≈-det p q
step-det (R-add p) (R-load q) | ()
step-det (R-add p) (R-str q) with [∙]≈-det p q
step-det (R-add p) (R-str q) | ()
step-det (R-add p) (R-br⁺Z q) with [∙]≈-det p q
step-det (R-add p) (R-br⁺Z q) | ()
step-det (R-add p) (R-br⁺S q) with [∙]≈-det p q
step-det (R-add p) (R-br⁺S q) | ()
step-det (R-add p) (R-br⁻Z q _) with [∙]≈-det p q
step-det (R-add p) (R-br⁻Z q _) | ()
step-det (R-add p) (R-br⁻S q) with [∙]≈-det p q
step-det (R-add p) (R-br⁻S q) | ()
step-det (R-load p) (R-load q) with [∙]≈-det p q
step-det (R-load p) (R-load q) | refl = refl
step-det (R-load p) (R-const q) with [∙]≈-det p q
step-det (R-load p) (R-const q) | ()
step-det (R-load p) (R-add q) with [∙]≈-det p q
step-det (R-load p) (R-add q) | ()
step-det (R-load p) (R-str q) with [∙]≈-det p q
step-det (R-load p) (R-str q) | ()
step-det (R-load p) (R-br⁺Z q) with [∙]≈-det p q
step-det (R-load p) (R-br⁺Z q) | ()
step-det (R-load p) (R-br⁺S q) with [∙]≈-det p q
step-det (R-load p) (R-br⁺S q) | ()
step-det (R-load p) (R-br⁻Z q _) with [∙]≈-det p q
step-det (R-load p) (R-br⁻Z q _)| ()
step-det (R-load p) (R-br⁻S q) with [∙]≈-det p q
step-det (R-load p) (R-br⁻S q) | ()
step-det (R-str p) (R-str q) with [∙]≈-det p q
step-det (R-str p) (R-str q) | refl = refl
step-det (R-str p) (R-const q) with [∙]≈-det p q
step-det (R-str p) (R-const q) | ()
step-det (R-str p) (R-add q) with [∙]≈-det p q
step-det (R-str p) (R-add q) | ()
step-det (R-str p) (R-load q) with [∙]≈-det p q
step-det (R-str p) (R-load q) | ()
step-det (R-str p) (R-br⁺Z q) with [∙]≈-det p q
step-det (R-str p) (R-br⁺Z q) | ()
step-det (R-str p) (R-br⁺S q) with [∙]≈-det p q
step-det (R-str p) (R-br⁺S q) | ()
step-det (R-str p) (R-br⁻Z q _) with [∙]≈-det p q
step-det (R-str p) (R-br⁻Z q _) | ()
step-det (R-str p) (R-br⁻S q) with [∙]≈-det p q
step-det (R-str p) (R-br⁻S q) | ()
step-det (R-br⁺Z p) (R-br⁺Z q) with [∙]≈-det p q
step-det (R-br⁺Z p) (R-br⁺Z q) | refl = refl
step-det (R-br⁺Z p) (R-const q) with [∙]≈-det p q
step-det (R-br⁺Z p) (R-const q) | ()
step-det (R-br⁺Z p) (R-add q) with [∙]≈-det p q
step-det (R-br⁺Z p) (R-add q) | ()
step-det (R-br⁺Z p) (R-load q) with [∙]≈-det p q
step-det (R-br⁺Z p) (R-load q) | ()
step-det (R-br⁺Z p) (R-str q) with [∙]≈-det p q
step-det (R-br⁺Z p) (R-str q) | ()
step-det (R-br⁺Z p) (R-br⁻Z q _) with [∙]≈-det p q
step-det (R-br⁺Z p) (R-br⁻Z q _) | ()
step-det (R-br⁺S p) (R-br⁺S q) with [∙]≈-det p q
step-det (R-br⁺S p) (R-br⁺S q) | refl = refl
step-det (R-br⁺S p) (R-const q) with [∙]≈-det p q
step-det (R-br⁺S p) (R-const q) | ()
step-det (R-br⁺S p) (R-add q) with [∙]≈-det p q
step-det (R-br⁺S p) (R-add q) | ()
step-det (R-br⁺S p) (R-load q) with [∙]≈-det p q
step-det (R-br⁺S p) (R-load q) | ()
step-det (R-br⁺S p) (R-str q) with [∙]≈-det p q
step-det (R-br⁺S p) (R-str q) | ()
step-det (R-br⁺S p) (R-br⁻S q) with [∙]≈-det p q
step-det (R-br⁺S p) (R-br⁻S q) | ()
step-det (R-br⁻Z p eq₀) (R-br⁻Z q eq₁) with [∙]≈-det p q
step-det (R-br⁻Z {pc′ = pc} p eq₀) (R-br⁻Z {pc′ = pc′} q eq₁)
| refl rewrite +-cancelʳ-≡ pc pc′ (trans eq₀ (sym eq₁)) = refl
step-det (R-br⁻Z p _) (R-const q) with [∙]≈-det p q
step-det (R-br⁻Z p _) (R-const q) | ()
step-det (R-br⁻Z p _) (R-add q) with [∙]≈-det p q
step-det (R-br⁻Z p _) (R-add q) | ()
step-det (R-br⁻Z p _) (R-load q) with [∙]≈-det p q
step-det (R-br⁻Z p _) (R-load q) | ()
step-det (R-br⁻Z p _) (R-str q) with [∙]≈-det p q
step-det (R-br⁻Z p _) (R-str q) | ()
step-det (R-br⁻Z p _) (R-br⁺Z q) with [∙]≈-det p q
step-det (R-br⁻Z p _) (R-br⁺Z q) | ()
step-det (R-br⁻S p) (R-br⁻S q) with [∙]≈-det p q
step-det (R-br⁻S p) (R-br⁻S q) | refl = refl
step-det (R-br⁻S p) (R-const q) with [∙]≈-det p q
step-det (R-br⁻S p) (R-const q) | ()
step-det (R-br⁻S p) (R-add q) with [∙]≈-det p q
step-det (R-br⁻S p) (R-add q) | ()
step-det (R-br⁻S p) (R-load q) with [∙]≈-det p q
step-det (R-br⁻S p) (R-load q) | ()
step-det (R-br⁻S p) (R-str q) with [∙]≈-det p q
step-det (R-br⁻S p) (R-str q) | ()
step-det (R-br⁻S p) (R-br⁺S q) with [∙]≈-det p q
step-det (R-br⁻S p) (R-br⁺S q) | ()
machine-det : ∀ {∣V∣ : ℕ} {C₀ C₁ C₂ : Config ∣V∣} {P : MachineProgram ∣V∣}
→ [ C₀ , P ]halts-with C₁ → [ C₀ , P ]halts-with C₂ → C₁ ≡ C₂
machine-det (ε , _) (ε , _) = refl
machine-det (ε , φ) (_◅_ {_} {C′} q q⋆ , ψ) = ⊥-elim (φ C′ q)
machine-det (_◅_ {_} {C′} p p⋆ , φ) (ε , ψ) = ⊥-elim (ψ C′ p)
machine-det (p ◅ p⋆ , φ) (q ◅ q⋆ , ψ) rewrite step-det p q =
machine-det (p⋆ , φ) (q⋆ , ψ)