This file is indexed.

/usr/lib/swi-prolog/boot/dwim.pl is in swi-prolog-nox 6.6.4-2ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
/*  $Id$

    Part of SWI-Prolog

    Author:        Jan Wielemaker
    E-mail:        J.Wielemaker@uva.nl
    WWW:           http://www.swi-prolog.org
    Copyright (C): 1985-2009, University of Amsterdam

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

    As a special exception, if you link this library with other files,
    compiled with a Free Software compiler, to produce an executable, this
    library does not by itself cause the resulting executable to be covered
    by the GNU General Public License. This exception does not however
    invalidate any other reasons why the executable file might be covered by
    the GNU General Public License.
*/

:- module('$dwim',
	[ dwim_predicate/2,
	  '$dwim_correct_goal'/3,
	  '$find_predicate'/2,
	  '$similar_module'/2
	]).

:- meta_predicate
	dwim_predicate(:, -),
	'$dwim_correct_goal'(:, +, -),
	'$similar_module'(:, -),
	'$find_predicate'(:, -).

%%	'$dwim_correct_goal'(:Goal, +Bindings, -Corrected)
%
%	Correct a goal (normally typed by the   user)  in the `Do What I
%	Mean' sense. Ask the user to confirm  if a unique correction can
%	be found.
%
%	@error	existence_error(procedure, PI) if the goal cannot be
%		corrected.

'$dwim_correct_goal'(M:Goal, Bindings, Corrected) :-
	correct_goal(Goal, M, Bindings, Corrected).

correct_goal(Goal, M, _, M:Goal) :-
	var(Goal), !.
correct_goal(Module:Goal, _, _, Module:Goal) :-
	(   var(Module)
	;   var(Goal)
	), !.
correct_goal(Vars^Goal0, M, Bindings, Vars^Goal) :- !, % setof/bagof
	correct_goal(Goal0, M, Bindings, Goal).
correct_goal(Module:Goal0, _, Bindings, Module:Goal) :-
	current_predicate(_, Module:Goal0), !,
	correct_meta_arguments(Goal0, Module, Bindings, Goal).
correct_goal(Goal0, M, Bindings, M:Goal) :-	% is defined
	current_predicate(_, M:Goal0), !,
	correct_meta_arguments(Goal0, M, Bindings, Goal).
correct_goal(Goal0, M, Bindings, Goal) :-	% correct the goal
	dwim_predicate_list(M:Goal0, DWIMs0), !,
	principal_predicates(DWIMs0, M, DWIMs),
	correct_literal(M:Goal0, Bindings, DWIMs, Goal1),
	correct_meta_arguments(Goal1, M, Bindings, Goal).
correct_goal(Goal, Module, _, NewGoal) :-	% try to autoload
	\+ current_prolog_flag(Module:unknown, fail),
	callable(Goal), !,
	functor(Goal, Name, Arity),
	'$undefined_procedure'(Module, Name, Arity, Action),
	(   Action == error
	->  existence_error(Module:Name/Arity),
	    NewGoal = fail
	;   Action == retry
	->  NewGoal = Goal
	;   NewGoal = fail
	).
correct_goal(Goal, M, _, M:Goal).

existence_error(PredSpec) :-
	strip_module(PredSpec, M, _),
	current_prolog_flag(M:unknown, Unknown),
	dwim_existence_error(Unknown, PredSpec).

dwim_existence_error(fail, _) :- !.
dwim_existence_error(Unknown, PredSpec) :-
	'$module'(TypeIn, TypeIn),
	unqualify_if_context(TypeIn, PredSpec, Spec),
	(   no_context(Spec)
	->  true
	;   Context = context(toplevel, 'DWIM could not correct goal')
	),
	Error = error(existence_error(procedure, Spec), Context),
	(   Unknown == error
	->  throw(Error)
	;   print_message(warning, Error)
	).

%%	no_context(+PI) is semidet.
%
%	True if we should omit the DWIM message because messages.pl
%	gives an additional explanation.

no_context((^)/2).
no_context((:-)/2).
no_context((:-)/1).
no_context((?-)/1).


%%	correct_meta_arguments(:Goal, +Module, +Bindings, -Final) is det.
%
%	Correct possible meta-arguments. This performs a recursive check
%	on meta-arguments specified as `0' using :- meta_predicate/1. As
%	a special exception, the arment of call/1 is not checked, so you
%	can use call(X) as an escape from the DWIM system.

correct_meta_arguments(call(Goal), _, _, call(Goal)) :- !.
correct_meta_arguments(Goal0, M, Bindings, Goal) :-
	predicate_property(M:Goal0, meta_predicate(MHead)), !,
	functor(Goal0, Name, Arity),
	functor(Goal, Name, Arity),
	correct_margs(0, Arity, MHead, Goal0, Goal, M, Bindings).
correct_meta_arguments(Goal, _, _, Goal).

