-
Notifications
You must be signed in to change notification settings - Fork 0
/
botworldScript.sml
286 lines (240 loc) · 9.13 KB
/
botworldScript.sml
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
open HolKernel Parse boolLib bossLib realTheory lcsymtacs
open botworld_dataTheory botworld_serialiseTheory botworld_preambleTheory
open terminationTheory
val _ = temp_tight_equality();
val _ = new_theory"botworld"
val _ = Parse.bring_to_front_overload","{Name=",",Thy="pair"};
val _ = Parse.type_abbrev("state",``:botworld_data$state``);
(* Port of Botworld to more idiomatic HOL *)
val neighbour_coords_def = Define`
neighbour_coords ((x,y):coordinate) =
[(x ,y+1)
;(x+1,y+1)
;(x+1,y )
;(x+1,y-1)
;(x ,y-1)
;(x-1,y-1)
;(x-1,y )
;(x-1,y+1)]`;
val opposite_def = Define`
opposite d = (d + 4) MOD 8`;
val neighbours_def = Define`
neighbours (g:grid) c = MAP (FLOOKUP g) (neighbour_coords c)`;
(* environment phase *)
val requests_def = Define`
requests i items r =
case r.command of
| Lift li => li = i ∧ canLift r (EL i items)
| Build is m => MEM i is ∧
EVERY (λi. i < LENGTH items) is ∧
IS_SOME (construct (MAP (λi. EL i items) is) m)
| _ => F`;
val contested_def = Define`
contested sq i ⇔
i < LENGTH sq.items ∧
1 < LENGTH (FILTER (requests i sq.items) (MAP SND sq.robots))`;
val fled_def = Define`
(fled nb (Move dir) ⇔ dir < LENGTH nb ∧ IS_SOME (EL dir nb)) ∧
(fled nb _ = F)`;
val inspectAttempts_def = Define`
inspectAttempts intents nm =
LENGTH (FILTER ($= (Inspect nm)) intents)`;
val inspectShielded_def = Define`
inspectShielded intents nm inventory ⇔
inspectAttempts intents nm ≤
LENGTH (FILTER isInspectShield inventory)`;
val destroyAttempts_def = Define`
destroyAttempts intents nm =
LENGTH (FILTER ($= (Destroy nm)) intents)`;
val destroyShielded_def = Define`
destroyShielded intents nm inventory ⇔
destroyAttempts intents nm ≤
LENGTH (FILTER isDestroyShield inventory)`;
val act_def = Define`
act sq nb r =
case r.command of
| Move dir =>
if dir < LENGTH nb then
(if IS_SOME (EL dir nb) then MovedOut else MoveBlocked) dir
else MoveBlocked dir
| Lift i =>
if i < LENGTH sq.items then
if canLift r (EL i sq.items) then
if contested sq i then GrappledOver i else Lifted i
else CannotLift i
else Invalid
| Drop i =>
if i < LENGTH r.inventory then
Dropped i
else Invalid
| Inspect nm =>
(case ALOOKUP sq.robots nm of
NONE => Invalid
| SOME r' => if ¬fled nb (r'.command) then
if ¬inspectShielded
(MAP (robot_command o SND) sq.robots) nm r'.inventory
then Inspected nm r'
else InspectBlocked nm
else InspectTargetFled nm)
| Destroy nm =>
(case ALOOKUP sq.robots nm of
NONE => Invalid
| SOME r' => if ¬fled nb (r'.command) then
if ¬destroyShielded
(MAP (robot_command o SND) sq.robots) nm r'.inventory
then Destroyed nm
else DestroyBlocked nm
else DestroyTargetFled nm)
| Build is m =>
if EVERY (λi. i < LENGTH sq.items) is then
if ¬EXISTS (contested sq) is then
case construct (MAP (λi. EL i sq.items) is) m of
| NONE => Invalid
| SOME r => Built is r
else BuildInterrupted is
else Invalid
| Pass => Passed`;
val localActions_def = Define`
localActions sq nb =
MAP (λ(nm,r). (nm, (r, act sq nb r))) sq.robots`;
val defend_def = Define`
defend intents nm =
dropN (destroyAttempts intents nm) isDestroyShield o
dropN (inspectAttempts intents nm) isInspectShield`;
val updateInventory_def = Define`
updateInventory sq nm (r,a) =
let intents = MAP (robot_command o SND) sq.robots in
(nm,
case a of
| MovedOut _ => r
| Lifted n => r with inventory := (EL n sq.items)::(defend intents nm r.inventory)
| Dropped n => r with inventory := (defend intents nm (remove_indices ($= n) 0 r.inventory))
| _ => r with inventory := defend intents nm r.inventory)`;
val incomingFrom_def = Define`
(incomingFrom dir NONE = []) ∧
(incomingFrom dir (SOME sq) =
MAP (λ(nm,r). (nm, (r, MovedIn dir)))
(FILTER (λ(nm,r). r.command = Move (opposite dir)) sq.robots))`;
val dropItem_def = Define`
dropItem (r,a) =
case a of Dropped i => [EL i r.inventory] | _ => []`;
val usedItem_def = Define`
usedItem actions i =
EXISTS (λa. case a of Lifted l => i = l
| Built is _ => MEM i is
| _ => F) actions`;
val event_def = Define`
event sq nb =
let oldRobotsActions = localActions sq nb in
let veterans = MAP (UNCURRY (updateInventory sq)) oldRobotsActions in
let actions = MAP (SND o SND) oldRobotsActions in
<| robotActions :=
let immigrations = FLAT (GENLIST (λdir. incomingFrom dir (EL dir nb)) (LENGTH nb)) in
MAP2 (λ(nm,r) a. (nm,(r,a))) veterans actions ++ immigrations
; createdRobots := MAP (SND o destBuilt) (FILTER isBuilt actions)
; untouchedItems := remove_indices (usedItem actions) 0 sq.items
; droppedItems := FLAT (MAP (dropItem o SND) oldRobotsActions)
; fallenItems :=
MAP (λ(nm,r). <|components := shatter r
;possessions := r.inventory|>)
(FILTER (λ(nm,r). MEM (Destroyed nm) actions) veterans)
|>`;
(* computation phase *)
val private_def = Define`
(private (Inspected _ r) = pInspected r.processor r.memory) ∧
(private Invalid = pInvalid) ∧
(private _ = pNothing)`;
(* TODO:
define this in botworld_preambleTheory, as the environment produced by
the translator (plus extra definitions as necessary) *)
val preamble_env_def = Define`
preamble_env (ffi:'ffi ffi_state) = ARB:('ffi semanticPrimitives$state # v environment)`;
val ffi_from_observation_def = Define`
ffi_from_observation obs m =
initial_ffi_state botworld_oracle
(encode_observation obs::[]::clear_register 0 m)`;
val run_policy_def = Define`
run_policy obs k m =
let ffi = ffi_from_observation obs m in
let (st,env) = preamble_env ffi in
let code = read_code m in
let (st',_) = evaluate_prog (st with clock := k) env code in
case st'.ffi.ffi_state of
(_::c::m') => (decode_command c, m')`;
val runMachine_def = Define`
runMachine ev (nm,r,a) =
let obs = observation nm ev (private a) in
let (c,m) = run_policy obs r.processor r.memory in
(obs.name, r with <| command := c; memory := m |>)`;
val _ = overload_on("destroyed",
``λnm ras. MEM (Destroyed nm) (MAP (SND o SND) ras)``);
val computeSquare_def = Define`
computeSquare t (c,ev) =
<| items :=
ev.untouchedItems ++ ev.droppedItems ++
FLAT (MAP (λc. c.components ++ c.possessions) ev.fallenItems)
; robots :=
let ls =
FILTER (λ(nm,(r,a)). ¬isMovedOut a ∧ ¬destroyed nm ev.robotActions)
ev.robotActions ++
MAPi (λi r. (name t c i, (r, Passed))) ev.createdRobots in
MAP (runMachine ev) ls
|>`;
(* state *)
val computeEvents_def = Define`
computeEvents (g:grid) =
FMAP_MAP2 (λ(c,sq). event sq (neighbours g c)) g`;
val step_def = Define`
step st =
<| grid := FMAP_MAP2 (computeSquare st.time_step) (computeEvents st.grid)
; time_step := st.time_step + 1 |>`;
val wf_state_def = Define`
wf_state st ⇔
(∀c1 c2 s1 s2.
FLOOKUP st.grid c1 = SOME s1 ∧
FLOOKUP st.grid c2 = SOME s2 ∧
c1 ≠ c2
⇒
DISJOINT (set (MAP FST s1.robots)) (set (MAP FST s2.robots))) ∧
(∀sq. sq ∈ FRANGE st.grid ⇒ ALL_DISTINCT (MAP FST sq.robots)) ∧
(∀sq nm. sq ∈ FRANGE st.grid ∧ MEM nm (MAP FST sq.robots) ⇒
nm.built_step < st.time_step)`;
val _ = Datatype`
state_with_hole = <| state : state
; focal_name : name
|>`;
val wf_state_with_hole_def = Define`
wf_state_with_hole s ⇔
wf_state s.state ∧
∃sq.
sq ∈ FRANGE s.state.grid ∧
MEM s.focal_name (MAP FST sq.robots)`;
val if_focal_def = Define`
if_focal fnm f (nm, r) = (nm, if nm = fnm then f r else r)`
val fill_def = Define`
fill f s =
s.state with grid updated_by $o_f
(λsq. sq with robots updated_by
MAP (if_focal s.focal_name f))`;
val _ = overload_on("with_command",``λc. robot_command_fupd (K c)``);
val _ = overload_on("inspected",
``λnm ras. ∃r. MEM (Inspected nm r) (MAP (SND o SND) ras)``);
val steph_def = Define`
steph c s =
let s' = fill (with_command c) s in
let events = computeEvents s'.grid in
if ∀ev::FRANGE events.
¬destroyed s.focal_name ev.robotActions ∧
¬inspected s.focal_name ev.robotActions
then
let (ev,a) = CHOICE
{ (ev,a) | ∃r. MEM (s.focal_name,(r,a)) ev.robotActions ∧
¬isMovedOut a ∧ ev ∈ FRANGE events } in
SOME (observation s.focal_name ev (private a), s with state := step s')
else NONE
`;
(* histories *)
val _ = Parse.type_abbrev("history",``:state llist``);
val hist_def = Define`
(hist:state->history) s = LUNFOLD (λs. SOME (step s,s)) s`;
val _ = export_theory()