Source file navigation.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
type abstract = Existing of int | Fresh of Agent.t
type 'a port = 'a * int
type 'a arrow = ToNode of 'a port | ToNothing | ToInternal of int
type 'a step = 'a port * 'a arrow
type 'a t = 'a step list
let print_id sigs f = function
| Existing id -> Format.pp_print_int f id
| Fresh (id, ty) ->
Format.fprintf f "!%a-%i" (Signature.print_agent sigs) ty id
let print_id_site ?source sigs find_ty n =
let ty =
match n with
| Fresh (_, ty) -> ty
| Existing id ->
(match source with
| Some (Fresh (id', ty)) when id = id' -> ty
| None | Some (Fresh _ | Existing _) -> find_ty id)
in
Signature.print_site sigs ty
let print_id_internal_state sigs find_ty n =
Signature.print_site_internal_state sigs
(match n with
| Existing id -> find_ty id
| Fresh (_, ty) -> ty)
let id_up_to_alpha_to_yojson = function
| Existing i -> `Int i
| Fresh f -> Agent.to_json f
let id_up_to_alpha_of_yojson = function
| `Int i -> Existing i
| x -> Fresh (Agent.of_json x)
let port_to_yojson (a, s) = `List [ id_up_to_alpha_to_yojson a; `Int s ]
let port_of_yojson = function
| `List [ a; `Int s ] -> id_up_to_alpha_of_yojson a, s
| x -> raise (Yojson.Basic.Util.Type_error ("Incorrect navigation port", x))
let arrow_to_yojson = function
| ToNode p -> port_to_yojson p
| ToNothing -> `Null
| ToInternal i -> `Int i
let arrow_of_yojson = function
| `Null -> ToNothing
| `Int i -> ToInternal i
| x -> ToNode (port_of_yojson x)
let step_to_yojson (p, a) = `List [ port_to_yojson p; arrow_to_yojson a ]
let step_of_yojson = function
| `List [ p; a ] -> port_of_yojson p, arrow_of_yojson a
| x -> raise (Yojson.Basic.Util.Type_error ("Incorrect navigation step", x))
let to_yojson l = `List (List.map step_to_yojson l)
let of_yojson = function
| `List l -> List.map step_of_yojson l
| x -> raise (Yojson.Basic.Util.Type_error ("Incorrect navigation", x))
let extend f = function
| Existing _ -> f
| Fresh (id, ty) ->
fun x ->
if x = id then
ty
else
f x
let rec print sigs find_ty f = function
| [] -> ()
| ((source, site), ToNothing) :: t ->
Format.fprintf f "-%a(%a[.])->%a" (print_id sigs) source
(print_id_site sigs find_ty source)
site
(print sigs (extend find_ty source))
t
| ((source, site), ToNode (id, port)) :: t ->
Format.fprintf f "-%a(%a[%a.%a])->%a" (print_id sigs) source
(print_id_site sigs find_ty source)
site
(print_id_site ~source sigs find_ty id)
port (print_id sigs) id
(print sigs (extend (extend find_ty id) source))
t
| ((source, site), ToInternal i) :: t ->
Format.fprintf f "-%a(%a)->%a" (print_id sigs) source
(print_id_internal_state sigs find_ty source site)
(Some i)
(print sigs (extend find_ty source))
t
let compatible_fresh_point ~debug_mode e (sid, sty) ssite arrow =
match e, arrow with
| _, ToNode (Existing _, _) ->
raise
(ExceptionDefn.Internal_Error
(Loc.annot_with_dummy
"Navigation.compatible_fresh_point does not deal with existing \
arrow"))
| ((Fresh (id, ty), site), x), ToNothing ->
if ty = sty && site = ssite && x = ToNothing then (
let inj = Renaming.empty () in
if Renaming.imperative_add ~debug_mode id sid inj then
Some inj
else
None
) else
None
| ((Fresh (id, ty), site), x), ToInternal i ->
if ty = sty && site = ssite && x = ToInternal i then (
let inj = Renaming.empty () in
if Renaming.imperative_add ~debug_mode id sid inj then
Some inj
else
None
) else
None
| ( ((Fresh (id, ty), site), ToNode (Fresh (id', ty'), site')),
ToNode (Fresh (sid', sty'), ssite') ) ->
if ty = sty && site = ssite && ty' = sty' && site' = ssite' then (
let inj = Renaming.empty () in
if Renaming.imperative_add ~debug_mode id sid inj then
if Renaming.imperative_add ~debug_mode id' sid' inj then
Some inj
else
None
else
None
) else if ty = sty' && site = ssite' && ty' = sty && site' = ssite then (
let inj = Renaming.empty () in
if Renaming.imperative_add ~debug_mode id sid' inj then
if Renaming.imperative_add ~debug_mode id' sid inj then
Some inj
else
None
else
None
) else
None
| ( ( (Existing id, site), ToNode (Fresh (id', ty), site')
| (Fresh (id', ty), site), ToNode (Existing id, site') ),
ToNode (Fresh (sid', sty'), ssite') ) ->
if
((ssite = site && ssite' = site') || (ssite' = site && ssite = site'))
&& id = id' && sid = sid' && ty = sty && sty = sty'
then (
let inj = Renaming.empty () in
if Renaming.imperative_add ~debug_mode id sid inj then
Some inj
else
None
) else
None
| ((Existing _, _), _), _ -> None
| ((Fresh _, _), (ToNothing | ToInternal _)), ToNode _ -> None
let compatible_point ~debug_mode inj e e' =
match e, e' with
| ((Existing id, site), ToNothing), e ->
if
Renaming.mem id inj
&& e = ((Existing (Renaming.apply ~debug_mode inj id), site), ToNothing)
then
Some inj
else
None
| ((Existing id, site), ToInternal i), e ->
if
Renaming.mem id inj
&& e = ((Existing (Renaming.apply ~debug_mode inj id), site), ToInternal i)
then
Some inj
else
None
| ((Existing id, site), ToNode (Existing id', site')), e ->
if
Renaming.mem id inj && Renaming.mem id' inj
&& (e
= ( (Existing (Renaming.apply ~debug_mode inj id), site),
ToNode (Existing (Renaming.apply ~debug_mode inj id'), site') )
|| e
= ( (Existing (Renaming.apply ~debug_mode inj id'), site'),
ToNode (Existing (Renaming.apply ~debug_mode inj id), site) ))
then
Some inj
else
None
| ( ((Existing id, site), ToNode (Fresh (id', ty), site')),
((Existing sid, ssite), ToNode (Fresh (sid', ty'), ssite')) )
| ( ((Fresh (id', ty), site), ToNode (Existing id, site')),
((Existing sid, ssite), ToNode (Fresh (sid', ty'), ssite')) )
| ( ((Existing id, site), ToNode (Fresh (id', ty), site')),
((Fresh (sid', ty'), ssite), ToNode (Existing sid, ssite')) )
| ( ((Fresh (id', ty), site), ToNode (Existing id, site')),
((Fresh (sid', ty'), ssite), ToNode (Existing sid, ssite')) ) ->
if
ty' = ty
&& (not (Renaming.mem id' inj))
&& ((ssite = site && ssite' = site')
|| (id = id' && ssite = site' && ssite' = site))
then (
match Renaming.add ~debug_mode id' sid' inj with
| Some inj'
when Renaming.mem id inj' && sid = Renaming.apply ~debug_mode inj' id ->
Some inj'
| _ -> None
) else
None
| ((Existing _, _), ToNode (Fresh _, _)), (((Fresh _ | Existing _), _), _) ->
None
| ((Fresh (id, ty), site), ToNothing), ((Fresh (id', ty'), site'), x) ->
if ty = ty' && site = site' && x = ToNothing && not (Renaming.mem id inj)
then
Renaming.add ~debug_mode id id' inj
else
None
| ((Fresh (id, ty), site), ToInternal i), ((Fresh (id', ty'), site'), x) ->
if ty = ty' && site = site' && x = ToInternal i && not (Renaming.mem id inj)
then
Renaming.add ~debug_mode id id' inj
else
None
| ( ((Fresh (id, ty), site), ToNode (Fresh (id', ty'), site')),
((Fresh (sid, sty), ssite), ToNode (Fresh (sid', sty'), ssite')) ) ->
if (not (Renaming.mem id inj)) && not (Renaming.mem id' inj) then
if ty = sty && site = ssite && ty' = sty' && site' = ssite' then (
match Renaming.add ~debug_mode id sid inj with
| None -> None
| Some inj' ->
(match Renaming.add ~debug_mode id' sid' inj' with
| None -> None
| Some inj'' -> Some inj'')
) else if ty = sty' && site = ssite' && ty' = sty && site' = ssite then (
match Renaming.add ~debug_mode id sid' inj with
| None -> None
| Some inj' ->
(match Renaming.add ~debug_mode id' sid inj' with
| None -> None
| Some inj'' -> Some inj'')
) else
None
else
None
| ((Fresh _, _), _), ((Fresh _, _), _) -> None
| ((Fresh _, _), _), ((Existing _, _), _) -> None
let rec aux_sub ~debug_mode inj goal acc = function
| [] -> None
| h :: t ->
(match compatible_point ~debug_mode inj h goal with
| None -> aux_sub ~debug_mode inj goal (h :: acc) t
| Some inj' -> Some (inj', List.rev_append acc t))
let rec is_subnavigation ~debug_mode inj nav = function
| [] -> Some (inj, nav)
| h :: t ->
(match aux_sub ~debug_mode inj h [] nav with
| None -> None
| Some (inj', nav') -> is_subnavigation ~debug_mode inj' nav' t)
let rename_id ~debug_mode inj2cc = function
| Existing n -> inj2cc, Existing (Renaming.apply ~debug_mode inj2cc n)
| Fresh (id, ty) ->
let img = Renaming.image inj2cc in
let id' =
if Mods.IntSet.mem id img then (
match Mods.IntSet.max_elt img with
| None -> 1
| Some i -> succ i
) else
id
in
(match Renaming.add ~debug_mode id id' inj2cc with
| None -> assert false
| Some inj' -> inj', Fresh (id', ty))
let rec rename ~debug_mode inj2cc = function
| [] -> inj2cc, []
| ((x, i), ((ToNothing | ToInternal _) as a)) :: t ->
let inj, x' = rename_id ~debug_mode inj2cc x in
let inj', t' = rename ~debug_mode inj t in
inj', ((x', i), a) :: t'
| ((x, i), ToNode (y, j)) :: t ->
let inj, x' = rename_id ~debug_mode inj2cc x in
let inj', y' = rename_id ~debug_mode inj y in
let inj'', t' = rename ~debug_mode inj' t in
inj'', ((x', i), ToNode (y', j)) :: t'
let check_edge graph = function
| (Fresh (id, _), site), ToNothing -> Edges.is_free id site graph
| (Fresh (id, _), site), ToInternal i -> Edges.is_internal i id site graph
| (Fresh (id, _), site), ToNode (Existing id', site') ->
Edges.link_exists id site id' site' graph
| (Fresh (id, _), site), ToNode (Fresh (id', _), site') ->
Edges.link_exists id site id' site' graph
| (Existing id, site), ToNothing -> Edges.is_free id site graph
| (Existing id, site), ToInternal i -> Edges.is_internal i id site graph
| (Existing id, site), ToNode (Existing id', site') ->
Edges.link_exists id site id' site' graph
| (Existing id, site), ToNode (Fresh (id', _), site') ->
Edges.link_exists id site id' site' graph
let dst_is_okay ~debug_mode inj' graph root site = function
| ToNothing ->
if Edges.is_free root site graph then
Some inj'
else
None
| ToInternal i ->
if Edges.is_internal i root site graph then
Some inj'
else
None
| ToNode (Existing id', site') ->
if
Edges.link_exists root site
(Renaming.apply ~debug_mode inj' id')
site' graph
then
Some inj'
else
None
| ToNode (Fresh (id', ty), site') ->
(match Edges.exists_fresh root site ty site' graph with
| None -> None
| Some node -> Renaming.add ~debug_mode id' node inj')
let injection_for_one_more_edge ~debug_mode ?root inj graph = function
| (Existing id, site), dst ->
dst_is_okay ~debug_mode inj graph
(Renaming.apply ~debug_mode inj id)
site dst
| (Fresh (id, rty), site), dst ->
(match root with
| Some (root, rty') when rty = rty' ->
(match Renaming.add ~debug_mode id root inj with
| None -> None
| Some inj' -> dst_is_okay ~debug_mode inj' graph root site dst)
| _ -> None)
let imperative_dst_is_okay ~debug_mode inj' graph root site = function
| ToNothing -> Edges.is_free root site graph
| ToInternal i -> Edges.is_internal i root site graph
| ToNode (Existing id', site') ->
Edges.link_exists root site
(Renaming.apply ~debug_mode inj' id')
site' graph
| ToNode (Fresh (id', ty), site') ->
(match Edges.exists_fresh root site ty site' graph with
| None -> false
| Some node -> Renaming.imperative_add ~debug_mode id' node inj')
let imperative_edge_is_valid ~debug_mode ?root inj graph = function
| (Existing id, site), dst ->
imperative_dst_is_okay ~debug_mode inj graph
(Renaming.apply ~debug_mode inj id)
site dst
| (Fresh (id, rty), site), dst ->
(match root with
| Some (root, rty') when rty = rty' ->
Renaming.imperative_add ~debug_mode id root inj
&& imperative_dst_is_okay ~debug_mode inj graph root site dst
| _ -> false)
let concretize_port ~debug_mode inj = function
| Existing id, site -> Renaming.apply ~debug_mode inj id, site
| Fresh (id, _), site -> Renaming.apply ~debug_mode inj id, site
let concretize_arrow ~debug_mode inj = function
| (ToNothing | ToInternal _) as x -> x
| ToNode x -> ToNode (concretize_port ~debug_mode inj x)
let concretize ~debug_mode root graph nav =
let inj = Renaming.empty () in
let out =
List.fold_left
(fun out ((p, dst) as step) ->
match out with
| None -> out
| Some (root, acc) ->
if imperative_edge_is_valid ~debug_mode ?root inj graph step then (
let st =
( concretize_port ~debug_mode inj p,
concretize_arrow ~debug_mode inj dst )
in
Some (None, st :: acc)
) else
None)
(Some (Some root, []))
nav
in
Option_util.map (fun (_, l) -> List.rev l) out