2

Sometimes when I read some samples in Prolog I can see it has said that "this code is in the form of (+,?,?)". So, what is the meaning of these signs. I didn't know the name then I couldn't search more. Thanks

Amir Jalilifard
  • 2,027
  • 5
  • 26
  • 38

1 Answers1

4

The +, ?, -, and @ are standard predicate argument instantiation modes. They are used in the documentation of predicates to inform the user of the supported modes that can be used when calling the predicate. Their meaning, as defining in the ISO Prolog standard. are:

  • + - the argument shall be instantiated
  • ? - the argument shall be instantiated or a variable
  • @ - the argument shall remain unaltered
  • - - the argument shall be a variable that will be instantiated iff the goal succeeds

Therefore, usually + arguments are seen as input arguments, - arguments as output arguments, ? arguments as both input and output arguments.

The documentation of some Prolog systems and extensions often extend this set of argument mode indicators. For example, some systems may provide an additional mode indicator to indicate that an argument should be ground when calling a predicate. Or that an argument is a meta-argument (e.g. that's is going to be meta-called by the predicate).

Often predicates only support a small number of different mode templates. But well documented predicates are expected to specify also which errors to expect when used outside the supported templates.

Consider as an example the standard is/2 predicate. Its single template is:

is(?term, @evaluable)

Note that, besides the instantiation mode indicators, there's also type information. In this case, the first argument can be any term (which includes variables) and the second argument should be an evaluable arithmetic expression. The full specification of this predicate also tells the user that e.g. if the predicate is called with a unbound variable in the second argument we get an instantiation error.

Some predicates support more than one template. An example is the standard atom_concat/3 predicate:

atom_concat(?atom, ?atom, +atom)
atom_concat(+atom, +atom, -atom)

The first template tells the user that atom_concat/3 can be used to decompose an atom. The second template tells the user that the predicate can be used, as expected, to concatenate two atoms. Often each valid template is associated with determinism information. For example, the goal:

| ?- atom_concat(Prefix, Suffix, abc).

have multiple solutions:

Prefix = '',
Suffix = abc ;
Prefix = a,
Suffix = bc ;
Prefix = ab,
Suffix = c ;
Prefix = abc,
Suffix = ''.

You can also see in this example that some templates subsume other templates. In the above example, we+re using the predicate with the mode (-atom, -atom, +atom).

Paulo Moura
  • 18,373
  • 3
  • 23
  • 33