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
3
votes
1 answer

max list gnu prolog

In GNU prolog, with constraints: I have a list, which, for N = 5 (for instance), it is like this: [3*(1-_#0(0..1)),2*(1-_#18(0..1)),1*(1-_#36(0..1)),4*(1-_#54(0..1)),2*(1-_#72(0..1))] I am working on a finite domain constraint environment and I…
gsamaras
  • 71,951
  • 46
  • 188
  • 305
2
votes
2 answers

What do the numeric arguments for meta_predicate mean in SWI-Prolog?

I am writing a Prolog program, and I am trying to incorporate modules into the program design to encapsulate complexity reduce redundant functionality. One feature I am having difficulty with is the use of metapredicates. I would like to define a…
AugerAlpha
  • 23
  • 4
2
votes
1 answer

Prolog check for duplicates in answer to query (easy way?)

This is a terrible example, but say I have points a, b and c and blue and red lines, e.g. line(blue,a) means point a lines on a blue line. point(a). point(b). point(c). line(blue,a). line(blue,b). line(red,a). line(red,c). I want to find out which…
Matt
  • 809
  • 1
  • 10
  • 22
2
votes
1 answer

Filter association table prolog

I need to write the include_assoc / 3 predicate that filters the associative array (second argument) with the given predicate (first argument) and writes the result to the third argument. The predicate should work analogously to include / 3. The…
restarcik
  • 27
  • 5
2
votes
1 answer

Goal expansion for an `if_/3` operator in Prolog

I'm writing a tokeniser and I want to use if_/3 to preserve logical-purity in my code. The code looks like the following code1 on the left—but I want it to look like the one on the right. if_(Cond1_1, % ( Cond1_1 …
repeat
  • 18,496
  • 4
  • 54
  • 166
2
votes
1 answer

How to set domain of pair variable in CSP in Sicstus Prolog

I'm using Sicstus Prolog, and I'm trying to solve the Domino Puzzle. I have a list with triplets, which I need to set the domain of. I know how to set the domain of a single variable, or a list of single variables, but how can I do it if my list has…
Tirafesi
  • 1,297
  • 2
  • 18
  • 36
2
votes
0 answers

Behavior of `foldl1/3` and `foldr1/3` meta-predicates on empty lists

Looking for advice. I'm adding foldl1/3 and foldr1/3 meta-predicates to the Logtalk library. These can be easily defined: foldl1(Closure, [Head| Tail], Result) :- foldl(Closure, Head, Tail, Result). foldr1(Closure, [Head| Tail], Result) :- …
Paulo Moura
  • 18,373
  • 3
  • 23
  • 33
2
votes
1 answer

Prolog binding arguments

In sicstus prolog, there's a predicate: maplist(:Pred, +List) Pred is supposed to take just one argument - List element. How can I pass a 2-argument predicate, with first argument defined? In other languages it would be written…
Sebastian Nowak
  • 5,607
  • 8
  • 67
  • 107
2
votes
2 answers

Prolog, Determine if graph is acyclic

I need to define a predicate acyclic/1 that takes a graph in as input and determine if that graph is acyclic. So from my understanding graph1(a,b). graph1(b,c). graph1(c,a). Will return no and graph2(a,b). graph2(b,c). will return yes I…
2
votes
1 answer

How to avoid a meta argument warning in SICStus SPIDER?

This is probably related to a comp.lang.prolog-discussion. I'm getting several warnings like this using Eclipse with the SICStus SPIDER: The plain meta argument (Y) is passed as a closure argument (with 0 suppressed arguments) to the callee. Here…
Rune
  • 698
  • 1
  • 7
  • 22
1
vote
2 answers

maplist/2 vs extra recursion predicate with SICStus Prolog

Consider the following simple interaction with SICStus Prolog: $ sicstus -f SICStus 4.8.0 (x86_64-linux-glibc2.28): Sun Dec 4 13:17:41 UTC 2022 [...] | ?- use_module(library(between)), use_module(library(lists)). [...] | ?- compile(user). %…
repeat
  • 18,496
  • 4
  • 54
  • 166
1
vote
1 answer

Functional patterns in Prolog

How do I create a predicate that takes another predicate and returns a derived version of it? For example, pairwise predicates can be fairly mechanically extended to apply to lists: all_whatever(_, []). all_whatever(X, [Y|T]) :- whatever(X, Y),…
vasily
  • 2,850
  • 1
  • 24
  • 40
1
vote
1 answer

Prolog "switch" statement

How can I implement a switch statement equivalent to a nested set of if_s? Ideally something like (don't mind the syntax): compatible(X, Y) :- switch X a1 -> dif(Y, b2), a2 -> dif(Y, c2), dif(Y, c3), _ -> true working the same…
vasily
  • 2,850
  • 1
  • 24
  • 40
1
vote
2 answers

Checking all Members Of List

Let's say that I have the following list in my prolog: L=[10,11,2,3,5] Is there a way that we can check all the members of a list L to make sure that each member is less than 5?
annita
  • 41
  • 2
1
vote
1 answer

Finding a desired solution without using call_nth/2 multiple times or the use of findall/3?

As far as i understand, call_nth(:Goal, ?Nth) returns the Nth solution of Goal but at the same time it secretly calculates all the previous solutions (from 1st to (N-1)th) and simply ignores them. If we wish to compare the Xth with the (X+1)th…
TimzyPatzy
  • 95
  • 4