Re: fund with findall
Ross Boylan <[email protected]>
| Newsgroups | gmane.comp.ai.prolog.swi |
|---|---|
| Message-ID | <1396306942.9858.25.camel@localhost> |
Thanks for your reply. On Tue, 2014-04-01 at 00:08 +0200, Feliks Kluzniak wrote: > This behaviour of findall/3 is intentional. > > What would be the expected final instantiations of Src and Dest? My initial concern was that I would just get an error and the query wouldn't run. Given that it ran, I wouldn't have been surprised if the unbound argument B ended up "randomly" bound; it's much better that it doesn't. In my real use I'll want to pull out one of the solutions and bind it to B, but that works normally (despite the use of Dest inside the findall), e.g.: funky(Src, Dest, Matches) :- findall(x(Src, Dest), foo(Src, Dest), Matches), select(x(Src, Dest), Matches, Rest), forall(member(x(S, D), Rest), D < Dest). That code only works for -Dest. A query on that ends up with Dest bound. Ross P.S. How do I find the meaning of some of the notation in the manual? For example, selectchk and forall are "semi-deterministic"; forall is defined with forall(:Cond, :Action). "4.1 Notation of Predicate Descriptions" indicates that ":" indicates a meta-argument, but I can't find the meaning of Cond, Action, or semi-deterministic there or in the glossary. Ah, I found "Semi-deterministic predicates are predicates that either fail or succeed exactly once and, for well behaved predicates, leave no choicepoints." in http://www.swi-prolog.org/pldoc/man?section=testsemidet through search on the site. > > When one thinks a little about how to implement findall/3 in a Prolog that does not support it directly (hint: use assert/retract and a failure-driven loop), it becomes obvious that it would be quite difficult to give these variables some sensible instantiations, even if one knew what to expect. > > Fortunately, as you point out, this property of findall/3 does not cause any problems. It is genuinely a feature, not a bug. :-) > > Hope this helps a little, > — Feliks > > On Mar 31, 2014, at 23:03, Ross Boylan <[email protected]> wrote: > > > Given > > foo(1, 2). > > foo(3, 2). > > foo(1, 10). > > > > funky(Src, Dest, Matches) :- > > findall(x(Src, Dest), foo(Src, Dest), Matches). > > > > Then funky(1, B, M) produces > > > > M = [x(1, 2), x(1, 10)]. > > > > I find this slightly disturbing because it means that Dest got bound to > > 2 separate values, while remaining unbound at the end. > > > > If anyone could help me understand a bit better what's going on, I would > > appreciate it. The behavior is very useful since it works with Src or > > Dest is instantiated, but I'm a little surprised it works. > > > > Thanks. > > Ross Boylan > > > > _______________________________________________ > > SWI-Prolog mailing list > > [email protected] > > https://lists.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog > _______________________________________________ SWI-Prolog mailing list [email protected] https://lists.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog