5.1 Modes and Moded Types

Moded types are used when declaring types for code. They are used to specify which arguments of a call must be ground and which need not be ground when the call is made, which of the possibly non-ground arguments of the call will be ground on exit from the call, which may still be non-ground, and which will be unchanged by the call. Below we list the allowed moded types that were discussed in Type Definitions.

The moded types for code arguments are:

  • !Type - the argument must be ground and of type Type.
  • ?Type - the argument may be ground, partly ground, or a variable on call but will be ground and of type Type on exit from the call.
  • ??Type - the argument is not required to be ground on call and may remain unground on exit from the call.
  • @term - the call does not instantiate any variables in the term argument - i.e. the argument is left unchanged by the call.

The ! mode is more restrictive than the @ mode and the ? mode, and the ? mode is more restrictive than the ?? mode. In other words, for example, the moded type !Type is more constraining than the moded type ?Type.

For instances of polymorphic types such as list(int) we may use a less constraining mode annotation at the inner level from that used at the outer structure level. So, giving a relation argument the moded type !list(?int) means that this argument must be a complete list of known length at call time, but some or all the elements of the list may be variables, each of which will be instantiated to an integer value if the call succeeds. The moded type !list(?list(??int)) means that this argument must be a complete list of possibly partial lists of integers and variables, such as [[X,3,..L],[-7,Y,Z]]. It will become a complete list of complete lists of integers, and perhaps variables, if the call succeeds. So, [[X,3],[-7,Y,-6]] is a possible ’output’ value for the structure argument.

Modes must become less restrictive as we move into the type. This means we can have:

  • !list(!int) (for which we can use the shorthand !list(int)) and even list(int))
  • !list(@term) (with shorthand list(@term)) and
  • !list(?int) (with shorthand list(?int)) and
  • !list(??int) (with shorthand list(??int))

but not:

  • ?list(!int) and
  • ??list(!int) and
  • ?list(@term) (as @ is not given as less restrictive then as ?)

On This Site