This library allows adding wrappers to predicates. The notion of wrappers is known in various languages under several names. For example Logtalk knows these as before methods or after methods and Python has decorators. A SWI-Prolog wrapper is a body term that normally calls the original wrapped definition somewhere.
- wrap_predicate(:Head, +Name, -Wrapped, +Body) is det
- Wrap the predicate referenced by Head using Body. Subsequent calls
to Head call the given Body term. Body may call the original
definition through Wrapped. Wrapped is a term of the shape below,
where Closure is an opaque blob.
Name names the wrapper for inspection using predicate_property/2 or deletion using unwrap_predicate/2. If Head has a wrapper with Name the Body of the existing wrapper is updated without changing the order of the registered wrappers. The same predicate may be wrapped multiple times. Multiple wrappers are executed starting with the last registered (outermost).
The predicate referenced by Head does not need to be defined at the moment the wrapper is installed. If Head is undefined, the predicate is created instead of searched for using e.g., the auto loader.
- unwrap_predicate(:PI, ?Name) is semidet
- Remove the outermost wrapper whose name unifies with Name. Fails if no matching wrapper exists.
- current_predicate_wrapper(:Head, -Name, -Wrapped, -Body) is nondet
- True if Head is wrapped with Body. The arguments are compatible with
wrap_predicate/4 such that the result may be used to re-create the
Wrappers are enumerated starting with the first registered (innermost) wrapper.