for example: aggregate_all/3 , findall/3, aggregate/4 and so on.
What is the difference between aggregate/3 and aggregate/4.
The number is the so-called arity of the predicate and indicates the number of arguments of the predicate.
This is useful because there are often several variants of predicates that share the same name, but differ in their number of arguments.
Examples: findall/3 and findall/4, append/3 and append/2 etc.
You also often see the notation (Pred)/2. Example: (#=)/2.This is because #= (for example) is also an infix operator, and the parentheses turn (#=)/2 into a valid Prolog term.
The slash (/) symbol is not used only in built in predicates but in all predicates ant it states the number of parameters (arity) of the predicate for example aggregate/3 is a predicate with 3 parameters while aggregate/4 is a predicate with 4 parameters.
Related
I recently started studying and working with Prolog for an application where logic programming is very suited for. In particular, I am woroking with SWI-Prolog (v. 7.6.4, amd64) and Etalis (v. 1.1), an extension for prolog.
What I am not understading is the meaning of: /0, /1, and even /2, /3, that I found in examples during the definition of custom predicates. Most of the prolog directives are defined by directive_name/1.
So, what do these numbers stand for?
It's simply the number of arguments.
For example, append/3 means append(A1, A2, A3).
This number is called the arity, it is the number of arguments a predicate, or functor takes. A constant is in fact a fuctor with zero arity, so /0.
The name of a predicate together with the arity identify a specific predicate. For example the member/2 predicate is diffrent from the append/2 predicate, and the append/3 predicate is different from the append/2 predicate.
In Prolog, [H|T] is the list that begins with H and where the remaining elements are in the list T (internally represented with '.'(H, '.'(…))).
Is it possible to define new syntax in a similar fashion? For example, is it possible to define that [T~H] is the list that ends with H and where the remaining elements are in the list T, and then use it as freely as [H|T] in heads and bodies of predicates? Is it also possible to define e.g. <H|T> to be a different structure than lists?
One can interpret your question literally. A list-like data structure, where accessing the tail can be expressed without any auxiliary predicate. Well, these are the minus-lists which were already used in the very first Prolog system — the one which is sometimes called Prolog 0 and which was written in Algol-W. An example from the original report, p.32 transliterated into ISO Prolog:
t(X-a-l, X-a-u-x).
?- t(nil-m-e-t-a-l, Pluriel).
Pluriel = nil-m-e-t-a-u-x.
So essentially you take any left-associative operator.
But, I suspect, that's not what you wanted. You probably want an extension to lists.
There have been several attempts to do this, one more recent was Prolog III/Prolog IV. However, quite similar to constraints, you will have to face how to define equality over these operators. In other words, you need to go beyond syntactic unification into E-unification. The problem sounds easy in the beginning but it is frightening complex. A simple example in Prolog IV:
>> L = [a] o M, L = M o [z].
M ~ list,
L ~ list.
Clearly this is an inconsistency. That is, the system should respond false. There is simply no such M, but Prolog IV is not able to deduce this. You would have to solve at least such problems or get along with them somehow.
In case you really want to dig into this, consider the research which started with J. Makanin's pioneering work:
The Problem of Solvability of Equations in a Free Semi-Group, Akad. Nauk SSSR, vol.233, no.2, 1977.
That said, it might be the case that there is a simpler way to get what you want. Maybe a fully associative list operator is not needed.
Nevertheless, do not expect too much expressiveness from such an extension compared to what we have in Prolog, that is DCGs. In particular, general left-recursion would still be a problem for termination in grammars.
It is possible to extend or redefine syntax of Prolog with iso predicate
:- op(Precedence, Type, Name).
Where Precedence is a number between 0 and 1200, Type describe if the operatot is used postfix,prefix or infix:
infix: xfx, xfy, yfx
prefix: fx, fy
suffix: xf, yf
and finally name is the operator's name.
Operator definitions do not specify the meaning of an operator, but only describe how it can be used syntactically. It is only a definition extending the syntax of Prolog. It doesn't gives any information about when a predicate will succeed. So you need also to describe when your predicate succeeds. To answer your question and also give an example you could define :
:- op( 42, xfy, [ ~ ]).
where you declare an infix operator [ ~ ]. This doesn't means that is a representation of a list (yet). You could define clause:
[T ~ H]:-is_list([H|T]).
which matches [T~H] with the list that ends with H and where the remaining elements are in the list T.
Note also that it is not very safe to define predefined operators
like [ ] or ~ because you overwrite their existing functionality.
For example if you want to consult a file like [file]. this will
return false because you redefined operators.
I am teaching myself Prolog and have been given a handful of examples.
One of which uses the dynamic/1 built-in directive:
:- dynamic(items/1).
I get the idea of dynamic. That one can modify predicates via the assert, and retract predicates.
However, the program also uses the following in places:
:- dynamic(location/2).
What is the difference between the two /1 and /2, is their also a /3 .... /n?
In Prolog, predicates are identified by their name (or functor) and their number of arguments (or arity). Thus, items/1 denotes a predicate with functor items and arity 1 while location/2 denotes a predicate with functor location and arity 2. Two predicates with the same functor but different arities are different predicates.
I am new to prolog and was trying to create a binary predicate which will give
a list in which all numbers are squared, including those in sublists.
e.g.
?-dcountSublists([a,[[3]],b,4,c(5),4],C).
C=[a,[[9]],b,c(5),16]
Can anyone guide me how i can do this.
Thank You. Answer with a snippet is appreciated
This is easily achieved using recursion in Prolog. Remember that everything in Prolog is either a variable, or a term (atoms are just 0-arity terms), so a term like the following:
[a,[[3]],b,4,c(5),4]
...is easily deconstructed (also note that the list syntax [..] is sugar for the binary predicate ./2). Prolog offers a range of predicates to test for particular types of terms as well, such as numbers, strings, or compound terms (such as compound/1).
To build the predicate you're after, I recommend writing it using several predicates like this:
dcountSublists(In, Out) :-
% analyze type of In
% based on type, either:
% 1. split term into subterms for recursive processing
% 2. term cannot be split; either replace it, or pass it through
Here's an example to get you started which does the hard bit. The following recognizes compound terms and breaks them apart with the term de/constructor =../2:
dcountSublists(In, Out) :-
% test if In has type compound term
compound(In),
% cut to exclude backtracking to other cases below this predicate
!,
% deconstruct In into functor and an argument list
In =.. [Func|Args],
% apply dcountSublists/2 to every argument, building new args
maplist(dcountSublists, Args, NewArgs),
% re-construct In using the new arguments
Out =.. [Func|NewArgs].
dcountSublists(In, Out) :-
% test if In has type atom
atom(In), !,
% pass it through
Out = In.
Testing:
?- dcountSublists([a,[[e]],b,a,c(s),a], L).
L = [a, [[e]], b, a, c(s), a].
Note that this fails if the input term has numbers, because it doesn't have a predicate to recognize and deal with them. I'll leave this up to you.
Good luck!
SWI-Prolog has the predicate maplist/[2-5] which allows you to map a predicate over some lists.
Using that, you only have to make a predicate that will square a number or the numbers in a list and leave everything else the same. The predicates number/1, is_list/1 are true if their argument is a number or a list.
Therefore:
square(N,NN):-
integer(N),
NN is N*N.
square(L,LL):-
is_list(L),
dcountSublists(square,L,LL).
square(Other,Other):-
\+ number(Other),
\+ is_list(Other).
dcountSublists(L,LSquared):-
maplist(square,L,LSquared).
with the negation in the final predicate we avoid multiple (wrong) solutions:
for example dcountSublists([2],X) would return X=[4] and X=[2] otherwise.
This could be avoided if we used an if-then-else structure for square or once/1 to call square/2.
If this is homework maybe you should not use maplist since (probably) the aim of the exercise is to learn how to build a recursive function; in any case, I would suggest to try and write an equivalent predicate without maplist.
anyone have idea how to solve this problem
counts the number of occurrences of an operator inside an expression. For instance, the query:
?- count(a+b*c-(2+3*4)/(5*(2+a)+(b+c)^f((d-e)*(x-y))), *, C).
would count the number of occurrences of operator * in the expression given as the first argument and output on C
I am using SWI-prolog
Is this homework?
Here's some hints:
Prolog operators are syntactic sugar around normal prolog terms. The expression 3 * 2 + 1 is parsed as the term '+'('*'(3,2),1).
The built-in predicate =.. decomposes a term into a list, the head of which is the functor and the tail of which comprises the [non-decomposed] terms that are the arguments to the original term.
The built-in predicate functor/3 unifies a term with its functor and arity.
You might also want to look at arg/3 which provide the means to examine the arguments of the specified term by ordinal position.
Now that you know that, a fairly simple recursive solution should present itself. If you need to factor in the arity of the desired operator, it's a little more convoluted (but not much).