[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: Wujek Srujek
Subject: Re: prolog newbie - basic questions to a (self) written program
Date: Thu, 31 Jan 2013 22:44:22 +0100

On Thu, Jan 31, 2013 at 10:25 PM, Thierry Martinez <address@hidden> wrote:
> product([], []).
> product([L1Head|L1Tail], [L2First|[L2Second|L2Tail]]) :- L1Head is L2First *
> L2Second, iloczyn(L1Tail, L2Tail).

“iloczyn” should be “product”.

Yes, I was translating from polish and forgot about this one ;d
> 1. my first attempt in the first goal was:
> L1Head = L2First * L2Second, which apparently isn't true. Why doesn't it
> work? Why do I need the 'is' word there? It looks like prolog doesn't
> compute the value before unification, but why?

Because operators are term constructors, not arithmetic operations.
A * B is therefore the term with the root node “*” and the variables A
and B as sub-terms. The meaning of “multiplication” for the “*”
operator is given by the “is” predicate as in “X is Term”, which
evaluates its right operand Term as an arithmetic _expression_ and
unifies the result with its left operand X..

You may take a look to the answers of the following Stack Overflow question.

Thanks, especially for the link - the explanations there made it all very clear (and yours as well).
> 2. When I rewrite the program so:
> product([], []).
> product(L1, L2) :- [L1Head|L1Tail] = L1, [L2First|[L2Second|L2Tail]] = L2,
> L1Head is L2First * L2Second, product(L1Tail, L2Tail).

Since there are two clauses to define “product”, executing this
predicate introduces a choice between the two clauses: when you
execute product([], []), the first choice succeeds (therefore the
top-level says “true”), but there remains the second choice, so Prolog
asks you if you want to continue and explore it as well. Of course,
since in the second choice, L1 = [], it cannot succeed.

Why doesn't it do that for the case when the destructuring happens immediately in the 'parameters' list? 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?). If this is correct, is it a prolog, or gprolog feature?
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 carry the matching head constructor.

I will have to digest this one slowly ;d

Sorry for probably using wrong terms (parameters, body, execution, destructuring) but I have some programming background in various languages and that's what it is called there ;d I will get to the point where I can name things correctly.


reply via email to

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