Next: , Previous: Impurity semantics, Up: Impurity   [Contents]

15.4 Declaring impure functions and predicates

Every Mercury predicate or function has exactly two purity values associated with it. One is the declared purity of the predicate or function, which is given by the programmer. The other value is the inferred purity, which is calculated from the purity of goals in the body of the predicate or function.

A predicate is declared to be impure or semipure by preceding the word pred in its pred declaration with impure or semipure, respectively. Similarly, a function is declared impure or semipure by preceding the word func in its func declaration with impure or semipure. That is, a declaration of the form:

:- impure pred Pred(Arguments…).
:- semipure pred Pred(Arguments…).


:- impure func Func(Arguments…) = Result.
:- semipure func Func(Arguments…) = Result.

declares the predicate Pred to be impure and the function Func to be semipure, respectively.

Type class methods may also be declared as impure or semipure by preceding the word pred or func with the appropriate purity level. An instance of the type class must provide method implementations that are at least as pure as the method declaration.