Handling exceptions based on predicate based classification

Somewhat related to Using exceptions for "getting out", and after a remark by @jfm, I came up with a library that in my view solves most of the pressing issues with exceptions.

[@jfmc , could you allow for .pl files as attachments]

The idea is to have predicates to define a set of exceptions and have

 catch(Goal, Type, Ball, Recoverr)

Where Type specifies the error type and Ball is there only to pass the exception to Recover. In the implementation I constrain te Ball using freeze/2. Alternatively, we could wrap the recover in (error(Type,Ball) -> Recover ; throw(Ball)). SWI-Prolog however distinguishes caught from uncaught exceptions and wrapping would consider all exceptions caught, while uncaught exceptions trap the debugger ASAP.

For docs and motivation, read the comments in the file below.

I’m planning to add this as a standard library. Ideally after settling the interface as a PIP standard :slight_smile:

exceptions.pl
/*  Part of SWI-Prolog

    Author:        Jan Wielemaker
    E-mail:        jan@swi-prolog.org
    WWW:           http://www.swi-prolog.org
    Copyright (c)  2024, SWI-Prolog Solutions b.v.
    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions
    are met:

    1. Redistributions of source code must retain the above copyright
       notice, this list of conditions and the following disclaimer.

    2. Redistributions in binary form must reproduce the above copyright
       notice, this list of conditions and the following disclaimer in
       the documentation and/or other materials provided with the
       distribution.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    POSSIBILITY OF SUCH DAMAGE.
*/

:- module(exceptions,
          [ catch/4,                    % :Goal, +ErrorType, ?Ball, :Recover
            error/2                     % +ErrorType, ?Ball
          ]).
:- use_module(library(error)).

:- multifile
    error_term/2,                       % Type, Formal
    exception_term/2.                   % Type, Exception

/** <module> Error classification

Prolog catch/3 selects errors based on  unification. This is problematic
for two reasons. First, one typically  wants   the  exception term to be
more specific than the term  passed  to   the  2nd  (`Ball`) argument of
catch/3. Second, in many situations one wishes to select multiple errors
that may be raised  by  some  operations,   but  let  the  others  pass.
Unification is often not suitable  for   this.  For  example, open/3 can
raise an _existence_error_ or a _permission_error_  (and a couple more),
but  _existence_error_  are  also  raised  on,  for  example,  undefined
procedures. This is very hard  to  specify,   Below  is  an attempt that
still assumes nothing throws error(_,_).

catch(open(...), error(Formal,ImplDefined),
      (   ( Formal = existence_error(source_sink,_)
          ; Formal = permission_error(open, source_sink, _)
          )
      ->  <handle>
      ;   throw(Formal, ImplDefined)
      )),
...

Besides being hard to specify,  actual   Prolog  systems  define a large
number of additional error terms  because   there  is  no reasonable ISO
exception  defined.  For   example,   SWI-Prolog    open/3   may   raise
resource_error(max_files) if the maximum number of   file handles of the
OS is exceeded.

As a result, we see a lot of Prolog   code  in the wild that simply uses
the construct below to simply fail. But, this may fail for lack of stack
space, a programmer error that causes a type error, etc. This both makes
it much harder to debug the code  and provide meaningful feedback to the
user of the application.

catch(Goal, _, fail)

Many programing languages have their exceptions   organised by a (class)
hierarchy. Prolog has no hierarchy  of   terms.  We introduce error/2 as
error(+Type, ?Term), which can both  be  used   as  a  type  test for an
exception term and as a _constraint_ for  the `Ball` of catch/3. Using a
predicate we can express abstractions over concrete exception terms with
more flexibility than  a  hierarchy.   Using  a  _multifile_  predicate,
libraries can add their exceptions  to   defined  types or introduce new
types.

The predicate catch/4 completes the interface.
*/

%!  catch(:Goal, +ErrorType, ?Ball, :Recover)
%
%   As catch/3, only catching exceptions for which error(ErrorType,Ball)
%   is true. See error/2. For example,   the code below properly informs
%   the user some file could not  be   processed  due do some issue with
%   `File`, while propagating on all other reasons while process/1 could
%   not be executed.
%
%   ```
%       catch(process(File), file_error, Ball,
%             file_not_processed(File, Ball))
%
%   file_not_processed(File, Ball) :-
%       message_to_string(Ball, Msg),
%       format(user_error, 'Could not process ~p: ~s', [File, Msg]).
%   ```

catch(Goal, ErrorType, Ball, Recover) :-
    error(ErrorType, Ball),
    catch(Goal, Ball, Recover),
    del_attr(Ball, freeze).

%!  error(+Type, --Ball) is det.
%!  error(+Type, +Ball) is semidet.
%
%   If Ball is unbound, adds a delayed goal that tests the error belongs
%   to Type when Ball is  instantiated   (by  catch/3).  Else succeed is
%   error is of the specified Type.
%
%   Note that the delayed goal is added using freeze/2 and therefore the
%   stepwise instantiation of Ball does not work, e.g. error(file_error,
%   error(Formal,_)) immediately fails.
%
%   @see error_term/2 and exception_term/2 for classifying errors.

error(Type, Ball) :-
    freeze(Ball, is_error(Type, Ball)).

is_error((A;B), Formal) =>
    (   is_error(A, Formal)
    ->  true
    ;   is_error(B, Formal)
    ).
is_error(\+A, Formal) =>
    \+ is_error(A, Formal).
is_error(Type, error(Formal,_)), error_term(Type, Term) =>
    subsumes_term(Term, Formal),
    !.
is_error(Type, Exception), exception_term(Type, Term) =>
    subsumes_term(Term, Exception).
is_error(Type, _) =>
    existence_error(error_type, Type).

%!  error_term(?Type, ?Term) is nondet.
%
%   Describe the formal part of error(Formal,ImplDefined) exceptions.

error_term(file_error, existence_error(source_sink, _Culprit)).
error_term(file_error, permission_error(open, source_sink, _Culprit)).
error_term(file_error, resource_error(max_files)).
error_term(file_error, representation_error(max_symbolic_links)).
error_term(file_error, representation_error(max_path_length)).

error_term(network_error, socket_error(_Code, _Message)).
error_term(network_error, timeout_error(_Operation, _Culprit)).
error_term(network_error, io_error(_Operation, _Culprit)).

error_term(timeout, timeout_error(_Operation, _Culprit)).

error_term(evaluation_error, evaluation_error(_)).

%!  exception_term(?Type, ?Term) is nondet.
%
%   Describe exceptions that are not error(Formal, _) terms.

exception_term(timeout, time_limit_exceeded).
exception_term(timeout, time_limit_exceeded(_TimeLimit)).