- Documentation
- Reference manual
- The SWI-Prolog library
- library(aggregate): Aggregation operators on backtrackable predicates
- library(ansi_term): Print decorated text to ANSI consoles
- library(apply): Apply predicates on a list
- library(assoc): Association lists
- library(broadcast): Broadcast and receive event notifications
- library(charsio): I/O on Lists of Character Codes
- library(check): Consistency checking
- library(clpb): CLP(B): Constraint Logic Programming over Boolean Variables
- library(clpfd): CLP(FD): Constraint Logic Programming over Finite Domains
- library(clpqr): Constraint Logic Programming over Rationals and Reals
- library(csv): Process CSV (Comma-Separated Values) data
- library(dcg/basics): Various general DCG utilities
- library(dcg/high_order): High order grammar operations
- library(debug): Print debug messages and test assertions
- library(dicts): Dict utilities
- library(error): Error generating support
- library(gensym): Generate unique identifiers
- library(intercept): Intercept and signal interface
- library(iostream): Utilities to deal with streams
- library(listing): List programs and pretty print clauses
- library(lists): List Manipulation
- library(main): Provide entry point for scripts
- library(nb_set): Non-backtrackable set
- library(www_browser): Activating your Web-browser
- library(occurs): Finding and counting sub-terms
- library(option): Option list processing
- library(optparse): command line parsing
- library(ordsets): Ordered set manipulation
- library(pairs): Operations on key-value lists
- library(persistency): Provide persistent dynamic predicates
- library(pio): Pure I/O
- library(predicate_options): Declare option-processing of predicates
- library(prolog_jiti): Just In Time Indexing (JITI) utilities
- library(prolog_pack): A package manager for Prolog
- library(prolog_xref): Prolog cross-referencer data collection
- library(quasi_quotations): Define Quasi Quotation syntax
- library(random): Random numbers
- library(readutil): Read utilities
- library(record): Access named fields in a term
- library(registry): Manipulating the Windows registry
- library(settings): Setting management
- library(strings): String utilities
- library(simplex): Solve linear programming problems
- library(solution_sequences): Modify solution sequences
- library(tables): XSB interface to tables
- library(terms): Term manipulation
- library(thread): High level thread primitives
- library(thread_pool): Resource bounded thread management
- library(ugraphs): Unweighted Graphs
- library(url): Analysing and constructing URL
- library(varnumbers): Utilities for numbered terms
- library(yall): Lambda expressions
- The SWI-Prolog library
- Packages
- Reference manual
A.18 library(intercept): Intercept and signal interface
This library allows for creating an execution context (goal) which defines how calls to send_signal/1 are handled. This library is typically used to fetch values from the context or process results depending on the context.
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).
- intercept(:Goal, ?Ball, :Handler)
- Run Goal as call/1.
If somewhere during the execution of Goal
send_signal/1 is
called with a Signal that unifies with Ball, run
Handler and continue the execution.
This predicate is related to catch/3, but rather than aborting the execution of Goal and running Handler it continues the execution of Goal. This construct is also related to delimited continuations (see reset/3 and shift/1). It only covers one (common) use case for delimited continuations, but does so with a simpler interface, at lower overhead and without suffering from poor interaction with the cut.
Note that Ball and Handler are copied before calling the (copy) of Handler to avoid instantiation of Ball and/or Handler which can make a subsequent signal fail.
- See also
- intercept/4, reset/3, catch/4, broadcast_request/1.
- Compatibility
- Ciao
- intercept(:Goal, ?Ball, :Handler, +Arg)
- Similar to intercept/3,
but the copy of Handler is called as
call(Copy,Arg)
, which allows passing large context arguments or arguments subject to unification or destructive assignment. For example:?- intercept(send_signal(x), X, Y=X). true. ?- intercept(send_signal(x), X, =(X), Y). Y = x.
- intercept_all(+Template, :Goal, ?Ball, -List)
- True when List contains all instances of Template
that have been sent using send_signal/1
where the argument unifies with Ball. Note that backtracking
in Goal resets the List. For example, given
enum(I, Max) :- I =< Max, !, send_signal(emit(I)), I2 is I+1, enum(I2, Max). enum(_, _).
Consider the following queries
?- intercept_all(I, enum(1,6), emit(I), List). List = [1, 2, 3, 4, 5, 6]. ?- intercept_all(I, (between(1,3,Max),enum(1,Max)), emit(I), List). Max = 1, List = [1] ; Max = 2, List = [1, 2] ; Max = 3, List = [1, 2, 3].
- See also
- nb_intercept_all/4
- nb_intercept_all(+Template, :Goal, ?Ball, -List)
- As intercept_all/4,
but backtracing inside Goal does not reset List.
Consider this program and the subsequent queries
enum_b(F, T) :- forall(between(F, T, I), send_signal(emit(I))).
?- intercept_all(I, enum_b(1, 6), emit(I), List). List = []. ?- nb_intercept_all(I, enum_b(1, 6), emit(I), List). List = [1, 2, 3, 4, 5, 6].
- send_signal(+Signal)
- If this predicate is called from a sub-goal of intercept/3,
execute the associated Handler of the intercept/3
environment.
- Errors
unintercepted_signal(Signal)
if there is no matching intercept environment.
- send_silent_signal(+Signal)
- As send_signal/1, but succeed silently if there is no matching intercept environment.