Prolog GNU - Univ operator? Explanation of it

前端 未结 2 502
逝去的感伤
逝去的感伤 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

提交回复
热议问题