YAP 7.1.0
modules.yap
Go to the documentation of this file.
1/*************************************************************************
2* *
3* YAP Prolog *
4* *
5* Yap Prolog was developed at NCCUP - Universidade do Porto *
6* *
7* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
8* *
9**************************************************************************
10* *
11* File: modules.pl *
12* Last rev: *
13* mods: *
14* comments: module support *
15* *
16*************************************************************************/
17
18
19/**
20@file modules.yap
21
22 @defgroup ModuleBuiltins Module Support
23
24 @ingroup YAPModules
25 @{
26
27 **/
28:- system_module( '$_modules', [abolish_module/1,
33 expand_goal/2,
34 export/1,
35 export_list/2,
36 export_resource/1,
38 ls_imports/0,
45 use_module/3], ['$add_to_imports'/3,
46 '$clean_cuts'/2,
47 '$convert_for_export'/6,
48 '$do_import'/3,
49 '$extend_exports'/3,
50 '$get_undefined_pred'/4,
51 '$imported_predicate'/4,
52 '$meta_expand'/6,
53 '$meta_predicate'/2,
54 '$meta_predicate'/4,
55 '$module'/3,
56 '$module'/4,
57 '$module_expansion'/6,
58 '$module_transparent'/2,
59 '$module_transparent'/4]).
60
61
62
63:- '$c_built_in'/3use_system_module( '$_arith', []).
64
65:- '$lf_opt'/3'$load_files'/3use_system_module( '$_consult', [,
66 ]).
67
68:- '$skipeol'/1use_system_module( '$_debug', []).
69
70:- '$do_error'/2use_system_module( '$_errors', []).
71
72:- '$full_clause_optimisation'/4use_system_module( '$_eval', []).
73
74:- multifile '$system_module'/1.
75
76
77:- '$purge_clauses'(module(_,_), prolog).
78:- '$purge_clauses'('$module'(_,_), prolog).
79:- '$purge_clauses'(use_module(_), prolog).
80:- '$purge_clauses'(use_module(_,_), prolog).
81%
82% start using default definition of module.
83%
84
85/**
86 @pred use_module( +Files ) is directive
87 @brief load a module file
88
89This predicate loads the file specified by _Files_, importing all
90their public predicates into the current type-in module. It is
91implemented as if by:
92
93```
94use_module(F) :-
95 load_files(F, [if(not_loaded),must_be_module(true)]).
96```
97
98Notice that _Files_ may be a single file, or a list with a number
99files. The _Files_ are loaded in YAP only once, even if they have been
100updated meanwhile. YAP should also verify whether the files actually
101define modules. Please consult load_files/3 for other options when
102loading a file.
103
104Predicate name clashes between two different modules may arise, either
105when trying to import predicates that are also defined in the current
106type-in module, or by trying to import the same predicate from two
107different modules.
108
109In the first case, the local predicate is considered to have priority
110and use_module/1 simply gives a warning. As an example, if the file
111`a.pl` contains:
112
113```
114:- module( a, [a/1] ).
115
116:- use_module(b).
117
118a(1).
119a(X) :- b(X).
120```
121
122and the file `b.pl` contains:
123
124```
125:- module( b, [a/1,b/1] ).
126
127a(2).
128
129b(1).
130```
131
132YAP will execute as follows:
133
134
135```
136?- [a].
137 % consulting .../a.pl...
138 % consulting .../b.pl...
139 % consulted .../b.pl in module b, 0 msec 0 bytes
140 % consulted .../a.pl in module a, 1 msec 0 bytes
141true.
142 ?- a(X).
143X = 1 ? ;
144X = 1.
145```
146
147The example shows that the query `a(X)`has a single answer, the one
148defined in `a.pl`. Calls to `a(X)`succeed in the top-level, because
149the module `a` was loaded into `user`. On the other hand, `b(X)`is not
150exported by `a.pl`, and is not available to calls, although it can be
151accessed as a predicate in the module 'a' by using the `:` operator.
152
153Next, consider the three files `c.pl`, `d1.pl`, and `d2.pl`:
154
155```
156% c.pl
157:- module( c, [a/1] ).
158
159:- use_module([d1, d2]).
160
161a(X) :-
162 b(X).
163a(X) :-
164 c(X).
165a(X) :-
166 d(X).
167
168% d1.pl
169:- module( d1, [b/1,c/1] ).
170
171vvb(2).
172c(3).
173
174
175% d2.pl
176:- module( d2, [b/1,d/1] ).
177
178b(1).
179d(4).
180
181```
182
183The result is as follows:
184
185```
186./yap -l c
187YAP 6.3.4 (x86_64-darwin13.3.0): Tue Jul 15 10:42:11 CDT 2014
188
189 ERROR!!
190 at line 3 in o/d2.pl,
191 PERMISSION ERROR- loading .../c.pl: modules d1 and d2 both define b/1
192 ?- a(X).
193X = 2 ? ;
194 ERROR!!
195 EXISTENCE ERROR- procedure c/1 is undefined, called from context prolog:$user_call/2
196 Goal was c:c(_131290)
197```
198
199The state of the module system after this error is undefined.
200
201
202**/
203use_module(F) :- load_files(F,[if(not_loaded),must_be_module(true)]).
204
205
206
207/**
208 \pred use_module(+Files, +Imports)
209 loads a module file but only imports the named predicates
210
211
212This predicate loads the file specified by _Files_, importing their
213public predicates specified by _Imports_ into the current type-in
214module. It is implemented as if by:
215
216```
217use_module(Files, Imports) :-
218 load_files(Files, [if(not_loaded),must_be_module(true),imports(Imports)]).
219```
220
221The _Imports_ argument may be use to specify which predicates one
222wants to load. It can also be used to give the predicates a different name. As an example,
223the graphs library is implemented on top of the red-black trees library, and some predicates are just aliases:
224
225```
226:- use_module(library(rbtrees), [
227 rb_min/3 as min_assoc,
228 rb_max/3 as max_assoc,
229
230 ...]).
231```
232
233Unfortunately it is still not possible to change argument order.
234
235**/
236use_module(F,Is) :-
237 load_files(F, [if(not_loaded),must_be_module(true),imports(Is)] ).
238
239'$module'(O,N,P,Opts) :- '$module',
240 '$module'(O,N,P),
241 '$process_module_decls_options'(Opts,module(Opts,N,P)).
242
243
244'$process_module_decls_options'(Var,Mod) :-
245 var(Var), var,
246 '$do_error'(instantiation_error,Mod).
247'$process_module_decls_options'([],_) :- '$process_module_decls_options'.
248'$process_module_decls_options'([H|L],M) :- '$process_module_decls_options',
249 '$process_module_decls_option'(H,M),
250 '$process_module_decls_options'(L,M).
251'$process_module_decls_options'(T,M) :-
252 '$do_error'(type_error(list,T),M).
253
254'$process_module_decls_option'(Var,M) :-
255 var(Var),
256 '$do_error'(instantiation_error,M).
257'$process_module_decls_option'(At,M) :-
258 atom(At), atom,
259 use_module(M:At).
260'$process_module_decls_option'(library(L),M) :- '$process_module_decls_option',
261 use_module(M:library(L)).
262'$process_module_decls_option'(hidden(Bool),M) :- '$process_module_decls_option',
263 '$process_hidden_module'(Bool, M).
264'$process_module_decls_option'(Opt,M) :-
265 '$do_error'(domain_error(module_decl_options,Opt),M).
266
267'$process_hidden_module'(TNew,M) :-
268 '$convert_true_off_mod3'(TNew, New, M),
269 source_mode(Old, New),
270 '$prepare_restore_hidden'(Old,New).
271
272'$convert_true_off_mod3'(true, off, _) :- '$convert_true_off_mod3'.
273'$convert_true_off_mod3'(false, on, _) :- '$convert_true_off_mod3'.
274'$convert_true_off_mod3'(X, _, M) :-
275 '$do_error'(domain_error(module_decl_options,hidden(X)),M).
276
277'$prepare_restore_hidden'(Old,Old) :- '$prepare_restore_hidden'.
278'$prepare_restore_hidden'(Old,New) :-
279 recorda('$system_initialization', source_mode(New,Old), _).
280
281
282'$extend_exports'(HostF, Exports, DonorF ) :-
283 ( recorded('$module','$module'( DonorF, DonorM, _,DonorExports, _),_) -> recorded ; DonorF = recorded ),
284 ( recorded('$module','$module'( HostF, HostM, SourceF, AllExports, Line),R) -> erase(R) ; HostF = erase,AllExports=[] ),
285 '$convert_for_export'(Exports, DonorExports, DonorM, HostM, _TranslationTab, AllReExports),
286 '$convert_for_export':append( AllReExports, AllExports, Everything0 ),
287 '$sort'( Everything0, Everything ),
288 ( source_location(_, Line,_) -> source_location ; Line = 0 ),
289 recorda('$module','$module'(HostF,HostM,SourceF, Everything, Line),_).
290
291'$module_produced by'(M, M0, N, K) :-
292 recorded('$import','$import'(M,M0,_,_,N,K),_), recorded.
293'$module_produced by'(M, M0, N, K) :-
294 recorded('$import','$import'(MI,M0,G1,_,N,K),_),
295 functor(G1, N1, K1),
296 '$module_produced by'(M,MI,N1,K1).
297
298% prevent modules within the kernel module...
299/** @pred use_module(? _M_,? _F_,+ _L_)
300
301 SICStus directive for loading a module, it can operate in two
302different ways:
303
3041. If the module _M_ is given, import the procedures in _L_ from _M_
305to the current source module.
306
3072. Otherwise, operate as use_module/2, and load the files
308specified by _F_, importing the predicates specified in the list _L_.
309*/
310
311use_module(M,F,Is) :-
312 '$yap_strip_module'(F,M1,F1),
313 var(F1),
314 var,
315 ignore(M=M1),
316 '$use_module'(M,M1,Is).
317use_module(_M,F,Is) :-
318 use_module(F,Is).
319
320
321'$use_module'(M, M1, Is) :-
322 recorded('$module','$module'(F,M,_,_,_),_),
323 recorded,
324 load_files(M1:F, [if(not_loaded),must_be_module(true),imports(Is)]).
325'$use_module'(M, M1, Is) :-
326 nonvar(M),
327 nonvar,
328 load_files(M1:M, [if(not_loaded),must_be_module(true),imports(Is)]).
329'$use_module'(M, F, Is) :-
330 '$do_error'(error(instantiation_error, use_module(M,F,Is))).
331
332
333/** @pred current_module( ? Mod:atom) is nondet
334
335
336Succeeds if _M_ is a user-visible modules. A module is defined as soon as some
337predicate defined in the module is loaded, as soon as a goal in the
338module is called, or as soon as it becomes the current type-in module.
339
340
341*/
342current_module(Mod) :-
343 '$all_current_modules'(Mod),
344 \+ '$hidden_atom'(Mod).
345
346/** @pred current_module( ? Mod:atom, ? _F_ : file ) is nondet
347
348Succeeds if _M_ is a module associated with the file _F_, that is, if _File_ is the source for _M_. If _M_ is not declared in a file, _F_ unifies with `user`.
349 */
350current_module(Mod,TFN) :-
351 ( atom(Mod) -> atom ; '$all_current_modules'(Mod) ),
352 ( recorded('$module','$module'(TFN,Mod,_,_Publics, _),_) -> recorded ; TFN = recorded ).
353
354system_module(Mod) :-
355 ( atom(Mod) -> atom ; '$all_current_modules'(Mod) ),
356 '$is_system_module'(Mod).
357
358'$trace_module'(X) :-
359 open('P0:debug', append, S),
360 fornat(S, '~w~n', [X]),
361 close(S).
362'$trace_module'(_).
363
364'$trace_module'(X,Y) :- X==Y, '$trace_module'.
365'$trace_module'(X,Y) :-
366 telling(F),
367 tell('~/.dbg.modules'),
368 write('***************'), write,
371 tell(F),tell.
372'$trace_module'(_,_).
373
374/**
375 *
376 * @pred '$continue_imported'(+ModIn, +ModOut, +PredIn ,+PredOut)
377 *
378 * @return
379 */
380'$continue_imported'(Mod,Mod,Pred,Pred) :-
381 '$pred_exists'(Pred, Mod),
382 '$pred_exists'.
383'$continue_imported'(FM,Mod,FPred,Pred) :-
384 recorded('$import','$import'(IM,Mod,IPred,Pred,_,_),_),
385 '$continue_imported'(FM, IM, FPred, IPred), '$continue_imported'.
386'$continue_imported'(FM,Mod,FPred,Pred) :-
387 '$continue_imported':'$parent_module'(Mod,IM),
388 '$continue_imported'(FM, IM, FPred, Pred).
389
390
391/**
392be associated to a new file.
393
394\param[in] _Module_ is the name of the module to declare
395\param[in] _MSuper_ is the name of the context module. Use `prolog`or `system`
396 if you do not need a context.
397\param[in] _File_ is the canonical name of the file from which the modulvvvvve is loaded
398\param[in] Line is the line-number of the :- module/2 directive.
399\param[in] If _Redefine_ `true`, allow associating the module to a new file
400*/
401
402'$declare_module'(Name, _Super, Context, _File, _Line) :-
403 addi_mport_module(Name, Context, start).
404
405/**
406 @pred abolish_module( + Mod) is det
407 get rid of a module and of all predicates included in the module.
408*/
409abolish_module(Mod) :-
410 recorded('$module','$module'(_,Mod,_,_,_),R), erase(R),
411 erase.
412abolish_module(Mod) :-
413 recorded('$import','$import'(Mod,_,_,_,_,_),R), erase(R),
414 erase.
415abolish_module(Mod) :-
416 '$current_predicate'(Na,Mod,S,_),
417 functor(S, Na, Ar),
418 abolish(Mod:Na/Ar),
419 abolish.
421
422export(Resource) :-
423 var(Resource),
424 '$do_error'(instantiation_error,export(Resource)).
425export([]) :- export.
426export([Resource| Resources]) :- export,
427 export_resource(Resource),
428 export(Resources).
429export(Resource) :-
430 export_resource(Resource).
431
432export_resource(Resource) :-
433 var(Resource), var,
434 '$do_error'(instantiation_error,export(Resource)).
435export_resource(P) :-
436 P = F/N, atom(F), number(N), N >= 0, number,
437 '$current_module'(Mod),
438 (
439 recorded('$module','$module'(File,Mod,SourceF,ExportedPreds,Line),R)
440 ->
441 (
442 lists:member(P,ExportedPreds)
443 ->
444 member
445 ;
446 erase(R),
447 recorda('$module','$module'(File,Mod,SourceF,[P|ExportedPreds],Line),_)
448 )
449 ;
450 (
451 prolog_load_context(file, File)
452 ->
453 recorda('$module','$module'(File,Mod,SourceF,[P],Line),_)
454 ;
455 recorda('$module','$module'(user_input,Mod,user_input,[P],1),_)
456 )
457 ).
458export_resource(P0) :-
459 P0 = F//N, atom(F), number(N), N >= 0, number,
460 N1 is N+2, P = F/N1,
461 export_resource(P).
462export_resource(op(Prio,Assoc,Name)) :-
463 '$current_module'(Mod),
464 op(Prio,Assoc,Mod:Name),
465 prolog_load_context(file, File),
466 (
467 recorded('$module','$module'(File,Mod,SourceF,ExportedPreds,Line),R)
468 ->
469 (
470 lists:delete(ExportedPreds, op(OldPrio, Assoc, Name), Rem)
471 ->
472 (
473 OldPrio == Prio
474 ->
475 Update = false
476 ;
477 Update = true
478 )
479 ;
480 Update = , Rem = ExportedPreds
481 ),
482 Update == true,
483 erase(R)
484 ),
485 !,
486 (
487 Update ==
488 ->
489 recorda('$module','$module'(File,Mod,SourceF,[op(Prio,Assoc,Name)|Rem],Line ),_)
490 ;
491 nonvar(File)
492 ->
493 recorda('$module','$module'(File,Mod,SourceF,[op(Prio,Assoc,Name)],Line),_)
494 ;
495 recorda('$module','$module'(user_input,Mod,user_input,[op(Prio,Assoc,Name)],1),_)
496 ).
497export_resource(Resource) :-
498 '$do_error'(type_error(predicate_indicator,Resource),export(Resource)).
499
500export_list(Module, List) :-
501 recorded('$module','$module'(_,Module,_,List,_),_).
502
503
504'$add_to_imports'([], _, _).
505% no need to import from the actual module
506'$add_to_imports'([T|_Tab], Module, ContextModule) :-
507 '$do_import'(T, Module, ContextModule),
508 '$do_import'.
509'$add_to_imports'([_T|Tab], Module, ContextModule) :-
510 '$add_to_imports'(Tab, Module, ContextModule).
511
512'$do_import'(op(Prio,Assoc,Name), Mod, ContextMod) :-
513 '$do_import',
514 op(Prio,Assoc,ContextMod:Name),
515 op(Prio,Assoc,Mod:Name),
516 op.
517'$do_import'( N0/K-N1/K, M0, M1) :-
518 M0\=M1,
519 once('$check_import'(M1,M0,N1,K)),
520 functor(G0,N0,K),
521 G0=..[N0|Args],
522 G1=..[N1|Args],
523 recordaifnot('$import','$import'(M0,M1,G0,G1,N1,K),_),
524 %writeLn((M1:G1 :- M0:G0)),
525 current_prolog_flag(source, YFlag),
526 set_prolog_flag(source, false),
527 asserta_static(M1:(G1 :- M0:G0)),
528 set_prolog_flag(source, YFlag),
529 '$proxy_predicate'(G1,M1),
530 '$proxy_predicate'.
531
532'$follow_import_chain'(prolog,G,prolog,G) :- '$follow_import_chain'.
533'$follow_import_chain'(ImportingM,G,M0,G0) :-
534 recorded('$import','$import'(ExportingM1,ImportingM,G1,G,_,_),_), recorded,
535 '$follow_import_chain'(ExportingM1,G1,M0,G0).
536'$follow_import_chain'(M,G,M,G).
537
538'$import_chain'(prolog,_G,_,_) :- '$import_chain'.
539'$import_chain'(M,_G,M,_G).
540'$import_chain'(ImportingM,G,M0,G0) :-
541 recorded('$import','$import'(ExportingM1,ImportingM,G1,G,_,_),_),
542 '$import_chain'(ExportingM1,G1,M0,G0).
543
544
545% trying to import Mod:N/K into ContextM
546'$check_import'(prolog, _ContextM, _N, _K) :-
547 '$check_import',
548 '$check_import'.
549'$check_import'(M, M, _N, _K) :-
550 '$check_import',
551 '$check_import'.
552'$check_import'(_, _, N, K) :-
553 system_predicate(N/K),
554 system_predicate,
555 system_predicate.
556'$check_import'(M0, M1, N, K) :-
557 recorded('$import','$import'(M2, M1, _, _, N,K),_R),
558 recorded,
559 (M2 == M0
560 ->
561 '$redefine_import'( M2, M1, M0, N/K)
562 ;
563 '$check_import'(M0, M2, N, K)
564 ).
565'$check_import'(_,_,_,_).
566
567'$redefine_import'( M1, M2, Mod, ContextM, N/K) :-
568 '__NB_getval__'('$lf_status', TOpts, fail),
569 '$lf_opt'(redefine_module, TOpts, Action), '$lf_opt',
570 '$redefine_action'(Action, M1, M2, Mod, ContextM, N/K).
571'$redefine_import'( M1, M2, Mod, ContextM, N/K) :-
572 '$redefine_action'(false, M1, M2, Mod, ContextM, N/K).
573
574
575'$redefine_action'(ask, M1, M2, M, _, N/K) :-
576 stream_property(user_input,tty(true)), stream_property,
577 format(user_error,'NAME CLASH: ~w was already imported to module ~w;~n',[M1:N/K,M2]),
578 format(user_error,' Do you want to import it from ~w ? [y, n, e or h] ',M),
579 '$mod_scan'(C),
580 ( C == e -> halt(1) ;
581 C == halt ).
582'$redefine_action'(true, M1, _, _, _, _) :- '$redefine_action',
583 recorded('$module','$module'(F, M1, _, _MyExports,_Line),_),
584 unload_file(F).
585'$redefine_action'(false, M1, M2, _M, ContextM, N/K) :-
586 recorded('$module','$module'(F, ContextM, _, _MyExports,_Line),_),
587 '$current_module'(_, M2),
588 '$do_error'(permission_error(import,M1:N/K,redefined,M2),F).
589
590'$mod_scan'(C) :-
591 get_char(C),
592 '$skipeol'(C),
593 (C == y -> true; C == n).
594
595/**
596 @pred set_base_module( +Expor
597tingModule ) is det
598 @brief All
599predicates exported from _ExportingModule_ are automatically available to the
600other source modules.
601
602This built-in was introduced by SWI-Prolog. In YAP, by default, modules only
603inherit from `prolog`. This extension allows predicates in the current
604module (see module/2 and module/1) to inherit from `user` or other modules.
605
606*/
607set_base_module(ExportingModule) :-
608 var(ExportingModule),
609 '$do_error'(instantiation_error,set_base_module(ExportingModule)).
610set_base_module(ExportingModule) :-
611 atom(ExportingModule), atom,
612 '$current_module'(Mod),
613 retractall(prolog:'$parent_module'(Mod,_)),
614 asserta(prolog:'$parent_module'(Mod,ExportingModule)).
615set_base_module(ExportingModule) :-
616 '$do_error'(type_error(atom,ExportingModule),set_base_module(ExportingModule)).
617
618/**
619 * @pred import_module( +ImportingModule, +ExportingModule ) is det
620 * All exported predicates from _ExportingModule_
621 * are automatically available to the
622 * source module _ImportModule_.
623
624This innovation was introduced by SWI-Prolog. By default, modules only
625inherit from `prolog` and `user`. This extension allows predicates in
626any module to inherit from `user` and other modules.
627
628*/
629import_module(Mod, ImportModule) :-
630 var(Mod),
631 '$do_error'(instantiation_error,import_module(Mod, ImportModule)).
632import_module(Mod, ImportModule) :-
633 atom(Mod), atom,
634 atom:'$parent_module'(Mod,ImportModule).
635import_module(Mod, EM) :-
636 '$do_error'(type_error(atom,Mod),import_module(Mod, EM)).
637
638
639/**
640 @pred add_import_module( + _Module_, + _ImportModule_ , +_Pos_) is det
641Add all exports in _ImportModule_ as available to _Module_.
642
643
644All exported predicates from _ExportModule_ are made available to the
645 source module _ImportModule_. If _Position_ is bound to `start` the
646 module _ImportModule_ is tried first, if _Position_ is bound to `end`,
647 the module is consulted last.
648
649*/
650add_import_module(Mod, ImportModule, Pos) :-
651 var(Mod),
652 '$do_error'(instantiation_error,add_import_module(Mod, ImportModule, Pos)).
653add_import_module(Mod, ImportModule, Pos) :-
654 var(Pos),
655 '$do_error'(instantiation_error,add_import_module(Mod, ImportModule, Pos)).
656add_import_module(Mod, ImportModule, start) :-
657 atom(Mod), atom,
658 retractall(prolog:'$parent_module'(Mod,ImportModule)),
659 asserta(prolog:'$parent_module'(Mod,ImportModule)).
660add_import_module(Mod, ImportModule, end) :-
661 atom(Mod), atom,
662 retractall(prolog:'$parent_module'(Mod,ImportModule)),
663 assertz(prolog:'$parent_module'(Mod,ImportModule)).
664add_import_module(Mod, ImportModule, Pos) :-
665 \+ atom(Mod), atom,
666 '$do_error'(type_error(atom,Mod),add_import_module(Mod, ImportModule, Pos)).
667add_import_module(Mod, ImportModule, Pos) :-
668 '$do_error'(domain_error(start_end,Pos),add_import_module(Mod, ImportModule, Pos)).
669
670/**
671 @pred delete_import_module( + _ExportModule_, + _ImportModule_ ) is det
672Exports in _ImportModule_ are no longer available to _Module_.
673
674
675All exported predicates from _ExportModule_ are discarded from the
676 ones used vy the source module _ImportModule_.
677
678*/
679delete_import_module(Mod, ImportModule) :-
680 var(Mod),
681 '$do_error'(instantiation_error,delete_import_module(Mod, ImportModule)).
682delete_import_module(Mod, ImportModule) :-
683 var(ImportModule),
684 '$do_error'(instantiation_error,delete_import_module(Mod, ImportModule)).
685delete_import_module(Mod, ImportModule) :-
686 atom(Mod),
687 atom(ImportModule), atom,
688 retractall(prolog:'$parent_module'(Mod,ImportModule)).
689delete_import_module(Mod, ImportModule) :-
690 \+ atom(Mod), atom,
691 '$do_error'(type_error(atom,Mod),delete_import_module(Mod, ImportModule)).
692delete_import_module(Mod, ImportModule) :-
693 '$do_error'(type_error(atom,ImportModule),delete_import_module(Mod, ImportModule)).
694
695'$set_source_module'(Source0, SourceF) :-
696 prolog_load_context(module, Source0), prolog_load_context,
697 module(SourceF).
698'$set_source_module'(Source0, SourceF) :-
699 current_module(Source0, SourceF).
700
701/**
702 @pred module_property( +Module, ? _Property_ ) is nondet
703
704Enumerate non-deterministically the main properties of _Module_ .
705
706Reports the following properties of _Module_:
707
708 + `class`( ?_Class_ ): whether it is a `system`, `library`, or `user` module.
709
710 + `line_count`(?_Ls_): number of lines in source file (if there is one).
711
712 + `file`(?_F_): source file for _Module_ (if there is one).
713
714 + `exports`(-Es): list of all predicate symbols and
715 operator symbols exported or re-exported by this module.
716
717*/
718module_property(Mod, Prop) :-
719 var(Mod),
720 var,
721 recorded('$module','$module'(_,Mod,_,_Es,_),_),
722 module_property(Mod, Prop).
723module_property(Mod, class(L)) :-
724 '$module_class'(Mod, L).
725module_property(Mod, line_count(L)) :-
726 recorded('$module','$module'(_F,Mod,_,_,L),_).
727module_property(Mod, file(F)) :-
728 recorded('$module','$module'(F,Mod,_,_,_),_).
729module_property(Mod, exports(Es)) :-
730 (
731 recorded('$module','$module'(_,Mod,_,Es,_),_)
732 ->
733 recorded
734 ;
735 Mod==recorded
736 ->
737 findall( P, (current_predicate(user:P)), Es)
738 ;
739 Mod==findall
740 ->
741 findall( N/A, (predicate_property(Mod:P0, public),functor(P0,N,A)), Es)
742 ).
743
744'$module_class'( Mod, system) :- '$is_system_module'( Mod ), '$is_system_module'.
745'$module_class'( Mod, library) :- '$library_module'( Mod ), '$library_module'.
746'$module_class'(_Mod, user) :- '$module_class'.
747'$module_class'( _, temporary) :- '$module_class'.
748'$module_class'( _, test) :- '$module_class'.
749'$module_class'( _, development) :- '$module_class'.
750
751'$library_module'(M1) :-
752 recorded('$module','$module'(_, M1, library(_), _MyExports,_Line),_).
753
754recorded :-
755 recorded('$import','$import'(M0,M,G0,G,_N,_K),_R),
756 numbervars(G0+G, 0, _),
757 format('~a:~w <- ~a:~w~n', [M, G, M0, G0]),
758 format.
759format.
760
761unload_module(Mod) :-
762 recorded('$mf', meta_predicate(Mod,_P), _, R),
763 erase(R),
764 erase.
765unload_module(Mod) :-
766 recorded('$multifile_defs','$defined'(_FileName,_Name,_Arity,Mod), R),
767 erase(R),
768 erase.
769unload_module(Mod) :-
770 recorded( '$foreign', Mod:_Foreign, R),
771 erase(R),
772 erase.
773% remove imported modules
774unload_module(Mod) :-
775 setof( M, recorded('$import',_G0^_G^_N^_K^_R^'$import'(Mod,M,_G0,_G,_N,_K),_R), Ms),
776 recorded('$module','$module'( _, Mod, _, _, Exports), _),
777 recorded:member(M, Ms),
778 current_op(X, Y, M:Op),
779 current_op:member( op(X, Y, Op), Exports ),
780 op(X, 0, M:Op),
781 op.
782unload_module(Mod) :-
783 recorded('$module','$module'( _, Mod, _, _, Exports), _),
784 recorded:member( op(X, _Y, Op), Exports ),
785 op(X, 0, Mod:Op),
786 op.
787unload_module(Mod) :-
788 current_predicate(Mod:P),
789 abolish(P),
790 abolish.
791unload_module(Mod) :-
792 recorded('$import','$import'(Mod,_M,_G0,_G,_N,_K),R),
793 erase(R),
794 erase.
795unload_module(Mod) :-
796 recorded('$module','$module'( _, Mod, _, _, _), R),
797 erase(R),
798 erase.
799
800/* debug */
801moderase :-
802 recorded('$module','$module'(HostF,HostM,SourceF, Everything, Line),_),
803 \+ system_module(HostM),
804 format('%%%%%%~n ~a,~n%% at ~w,~n%% loaded at ~a:~d,~n%% Exporlist ~w.~nImports~n:', [HostM,SourceF, HostF, Line, Everything]),
805 (
806 recorded('$import','$import'(M,HostM,_G,_GO,N,K),_R),
807 format('% ~w:~a/~d:.~n',[M,N,K])
808 ;
809 format('%%%%%%~nExports~n:', []),
810 recorded('$import','$import'(HostM,M,_G,_GO,N,K),_R),
811 format('% ~w:~a/~d:.~n',[M,N,K])
812 ),
813 fail.
814module_state.
815
816:- recorda('$module','$module'(user,user,user,[],1),_).
817
818%% @}
819
abolish(+ PredSpec)
asserta_static(: C)
close(+ S)
current_op( P, T, F)
current_predicate( F )
module(+M)
op(+ P,+ T,+ A)
open(+ F,+ M,- S)
recordaifnot(+ K, T,- R)
start input for a meta-clause
source_module(-Mod)
stream_property( Stream, Prop )
Definition: top.yap:2
system_module( + Mod)
system_predicate( ?_P_ )
tell(+ S)
Definition: edio.yap:70
telling(- S)
get_char(- C)
asserta(+ C)
assertz(+ C)
retractall(+ G)
format(+ T, :L)
erase(+ R)
portray_clause(+ C)
abolish_module( + Mod)
add_import_module( + Module, + ImportModule , +_Pos_)
current_module( ? Mod:atom)
current_module( ? Mod:atom, ? F : file )
delete_import_module( + ExportModule, + ImportModule )
import_module( +ImportingModule, +ExportingModule )
module_property( +Module, ? Property )
set_base_module( +Expor tingModule )
Definition: modules.yap:527
use_module( +Files )
use_module(+Files, +Imports)
use_module(? M,? F,+ L)
findall( T,+ G,- L)
Definition: setof.yap:70
setof( X,+ P,- B)
halt(+ I)
ignore(: Goal)
once( 0:G)
current_prolog_flag(? Flag,- Value)
set_prolog_flag(+ Flag,+ Value)
source_mode(- O,+ N)
load_files(+_Files_, +_Options_)
prolog_load_context(? Key, ? Value)
Definition: consult.yap:479
numbervars( t,+ _N1,- Nn)
atom( T)
functor( T, F, N)
nonvar( T)
number( T)
var( T)
append(? List1,? List2,? List3)
delete(+ List, ? Element, ? Residue)
member(?Element, ?Set) is true when Set is a list, and Element occurs in it