summaryrefslogtreecommitdiff
path: root/stdlib.pl
blob: 5c35fbcb2c06dc7c75d74b59e825933c5bd55baf (plain) (blame)
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
:- module(system, []).

% Logic and control predicates
\+ Goal :- call(Goal), !, fail.
\+ Goal.

once(Goal) :-
	call(Goal),
	!.

repeat :- true ; repeat.

% Control structures.

If -> Then :-
	If, !, Then.

If -> Then ; _ :- 
	If, !, Then.

_ -> _ ; Else :-
	!, Else.

If ; _ :-
	If.

_ ; Else :-
	Else.

A , B :- A , B.

% Term unification
A = A.

A \= B :- 
	\+ A = B.

% Comparison of terms using the standard order

A == B :-
	compare(=, A, B).

A \== B :-
	\+ A == B.

A @< B :-
	compare(<, A, B).

A @=< B :-
	A == B.
A @=< B :-
	A @< B.

A @> B :-
	compare(>, A, B).

A @>= B :-
	A == B.
A @>= B :-
	A @> B.

% Input output

open(SourceSink, Mode, Stream) :-
	open(SourceSink, Mode, Stream, []).

close(StreamOrAlias) :-
	close(StreamOrAlias, []).

% Standard exceptions

instantiation_error :-
	throw(error(instantiation_error, _)).

type_error(ValidType, Culprit) :-
	throw(error(type_error(ValidType, Culprit), _)).

domain_error(ValidDomain, Culprit) :-
	throw(error(domain_error(ValidDomain, Culprit), _)).

existence_error(ObjectType, Culprit) :-
	throw(error(existence_error(ObjectType, Culprit), _)).

permission_error(Operation, PermissionType, Culprit) :-
	throw(error(permission_error(Operation, PermissionType, Culprit), _)).

representation_error(Flag) :-
	throw(error(representation_error(Flag), _)).

evaluation_error(Error) :-
	throw(error(evaluation_error(Error), _)).

resource_error(Resource) :-
	throw(error(resource_error(Resource), _)).

syntax_error(Error) :-
	throw(error(syntax_error(Error), _)).

% Input and output

read_term(Term, Options) :-
	current_input(S),
	read_term(S, Term, Options).

read(Term) :-
	current_input(S),
	read_term(S, Term, []).

write_term(Term, Options) :-
	current_output(S),
	write_term(S, Term, Options).

write(Term) :-
	current_output(S),
	write_term(S, Term, [numbervars(true)]).

writeq(Term) :-
	current_output(S),
	write_term(S, Term, [quoted(true), numbervars(true)]).

writeq(S, Term) :-
	write_term(S, Term, [quoted(true), numbervars(true)]).

write_canonical(Term) :-
	current_output(S),
	write_term(S, Term, [quoted(true), ignore_ops(true)]).

write_canonical(S, Term) :-
	write_term(S, Term, [quoted(true), ignore_ops(true)]).

% Arithmetic comparisons defined in terms of >=. This is not the most effective way,
% but it is fine for now.

E1 =:= E2 :-
	E1 >= E2,
	E2 >= E1.

E1 =\= E2 :-
	\+ E1 =:= E2.

E1 < E2 :-
	E2 >= E1,
	E1 =\= E2.

E1 =< E2 :-
	E2 >= E1.

E1 > E2 :-
	E2 < E1.


% Clause retrieval and information and removal

clause(Head, Body) :-
	clause(Head, Body, Clauses),
	member(clause(Head, Body), Clauses).

current_predicate(PI) :-
	current_predicate(PI, Predicates),
	member(PI, Predicates).

retract(Clause) :-
	copy_term(Clause, ClauseCopy),
	retract_one(ClauseCopy),
	( Clause = ClauseCopy
	; retract(Clause)
	).

% Basic list predicates

member(X, [X|_]).
member(X, [_|Tail]) :-
	member(X, Tail).

% Additional type tests

callable(T) :- atom(T) ; compound(T).

list([]).
list([_|T]) :- list(T).

partial_list(T) :- var(T).
partial_list([_|T]) :- partial_list(T).

% type assertions (throws an error if false)

is_callable(T) :- callable(T), ! ; type_error(callable, T).

is_nonvar(T) :- nonvar(T), ! ; instantiation_error.

is_list_or_partial_list(T) :- (list(T) ; partial_list(T)), ! ; type_error(list, T).

% All solutions

findall(Template, Goal, Instances) :-
	is_nonvar(Goal),
	is_callable(Goal),
	is_list_or_partial_list(Instances),
	system:asserta('find all'([])),
	call(Goal),
	system:asserta('find all'(solution(Template))),
	fail.
findall(Template, Goal, Instances) :-
	findall_collect([], Instances).

findall_collect(Acc, Instances) :-
	retract('find all'(Item)),
	!,
	findall_collect(Item, Acc, Instances).
findall_collect([], Instances, Instances).
findall_collect(solution(T), Acc, Instances) :-
	findall_collect([T|Acc], Instances).