Skip to content

Commit

Permalink
generalize multifile/dynamic/discontiguous declarations over lists of…
Browse files Browse the repository at this point in the history
… predicate indicators (#1586)
  • Loading branch information
mthom committed Sep 21, 2023
1 parent b3239ab commit c26e943
Showing 1 changed file with 29 additions and 33 deletions.
62 changes: 29 additions & 33 deletions src/loader.pl
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,29 @@
; domain_error(module_specifier, Module, use_module/2)
).

:- meta_predicate add_predicate_declaration(3, ?).

add_predicate_declaration(Handler, Name/Arity) :-
must_be(atom, Name),
must_be(integer, Arity),
prolog_load_context(module, Module),
call(Handler, Module, Name, Arity).
add_predicate_declaration(Handler, Module:Name/Arity) :-
must_be(atom, Module),
must_be(atom, Name),
must_be(integer, Arity),
call(Handler, Module, Name, Arity).
add_predicate_declaration(Handler, [PI|PIs]) :-

This comment has been minimized.

Copy link
@triska

triska Sep 22, 2023

Contributor

I think add_predicate_declaration(_, []). is missing?

This comment has been minimized.

Copy link
@UWN

UWN Sep 22, 2023

Note that predicate indicator lists do not include the empty list.

This comment has been minimized.

Copy link
@triska

triska Sep 22, 2023

Contributor

Ah yes, thank you! I was confused by GNU Prolog which incorrectly accepts [].

maplist(loader:add_predicate_declaration(Handler), [PI|PIs]).

add_dynamic_predicate(Evacuable, Module, Name, Arity) :-
'$add_dynamic_predicate'(Module, Name, Arity, Evacuable).

add_multifile_predicate(Evacuable, Module, Name, Arity) :-
'$add_multifile_predicate'(Module, Name, Arity, Evacuable).

add_discontiguous_predicate(Evacuable, Module, Name, Arity) :-
'$add_discontiguous_predicate'(Module, Name, Arity, Evacuable).

compile_declaration(use_module(Module), Evacuable) :-
use_module(Module, [], Evacuable).
Expand All @@ -392,39 +415,12 @@
'$declare_module'(Module, Exports, Evacuable)
; type_error(atom, Module, load/1)
).
compile_declaration(dynamic(Module:Name/Arity), Evacuable) :-
!,
must_be(atom, Module),
must_be(atom, Name),
must_be(integer, Arity),
'$add_dynamic_predicate'(Module, Name, Arity, Evacuable).
compile_declaration(dynamic(Name/Arity), Evacuable) :-
must_be(atom, Name),
must_be(integer, Arity),
prolog_load_context(module, Module),
'$add_dynamic_predicate'(Module, Name, Arity, Evacuable).
compile_declaration(multifile(Module:Name/Arity), Evacuable) :-
!,
must_be(atom, Module),
must_be(atom, Name),
must_be(integer, Arity),
'$add_multifile_predicate'(Module, Name, Arity, Evacuable).
compile_declaration(multifile(Name/Arity), Evacuable) :-
must_be(atom, Name),
must_be(integer, Arity),
prolog_load_context(module, Module),
'$add_multifile_predicate'(Module, Name, Arity, Evacuable).
compile_declaration(discontiguous(Module:Name/Arity), Evacuable) :-
!,
must_be(atom, Module),
must_be(atom, Name),
must_be(integer, Arity),
'$add_discontiguous_predicate'(Module, Name, Arity, Evacuable).
compile_declaration(discontiguous(Name/Arity), Evacuable) :-
must_be(atom, Name),
must_be(integer, Arity),
prolog_load_context(module, Module),
'$add_discontiguous_predicate'(Module, Name, Arity, Evacuable).
compile_declaration(dynamic(PIs), Evacuable) :-
add_predicate_declaration(loader:add_dynamic_predicate(Evacuable), PIs).
compile_declaration(multifile(PIs), Evacuable) :-
add_predicate_declaration(loader:add_multifile_predicate(Evacuable), PIs).
compile_declaration(discontiguous(PIs), Evacuable) :-
add_predicate_declaration(loader:add_discontiguous_predicate(Evacuable), PIs).
compile_declaration(initialization(Goal), Evacuable) :-
prolog_load_context(module, Module),
assertz(Module:'$initialization_goals'(Goal)).
Expand Down

0 comments on commit c26e943

Please sign in to comment.