Newsgroups: comp.lang.prolog
Path: cantaloupe.srv.cs.cmu.edu!das-news2.harvard.edu!news.dfci.harvard.edu!camelot.ccs.neu.edu!nntp.neu.edu!grapevine.lcs.mit.edu!bloom-beacon.mit.edu!news.mathworks.com!cam-news-hub1.bbnplanet.com!news.bbnplanet.com!su-news-hub1.bbnplanet.com!newsout1.alt.net!news1.alt.net!news.u.washington.edu!uw-beaver!uw-coco!news-wa16.mdd.comm.mot.com!lego.wes.mot.com!mothost.mot.com!schbbs!anewman
From: anewman@epidigm.geg.mot.com (M. Alan Newman)
Subject: Re: Minimum value of predicate?
Content-Type: text/plain; charset=ISO-8859-1
Organization: Motorola
Date: Tue, 07 Jan 1997 07:31:34 -0700
Message-ID: <anewman-ya023180000701970731340001@129.188.137.103>
Mime-Version: 1.0
X-Newsreader: Yet Another NewsWatcher 2.3.1
Content-Transfer-Encoding: 8bit
References: <5alj3f$kps@news.huji.ac.il> <s18+GjAAqtzyEw$H@maproom.demon.co.uk> <32CFB772.1805@dial.pipex.com> <5aqjft$76g@lyra.csx.cam.ac.uk> <slrn5d2pm9.8en.dsmith@rose.cs.waikato.ac.nz> <5aso1l$dhd$1@goanna.cs.rmit.edu.au>
Sender: news@schbbs.mot.com (SCHBBS News Account)
Nntp-Posting-Host: 137.124.91.130
Lines: 51

In article <5aso1l$dhd$1@goanna.cs.rmit.edu.au>, ok@goanna.cs.rmit.edu.au
(Richard A. O'Keefe) wrote:

> dsmith@cs.waikato.ac.nz (Don Smith) writes:
> 
> >Too bad it needs to collect all the solutions in a list just to find
> >the minimum element!
> 
> It's not clear that this is a real disadvantage.
> 
>   With encapsulated search (as in Oz) or engines
> >(as in BinProlog) one could do it in almost constant space (and a lot
> >of overhead?).
> 
> It is TRIVIAL to do using the same technique as findall:
> 
>         find_min(X, Query) :-
>             asserta('find min'(_)),
>             (   call(Query),
>                 'find min update'(X),
>                 fail
>             ;   retract('find min'(Y))
>             ),
>             !,
>             nonvar(Y),
>             X = Y.
> 
>         'find min update'(X) :-
>             retract('find min'(Y)),
>             (  nonvar(Y), Y < X -> Z = Y ; Z = X ),
>             !,
>             asserta('find min'(Z)).
> 

Just a minor tweak of 'find min update'/1 to avoid uninstantiated query
results, and for efficiency by avoiding retraction and reassertion of an
unchanged 'find min'/1 value (although less efficient if query results
occur in sorted order):

   'find min update'(X) :-
         var(X) , !
      ;
         'find min'(Y) , Y =< X , !
      ;
         retract('find min'(_)) ,
         asserta('find min'(X)) .

-- 
M. Alan Newman (P20582@email.mot.com)
Motorola, Scottsdale, Arizona
Speaking for myself.
