Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!samsung!munnari.oz.au!goanna!ok From: ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) Newsgroups: comp.lang.prolog Subject: Re: Standards question: behavior of arg/3 Keywords: ANSI, standard Message-ID: <4018@goanna.cs.rmit.oz.au> Date: 19 Oct 90 02:46:32 GMT References: <9888@bunny.GTE.COM> <3992@goanna.cs.rmit.oz.au> <1920@tuvie> Organization: Comp Sci, RMIT, Melbourne, Australia Lines: 65 In article <1920@tuvie>, brock@tuvie (Inst.f.Prakt.Info 1802) writes: > In article <3992@goanna.cs.rmit.oz.au> ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) writes: > [...discussion of design principles for evaluable predicates...] > > ?- arg(N, [a,b,c], X). > >makes perfect sense and has two solutions > > N = 1, X = a ; > > N = 2, X = [b,c] . > Which Prolog systems have arg/3 defined in this way? Re-read that carefully. I said that the query makes sense and it has two solutions. I didn't say anything about implementations. I _have_ seen a Prolog system which did this, but can no longer recall which. Quintus Prolog has an operation that does this, it just calls it genarg/3 rather than arg/3. The thing which would make extending arg/3 tricky on the WAM is that WAM registers don't survive choice point creation, so that open-coding non-determinate evaluable predicates doesn't help a lot. If a compiler can do data-flow analysis, it can pick the determinate case a lot of the time, and I'm not even talking about inter-clause analysis. Consider first_N_args_the_same(N, T1, T2) :- integer(N), first_N_args_the_same(0, T1, T2, N). first_N_args_the_same(I, T1, T2, N) :- ( I < N -> J is I+1, arg(J, T1, X), % number(J) is *known* here arg(J, T2, X), % and here, so open-coding pays off first_N_args_the_same(J, T1, T2, N) ; I =:= N ). As for the benefit of making arg/3 logical, consider path_arg([], Term, Term). path_arg([Selector|Selectors], Term, SubTerm) :- arg(Selector, Term, Arg), path_arg(Selectors, Arg, SubTerm). If arg/3 can solve for its first argument, then so can path_arg/3. The wording in my 1984 document (BSI number PS/6) was very carefully chosen to insist that when the logic says there is a unique answer, an implementation must find it, but when there are many answers, an implementation may give up, provided it admits to the fault. For example, a query like times(X, Y, 1000000000000000000000000000) has a well defined finite solution set, but an implementation may not be prepared to go to the trouble of finding it. (The next revision of that document defined times/3 to work on rationals, not integers, so the solution set is infinite. This is just an example.) (For the record, when you ask ?- arg(N, [a,b,c], X). SICStus Prolog 0.7 fails quietly, and NU Prolog reports that the query ``floundered''.) -- Fear most of all to be in error. -- Kierkegaard, quoting Socrates.