Search code examples
prolog

How to tell prolog that N is a natural number if N - 1 is a natural number?


I am very new to Prolog and I am trying to do a simple recursive example. My KB is:

    natural(0).
    natural(n) :- natural(n - 1).

Then my query is:

    natural(1). 

and the answer is false.

From what I read, numbers in Prolog are constants like 'book' and this might be causing the problem, but I am not sure how to fix it.

Any help would be much appreciated. Thank you!


Solution

  • You should do it like this:

    natural(0).
    natural(N) :- M is N - 1, natural(M).
    

    Prolog is not an imperative language and natural is not a function. What happens here is I'm unifying M with N - 1 and then look if M is natural.

    So, translating to imperative, use is for assignment. Also keep in mind that variables must start with uppercase letters, lowercase letters are for constants.