-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSound.agda
140 lines (129 loc) · 7.41 KB
/
Sound.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
open import Common
module Sound (∣Heap∣ : ℕ) where
open import Language ∣Heap∣
-- sequence of ↣′ transitions with different heaps
infix 3 H⊢_↣′_ H⊢_↣′⋆_
H⊢_↣′_ : Rel (Logs × Expression′)
H⊢ c ↣′ c′ = Σ Heap (λ h → h ⊢ c ↣′ c′)
H⊢_↣′⋆_ : Rel (Logs × Expression′)
H⊢_↣′⋆_ = Star H⊢_↣′_
-- only backwards consistency preservation when heap changes
H↣′-Consistent : ∀ {h′ l e l′ e′} →
H⊢ l , e ↣′ l′ , e′ →
Consistent h′ l′ → Consistent h′ l
H↣′-Consistent (h , ↣′-ℕ) cons′ = cons′
H↣′-Consistent (h , ↣′-R m b↣b′) cons′ = H↣′-Consistent (h , b↣b′) cons′
H↣′-Consistent (h , ↣′-L b a↣a′) cons′ = H↣′-Consistent (h , a↣a′) cons′
H↣′-Consistent (h , ↣′-writeE e↣e′) cons′ = H↣′-Consistent (h , e↣e′) cons′
H↣′-Consistent (h , ↣′-writeℕ) cons′ = cons′
H↣′-Consistent {h′} (h , ↣′-read l v) cons′ with Vec.lookup v (Logs.ω l)
... | ● m = cons′
... | ○ with Vec.lookup v (Logs.ρ l) | ≡.inspect (Vec.lookup v) (Logs.ρ l)
... | ● m | _ = cons′
... | ○ | [ ρ[v]≡○ ] = cons where
cons : Consistent h′ l
cons v′ with v′ ≟Fin v
... | yes v′≡v rewrite v′≡v | ρ[v]≡○ = λ m ()
... | no v′≢v with cons′ v′
... | cons′-v′ rewrite Vec.lookup∘update′ v′≢v (Logs.ρ l) (● (Vec.lookup v h)) = cons′-v′
H↣′⋆-Consistent : ∀ {h′ l′ e′ l e} →
H⊢ l , e ↣′⋆ l′ , e′ →
Consistent h′ l′ → Consistent h′ l
H↣′⋆-Consistent {h′} {l′} {e′} = flip $
⋆.gfold fst (const ∘ Consistent h′) H↣′-Consistent {k = l′ , e′}
private
extract : ∀ {α h R l e h′ c′ e′ h″ c″ e″} →
H⊢ ∅ , R ↣′⋆ l , e →
α ≢ τ →
h , ↣: ● (R , l) , atomic e ↠⋆ h′ , c′ , e′ →
α ⊢ h′ , c′ , e′ ↠ h″ , c″ , e″ →
∃₂ λ l′ m →
α ≡ ☢ ×
c′ , e′ ≡ ↣: ● (R , l′) , atomic (# m) ×
h″ , c″ , e″ ≡ Update h′ l′ , ↣: ○ , # m ×
Consistent h′ l′ ×
H⊢ ∅ , R ↣′⋆ l′ , # m
extract R↣′⋆e α≢τ [] (↠-↣ (↣-step e↣e′)) = ⊥-elim (α≢τ ≡.refl)
extract R↣′⋆e α≢τ [] (↠-↣ (↣-mutate h′)) = ⊥-elim (α≢τ ≡.refl)
extract R↣′⋆e α≢τ [] (↠-↣ (↣-abort ¬cons)) = ⊥-elim (α≢τ ≡.refl)
extract R↣′⋆e α≢τ [] (↠-↣ (↣-commit cons)) = _ , _ , ≡.refl , ≡.refl , ≡.refl , cons , R↣′⋆e
extract R↣′⋆e α≢τ (↠-↣ (↣-step e↣e′) ∷ c′↠⋆c″) c″↠c‴ = extract (R↣′⋆e ◅◅ (_ , e↣e′) ∷ []) α≢τ c′↠⋆c″ c″↠c‴
extract R↣′⋆e α≢τ (↠-↣ (↣-mutate h′) ∷ c′↠⋆c″) c″↠c‴ = extract R↣′⋆e α≢τ c′↠⋆c″ c″↠c‴
extract R↣′⋆e α≢τ (↠-↣ (↣-abort ¬cons) ∷ c′↠⋆c″) c″↠c‴ = extract [] α≢τ c′↠⋆c″ c″↠c‴
↣-extract : ∀ {α h R h′ c′ e′ h″ c″ e″} →
α ≢ τ →
h , ↣: ○ , atomic R ↠⋆ h′ , c′ , e′ →
α ⊢ h′ , c′ , e′ ↠ h″ , c″ , e″ →
∃₂ λ l′ m →
α ≡ ☢ ×
c′ , e′ ≡ ↣: ● (R , l′) , atomic (# m) ×
h″ , c″ , e″ ≡ Update h′ l′ , ↣: ○ , # m ×
Consistent h′ l′ ×
H⊢ ∅ , R ↣′⋆ l′ , # m
↣-extract α≢τ [] (↠-↣ ↣-begin) = ⊥-elim (α≢τ ≡.refl)
↣-extract α≢τ (↠-↣ ↣-begin ∷ c↠⋆c′) c′↠c″ = extract [] α≢τ c↠⋆c′ c′↠c″
↣′-swap : ∀ {h h′ l e l′ e′} →
Consistent h′ l′ →
h ⊢ l , e ↣′ l′ , e′ →
h′ ⊢ l , e ↣′ l′ , e′
↣′-swap cons′ ↣′-ℕ = ↣′-ℕ
↣′-swap cons′ (↣′-R m b↣b′) = ↣′-R m (↣′-swap cons′ b↣b′)
↣′-swap cons′ (↣′-L b a↣a′) = ↣′-L b (↣′-swap cons′ a↣a′)
↣′-swap cons′ (↣′-writeE e↣e′) = ↣′-writeE (↣′-swap cons′ e↣e′)
↣′-swap cons′ ↣′-writeℕ = ↣′-writeℕ
↣′-swap {h} {h′} cons′ (↣′-read l v) with cons′ v (Vec.lookup v h) | ↣′-read {h = h′} l v
... | cons′-v-h | ↣′-read′ with Vec.lookup v (Logs.ω l)
... | ● m = ↣′-read′
... | ○ with Vec.lookup v (Logs.ρ l)
... | ● m = ↣′-read′
... | ○ rewrite Vec.lookup∘update v (Logs.ρ l) (● (Vec.lookup v h)) | cons′-v-h ≡.refl = ↣′-read′
↣′⋆-swap : ∀ {h′ l e l′ e′} →
Consistent h′ l′ →
H⊢ l , e ↣′⋆ l′ , e′ →
h′ ⊢ l , e ↣′⋆ l′ , e′
↣′⋆-swap {h′} cons = fst ∘ ⋆.gfold id P f ([] , cons) where
P : Logs × Expression′ → Logs × Expression′ → Set
P (l , e) (l′ , e′) = h′ ⊢ l , e ↣′⋆ l′ , e′ × Consistent h′ l
f : ∀ {c c′ c″} → H⊢ c ↣′ c′ → P c′ c″ → P c c″
f (h , e↣e′) (e′↣′⋆e″ , cons′)
= ↣′-swap cons′ e↣e′ ∷ e′↣′⋆e″
, H↣′-Consistent (h , e↣e′) cons′
{-
-- alternative definition with explicit recursion (and recomputing cons′ from scratch)
↣′⋆-swap {h′} cons″ [] = []
↣′⋆-swap {h′} cons″ ((h , e↣e′) ∷ H⊢e′↣e″) = ↣′-swap cons′ e↣e′ ∷ ↣′⋆-swap cons″ H⊢e′↣e″ where
cons′ = H↣′⋆-Consistent H⊢e′↣e″ cons″
-}
↣′→↦′ : ∀ {h l e l′ e′ h₀} →
Consistent h₀ l →
Equivalent h₀ l h →
h₀ ⊢ l , e ↣′ l′ , e′ →
∃ λ h′ →
Consistent h₀ l′ ×
Equivalent h₀ l′ h′ ×
h , e ↦′ h′ , e′
↣′→↦′ cons equiv ↣′-ℕ = _ , cons , equiv , ↦′-ℕ
↣′→↦′ cons equiv (↣′-R m b↣b′) = Σ.map id (Σ.map id (Σ.map id (↦′-R m))) (↣′→↦′ cons equiv b↣b′)
↣′→↦′ cons equiv (↣′-L b a↣a′) = Σ.map id (Σ.map id (Σ.map id (↦′-L b))) (↣′→↦′ cons equiv a↣a′)
↣′→↦′ cons equiv (↣′-writeE e↣e′) = Σ.map id (Σ.map id (Σ.map id ↦′-writeE)) (↣′→↦′ cons equiv e↣e′)
↣′→↦′ cons equiv ↣′-writeℕ = _ , cons , Write-Equivalent equiv , ↦′-writeℕ
↣′→↦′ {h} cons equiv (↣′-read l v) with equiv v | ↦′-read {h} v
... | equiv-v | ↦′-read′ with Vec.lookup v (Logs.ω l)
... | ● m rewrite equiv-v = _ , cons , equiv , ↦′-read′
... | ○ with Vec.lookup v (Logs.ρ l) | ≡.inspect (Vec.lookup v) (Logs.ρ l)
... | ● m | _ rewrite equiv-v = _ , cons , equiv , ↦′-read′
... | ○ | [ ρ[v]≡○ ] rewrite equiv-v = _
, Equivalence.to (Read-Consistent l v ρ[v]≡○) ⟨$⟩ cons
, Read-Equivalent cons equiv , ↦′-read′
↣′⋆→↦′⋆ : ∀ {h l e l′ e′ h₀} →
Consistent h₀ l →
Equivalent h₀ l h →
h₀ ⊢ l , e ↣′⋆ l′ , e′ →
∃ λ h′ →
Consistent h₀ l′ ×
Equivalent h₀ l′ h′ ×
h , e ↦′⋆ h′ , e′
↣′⋆→↦′⋆ cons equiv [] = _ , cons , equiv , []
↣′⋆→↦′⋆ cons equiv (e↣′e′ ∷ e′↣′⋆e″) with ↣′→↦′ cons equiv e↣′e′
... | h′ , cons′ , equiv′ , e↦′e′ with ↣′⋆→↦′⋆ cons′ equiv′ e′↣′⋆e″
... | h″ , cons″ , equiv″ , e′↦′⋆e″ = h″ , cons″ , equiv″ , e↦′e′ ∷ e′↦′⋆e″