Previous Up Next

10.3  Records

A record is an anonymous or named object which can be used to store information across failures. A typical application is the collection of multiples solutions during backtracking. Another application is communication between engines/threads.

A record is a list of (copies of) arbitrary terms. New terms can be added at either end of the list (recorda/2, recordz/2), and list elements can be removed from any position in the list (erase/2). Elements can be retrieved individually (recorded/2) or collectively (recorded_list/2).

Records come in two flavours: anonymous records are created with record_create/1 and referred to by handle, while named records are created with a record/1 declaration and referred to by their name within a module. If possible, anonymous records should be preferred because they make it easier to write robust, reentrant code. For example, an anonymous record automatically disappears when the system backtracks over its creation, or when the store handle gets garbage collected. Named records, on the other hand, must be explicitly destroyed in order to free the associated memory.

A typical application is the implementation of the findall/3 predicate or similar functionality:

    simple_findall(Goal, Solutions) :-
        record_create(Record),
        (
            call(Goal),
            recordz(Record, Goal),
            fail
        ;
            true
        ),
        recorded_list(Record, Solutions).

For an example involving thread communication, see record_wait_append/4.


Previous Up Next