English 中文(简体)
prolog cut off in method
原标题:
  • 时间:2009-11-13 16:17:57
  •  标签:
  • prolog
  • logic

I have a question I would like to ask you something about a code snippet:

insert_pq(State, [], [State]) :- !.
insert_pq(State, [H|Tail], [State, H|Tail]) :-
    precedes(State, H).
insert_pq(State, [H|T], [H|Tnew]) :-
    insert_pq(State, T, Tnew).
precedes(X, Y) :- X < Y.  % < needs to be defined depending on problem

the function quite clearly adds an item to a priority queue. The problem I have is the cut off operator in the first line. Presumably whenever the call reaches this line of code this is the only possible solution to the query, and the function calls would simply unwind (or is it wind up?), there would be no need to back track and search for another solution to the query.

So this cut off here is superfluous. Am I correct in my deduction?

问题回答

Yes, any half-decent Prolog compiler will notice that there is no other clause where the second argument is an empty list.

It would be more useful at the end of the second clause, though I d rather combine the second and the third clause and use a local cut (precedes(...) -> ... ; ...).

The particular technique that the compiler users to eliminate candidate predicates for matching is called argument indexing. Different prolog implementations could potentially index different numbers of arguments by default.

So if you re worried about whether an argument is being indexed or not, you should check how many arguments the prolog you re using indexes. According to the SWI reference manual it only indexes the first argument by default. So in your case the cut is actually not redundant. You can however explicitly stipulate which arguments should be indexed using the predicates index/1 and hash/1 which are linked to in the above link.

Or you could just reorder the arguments, or you could just keep the cut.

Yes, you are correct. Even if the compiler isn t half-decent (which SWI Prolog certainly is), the worst it can do is match the second and third clauses, which will fail immediately.

However, if the second clause matches, the third does as well. Is this the intended behaviour?





相关问题
Prolog difference routine

I need some help with a routine that I am trying to create. I need to make a routine that will look something like this: difference([(a,b),(a,c),(b,c),(d,e)],[(a,_)],X). X = [(b,c),(d,e)]. I really ...

Python: Analyzing complex statements during execution

I am wondering if there is any way to get some meta information about the interpretation of a python statement during execution. Let s assume this is a complex statement of some single statements ...

Python nested lists and recursion problem

I m trying to process a first order logic formula represented as nested lists and strings in python so that that its in disjunctive normal form, i.e [ & , [ | , a , b ], [ | , c , d ]] ...

prolog - why this strange trace

here is the prolog code (which i sort of follow). len([],0). len([_|T],N) :- len(T,X), N is X+1. and here is the trace for it (im running linux, swi) [trace] ?- len([d,f,w,c],X). Call: (7) ...

prolog cut off in method

I have a question I would like to ask you something about a code snippet: insert_pq(State, [], [State]) :- !. insert_pq(State, [H|Tail], [State, H|Tail]) :- precedes(State, H). insert_pq(State, [...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

how to create a parser for search queries

for example i d need to create something like google search query parser to parse such expressions as: flying hiking or swiming -"**walking in boots **" **author:**hamish **author:**reid or ...

Applications of Unification?

What are the (practical) applications of Unification? Where it is actually being used in real world? I couldn t understand the whole idea of what it is really about and why it s considered as a part ...

热门标签