Mombu the Programming Forum sponsored links

Go Back   Mombu the Programming Forum > Programming > Prolog goals order
User Name
Password
REGISTER NOW! Mark Forums Read

sponsored links


Reply
 
1 5th April 00:30
mauro dinuzzo
External User
 
Posts: 1
Default Prolog goals order



Hi All!

I wish to submit an issue to the newsgroup. Perhaps it is old and not all
would be intereseted in.
However, it deals with the order of the goals in a clause. I will consider
mathematics for simplicity, but I think it could be applied whereever.
Prolog system reads goals in sequence (of course), also when it attempts to
solve a set of goals. The only way to achieve the maximum knowledge over the
given set of goals is to delay some of them. This is what happens by means
of attributed variables.
As an example, the goal:
?- X+1>10, read(X).
raises an instantiation error. (we have the mind full of examples like
this!)

My question is: "why Prolog does not change itself to overcome this


http://www.prologonlinereference.org/archives/freeze.zip

Feel free to modify it and to discuss here.
Thank you.

/\/\
  Reply With Quote


  sponsored links


2 5th April 09:52
peter wing larsen
External User
 
Posts: 1
Default Prolog goals order



This is also something I have thought about as a novice prolog learner. Is
there any reason why prolog is this way?
  Reply With Quote
3 5th April 09:52
jan wielemaker
External User
 
Posts: 1
Default Prolog goals order


I think the answer is that Prolog is not a theorem prover, but a
programming language. If I write write(a), write(b) I want "ab" and
not "ba", so order matters. If the system starts reordering things,
you as a programmer quickly do not feel in control any longer. If
you have two goals and you cannot put them in the right order because
it is depending on the runtime behaviour there are several options.
Use conditional code as in (Cond -> a,b ; b,a) or use coroutining:
?- freeze(X, X+1>10), read(X).
|: 10.

X = 10

Yes
2 ?-

Coroutining is not part of standard Prolog, but an increasing number
provide it. See also when/2 and dif/2. Note however this is a lot
more machinery, making it slower as well as more difficult to
understand.

--- Jan
  Reply With Quote
4 5th April 09:52
peter wing larsen
External User
 
Posts: 1
Default Prolog goals order


[snip]

Thanks for the explanation. That makes sense.
  Reply With Quote
5 5th April 14:14
greg buchholz
External User
 
Posts: 1
Default Prolog goals order


FWIW, I think the Mercury langauge does exactly that. From
http://doc.rz.ifi.lmu.de/programming/mercury/transition_guide.html

"the Mercury compiler automatically reorders conjunctions so that all
consumers of a variable come after the producer"

Greg Buchholz
  Reply With Quote
6 5th April 14:14
bart demoen
External User
 
Posts: 1
Default Prolog goals order


The Mercury compiler reorders goals in a conjunction statically and
within a clause. That's cool, but it is also quite limited.
It relies on your declarations (or what it can infer from your
declarations+program). Dynamic reordering and reordering beyond the
clause scope is what one would really want - or what I guess Mauro would
like. It is possible, but as Jan has pointed out, it comes at a
performance price (especially when you don't need it). There is another
cost: if two (or more) goals are ready to execute at the same time, you
(as a programmer) usually have no control over the order in which they are
executed. This means you might lose control over the complexity of your
program. For programmers, that's unacceptable. For a specification, that
is fine. Maybe some time in the future, computer science bridges the gap
between programming and specifying. Until then, entre les deux notre coeur
se balance: try a bit of Prolog, try a bit of Answer Set Programming. And
tell us about your experience.

Cheers

Bart Demoen
  Reply With Quote
7 5th April 14:14
bart demoen
External User
 
Posts: 1
Default Prolog goals order


Your PR talk rocks ...

(nil)~/A/freeze# wc freeze.pl
85 321 3621 freeze.pl

:-)


Anyway, you are doing this only for the builtin arithmetic, unifyability
and term equality. Why not for user defined predicates ?

Cheers

Bart Demoen
  Reply With Quote


  sponsored links


Reply


Thread Tools
Display Modes




Copyright © 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666