# Re: [eclipse-users] simple_findall/2 example

From: Kish Shen <kish_at_...2...>
Date: Tue, 08 May 2007 17:56:55 +0100
```Hi Chuck,

Lutz, Charles D wrote:
>
> Hi ECLiPSe users,
>
> I’m trying to understand the working of the simple_findall/2 example
> shown in
>
> section 4.4.1 of the Tutorial (v. 21 Apr 07).
>
> Here is the simple_findall/2 example:
>
> %From section 4.4.1:
>
> simple_findall(Goal, Solutions) :-
>
> bag_create(Bag),
>
> (
>
> call(Goal),
>
> bag_enter(Bag, Goal),
>
> fail
>
> ;
>
> bag_dissolve(Bag, Solutions)
>
> ).
>
> Here is a run using it:
>
> [eclipse 9]: simple_findall(member(1, [2, 3, 5, 1, 3, 6, 1, 1]), Solns).
>
> Solns = [member(1, [2, 3, 5, 1, 3, 6, 1, 1]), member(1, [2, 3, 5, 1,
> 3, 6, 1, 1]
>
> ), member(1, [2, 3, 5, 1, 3, 6, 1, 1])]
>
> Yes (0.00s cpu)
>
> The interesting thing is how the solution set consists of the same
> thing three times,
>
> for the three times that the symbolic atom “1” appears in the list.
>
They are not technically the `same' solution, but the 3 different
solutions as you say, but as everything is ground, it is all apparently
the same.
>
> What I was unsure of was the use of the “fail” and how bag_enter/2
> collects the
>
> individual solutions.
>
> The top “half” of the parenthesized disjunction is a conjuction with
> one of the terms
>
> being “fail”. Shouldn’t the whole conjuction fail, then?
>
The fail causes the system to backtrack, and retry the most recent goal
that has alternatives (in this case call(Goal)), this obtains the next
solution, until no more solutions are found, when you then backtrack
further, and try the alternative in the conjunction, i.e. the
bag_dissolve/1 goal.

The tutorial is probably not the best place to learn Prolog if you are
completely new to Prolog -- you may want to read some Prolog textbook,
which covers Prolog in much more detail,
>
> I read the help on bag_enter/2 and to be honest I didn’t get much out
> of it. There is
>
> a main paragraph that discusses the unordered nature of the bag etc.
> It ends with a
>
> sentence that doesn’t “click” for me: “In particular, if the term
> contains variables,
>
> they lose their identity and are replaced with fresh ones.” What does
> this say? J
>
`fresh' variables means new instances of variables. Consider:

[eclipse 5]: X = foo(Y), Alias = X, Alias = foo(MyY).

X = foo(Y)
Y = Y
Alias = foo(Y)
MyY = Y
Yes (0.00s cpu)

In this case, MyY and Y are the same variable.

bag_enter/2 make a copy of the term, and unlike the unification example
above, any variables are replaced with new variables, so that

[eclipse 4]: bag_create(Bag),X = foo(Y), bag_enter(Bag, X),
bag_dissolve(Bag,[Alias]), Alias = foo(MyY).

Bag = 'HANDLE'(16'00000000)
X = foo(Y)
Y = Y
Alias = foo(MyY)
MyY = MyY
Yes (0.00s cpu)

MyY and Y are different variables in this case.

Cheers,

Kish
>
> Thanks in advance for any insight.
>
> **Chuck Lutz**
>
> Lockheed Martin
>
> Systems of Systems Engineering
>
> BMC4I Modeling and Simulation
>
> Moorestown, NJ
>
> (856)638-7234 (office)
>
> charles.d.lutz_at_...53... <mailto:charles.d.lutz@...53...>
>
> “A graphic is never an end in itself; it is a moment
>
> in the process of decision making.”
> - Jacques Bertin
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> ECLiPSe-Users mailing list
> ECLiPSe-Users_at_...2...
> http://www.crosscoreop.com/mailman/listinfo/eclipse-users
>
```
Received on Tue May 08 2007 - 17:57:14 CEST

This archive was generated by hypermail 2.3.0 : Sat Jul 20 2019 - 18:14:38 CEST