How do I print dynamic/1 in Prolog - prolog

I want to print these all names under each dynamic/1. For example I want to print all names of male under one query for print all males in Prolog.

The easiest way to do this in swi-prolog (and also in gnu-prolog) is to use predicate listing/1:
?- listing(male/1).
:- dynamic male/1.
male(saad).
male(sohaib).
male(salman).
...
true.
If you want more control over display formatting, you can define your own predicate as:
list_facts(Name/Arity) :-
functor(Head, Name, Arity),
forall( clause(Head, true),
format('~w.\n', [Head]) ). % Adjust formatting here!
Example:
?- list_facts(female/1).
female(rida).
female(florida).
female(yasmeen).
...
true.
If you want to show just the names, try:
?- forall(female(Name), format('~w\n', [Name])).
rida
florida
yasmeen
...
true.
REMARK The swi-prolog built-in predicate forall/2is defined as:
forall(Cond, Action) :-
\+ (Cond, \+ Action).

The supplied code works with SWI-Prolog, don't know if this works with other Prolog systems but the basis of the code should be portable.
This is from actual code I use daily so it more production quality code than simple answer code but the basis is to use functor/3 to get a head and then use the head in forall/2 to get the rules (rule/2) and format/3 to output each rule. Normally a rule is a head and body but since this is outputting facts only the heads are output. To convert the rules to individual values =../2 is used. This uses library(options). This also uses current_predicate/1 to check if the predicate exists to avoid an exception if it does not. functor/3 could probably be used but somewhere I recall a corner case that it did now work. The rest should not need explaining.
I only entered 3 of the male facts. Would have used all of them if they were typed as text into the question as then they could have been easily copied.
If the facts are in a different module remember to change the name of the module when calling list_facts/N.
:- module(examples,
[
list_facts/1,
list_facts/2,
list_facts/3
]).
:- dynamic female/1.
:- dynamic male/1.
male(saad).
male(sohaib).
male(salman).
list_facts(Module:Name/Arity) :-
list_facts(user_output,Module:Name/Arity,[]).
list_facts(Output_stream,Module:Name/Arity) :-
list_facts(Output_stream,Module:Name/Arity,[]).
list_facts(Output_stream,Module:Name/Arity,Options) :-
(
current_predicate(Module:Name/Arity)
->
(
option(left_margin_size(Left_margin_size),Options)
->
atomic_list_concat(['~|~',Left_margin_size,'+~|~w'],Format)
;
Format = '~w'
),
(
option(title(Title),Options)
->
format(Output_stream,Format,[Title]),
format(Output_stream,'~n',[])
;
true
),
functor(Head,Name,Arity),
forall(
rule(Module:Head,Rule),
list_fact(Output_stream,Format,Rule)
)
;
format(Output_stream,'No facts found: ~w:~w/~w.~n',[Module,Name,Arity])
).
list_fact(Output_stream,Format,Fact) :-
Fact =.. Fact_values,
list_values(Output_stream,Format,false,Fact_values).
% Recursive case - functor name
list_values(Output_stream,Format,false,[_Value|Values]) :-
!,
list_values(Output_stream,Format,true,Values).
% Recursive case - fact arguments
list_values(Output_stream,Format,true,[Value|Values]) :-
!,
format(Output_stream,Format,[Value]),
list_values(Output_stream,Format,true,Values).
% Base case
list_values(Output_stream,_Format,_Functor_done,[]) :-
format(Output_stream,'~n',[]).
Example usage.
?- working_directory(_,'C:/Users/Groot').
true.
?- [examples].
true.
?- list_facts(examples:male/1).
saad
sohaib
salman
true.
?- list_facts(user_output,examples:male/1,[left_margin_size(3),title('male/1')]).
male/1
saad
sohaib
salman
true.

Related

Different order of interpretation depending on the context?

