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

maplist with parametrized predicate

Is it possible to use parametrized predicate in mapList? For example, I would like to do following: iter by list, and for each even element (list contains only numbers) map this element to some value (this value is set by parameter of predicate). …
user6023611
5
votes
1 answer

Prolog Higher-order reduce predicate

We can define a higher-order map predicate as: map([], [], F). map([A|As], [B|Bs], F) :- call(F, A, B), map(As, Bs, F). Similarly, we can define fold (left) as: fold([], Acc, Acc, _F). fold([A|As], B, Acc1, F) :- call(F, Acc1, A, Acc2), …
S0rin
  • 1,283
  • 1
  • 10
  • 22
5
votes
1 answer

Module expansion of goals passed to library meta-predicates

Using SWI-Prolog (Multi-threaded, 64 bits, Version 7.3.5), we proceed step by step: Define dcg nonterminal a//1 in module dcgAux (pronounced: "di-SEE-goh"): :- module(dcgAux,[a//1]). a(0) --> []. a(s(N)) --> [a], a(N). Run the following…
repeat
  • 18,496
  • 4
  • 54
  • 166
4
votes
3 answers

Why doesn't maplist/3 use a template?

The maplist/3 predicate has the following form maplist(:Goal, ?List1, ?List2) However the very similar function findall/3 has the form findall(+Template, :Goal, -Bag) Not only does it have a goal but a template as well. I've found this template…
Wheat Wizard
  • 3,982
  • 14
  • 34
4
votes
3 answers

Reverse lookup in Prolog? (how do I find everything that is true about X?)

So, let's say I have the following in a Prolog database: person(john). person(mary). happy(john). It is clear to that if I want to list all people, I can type: person(X). But, what if I want to find all the things that are true about john? I…
MikeC8
  • 3,783
  • 4
  • 27
  • 33
3
votes
1 answer

Meta-predicate that receives a list of goals as input

I've implemented the following meta-predicate that use SWI-Prolog engines to simultaneously enumerate solutions to two or more backtrackable predicates. enumerate(Gs) :- maplist(term_variables, Gs, Xs), findall(E, ( nth1(I, Gs, G), …
slago
  • 5,025
  • 2
  • 10
  • 23
3
votes
3 answers

Prolog filter a list of all elements for which a custom goal fails

I am trying to write a predicate filter(List, PredName, Result) that filters a List of all its elements for which the goal PredName fails and subsequently returns the Result list. The predicate PredName/1 should be defined when calling the procedure…
General_9
  • 2,249
  • 4
  • 28
  • 46
3
votes
1 answer

How to create a higher order DCG parser in Prolog?

I would like to have some more general parsers, like for example paren that would take a parser and wrap it with parentheses: paren(Parser, Result) --> "(", some_magic_dcg_call(Parser, Result), ")". :- phrase(paren(number, N), "(123)"). 123 :-…
radrow
  • 6,419
  • 4
  • 26
  • 53
3
votes
1 answer

Testing goal expansion of meta-predicate maplist

Q: How can we get rid of maplist overhead—like SWI's apply_macros—in SICStus Prolog? A: Goal expansion. First, we define the auxiliary predicates we'll need. In the following we use SICStus Prolog 4.5.0. :- module(maplist_macros, [maplist/2,…
repeat
  • 18,496
  • 4
  • 54
  • 166
3
votes
2 answers

Can I prepend the argument list in call/2?

call(Goal,Arg) allows to append the argument Arg to the arguments of Goal and call the resulting goal, e.g. call(succ(1), R). is the same as succ(1, R). However, I don't want to append to the argument list, but instead prepend,…
morxa
  • 3,221
  • 3
  • 27
  • 43
3
votes
1 answer

DCG version for maplist/3

The following meta-predicate is often useful. Note that it cannot be called maplist//2, because its expansion would collide with maplist/4. maplistDCG(_P_2, []) --> []. maplistDCG(P_2, [A|As]) --> {call(P_2, A, B)}, [B], maplistDCG(P_2,…
false
  • 10,264
  • 13
  • 101
  • 209
3
votes
1 answer

is it possible to use maplist/3 with 'anonymous' predicate?

What I wish to achieve with maplist/3 could for example be following pseudo code: maplist( lambda X: Z/Y=X, to_lower(Z,LC), char_code(L,LC), return L/Y, ['A'/42, 'B'/500], Res). Res = ['a'/42, 'b'/500] I know that it is possible to write…
Michelrandahl
  • 3,365
  • 2
  • 26
  • 41
3
votes
2 answers

Determining if graph is connected in prolog

I need to make a predicate isConnected/1 that takes a graph as an argument and determines if there is an undirected path between the pairs. Suppose I have a list of edges (where G is a graph): isEdge(G,1,2). isEdge(G,2,3). isEdge(G,4,5). So because…
3
votes
1 answer

What kind of meta argument is the first argument of predicate_property/2?

In other words, should it be 0 or : or something else? The Prolog systems SICStus, YAP, and SWI all indicate this as :. Is this appropriate? Shouldn't it be rather a 0 which means a term that can be called by call/1? To check your system type: | ?-…
false
  • 10,264
  • 13
  • 101
  • 209
3
votes
3 answers

How do I code rec/3 in the presence of a meta_predicate declaration?

I have the following code which works fine without a meta_predicate declaration. I have defined a predicate rec/3 as follows: :- use_module(library(lambda)). rec(F,1,F). rec(F,N,\A^B^(call(F,A,H),call(G,H,B))) :- N>1, M is N-1,…
user502187