0
votes

I have this portion of code as base case in a recursive predicate.

riduci_lista_valori([[V, N]],m_var,Lr):-
   member(V, m_var),
   Lr =[N].

The problem is that when I perform my query it does not unify correctly the arguments of the predicate with its parameters.

Given the code, my query is: riduci_lista_valori([[a, 5]], [c,e], F). And I expect Prolog to return F = [5]. Debugging the code seems it doesn't recognize properly the arguments because it does not unify like: V = a, N = 5 m_var = [c,e] but it gives: 1 = [[a, 5]] and 2 = [c, e].

Whereas if I prompt : [[V, N]] = [[a,5]]. it makes the correct unification: V = a , N = 5.

What am I doing wrong? thank you!

1

1 Answers

4
votes

To detect causes of failure in Prolog, use declarative debugging:

Add the following definitions to your program:

:- op(950,fy, *).
*_.

Now, you can use (*)/1 to generalize away specific goals.

For example:

riduci_lista_valori([[V, N]], m_var, Lr) :-
        * member(V, m_var),
        * Lr =[N].

Even with this much more general version, we get:

?- riduci_lista_valori([[a, 5]], [c,e], F).
false.

This means that if you expect the query to succeed at all, you need to further generalize your program. See what actually remains of the program:

riduci_lista_valori([[V, N]], m_var, Lr) :- true.

This snippet is already too specific. Let us generalize it like this:

riduci_lista_valori([[V, N]], Var, Lr) :- true.

Now the query succeeds:

?- riduci_lista_valori([[a, 5]], [c,e], F).
true.

Thus, this second argument of your clause head is a good candidate for thorough inspection!

Maybe you meant to use a variable instead of the atom m_var?