* Fix paths to runtex
[chr.git] / hprolog.pl
blobe6cebb68ce0136d4b947c3dcd1e1dbbac14c9ce1
1 :- module(hprolog,
2 [ append/2, % +ListOfLists, -List
3 nth/3, % ?Index, ?List, ?Element
4 substitute/4, % +OldVal, +OldList, +NewVal, -NewList
5 memberchk_eq/2, % +Val, +List
6 intersect_eq/3, % +List1, +List2, -Intersection
7 list_difference_eq/3, % +List, -Subtract, -Rest
8 take/3, % +N, +List, -FirstElements
9 drop/3, % +N, +List, -LastElements
10 max_go_list/2, % +List, -Max
11 or_list/2, % +ListOfInts, -BitwiseOr
12 sublist/2, % ?Sublist, +List
13 bounded_sublist/3, % ?Sublist, +List, +Bound
14 min_list/2,
15 chr_delete/3,
16 init_store/2,
17 get_store/2,
18 update_store/2,
19 make_get_store_goal/3,
20 make_update_store_goal/3,
21 make_init_store_goal/3,
23 empty_ds/1,
24 ds_to_list/2,
25 get_ds/3,
26 put_ds/4
28 ]).
29 :- use_module(library(lists)).
30 :- use_module(library(assoc)).
32 empty_ds(DS) :- empty_assoc(DS).
33 ds_to_list(DS,LIST) :- assoc_to_list(DS,LIST).
34 get_ds(A,B,C) :- get_assoc(A,B,C).
35 put_ds(A,B,C,D) :- put_assoc(A,B,C,D).
38 init_store(Name,Value) :- nb_setval(Name,Value).
40 get_store(Name,Value) :- nb_getval(Name,Value).
42 update_store(Name,Value) :- b_setval(Name,Value).
44 make_init_store_goal(Name,Value,Goal) :- Goal = nb_setval(Name,Value).
46 make_get_store_goal(Name,Value,Goal) :- Goal = nb_getval(Name,Value).
48 make_update_store_goal(Name,Value,Goal) :- Goal = b_setval(Name,Value).
51 /*******************************
52 * MORE LIST OPERATIONS *
53 *******************************/
55 % append(+ListOfLists, -List)
57 % Convert a one-level nested list into a flat one. E.g.
58 % append([[a,b], [c]], X) --> X = [a,b,c]. See also
59 % flatten/3.
61 append([],[]).
62 append([X|Xs],L) :-
63 append(X,T,L),
64 append(Xs,T).
67 % nth(?Index, ?List, ?Element)
69 % Same as nth1/3
71 nth(Index, List, Element) :-
72 nth1(Index, List, Element).
75 % substitute(+OldVal, +OldList, +NewVal, -NewList)
77 % Substitute OldVal by NewVal in OldList and unify the result
78 % with NewList. JW: Shouldn't this be called substitute_eq/4?
80 substitute(_, [], _, []) :- ! .
81 substitute(X, [U|Us], Y, [V|Vs]) :-
82 ( X == U
83 -> V = Y,
84 substitute(X, Us, Y, Vs)
85 ; V = U,
86 substitute(X, Us, Y, Vs)
89 % memberchk_eq(+Val, +List)
91 % Deterministic check of membership using == rather than
92 % unification.
94 memberchk_eq(X, [Y|Ys]) :-
95 ( X == Y
96 -> true
97 ; memberchk_eq(X, Ys)
101 % list_difference_eq(+List, -Subtract, -Rest)
103 % Delete all elements of Subtract from List and unify the result
104 % with Rest. Element comparision is done using ==/2.
106 list_difference_eq([],_,[]).
107 list_difference_eq([X|Xs],Ys,L) :-
108 ( memberchk_eq(X,Ys)
109 -> list_difference_eq(Xs,Ys,L)
110 ; L = [X|T],
111 list_difference_eq(Xs,Ys,T)
114 % intersect_eq(+List1, +List2, -Intersection)
116 % Determine the intersection of two lists without unifying values.
118 intersect_eq([], _, []).
119 intersect_eq([X|Xs], Ys, L) :-
120 ( memberchk_eq(X, Ys)
121 -> L = [X|T],
122 intersect_eq(Xs, Ys, T)
123 ; intersect_eq(Xs, Ys, L)
127 % take(+N, +List, -FirstElements)
129 % Take the first N elements from List and unify this with
130 % FirstElements. The definition is based on the GNU-Prolog lists
131 % library. Implementation by Jan Wielemaker.
133 take(0, _, []) :- !.
134 take(N, [H|TA], [H|TB]) :-
135 N > 0,
136 N2 is N - 1,
137 take(N2, TA, TB).
139 % Drop the first N elements from List and unify the remainder with
140 % LastElements.
142 drop(0,LastElements,LastElements) :- !.
143 drop(N,[_|Tail],LastElements) :-
144 N > 0,
145 N1 is N - 1,
146 drop(N1,Tail,LastElements).
148 % max_go_list(+List, -Max)
150 % Return the maximum of List in the standard order of terms.
152 max_go_list([H|T], Max) :-
153 max_go_list(T, H, Max).
155 max_go_list([], Max, Max).
156 max_go_list([H|T], X, Max) :-
157 ( H @=< X
158 -> max_go_list(T, X, Max)
159 ; max_go_list(T, H, Max)
162 % or_list(+ListOfInts, -BitwiseOr)
164 % Do a bitwise disjuction over all integer members of ListOfInts.
166 or_list(L, Or) :-
167 or_list(L, 0, Or).
169 or_list([], Or, Or).
170 or_list([H|T], Or0, Or) :-
171 Or1 is H \/ Or0,
172 or_list(T, Or1, Or).
175 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
176 sublist(L, L).
177 sublist(Sub, [H|T]) :-
178 '$sublist1'(T, H, Sub).
180 '$sublist1'(Sub, _, Sub).
181 '$sublist1'([H|T], _, Sub) :-
182 '$sublist1'(T, H, Sub).
183 '$sublist1'([H|T], X, [X|Sub]) :-
184 '$sublist1'(T, H, Sub).
186 bounded_sublist(Sublist,_,_) :-
187 Sublist = [].
188 bounded_sublist(Sublist,[H|List],Bound) :-
189 Bound > 0,
191 Sublist = [H|Rest],
192 NBound is Bound - 1,
193 bounded_sublist(Rest,List,NBound)
195 bounded_sublist(Sublist,List,Bound)
199 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
200 min_list([H|T], Min) :-
201 '$min_list1'(T, H, Min).
203 '$min_list1'([], Min, Min).
204 '$min_list1'([H|T], X, Min) :-
205 ( H>=X ->
206 '$min_list1'(T, X, Min)
207 ; '$min_list1'(T, H, Min)
210 chr_delete([], _, []).
211 chr_delete([H|T], X, L) :-
212 ( H==X ->
213 chr_delete(T, X, L)
214 ; L=[H|RT],
215 chr_delete(T, X, RT)