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


16.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, the declarations

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

declare the predicate Pred and the function Func to be impure and 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.