项目作者: rla

项目描述 :
Runtime determinacy checker for SWI-Prolog
高级语言: Prolog
项目地址: git://github.com/rla/rdet.git
创建时间: 2015-11-02T22:11:59Z
项目社区:https://github.com/rla/rdet

开源协议:MIT License

下载


Runtime non-failure checker for SWI-Prolog

Many predicates dealing with databases and external systems
are non-failure. These predicates have the following
properties:

  • They should not fail.
  • The first solution is the right solution.
  • Errors are handled through exceptions.

This library attempts to provide runtime check for these conditions.

Build Status

API

Use directive rdet/1 to annotate predicates that are supposed to be
deterministic:

  1. :- rdet(somepred/arity).

When annotated predicate call fails, an error is thrown:

  1. error(goal_failed(PredicateIndicator), _)

Example

  1. :- use_module(library(rdet)).
  2. :- rdet(insert_sort/2).
  3. :- rdet(i_sort/3).
  4. :- rdet(insert/3).
  5. insert_sort(List, Sorted):-
  6. i_sort(List, [], Sorted).
  7. i_sort([], Acc, Acc).
  8. i_sort([H|T], Acc, Sorted):-
  9. insert(H,Acc,NAcc),
  10. i_sort(T,NAcc,Sorted).
  11. insert(X,[Y|T],[Y|NT]):-
  12. X>Y,
  13. insert(X,T,NT).
  14. insert(X,[Y|T],[X,Y|T]):-
  15. X=<Y.
  16. insert(X,[a],[X]).

Running the sorting procedure will throw an error (the last clause of insert/3 is
intentionally made to fail with numbers):

  1. ?- insert_sort([2,4,1,3], Sorted).
  2. ERROR: Unhandled exception: Goal insert/3 failed.

How does it work?

The library uses SWI-Prolog built-in predicate wrap_predicate to insert
necessary checks.

Previous version of this library was using goal expansion.

Debugging

Enable rdet debug statements (before loading code to be enhanced):

  1. ?- debug(rdet).
  2. Warning: rdet: no matching debug topic (yet)
  3. true.
  4. ?- [insert].
  5. % rdet: adding goal: user:insert_sort/2
  6. % rdet: adding goal: user:i_sort/3
  7. % rdet: adding goal: user:insert/3
  8. true.

Module named user?

Module named user usually contains the code that is not explicitly
put into a module.

Why not a static system?

Good luck building a static analysis system that includes the following:

  • Instantiatedness (unbound, ground, partial)
  • Type information (list vs. atom, higher order types)
  • Module system support
  • Supports goal_expansion and other extensions
  • Higher order calls (maplist/call/etc)
  • Static exceptions
  • Different determinism modes: det/semidet/nondet/comitted choice
  • Determinism dependence on all previous

And has all of this optional: you can combine your code easily
with 3rd party untyped/unmoded libraries.

Installation

To install as a package:

  1. pack_install(rdet).

It requires a recent SWI-Prolog version 8.x or newer.

Running tests

In the package root, insert into swipl:

  1. [tests/tests].
  2. run_tests.

Or if you cloned the repo:

  1. make test

Bug reports/feature requests

Please send bug reports/feature request through the GitHub
project page.

License

The MIT license. See the LICENSE file.