~sforman/Prolog-Junkyard

Prolog-Junkyard/miscellaneous/itc.pl -rw-r--r-- 15.0 KiB
466f0094Simon Forman Simple, easy Sudoku puzzles. 3 months ago
                                                                                
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
:- use_module(library(clpfd)).
/*

██╗███╗   ██╗████████╗███████╗██████╗ ██╗   ██╗ █████╗ ██╗
██║████╗  ██║╚══██╔══╝██╔════╝██╔══██╗██║   ██║██╔══██╗██║
██║██╔██╗ ██║   ██║   █████╗  ██████╔╝██║   ██║███████║██║
██║██║╚██╗██║   ██║   ██╔══╝  ██╔══██╗╚██╗ ██╔╝██╔══██║██║
██║██║ ╚████║   ██║   ███████╗██║  ██║ ╚████╔╝ ██║  ██║███████╗
╚═╝╚═╝  ╚═══╝   ╚═╝   ╚══════╝╚═╝  ╚═╝  ╚═══╝  ╚═╝  ╚═╝╚══════╝

████████╗██████╗ ███████╗███████╗
╚══██╔══╝██╔══██╗██╔════╝██╔════╝
   ██║   ██████╔╝█████╗  █████╗
   ██║   ██╔══██╗██╔══╝  ██╔══╝
   ██║   ██║  ██║███████╗███████╗
   ╚═╝   ╚═╝  ╚═╝╚══════╝╚══════╝

 ██████╗██╗      ██████╗  ██████╗██╗  ██╗
██╔════╝██║     ██╔═══██╗██╔════╝██║ ██╔╝
██║     ██║     ██║   ██║██║     █████╔╝
██║     ██║     ██║   ██║██║     ██╔═██╗
╚██████╗███████╗╚██████╔╝╚██████╗██║  ██╗
 ╚═════╝╚══════╝ ╚═════╝  ╚═════╝╚═╝  ╚═╝

This file is an implementation in (SWI) Prolog of the Interval Tree Clock
concept described in the 2008 paper by Paulo Sérgio, Almeida Carlos, and
Baquero Victor Fonte, "Interval Tree Clocks: A Logical Clock for Dynamic
Systems"

https://haslab.uminho.pt/cbm/files/itc.pdf -or-
https://gsd.di.uminho.pt/members/cbm/ps/itc2008.pdf
https://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.160.813

This went by on HN and I got nerd-sniped:
https://ferd.ca/interval-tree-clocks.html
https://news.ycombinator.com/item?id=25243376

This code follows the equations in the paper pretty closely.  If anything
is unclear consult the paper.

> A stamp will consist of a pair (i,e): the identity and the event
components, both functions from some arbitrary domain to natural numbers.
The identity component is a characteristic function (maps elements to
{0,1}) that defines the set of elements in the domain available to
inflate (“increment”) the event function when an event occurs.

> The essential point towards ensuring a correct tracking of causality is
to be able to inflate themapping of some element which no other
participant (process or replica) has access to ... each participant
having an identity which maps to 1 some element which is mapped to 0 in
all other participants.

*/

seed_itc(itc(l, i(0))).

fork_itc(itc(I, E), itc(L, E), itc(R, E)) :-
    split_id_tree(I, L, R).

join_itc(itc(IL, EL), itc(IR, ER), itc(I, E)) :-
    sum_id(IL, IR, I),
    join_evt(EL, ER, E).

event_itc(itc(I, E0), itc(I, E)) :-
    inflate_event(I, E0, E).


/*

██╗██████╗ ███████╗███╗   ██╗████████╗██╗████████╗██╗   ██╗
██║██╔══██╗██╔════╝████╗  ██║╚══██╔══╝██║╚══██╔══╝╚██╗ ██╔╝
██║██║  ██║█████╗  ██╔██╗ ██║   ██║   ██║   ██║    ╚████╔╝
██║██║  ██║██╔══╝  ██║╚██╗██║   ██║   ██║   ██║     ╚██╔╝
██║██████╔╝███████╗██║ ╚████║   ██║   ██║   ██║      ██║
╚═╝╚═════╝ ╚══════╝╚═╝  ╚═══╝   ╚═╝   ╚═╝   ╚═╝      ╚═╝

"The id component is an id tree with the recursive form":

    i ::= o | l | idt(i, i)

Instead of 0 and 1 I'm using the atoms o and l (lowercase letter O and L,
because they look similar) for the two terminals.

    id_tree(o).
    id_tree(l).
    id_tree(idt(I1, I2)) :-
        id_tree(I1),
        id_tree(I2).

But this is a symmetric structure that admits of empty intervals, which
are not valid ids.

Instead, let's start by describing the empty interval.

 */

