Create predicate using operator: -op?


I have some doubts about the operator: -op in Prolog, because I can not handle it. My exercise is focused on creating a predicate that from a list attribute-value, whose format is [a1 => v1, a2 => v2, ..., aN => vN], get the value of a attribute that is requested.


Example:? - value (shape, [color => blue, shape = spherical, weight => light,   material = > plastic], X). X = spherical.

To do this, we suggest defining the operator = > in the logical base.

asked by Luiso Vega 31.08.2017 в 18:25

2 answers


The op/3 directive allows you to declare atoms that will be treated syntactically as operators with a certain class (infix, suffix or prefix), associativity and priority.

In your case, you need an operator =>/2 infix that is not associative:

:- op(300, xfx, '=>').

Now you can handle the atom '=>'(x,y) as x => y (although internally these two structures are equivalent).

Finally, you can write your predicate valor/3 as:

valor(X, [X=>Y|_], Y).
valor(X, [_|T], Y) :- valor(X, T, Y).

Here you have more information about the operators in Prolog.

answered by 01.09.2017 / 11:35

Wao, surprising. It worked for me without problems. Thanks for the collaboration.

?- valor(forma, [color=>azul, forma=>esferica, peso=>ligero, material=>plastico], X). 
X = esferica ; 

?- valor(peso, [color=>azul, forma=>esferica, peso=>ligero, material=>plastico], X). 
X = ligero ; 

?- valor(casa, [color=>azul, forma=>esferica, peso=>ligero, material=>plastico], X). 
answered by 01.09.2017 в 17:30