My Prolog version doesn't seem to know member - prolog

I'm trying out next example in Amzi! Prolog:
member(apple, [apple, broccoli, crackers]).
And the answer is no.
?- member(apple, [apple, broccoli, crackers]).
no
Is there a problem with my version? If I try the same in swipl, then it does work...
?- member(apple, [apple, broccoli, crackers]).
true .
Does Amzi! Prolog use an own implementation of Prolog?

The predicate member/2 is not specified in the official ISO standard, although is often provided by Prolog implementations. But the implementation differs from system to system. Some provide it as a built-in predicate. Others as a library predicate. In the specific case of SWI-Prolog, and assuming the default configuration, the member/2 predicate is defined in the module library(lists), which is auto-loaded when one of its exported predicates is called. Auto-loading is, by default, silent, which often leads users to think that they are calling a built-in predicate. In the case of Amzi! Prolog, it seems that the member/2 predicate is not built-in but you can easily define it yourself. In order to check if a given predicate is a built-in predicate, you can usually use the standard predicate_property/2 predicate. For example, using SWI-Prolog, you get for member/2:
?- predicate_property(member(_,_), Property).
Property = interpreted ;
Property = visible ;
Property = imported_from(lists) ;
Property = file('/Users/pmoura/lib/swipl-6.5.2/library/lists.pl') ;
Property = line_count(106) ;
Property = nodebug ;
Property = number_of_clauses(1) ;
Property = number_of_rules(1) ;
false.
But sadly not all systems provide the standard predicate_property/2 predicate. Prolog standardization is not, unfortunately, one of its shiny points.

Every version of Prolog I ever used was slightly different from the others... Nice to see there's now another one to consider, ISO Prolog :-)
I think this link has good information: http://osprey.unisa.ac.za/phorum/read.php?674,173265,173265

Related

Representing truth regarding beliefs in prolog

How to make this (or something similar) work in Prolog:
belief(john,red(apple)).
belief(peter,red(apple)).
X :- belief(john,X), belief(peter,X).
And get true. for the following query (while consulting above):-
?- red(apple).
First, it's useful to define a little helper to capture when all (relevant) persons believe something:
all_believe(Belief) :-
belief(john, Belief),
belief(peter, Belief).
Then you can define, for example:
red(Object) :-
all_believe(red(Object)).
green(Object) :-
all_believe(green(Object)).
And with your given set of beliefs you get:
?- red(apple).
true.
?- green(apple).
false.
This works. It requires you to define similar rules for any term that you want to use as a belief.
You can make this a bit shorter with macro definitions using term_expansion:
term_expansion(declare_belief(Belief),
Belief :- all_believe(Belief)).
This means that every top-level definition in your source code of the form declare_belief(Belief) should be treated as if you had written Belief :- all_believe(Belief) instead (with the variable Belief substituted appropriately).
So now you can just write this:
declare_belief(red(_)).
declare_belief(green(_)).
and it will be treated exactly like the longer definitions for red(Object) and red(Object) above. You will still have to write this kind of declaration for any term that you want to use as a possible belief.
Prolog does not allow the head of a rule to be just a variable. The head must be a nonvar term, whose functor (i.e., name and arity) identifies the predicate being defined. So, a possible solution would be something like this:
true_belief(X) :-
belief(john, X),
belief(peter, X).
belief(john, red(apple)).
belief(peter, red(apple)).
Examples:
?- true_belief(red(apple)).
true.
?- true_belief(X).
X = red(apple).

current_predicate in SICStus Prolog

SICStus Prolog offers both current_predicate/1 and current_predicate/2.
The manual page states:
current_predicate(?PredSpec)
Unifies PredSpec with a predicate specifications of the form Name/Arity.
current_predicate(?Name, ?Term)
Unifies Name with the name of a user-defined predicate, and Term with the most general term corresponding to that predicate.
They appear to have the same features:
both predicates work for enumerating predicates, both work with modules.
The manual page comments:
current_predicate/1 is part of the ISO Prolog standard; current_predicate/2 is not.
Should I ever use current_predicate/2 in new (= non-legacy) code?
Short answer, no. Don't use it in new code.
The benefit of current_predicate/2 was to allow querying, using a predicate call template, if the predicate is defined, unlike current_predicate/1. E.g.
...,
( current_predicate(_, foo(_, _)) ->
foo(A, B)
; ...
),
...
But you can often use instead the standard predicate_property/2 predicate, which takes a template as first argument.
P.S. The Logtalk linter will scream at you (and suggest a more standard alternative) if it finds you calling current_predicate/2 :-)

Is there a way to annotate the parameter and return value in Prolog?

