Did you know ... | Search Documentation: |
Packs (add-ons) for SWI-Prolog |
Title: | Avoid instantiation errors for built-in predicates |
---|---|
Rating: | Not rated. Create the first rating! |
Latest version: | 0.3.3 |
SHA1 sum: | b8ce040f5093ddbf2d069e0cbaf20072b41a1017 |
Author: | Michael Hendricks <michael@ndrix.org> |
Maintainer: | Michael Hendricks <michael@ndrix.org> |
Packager: | Michael Hendricks <michael@ndrix.org> |
Home page: | https://github.com/mndrix/delay |
Download URL: | http://storage.googleapis.com/packs.ndrix.com/delay/delay-v0.3.3.zip |
No reviews. Create the first review!.
Version | SHA1 | #Downloads | URL |
---|---|---|---|
0.3.3 | b8ce040f5093ddbf2d069e0cbaf20072b41a1017 | 56 | http://storage.googleapis.com/packs.ndrix.com/delay/delay-0.3.3.zip |
https://github.com/mndrix/delay/archive/v0.3.3.zip | |||
0.3.2 | caf420ceee71211e6a11ed0f26479eb25d7d377f | 1 | https://github.com/mndrix/delay/archive/v0.3.2.zip |
0.2.1 | cd1b39a214c3cfc05f401ff53f114b8a4ce7a5dd | 7 | http://packs.ndrix.com/delay/delay-0.2.1.tgz |
0.2.0 | 79d4cc57c7ae944cc9dd68445ec57e0111dbcb0c | 12 | http://packs.ndrix.com/delay/delay-0.2.0.tgz |
0.1.1 | 7b81040846f6f9173106e83b627e21bc5045a2c7 | 1 | http://packs.ndrix.com/delay/delay-0.1.1.tgz |
0.1.0 | b132527cff94f8f98607463884a019692a4148cb | 3 | http://packs.ndrix.com/delay/delay-0.1.0.tgz |
0.0.2 | 14b30256169f0e3f8f03d09577b72e6d8b485cbf | 3 | http://packs.ndrix.com/delay/delay-0.0.2.tgz |
0.0.1 | 62f899f3bc50543c0a2765e8f2dcadaf3ae920d7 | 1 | http://packs.ndrix.com/delay/delay-0.0.1.tgz |
:- use_module(library(delay)). main :- % describe a relationship between variables. % none of them need values yet. delay(atom_codes(Atom,Codes)), % now we can bind a value... Codes = "hello", % and the other one receives a value too Atom == hello.
Many Prolog predicates (succ/2, plus/3, atom_codes/2, etc) describe a relationship between values. However, if called with variables whose values are not yet known, they throw a "not sufficiently instantiated" exception. Unfortunately, this imposes a strict order of execution and often requires one to write additional clauses that differ only in goal order.
By wrapping these predicates in delay/1, they become purely declarative. Their execution order no longer matters and happens as soon as it reasonably can.
For example, imagine a simple DCG for saying hello. We want the subject of our greeting to be specified as an atom. So we might write
hello(Whom) --> { atom_codes(Whom, Codes) }, "Hello, ", string(Codes).
That works fine if we run it forward
?- phrase(hello(john), X). X = "Hello, john".
but not if we run it backwards
?- phrase(hello(Whom), "Hello, john"). ERROR: atom_codes/2: Arguments are not sufficiently instantiated
Instead of writing a second clause for hello//1, in which the goals are reordered, we use delay/1
hello(Whom) --> { delay(atom_codes(Whom, Codes)) }, "Hello, ", string(Codes).
and now we get what we wanted
?- phrase(hello(Whom), "Hello, john"). Whom = john ; false.
library(delay)
comes with support for some built-in predicates. To
add support other predicates, define clauses for the multifile
predicate mode/1.
For example, if the module utils
exports helpful/2 and that
predicate requires at least one of its arguments to be ground, you can
add delay support with
:- multifile delay:mode/1. delay:mode(utils:helpful(ground,_)). delay:mode(utils:helpful(_,ground)).
library(delay)
first looks for mode information under the calling
module's name. If none is found, it looks for it under the exporting
module's name. The example above defines mode information for all
users of helpful/2. If your module mine
imports helpful/2
and you only want the mode declarations to have effect locally, you
can do this instead:
:- multifile delay:mode/1. delay:mode(mine:helpful(ground,_)). delay:mode(mine:helpful(_,ground)).
Don't worry about adding mode/1 declarations for predicates that already have them. Redundant mode declarations are ignored.
If you create mode declarations for built-in predicates, please consider contributing them as a pull request to this library (see below). That way, other users can benefit too.
When using delay/1, be certain to think carefully about all surrounding cuts. Delaying over a cut changes the semantics.
This library might someday offer development-time warnings to bring these to your attention. However, such a tool doesn't now exist and is unlikely to discover all circumstances in which delay and cut interact unexpectedly.
SWI-Prolog's SICSTUS compatibility layer includes the block/1 directive. That directive fills a niche very similar to delay/1.
Here are some advantages of delay/1 over block/1:
One disadvantage is that delay/1 is not compatible with existing SICSTUS code.
Using SWI-Prolog 6.3 or later:
?- pack_install(delay).
This module uses semantic versioning.
Source code is available at http://github.com/mndrix/delay
Pack contains 11 files holding a total of 15.1K bytes.