Default predicate definitions

Using Logtalk or just plain Prolog, sometimes we need to write default predicate definitions. A default definition may be complete and describe a common case. It may be partial, requiring the user to extend it for a complete definition.

A common case is allowing the user to override a default definition used by a library or a tool. A good example is the message printing mechanism found in Logtalk and some Prolog systems. The user may want e.g. to suppress, divert, or rewrite a message in alternative to the default printing of the message. Multifile predicates provide a solution here as long there is only a single palce in the codebase where the override is required.

In other cases, we need access to the definition that we are overriding to provide an alternative definition. I.e. we need to specialize instead of replacing the default definition. This may seem like a case for using multifile predicates. But it is not. A multifile predicate allows a definition from anywhere while specialization is only possible if the predicate that we are specializing is within scope.

A third alternative is using a parametric object with the parameters, which are logical variables, being used to customize the predicate definitions. The default predicate definitions can than be supported by defining default parameter values.