PLC: Hints Assignment #8

Here is another example for a logic problem out of the Logeleien column: heritage.pl

Prolog provides two operators for division, / returns a possibly float while // always returns an integer result:

1 ?- X is 7 / 4.

X = 1.75 ;

No
2 ?- X is 7 // 4.

X = 1 ;

No
3 ?-

Some are apparently struggling with generating a sequence of numbers out of a given range (e.g. 10 to 99). Here is a solution for that problem:

/* natural(N) : N is a natural number */
natural(1).
natural(N) :- integer(N), N > 0.
natural(N) :- var(N), natural(M), N is M + 1.

/* a natural number within [L..U] */
natural(N,L,U) :- natural(L), natural(U), L =< U, gen_natural(N,L,U).

/* generate a natural number within [L..U] */
gen_natural(N,L,U) :- integer(N), N >= L, N =< U.
gen_natural(N,L,_) :- var(N), N is L.
gen_natural(N,L,U) :- var(N), L1 is L+1, natural(N,L1,U).

Simply use natural(Age,10,99) to generate an age out of the range 10 to 99.

If you copy this generator into your submission, you should explain why

age(N) :- natural(N), N >= 10, N < 100.

does not work and how that problem was solved by the construct above.


Andreas F. Borchert, May 1, 2002