Article View: comp.lang.prolog
Article #1130Re: Deterministic predicate question
From: jgarland
Date: Tue, 28 Aug 1990 21:28
Date: Tue, 28 Aug 1990 21:28
74 lines
2902 bytes
2902 bytes
In article <3631@goanna.cs.rmit.oz.au>, ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) writes:
> In article <90239.224751F0O@psuvm.psu.edu>, F0O@psuvm.psu.edu writes:
>> I'm curious as to why length is deterministic where both of the
>> build_board predicates are not. Also, is there a way to make one of the
>> build_board predicates deterministic, without using the cut?
>
> It would help if you indicated the language you are using.
> From the extremely confusing use of '=' for arithmetic, I
> infer this is Turbo/PDC ``Prolog''.
!!!!!
Don't be so cute, you *know* it is--you've seen, and answered, Tim's queries
before.
>> build_board1(10).
>> build_board1(SquareNum) :-
>> NextSquare = SquareNum + 1,
>> build_board1(NextSquare),
>> assert(board(SquareNum," ")).
>
> This predicate is non-determinate for the very simple reason that
> any query (such as ?- build_board1(10)) which matches the first
> clause will *also* match the second clause. That's what it _means_
> to be non-determinate. If you call
>
> ?- build_board1(0), fail.
^^^^^^^^^^^^^^^^^^^^^^^^^
I'm not sure exactly what T/PDC Prolog will do here...I intend to
check it out the second I'm free...it's an interesting point. I *think* it
will try the first clause, then the next and then quit, but check it
out for yourself.
> or however you do that in Turbo/PDC Prolog, it will loop forever.
>
> The answer is simple: say what you *really* mean.
^^^^^^^^^^^^^^^^^^^^^^^^^^
About the best Prolog advice ever. Or turn it around: Prolog does
what you *really* say...often in a way that is non-obvious to
procedural thinking.
Anyway, this is a perfect place to use a cut. You're essentially
trying to implement a while-do loop. And given the order of the
clauses, tail recursion optimization takes care of any stack problems.
[In terms of the basic loop, that is. Not asserts.]
********listing******************************************
predicates
while_do(integer)
clauses
while_do(0) :- !.
while_do(X) :-
write("Countdown: ",X),nl,
X1=X-1,
while_do(X1).
goal
while_do(10).
***********end of listing***************************************
Your stuff is looking much more elegant.
JG
jgarland@mun Bitnet
jgarland@kean.ucs.mun.ca Internet
Technical Details
Message-ID:
<129469@kean.ucs.mun.ca>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!stretch.cs.mun.ca!leif!jgarlandReferences:
<90239.224751F0O@psuvm.psu.edu> <3631@goanna.cs.rmit.oz.au>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!stretch.cs.mun.ca!leif!jgarland From: jgarland@kean.ucs.mun.ca Newsgroups: comp.lang.prolog Subject: Re: Deterministic predicate question Message-ID: <129469@kean.ucs.mun.ca> Date: 28 Aug 90 21:28:04 GMT References: <90239.224751F0O@psuvm.psu.edu> <3631@goanna.cs.rmit.oz.au> Organization: Memorial University. St.John's Nfld, Canada Lines: 74