Modules that require special finalisation at program termination may include any number of ‘finalise’ directives in their implementation sections.
A ‘finalise’ directive has the following form:
:- finalise finalpredname/arity.
where the predicate ‘finalpredname/arity’ must be declared with one of the following signatures:
:- pred finalpredname(io::di, io::uo) is Det. :- impure pred finalpredname is Det
Det must be either
The effect of the ‘finalise’ declaration is to ensure that ‘finalpredname/arity’ is invoked after the program’s ‘main’ predicate. Finalisation predicates within a module are executed in the order in which they are specified, although no order may be assumed between different modules or submodules. Any finalisation required by the Mercury standard library will always occur after any finalisation predicates have been invoked.
If ‘finalpredname/arity’ terminates with an uncaught exception, then the program will immediately abort execution. No predicates specified by other ‘finalise’ directives that have not yet been executed will be executed. If the program’s ‘main/2’ predicate terminates with an uncaught exception, then no finalisation predicates will be executed.
‘finalize’ is also allowed as a synonym for ‘finalise’.