Skip to content

Instantly share code, notes, and snippets.

@Risto-Stevcev
Last active May 25, 2022 01:35
Show Gist options
  • Save Risto-Stevcev/6c01853a76702c10d66f9fea9e0a841c to your computer and use it in GitHub Desktop.
Save Risto-Stevcev/6c01853a76702c10d66f9fea9e0a841c to your computer and use it in GitHub Desktop.
Convenient monotonic if
:- use_module(library(reif)).
% A convenient version that can "understand" the non-monotonic predicates
iff_(Cond, Success, Failure) :-
functor(Cond, _, Arity),
if_(
Arity = 3,
if_(Cond, Success, Failure),
if_([T]>> (call(Cond) -> T = true; T = false), Success, Failure)
).
% ?- iff_(ground(X), Result = true, Result = false).
% Result = false.
%
% ?- X = foo, iff_(ground(X), Result = true, Result = false).
% X = foo,
% Result = true.
% ?- member(1, [1,X]). % checks membership, but also eagerly unifies..
% true ;
% X = 1.
%
% ?- assert((
% memberd(X, [E|Es]) :-
% iff_( X = E
% , true
% , memberd(X, Es) )
% )).
% true.
%
% ?- memberd(1, [1,X]). % fully deterministic
% true.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment