-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.ml
359 lines (328 loc) · 8.48 KB
/
action.ml
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
(*
* Author: Kaustuv Chaudhuri <[email protected]>
* Copyright (C) 2013 Inria (Institut National de Recherche
* en Informatique et en Automatique)
* See LICENSE for licensing details.
*)
open Batteries
open Syntax
module Src = Syntax_fmt.Src
type mmode =
| Unmarked
| Marked
type snap = {
mmode : mmode ;
form : form ;
}
type history = {
work : snap ;
dirty : bool ;
past : snap list ;
present : snap ;
future : snap list ;
}
(* work and present are the same formula, just with (possibly)
different cursor positions and/or marks *)
let init f =
let cur = {
form = f ;
mmode = Unmarked ;
} in {
work = cur ; dirty = false ;
past = [] ; present = cur ; future = []
}
let is_history_of hi f =
let cand =
begin match hi.past with
| [] -> hi.present.form
| _ -> (List.last hi.past).form
end in
aeq_forms cand f
let strip_mmode snap = snap.form
let render hi =
let past = List.map strip_mmode hi.past in
let cur = strip_mmode hi.work in
let fut = List.map strip_mmode hi.future in
(past, cur, fut)
type action_error =
| Cancelled
| Invalid of string
exception Action of action_error
let explain = function
| Cancelled -> ""
| Invalid msg -> msg
let actfail msg = raise (Action (Invalid msg))
let tinker ~fn hi =
try
let work = fn hi.work in
{ hi with dirty = true ; work }
with
| Traversal.Traversal_failure err -> actfail (Traversal.explain err)
| Rules.Rule_failure err -> actfail (Rules.explain err)
let commit ~fn hi =
try
let present = fn hi.work in
let past = hi.work :: hi.past in
{ work = present ; dirty = false ; past ; present ; future = [] }
with
| Traversal.Traversal_failure err -> actfail (Traversal.explain err)
| Rules.Link_matching err -> actfail (Rules.explain_link_error err)
| Rules.Rule_failure err -> actfail (Rules.explain err)
type action = {
enabled : history -> bool ;
perform : history -> history ;
}
type t = action
let action_undo = {
enabled = (fun hi -> hi.dirty || hi.past <> []) ;
perform = begin fun hi ->
if hi.dirty then
{ hi with dirty = false ; work = hi.present }
else begin
match hi.past with
| p :: past ->
{ work = p ;
dirty = false ;
past ;
present = p ;
future = hi.present :: hi.future }
| _ ->
actfail "no previous states"
end
end }
let action_redo = {
enabled = (fun hi -> hi.future <> []) ;
perform = begin fun hi ->
begin match hi.future with
| f :: future ->
{ work = f ;
dirty = false ;
past = hi.present :: hi.past ;
present = f ;
future }
| _ -> actfail "no future states"
end
end }
let action_descend = {
enabled = begin fun hi ->
let (_, f) = unsubst hi.work.form in
match f with
| Conn (_, _ :: _) -> true
| _ -> false
end ;
perform = begin fun hi ->
tinker hi ~fn:begin
fun snap ->
let form = Traversal.go_down 0 snap.form in
{ snap with form }
end
end }
let action_ascend = {
enabled = begin fun hi ->
let (fcx, _) = unsubst hi.work.form in
not (Fcx.is_empty fcx)
end ;
perform = begin fun hi ->
tinker hi ~fn:begin
fun snap ->
let form = Traversal.go_up snap.form in
{ snap with form }
end
end }
let action_ascend_to_top = {
enabled = (fun _ -> true) ;
perform = begin fun hi ->
tinker hi ~fn:begin
fun snap ->
let form = Traversal.go_top snap.form in
{ snap with form }
end
end }
let action_left = {
enabled = begin fun hi ->
let (fcx, _) = unsubst hi.work.form in
match Fcx.rear fcx with
| Some (_, {left = _ :: _ ; _}) -> true
| _ -> false
end ;
perform = begin fun hi ->
tinker hi ~fn:begin
fun snap ->
let form = Traversal.go_left snap.form in
{ snap with form }
end
end }
let action_right = {
enabled = begin fun hi ->
let (fcx, _) = unsubst hi.work.form in
match Fcx.rear fcx with
| Some (_, {right = _ :: _ ; _}) -> true
| _ -> false
end ;
perform = begin fun hi ->
tinker hi ~fn:begin
fun snap ->
let form = Traversal.go_right snap.form in
{ snap with form }
end
end }
let action_mark_source = {
enabled = (fun hi -> hi.work.mmode = Unmarked) ;
perform = begin fun hi ->
tinker hi ~fn:begin
fun snap ->
let (fcx, f) = unsubst snap.form in
Log.(log DEBUG "Marking: %s" (Src.form_to_string (fcx_vars fcx) f)) ;
let form = subst fcx (mark SRC f) in
let mmode = Marked in
{ form ; mmode }
end
end }
let action_unmark_source = {
enabled = begin fun hi ->
hi.work.mmode = Marked && begin
let (_, f) = unsubst hi.work.form in
match f with
| Mark (SRC, _) -> true
| _ -> false
end
end ;
perform = begin fun hi ->
tinker hi ~fn:begin
fun snap ->
let (fcx, f) = unsubst snap.form in
let f = unmark f in
let form = subst fcx f in
let mmode = Unmarked in
{ form ; mmode }
end
end }
let action_zero = {
enabled = begin fun hi ->
let (_, f) = unsubst hi.work.form in
match f with
| Conn (Plus, []) -> false
| _ -> not (has_mark f)
end ;
perform = begin fun hi ->
commit hi ~fn:begin
fun snap ->
let (fcx, _) = unsubst snap.form in
let form = subst fcx _Zero in
{ snap with form }
end
end }
let action_weaken = {
enabled = begin fun hi ->
let (_, f) = unsubst hi.work.form in
match f with
| Conn (Qm, [f]) -> not (has_mark f)
| _ -> false
end ;
perform = begin fun hi ->
commit hi ~fn:begin
fun snap ->
let (fcx, _) = unsubst snap.form in
let form = subst fcx _Bot in
{ snap with form }
end
end }
let action_derelict = {
enabled = begin fun hi ->
let (_, f) = unsubst hi.work.form in
match f with
| Conn (Qm, [_]) -> true
| _ -> false
end ;
perform = begin fun hi ->
commit hi ~fn:begin
fun snap ->
let (fcx, form) = unsubst snap.form in
match form with
| Conn (Qm, [form]) ->
let form = subst fcx form in
{ snap with form }
| _ -> assert false
end
end }
let action_contract = {
enabled = begin fun hi ->
let (_, f) = unsubst hi.work.form in
match f with
| Conn (Qm, [f]) -> not (has_mark f)
| _ -> false
end ;
perform = begin fun hi ->
commit hi ~fn:begin
fun snap ->
let (fcx, form) = unsubst snap.form in
let (fcx, fr) =
begin match Fcx.rear fcx with
| Some (fcx, ({conn = Par ; right ; _} as fr)) ->
(fcx, {fr with right = form :: right})
| _ ->
(fcx, {conn = Par ; left = [] ; right = [form]})
end in
let fcx = Fcx.snoc fcx fr in
let form = subst fcx form in
{ snap with form }
end
end }
let action_witness ~read = {
enabled = begin fun hi ->
let (_, f) = unsubst hi.work.form in
match f with
| Conn (Qu (Ex, _), _) -> true
| _ -> false
end ;
perform = begin fun hi ->
commit hi ~fn:begin
fun snap ->
let (fcx, form) = unsubst snap.form in
match form with
| Conn (Qu (Ex, x), [fb]) ->
let t = read x (fcx_vars fcx) in
let ss = Dot (Shift 0, t)in
let form = subst fcx (sub_form ss fb) in
{ snap with form }
| _ -> assert false
end
end }
let action_cut ~read = {
enabled = (fun _ -> true) ;
perform = begin fun hi ->
commit hi ~fn:begin
fun snap ->
let (fcx, form) = unsubst snap.form in
let cform = read (fcx_vars fcx) in
let fcx = Fcx.snoc fcx
{conn = Tens ; left = [cform] ; right = []} in
let fcx = Fcx.snoc fcx
{conn = Par ; left = [negate cform] ; right = []} in
let form = subst fcx form in
{ snap with form }
end
end }
let action_complete_link = {
enabled = begin fun hi ->
hi.work.mmode = Marked
&& not (has_mark (focus hi.work.form))
end ;
perform = begin fun hi ->
commit hi ~fn:begin
fun snap ->
let (fcx, f) = unsubst snap.form in
let form = subst fcx (mark SNK f) in
let form = Rules.resolve_mpar form in
{ mmode = Unmarked ; form }
end
end }
let action_reset = {
enabled = (fun _ -> true) ;
perform = begin fun hi ->
tinker hi ~fn:begin
fun snap ->
let form = Traversal.cleanup snap.form in
{ mmode = Unmarked ; form }
end
end }