Computation failure when no rule is applicable.
Solution:
default f(X) = value.
f(0):= 0. f(1):= 1. default f(X)= 2.Goal f(X) => Result 2 if X ~= 0, X ~= 1
PROLOG predicates: Partially defined boolean functions.
CWA + Negation -> default rule with result false.
Compute the substraction of natural numbers.The partially defined version is completed with a default rule in order to return an integer. Integers are defined as signed numbers.
type int = + nat | - nat. fun invert: int -> int. invert (+ Y) = - Y. invert (- Y) = + Y. fun subs : nat * nat -> int. subs Y 0 = + Y. subs (succ Y) (succ X) = subs Y X. default subs Y X) = invert (subs X Y). eval subs (succ 0) X. > result: + (succ 0) answer: X = 0 > result + 0 answer: X = (succ 0) > result - X' answer: X = (succ X'), X' ~= 0 > no (more) solutions.
Compute the infinite list of integer square roots of the natural numbers. We accumulate the previous square root and it is increased when a perfect square is found. A natural number is a perfect square if it is the product of another natural number by itself. Otherwise the number is no perfect.Try to solve it in your favourite language before you check the solution in a functional logic language
Decide if a pair of lists are disjointed, i.e. they do not share elements.
fun member: A * list A -> bool. member X [Y|L] = X=Y => true # member X L. default member X L = false. fun disjoint: list A * list A -> bool. disjoint L1 L2 = ~ ((member Y L1), (member Y L2)). solve disjoint [Y | L] [X, (succ 0)]. > result: true answer: L = [], X ~= Y, Y ~= s (0) > . . .Remember that we had explicit negation, so the programmer can freely use explicit negation and negation as failure for a predicate. Kowalski pointed out the advantages of the distinction between explicit and implicit negation for knowledge representation.
Go to index