correct_margs(Arity, Arity, _, _, _, _, _) :- !.
correct_margs(A, Arity, MHead, GoalIn, GoalOut, M, Bindings) :-
	I is A+1,
	arg(I, GoalIn, Ain),
	arg(I, GoalOut, AOut),
	(   arg(I, MHead, 0)
	->  correct_goal(Ain, M, Bindings, AOut0),
	    unqualify_if_context(M, AOut0, AOut)
	;   AOut = Ain
	),
	correct_margs(I, Arity, MHead, GoalIn, GoalOut, M, Bindings).


%%	correct_literal(:Goal, +Bindings, +DWIMs, -Corrected) is semidet.
%
%	Correct a single literal.  DWIMs is a list of heads that can
%	replace the head in Goal.

correct_literal(Goal, Bindings, [Dwim], DwimGoal) :-
	strip_module(Goal, CM, G1),
	strip_module(Dwim, DM, G2),
	functor(G1, _, Arity),
	functor(G2, Name, Arity), !,	% same arity: we can replace arguments
	G1 =.. [_|Arguments],
	G2 =.. [Name|Arguments],
	(   (   current_predicate(CM:Name/Arity)
	    ->	ConfirmGoal = G2,
		DwimGoal = CM:G2
	    ;	'$prefix_module'(DM, CM, G2, ConfirmGoal),
		DwimGoal = ConfirmGoal
	    ),
	    goal_name(ConfirmGoal, Bindings, String),
	    '$confirm'(dwim_correct(String))
	->  true
	;   DwimGoal = Goal
	).
correct_literal(Goal, Bindings, Dwims, NewGoal) :-
	strip_module(Goal, _, G1),
	functor(G1, _, Arity),
	include_arity(Dwims, Arity, [Dwim]), !,
	correct_literal(Goal, Bindings, [Dwim], NewGoal).
correct_literal(Goal, _, Dwims, _) :-
	print_message(error, dwim_undefined(Goal, Dwims)),
	fail.

include_arity([], _, []).
include_arity([H|T0], Arity, [H|T]) :-
	strip_module(H, _, G),
	functor(G, _, Arity), !,
	include_arity(T0, Arity, T).
include_arity([_|T0], Arity, T)	:-
	include_arity(T0, Arity, T).


%	goal_name(+Goal, +Bindings, -Name)
%
%	Transform Goal into a readable format by binding its variables.

goal_name(Goal, Bindings, String) :-
	State = s(_),
	(   bind_vars(Bindings),
	    numbervars(Goal, 0, _, [singletons(true), attvar(skip)]),
	    format(string(S), '~q', [Goal]),
	    nb_setarg(1, State, S),
	    fail
	;   arg(1, State, String)
	).

bind_vars([]).
bind_vars([Name=Var|T]) :-
	Var = '$VAR'(Name),		% portray prints Name
	bind_vars(T).


%%	'$find_predicate'(:Spec, -PIs:list(pi)) is det.
%
%	Unify `List' with a list of  predicate indicators that match the
%	specification `Spec'. `Spec' is a   term Name/Arity, a ``Head'',
%	or just an atom. The latter refers to all predicate of that name
%	with arbitrary arity. `Do What I   Mean'  correction is done. If
%	the requested module is `user' predicates residing in any module
%	will be considered matching.
%
%	@error	existence_error(procedure, Spec) if no matching predicate
%		can be found.

'$find_predicate'(M:S, List) :-
	name_arity(S, Name, Arity),
	'$module'(TypeIn, TypeIn),
	(   M == TypeIn			% I.e. unspecified default module
	->  true
	;   Module = M
	),
	find_predicate(Module, Name, Arity, L0), !,
	sort(L0, L1),
	principal_pis(L1, Module, List).
'$find_predicate'(_:S, List) :-
	name_arity(S, Name, Arity),
	findall(Name/Arity,
		'$in_library'(Name, Arity, _Path), List),
	List \== [], !.
'$find_predicate'(Spec, _) :-
	existence_error(Spec).

find_predicate(Module, Name, Arity, VList) :-
	findall(Head, find_predicate_(Module, Name, Arity, Head), VList),
	VList \== [], !.
find_predicate(Module, Name, Arity, Pack) :-
	findall(PI, find_sim_pred(Module, Name, Arity, PI), List),
	pack(List, Module, Arity, Packs),
	'$member'(Dwim-Pack, Packs),
	'$confirm'(dwim_correct(Dwim)), !.

unqualify_if_context(_, X, X) :-
	var(X), !.
unqualify_if_context(C, C2:X, X) :-
	C == C2, !.
unqualify_if_context(_, X, X) :- !.

%%	pack(+PIs, +Module, +Arity, +Context, -Packs)
%
%	Pack the list of heads into packets, consisting of the corrected
%	specification and a list of heads satisfying this specification.

pack([], _, _, []) :- !.
pack([M:T|Rest], Module, Arity, [Name-[M:T|R]|Packs]) :-
	pack_name(M:T, Module, Arity, Name),
	pack_(Module, Arity, Name, Rest, R, NewRest),
	pack(NewRest, Module, Arity, Packs).

