Did you know ... Search Documentation:
Packs (add-ons) for SWI-Prolog

Package "memo"

Title:Persistent memoisation of deterministic predicates
Rating:Not rated. Create the first rating!
Latest version:0.6.1
SHA1 sum:be8f8a90ab523862cbfefc3130859e10fa6caa4e
Author:Samer Abdallah <s.abdallah@ucl.ac.uk>
Download URL:https://raw.githubusercontent.com/samer--/prolog/master/memo/release/memo-0.6.1.tgz
Requires:genutils
typedef

Reviews

No reviews. Create the first review!.

Details by download location

VersionSHA1#DownloadsURL
0.6.1be8f8a90ab523862cbfefc3130859e10fa6caa4e46https://raw.githubusercontent.com/samer--/prolog/master/memo/release/memo-0.6.1.tgz
0.6.0647bb7f44f99d3d7d363fd926ec51648f636978c2https://raw.githubusercontent.com/samer--/prolog/master/memo/release/memo-0.6.0.tgz
0.5.04e0b5e674e9f83b2e771c7b626f058a8ed8902a415https://raw.githubusercontent.com/samer--/prolog/master/memo/release/memo-0.5.0.tgz
0.4.1d116c038741e9e1770033d34c96567fe26e42dca9https://raw.githubusercontent.com/samer--/prolog/master/memo/release/memo-0.4.1.tgz
0.4.0eb10eb8b4ae8a2ec6aa571385550bca750c39d4b2https://raw.githubusercontent.com/samer--/prolog/master/memo/release/memo-0.4.0.tgz
0.3.714a05de4523ee3fd6e4b386c1abc217bc61266441https://raw.githubusercontent.com/samer--/prolog/master/memo/release/memo-0.3.7.tgz
0.3.63dcbe6e5c88e36ac1a5c4a6c46faace685a6cb2c1https://github.com/samer--/prolog/raw/master/memo/release/memo-0.3.6.tgz
0.3.5fb7983c1127f4890ace3f3aa29058ba33f1c65bd2https://github.com/samer--/prolog/raw/master/memo/release/memo-0.3.5.tgz
0.3.3017ff2b7a82b5a0873d423b1c88ab4cf28415b4d2https://github.com/samer--/prolog/raw/master/memo/release/memo-0.3.3.tgz
0.3.2f283672cf6764bd51bd5602bb26b2eba336d8dad2https://github.com/samer--/prolog/raw/master/memo/release/memo-0.3.2.tgz
0.3.1f3221fca2e21e7b833b49a8417f93fff77ef72121https://github.com/samer--/prolog/raw/master/memo/release/memo-0.3.1.tgz
0.3.04c47821a6e3d116b2edd125b333531214157b24c2https://github.com/samer--/prolog/raw/master/memo/release/memo-0.3.0.tgz
0.2.02807cbef762b4fa323df82d88f5d2d36c50e22a21https://github.com/samer--/prolog/raw/master/memo/release/memo-0.2.0.tgz
0.1.5a2023d8254e40fe307c175c2daa4d13f96fe4e256https://github.com/samer--/prolog/raw/master/memo/release/memo-0.1.5.tgz
0.1.4996d5115e2a07a2476e06d9ff62ca2873d6763a61http://code.soundsoftware.ac.uk/projects/memo/repository/raw/release/memo-0.1.4.tgz
0.1.365179b6f82946d7951b7e92b8bcde9f43f77be4b2http://code.soundsoftware.ac.uk/projects/memo/repository/raw/release/memo-0.1.3.tgz
0.1.21643decbbfec19dee5e6a41cb3ba3d2d20d951751http://code.soundsoftware.ac.uk/projects/memo/repository/raw/release/memo-0.1.2.tgz
0.17b164a3ee4e3edd0dc3779d8b7e11f9b473b9ddf1http://code.soundsoftware.ac.uk/projects/memo/repository/raw/release/memo-0.1.tgz

This package provides library(memo), a tool for memoising deterministic predicates in memory or persistently on the file system. It also includes library(typedef), which provides support for defining algebraic data types that hook into the type-checking framework of library(error).

See library(memo) module header for more information.

Quick test

This is a small test file that you can use to check if it works.

:- use_module(library(memo)).
:- use_module(library(typedef)).

:- type maybe(A) ---> nothing; just(A).
:- volatile_memo sqrt1(+float, -maybe(float)).
:- persistent_memo sqrt2( +float, -float).

sqrt1(X,just(Y)) :- X>=0, !, Y is sqrt(X).
sqrt1(_,nothing).

% this will throw an exception if X<0
sqrt2(X,Y) :- Y is sqrt(X).

:- initialization
                db_attach('test.db',[]),  % attach database for persistent memo
                debug(memo).              % tocheck memoisation operations

Then, you can make queries such as

?- sqrt1(16,X).   % leads to type error (16 is not a float)
?- sqrt1(16.0,X). % computes X=4.0
?- sqrt1(16.0,X). % looks up old result, X=4.0
?- sqrt1(-4.0,X). % fails
?- sqrt2(9.0,X).  % computes X=3.0 and stores persistently
?- sqrt2(-4.0,X). % records exception and then rethrow is
?- sqrt2(-4.0,X). % rethrows recorded exception

The debug messages should indicate that repeated computations are being avoided, even for failing goals, like sqrt1(-4.0,X), and computations that throw exceptions, like sqrt2(-4.0,X).

Changes in v0.5

memo/1 and memo/2 are now steadfast with respect to output arguments in the
memoised goal. This means that instead of throwing an exception if an
output argument is nonvar on entry, the computation proceeeds _exactly_ as
if it were unbound, followed by a final semi-deterministic unification.
Any side-effects of calling the memoised computation (ie the memoisation of
the result) goes ahead even if the output arguments do not unify and the
call as a whole fails.

An alternative approach would be to abort the memoisation if the output
values do not unify with those provided on input.

Changes in v0.4

memo/1 now behaves differently if the memoised predicate fails or throws
an exception, in which case the result will now NOT be stored. This is probably
the more useful behaviour on the whole, because failures and exceptions
are more likely to be due to bugs rather than any inherent partiality in the
function being computed.

Changes in v0.3

The library now has a notion of 'current evaluation mode', which applies when
memoised predicates are called directly by their original names. In the previous
version, this resulted in a call to memo/1, i.e., strict mode checking is done,
previously stored computations are checked, and a new computation done if
necessary and the result stored. In v0.3, the current mode can be 'memo',
'browse' or 'compute', which result in calls to memo/1, browse/1 or compute/1
respectively. The initial mode is 'memo', retaining backwards compatibility with
v0.2. The only way to affect the mode is to use call_with_mode(Mode,Goal), which
calls an arbitrary goal in an environment with the evaluation mode set to Mode.
There is no way to globally set the evaluation-mode -- this is an intentional
design decision to ensure that any goal given at the top level ?- prompt always
behaves the same way.

Changes in v0.2

Contents of pack "memo"

Pack contains 5 files holding a total of 66.6K bytes.