Search code examples
prolog

True if for every true value of a fact another fact is also true


I tried this as hello, but I think it doesn't work.

kng_bck(1).
kng_bck(2).
kng_bck(3).

:- dynamic(p/1).

hello :- kng_bck(X), p(X).

% true if p(1). p(2). and p(3). are all defined, 
% that is for every true value of kng_bck(X), p(X) is
also true.

Solution

  • Try using the following definition of hello instead:

    hello :- \+ (kng_bck(X), \+ p(X)).
    

    The following query should then return true:

    ?- assert(p(1)), assert(p(2)), assert(p(3)), hello.
    

    \+/1 is true when the goal cannot be proven, so this definition of hello is checking there is not a situation where "kng_bck(X) and not p(X)".


    Update based on feedback in comments:

    You can also achieve this by using forall/2. Example:

    ?- assert(p(1)), assert(p(2)), assert(p(3)), forall(kng_bck(X), p(X)).