pack_(Module, Arity, Name, List, [H|R], Rest) :-
	'$select'(M:PI, List, R0),
	pack_name(M:PI, Module, Arity, Name), !,
	'$prefix_module'(M, C, PI, H),
	pack_(Module, Arity, Name, C, R0, R, Rest).
pack_(_, _, _, _, Rest, [], Rest).

pack_name(_:Name/_, M, A,   Name) :-
	var(M), var(A), !.
pack_name(M:Name/_, _, A, M:Name) :-
	var(A), !.
pack_name(_:PI, M, _, PI)   :-
	var(M), !.
pack_name(QPI, _, _, QPI).


find_predicate_(Module, Name, Arity, Module:Name/Arity) :-
	current_module(Module),
	current_predicate(Name, Module:Term),
	functor(Term, Name, Arity).

find_sim_pred(M, Name, Arity, Module:DName/Arity) :-
	sim_module(M, Module),
	'$dwim_predicate'(Module:Name, Term),
	functor(Term, DName, DArity),
	sim_arity(Arity, DArity).

sim_module(M, Module) :-
	var(M), !,
	current_module(Module).
sim_module(M, M) :-
	current_module(M), !.
sim_module(M, Module) :-
	current_module(Module),
	dwim_match(M, Module).

sim_arity(A, _) :- var(A), !.
sim_arity(A, D) :- abs(A-D) < 2.

%%	name_arity(+Spec, -Name, -Arity)
%
%	Obtain the name and arity of a predicate specification. Warn if
%	this is not a legal specification.

name_arity(Atom, Atom, _) :-
	atom(Atom), !.
name_arity(Name/Arity, Name, Arity) :- !.
name_arity(Name//DCGArity, Name, Arity) :-
	(   var(DCGArity)
	->  true
	;   Arity is DCGArity+2
	).
name_arity(Term, Name, Arity) :-
	callable(Term), !,
	functor(Term, Name, Arity).
name_arity(Spec, _, _) :-
	throw(error(type_error(predicate_indicator, Spec), _)).


principal_pis(PIS, M, Principals) :-
	map_pi_heads(PIS, Heads),
	principal_predicates(Heads, M, Heads2),
	map_pi_heads(Principals, Heads2).

map_pi_heads([], []) :- !.
map_pi_heads([PI0|T0], [H0|T]) :-
	map_pi_head(PI0, H0),
	map_pi_heads(T0, T).

map_pi_head(M:PI, M:Head) :-
	nonvar(M), !,
	map_pi_head(PI, Head).
map_pi_head(Name/Arity, Term) :-
	functor(Term, Name, Arity).

%%	principal_predicates(:Heads, +Context, -Principals)
%
%	Get the principal predicate list from a list of heads (e.g., the
%	module in which the predicate is defined).

principal_predicates(Heads, M, Principals) :-
	find_definitions(Heads, M, Heads2),
	'$list_to_set'(Heads2, Principals).

find_definitions([], _, []).
find_definitions([H0|T0], M, [H|T]) :-
	find_definition(H0, M, H),
	find_definitions(T0, M, T).

find_definition(Head, _, Def) :-
	strip_module(Head, _, Plain),
	callable(Plain),
	(   predicate_property(Head, imported_from(Module))
	->  (   predicate_property(system:Plain, imported_from(Module))
	    ->	Def = system:Plain
	    ;	Def = Module:Plain
	    )
	;   Def = Head
	).


%%	dwim_predicate(:Head, -NewHead) is nondet.
%
%	Find a head that is in a `Do What I Mean' sence the same as `Head'.
%	backtracking produces more such predicates.  If searches for:
%
%	    * predicates with a similar name in an import module
%	    * predicates in a similar module with the same name
%	    * predicates in any module with the same name

dwim_predicate(Head, DWIM) :-
	dwim_predicate_list(Head, DWIMs),
	'$member'(DWIM, DWIMs).

dwim_predicate_list(Head, [Head]) :-
	current_predicate(_, Head), !.
dwim_predicate_list(M:Head, DWIMs) :-
	setof(DWIM, dwim_pred(M:Head, DWIM), DWIMs), !.
dwim_predicate_list(Head, DWIMs) :-
	setof(DWIM, '$similar_module'(Head, DWIM), DWIMs), !.
dwim_predicate_list(_:Goal, DWIMs) :-
	setof(Module:Goal,
	      current_predicate(_, Module:Goal),
	      DWIMs).

%%	dwim_pred(:Head, -DWIM) is nondet.
%
%	True if DWIM is a predicate with a similar name than Head in the
%	module of Head or an import module thereof.

dwim_pred(Head, M:Dwim) :-
	strip_module(Head, Module, H),
	default_module(Module, M),
	'$dwim_predicate'(M:H, Dwim).

%%	'$similar_module'(:Goal, -DWIMGoal) is nondet.
%
%	True if DWIMGoal exists and is, except from a typo in the
%	module specification, equivalent to Goal.

'$similar_module'(Module:Goal, DwimModule:Goal) :-
	current_module(DwimModule),
	dwim_match(Module, DwimModule),
	current_predicate(_, DwimModule:Goal).