[ Non-logical Variables, Arrays, Bags, Shelves and Stores | Reference Manual | Alphabetic Index ]

bag_create(-BagHandle)

Create a bag object which can store data across failures
BagHandle
A free variable

Description

This creates an anonymous bag object which can be used to store information across failures. A typical application is the implementation of the findall/3 predicate or similar functionality. Bags are similar to records, with two differences: First, a bag is considered unordered, so one should not expect the bag content to indicate the order in which information was entered. Second, bags are referred to by handle, not by name, so they make it much easier to write robust, reentrant code.

Modes and Determinism

Exceptions

(5) type error
BagHandle is not a variable

Examples

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

See Also

bag_erase / 1, bag_enter / 2, bag_count / 2, bag_dissolve / 2, bag_retrieve / 2, bag_abolish / 1