Previous Up Next

10.2  Bags

A bag is an anonymous object which can be used to store information across failures. A bag is unordered and untyped. Any ECLiPSe term can be stored in a bag. Bags are referred to by a handle. An empty bag is created using bag_create/1, data is stored in the bag by invoking bag_enter/2, and the stored data can be retrieved as a list with bag_retrieve/2 or bag_dissolve/2.

A typical application is the implementation of the findall/3 predicate or similar functionality. As opposed to the use of record/2 or assert/1, the solution using bags is more efficient, more robust, and trivially reentrant.

    simple_findall(Goal, Solutions) :-
        bag_create(Bag),
        (
            call(Goal),
            bag_enter(Bag, Goal),
            fail
        ;
            true
        ),
        bag_dissolve(Bag, Solutions).

Previous Up Next