Questions tagged [meta-predicate]

A meta predicate is a predicate that contains an argument defined over predicates or related to some module. In many Prolog systems, such predicates are declared with a directive (meta_predicate)/1.

A predicate is called a meta-predicate or higher-order predicate if one of its arguments denotes a predicate.

Meta-predicates are an important means for describing common patterns in a general and flexible way. For example, the common situation of describing a relation Rel_1 that holds for each element of a list can be defined abstractly as:

maplist(_, []).
maplist(Rel_1, [L|Ls]) :-
    call(Rel_1, L),
    maplist(Rel_1, Ls).

Rel_1 denotes a partial predicate indicator, which is called a closure. The higher-order predicate call/2 is used to call a closure with an additional argument.

Important and widely available meta-predicates are:

  • call/[2-8] call a closure with additional arguments
  • maplist/[2,3] denoting relations for each element of a list
  • foldl/4 describes a list traversal that keeps track of a state.

Richard O'Keefe's draft for An Elementary Prolog Library contains descriptions and implementations of many important meta-predicates.

62 questions
1
vote
1 answer

Prolog Implementation of foldl1

I am trying to implement foldl1 in prolog without using build-in foldl predicate. My code produce Syntax error: Operator expected: foldl1(_, [E], E). foldl1(Predicate, [X,Y|Z], Result) :- call(Predicate(X), Y, Ans), foldl1(Predicate, [Ans|Z],…
MaxSha
  • 129
  • 1
  • 7
1
vote
1 answer

How to specify a predicate as parameter to another in prolog?

I want to write a prolog predicate that holds if the predicate appearing as first parameter holds for all elements of the list appering in second parameter. Here is something that I have tried: ?- listing(all). all(pred(_), [A|B]) :- pred(A), …
pii_ke
  • 2,811
  • 2
  • 20
  • 30
1
vote
1 answer

Transform in meta-predicate at prolog

I have been reading and noticed that predicates like call are called meta-predicates and they can return other predicates as a result (Don't know if return is a good use of the word here) for example here: assert(call(goal, X,Y)). Edit: lurker…
Souza
  • 1,124
  • 5
  • 19
  • 44
1
vote
2 answers

Abstracting over predicates

An exercise I'm trying out starts with the following…
kjo
  • 33,683
  • 52
  • 148
  • 265
1
vote
1 answer

Remove item from multiple lists

I have a subproblem for which the solution is way larger than what I would suspect is necessary. The problem is defined as follows Remove X from all groups where group has id between Y and Z or A and B expressed as a pseudo query it looks like this…
Torben Pi Jensen
  • 850
  • 9
  • 27
1
vote
1 answer

Retrieve output from body executed with call in Prolog

Suppose we have a predicate p/2 that does something similar to this: p('an atom', OutputList) :- some_predicate_1, some_predicate_2, ... findall(...,...,OutputList). p/2 does something arbitrarly complex and in the end put some result in…
Kami
  • 1,079
  • 2
  • 13
  • 28
1
vote
2 answers

put a Prolog goal as input in the first argument position and returns the number of times this goal succeeds in the second argument position

As the title says I want to write a program that does this. an example would be: ?- count(member(X,[1,2,3]), N). N = 3 Yes But not only for the build in member, but also for some operators like: ?- count(17 =:= 12 + 5, N). N = 1 Yes Can…
New Guy
  • 69
  • 1
  • 1
  • 6
1
vote
2 answers

How can I use a functor name like a variable in Prolog?

We have this assignment for our Prolog course. After two months of one hour per week of Prolog, it is still an enigma to me, my thinking seems unable to adapt from procedural languages - yet. There is a knowledge base containing predicates/functors…
AOphagen
  • 308
  • 2
  • 18
1
vote
1 answer

Higher-order predicates

boolean(true). boolean(false). formula_0(P, Q):- (P; Q), \+ P. solution_for_formula(P, Q, Formula):- maplist(boolean, [P, Q]), call([Formula, P, Q]). A follow-up to my earlier question. Why wouldn't this work? (If I replace call([Formula, P,…
user797257
0
votes
1 answer

Struggling to use Prolog's maplist/2 like a functional mapping function

I'm using SWI-prolog and I'm trying to create a helper-function that will map over a list and multiply each element with a given integer. My intention is to create a predicate that corresponds to the Haskell function multByN x n = x * n and use it…
Nezhaii
  • 27
  • 6
0
votes
3 answers

How to predicate elements in a list based on element type

Given a list of the format, [item(a), item(b), item(c), other(d), other(e) ...] where the number of items isn't fixed, nor is the number of others, but items always precede other, how do I split the list so that I can pass the items and the others…
15150776
  • 21
  • 7
0
votes
1 answer

Prolog reasoning about predicates

Assume someone wrote the following huge list of clauses: loves(me, wife). loves(me, dog). loves(wife, dog). hates(me, enemy). attracts(iron, magnet). ... Now I want to automatically generate reciprocal clauses given some higher-order predicate and…
0
votes
0 answers

What's the equivalent of a map function in Prolog?

If I have a list, and want to produce a list with all items transformed by a "function", eg, the equivalent of Lisp's (map f xs) How do I do the equivalent in Prolog?
interstar
  • 26,048
  • 36
  • 112
  • 180
0
votes
4 answers

Calculating arithmetically in Prolog on lists

we are currently working on the implementation of the predicate reduce, which Needs to take a list L, a binary function F that operates on the elements of L and has neutral element N and maps them to a number E that is obtained by applying F to the…
user8060841
0
votes
2 answers

Use of Prolog =.. predicate

I'm working on an exercise whereby I'm trying to use the =.. predicate to write a procedure that removes all elements in List for which PredName(X) fails and returns the remaining list as Result: filter(List, PredName, Result) In this case with…
Zenadia Groenewald
  • 107
  • 1
  • 3
  • 12