Prolog GNU - Univ operator? Explanation of it

前端 未结 2 495
逝去的感伤
逝去的感伤 2020-12-05 19:47

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         


        
相关标签:
2条回答
  • 2020-12-05 20:08

    The most appropriate rewrite in my opinion would be:

     bar( [H|_], Item ) :- call(H, Item).
    

    call/n are not yet part of the ISO core standard, but they might become in the near future (*). A lot of Prolog systems already support them.

    There is one reason why call/n should be preferred over simple (=..)/2 and functor/3 + arg/3 solutions. The call/n solution is capable to handle closures (**).

    With the simple (=..)/2 and functor/3 + arg/3 solution one can invoke bar/2 only with atoms in the first list argument. For example:

     p1(1).
     p2(2).
     ?- bar( [p1, p2], 1 ).
     Yes
     ?- bar( [p1, p2], 2 ).
     Yes
     ?- bar( [p1, p2], 3 ).
     No
    

    With closures we are not restricted to atoms, and we might save some coding effort. For example we can do the following directly:

     ?- bar( [=(1), =(2)], 1 ).
     Yes
     ?- bar( [=(1), =(2)], 2 ).
     Yes
     ?- bar( [=(1), =(2)], 3 ).
     No
    

    Best Regards

    (*)
    Draft Technical Corrigendum 2
    http://www.complang.tuwien.ac.at/ulrich/iso-prolog/dtc2#call

    (**)
    Who Invented It?: call/n Predicates
    http://www.complang.tuwien.ac.at/ulrich/Prolog-inedit/naish.html

    0 讨论(0)
  • 2020-12-05 20:26

    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
    
    0 讨论(0)
提交回复
热议问题