empty_interval(o).
empty_interval(idt(I0, I1)) :-
    empty_interval(I0),
    empty_interval(I1).

% Then the non-empty interval.

non_empty_interval(l).

non_empty_interval(idt(I0, I1)) :-
    empty_interval(I0),
    non_empty_interval(I1).

non_empty_interval(idt(I0, I1)) :-
    non_empty_interval(I0),
    empty_interval(I1).

non_empty_interval(idt(I0, I1)) :-
    non_empty_interval(I0),
    non_empty_interval(I1).

% Now we can characterize valid ids.

valid_id_tree(I) :- non_empty_interval(I).

/*

The grammar for that looks something like this:

    e ::= o | (e, e)
    i ::= l | (e, i) | (i, e) | (i, i)

It's not really important because you never use it.


  ___      _ _ _     ___    _         _   _ _
 / __|_ __| (_) |_  |_ _|__| |___ _ _| |_(_) |_ _  _
 \__ \ '_ \ | |  _|  | |/ _` / -_) ' \  _| |  _| || |
 |___/ .__/_|_|\__| |___\__,_\___|_||_\__|_|\__|\_, |
     |_|                                        |__/

Split Identity

If you start with the initial "seed" id of "l" then no amount of
subsequent splitting ever creates idt(o, o).  If you examine the rules
below, you'll find they never actually split "o".  Even if joining were
to create them, normalization does away with them before they can escape.

*/

split_id_tree(l, idt(l, o), idt(o, l)).

split_id_tree(idt(o, I), idt(o, I1), idt(o, I2)) :-
    split_id_tree(I, I1, I2).

split_id_tree(idt(I, o), idt(I1, o), idt(I2, o)) :-
    split_id_tree(I, I1, I2).

split_id_tree(idt(I1, I2), idt(I1, o), idt(o, I2)) :-
    dif(o, I1),
    dif(o, I2).

/*

Some things to note about split_id_tree/3: it only works on valid,
normalized ids; the dif/2 constraints are just to help Prolog pattern
match the rules without cuts.


  ___              ___    _         _   _ _
 / __|_  _ _ __   |_ _|__| |___ _ _| |_(_) |_ _  _
 \__ \ || | '  \   | |/ _` / -_) ' \  _| |  _| || |
 |___/\_,_|_|_|_| |___\__,_\___|_||_\__|_|\__|\_, |
                                              |__/
Sum Identity

Broadly speaking it's not valid to attempt to sum "l" with some other id
because that atom at the "top" of an identity is the whole [0..1) range!
Adding "o" at the "top" level is likewise meaningless.  Only identities
previously split from the same root "l" can subsequently be summed.  Some
subinterval covered by "l" in one summand identity must be clear in the
other ('s the whole point innit?) And if the ids are normalized then the
other id's zero subinterval will already be "o" and so the rules for that
take care of it.

 */

sum_id(idt(L1, R1), idt(L2, R2), Norm) :-
    sum_id(L1, L2, L),
    sum_id(R1, R2, R),
    norm_id(L, R, Norm).

sum_id(o, I, I).
sum_id(I, o, I) :-
    dif(I, o).
    % dif/2 only because, if it WAS "o", the rule above already handled it.

/*

Spell out norm_id/3 with dif/2 constraints.  It's basically a
pass-through for all but ooo and lll.

*/

norm_id(o, o, o).
norm_id(l, l, l).
norm_id(L, o, idt(L, o)) :- dif(o, L).
norm_id(L, l, idt(L, l)) :- dif(l, L).
norm_id(o, R, idt(o, R)) :- dif(o, R).
norm_id(l, R, idt(l, R)) :- dif(l, R).
norm_id(L, R, idt(L, R)) :-
    dif(L, l),
    dif(L, o),
    dif(R, l),
    dif(R, o).


/*

For fun let's define a predicate that describes normalized ids.

*/

normalized_id(l).
normalized_id(idt(l, o)).
normalized_id(idt(o, l)).
normalized_id(idt(l, I)) :- dif(I, l), normalized_id(I).
normalized_id(idt(I, l)) :- dif(I, l), normalized_id(I).
normalized_id(idt(o, I)) :- dif(I, o), normalized_id(I).
normalized_id(idt(I, o)) :- dif(I, o), normalized_id(I).



