1:- module(rdet, [
2 rdet/1 3]). 4
5:- use_module(library(error)). 6:- use_module(library(debug), [debug/3]). 7:- use_module(library(prolog_code), [pi_head/2]). 8:- use_module(library(prolog_wrap), [wrap_predicate/4]). 9:- meta_predicate(rdet(:)). 10
11rdet(PredicateIndicator):-
12 must_be(ground, PredicateIndicator),
13 ( ( PredicateIndicator = Module:Name/Arity
14 ; PredicateIndicator = Module:Name//Arity)
15 -> must_be(atom, Module),
16 must_be(atom, Name),
17 must_be(integer, Arity)
18 ; throw(error(invalid_rdet_pi(PredicateIndicator), _))),
19 debug(rdet, 'rdet: adding goal: ~w', [PredicateIndicator]),
20 pi_head(PredicateIndicator, Head),
21 wrap_predicate(Head, rdet_wrapper, Closure,
22 (Closure -> true ; throw(error(goal_failed(PredicateIndicator), _)))).
23
24:- multifile(prolog:message//1). 25
27
28prolog:message(error(goal_failed(Name/Arity), _)) -->
29 ['Goal ~w failed.'-[Name/Arity]].
30
31prolog:message(error(invalid_rdet_pi(PredicateIndicator), _)) -->
32 ['Invalid rdet annotation: ~w (not a predicate indicator).'-[PredicateIndicator]]