eyeprolog 1.3.79 → 1.3.81
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.
- package/examples/output/portable-library-overlap.pl +1 -0
- package/examples/portable-library-overlap.pl +46 -0
- package/package.json +1 -1
- package/playground.html +2 -1
- package/src/cleanup.js +1 -12
- package/src/iso.js +6 -3
- package/src/lib/assoc.pl +472 -31
- package/src/lib/charsio.pl +49 -0
- package/src/lib/clpb.pl +1975 -0
- package/src/lib/debug.pl +32 -6
- package/src/lib/dif.pl +7 -0
- package/src/lib/error.pl +2 -2
- package/src/lib/format.pl +60 -26
- package/src/lib/gensym.pl +33 -0
- package/src/lib/lists.pl +30 -0
- package/src/lib/ordsets.pl +619 -0
- package/src/lib/pio.pl +61 -0
- package/src/lib/random.pl +44 -2
- package/src/lib/reif.pl +94 -0
- package/src/lib/tabling.pl +16 -0
- package/src/lib/time.pl +26 -0
- package/src/lib/ugraphs.pl +633 -0
- package/src/lib/uuid.pl +66 -3
- package/src/lib/when.pl +103 -0
- package/src/parser.js +7 -1
- package/src/playground-worker.js +1 -1
- package/src/program.js +3 -2
- package/src/scryer-compat.js +181 -1
- package/src/solver.js +36 -60
- package/src/standard-library.js +90 -9
- package/test/run-regression.mjs +104 -6
- package/the-art-of-eyeprolog.md +84 -38
|
@@ -0,0 +1 @@
|
|
|
1
|
+
overlap_example(overlap(booleans([1, 0]), union("abc"), reachable("abc"), filtered("aa"), awakened(yes), generated(shared1), uppercase("A"), transposed([[1, 4], [2, 5], [3, 6]]), tabled_paths("bc"))).
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
% Representative source shared with the Scryer and Trealla library layouts.
|
|
2
|
+
|
|
3
|
+
:- use_module(library(charsio), [char_type/2]).
|
|
4
|
+
:- use_module(library(clpb), [sat/1, labeling/1]).
|
|
5
|
+
:- use_module(library(gensym), [gensym/2, reset_gensym/1]).
|
|
6
|
+
:- use_module(library(lists), [transpose/2]).
|
|
7
|
+
:- use_module(library(ordsets), [ord_union/3]).
|
|
8
|
+
:- use_module(library(reif), [tfilter/3, (=)/3]).
|
|
9
|
+
:- use_module(library(tabling)).
|
|
10
|
+
:- use_module(library(ugraphs), [vertices_edges_to_ugraph/3, reachable/3]).
|
|
11
|
+
:- use_module(library(when), [when/2]).
|
|
12
|
+
|
|
13
|
+
:- table path/2.
|
|
14
|
+
|
|
15
|
+
edge(a, b).
|
|
16
|
+
edge(b, c).
|
|
17
|
+
|
|
18
|
+
path(X, Y) :- path(X, Z), edge(Z, Y).
|
|
19
|
+
path(X, Y) :- edge(X, Y).
|
|
20
|
+
|
|
21
|
+
%% goal: overlap_example(Result)
|
|
22
|
+
|
|
23
|
+
overlap_example(overlap(
|
|
24
|
+
booleans([X,Y]),
|
|
25
|
+
union(Union),
|
|
26
|
+
reachable(Reachable),
|
|
27
|
+
filtered(Filtered),
|
|
28
|
+
awakened(Awakened),
|
|
29
|
+
generated(Generated),
|
|
30
|
+
uppercase(Uppercase),
|
|
31
|
+
transposed(Transposed),
|
|
32
|
+
tabled_paths(Paths)
|
|
33
|
+
)) :-
|
|
34
|
+
sat(X * ~Y),
|
|
35
|
+
labeling([X,Y]),
|
|
36
|
+
ord_union([a,c], [b,c], Union),
|
|
37
|
+
vertices_edges_to_ugraph([a,b,c], [a-b,b-c], Graph),
|
|
38
|
+
reachable(a, Graph, Reachable),
|
|
39
|
+
tfilter(=(a), [a,b,a], Filtered),
|
|
40
|
+
when(nonvar(Wake), Awakened = yes),
|
|
41
|
+
Wake = now,
|
|
42
|
+
reset_gensym(shared),
|
|
43
|
+
gensym(shared, Generated),
|
|
44
|
+
char_type(a, upper(Uppercase)),
|
|
45
|
+
transpose([[1,2,3],[4,5,6]], Transposed),
|
|
46
|
+
findall(Node, path(a, Node), Paths).
|
package/package.json
CHANGED
package/playground.html
CHANGED
|
@@ -600,6 +600,7 @@
|
|
|
600
600
|
"pi",
|
|
601
601
|
"pointer-analysis",
|
|
602
602
|
"polynomial",
|
|
603
|
+
"portable-library-overlap",
|
|
603
604
|
"prime-range",
|
|
604
605
|
"proof-contrapositive",
|
|
605
606
|
"quadratic-formula",
|
|
@@ -965,7 +966,7 @@
|
|
|
965
966
|
runButton.disabled = true;
|
|
966
967
|
stopButton.disabled = false;
|
|
967
968
|
|
|
968
|
-
const workerUrl = new URL('./src/playground-worker.js?playground=
|
|
969
|
+
const workerUrl = new URL('./src/playground-worker.js?playground=20260825a', location.href);
|
|
969
970
|
activeWorker = new Worker(workerUrl, { type: 'module' });
|
|
970
971
|
const worker = activeWorker;
|
|
971
972
|
const request = {
|
package/src/cleanup.js
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
// therefore need two pieces of host support:
|
|
6
6
|
// * discarded resumeBuiltin iterators must be closed when search is pruned;
|
|
7
7
|
// * a cleanup iterator can report that its protected goal has no alternatives,
|
|
8
|
-
// so the
|
|
8
|
+
// so the solver does not retain an exhausted resume frame.
|
|
9
9
|
import { PrologError } from './errors.js';
|
|
10
10
|
import { deref } from './term.js';
|
|
11
11
|
|
|
@@ -59,10 +59,6 @@ export function installCleanupLifecycle(Solver) {
|
|
|
59
59
|
}
|
|
60
60
|
}
|
|
61
61
|
};
|
|
62
|
-
|
|
63
|
-
prototype.hasPendingAlternatives = function cleanupAwarePendingAlternatives() {
|
|
64
|
-
return this.solveStacks.some((stack) => stack.some(frameHasPendingAlternative));
|
|
65
|
-
};
|
|
66
62
|
}
|
|
67
63
|
|
|
68
64
|
export function registerCleanupBuiltins(registry) {
|
|
@@ -168,13 +164,6 @@ function closeFrames(frames, suppressErrors) {
|
|
|
168
164
|
if (!suppressErrors && firstError != null) throw firstError;
|
|
169
165
|
}
|
|
170
166
|
|
|
171
|
-
function frameHasPendingAlternative(frame) {
|
|
172
|
-
if (frame?.kind !== 'resumeBuiltin') return true;
|
|
173
|
-
const predicate = frame.iterator?.hasPendingAlternatives;
|
|
174
|
-
if (typeof predicate !== 'function') return true;
|
|
175
|
-
return predicate.call(frame.iterator);
|
|
176
|
-
}
|
|
177
|
-
|
|
178
167
|
function callableTerm(term, env) {
|
|
179
168
|
const value = deref(term, env);
|
|
180
169
|
if (value.type === 'var') throw new PrologError('instantiation_error');
|
package/src/iso.js
CHANGED
|
@@ -2598,7 +2598,7 @@ function callResidueVarsBuiltin({ solver, goal, env }) {
|
|
|
2598
2598
|
const child = solver.cloneForInnerGoal();
|
|
2599
2599
|
let pending = true;
|
|
2600
2600
|
// The wrapper iterator is resumable even for a deterministic Goal. Expose
|
|
2601
|
-
// the child's actual search state so the
|
|
2601
|
+
// the child's actual search state so the solver does not install a phantom
|
|
2602
2602
|
// choicepoint after the final answer.
|
|
2603
2603
|
const iterator = (function* residueSolutions() {
|
|
2604
2604
|
try {
|
|
@@ -2663,7 +2663,10 @@ function* phraseBuiltin({ solver, goal, env }) {
|
|
|
2663
2663
|
: variable(`\u0000phrase:${++isoFresh}`);
|
|
2664
2664
|
const expanded = expandDcgBody(grammarBody, input, finalOutput, {
|
|
2665
2665
|
env,
|
|
2666
|
-
|
|
2666
|
+
// A meta-predicate wrapper qualifies its grammar argument at the call
|
|
2667
|
+
// site. Preserve that qualification instead of replacing it with the
|
|
2668
|
+
// lexical module of the wrapper's phrase/2 call.
|
|
2669
|
+
module: grammarBody.module ?? goal.module ?? 'user',
|
|
2667
2670
|
});
|
|
2668
2671
|
const finish = goal.arity === 2 ? null : compound('=', [finalOutput, requestedOutput]);
|
|
2669
2672
|
// Recursive DCGs are automatically tabled in normal mode. Keep tables in a
|
|
@@ -2671,7 +2674,7 @@ function* phraseBuiltin({ solver, goal, env }) {
|
|
|
2671
2674
|
// same grammar/input (issue #48) reuses its completed table, while switching
|
|
2672
2675
|
// to a distinct input (issue #28) drops the previous invocation as one unit
|
|
2673
2676
|
// instead of retaining or individually evicting every recursive tail.
|
|
2674
|
-
const phraseModule =
|
|
2677
|
+
const phraseModule = grammarBody.module ?? goal.module ?? 'user';
|
|
2675
2678
|
const tableScopeSignature = solver.innerTableSignature(
|
|
2676
2679
|
[grammarBody, input, requestedOutput],
|
|
2677
2680
|
env,
|
package/src/lib/assoc.pl
CHANGED
|
@@ -1,42 +1,483 @@
|
|
|
1
|
-
|
|
1
|
+
/* Author: R.A.O'Keefe, L.Damas, V.S.Costa, Glenn Burgess,
|
|
2
|
+
Jiri Spitz and Jan Wielemaker
|
|
3
|
+
E-mail: J.Wielemaker@vu.nl
|
|
4
|
+
WWW: http://www.swi-prolog.org
|
|
5
|
+
Copyright (c) 2004-2018, various people and institutions
|
|
6
|
+
All rights reserved.
|
|
2
7
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
8
|
+
Redistribution and use in source and binary forms, with or without
|
|
9
|
+
modification, are permitted provided that the following conditions
|
|
10
|
+
are met:
|
|
11
|
+
|
|
12
|
+
1. Redistributions of source code must retain the above copyright
|
|
13
|
+
notice, this list of conditions and the following disclaimer.
|
|
14
|
+
|
|
15
|
+
2. Redistributions in binary form must reproduce the above copyright
|
|
16
|
+
notice, this list of conditions and the following disclaimer in
|
|
17
|
+
the documentation and/or other materials provided with the
|
|
18
|
+
distribution.
|
|
19
|
+
|
|
20
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
21
|
+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
22
|
+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
23
|
+
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
24
|
+
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
25
|
+
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
26
|
+
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
27
|
+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
28
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
29
|
+
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
30
|
+
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
31
|
+
POSSIBILITY OF SUCH DAMAGE.
|
|
7
32
|
*/
|
|
8
33
|
|
|
9
|
-
:- module(assoc,
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
34
|
+
:- module(assoc,
|
|
35
|
+
[ empty_assoc/1, % -Assoc
|
|
36
|
+
is_assoc/1, % +Assoc
|
|
37
|
+
assoc_to_list/2, % +Assoc, -Pairs
|
|
38
|
+
assoc_to_keys/2, % +Assoc, -List
|
|
39
|
+
assoc_to_values/2, % +Assoc, -List
|
|
40
|
+
gen_assoc/3, % ?Key, +Assoc, ?Value
|
|
41
|
+
get_assoc/3, % +Key, +Assoc, ?Value
|
|
42
|
+
get_assoc/5, % +Key, +Assoc0, ?Val0, ?Assoc, ?Val
|
|
43
|
+
list_to_assoc/2, % +List, ?Assoc
|
|
44
|
+
map_assoc/2, % :Goal, +Assoc
|
|
45
|
+
map_assoc/3, % :Goal, +Assoc0, ?Assoc
|
|
46
|
+
max_assoc/3, % +Assoc, ?Key, ?Value
|
|
47
|
+
min_assoc/3, % +Assoc, ?Key, ?Value
|
|
48
|
+
ord_list_to_assoc/2, % +List, ?Assoc
|
|
49
|
+
put_assoc/4, % +Key, +Assoc0, +Value, ?Assoc
|
|
50
|
+
del_assoc/4, % +Key, +Assoc0, ?Value, ?Assoc
|
|
51
|
+
del_min_assoc/4, % +Assoc0, ?Key, ?Value, ?Assoc
|
|
52
|
+
del_max_assoc/4 % +Assoc0, ?Key, ?Value, ?Assoc
|
|
53
|
+
]).
|
|
54
|
+
|
|
55
|
+
:- use_module(library(lists)).
|
|
56
|
+
|
|
57
|
+
/** Binary associations
|
|
58
|
+
|
|
59
|
+
Assocs are Key-Value associations implemented as a balanced binary tree
|
|
60
|
+
(AVL tree).
|
|
61
|
+
|
|
62
|
+
Authors: R.A.O'Keefe, L.Damas, V.S.Costa and Jan Wielemaker
|
|
63
|
+
*/
|
|
64
|
+
|
|
65
|
+
:- meta_predicate(map_assoc(1, ?)).
|
|
66
|
+
:- meta_predicate(map_assoc(2, ?, ?)).
|
|
67
|
+
|
|
68
|
+
%% empty_assoc(?Assoc) is semidet.
|
|
69
|
+
%
|
|
70
|
+
% Is true if Assoc is the empty association list.
|
|
71
|
+
|
|
72
|
+
empty_assoc(t).
|
|
73
|
+
|
|
74
|
+
%% assoc_to_list(+Assoc, -Pairs) is det.
|
|
75
|
+
%
|
|
76
|
+
% Translate Assoc to a list Pairs of Key-Value pairs. The keys
|
|
77
|
+
% in Pairs are sorted in ascending order.
|
|
78
|
+
|
|
79
|
+
assoc_to_list(Assoc, List) :-
|
|
80
|
+
assoc_to_list(Assoc, List, []).
|
|
81
|
+
|
|
82
|
+
assoc_to_list(t(Key,Val,_,L,R), List, Rest) :-
|
|
83
|
+
assoc_to_list(L, List, [Key-Val|More]),
|
|
84
|
+
assoc_to_list(R, More, Rest).
|
|
85
|
+
assoc_to_list(t, List, List).
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
%% assoc_to_keys(+Assoc, -Keys) is det.
|
|
89
|
+
%
|
|
90
|
+
% True if Keys is the list of keys in Assoc. The keys are sorted
|
|
91
|
+
% in ascending order.
|
|
92
|
+
|
|
93
|
+
assoc_to_keys(Assoc, List) :-
|
|
94
|
+
assoc_to_keys(Assoc, List, []).
|
|
95
|
+
|
|
96
|
+
assoc_to_keys(t(Key,_,_,L,R), List, Rest) :-
|
|
97
|
+
assoc_to_keys(L, List, [Key|More]),
|
|
98
|
+
assoc_to_keys(R, More, Rest).
|
|
99
|
+
assoc_to_keys(t, List, List).
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
%% assoc_to_values(+Assoc, -Values) is det.
|
|
103
|
+
%
|
|
104
|
+
% True if Values is the list of values in Assoc. Values are
|
|
105
|
+
% ordered in ascending order of the key to which they were
|
|
106
|
+
% associated. Values may contain duplicates.
|
|
107
|
+
|
|
108
|
+
assoc_to_values(Assoc, List) :-
|
|
109
|
+
assoc_to_values(Assoc, List, []).
|
|
110
|
+
|
|
111
|
+
assoc_to_values(t(_,Value,_,L,R), List, Rest) :-
|
|
112
|
+
assoc_to_values(L, List, [Value|More]),
|
|
113
|
+
assoc_to_values(R, More, Rest).
|
|
114
|
+
assoc_to_values(t, List, List).
|
|
115
|
+
|
|
116
|
+
%% is_assoc(+Assoc) is semidet.
|
|
117
|
+
%
|
|
118
|
+
% True if Assoc is an association list. This predicate checks
|
|
119
|
+
% that the structure is valid, elements are in order, and tree
|
|
120
|
+
% is balanced to the extent guaranteed by AVL trees. I.e.,
|
|
121
|
+
% branches of each subtree differ in depth by at most 1.
|
|
122
|
+
|
|
123
|
+
is_assoc(Assoc) :-
|
|
124
|
+
is_assoc(Assoc, _Min, _Max, _Depth).
|
|
125
|
+
|
|
126
|
+
is_assoc(t,X,X,0) :- !.
|
|
127
|
+
is_assoc(t(K,_,-,t,t),K,K,1) :- !, ground(K).
|
|
128
|
+
is_assoc(t(K,_,>,t,t(RK,_,-,t,t)),K,RK,2) :-
|
|
129
|
+
% Ensure right side Key is 'greater' than K
|
|
130
|
+
!, ground((K,RK)), K @< RK.
|
|
131
|
+
|
|
132
|
+
is_assoc(t(K,_,<,t(LK,_,-,t,t),t),LK,K,2) :-
|
|
133
|
+
% Ensure left side Key is 'less' than K
|
|
134
|
+
!, ground((LK,K)), LK @< K.
|
|
135
|
+
|
|
136
|
+
is_assoc(t(K,_,B,L,R),Min,Max,Depth) :-
|
|
137
|
+
is_assoc(L,Min,LMax,LDepth),
|
|
138
|
+
is_assoc(R,RMin,Max,RDepth),
|
|
139
|
+
% Ensure Balance matches depth
|
|
140
|
+
compare(Rel,RDepth,LDepth),
|
|
141
|
+
balance(Rel,B),
|
|
142
|
+
% Ensure ordering
|
|
143
|
+
ground((LMax,K,RMin)),
|
|
144
|
+
LMax @< K,
|
|
145
|
+
K @< RMin,
|
|
146
|
+
Depth is max(LDepth, RDepth)+1.
|
|
147
|
+
|
|
148
|
+
% Private lookup table matching comparison operators to Balance operators used in tree
|
|
149
|
+
balance(=,-).
|
|
150
|
+
balance(<,<).
|
|
151
|
+
balance(>,>).
|
|
152
|
+
|
|
153
|
+
%% gen_assoc(?Key, +Assoc, ?Value) is nondet.
|
|
154
|
+
%
|
|
155
|
+
% True if Key-Value is an association in Assoc. Enumerates keys in
|
|
156
|
+
% ascending order on backtracking.
|
|
157
|
+
|
|
158
|
+
gen_assoc(Key, Assoc, Value) :-
|
|
159
|
+
( ground(Key)
|
|
160
|
+
-> get_assoc(Key, Assoc, Value)
|
|
161
|
+
; gen_assoc_(Key, Assoc, Value)
|
|
162
|
+
).
|
|
163
|
+
|
|
164
|
+
gen_assoc_(Key, t(_,_,_,L,_), Val) :-
|
|
165
|
+
gen_assoc_(Key, L, Val).
|
|
166
|
+
gen_assoc_(Key, t(Key,Val,_,_,_), Val).
|
|
167
|
+
gen_assoc_(Key, t(_,_,_,_,R), Val) :-
|
|
168
|
+
gen_assoc_(Key, R, Val).
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
%% get_assoc(+Key, +Assoc, -Value) is semidet.
|
|
172
|
+
%
|
|
173
|
+
% True if Key-Value is an association in Assoc.
|
|
174
|
+
%
|
|
175
|
+
% Throws error: `type_error(assoc, Assoc)` if Assoc is not an association list.
|
|
176
|
+
|
|
177
|
+
get_assoc(Key, Assoc, Val) :-
|
|
178
|
+
must_be(assoc, Assoc),
|
|
179
|
+
get_assoc_(Key, Assoc, Val).
|
|
180
|
+
|
|
181
|
+
/*
|
|
182
|
+
:- if(current_predicate('$btree_find_node'/5)).
|
|
183
|
+
get_assoc_(Key, Tree, Val) :-
|
|
184
|
+
Tree \== t,
|
|
185
|
+
'$btree_find_node'(Key, Tree, 0x010405, Node, =),
|
|
186
|
+
arg(2, Node, Val).
|
|
187
|
+
:- else.
|
|
188
|
+
*/
|
|
189
|
+
get_assoc_(Key, t(K,V,_,L,R), Val) :-
|
|
190
|
+
compare(Rel, Key, K),
|
|
191
|
+
get_assoc(Rel, Key, V, L, R, Val).
|
|
192
|
+
|
|
193
|
+
get_assoc(=, _, Val, _, _, Val).
|
|
194
|
+
get_assoc(<, Key, _, Tree, _, Val) :-
|
|
195
|
+
get_assoc(Key, Tree, Val).
|
|
196
|
+
get_assoc(>, Key, _, _, Tree, Val) :-
|
|
197
|
+
get_assoc(Key, Tree, Val).
|
|
198
|
+
% :- endif.
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
%% get_assoc(+Key, +Assoc0, ?Val0, ?Assoc, ?Val) is semidet.
|
|
202
|
+
%
|
|
203
|
+
% True if Key-Val0 is in Assoc0 and Key-Val is in Assoc.
|
|
204
|
+
|
|
205
|
+
get_assoc(Key, t(K,V,B,L,R), Val, t(K,NV,B,NL,NR), NVal) :-
|
|
206
|
+
compare(Rel, Key, K),
|
|
207
|
+
get_assoc(Rel, Key, V, L, R, Val, NV, NL, NR, NVal).
|
|
208
|
+
|
|
209
|
+
get_assoc(=, _, Val, L, R, Val, NVal, L, R, NVal).
|
|
210
|
+
get_assoc(<, Key, V, L, R, Val, V, NL, R, NVal) :-
|
|
211
|
+
get_assoc(Key, L, Val, NL, NVal).
|
|
212
|
+
get_assoc(>, Key, V, L, R, Val, V, L, NR, NVal) :-
|
|
213
|
+
get_assoc(Key, R, Val, NR, NVal).
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
%% list_to_assoc(+Pairs, -Assoc) is det.
|
|
217
|
+
%
|
|
218
|
+
% Create an association from a list Pairs of Key-Value pairs. List
|
|
219
|
+
% must not contain duplicate keys.
|
|
220
|
+
%
|
|
221
|
+
% Throws error: `domain_error(unique_key_pairs, List)` if List contains duplicate keys
|
|
222
|
+
|
|
223
|
+
list_to_assoc(List, Assoc) :-
|
|
224
|
+
( List = [] -> Assoc = t
|
|
225
|
+
; keysort(List, Sorted),
|
|
226
|
+
( ord_pairs(Sorted)
|
|
227
|
+
-> length(Sorted, N),
|
|
228
|
+
list_to_assoc(N, Sorted, [], _, Assoc)
|
|
229
|
+
; throw(error(domain_error(unique_key_pairs, List), list_to_assoc/2))
|
|
230
|
+
)
|
|
231
|
+
).
|
|
232
|
+
|
|
233
|
+
list_to_assoc(1, [K-V|More], More, 1, t(K,V,-,t,t)) :- !.
|
|
234
|
+
list_to_assoc(2, [K1-V1,K2-V2|More], More, 2, t(K2,V2,<,t(K1,V1,-,t,t),t)) :- !.
|
|
235
|
+
list_to_assoc(N, List, More, Depth, t(K,V,Balance,L,R)) :-
|
|
236
|
+
N0 is N - 1,
|
|
237
|
+
RN is N0 div 2,
|
|
238
|
+
Rem is N0 mod 2,
|
|
239
|
+
LN is RN + Rem,
|
|
240
|
+
list_to_assoc(LN, List, [K-V|Upper], LDepth, L),
|
|
241
|
+
list_to_assoc(RN, Upper, More, RDepth, R),
|
|
242
|
+
Depth is LDepth + 1,
|
|
243
|
+
compare(B, RDepth, LDepth),
|
|
244
|
+
balance(B, Balance).
|
|
245
|
+
|
|
246
|
+
%% ord_list_to_assoc(+Pairs, -Assoc) is det.
|
|
247
|
+
%
|
|
248
|
+
% Assoc is created from an ordered list Pairs of Key-Value
|
|
249
|
+
% pairs. The pairs must occur in strictly ascending order of
|
|
250
|
+
% their keys.
|
|
251
|
+
%
|
|
252
|
+
% Throws error: `domain_error(key_ordered_pairs, List)` if pairs are not ordered.
|
|
253
|
+
|
|
254
|
+
ord_list_to_assoc(Sorted, Assoc) :-
|
|
255
|
+
( Sorted = [] -> Assoc = t
|
|
256
|
+
; ( ord_pairs(Sorted)
|
|
257
|
+
-> length(Sorted, N),
|
|
258
|
+
list_to_assoc(N, Sorted, [], _, Assoc)
|
|
259
|
+
; domain_error(key_ordered_pairs, Sorted)
|
|
260
|
+
)
|
|
261
|
+
).
|
|
262
|
+
|
|
263
|
+
%% ord_pairs(+Pairs) is semidet
|
|
264
|
+
%
|
|
265
|
+
% True if Pairs is a list of Key-Val pairs strictly ordered by key.
|
|
266
|
+
|
|
267
|
+
ord_pairs([K-_V|Rest]) :-
|
|
268
|
+
ord_pairs(Rest, K).
|
|
269
|
+
ord_pairs([], _K).
|
|
270
|
+
ord_pairs([K-_V|Rest], K0) :-
|
|
271
|
+
K0 @< K,
|
|
272
|
+
ord_pairs(Rest, K).
|
|
273
|
+
|
|
274
|
+
%% map_assoc(:Pred, +Assoc) is semidet.
|
|
275
|
+
%
|
|
276
|
+
% True if Pred(Value) is true for all values in Assoc.
|
|
277
|
+
|
|
278
|
+
map_assoc(Pred, T) :-
|
|
279
|
+
map_assoc_(T, Pred).
|
|
280
|
+
|
|
281
|
+
map_assoc_(t, _).
|
|
282
|
+
map_assoc_(t(_,Val,_,L,R), Pred) :-
|
|
283
|
+
map_assoc_(L, Pred),
|
|
284
|
+
call(Pred, Val),
|
|
285
|
+
map_assoc_(R, Pred).
|
|
286
|
+
|
|
287
|
+
%% map_assoc(:Pred, +Assoc0, ?Assoc) is semidet.
|
|
288
|
+
%
|
|
289
|
+
% Map corresponding values. True if Assoc is Assoc0 with Pred
|
|
290
|
+
% applied to all corresponding pairs of of values.
|
|
291
|
+
|
|
292
|
+
map_assoc(Pred, T0, T) :-
|
|
293
|
+
map_assoc_(T0, Pred, T).
|
|
294
|
+
|
|
295
|
+
map_assoc_(t, _, t).
|
|
296
|
+
map_assoc_(t(Key,Val,B,L0,R0), Pred, t(Key,Ans,B,L1,R1)) :-
|
|
297
|
+
map_assoc_(L0, Pred, L1),
|
|
298
|
+
call(Pred, Val, Ans),
|
|
299
|
+
map_assoc_(R0, Pred, R1).
|
|
300
|
+
|
|
301
|
+
|
|
302
|
+
%% max_assoc(+Assoc, -Key, -Value) is semidet.
|
|
303
|
+
%
|
|
304
|
+
% True if Key-Value is in Assoc and Key is the largest key.
|
|
305
|
+
|
|
306
|
+
max_assoc(t(K,V,_,_,R), Key, Val) :-
|
|
307
|
+
max_assoc(R, K, V, Key, Val).
|
|
308
|
+
|
|
309
|
+
max_assoc(t, K, V, K, V).
|
|
310
|
+
max_assoc(t(K,V,_,_,R), _, _, Key, Val) :-
|
|
311
|
+
max_assoc(R, K, V, Key, Val).
|
|
312
|
+
|
|
313
|
+
|
|
314
|
+
%% min_assoc(+Assoc, -Key, -Value) is semidet.
|
|
315
|
+
%
|
|
316
|
+
% True if Key-Value is in assoc and Key is the smallest key.
|
|
317
|
+
|
|
318
|
+
min_assoc(t(K,V,_,L,_), Key, Val) :-
|
|
319
|
+
min_assoc(L, K, V, Key, Val).
|
|
320
|
+
|
|
321
|
+
min_assoc(t, K, V, K, V).
|
|
322
|
+
min_assoc(t(K,V,_,L,_), _, _, Key, Val) :-
|
|
323
|
+
min_assoc(L, K, V, Key, Val).
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
%% put_assoc(+Key, +Assoc0, +Value, -Assoc) is det.
|
|
327
|
+
%
|
|
328
|
+
% Assoc is Assoc0, except that Key is associated with
|
|
329
|
+
% Value. This can be used to insert and change associations.
|
|
330
|
+
|
|
331
|
+
put_assoc(Key, A0, Value, A) :-
|
|
332
|
+
insert(A0, Key, Value, A, _).
|
|
333
|
+
|
|
334
|
+
insert(t, Key, Val, t(Key,Val,-,t,t), yes).
|
|
335
|
+
insert(t(Key,Val,B,L,R), K, V, NewTree, WhatHasChanged) :-
|
|
336
|
+
compare(Rel, K, Key),
|
|
337
|
+
insert(Rel, t(Key,Val,B,L,R), K, V, NewTree, WhatHasChanged).
|
|
338
|
+
|
|
339
|
+
insert(=, t(Key,_,B,L,R), _, V, t(Key,V,B,L,R), no).
|
|
340
|
+
insert(<, t(Key,Val,B,L,R), K, V, NewTree, WhatHasChanged) :-
|
|
341
|
+
insert(L, K, V, NewL, LeftHasChanged),
|
|
342
|
+
adjust(LeftHasChanged, t(Key,Val,B,NewL,R), left, NewTree, WhatHasChanged).
|
|
343
|
+
insert(>, t(Key,Val,B,L,R), K, V, NewTree, WhatHasChanged) :-
|
|
344
|
+
insert(R, K, V, NewR, RightHasChanged),
|
|
345
|
+
adjust(RightHasChanged, t(Key,Val,B,L,NewR), right, NewTree, WhatHasChanged).
|
|
346
|
+
|
|
347
|
+
adjust(no, Oldree, _, Oldree, no).
|
|
348
|
+
adjust(yes, t(Key,Val,B0,L,R), LoR, NewTree, WhatHasChanged) :-
|
|
349
|
+
table(B0, LoR, B1, WhatHasChanged, ToBeRebalanced),
|
|
350
|
+
rebalance(ToBeRebalanced, t(Key,Val,B0,L,R), B1, NewTree, _, _).
|
|
351
|
+
|
|
352
|
+
% balance where balance whole tree to be
|
|
353
|
+
% before inserted after increased rebalanced
|
|
354
|
+
table(- , left , < , yes , no ) :- !.
|
|
355
|
+
table(- , right , > , yes , no ) :- !.
|
|
356
|
+
table(< , left , - , no , yes ) :- !.
|
|
357
|
+
table(< , right , - , no , no ) :- !.
|
|
358
|
+
table(> , left , - , no , no ) :- !.
|
|
359
|
+
table(> , right , - , no , yes ) :- !.
|
|
360
|
+
|
|
361
|
+
%% del_min_assoc(+Assoc0, ?Key, ?Val, -Assoc) is semidet.
|
|
362
|
+
%
|
|
363
|
+
% True if Key-Value is in Assoc0 and Key is the smallest key.
|
|
364
|
+
% Assoc is Assoc0 with Key-Value removed. Warning: This will
|
|
365
|
+
% succeed with _no_ bindings for Key or Val if Assoc0 is empty.
|
|
366
|
+
|
|
367
|
+
del_min_assoc(Tree, Key, Val, NewTree) :-
|
|
368
|
+
del_min_assoc(Tree, Key, Val, NewTree, _DepthChanged).
|
|
369
|
+
|
|
370
|
+
del_min_assoc(t(Key,Val,_B,t,R), Key, Val, R, yes) :- !.
|
|
371
|
+
del_min_assoc(t(K,V,B,L,R), Key, Val, NewTree, Changed) :-
|
|
372
|
+
del_min_assoc(L, Key, Val, NewL, LeftChanged),
|
|
373
|
+
deladjust(LeftChanged, t(K,V,B,NewL,R), left, NewTree, Changed).
|
|
374
|
+
|
|
375
|
+
%% del_max_assoc(+Assoc0, ?Key, ?Val, -Assoc) is semidet.
|
|
376
|
+
%
|
|
377
|
+
% True if Key-Value is in Assoc0 and Key is the greatest key.
|
|
378
|
+
% Assoc is Assoc0 with Key-Value removed. Warning: This will
|
|
379
|
+
% succeed with _no_ bindings for Key or Val if Assoc0 is empty.
|
|
380
|
+
|
|
381
|
+
del_max_assoc(Tree, Key, Val, NewTree) :-
|
|
382
|
+
del_max_assoc(Tree, Key, Val, NewTree, _DepthChanged).
|
|
383
|
+
|
|
384
|
+
del_max_assoc(t(Key,Val,_B,L,t), Key, Val, L, yes) :- !.
|
|
385
|
+
del_max_assoc(t(K,V,B,L,R), Key, Val, NewTree, Changed) :-
|
|
386
|
+
del_max_assoc(R, Key, Val, NewR, RightChanged),
|
|
387
|
+
deladjust(RightChanged, t(K,V,B,L,NewR), right, NewTree, Changed).
|
|
388
|
+
|
|
389
|
+
%% del_assoc(+Key, +Assoc0, ?Value, -Assoc) is semidet.
|
|
390
|
+
%
|
|
391
|
+
% True if Key-Value is in Assoc0. Assoc is Assoc0 with
|
|
392
|
+
% Key-Value removed.
|
|
393
|
+
|
|
394
|
+
del_assoc(Key, A0, Value, A) :-
|
|
395
|
+
delete(A0, Key, Value, A, _).
|
|
396
|
+
|
|
397
|
+
% delete(+Subtree, +SearchedKey, ?SearchedValue, ?SubtreeOut, ?WhatHasChanged)
|
|
398
|
+
delete(t(Key,Val,B,L,R), K, V, NewTree, WhatHasChanged) :-
|
|
399
|
+
compare(Rel, K, Key),
|
|
400
|
+
delete(Rel, t(Key,Val,B,L,R), K, V, NewTree, WhatHasChanged).
|
|
401
|
+
|
|
402
|
+
% delete(+KeySide, +Subtree, +SearchedKey, ?SearchedValue, ?SubtreeOut, ?WhatHasChanged)
|
|
403
|
+
% KeySide is an operator {<,=,>} indicating which branch should be searched for the key.
|
|
404
|
+
% WhatHasChanged {yes,no} indicates whether the NewTree has changed in depth.
|
|
405
|
+
delete(=, t(Key,Val,_B,t,R), Key, Val, R, yes) :- !.
|
|
406
|
+
delete(=, t(Key,Val,_B,L,t), Key, Val, L, yes) :- !.
|
|
407
|
+
delete(=, t(Key,Val,>,L,R), Key, Val, NewTree, WhatHasChanged) :-
|
|
408
|
+
% Rh tree is deeper, so rotate from R to L
|
|
409
|
+
del_min_assoc(R, K, V, NewR, RightHasChanged),
|
|
410
|
+
deladjust(RightHasChanged, t(K,V,>,L,NewR), right, NewTree, WhatHasChanged),
|
|
411
|
+
!.
|
|
412
|
+
delete(=, t(Key,Val,B,L,R), Key, Val, NewTree, WhatHasChanged) :-
|
|
413
|
+
% Rh tree is not deeper, so rotate from L to R
|
|
414
|
+
del_max_assoc(L, K, V, NewL, LeftHasChanged),
|
|
415
|
+
deladjust(LeftHasChanged, t(K,V,B,NewL,R), left, NewTree, WhatHasChanged),
|
|
416
|
+
!.
|
|
417
|
+
|
|
418
|
+
delete(<, t(Key,Val,B,L,R), K, V, NewTree, WhatHasChanged) :-
|
|
419
|
+
delete(L, K, V, NewL, LeftHasChanged),
|
|
420
|
+
deladjust(LeftHasChanged, t(Key,Val,B,NewL,R), left, NewTree, WhatHasChanged).
|
|
421
|
+
delete(>, t(Key,Val,B,L,R), K, V, NewTree, WhatHasChanged) :-
|
|
422
|
+
delete(R, K, V, NewR, RightHasChanged),
|
|
423
|
+
deladjust(RightHasChanged, t(Key,Val,B,L,NewR), right, NewTree, WhatHasChanged).
|
|
424
|
+
|
|
425
|
+
deladjust(no, OldTree, _, OldTree, no).
|
|
426
|
+
deladjust(yes, t(Key,Val,B0,L,R), LoR, NewTree, RealChange) :-
|
|
427
|
+
deltable(B0, LoR, B1, WhatHasChanged, ToBeRebalanced),
|
|
428
|
+
rebalance(ToBeRebalanced, t(Key,Val,B0,L,R), B1, NewTree, WhatHasChanged, RealChange).
|
|
15
429
|
|
|
16
|
-
|
|
430
|
+
% balance where balance whole tree to be
|
|
431
|
+
% before deleted after changed rebalanced
|
|
432
|
+
deltable(- , right , < , no , no ) :- !.
|
|
433
|
+
deltable(- , left , > , no , no ) :- !.
|
|
434
|
+
deltable(< , right , - , yes , yes ) :- !.
|
|
435
|
+
deltable(< , left , - , yes , no ) :- !.
|
|
436
|
+
deltable(> , right , - , yes , no ) :- !.
|
|
437
|
+
deltable(> , left , - , yes , yes ) :- !.
|
|
438
|
+
% It depends on the tree pattern in avl_geq whether it really decreases.
|
|
17
439
|
|
|
18
|
-
|
|
440
|
+
% Single and double tree rotations - these are common for insert and delete.
|
|
441
|
+
/* The patterns (>)-(>), (>)-( <), ( <)-( <) and ( <)-(>) on the LHS
|
|
442
|
+
always change the tree height and these are the only patterns which can
|
|
443
|
+
happen after an insertion. That's the reason why we can use a table only to
|
|
444
|
+
decide the needed changes.
|
|
19
445
|
|
|
20
|
-
|
|
21
|
-
|
|
446
|
+
The patterns (>)-( -) and ( <)-( -) do not change the tree height. After a
|
|
447
|
+
deletion any pattern can occur and so we return yes or no as a flag of a
|
|
448
|
+
height change. */
|
|
22
449
|
|
|
23
|
-
assoc__get([K-V|Pairs], Key, Value) :-
|
|
24
|
-
compare(Order, Key, K),
|
|
25
|
-
assoc__get(Order, Key, Value, V, Pairs).
|
|
26
450
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
451
|
+
rebalance(no, t(K,V,_,L,R), B, t(K,V,B,L,R), Changed, Changed).
|
|
452
|
+
rebalance(yes, OldTree, _, NewTree, _, RealChange) :-
|
|
453
|
+
avl_geq(OldTree, NewTree, RealChange).
|
|
30
454
|
|
|
31
|
-
|
|
32
|
-
|
|
455
|
+
avl_geq(t(A,VA,>,Alpha,t(B,VB,>,Beta,Gamma)),
|
|
456
|
+
t(B,VB,-,t(A,VA,-,Alpha,Beta),Gamma), yes) :- !.
|
|
457
|
+
avl_geq(t(A,VA,>,Alpha,t(B,VB,-,Beta,Gamma)),
|
|
458
|
+
t(B,VB,<,t(A,VA,>,Alpha,Beta),Gamma), no) :- !.
|
|
459
|
+
avl_geq(t(B,VB,<,t(A,VA,<,Alpha,Beta),Gamma),
|
|
460
|
+
t(A,VA,-,Alpha,t(B,VB,-,Beta,Gamma)), yes) :- !.
|
|
461
|
+
avl_geq(t(B,VB,<,t(A,VA,-,Alpha,Beta),Gamma),
|
|
462
|
+
t(A,VA,>,Alpha,t(B,VB,<,Beta,Gamma)), no) :- !.
|
|
463
|
+
avl_geq(t(A,VA,>,Alpha,t(B,VB,<,t(X,VX,B1,Beta,Gamma),Delta)),
|
|
464
|
+
t(X,VX,-,t(A,VA,B2,Alpha,Beta),t(B,VB,B3,Gamma,Delta)), yes) :-
|
|
465
|
+
!,
|
|
466
|
+
table2(B1, B2, B3).
|
|
467
|
+
avl_geq(t(B,VB,<,t(A,VA,>,Alpha,t(X,VX,B1,Beta,Gamma)),Delta),
|
|
468
|
+
t(X,VX,-,t(A,VA,B2,Alpha,Beta),t(B,VB,B3,Gamma,Delta)), yes) :-
|
|
469
|
+
!,
|
|
470
|
+
table2(B1, B2, B3).
|
|
33
471
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
assoc__put(Order, Key, Value, K, V, Pairs0, Pairs).
|
|
472
|
+
table2(< ,- ,> ).
|
|
473
|
+
table2(> ,< ,- ).
|
|
474
|
+
table2(- ,- ,- ).
|
|
38
475
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
476
|
+
must_be(assoc, X) :-
|
|
477
|
+
( X == t
|
|
478
|
+
-> true
|
|
479
|
+
; compound(X),
|
|
480
|
+
functor(X, t, 5)
|
|
481
|
+
), !.
|
|
482
|
+
must_be(assoc, X) :-
|
|
483
|
+
throw(error(type_error(assoc, X), _)).
|