I have the following rules to convert a structure.
cmap(predicate(_,Verb,named(N)),[S1,S2]) :-
next_uid(NewVar),
S1 =.. [named,N,NewVar],
S2 =.. [Verb,NewVar].
cmap(predicate(_,Verb,Subj),S) :-
S =.. [Verb,Subj].
the problem is that one rule needs clauses to be in one order and there is another rule that needs clauses to be in another order, as below :
cmap(predicate(_,Verb,Subj),S) :-
S =.. [Verb,Subj].
cmap(predicate(_,Verb,named(N)),[S1,S2]) :-
next_uid(NewVar),
S1 =.. [named,N,NewVar],
S2 =.. [Verb,NewVar].
how can I do that ?
The problem fields are : named(N) <=> Subj
The reason I need different order is that case1(fact) I have value already assigned, in case2(question) I need the variable to be unbound, so that it can bind it later.
PS> if it make any difference the rule-call is two steps removed i.e. via another two rules.
here the exact usage . the diff is one does skolemization the other dont.
The question variables have to stay un-unified/free, so they can be used as a query.
In addition named(X), have to extracted as its own separate fact when it is in the structure /thats DRS weirdnes i have no control over/.
% provides/unifies ID for every Reference variable
skolem([]).
skolem([H|T]) :- next_uid(H), skolem(T).
convert([],[]).
% use the map to convert any Item
convert(Item-_,R) :- cmap(Item,R).
%processing Questions
convert(drs([], [question(drs(_Refs,Conds))]), Res) :- convert(Conds, Res).
%DRS processing
convert(drs(Refs,Conds),Res) :- skolem(Refs), convert(Conds,Res).
% process list of items
convert([H|T],[RH|RT]) :- convert(H,RH), convert(T,RT).
may be something along the lines of :
cmap(predicate(_,Verb,named(N)),[named(N,NewVar),S2]) :- next_uid(NewVar), cmap(predicate(_,Verb,?X?),S2).
You want to distinguish the cases by explicitly verifying whether the 3rd argument of predicate/3 is bound ("is a variable", to perform the standard language abuse). This is all very outside of first-order positive logic:
cmap(predicate(_,Verb,NN),[S1,S2]) :-
nonvar(NN),
!,
cmap_nonvar(predicate(_,Verb,NN),[S1,S2]).
Symmetrically (one can actually get rid of the var(NN),! as the test and the subsequent committement to the clause have already happened, but I like symmetry if it is not expensive:
cmap(predicate(_,Verb,NN),[S1,S2]) :-
var(NN),
!,
cmap_var(predicate(_,Verb,NN),[S1,S2]).
Modifications: Here we can get rid of one use of "univ", =../2 (don't use "univ" if you don't need it). Also, it's strange and unbalanced that the second argument is a list in one case and a non-list in the other case. Why not use a list in both cases?
"nonvar" case
Possible problem: for cmap_nonvar the second clause executes if the 3rd argument of predicate/3 does not unify with named(N) as well as if it unifies with named(N). Is that really wanted?
% if the 3rd argument of predicate/3 unifies with named(N)
cmap_nonvar(predicate(_,Verb,named(N)),[named(N,NewVar),S]) :-
next_uid(NewVar),
S =.. [Verb,NewVar].
cmap_nonvar(predicate(_,Verb,Subj),[S]) :-
S =.. [Verb,Subj].
"var" case
Possible problem: I suppose you do not want the second clause here, which is always executed as a second case with Subj unbound as Subj can be unified with named(N).
cmap_var(predicate(_,Verb,Subj),S) :-
S =.. [Verb,Subj].
cmap_var(predicate(_,Verb,named(N)),[named(N,NewVar),S]) :-
next_uid(NewVar),
S =.. [Verb,NewVar].
Trick: tag the argument
"tagging" means enclosing a term into another term to be able to write Prolog code more idiomatically.
In this case, one can tag NN:
tag(X,var(X)) :- var(X),!.
tag(X,nonvar(X)) :- nonvar(X),!.
cmap(predicate(_,Verb,NN),[S1,S2]) :-
tag(NN,TaggedNN),
cmap_tag_aware(predicate(_,Verb,TaggedNN),[S1,S2]).
cmap can then match on the particular form of the passed term:
cmap_tag_aware(predicate(_,Verb,nonvar(named(N))),[named(N,NewVar),S]) :-
next_uid(NewVar),
S =.. [Verb,NewVar].
cmap_tag_aware(predicate(_,Verb,nonvar(Subj)),[S]) :-
S =.. [Verb,Subj].
cmap_tag_aware(predicate(_,Verb,var(Subj)),S) :-
S =.. [Verb,Subj].
cmap_tag_aware(predicate(_,Verb,var(named(N))),[named(N,NewVar),S]) :-
next_uid(NewVar),
S =.. [Verb,NewVar].

I have defined multiple predicates that seem to share a common form

All of these predicates are defined in pretty much the same way. The base case is defined for the empty list. For non-empty lists we unify in the head of the clause when a certain predicate holds, but do not unify if that predicate does not hold. These predicates look too similar for me to think it is a coincidence. Is there a name for this, or a defined abstraction?
intersect([],_,[]).
intersect(_,[],[]).
intersect([X|Xs],Ys,[X|Acc]) :-
member(X,Ys),
intersect(Xs,Ys,Acc).
intersect([X|Xs],Ys,Acc) :-
\+ member(X,Ys),
intersect(Xs,Ys,Acc).
without_duplicates([],[]).
without_duplicates([X|Xs],[X|Acc]) :-
\+ member(X,Acc),
without_duplicates(Xs,Acc).
without_duplicates([X|Xs],Acc) :-
member(X,Acc),
without_duplicates(Xs,Acc).
difference([],_,[]).
difference([X|Xs],Ys,[X|Acc]) :-
\+ member(X,Ys),
difference(Xs,Ys,Acc).
difference([X|Xs],Ys,Acc) :-
member(X,Ys),
difference(Xs,Ys,Acc).
delete(_,[],[]).
delete(E,[X|Xs],[X|Ans]) :-
E \= X,
delete(E,Xs,Ans).
delete(E,[X|Xs],Ans) :-
E = X,
delete(E,Xs,Ans).
There is an abstraction for "keep elements in list for which condition holds".
The names are inclide, exclude. There is a library for those in SWI-Prolog that you can use or copy. Your predicates intersect/3, difference/3, and delete/3 would look like this:
:- use_module(library(apply)).
intersect(L1, L2, L) :-
include(member_in(L1), L2, L).
difference(L1, L2, L) :-
exclude(member_in(L2), L1, L).
member_in(List, Member) :-
memberchk(Member, List).
delete(E, L1, L) :-
exclude(=(E), L1, L).
But please take a look at the implementation of include/3 and exclude/3, here:
https://www.swi-prolog.org/pldoc/doc/_SWI_/library/apply.pl?show=src#include/3
Also in SWI-Prolog, in another library, there are versions of those predicates called intersection/3, subtract/3, delete/3:
https://www.swi-prolog.org/pldoc/doc/_SWI_/library/lists.pl?show=src#intersection/3
https://www.swi-prolog.org/pldoc/doc/_SWI_/library/lists.pl?show=src#subtract/3
https://www.swi-prolog.org/pldoc/doc_for?object=delete/3
Those are similar in spirit to your solutions.
Your next predicate, without_duplicates, cannot be re-written like that with include/3 or exclude/3. Your implementation doesn't work, either. Try even something easy, like:
?- without_duplicates([a,b], L).
What happens?
But yeah, it is not the same as the others. To implement it correctly, depending on whether you need the original order or not.
If you don't need to keep the initial order, you can simply sort; this removes duplicates. Like this:
?- sort(List_with_duplicates, No_duplicates).
If you want to keep the original order, you need to pass the accumulated list to the recursive call.
without_duplicates([], []).
without_duplicates([H|T], [H|Result]) :-
without_duplicates_1(T, [H], Result).
without_duplicates_1([], _, []).
without_duplicates_1([H|T], Seen0, Result) :-
( memberchk(H, Seen0)
-> Seen = Seen0 , Result = Result0
; Seen = [H|Seen0], Result = [H|Result0]
),
without_duplicates_1(T, Seen, Result0).
You could get rid of one argument if you use a DCG:
without_duplicates([], []).
without_duplicates([H|T], [H|No_duplicates]) :-
phrase(no_dups(T, [H]), No_duplicates).
no_dups([], _) --> [].
no_dups([H|T], Seen) -->
{ memberchk(H, Seen) },
!,
no_dups(T, Seen).
no_dups([H|T], Seen) -->
[H],
no_dups(T, [H|Seen]).
Well, these are the "while loops" of Prolog on the one hand, and the inductive definitions of mathematical logic on the other hand (See also: Logic Programming, Functional Programming, and Inductive Definitions, Lawrence C. Paulson, Andrew W. Smith, 2001), so it's not surprising to find them multiple times in a program - syntactically similar, with slight deviations.
In this case, you just have a binary decision - whether something is the case or not - and you "branch" (or rather, decide to not fail the body and press on with the selected clause) on that. The "guard" (the test which supplements the head unification), in this case member(X,Ys) or \+ member(X,Ys) is a binary decision (it also is exhaustive, i.e. covers the whole space of possible X)
intersect([X|Xs],Ys,[X|Acc]) :- % if the head could unify with the goal
member(X,Ys), % then additionally check that ("guard")
(...action...). % and then do something
intersect([X|Xs],Ys,Acc) :- % if the head could unify with the goal
\+ member(X,Ys), % then additionally check that ("guard")
(...action...). % and then do something
Other applications may need the equivalent of a multiple-decision switch statement here, and so N>2 clauses may have to be written instead of 2.
foo(X) :-
member(X,Set1),
(...action...).
foo(X) :-
member(X,Set2),
(...action...).
foo(X) :-
member(X,Set3),
(...action...).
% inefficient pseudocode for the case where Set1, Set2, Set3
% do not cover the whole range of X. Such a predicate may or
% may not be necessary; the default behaviour would be "failure"
% of foo/1 if this clause does not exist:
foo(X) :-
\+ (member(X,Set1);member(X,Set2);member(X,Set3)),
(...action...).
Note:
Use memberchk/2 (which fails or succeeds-once) instead of member/2 (which fails or succeeds-and-then-tries-to-succeed-again-for-the-rest-of-the-set) to make the program deterministic in its decision whether member(X,L).
Similarly, "cut" after the clause guard to tell Prolog that if a guard of one clause succeeds, there is no point in trying the other clauses because they will all turn out false: member(X,Ys),!,...
Finally, use term comparison == and \== instead of unification = or unification failure \= for delete/3.

Expanding Prolog clauses without parameters

I am writing a program that transforms other programs by expanding predicates. I usually do this using clause/2, but it doesn't always expand a predicate if it has no parameters:
:- set_prolog_flag('double_quotes','chars').
:- initialization(main).
main :- clause(thing,C),writeln(C).
% this prints "true" instead of "A = 1"
thing :- A = 1.
Is it possible to expand predicates that have no parameters?
Some general remark: Note that this code is highly specific to SWI. In other systems which are ISO conforming you can only access definitions via clause/2, if that predicate happens to be dynamic.
For SWI, say listing. to see what is happening.
?- assert(( thing :- A = 1 )).
true.
?- listing(thing).
:- dynamic thing/0.
thing.
true.
?- assert(( thing :- p(A) = p(1) )).
true.
?- assert(( thing(X) :- Y = 2 )).
true.
?- listing(thing).
:- dynamic thing/0.
thing.
thing :-
p(_)=p(1).
:- dynamic thing/1.
thing(_).
true.
It all looks like some tiny source level optimization.

Variables for predicates in metagol

The following program noMetagolR is given in:
http://www.doc.ic.ac.uk/~shm/Papers/metagol_gram.pdf page 33.
parse(S,G1,G2) :- parse(s(0),S,[],G1,G2).
parse(Q,X,X,G1,G2) :- abduce(acceptor(Q),G1,G2).
parse(Q,[C|X],Y,G1,G2) :- Skolem(P), abduce(delta1(Q,C,P),G1,G3), parse(P,X,Y,G3,G2).
abduce(X,G,G) :- member(X,G).
abduce(X,G,[X|G]) :- not(member(X,G)).
Skolem(s(0)). Skolem(s(1)). ...
An example query is :
parse([],[],G1), parse([0],G1,G2), parse([0,0],G2,G3), parse([1,1],G3,G4), parse([0,0,0],G4,G5), parse([0,1,1],G5,G6), parse([1,0,1],G6,G),not(parse([1],G,G)), not(parse([0,1],G,G)).
The answer substitutions should return a learnt grammar for parity.
The program is said to run in Yap. I normally use SWI-prolog. Either way,
what do I do to make them understand Skolem/1 ? Presumbly this means that Skolem is a variable? I thought maybe using =.. but this does not work.
Also how many Skolem/1 facts are needed?
in SWI-Prolog, you can place in your source the directive
:- set_prolog_flag(allow_variable_name_as_functor,true).
see current_prolog_flag/2
example on REPL:
1 ?- set_prolog_flag(allow_variable_name_as_functor,true).
true.
2 ?- assert(X(1)).
true.
3 ?- X(Y).
Y = 1.

Is a predicate with variable arity acceptable in Prolog?

Is it possible to have a "variable arity predicate" in Prolog?
I mean something like this:
my_predicate( [a,b,c], [a,c], [a], [a,b,c,d], N, RESULT)
with the number of initial lists unknown at the beginning?
Using the univ operator ( =.. ) it would be possible to unify it with a list of terms and traversing it like every other list. But how to write the goal?
my_predicate(??) =.. [??]
I really don't know if this is even possible..
you can define predicates with different arities that have the same name but they will be different predicates.
foo(1).
foo(2,1).
?-foo(2).
false
my suggestion is to change the encoding; instead of a number of initial lists, have a list of initial lists.
the other solution would be to write predicates for all the possible numbers of arguments (or dynamically generate them).
As #thanosQR suggests, it probably is the best to change your representation to some list.
There are, however - very seldom but nevertheless - situations where you want to define a predicate for many different arities. In this very rare case, you can define such a predicate manually. That is, for each arity manually. Of course, you will only define several cases. As an example, see library(lambda).
You always can go up to one level :) Many years ago I saw implementation of ANSI prolog interpter in Turbo Prolog. Idea was very simple, enclose all user-space facts and rules in single fact backed by assert/retract-like operations.
Consider enclosing all your targets in another compose:
target(my_predicate( [a,b,c], [a,c], [a], [a,b,c,d], N, RESULT)) :- RESULT=[a], N=1.
target(H) :- H =.. [my_predicate|_].
target(using_my_predicate(X, Y)) :- target(my_predicate(X,1,Y)).
Some prologs (at least YAP) have directives to declare handlers for unknown targets:
:- module(sumtest).
target(sum(0)).
target(H) :-
H =.. [sum, S, X|XS],
H1 =.. [sum, S1|XS],
H1,
S is (S1+X).
target(sumtest:G):- target(G). % HACK: strip-off module
:- unknown(_, target(_)).
test:-
sum(X,1), write(X), nl,
sum(Y,2,3), write(Y), nl,
sum(Z,3,4,2), write(Z), nl,
target(sum(X1,1)), write(X1), nl,
target(sum(Y1,2,3)), write(Y1), nl,
target(sum(Z1,3,4,2)), write(Z1), nl.
:- test, halt.
% % yap -l sumtest.pl
% YAP 6.2.0 (amd64): Thu Oct 21 10:31:27 EEST 2010
% MYDDAS version MYDDAS-0.9.1
% 1
% 5
% 9
% 1
% 5
% 9
% % YAP execution halted

Resources