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
46
votes
3 answers

Definition of a path/trail/walk

Many predicates define some kind of an acyclic path built from edges defined via a binary relation, quite similarly to defining transitive closure. A generic definition is thus called for. Note that the notions defined in graph theory do not readily…
false
  • 10,264
  • 13
  • 101
  • 209
40
votes
1 answer

Prolog map procedure that applies predicate to list elements

How do you write a Prolog procedure map(List, PredName, Result) that applies the predicate PredName(Arg, Res) to the elements of List, and returns the result in the list Result? For example: test(N,R) :- R is N*N. ?- map([3,5,-2], test, L). L =…
General_9
  • 2,249
  • 4
  • 28
  • 46
27
votes
1 answer

Definition of Reflexive Transitive Closure

Many predicates essentially use some form of transitive closure, only to discover that termination has to be addressed too. Why not solve this once and forever with closure0/3: :- meta_predicate closure0(2,?,?). :- meta_predicate closure(2,?,?). :-…
false
  • 10,264
  • 13
  • 101
  • 209
22
votes
3 answers

Most general higher-order constraint describing a sequence of integers ordered with respect to a relation

In CLP(FD), we frequently need to state: "This is a list of integers and finite domain variables in (sometimes: strictly) ascending/descending order." Is there any CLP(FD) system that provides a general (parametrisable) built-in constraint for this…
mat
  • 40,498
  • 3
  • 51
  • 78
18
votes
6 answers

Prolog: Filtering a list?

I'm currently working on a very short project on Prolog, and just got stuck trying to apply a "filter" I have created to a list. I have what you could call the filter ready, but I can't apply it. It'd be better if I illustrate: filter(A, B)…
Sergio Morales
  • 2,600
  • 6
  • 32
  • 40
14
votes
2 answers

Prolog GNU - Univ operator? Explanation of it

So the univ operator. I don't exactly understand it. For example this: foo(PredList,[H|_]) :- bar(PredList,H). foo(PredList,[_|T]) :- foo(PredList,T),!. bar([H|_],Item) :- G =.. [H,Item],G. bar([_|T],Item) :- bar(T,Item). What is this doing? This…
Matt
  • 7,049
  • 7
  • 50
  • 77
11
votes
2 answers

Pairwise relation over list

The following higher order predicate succeeds if all pairs of the list's elements are true for a given relation. Is there a common or better, more intention revealing name for this relation? My original motivation for this name was that in clpfd,…
false
  • 10,264
  • 13
  • 101
  • 209
9
votes
3 answers

Fold over a partial list

This is a question provoked by an already deleted answer to this question. The issue could be summarized as follows: Is it possible to fold over a list, with the tail of the list generated while folding? Here is what I mean. Say I want to…
user1812457
8
votes
2 answers

Are HiLog terms still useful in modern Prolog?

Are Hilog terms (i.e. compounds having as functors arbitrary terms) still regarded as a powerful feature in XSB Prolog (or any other Prolog) ? Are there many XSB projects currently using this feature ? which of them for example ? I ask since as far…
Sergio
  • 8,532
  • 11
  • 52
  • 94
7
votes
3 answers

Collect all "minimum" solutions from a predicate

Given the following facts in a database: foo(a, 3). foo(b, 2). foo(c, 4). foo(d, 3). foo(e, 2). foo(f, 6). foo(g, 3). foo(h, 2). I want to collect all first arguments that have the smallest second argument, plus the value of the second argument.…
user1812457
6
votes
1 answer

Reified call_with_time_limit / call_with_inference_limit

I am trying to define a relation callto_status(Goal, Status) that always succeeds and unifies Status according to the result of calling Goal (in other words, I would like to implement a reified version of call_with_inference_limit/3). My…
lambda.xy.x
  • 4,918
  • 24
  • 35
6
votes
2 answers

Relying on rule order

To calculate the hamming distance between two lists of the same length, I use foldl(hamm, A, B, 0, R). with this definition of hamm/4: hamm(A, A, V, V) :- !. hamm(A, B, V0, V1) :- A \= B, V1 is V0 + 1. The cut in the first rule prevents the…
user1812457
5
votes
2 answers

Lambdas in Prolog?

I normally was able to figure out some usages of Lambda with maplist, but in general have hard time using lambda in prolog. It is probably because it differ from other languages, because of unification. Here is one of the sticking points : How do…
sten
  • 7,028
  • 9
  • 41
  • 63
5
votes
1 answer

What modules does an unqualified assert add a term to?

?- assertz(:- module(foo1, [f/1])). true. ?- foo1:assertz(f(1)). true. ?- foo1:f(1). true. ?- foo2:f(1). Correct to: "foo1:f(1)"? no ERROR: Undefined procedure: foo2:f/1 ERROR: In: ERROR: [8] foo2:f(1) ERROR: [7] Makes sense to me.…
user48956
  • 14,850
  • 19
  • 93
  • 154
5
votes
1 answer

Custom DCG Operators

Suppose I want to write a custom operator for composing DCG rules in a way that would otherwise be repetitive. For example, suppose I had I a DCG, ws such that: ws --> []. ws --> " ", ws. to match zero or more spaces. Obviously, if I want optional…
junius
  • 570
  • 3
  • 14
1
2 3 4 5