parent(mel, joan).
parent(jane, betty).
parent(jane, tom).
parent(richard, adam).
parent(richard, rosa).
parent(joan, fran).
For example someone asks me to find all ancestors of a parent. I give him the code:
ancestor(P,C) :- parent(P, C).
ancestor(P,C) :- ancestor(P,P1), parent(P1, C).
But my friend still doesn't know how to use the predicate. Does he call it like
ancestor(richard, C) or ancestor(C, richard) ?
Is there a way to annotate that P is the parameter while C is the return value? And in a complex case, there will be predicates with different names, how should my user know which predicate is the final predicate he wants to use?
To help the human-readable meaning, you could add an extra predicate documenting the parameters as readable name/value pairs:
entry_ancestor_of(ancestor=P, descendent=C) :-
ancestor(P,C).
?- entry_ancestor_of(ancestor=richard, descendent=C).
C = adam .
Above, the suffix *ancestor_of* suggests param 1 is ancestor of param 2, so naming the predicate carefully can make it clearer.
Usually(convention), input parameters are the earlier parameters, and output parameters are later parameters, but where the predicate 'works both ways', ie. either could be input or output, this rule can't hold. This is the case for your predicate:
?- entry_ancestor_of(ancestor=X, descendent=adam).
X = richard .
Either parameter could be input or output, so there is no need to codify/explain them as such, although you might want to comment that it works both ways.
I would usually comment these 'flexible' predicates by putting an example of both of the above usages in a comment next to the predicate.
For entrypoint labelling, just do one or more of the following:
explicitly name the predicate as an entrypoint, as above
document using comments in the code which are the entrypoints
arrange the entrypoints in the same physical section with a comment
block saying that the predicates below are entrypoints.
Edit: Extra things re: coding guidelines / other answers.
In Coding guidelines for Prolog, section 3.8, it says 'For example, mother_of(A, B) is ambiguous;', so I gave bad advice on that.. perhaps acapelli's suggestion would be more useful on that.
In that document, also have a look at:
3.5 Choose sensible names for auxiliary predicates
3.8 Choose predicate names to help show the argument order
3.13 Decide whether predicate names should carry the types on which they operate
4.1 Begin every predicate (except perhaps auxiliary predicates) with an introductory comment in a well-defined format
The '?' system for identifying parameter types that will ness mentioned is on page 21.
a useful convention, sponsored for instance by Markus Triska, builds a predicate functor by joining the parameters 'names' - in a wide, applicable sense. Your example could be
parent_child(mel, joan).
...
ancestor_descendant(P, C) :- parent_child(P, C).
ancestor_descendant(A, D) :- ancestor_descendant(A, I), parent_child(I, D).
Also ISO-Prolog, and - for instance - SWI-Prolog library, attempt to follow this strategy.
For instance
atom_codes(Atom, Codes) :- ...
WRT to declare the type and status of arguments, some Prolog provide declarations - for instance Turbo Prolog, ECLiPSe, others... Sometime such declarations are required - usually to check correctness, often to speed up the computation.
SWI-Prolog offers 'just' structured comments, that IDE process automatically, and there has been a contribution aiming to exploit such declarations with runtime check.
Yes, with comments, and/or meaningful argument names,
% ancestor( ?Ancestor, ?Descendent).
ancestor(P,C) :- parent(P, C).
ancestor(P,C) :- ancestor(P,P1), parent(P1, C).
? means the argument can be used both as input (already set when the call is made), or for output (not yet set when the call is made).
The convention promoted in The Art of Prolog (I think) is that you place the name of the predicate after its first argument, to get at the intended argument ordering: P "is" ancestor C. Presumably "ancestor_of". But if you use that name, someone unfamiliar with that convention might read ancestor_of(P,C) as "ancestor of P is C", so it's a double-edged sword.

What is the meaning of predicate "simple/1" in Prolog (SWI-Prolog)

I run into problem while reading a book.
I see a program use predicate "simple" ( I guess simple/1 ). I don't know what is the meaning of this predicate, I can't find it with ?-help(simple) in the console.
But when I tried with some queries in console, it worked something like:
5 ?- simple(p(x)).
false.
6 ?- simple(mia).
true.
7 ?- simple(Mia).
true.
8 ?- simple(f(Mia)).
false.
I guess it is some sort of predicate to determine if the argument was Terms(or Variables) or Complex Terms.
The swi-prolog manual has the following definition:
simple(#Term) is semidet
Term is atomic or a variable.
the definition is in the quintus prolog compatibility library; in the quintus prolog documentation the definition is:
simple(+Term)
Term is currently instantiated to either an atom, a number, a database
or a variable.
in any case, simple/1 is true if the argument is a simple term (not sure what the quintus manuals means by database; possibly a handler for an ODBC connection i guess)
translated to ISO predicates:
simple(T) :- var(T) ; atomic(T).
var/1 it's the most basic metaprogramming device, because it's impossible to predicate (i.e. execute code, binding variables) about any clause without instancing the variables, that are many times the essential part we are interested to.

How can I insert an additional argument after the last argument in a prolog procedure?

I am new to learning prolog, and I want to know, if we have some procedure like
father("Nic","Adam").
and I want to write a function that it will add new value to this
father("Nic","Adam","something"..)
how can I do this? Using list? Or what?
Quick answer: You don't want to do that.
Longer answer: The father/2 predicate has a certain meaning, namely that for father(X,Y) X is the father of Y. A father/3 predicate is a different thing altogether. What do you want to achieve with that third argument? Normally, you use additional rules, which derive things from the father/2 predicate, or even resolve it to a father/3 argument.
The main question remains: what's the purpose of the third argument? If you want your resolution to work for certain specific 3rd arguments based on the existance of a corresponding father/2 predicate for example, you could do father(X, Y, 'something') :- father(X,Y) which will succeed if you have a corresponding father/2 fact.
PS: Do learn your terminology. In Prolog we don't speak of procedures and we don't write functions. Instead we have predicates, facts, rules, ...
PPS: I am not sure which Prolog implementation you are using, but you might want to use 'something' instead of "something". The latter usually creates a list of character codes, not a string:
?- X = 'some'.
X = some.
?- X = "some".
X = [115, 111, 109, 101].
Simply writing
father(nic, adam).
As a predicate already defines it. It is like stating a fact: you declare that father(nic, adam) is true, then you can execute the following with these expected results :
?- father(nic, adam).
Yes
?- father(nic, X).
X = adam

Resources