Re: dynamic programming

"Richard A. O'Keefe" <[email protected]>
Newsgroups gmane.comp.ai.prolog.swi
Message-ID <[email protected]>
On 5/09/2013, at 5:09 AM, Kilián Imre wrote:
> 
> but I have reached another problem regarding dynamic programming...

I wish you wouldn't call it "dynamic programming".  Whenever I see that
phrase I think it has its usual reference, see
http://en.wikipedia.org/wiki/Dynamic_programming

"Using dynamic predicates" might confuse me less.

> modelDriven: 67 ?- assert(ontology:('Animal'(X,X):-a,b)).
> 
> gives:
> 
> 'Animal'(A, A) :-
>    ontology:
>    (   a,
>        b
>    ).

Actually, if I copy your example and paste it into a running SWI
Prolog I don't get that, I get

?- assert(ontology:('Animal'(X,X):-a,b)).
true.

?- listing(ontology:'Animal'/2).
:- dynamic'Animal'/2.

'Animal'(A, A) :-
        a,
        b.

true.

Can we possibly be using different versions of SWI Prolog?
I'm using 6.5.0.

> To my understanding it has compiled ( , ) to a built-in predicate call...

No, it hasn't.

	ontology:(a, b)
and	ontology:a, ontology:b

have the same effect and I'd expect them to compile to the same code.
(Certainly the Quintus compiler would have generated exactly the same
code for them.)  And if either of those forms is inside a clause which
is inside the ontology: module, they would have the same effect as
(and generate the same code as)

	a, b.

> I can't find out what is the difference between the two... In what circumstance does it compile conjunction directly, and when does it compile to a predicate call?

You haven't been seeing the effects of *compilation* at all.

		source term
		    |
		    |<-- assert/1
		    |
		    v
		stored clause
		   /|\
     clause/2---->/ | \<---compilation
		 /  |  \
	 source'    |   executable code
		    |
      debugger----->|
                 interpretation

The "assert" predicates (and for that matter, the "record" ones)
store what they are given in some kind of internal form.  There
may be just one (Bill Clocksin had a Prolog with a single
"compiled" form that could be executed one way to run or another
way to regenerate equivalent source) or there might be several
(something tree-like, threaded code, native code).

Quintus experimented with decompilation, but settled on a
tree-like form and a threaded-code form.

Different source forms may yield exactly the same executable
form.  For example,
	p :- q.
	p :- true, q.
	p :- q, true.
	p :- (q, true), true.
might result in the *same* executable code; you cannot rely on
sticking ", true" at the end of a clause to prevent tail recursion.
They might at the same time be reported back without simplification.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.