سؤال

I want the prolog engine to return false instead of an error. I have this code:

:-use_module(library(clpfd)).

set_1(X):-member(X,[f(f(1))]).
p :- X in 1..3,set_1(f(X)).

When I query p, I get an error:

?- p.
ERROR: Type error: `integer' expected, found `f(1)'

I want the output to look like:

 ?- p.
 false

I would expect to see false even if I change member(X,[f(f(1))]) to member(X,[f(f(f(1)))]) , or to some other set, without making any other changes to my program.

I am using swi-prolog v 5.10.4

هل كانت مفيدة؟

المحلول

Use catch/3. Example:

?- catch(p, error(type_error(_,_),_), false).
false.

It is declaratively perfectly OK to replace type errors by silent failure like this. This is not true for instantiation errors, since an instantiation error can turn to solutions when you add more goals, but a type error can not.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top