Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prolog GNU - Univ operator? Explanation of it

So the univ operator. I don't exactly understand it.

For example this:

foo(PredList,[H|_]) :- bar(PredList,H).
foo(PredList,[_|T]) :- foo(PredList,T),!.

bar([H|_],Item) :- G =.. [H,Item],G.
bar([_|T],Item) :- bar(T,Item).

What is this doing? This looks to see if another predicate is true. I don't understand what the ".." does.

How would you rewrite this without the univ operator?

like image 691
Matt Avatar asked Nov 09 '10 00:11

Matt


1 Answers

Univ (=..) breaks up a term into a list of constituents, or constructs a term from such a list. Try:

?- f(x,y) =.. L.
L = [f, x, y].

?- f(x,y,z) =.. [f|Args].
Args = [x, y, z].

?- Term =.. [g,x,y].
Term = g(x, y).

bar seems to call each predicate in PredList on Item, with foo backtracking over the Items. (Using a variable as a predicate is not portable; the call predicate should be preferred.)

Edit: Kaarel is right, univ can be replaced by functor/3 and arg/3, as follows:

bar([H|_],Item) :-
    functor(Goal,H,1),   % unifies Goal with H(_)
    arg(1,Goal,Item),    % unifies first argument of Goal with Item
    call(Goal).          % use this for portability
like image 193
Fred Foo Avatar answered Sep 22 '22 23:09

Fred Foo