Mercurial > hg > Gears > GearsAgda
comparison ModelChecking.agda @ 721:2abfce56523a
init 5 phils done without infinite loop
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Sun, 22 May 2022 19:07:20 +0900 |
parents | e9d781c38629 |
children | b088fa199d3d |
comparison
equal
deleted
inserted
replaced
720:e9d781c38629 | 721:2abfce56523a |
---|---|
92 E_cas_fail : Error | 92 E_cas_fail : Error |
93 | 93 |
94 -- | 94 -- |
95 -- Data Gear | 95 -- Data Gear |
96 -- | 96 -- |
97 -- waiting / pointer / available | |
98 -- | |
97 data Data : Set where | 99 data Data : Set where |
98 -- D_AtomicNat : AtomicNat → ℕ → Data | 100 -- D_AtomicNat : AtomicNat → ℕ → Data |
99 D_AtomicNat : AtomicNat → Data | 101 D_AtomicNat : AtomicNat → Data |
100 D_Phil : Phil → Data | 102 D_Phil : Phil → Data |
101 D_Error : Error → Data | 103 D_Error : Error → Data |
138 ... | tri≈ ¬a b ¬c = next (AtomicNat-API.next api) ( record c { memory = bt ; c_AtomicNat-API = record api { impl = prev } } ) -- update memory | 140 ... | tri≈ ¬a b ¬c = next (AtomicNat-API.next api) ( record c { memory = bt ; c_AtomicNat-API = record api { impl = prev } } ) -- update memory |
139 ... | tri< a₁ ¬b ¬c = next (AtomicNat-API.fail api) c --- cleaer API | 141 ... | tri< a₁ ¬b ¬c = next (AtomicNat-API.fail api) c --- cleaer API |
140 ... | tri> ¬a ¬b _ = next (AtomicNat-API.fail api) c | 142 ... | tri> ¬a ¬b _ = next (AtomicNat-API.fail api) c |
141 f_cas a bt = next ( Context.fail c ) c -- type error / panic | 143 f_cas a bt = next ( Context.fail c ) c -- type error / panic |
142 | 144 |
143 | 145 -- putdown_rfork : Phil → (? → t) → t |
144 | 146 -- putdown_rfork p next = goto p->lfork->cas( 0 , putdown_lfork, putdown_lfork ) , next |
145 -- putdown_rfork p next = f_set (Phil.right p) 0 ( λ f → putdown_lfork record p { right = f } next ) | 147 |
146 -- goto p->cas( 0 , putdown_lfork ) | |
147 Phil_putdown_rfork_sub : {n : Level} {t : Set n} → Context → ( Code → Context → t ) → t | 148 Phil_putdown_rfork_sub : {n : Level} {t : Set n} → Context → ( Code → Context → t ) → t |
148 Phil_putdown_rfork_sub c next = next C_cas_int record c { | 149 Phil_putdown_rfork_sub c next = next C_cas_int record c { |
149 c_AtomicNat-API = record { impl = Phil.right phil ; value = 0 ; fail = C_putdown_lfork ; next = C_putdown_lfork } } where | 150 c_AtomicNat-API = record { impl = Phil.right phil ; value = 0 ; fail = C_putdown_lfork ; next = C_putdown_lfork } } where |
150 phil : Phil | 151 phil : Phil |
151 phil = Phil-API.impl ( Context.c_Phil-API c ) | 152 phil = Phil-API.impl ( Context.c_Phil-API c ) |
152 | 153 |
153 Phil_putdown_lfork_sub : {n : Level} {t : Set n} → Context → ( Code → Context → t ) → t | 154 Phil_putdown_lfork_sub : {n : Level} {t : Set n} → Context → ( Code → Context → t ) → t |
226 init-AtomicNat1 : {n : Level} {t : Set n} → Context → ( Context → AtomicNat → t ) → t | 227 init-AtomicNat1 : {n : Level} {t : Set n} → Context → ( Context → AtomicNat → t ) → t |
227 init-AtomicNat1 c1 next = new-data c1 ( λ ptr → D_AtomicNat (a ptr) ) $ λ c2 ptr → next c2 (a ptr) where | 228 init-AtomicNat1 c1 next = new-data c1 ( λ ptr → D_AtomicNat (a ptr) ) $ λ c2 ptr → next c2 (a ptr) where |
228 a : ℕ → AtomicNat | 229 a : ℕ → AtomicNat |
229 a ptr = record { ptr = ptr ; value = 0 } | 230 a ptr = record { ptr = ptr ; value = 0 } |
230 | 231 |
231 init-Phil-1 : (ps : List Context) → (left right : AtomicNat ) → List Context | 232 init-Phil-1 : (c1 : Context) → Context |
232 init-Phil-1 ps left right = new-data (c1 ps) ( λ ptr → D_Phil (p ptr) ) $ λ c ptr → record c { c_Phil-API = record { next = C_thinking ; impl = p ptr }} ∷ ps where | 233 init-Phil-1 c1 = record c1 { memory = mem2 $ mem1 $ mem ; mbase = n + 3 } where |
233 p : ℕ → Phil | 234 n : ℕ |
234 p ptr = record init-Phil { ptr = ptr ; left = left ; right = right } | 235 n = Context.mbase c1 |
235 c1 : List Context → Context | 236 left = record { ptr = suc n ; value = 0} |
236 c1 [] = init-context | 237 right = record { ptr = suc (suc n) ; value = 0} |
237 c1 (c2 ∷ ct) = c2 | 238 mem : bt Data |
238 | 239 mem = insertTree ( Context.memory c1) (suc n) (D_AtomicNat left) |
239 test-j0 : {n : Level} {t : Set n} → Context → (Context → AtomicNat → t) → t | 240 $ λ t → t |
240 test-j0 c next = init-AtomicNat1 c next | 241 mem1 : bt Data → bt Data |
241 | 242 mem1 t = insertTree t (suc (suc n)) (D_AtomicNat right ) |
242 test-j2 : AtomicNat | 243 $ λ t → t |
243 test-j2 = test-j0 init-context (λ c f → f ) | 244 mem2 : bt Data → bt Data |
244 | 245 mem2 t = insertTree t (n + 3) (D_Phil record { ptr = n + 3 ; left = left ; right = right }) |
245 test-i1 : Context → Context | 246 $ λ t → t |
246 test-i1 c = init-AtomicNat1 c $ λ c f → c | 247 |
247 | 248 test-i0 : bt Data |
248 test-i0 : Context | 249 test-i0 = Context.memory (init-Phil-1 init-context) |
249 test-i0 = test-i1 init-context | 250 |
250 | 251 make-phil : ℕ → Context |
251 test-i2 : Context | 252 make-phil zero = init-context |
252 test-i2 = test-i1 test-i0 | 253 make-phil (suc n) = init-Phil-1 ( make-phil n ) |
253 | 254 |
254 test20 : bt (Data) | 255 test-i5 : bt Data |
255 test20 = init-AtomicNat1 init-context $ λ c1 left → alloc-data c1 | 256 test-i5 = Context.memory (make-phil 5) |
256 $ λ c n → Context.memory c | |
257 | |
258 -- infinite loop | |
259 test21 : bt (Data) | |
260 test21 = init-AtomicNat1 init-context $ λ c1 left → alloc-data c1 | |
261 $ λ c n → insertTree ( Context.memory c) 2 (D_AtomicNat record { ptr = 2 ; value = 0 }) $ λ t → t | |
262 | |
263 -- Ok | |
264 test212 : bt Data | |
265 test212 = insertTree (node 1 (D_AtomicNat record { ptr = 1 ; value = 0 }) leaf leaf ) 2 (D_AtomicNat record { ptr = 2 ; value = 0 }) | |
266 $ λ t → t | |
267 | |
268 open _∧_ | |
269 | |
270 test213 : bt Data | |
271 test213 = insertTree (test20) 2 (D_AtomicNat record { ptr = 2 ; value = 0 }) $ λ t → t | |
272 | |
273 test2 : List Context | |
274 test2 = init-AtomicNat1 init-context $ λ c left → alloc-data c | |
275 $ λ c n → insertTree (Context.memory c) n (D_AtomicNat (a n)) | |
276 $ λ bt → record c { memory = bt } ∷ [] where | |
277 a : ℕ → AtomicNat | |
278 a ptr = record { ptr = ptr ; value = 0 } | |
279 | |
280 test-newdata : bt Data | |
281 test-newdata = new-data init-context ( λ ptr → D_AtomicNat (a ptr) ) $ λ c n → Context.memory c where | |
282 a : ℕ → AtomicNat | |
283 a ptr = record { ptr = ptr ; value = 0 } | |
284 | |
285 test0 : List Context | |
286 test0 = init-AtomicNat1 init-context $ λ c left → c ∷ [] | |
287 | |
288 test01 : bt Data | |
289 test01 = alloc-data init-context $ λ c1 n → insertTree (Context.memory c1) n (D_AtomicNat (a n)) ( λ bt → next1 bt record c1 { memory = bt } n ) where | |
290 a : ℕ → AtomicNat | |
291 a ptr = record { ptr = ptr ; value = 0 } | |
292 next1 : bt Data → Context → ℕ → bt Data | |
293 next1 t c1 n = t | |
294 | |
295 test01-tree : bt Data | |
296 test01-tree = node 1 (D_AtomicNat record { ptr = 1 ; value = 0 } ) leaf leaf | |
297 | |
298 test01-assert : test01-tree ≡ test01 | |
299 test01-assert = refl | |
300 | |
301 tail1 : Maybe (List (bt Data)) → Maybe (List (bt Data)) | |
302 tail1 (just x) = tail x | |
303 tail1 nothing = nothing | |
304 | |
305 test22 : Context | |
306 test22 = init-AtomicNat1 init-context $ λ c left → init-AtomicNat1 c $ λ c1 right → c1 | |
307 | |
308 test : List Context | |
309 test = init-AtomicNat1 init-context $ λ c left → init-AtomicNat1 c $ λ c1 right → init-Phil-1 (c1 ∷ []) left right | |
310 | |
311 test1 : List Context | |
312 test1 = init-AtomicNat1 init-context $ λ c left → init-AtomicNat1 c $ λ c1 right → step (init-Phil-1 (c1 ∷ []) left right) (λ ps → ps ) | |
313 | |
314 | 257 |
315 -- loop exexution | 258 -- loop exexution |
316 | 259 |
317 -- concurrnt execution | 260 -- concurrnt execution |
318 | 261 |