The use of finite domains as values of functions works as follows. Like any other term, a domain term can be specified as (part of) the returned value in a function definition. Such a function then returns the finite domain to its caller as a `closed' term representing a finite number of non-deterministic values, which without domain terms available would typically be enumerated via backtracking.
For instance, the directed equations
direction(old) :-& dom[east,west]. direction(new) :-& dom[north,south]. direction(all) :-& dom[north,west,south,east].use dom values for compactly defining a direction function. The first clause, e.g., can be regarded as a `closed' form of the non-deterministic, two-clause function definition produced by (section 5.1):
direction(old) :-& east. direction(old) :-& west.
A main call unifies returned domain terms just like for anonymously specified domains. For instance, using the variable-length tup function for list building,
In particular, a domain functionally returned to the top-level gives the user a more compact representation of results than their enumeration, much like a domain assigned to a relational request variable.
We may also call domain-valued functions within is-calls. For example, while the query
D is direction(old), D is direction(new)fails (the domains are disjoint), the query
D is direction(all), D is direction(new)succeeds, temporally binding D to dom[north,west,south,east], but then specializing it to dom[north,south].
The is-embedded non-ground functional query
[new,dom[west,north]] is tup(Which,direction(Which))succeeds by binding, as its second attempt, Which to new and building the list [new,dom[north,south]], whose most general `instantiation' in common with the is-lhs (left-hand side) is the domless ground list [new,north].
Analogously, an exclusion term can be (part of) the returned value of a function. For instance, the definition
permitted(butcher-shop) :-& exc[dog]. permitted(pet-shop) :-& exc[cat,dog].prohibits certain entries to butcher and pet shops: the non-ground call
permitted(Where)enumerates the exclusion values exc[dog], binding Where to butcher-shop, and exc[cat,dog], binding Where to pet-shop.
Two such permitted calls may be embedded into an is-call:
[cat,dom[kid,dog]] is tup(permitted(Where),permitted(Where))This succeeds by specializing the is-lhs to [cat,kid], consistently binding Where to butcher-shop.
Finally, a function can also return a mix of domains and exclusions. For example, the dishes (dis)liked by several people may be defined thus:
dish(john) :-& dom[chilli,pizza,sushi,chop-suey]. dish(mary) :-& exc[sushi]. dish(fred) :-& exc[spaghetti,pizza]. dish(tina) :-& dom[sushi,chop-suey,hamburger].For constraining the set of candidate restaurants, they could perform intersection-difference operations equivalent to
[D,D,D,D] is tup(dish(john),dish(mary),dish(fred),dish(tina))binding D to the (fortunately unique) solution chop-suey.