Last active
May 25, 2022 01:35
-
-
Save Risto-Stevcev/6c01853a76702c10d66f9fea9e0a841c to your computer and use it in GitHub Desktop.
Convenient monotonic if
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
:- 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