From the discussion forum: (https://swi-prolog.discourse.group/t/pushed-library-intercept/1691)
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.