5.4.1.1 User defined functions on dicts
The tag of a dict associates the dict to a module. If the dot notation uses a compound term, this calls the goal below.
<module>:<name>(Arg1, ..., +Dict, -Value)
Functions are normal Prolog predicates. The dict infrastructure
provides a more convenient syntax for representing the head of such
predicates without worrying about the argument calling conventions. The
code below defines a function multiply(Times)
on a point
that creates a new point by multiplying both coordinates. and len
179as length
would result in a predicate length/2,
this name cannot be used. This might change in future versions.
to compute the length from the origin. The . and :=
operators are used to abstract the location of the predicate arguments.
It is allowed to define multiple a function with multiple clauses,
providing overloading and non-determinism.
:- module(point, []). M.multiply(F) := point{x:X, y:Y} :- X is M.x*F, Y is M.y*F. M.len() := Len :- Len is sqrt(M.x**2 + M.y**2).
After these definitions, we can evaluate the following functions:
?- X = point{x:1, y:2}.multiply(2). X = point{x:2, y:4}. ?- X = point{x:1, y:2}.multiply(2).len(). X = 4.47213595499958.