onurozcelik
onurozcelik

Reputation: 1214

Prolog test whether predicate failed or not

I want to write a predicate which returns a solution from a factbase. When no solution exists the predicate should return "No solutions exists". Returning a solution which exists in factbase is trivial. How to implement "No solution exists" part. I use SWI-Prolog.

Upvotes: 0

Views: 1259

Answers (1)

Fred Foo
Fred Foo

Reputation: 363487

If you have a predicate, say fact/1 that contains all your facts, you can use an if-then-else statement as follows:

fact_or_no_solution(Sol) :-
    (fact(X) ->
        Sol = X
    ;
        Sol = 'No solution exists'
    ).

Upvotes: 2

Related Questions