Skip to content
Documentation out of dateLearn more

Negation as failure

Negation as failure. It is possible to do negation-as-failure using STELF’s logic programming engine, with the use of %deterministic. As an example, we will define logic programs to compute the less-than and greater-than-or-equal-to functions. The less-than function will be defined in a standard way. The greater-than-or-equal-to function will be defined using a negation-as-failure interpretation of the less-function.

Natural numbers and booleans.

%sort nat %.
%term z nat %.
%term s %pi nat %-> nat %.
%sort bool %.
%term true bool %.
%term false bool %.

less is defined inductively in the standard way.

%sort less {_ nat} {_ nat} %.
%mode less %in %in %.
%term less/z less z (s _) %.
%term less/s %pi (less (s N1) (s N2)) %<- (less N1 N2) %.

We will need a judgment that tests whether a boolean is false in order to use negation-as-failure.

%sort isfalse {_ bool} %.
%mode isfalse %in %.
%term isfalse/i isfalse false %.

We define a logic program that when given two numbers returns true if the first is less than the second, and false otherwise. We use %deterministic to make the logic programming engine commit to the first solution it finds.

%sort less-bool {_ nat} {_ nat} {_ bool} %.
%mode less-bool %in %in %out %.
%deterministic less-bool %.

Because the less-bool/true case is first, it will attempt to find a proof that the first number is less than the second.

%term less-bool/true %pi (less-bool N1 N2 true) %<- (less N1 N2) %.

Because of the %deterministic declaration, when searching for a proof of less N1 N2 B, only executes when less-bool/true fails. However, because of pattern matching a search for a proof of less N1 N2 false always succeeds.

%term less-bool/false less-bool N1 N2 false %.

We will now define gte using less-bool. It has only one rule, which makes a call to less-bool N1 N2 B. It is important to make sure that the result B is not directly identified as false so that it executes less-bool in the appropriate order. We use the call to isfalse B to verify that the output really is false.

%sort gte {_ nat} {_ nat} %.
%mode gte %in %in %.
%term gte/i %pi (gte N1 N2) %<- (less-bool N1 N2 B) %<- (isfalse B) %.

We can use a number of %solve declarations to test our less and gte judgments.

%solve deriv : less (s z) (s (s z)) %.
%solve deriv1 : gte (s (s z)) (s z) %.
%solve deriv2 : gte z z %.
% solve deriv3 : gte (s z) (s (s z)). % should fail

It is important to note that while these definitions work as intended as logic programs, proving appropriate meta-theorems about judgments that use negation as failure is problematic or impossible.

Note from dklee: I had to run home to take care of some stuff. I will finish documenting this later in the evening.