Did you know ... | Search Documentation: |
Packs (add-ons) for SWI-Prolog |
Title: | Condition system a la Common Lisp |
---|---|
Rating: | Not rated. Create the first rating! |
Latest version: | 0.1.1 |
SHA1 sum: | 6f1204f9185773489da29ce66b40534c7c0fd32a |
Author: | Michael Hendricks <michael@ndrix.org> |
Maintainer: | Michael Hendricks <michael@ndrix.org> |
Packager: | Michael Hendricks <michael@ndrix.org> |
Home page: | http://packs.ndrix.com/condition/index.html |
Download URL: | http://packs.ndrix.com/condition/condition-0.1.1.tgz |
Requires: | lambda |
No reviews. Create the first review!.
Version | SHA1 | #Downloads | URL |
---|---|---|---|
0.0.1 | d4c523a56c7ab0a2c146006381eefa592dbc888c | 5 | http://packs.ndrix.com/condition/condition-0.0.1.tgz |
0.0.2 | 9f0f925413ced10dd0402196b393ead04ba49ad8 | 6 | http://packs.ndrix.com/condition/condition-0.0.2.tgz |
0.1.0 | 565a04d68d5606c31d541b6939c333ca222252d1 | 1 | http://packs.ndrix.com/condition/condition-0.1.0.tgz |
0.1.1 | 6f1204f9185773489da29ce66b40534c7c0fd32a | 57 | http://packs.ndrix.com/condition/condition-0.1.1.tgz |
:- use_module(library(condition)). main :- handle(stuff, oops, warn). stuff :- writeln('Doing something useful...'), % oh no, a problem! signal(oops, Restart), ( Restart = ignore -> true ; Restart = warn -> print_message(warning, oops) ; Restart = error -> print_message(error, oops) ; % otherwise -> type_error(oneof([ignore,warn,error]), Restart) ).
A condition system is a mechanism for working with software errors. The idea was popularized by Common Lisp. Here's an analogy, modified from that Kent Pitman link, explaining roughly how it works:
Think of the process of signaling and handling as analogous to finding a fork in a road that you do not commonly travel. You don't know which way to go, so you make known your dilemma (signal a condition). Various sources of wisdom (handlers) present themselves, and you consult each, placing your trust in them because you have no special knowledge yourself of what to do. Not all sources of wisdom are experts on every topic, so some may decline to help, some may disagree. Using those sources of wisdom, you act. The situation has been handled.
In the following description, condition system terminology is highlighted.
When a predicate encounters a problematic situation and doesn't know how to proceed, it can signal a condition (instead of throwing an exception). A handler higher up in the call stack can respond to this condition with a restart. Unlike with exceptions, the call stack is never unwound so the precise context of the error is preserved in case it's needed for continuing the computation. A condition communicates information from a signaler to a handler. Based on this information, the handler sends a restart which communicates in the opposite direction.
In the Synopsis above, stuff/0 encounters a problem. It indicates this problem by sending the condition oops/0, which could have been any term. Fortunately, main/0 has a handler for that condition. In this case, it responds by sending a warn/0 restart. Again, the restart could have been any term. stuff/0 continues based on the restart value.
It's good practice to document which conditions a predicate might signal as well as the restarts that it understands. For publicly accessible predicates, conditions and restarts should be considered part of the API.
This library departs from Common Lisp by allowing all handlers a chance to respond to a condition, not just the innermost, matching handler. The innermost handler gets the first attempt, but on backtracking signal/2 iterates all restarts. One could act on just the first, the most popular, try them all, etc.
The first time one hears about a condition system, it's not apparent how it might be used. Here are some examples to help.
Of course, there are dozens of ways to address these same use cases without a condition system. They often involve passing configuration values or callbacks or using multifile
predicates.
A condition system decouples all participating software components by agreeing on a protocol by which they may communicate. Configuration and callbacks don't have to be passed down the call stack through arbitrary intermediaries. It doesn't have to rely on an HTTP library, for example, to implement the exact backoff strategy that's needed. A library provides some primitive conditions and restarts. Its users decide how to compose those as they see fit.
See History.md file.
Using SWI-Prolog 6.3 or later:
?- pack_install(condition).
This module uses semantic versioning.
Source code available and pull requests accepted at http://github.com/mndrix/condition
Pack contains 5 files holding a total of 10.6K bytes.