[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: prolog newbie - basic questions to a (self) written program

From: Thierry Martinez
Subject: Re: prolog newbie - basic questions to a (self) written program
Date: Thu, 31 Jan 2013 23:08:34 +0100

> product([L1Head|L1Tail], [L2First|[L2Second|L2Tail]]) :-

I forgot to mention that you may write more conveniently, for the
second argument:
[L2First, L2Second | L2Tail].

> Because prolog knows (after having
> destructured the lists) that it can't possibly succeed, and it doesn't even
> try to execute that rule, whereas for the second case (destructuring in the
> 'body' of the rule) it does execute it (as it can't know by just binding the
> parameters that it can't succeed?).

Yes, with some limitations: this is a one-step deconstruction applied
to the first argument only.
That is to say, only the head of the term in the first argument is
considered for indexing.
(Executing the goal product(X, []) will always create a choice point,
since the first argument is not instantiated; and if there were
several clauses discussing the value of the contents of the list,
there will as many choice points as clauses as well, since only the
head of the term, that is to say the list constructor, is

> If this is correct, is it a prolog, or gprolog feature?

It is a usual feature shared by most Prolog implementations, known as
argument indexing, that I tried to explain in the paragraph below, but
perhaps you have better to experiment to understand the details...

>> However, GNU Prolog does “first argument head indexing”: clauses whose
>> heads have a term as first argument are compiled with a more efficient
>> selection than trying all the clauses.  When the first argument of the
>> call is instantiated, this selection directly branches to the clauses
>> whose first term carr[ies] the matching head constructor.

> Sorry for probably using wrong terms (parameters, body, execution, 
> destructuring)

As far as I can say, these terms are fine for me!


reply via email to

[Prev in Thread] Current Thread [Next in Thread]