Pushed library(intercept)

Following the discussion on error handling, I added a Ciao compatible library providing intercept/3, send_signal/1 and a few more helpers in this family, some of which are not in the Ciao version. The extensions deal with providing arguments that are not copied and collecting signals in a list. The example is this:


For example, assume we parse a (large) file using a grammar (see phrase_from_file/3) that has some sort of record structure. What should we do with the recognised records? We can return them in a list, but if the input is large this is a huge overhead if the records are to be asserted or written to a file. Using this interface we can use

document -->
    record(Record),
    !,
    { send_signal(record(Record)) },
    document.
document -->
    [].

Given the above, we can assert all records into the database using the following query:

    ...,
    intercept(phrase_from_file(File, document),
              record(Record),
              assertz(Record)).

Or, we can collect all records in a list using intercept_all/4:

    ...,
    intercept_all(Record,
                  phrase_from_file(File, document), record(Record),
                  Records).

Comments are more than welcome. The interface may change based on further discussion.

7 Likes