/* 




███████╗██╗   ██╗███████╗███╗   ██╗████████╗
██╔════╝██║   ██║██╔════╝████╗  ██║╚══██╔══╝
█████╗  ██║   ██║█████╗  ██╔██╗ ██║   ██║
██╔══╝  ╚██╗ ██╔╝██╔══╝  ██║╚██╗██║   ██║
███████╗ ╚████╔╝ ███████╗██║ ╚████║   ██║
╚══════╝  ╚═══╝  ╚══════╝╚═╝  ╚═══╝   ╚═╝

> The event component is a binary event tree with non-negative integers in
nodes...

    e ::= i(n) | evt(n, e e)

 */

event_tree(i(N)) :-
    N #>= 0.

event_tree(evt(N, E1, E2)) :-
    N #>= 0,
    event_tree(E1),
    event_tree(E2).


/*
  _    _  __ _     _____ _      _     ___             _
 | |  (_)/ _| |_  / / __(_)_ _ | |__ | __|_ _____ _ _| |_
 | |__| |  _|  _|/ /\__ \ | ' \| / / | _|\ V / -_) ' \  _|
 |____|_|_|  \__/_/ |___/_|_||_|_\_\ |___|\_/\___|_||_\__|

Lift/Sink Event

 */

lift_event_tree(i(N), M, i(T)) :-
    T #= N + M.

lift_event_tree(evt(N, L, R), M, evt(T, L, R)) :-
    T #= N + M.


sink_event_tree(i(N), M, i(T)) :-
    T #= N - M.

sink_event_tree(evt(N, L, R), M, evt(T, L, R)) :-
    T #= N - M.


/*

  _  _                    _ _          ___             _
 | \| |___ _ _ _ __  __ _| (_)______  | __|_ _____ _ _| |_
 | .` / _ \ '_| '  \/ _` | | |_ / -_) | _|\ V / -_) ' \  _|
 |_|\_\___/_| |_|_|_\__,_|_|_/__\___| |___|\_/\___|_||_\__|

Normalize Event

*/

norm_evt(i(N), i(N)).

norm_evt(evt(N, i(M), i(K)), i(Norm)) :-
    M #= K,
    Norm #= N + M.

norm_evt(evt(N, i(M), i(K)), Norm) :-
    M #\= K,
    norm_evt_assist(evt(N, i(M), i(K)), Norm).

norm_evt(E, evt(N1, Esunk1, Esunk2)) :-
    norm_evt_guard(E),
    norm_evt_assist(E, evt(N1, Esunk1, Esunk2)).

norm_evt_guard(evt(_, evt(_, _, _), i(_))).
norm_evt_guard(evt(_, i(_), evt(_, _, _))).
norm_evt_guard(evt(_, evt(_, _, _), evt(_, _, _))).

norm_evt_assist(evt(N, E1, E2), evt(N1, Esunk1, Esunk2)) :-
    min_evt(E1, Min1),
    min_evt(E2, Min2),
    M #= min(Min1, Min2),
    N1 #= N + M,
    sink_event_tree(E1, M, Esunk1),
    sink_event_tree(E2, M, Esunk2).

/*
  __  __ _        ____  __            ___             _
 |  \/  (_)_ _   / /  \/  |__ ___ __ | __|_ _____ _ _| |_
 | |\/| | | ' \ / /| |\/| / _` \ \ / | _|\ V / -_) ' \  _|
 |_|  |_|_|_||_/_/ |_|  |_\__,_/_\_\ |___|\_/\___|_||_\__|

Min/Max Event

*/

min_evt(i(N), N).
min_evt(evt(N, i(0), _), N).
min_evt(evt(N, E1, i(0)), N) :-  dif(E1, i(0)).
min_evt(evt(N, E1, E2), Min) :-
    dif(E1, i(0)),
    dif(E2, i(0)),
    min_evt(E1, Min1),
    min_evt(E2, Min2),
    Min #= N + min(Min1, Min2).


max_evt(i(N), N).
max_evt(evt(N, i(0), _), N).
max_evt(evt(N, E1, i(0)), N) :-  dif(E1, i(0)).
max_evt(evt(N, E1, E2), Min) :-
    dif(E1, i(0)),
    dif(E2, i(0)),
    max_evt(E1, Min1),
    max_evt(E2, Min2),
    Min #= N + max(Min1, Min2).


/*
     _     _        ___             _
  _ | |___(_)_ _   | __|_ _____ _ _| |_
 | || / _ \ | ' \  | _|\ V / -_) ' \  _|
  \__/\___/_|_||_| |___|\_/\___|_||_\__|

Join Event

 */

join_evt(i(N), i(M), i(E)) :-
    E #= max(N, M).

join_evt(i(N), evt(M, L, R), E) :-
    join_evt(evt(N, i(0), i(0)), evt(M, L, R), E).

join_evt(evt(M, L, R), i(N), E) :-
    join_evt(evt(M, L, R), evt(N, i(0), i(0)), E).

