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 signature:
:- pred finalpredname(io::di, io::uo) is Det. :- impure pred finalpredname is Det
Det must be either ‘det’ or ‘cc_multi’.
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 sub-modules. 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.
‘finalize’ is also allowed as a synonym for ‘finalise’.