annotate ModelChecking.agda @ 726:e545646e5f64

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 10 Apr 2023 17:57:26 +0900
parents b088fa199d3d
children 0b791ae19543
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
635
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 module ModelChecking where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
2
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
3 open import Level renaming (zero to Z ; suc to succ)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
4
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 open import Data.Nat hiding (compare)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 open import Data.Nat.Properties as NatProp
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 open import Data.Maybe
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
8 -- open import Data.Maybe.Properties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 open import Data.Empty
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 open import Data.List
710
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 709
diff changeset
11 -- open import Data.Tree.Binary
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 709
diff changeset
12 -- open import Data.Product
635
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 open import Function as F hiding (const)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
14 open import Relation.Binary
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 open import Relation.Binary.PropositionalEquality
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
16 open import Relation.Nullary
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 open import logic
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
18 open import Data.Unit hiding ( _≟_ ; _≤?_ ; _≤_)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
19 open import Relation.Binary.Definitions
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
20
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
21 record AtomicNat : Set where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
22 field
714
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 713
diff changeset
23 ptr : ℕ -- memory pointer ( unique id of DataGear )
635
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
24 value : ℕ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
25
713
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 712
diff changeset
26 init-AtomicNat : AtomicNat
714
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 713
diff changeset
27 init-AtomicNat = record { ptr = 0 ; value = 0 }
713
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 712
diff changeset
28
709
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 708
diff changeset
29 --
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 708
diff changeset
30 -- single process implemenation
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 708
diff changeset
31 --
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 708
diff changeset
32
710
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 709
diff changeset
33 f_set : {n : Level } {t : Set n} → AtomicNat → (value : ℕ) → ( AtomicNat → t ) → t
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 709
diff changeset
34 f_set a v next = next record a { value = v }
635
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
35
712
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 711
diff changeset
36 record Phil : Set where
635
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
37 field
714
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 713
diff changeset
38 ptr : ℕ
635
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
39 left right : AtomicNat
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
40
713
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 712
diff changeset
41 init-Phil : Phil
714
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 713
diff changeset
42 init-Phil = record { ptr = 0 ; left = init-AtomicNat ; right = init-AtomicNat }
713
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 712
diff changeset
43
712
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 711
diff changeset
44 pickup_rfork : {n : Level} {t : Set n} → Phil → ( Phil → t ) → t
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 711
diff changeset
45 pickup_lfork : {n : Level} {t : Set n} → Phil → ( Phil → t ) → t
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 711
diff changeset
46 eating : {n : Level} {t : Set n} → Phil → ( Phil → t ) → t
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 711
diff changeset
47 putdown_rfork : {n : Level} {t : Set n} → Phil → ( Phil → t ) → t
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 711
diff changeset
48 putdown_lfork : {n : Level} {t : Set n} → Phil → ( Phil → t ) → t
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 711
diff changeset
49 thinking : {n : Level} {t : Set n} → Phil → ( Phil → t ) → t
635
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
50
714
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 713
diff changeset
51 pickup_rfork p next = f_set (Phil.right p) (Phil.ptr p) ( λ f → pickup_lfork record p { right = f } next )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 713
diff changeset
52 pickup_lfork p next = f_set (Phil.left p) (Phil.ptr p) ( λ f → eating record p { left = f } next )
709
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 708
diff changeset
53 eating p next = putdown_rfork p next
712
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 711
diff changeset
54 putdown_rfork p next = f_set (Phil.right p) 0 ( λ f → putdown_lfork record p { right = f } next )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 711
diff changeset
55 putdown_lfork p next = f_set (Phil.left p) 0 ( λ f → thinking record p { left = f } next )
709
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 708
diff changeset
56 thinking p next = next p
635
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
57
712
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 711
diff changeset
58 run : Phil
716
a36147bb596d fix context
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 715
diff changeset
59 run = pickup_rfork record { ptr = 1 ; left = record { ptr = 2 ; value = 0 } ; right = record { ptr = 3 ; value = 0 } } $ λ p → p
635
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
60
709
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 708
diff changeset
61 --
711
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 710
diff changeset
62 -- Coda Gear
709
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 708
diff changeset
63 --
635
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
64
636
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 635
diff changeset
65 data Code : Set where
713
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 712
diff changeset
66 C_nop : Code
711
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 710
diff changeset
67 C_cas_int : Code
636
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 635
diff changeset
68 C_putdown_rfork : Code
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 635
diff changeset
69 C_putdown_lfork : Code
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 635
diff changeset
70 C_thinking : Code
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 635
diff changeset
71 C_pickup_rfork : Code
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 635
diff changeset
72 C_pickup_lfork : Code
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 635
diff changeset
73 C_eating : Code
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 635
diff changeset
74
709
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 708
diff changeset
75 --
713
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 712
diff changeset
76 -- all possible arguments in -APIs
709
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 708
diff changeset
77 --
713
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 712
diff changeset
78 record AtomicNat-API : Set where
636
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 635
diff changeset
79 field
709
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 708
diff changeset
80 next : Code
711
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 710
diff changeset
81 fail : Code
709
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 708
diff changeset
82 value : ℕ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 708
diff changeset
83 impl : AtomicNat
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 708
diff changeset
84
713
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 712
diff changeset
85 record Phil-API : Set where
709
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 708
diff changeset
86 field
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 708
diff changeset
87 next : Code
712
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 711
diff changeset
88 impl : Phil
709
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 708
diff changeset
89
713
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 712
diff changeset
90 data Error : Set where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 712
diff changeset
91 E_panic : Error
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 712
diff changeset
92 E_cas_fail : Error
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 712
diff changeset
93
711
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 710
diff changeset
94 --
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 710
diff changeset
95 -- Data Gear
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 710
diff changeset
96 --
721
2abfce56523a init 5 phils done without infinite loop
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 720
diff changeset
97 -- waiting / pointer / available
2abfce56523a init 5 phils done without infinite loop
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 720
diff changeset
98 --
709
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 708
diff changeset
99 data Data : Set where
715
8f8f136f2162 insertTree have some bug
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 714
diff changeset
100 -- D_AtomicNat : AtomicNat → ℕ → Data
710
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 709
diff changeset
101 D_AtomicNat : AtomicNat → Data
712
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 711
diff changeset
102 D_Phil : Phil → Data
713
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 712
diff changeset
103 D_Error : Error → Data
710
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 709
diff changeset
104
715
8f8f136f2162 insertTree have some bug
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 714
diff changeset
105 -- data API : Set where
8f8f136f2162 insertTree have some bug
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 714
diff changeset
106 -- A_AtomicNat : AtomicNat-API → API
8f8f136f2162 insertTree have some bug
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 714
diff changeset
107 -- A_Phil : Phil-API → API
710
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 709
diff changeset
108
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 709
diff changeset
109 open import hoareBinaryTree
709
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 708
diff changeset
110
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 708
diff changeset
111 record Context : Set where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 708
diff changeset
112 field
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 708
diff changeset
113 next : Code
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 708
diff changeset
114 fail : Code
711
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 710
diff changeset
115
715
8f8f136f2162 insertTree have some bug
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 714
diff changeset
116 -- -API list (frame in Gears Agda ) --- a Data of API
8f8f136f2162 insertTree have some bug
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 714
diff changeset
117 -- api : List API
8f8f136f2162 insertTree have some bug
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 714
diff changeset
118 -- c_Phil-API : Maybe Phil-API
713
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 712
diff changeset
119 c_Phil-API : Phil-API
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 712
diff changeset
120 c_AtomicNat-API : AtomicNat-API
711
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 710
diff changeset
121
710
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 709
diff changeset
122 mbase : ℕ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 709
diff changeset
123 memory : bt Data
709
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 708
diff changeset
124 error : Data
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 708
diff changeset
125 fail_next : Code
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 708
diff changeset
126
710
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 709
diff changeset
127 --
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 709
diff changeset
128 -- second level stub
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 709
diff changeset
129 --
712
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 711
diff changeset
130 AtomicInt_cas_stub : {n : Level} {t : Set n} → Context → ( Code → Context → t ) → t
716
a36147bb596d fix context
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 715
diff changeset
131 AtomicInt_cas_stub {_} {t} c next = updateTree (Context.memory c) (AtomicNat.ptr ai) ( D_AtomicNat (record ai { value = AtomicNat-API.value api } ))
711
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 710
diff changeset
132 ( λ bt → next ( Context.fail c ) c ) -- segmentation fault ( null pointer )
716
a36147bb596d fix context
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 715
diff changeset
133 $ λ prev bt → f_cas prev bt where
713
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 712
diff changeset
134 api : AtomicNat-API
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 712
diff changeset
135 api = Context.c_AtomicNat-API c
711
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 710
diff changeset
136 ai : AtomicNat
713
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 712
diff changeset
137 ai = AtomicNat-API.impl api
716
a36147bb596d fix context
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 715
diff changeset
138 f_cas : Data → bt Data → t
a36147bb596d fix context
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 715
diff changeset
139 f_cas (D_AtomicNat prev) bt with <-cmp ( AtomicNat.value ai ) ( AtomicNat.value prev )
a36147bb596d fix context
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 715
diff changeset
140 ... | tri≈ ¬a b ¬c = next (AtomicNat-API.next api) ( record c { memory = bt ; c_AtomicNat-API = record api { impl = prev } } ) -- update memory
715
8f8f136f2162 insertTree have some bug
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 714
diff changeset
141 ... | tri< a₁ ¬b ¬c = next (AtomicNat-API.fail api) c --- cleaer API
713
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 712
diff changeset
142 ... | tri> ¬a ¬b _ = next (AtomicNat-API.fail api) c
716
a36147bb596d fix context
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 715
diff changeset
143 f_cas a bt = next ( Context.fail c ) c -- type error / panic
a36147bb596d fix context
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 715
diff changeset
144
721
2abfce56523a init 5 phils done without infinite loop
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 720
diff changeset
145 -- putdown_rfork : Phil → (? → t) → t
2abfce56523a init 5 phils done without infinite loop
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 720
diff changeset
146 -- putdown_rfork p next = goto p->lfork->cas( 0 , putdown_lfork, putdown_lfork ) , next
709
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 708
diff changeset
147
712
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 711
diff changeset
148 Phil_putdown_rfork_sub : {n : Level} {t : Set n} → Context → ( Code → Context → t ) → t
721
2abfce56523a init 5 phils done without infinite loop
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 720
diff changeset
149 Phil_putdown_rfork_sub c next = next C_cas_int record c {
716
a36147bb596d fix context
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 715
diff changeset
150 c_AtomicNat-API = record { impl = Phil.right phil ; value = 0 ; fail = C_putdown_lfork ; next = C_putdown_lfork } } where
712
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 711
diff changeset
151 phil : Phil
713
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 712
diff changeset
152 phil = Phil-API.impl ( Context.c_Phil-API c )
709
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 708
diff changeset
153
712
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 711
diff changeset
154 Phil_putdown_lfork_sub : {n : Level} {t : Set n} → Context → ( Code → Context → t ) → t
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 711
diff changeset
155 Phil_putdown_lfork_sub c next = next C_cas_int record c {
716
a36147bb596d fix context
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 715
diff changeset
156 c_AtomicNat-API = record { impl = Phil.left phil ; value = 0 ; fail = C_thinking ; next = C_thinking } } where
712
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 711
diff changeset
157 phil : Phil
713
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 712
diff changeset
158 phil = Phil-API.impl ( Context.c_Phil-API c )
709
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 708
diff changeset
159
712
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 711
diff changeset
160 Phil_thiking : {n : Level} {t : Set n} → Context → ( Code → Context → t ) → t
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 711
diff changeset
161 Phil_thiking c next = next C_pickup_rfork c
709
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 708
diff changeset
162
712
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 711
diff changeset
163 Phil_pickup_lfork_sub : {n : Level} {t : Set n} → Context → ( Code → Context → t ) → t
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 711
diff changeset
164 Phil_pickup_lfork_sub c next = next C_cas_int record c {
716
a36147bb596d fix context
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 715
diff changeset
165 c_AtomicNat-API = record { impl = Phil.left phil ; value = Phil.ptr phil ; fail = C_pickup_lfork ; next = C_pickup_rfork } } where
712
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 711
diff changeset
166 phil : Phil
713
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 712
diff changeset
167 phil = Phil-API.impl ( Context.c_Phil-API c )
709
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 708
diff changeset
168
712
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 711
diff changeset
169 Phil_pickup_rfork_sub : {n : Level} {t : Set n} → Context → ( Code → Context → t ) → t
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 711
diff changeset
170 Phil_pickup_rfork_sub c next = next C_cas_int record c {
716
a36147bb596d fix context
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 715
diff changeset
171 c_AtomicNat-API = record { impl = Phil.left phil ; value = Phil.ptr phil ; fail = C_pickup_rfork ; next = C_eating } } where
712
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 711
diff changeset
172 phil : Phil
713
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 712
diff changeset
173 phil = Phil-API.impl ( Context.c_Phil-API c )
709
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 708
diff changeset
174
712
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 711
diff changeset
175 Phil_eating_sub : {n : Level} {t : Set n} → Context → ( Code → Context → t ) → t
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 711
diff changeset
176 Phil_eating_sub c next = next C_putdown_rfork c
709
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 708
diff changeset
177
711
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 710
diff changeset
178 code_table : {n : Level} {t : Set n} → Code → Context → ( Code → Context → t) → t
713
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 712
diff changeset
179 code_table C_nop = λ c next → next C_nop c
712
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 711
diff changeset
180 code_table C_cas_int = AtomicInt_cas_stub
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 711
diff changeset
181 code_table C_putdown_rfork = Phil_putdown_rfork_sub
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 711
diff changeset
182 code_table C_putdown_lfork = Phil_putdown_lfork_sub
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 711
diff changeset
183 code_table C_thinking = Phil_thiking
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 711
diff changeset
184 code_table C_pickup_rfork = Phil_pickup_lfork_sub
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 711
diff changeset
185 code_table C_pickup_lfork = Phil_pickup_rfork_sub
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 711
diff changeset
186 code_table C_eating = Phil_eating_sub
636
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 635
diff changeset
187
709
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 708
diff changeset
188 step : {n : Level} {t : Set n} → List Context → (List Context → t) → t
708
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 637
diff changeset
189 step {n} {t} [] next0 = next0 []
716
a36147bb596d fix context
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 715
diff changeset
190 step {n} {t} (p ∷ ps) next0 = code_table (Context.next p) p ( λ code np → next0 (update-context ps np ++ ( record np { next = code } ∷ [] ))) where
a36147bb596d fix context
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 715
diff changeset
191 update-context : List Context → Context → List Context
a36147bb596d fix context
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 715
diff changeset
192 update-context [] _ = []
a36147bb596d fix context
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 715
diff changeset
193 update-context (c1 ∷ t) np = record c1 { memory = Context.memory np ; mbase = Context.mbase np } ∷ t
708
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 637
diff changeset
194
713
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 712
diff changeset
195 init-context : Context
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 712
diff changeset
196 init-context = record {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 712
diff changeset
197 next = C_nop
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 712
diff changeset
198 ; fail = C_nop
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 712
diff changeset
199 ; c_Phil-API = record { next = C_nop ; impl = init-Phil }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 712
diff changeset
200 ; c_AtomicNat-API = record { next = C_nop ; fail = C_nop ; value = 0 ; impl = init-AtomicNat }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 712
diff changeset
201 ; mbase = 0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 712
diff changeset
202 ; memory = leaf
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 712
diff changeset
203 ; error = D_Error E_panic
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 712
diff changeset
204 ; fail_next = C_nop
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 712
diff changeset
205 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 712
diff changeset
206
718
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 717
diff changeset
207 alloc-data : {n : Level} {t : Set n} → ( c : Context ) → ( Context → ℕ → t ) → t
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 717
diff changeset
208 alloc-data {n} {t} c next = next record c { mbase = suc ( Context.mbase c ) } mnext where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 717
diff changeset
209 mnext = suc ( Context.mbase c )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 717
diff changeset
210
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 717
diff changeset
211 new-data : {n : Level} {t : Set n} → ( c : Context ) → (ℕ → Data ) → ( Context → ℕ → t ) → t
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 717
diff changeset
212 new-data c x next = alloc-data c $ λ c1 n → insertTree (Context.memory c1) n (x n) ( λ bt → next record c1 { memory = bt } n )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 717
diff changeset
213
720
e9d781c38629 Ok this is a termination bug og agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 719
diff changeset
214 init-AtomicNat0 : {n : Level} {t : Set n} → Context → ( Context → ℕ → t ) → t
e9d781c38629 Ok this is a termination bug og agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 719
diff changeset
215 init-AtomicNat0 c1 next = new-data c1 ( λ ptr → D_AtomicNat (a ptr) ) $ λ c2 ptr → next c2 ptr where
e9d781c38629 Ok this is a termination bug og agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 719
diff changeset
216 a : ℕ → AtomicNat
e9d781c38629 Ok this is a termination bug og agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 719
diff changeset
217 a ptr = record { ptr = ptr ; value = 0 }
e9d781c38629 Ok this is a termination bug og agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 719
diff changeset
218
e9d781c38629 Ok this is a termination bug og agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 719
diff changeset
219 init-Phil-0 : (ps : List Context) → (left right : AtomicNat ) → List Context
e9d781c38629 Ok this is a termination bug og agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 719
diff changeset
220 init-Phil-0 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
e9d781c38629 Ok this is a termination bug og agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 719
diff changeset
221 p : ℕ → Phil
e9d781c38629 Ok this is a termination bug og agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 719
diff changeset
222 p ptr = record init-Phil { ptr = ptr ; left = left ; right = right }
e9d781c38629 Ok this is a termination bug og agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 719
diff changeset
223 c1 : List Context → Context
e9d781c38629 Ok this is a termination bug og agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 719
diff changeset
224 c1 [] = init-context
e9d781c38629 Ok this is a termination bug og agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 719
diff changeset
225 c1 (c2 ∷ ct) = c2
e9d781c38629 Ok this is a termination bug og agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 719
diff changeset
226
718
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 717
diff changeset
227 init-AtomicNat1 : {n : Level} {t : Set n} → Context → ( Context → AtomicNat → t ) → t
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 717
diff changeset
228 init-AtomicNat1 c1 next = new-data c1 ( λ ptr → D_AtomicNat (a ptr) ) $ λ c2 ptr → next c2 (a ptr) where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 717
diff changeset
229 a : ℕ → AtomicNat
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 717
diff changeset
230 a ptr = record { ptr = ptr ; value = 0 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 717
diff changeset
231
721
2abfce56523a init 5 phils done without infinite loop
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 720
diff changeset
232 init-Phil-1 : (c1 : Context) → Context
2abfce56523a init 5 phils done without infinite loop
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 720
diff changeset
233 init-Phil-1 c1 = record c1 { memory = mem2 $ mem1 $ mem ; mbase = n + 3 } where
2abfce56523a init 5 phils done without infinite loop
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 720
diff changeset
234 n : ℕ
2abfce56523a init 5 phils done without infinite loop
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 720
diff changeset
235 n = Context.mbase c1
2abfce56523a init 5 phils done without infinite loop
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 720
diff changeset
236 left = record { ptr = suc n ; value = 0}
2abfce56523a init 5 phils done without infinite loop
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 720
diff changeset
237 right = record { ptr = suc (suc n) ; value = 0}
2abfce56523a init 5 phils done without infinite loop
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 720
diff changeset
238 mem : bt Data
2abfce56523a init 5 phils done without infinite loop
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 720
diff changeset
239 mem = insertTree ( Context.memory c1) (suc n) (D_AtomicNat left)
2abfce56523a init 5 phils done without infinite loop
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 720
diff changeset
240 $ λ t → t
2abfce56523a init 5 phils done without infinite loop
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 720
diff changeset
241 mem1 : bt Data → bt Data
2abfce56523a init 5 phils done without infinite loop
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 720
diff changeset
242 mem1 t = insertTree t (suc (suc n)) (D_AtomicNat right )
2abfce56523a init 5 phils done without infinite loop
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 720
diff changeset
243 $ λ t → t
2abfce56523a init 5 phils done without infinite loop
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 720
diff changeset
244 mem2 : bt Data → bt Data
2abfce56523a init 5 phils done without infinite loop
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 720
diff changeset
245 mem2 t = insertTree t (n + 3) (D_Phil record { ptr = n + 3 ; left = left ; right = right })
2abfce56523a init 5 phils done without infinite loop
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 720
diff changeset
246 $ λ t → t
2abfce56523a init 5 phils done without infinite loop
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 720
diff changeset
247
2abfce56523a init 5 phils done without infinite loop
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 720
diff changeset
248 test-i0 : bt Data
2abfce56523a init 5 phils done without infinite loop
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 720
diff changeset
249 test-i0 = Context.memory (init-Phil-1 init-context)
718
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 717
diff changeset
250
721
2abfce56523a init 5 phils done without infinite loop
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 720
diff changeset
251 make-phil : ℕ → Context
2abfce56523a init 5 phils done without infinite loop
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 720
diff changeset
252 make-phil zero = init-context
2abfce56523a init 5 phils done without infinite loop
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 720
diff changeset
253 make-phil (suc n) = init-Phil-1 ( make-phil n )
715
8f8f136f2162 insertTree have some bug
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 714
diff changeset
254
721
2abfce56523a init 5 phils done without infinite loop
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 720
diff changeset
255 test-i5 : bt Data
2abfce56523a init 5 phils done without infinite loop
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 720
diff changeset
256 test-i5 = Context.memory (make-phil 5)
708
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 637
diff changeset
257
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 637
diff changeset
258 -- loop exexution
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 637
diff changeset
259
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 637
diff changeset
260 -- concurrnt execution
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 637
diff changeset
261
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 637
diff changeset
262 -- state db ( binary tree of processes )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 637
diff changeset
263
722
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 721
diff changeset
264 -- depth first execution
708
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 637
diff changeset
265
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 637
diff changeset
266 -- verify temporal logic poroerries
636
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 635
diff changeset
267
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 635
diff changeset
268
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 635
diff changeset
269