eyeprolog 1.3.78 → 1.3.80

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,619 @@
1
+ /* Author: Jan Wielemaker
2
+ E-mail: J.Wielemaker@vu.nl
3
+ WWW: http://www.swi-prolog.org
4
+ Copyright (c) 2001-2014, University of Amsterdam
5
+ VU University Amsterdam
6
+ All rights reserved.
7
+ Redistribution and use in source and binary forms, with or without
8
+ modification, are permitted provided that the following conditions
9
+ are met:
10
+ 1. Redistributions of source code must retain the above copyright
11
+ notice, this list of conditions and the following disclaimer.
12
+ 2. Redistributions in binary form must reproduce the above copyright
13
+ notice, this list of conditions and the following disclaimer in
14
+ the documentation and/or other materials provided with the
15
+ distribution.
16
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19
+ FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20
+ COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21
+ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22
+ BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
+ LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
+ ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
+ POSSIBILITY OF SUCH DAMAGE.
28
+ */
29
+
30
+ :- module(ordsets,
31
+ [ is_ordset/1, % @Term
32
+ list_to_ord_set/2, % +List, -OrdSet
33
+ ord_add_element/3, % +Set, +Element, -NewSet
34
+ ord_del_element/3, % +Set, +Element, -NewSet
35
+ ord_selectchk/3, % +Item, ?Set1, ?Set2
36
+ ord_intersect/2, % +Set1, +Set2 (test non-empty)
37
+ ord_intersect/3, % +Set1, +Set2, -Intersection
38
+ ord_intersection/3, % +Set1, +Set2, -Intersection
39
+ ord_intersection/4, % +Set1, +Set2, -Intersection, -Diff
40
+ ord_disjoint/2, % +Set1, +Set2
41
+ ord_subtract/3, % +Set, +Delete, -Remaining
42
+ ord_union/2, % +SetOfOrdSets, -Set
43
+ ord_union/3, % +Set1, +Set2, -Union
44
+ ord_union/4, % +Set1, +Set2, -Union, -New
45
+ ord_subset/2, % +Sub, +Super (test Sub is in Super)
46
+ % Non-Quintus extensions
47
+ ord_empty/1, % ?Set
48
+ ord_memberchk/2, % +Element, +Set,
49
+ ord_symdiff/3, % +Set1, +Set2, ?Diff
50
+ % SICSTus extensions
51
+ ord_seteq/2, % +Set1, +Set2
52
+ ord_intersection/2 % +PowerSet, -Intersection
53
+ ]).
54
+
55
+ :- use_module(library(lists)).
56
+
57
+ /** Ordered set manipulation
58
+
59
+ Ordered sets are lists with unique elements sorted to the standard order
60
+ of terms (see `sort/2`). Exploiting ordering, many of the set operations
61
+ can be expressed in order N rather than N^2 when dealing with unordered
62
+ sets that may contain duplicates. The library(ordsets) is available in a
63
+ number of Prolog implementations. Our predicates are designed to be
64
+ compatible with common practice in the Prolog community.
65
+ Some of these predicates match directly to corresponding list
66
+ operations. It is advised to use the versions from this library to make
67
+ clear you are operating on ordered sets. An exception is `member/2`. See
68
+ `ord_memberchk/2`.
69
+
70
+ The ordsets library is based on the standard order of terms. This
71
+ implies it can handle all Prolog terms, including variables. Note
72
+ however, that the ordering is not stable if a term inside the set is
73
+ further instantiated. Also note that variable ordering changes if
74
+ variables in the set are unified with each other or a variable in the
75
+ set is unified with a variable that is _older_ than the newest variable
76
+ in the set. In practice, this implies that it is allowed to use
77
+ member(X, OrdSet) on an ordered set that holds variables only if X is a
78
+ fresh variable. In other cases one should cease using it as an ordset
79
+ because the order it relies on may have been changed.
80
+ */
81
+
82
+ %% is_ordset(@Term) is semidet.
83
+ %
84
+ % True if Term is an ordered set. All predicates in this library
85
+ % expect ordered sets as input arguments. Failing to fullfil this
86
+ % assumption results in undefined behaviour. Typically, ordered
87
+ % sets are created by predicates from this library, `sort/2` or
88
+ % `setof/3`.
89
+
90
+ is_ordset(Term) :-
91
+ ord_proper_list(Term),
92
+ is_ordset2(Term).
93
+
94
+ ord_proper_list([]).
95
+ ord_proper_list([_|Xs]) :- ord_proper_list(Xs).
96
+
97
+ is_ordset2([]).
98
+ is_ordset2([H|T]) :-
99
+ is_ordset3(T, H).
100
+
101
+ is_ordset3([], _).
102
+ is_ordset3([H2|T], H) :-
103
+ H2 @> H,
104
+ is_ordset3(T, H2).
105
+
106
+
107
+ %% ord_empty(?List) is semidet.
108
+ %
109
+ % True when List is the empty ordered set. Simply unifies list
110
+ % with the empty list. Not part of Quintus.
111
+
112
+ ord_empty([]).
113
+
114
+
115
+ %% ord_seteq(+Set1, +Set2) is semidet.
116
+ %
117
+ % True if Set1 and Set2 have the same elements. As both are
118
+ % canonical sorted lists, this is the same as `==/2`.
119
+
120
+ ord_seteq(Set1, Set2) :-
121
+ Set1 == Set2.
122
+
123
+
124
+ %% list_to_ord_set(+List, -OrdSet) is det.
125
+ %
126
+ % Transform a list into an ordered set. This is the same as
127
+ % sorting the list.
128
+
129
+ list_to_ord_set(List, Set) :-
130
+ sort(List, Set).
131
+
132
+
133
+ %% ord_intersect(+Set1, +Set2) is semidet.
134
+ %
135
+ % True if both ordered sets have a non-empty intersection.
136
+
137
+ ord_intersect([H1|T1], L2) :-
138
+ ord_intersect_(L2, H1, T1).
139
+
140
+ ord_intersect_([H2|T2], H1, T1) :-
141
+ compare(Order, H1, H2),
142
+ ord_intersect__(Order, H1, T1, H2, T2).
143
+
144
+ ord_intersect__(<, _H1, T1, H2, T2) :-
145
+ ord_intersect_(T1, H2, T2).
146
+ ord_intersect__(=, _H1, _T1, _H2, _T2).
147
+ ord_intersect__(>, H1, T1, _H2, T2) :-
148
+ ord_intersect_(T2, H1, T1).
149
+
150
+
151
+ %% ord_disjoint(+Set1, +Set2) is semidet.
152
+ %
153
+ % True if Set1 and Set2 have no common elements. This is the
154
+ % negation of `ord_intersect/2`.
155
+
156
+ ord_disjoint(Set1, Set2) :-
157
+ \+ ord_intersect(Set1, Set2).
158
+
159
+
160
+ %% ord_intersect(+Set1, +Set2, -Intersection)
161
+ %
162
+ % Intersection holds the common elements of Set1 and Set2.
163
+ %
164
+ % This predicate is *deprecated*. Use `ord_intersection/3`
165
+
166
+ ord_intersect(Set1, Set2, Intersection) :-
167
+ oset_int(Set1, Set2, Intersection).
168
+
169
+
170
+ %% ord_intersection(+PowerSet, -Intersection)
171
+ %
172
+ % Intersection of a powerset. True when Intersection is an ordered
173
+ % set holding all elements common to all sets in PowerSet.
174
+
175
+ ord_intersection(PowerSet, Intersection) :-
176
+ key_by_length(PowerSet, Pairs),
177
+ keysort(Pairs, [_-S|Sorted]),
178
+ l_int(Sorted, S, Intersection).
179
+
180
+ key_by_length([], []).
181
+ key_by_length([H|T0], [L-H|T]) :-
182
+ length(H, L),
183
+ key_by_length(T0, T).
184
+
185
+ l_int([], S, S).
186
+ l_int([_-H|T], S0, S) :-
187
+ ord_intersection(S0, H, S1),
188
+ l_int(T, S1, S).
189
+
190
+
191
+ %% ord_intersection(+Set1, +Set2, -Intersection) is det.
192
+ %
193
+ % Intersection holds the common elements of Set1 and Set2. Uses
194
+ % `ord_disjoint/2` if Intersection is bound to `[]` on entry.
195
+
196
+ ord_intersection(Set1, Set2, Intersection) :-
197
+ ( Intersection == []
198
+ -> ord_disjoint(Set1, Set2)
199
+ ; oset_int(Set1, Set2, Intersection)
200
+ ).
201
+
202
+
203
+ %% ord_intersection(+Set1, +Set2, ?Intersection, ?Difference) is det.
204
+ %
205
+ % Intersection and difference between two ordered sets.
206
+ % Intersection is the intersection between Set1 and Set2, while
207
+ % Difference is defined by `ord_subtract(Set2, Set1, Difference)`.
208
+
209
+ ord_intersection([], L, [], L) :- !.
210
+ ord_intersection([_|_], [], [], []) :- !.
211
+ ord_intersection([H1|T1], [H2|T2], Intersection, Difference) :-
212
+ compare(Diff, H1, H2),
213
+ ord_intersection2(Diff, H1, T1, H2, T2, Intersection, Difference).
214
+
215
+ ord_intersection2(=, H1, T1, _H2, T2, [H1|T], Difference) :-
216
+ ord_intersection(T1, T2, T, Difference).
217
+ ord_intersection2(<, _, T1, H2, T2, Intersection, Difference) :-
218
+ ord_intersection(T1, [H2|T2], Intersection, Difference).
219
+ ord_intersection2(>, H1, T1, H2, T2, Intersection, [H2|HDiff]) :-
220
+ ord_intersection([H1|T1], T2, Intersection, HDiff).
221
+
222
+
223
+ %% ord_add_element(+Set1, +Element, ?Set2) is det.
224
+ %
225
+ % Insert an element into the set. This is the same as
226
+ % `ord_union(Set1, [Element], Set2)`.
227
+
228
+ ord_add_element(Set1, Element, Set2) :-
229
+ oset_addel(Set1, Element, Set2).
230
+
231
+
232
+ %% ord_del_element(+Set, +Element, -NewSet) is det.
233
+ %
234
+ % Delete an element from an ordered set. This is the same as
235
+ % `ord_subtract(Set, [Element], NewSet)`.
236
+
237
+ ord_del_element(Set, Element, NewSet) :-
238
+ oset_delel(Set, Element, NewSet).
239
+
240
+
241
+ %% ord_selectchk(+Item, ?Set1, ?Set2) is semidet.
242
+ %
243
+ % `selectchk/3`, specialised for ordered sets. Is true when
244
+ % select(Item, Set1, Set2) and Set1, Set2 are both sorted lists
245
+ % without duplicates. This implementation is only expected to work
246
+ % for Item ground and either Set1 or Set2 ground. The "chk" suffix
247
+ % is meant to remind you of `memberchk/2`, which also expects its
248
+ % first argument to be ground. `ord_selectchk(X, S, T) =>
249
+ % ord_memberchk(X, S) & \+ ord_memberchk(X, T).`
250
+ %
251
+ % Author: Richard O'Keefe
252
+
253
+ ord_selectchk(Item, [X|Set1], [X|Set2]) :-
254
+ X @< Item,
255
+ !,
256
+ ord_selectchk(Item, Set1, Set2).
257
+ ord_selectchk(Item, [Item|Set1], Set1) :-
258
+ ( Set1 == []
259
+ -> true
260
+ ; Set1 = [Y|_]
261
+ -> Item @< Y
262
+ ).
263
+
264
+
265
+ %% ord_memberchk(+Element, +OrdSet) is semidet.
266
+ %
267
+ % True if Element is a member of OrdSet, compared using ==. Note
268
+ % that _enumerating_ elements of an ordered set can be done using
269
+ % `member/2`.
270
+ %
271
+ % Some Prolog implementations also provide `ord_member/2`, with the
272
+ % same semantics as `ord_memberchk/2`. We believe that having a
273
+ % semidet `ord_member/2` is unacceptably inconsistent with the \*\_chk
274
+ % convention. Portable code should use `ord_memberchk/2` or
275
+ % `member/2`.
276
+ %
277
+ % Author: Richard O'Keefe
278
+
279
+ ord_memberchk(Item, [X1,X2,X3,X4|Xs]) :-
280
+ !,
281
+ compare(R4, Item, X4),
282
+ ( R4 = (>) -> ord_memberchk(Item, Xs)
283
+ ; R4 = (<) ->
284
+ compare(R2, Item, X2),
285
+ ( R2 = (>) -> Item == X3
286
+ ; R2 = (<) -> Item == X1
287
+ ;/* R2 = (=), Item == X2 */ true
288
+ )
289
+ ;/* R4 = (=) */ true
290
+ ).
291
+ ord_memberchk(Item, [X1,X2|Xs]) :-
292
+ !,
293
+ compare(R2, Item, X2),
294
+ ( R2 = (>) -> ord_memberchk(Item, Xs)
295
+ ; R2 = (<) -> Item == X1
296
+ ;/* R2 = (=) */ true
297
+ ).
298
+ ord_memberchk(Item, [X1]) :-
299
+ Item == X1.
300
+
301
+
302
+ %% ord_subset(+Sub, +Super) is semidet.
303
+ %
304
+ % Is true if all elements of Sub are in Super
305
+
306
+ ord_subset([], _).
307
+ ord_subset([H1|T1], [H2|T2]) :-
308
+ compare(Order, H1, H2),
309
+ ord_subset_(Order, H1, T1, T2).
310
+
311
+ ord_subset_(>, H1, T1, [H2|T2]) :-
312
+ compare(Order, H1, H2),
313
+ ord_subset_(Order, H1, T1, T2).
314
+ ord_subset_(=, _, T1, T2) :-
315
+ ord_subset(T1, T2).
316
+
317
+
318
+ %% ord_subtract(+InOSet, +NotInOSet, -Diff) is det.
319
+ %
320
+ % Diff is the set holding all elements of InOSet that are not in
321
+ % NotInOSet.
322
+
323
+ ord_subtract(InOSet, NotInOSet, Diff) :-
324
+ oset_diff(InOSet, NotInOSet, Diff).
325
+
326
+
327
+ %% ord_union(+SetOfSets, -Union) is det.
328
+ %
329
+ % True if Union is the union of all elements in the superset
330
+ % SetOfSets. Each member of SetOfSets must be an ordered set, the
331
+ % sets need not be ordered in any way.
332
+
333
+ ord_union([], []).
334
+ ord_union([Set|Sets], Union) :-
335
+ length([Set|Sets], NumberOfSets),
336
+ ord_union_all(NumberOfSets, [Set|Sets], Union, []).
337
+
338
+ ord_union_all(N, Sets0, Union, Sets) :-
339
+ ( N =:= 1
340
+ -> Sets0 = [Union|Sets]
341
+ ; N =:= 2
342
+ -> Sets0 = [Set1,Set2|Sets],
343
+ ord_union(Set1,Set2,Union)
344
+ ; A is N>>1,
345
+ Z is N-A,
346
+ ord_union_all(A, Sets0, X, Sets1),
347
+ ord_union_all(Z, Sets1, Y, Sets),
348
+ ord_union(X, Y, Union)
349
+ ).
350
+
351
+
352
+ %% ord_union(+Set1, +Set2, ?Union) is det.
353
+ %
354
+ % Union is the union of Set1 and Set2
355
+
356
+ ord_union(Set1, Set2, Union) :-
357
+ oset_union(Set1, Set2, Union).
358
+
359
+
360
+ %% ord_union(+Set1, +Set2, -Union, -New) is det.
361
+ %
362
+ % True iff `ord_union(Set1, Set2, Union)` and
363
+ % `ord_subtract(Set2, Set1, New)`.
364
+
365
+ ord_union([], Set2, Set2, Set2).
366
+ ord_union([H|T], Set2, Union, New) :-
367
+ ord_union_1(Set2, H, T, Union, New).
368
+
369
+ ord_union_1([], H, T, [H|T], []).
370
+ ord_union_1([H2|T2], H, T, Union, New) :-
371
+ compare(Order, H, H2),
372
+ ord_union(Order, H, T, H2, T2, Union, New).
373
+
374
+ ord_union(<, H, T, H2, T2, [H|Union], New) :-
375
+ ord_union_2(T, H2, T2, Union, New).
376
+ ord_union(>, H, T, H2, T2, [H2|Union], [H2|New]) :-
377
+ ord_union_1(T2, H, T, Union, New).
378
+ ord_union(=, H, T, _, T2, [H|Union], New) :-
379
+ ord_union(T, T2, Union, New).
380
+
381
+ ord_union_2([], H2, T2, [H2|T2], [H2|T2]).
382
+ ord_union_2([H|T], H2, T2, Union, New) :-
383
+ compare(Order, H, H2),
384
+ ord_union(Order, H, T, H2, T2, Union, New).
385
+
386
+
387
+ %% ord_symdiff(+Set1, +Set2, ?Difference) is det.
388
+ %
389
+ % Is true when Difference is the symmetric difference of Set1 and
390
+ % Set2. I.e., Difference contains all elements that are not in the
391
+ % intersection of Set1 and Set2. The semantics is the same as the
392
+ % sequence below (but the actual implementation requires only a
393
+ % single scan).
394
+ %
395
+ % ```
396
+ % ord_union(Set1, Set2, Union),
397
+ % ord_intersection(Set1, Set2, Intersection),
398
+ % ord_subtract(Union, Intersection, Difference).
399
+ % ```
400
+ %
401
+ % For example:
402
+ %
403
+ % ```
404
+ % ?- ord_symdiff([1,2], [2,3], X).
405
+ % X = [1,3].
406
+ % ```
407
+
408
+ ord_symdiff([], Set2, Set2).
409
+ ord_symdiff([H1|T1], Set2, Difference) :-
410
+ ord_symdiff(Set2, H1, T1, Difference).
411
+
412
+ ord_symdiff([], H1, T1, [H1|T1]).
413
+ ord_symdiff([H2|T2], H1, T1, Difference) :-
414
+ compare(Order, H1, H2),
415
+ ord_symdiff(Order, H1, T1, H2, T2, Difference).
416
+
417
+ ord_symdiff(<, H1, Set1, H2, T2, [H1|Difference]) :-
418
+ ord_symdiff(Set1, H2, T2, Difference).
419
+ ord_symdiff(=, _, T1, _, T2, Difference) :-
420
+ ord_symdiff(T1, T2, Difference).
421
+ ord_symdiff(>, H1, T1, H2, Set2, [H2|Difference]) :-
422
+ ord_symdiff(Set2, H1, T1, Difference).
423
+
424
+ /* The osets library on which ordsets depends.
425
+
426
+ Author: Jon Jagger
427
+ E-mail: J.R.Jagger@shu.ac.uk
428
+ Copyright (c) 1993-2011, Jon Jagger
429
+ All rights reserved.
430
+ Redistribution and use in source and binary forms, with or without
431
+ modification, are permitted provided that the following conditions
432
+ are met:
433
+ 1. Redistributions of source code must retain the above copyright
434
+ notice, this list of conditions and the following disclaimer.
435
+ 2. Redistributions in binary form must reproduce the above copyright
436
+ notice, this list of conditions and the following disclaimer in
437
+ the documentation and/or other materials provided with the
438
+ distribution.
439
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
440
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
441
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
442
+ FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
443
+ COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
444
+ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
445
+ BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
446
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
447
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
448
+ LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
449
+ ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
450
+ POSSIBILITY OF SUCH DAMAGE.
451
+ */
452
+
453
+
454
+ /* Ordered set manipulation
455
+
456
+ This library defines set operations on sets represented as ordered
457
+ lists.
458
+
459
+ @author Jon Jagger
460
+ @deprecated Use the de-facto library ordsets.pl
461
+ */
462
+
463
+
464
+ %% oset_is(+OSet)
465
+ % check that OSet in correct format (standard order)
466
+
467
+ oset_is(-) :- !, fail. % var filter
468
+ oset_is([]).
469
+ oset_is([H|T]) :-
470
+ oset_is(T, H).
471
+
472
+ oset_is(-, _) :- !, fail. % var filter
473
+ oset_is([], _H).
474
+ oset_is([H|T], H0) :-
475
+ H0 @< H, % use standard order
476
+ oset_is(T, H).
477
+
478
+
479
+
480
+ %% oset_union(+OSet1, +OSet2, -Union).
481
+
482
+ oset_union([], Union, Union).
483
+ oset_union([H1|T1], L2, Union) :-
484
+ union2(L2, H1, T1, Union).
485
+
486
+ union2([], H1, T1, [H1|T1]).
487
+ union2([H2|T2], H1, T1, Union) :-
488
+ compare(Order, H1, H2),
489
+ union3(Order, H1, T1, H2, T2, Union).
490
+
491
+ union3(<, H1, T1, H2, T2, [H1|Union]) :-
492
+ union2(T1, H2, T2, Union).
493
+ union3(=, H1, T1, _H2, T2, [H1|Union]) :-
494
+ oset_union(T1, T2, Union).
495
+ union3(>, H1, T1, H2, T2, [H2|Union]) :-
496
+ union2(T2, H1, T1, Union).
497
+
498
+
499
+ %% oset_int(+OSet1, +OSet2, -Int)
500
+ % ordered set intersection
501
+
502
+ oset_int([], _Int, []).
503
+ oset_int([H1|T1], L2, Int) :-
504
+ isect2(L2, H1, T1, Int).
505
+
506
+ isect2([], _H1, _T1, []).
507
+ isect2([H2|T2], H1, T1, Int) :-
508
+ compare(Order, H1, H2),
509
+ isect3(Order, H1, T1, H2, T2, Int).
510
+
511
+ isect3(<, _H1, T1, H2, T2, Int) :-
512
+ isect2(T1, H2, T2, Int).
513
+ isect3(=, H1, T1, _H2, T2, [H1|Int]) :-
514
+ oset_int(T1, T2, Int).
515
+ isect3(>, H1, T1, _H2, T2, Int) :-
516
+ isect2(T2, H1, T1, Int).
517
+
518
+
519
+ %% oset_diff(+InOSet, +NotInOSet, -Diff)
520
+ % ordered set difference
521
+
522
+ oset_diff([], _Not, []).
523
+ oset_diff([H1|T1], L2, Diff) :-
524
+ diff21(L2, H1, T1, Diff).
525
+
526
+ diff21([], H1, T1, [H1|T1]).
527
+ diff21([H2|T2], H1, T1, Diff) :-
528
+ compare(Order, H1, H2),
529
+ diff3(Order, H1, T1, H2, T2, Diff).
530
+
531
+ diff12([], _H2, _T2, []).
532
+ diff12([H1|T1], H2, T2, Diff) :-
533
+ compare(Order, H1, H2),
534
+ diff3(Order, H1, T1, H2, T2, Diff).
535
+
536
+ diff3(<, H1, T1, H2, T2, [H1|Diff]) :-
537
+ diff12(T1, H2, T2, Diff).
538
+ diff3(=, _H1, T1, _H2, T2, Diff) :-
539
+ oset_diff(T1, T2, Diff).
540
+ diff3(>, H1, T1, _H2, T2, Diff) :-
541
+ diff21(T2, H1, T1, Diff).
542
+
543
+
544
+ %% oset_dunion(+SetofSets, -DUnion)
545
+ % distributed union
546
+
547
+ oset_dunion([], []).
548
+ oset_dunion([H|T], DUnion) :-
549
+ oset_dunion(T, H, DUnion).
550
+
551
+ oset_dunion([], DUnion, DUnion).
552
+ oset_dunion([H|T], DUnion0, DUnion) :-
553
+ oset_union(H, DUnion0, DUnion1),
554
+ oset_dunion(T, DUnion1, DUnion).
555
+
556
+
557
+ %% oset_dint(+SetofSets, -DInt)
558
+ % distributed intersection
559
+
560
+ oset_dint([], []).
561
+ oset_dint([H|T], DInt) :-
562
+ dint(T, H, DInt).
563
+
564
+ dint([], DInt, DInt).
565
+ dint([H|T], DInt0, DInt) :-
566
+ oset_int(H, DInt0, DInt1),
567
+ dint(T, DInt1, DInt).
568
+
569
+
570
+ %! oset_power(+Set, -PSet)
571
+ %
572
+ % True when PSet is the powerset of Set. That is, Pset is a set of
573
+ % all subsets of Set, where each subset is a proper ordered set.
574
+
575
+ oset_power(S, PSet) :-
576
+ reverse(S, R),
577
+ pset(R, [[]], PSet0),
578
+ sort(PSet0, PSet).
579
+
580
+
581
+ % The powerset of a set is the powerset of a set of one smaller,
582
+ % together with the set of one smaller where each subset is extended
583
+ % with the new element. Note that this produces the elements of the set
584
+ % in reverse order. Hence the reverse in oset_power/2.
585
+
586
+ pset([], PSet, PSet).
587
+ pset([H|T], PSet0, PSet) :-
588
+ happ(PSet0, H, PSet1),
589
+ pset(T, PSet1, PSet).
590
+
591
+ happ([], _, []).
592
+ happ([S|Ss], H, [[H|S],S|Rest]) :-
593
+ happ(Ss, H, Rest).
594
+
595
+ %% oset_addel(+Set, +El, -Add)
596
+ % ordered set element addition
597
+
598
+ oset_addel([], El, [El]).
599
+ oset_addel([H|T], El, Add) :-
600
+ compare(Order, H, El),
601
+ addel(Order, H, T, El, Add).
602
+
603
+ addel(<, H, T, El, [H|Add]) :-
604
+ oset_addel(T, El, Add).
605
+ addel(=, H, T, _El, [H|T]).
606
+ addel(>, H, T, El, [El,H|T]).
607
+
608
+ %% oset_delel(+Set, +El, -Del)
609
+ % ordered set element deletion
610
+
611
+ oset_delel([], _El, []).
612
+ oset_delel([H|T], El, Del) :-
613
+ compare(Order, H, El),
614
+ delel(Order, H, T, El, Del).
615
+
616
+ delel(<, H, T, El, [H|Del]) :-
617
+ oset_delel(T, El, Del).
618
+ delel(=, _H, T, _El, T).
619
+ delel(>, H, T, _El, [H|T]).
package/src/lib/pio.pl ADDED
@@ -0,0 +1,61 @@
1
+ /** Pure I/O through DCGs.
2
+
3
+ This is the eager, portable core shared by the Scryer and Trealla pio
4
+ libraries. Grammar descriptions stay declarative; only opening, reading,
5
+ writing, and closing streams are effectful.
6
+ */
7
+
8
+ :- module(pio, [
9
+ phrase_from_file/2,
10
+ phrase_from_file/3,
11
+ phrase_to_file/2,
12
+ phrase_to_file/3,
13
+ phrase_to_stream/2
14
+ ]).
15
+
16
+ :- use_module(library(error), [must_be/2]).
17
+ :- use_module(library(charsio), [get_n_chars/3]).
18
+
19
+ :- meta_predicate(phrase_from_file(2, '?')).
20
+ :- meta_predicate(phrase_from_file(2, '?', '?')).
21
+ :- meta_predicate(phrase_to_file(2, '?')).
22
+ :- meta_predicate(phrase_to_file(2, '?', '?')).
23
+ :- meta_predicate(phrase_to_stream(2, '?')).
24
+
25
+ phrase_from_file(Grammar, File) :-
26
+ phrase_from_file(Grammar, File, []).
27
+
28
+ phrase_from_file(Grammar, File, Options) :-
29
+ must_be(list, Options),
30
+ setup_call_cleanup(
31
+ open(File, read, Stream, Options),
32
+ ( get_n_chars(Stream, _, Chars), phrase(Grammar, Chars) ),
33
+ close(Stream)
34
+ ).
35
+
36
+ phrase_to_file(Grammar, File) :-
37
+ phrase_to_file(Grammar, File, []).
38
+
39
+ phrase_to_file(Grammar, File, Options) :-
40
+ must_be(list, Options),
41
+ setup_call_cleanup(
42
+ open(File, write, Stream, Options),
43
+ phrase_to_stream(Grammar, Stream),
44
+ close(Stream)
45
+ ).
46
+
47
+ phrase_to_stream(Grammar, Stream) :-
48
+ phrase(Grammar, Units),
49
+ ( stream_property(Stream, type(binary)) -> pio__put_bytes(Units, Stream)
50
+ ; pio__put_chars(Units, Stream)
51
+ ).
52
+
53
+ pio__put_chars([], _).
54
+ pio__put_chars([Char|Chars], Stream) :-
55
+ put_char(Stream, Char),
56
+ pio__put_chars(Chars, Stream).
57
+
58
+ pio__put_bytes([], _).
59
+ pio__put_bytes([Byte|Bytes], Stream) :-
60
+ put_byte(Stream, Byte),
61
+ pio__put_bytes(Bytes, Stream).