Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prolog: ignore unwanted variables in the output

Is there a way to force the prolog CLI to return query results with only the variables I'm interested in? A simple example would be:

?- mother(M, C1), father(F, C1).

which returns bindings for all M, F and C1. But I'm interested only in M and F, while C1 is just clutter. In this simple example it's not bad but for longer queries with many helper variables it's much more vexing.

Is there a simple way to express that via the query; I mean without defining a separate rule?

Cheers, Jacek

like image 456
Jacek Avatar asked Dec 05 '22 15:12

Jacek


2 Answers

A very straight-forward way to do this is to use library(lambda) by Ulrich Neumerkel.

For example, in your case, given the sample facts:

mother_child(m, c).
father_child(f, c).

We get for your query:

?- mother_child(M, C),
   father_child(F, C).
M = m,
C = c,
F = f.

We would like to project away the variable C.

So we wrap the whole query inside a lambda expression such that only M and F have global scope and hence are reported by the toplevel:

?- M^F+\(mother_child(M, C),
         father_child(F, C)).
M = m,
F = f.

This obviously becomes all the more useful the more variables you want to project away. You only need to specify the variables you want the toplevel to report.

like image 90
mat Avatar answered Jan 13 '23 21:01

mat


In the case of SWI-Prolog, it offers a flag to hide variables that start with an underscore. To change the settings execute goal

set_prolog_flag(toplevel_print_anon, false).

in SWI-Prolog session. Alternatively, you can add it to your .swiplrc.

More detailed answer with examples is provided at https://stackoverflow.com/a/34917391/2471388.

like image 21
Jakub Mendyk Avatar answered Jan 13 '23 20:01

Jakub Mendyk