join_evt(evt(N1, L1, R1), evt(N2, L2, R2), E) :-
    N1 #> N2,
    join_evt_assist(evt(N2, L2, R2), evt(N1, L1, R1), E).

join_evt(evt(N1, L1, R1), evt(N2, L2, R2), E) :-
    N1 #=< N2,
    join_evt_assist(evt(N1, L1, R1), evt(N2, L2, R2), E).

join_evt_assist(evt(N1, L1, R1), evt(N2, L2, R2), E) :-
    M #= N1 - N2,
    lift_event_tree(L2, M, L2Lifted),
    lift_event_tree(R2, M, R2Lifted),
    join_evt(L1, L2Lifted, L),
    join_evt(R1, R2Lifted, R),
    norm_evt(evt(N1, L, R), E).


/*
  ___       __ _      _         ___             _
 |_ _|_ _  / _| |__ _| |_ ___  | __|_ _____ _ _| |_
  | || ' \|  _| / _` |  _/ -_) | _|\ V / -_) ' \  _|
 |___|_||_|_| |_\__,_|\__\___| |___|\_/\___|_||_\__|

Inflate Event

Try fill first and, if it doesn't result in a new event, try grow.

 */

inflate_event(I, E0, E) :-
    fill_evt(I, E0, E1),
    inflate_event(I, E0, E1, E).

inflate_event(I, E0, E0, E) :- grow_evt(I, E0, E, _).
inflate_event(_, E0, E, E) :- dif(E0, E).



fill_evt(o, E, E).

fill_evt(l, E, i(Emax)) :- max_evt(E, Emax).

fill_evt(idt(_, _), i(N), i(N)).

fill_evt(idt(l, Ir), evt(N, El, Er), E) :-
    fill_evt(Ir, Er, ErTick),
    max_evt(El, ElMax),
    min_evt(ErTick, ErTickMin),
    M #= max(ElMax, ErTickMin),
    norm_evt(evt(N, M, ErTick), E).

fill_evt(idt(Il, l), evt(N, El, Er), E) :-
    dif(Il, l),
    fill_evt(Il, El, ElTick),
    max_evt(Er, ErMax),
    min_evt(ElTick, ElTickMin),
    M #= max(ErMax, ElTickMin),
    norm_evt(evt(N, ElTick, M), E).

fill_evt(idt(Il, Ir), evt(N, El, Er), E) :-
    dif(Ir, l),
    dif(Il, l),
    fill_evt(Il, El, L),
    fill_evt(Ir, Er, R),
    norm_evt(evt(N, L, R), E).


grow_evt(l, i(N), i(M), 0) :-
    M #= N + 1.

grow_evt(I, i(N), E, Cost) :-
    dif(I, l),
    grow_evt(I, evt(N, i(0), i(0)), E, Cost0),
    Cost #= Cost0 + 1000.  % "N is some large constant"

grow_evt(idt(o, I), evt(N, El, Er), evt(N, El, ErGrown), Cost) :-
    grow_evt(I, Er, ErGrown, CostRight),
    Cost #= CostRight + 1.

grow_evt(idt(Il, o), evt(N, El, Er), evt(N, ElGrown, Er), Cost) :-
    dif(Il, o),
    grow_evt(Il, El, ElGrown, CostLeft),
    Cost #= CostLeft + 1.

grow_evt(idt(Il, Ir), evt(N, El, Er), E, Cost) :-
    dif(Il, o),
    dif(Ir, o),
    grow_evt(Ir, Er, ErGrown, CostRight),
    grow_evt(Il, El, ElGrown, CostLeft),
    grow_evt_assist(N, CostLeft, El, ElGrown, CostRight, Er, ErGrown, E, Cost).


grow_evt_assist(N, CostLeft, _, ElGrown, CostRight, Er, _, evt(N, ElGrown, Er), Cost) :-
    CostLeft #< CostRight,
    Cost #= CostLeft + 1.

grow_evt_assist(N, CostLeft, El, _, CostRight, _, ErGrown, evt(N, El, ErGrown), Cost) :-
    CostLeft #>= CostRight,
    Cost #= CostRight + 1.

/*

And there you go.  If I carried over the meaning of the equations
correctly into Prolog code, then the above is an implementation of the
ITC concept.

It's one of those things where, when I first tried to read the paper, it
was very confusing.  Then I noodled around a bit, wrote some code, and
the light goes on!  Now when I read the paper it all makes sense!

ITC is a very clever and in-hindsight-obvious device.  That's a common
hallmark of genius: an idea that, once it's known, seems obvious, right,
and true, yet it wasn't easy to come up with in the first place.  Like
the wheel, or plumbing.

*/