DeAL TutorialComplex Terms
Complex Terms (.deal | .fac)
Every argument in a predicate can itself be a complex term consisting of a functor and several arguments (which in turn can be complex terms). The following example illustrates the use of complex terms to store shapes of different structure and of using these in performing computation.
%A DB of flat parts described by their geometric shape and weight. %Different geometric shapes require a different number of %parameters. Also actualkg is the actual weight of the %part, but unitkg is the specific weight where the actual %weight can be easily derived from the area of the part query part-weight(No, Kilos). part-weight(No, Kilos) <- part(No, _ , actualkg(Kilos)). part-weight(No, Kilos) <- part(No, Shape, unitkg(K)), area(Shape, A), Kilos = K * A. area(circle(Dmtr), A1) <- A= Dmtr * Dmtr * 3.14/4. area(rectangle(Base, Height), A1) <- A1 = Base * Height. % part# shape , weight part(322, circle(11), actualkg(34)). part(121, rectangle(10, 20), unitkg(2.1)).
In computing this query on the first part we find that the goal in the first rule yields No=322 and Kilos=34 (the operation by which this goal and fact are made equal is called unification). The goal of the first rule fails to unify with the first fact, and instead unifies with the second one, yielding No=121, Shape=rectangle(10, 20), and K=2.1. Using these values, the area rules compute A and then then Kilos.
Lists
Lists are terms with two arguments, called the head and the tail of the list. Because lists are so common, a special notation is supported for lists, as follows:
- [ ] denotes an empty list,
- [X | Y] is a list with head X and tail Y
- [a, b, c, ...] is a short hand for [a | [b | [c ...]]]
Say for instance that we have facts as follows:
part(socks, [red, black, blue]).
From these facts we might want to list all the items, colors pairs (i.e., deriving a flat relation from a nested one) as follows:
query: part-color(Item, Color). part-color(I, C) <- subL(I, [C|_]). subL(I, Rest) <- subL(I, [C|Rest]). subL(I, CL) <- part(I, CL).