1:- module(onepointfour_basics_checks_throwers,
    2          [
    3           fail_unless_hard/1               % fail_unless_hard(Tuned)
    4          ,throw_or_fail_for_case_random/1  % throw_or_fail_for_case_random(Tuned)
    5          ,throw_or_fail/5                  % throw_or_fail(ErrorTerm,Culprit,Tuned,Ness,Dict)
    6          ,throw_2/3                        % throw(ErrorTerm,Msg,Culprit)
    7          ]).    8
    9:- use_module(library('onepointfour_basics/dict_settings.pl')).   10
   11/*  MIT License Follows (https://opensource.org/licenses/MIT)
   12
   13    Copyright 2021 David Tonhofer <ronerycoder@gluino.name>
   14
   15    Permission is hereby granted, free of charge, to any person obtaining
   16    a copy of this software and associated documentation files
   17    (the "Software"), to deal in the Software without restriction,
   18    including without limitation the rights to use, copy, modify, merge,
   19    publish, distribute, sublicense, and/or sell copies of the Software,
   20    and to permit persons to whom the Software is furnished to do so,
   21    subject to the following conditions:
   22
   23    The above copyright notice and this permission notice shall be
   24    included in all copies or substantial portions of the Software.
   25
   26    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
   27    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
   28    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
   29    IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
   30    CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
   31    TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
   32    SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
   33*/
   34
   35/* pldoc ==================================================================== */

Some helper predicates for checks.pl

The homepage for this module is at

https://github.com/dtonhofer/prolog_code/blob/main/unpacked/onepointfour_basics/README_checks.md

*/

   45% Checking the "Tuned" flag for being set to 'hard' or 'soft'
   46
   47fail_unless_hard(soft) :- !,fail.
   48fail_unless_hard(hard).
   49
   50% Predicates which check whether the Tuned flag is set to 'hard', and if so,
   51% construct an exception throw and then throw. If the Tuned flag is set to
   52% 'soft', they just fail.
   53%
   54% "Ness" is a a free string that expresses what was expected byut not seen
   55% For example, if "Ness" is "integer", the message will be about missing
   56% "integer-ness".
   57
   58throw_or_fail_for_case_random(Tuned) :-
   59   fail_unless_hard(Tuned),            % if this fails, the call fails (which is what we want)
   60   throw_2(random,"random failure as decided by a call to maybe/1",_).
   61
   62% TODO: This is less than perfect. The protocol for passing info is flaky
   63throw_or_fail(ErrorTerm,Culprit,Tuned,Info,Dict) :-
   64   fail_unless_hard(Tuned),            % if this fails, the call fails (which is what we want)
   65   get_setting(Dict,name,Name,""),     % pick Name out of Dict, which, if missing, is replaced by ""
   66   name_to_string(Name,NameStr), 
   67   (
   68      (Info = be(What))
   69      ->
   70      format(string(Msg),"~s should be ~q",[NameStr,What])
   71      ;
   72      (Info = be(What,Text))
   73      ->
   74      format(string(Msg),"~s should be ~q. ~q",[NameStr,What,Text])
   75      ;
   76      format(string(Msg),"~s should fulfill ~q-ness",[NameStr,Info])
   77   ),
   78   throw_2(ErrorTerm,Msg,Culprit).
   79
   80% "Lowermost" throwing predicate constructing the exception term itself.
   81% All the throws are here for easy modification
   82
   83throw_2(domain(Expected),Msg,Culprit)  :- throw(error(check(domain            , Expected  , Msg , Culprit)  , _)).
   84throw_2(type(Expected),Msg,Culprit)    :- throw(error(check(type              , Expected  , Msg , Culprit)  , _)).
   85throw_2(domain,Msg,Culprit)            :- throw(error(check(domain            , _Expected , Msg , Culprit)  , _)).
   86throw_2(type,Msg,Culprit)              :- throw(error(check(type              , _Expected , Msg , Culprit)  , _)).
   87throw_2(uninstantiation,Msg,Culprit)   :- throw(error(check(uninstantiation   , _Expected , Msg , Culprit)  , _)). % ISO's "uninstantiation error"
   88throw_2(instantiation,Msg,Culprit)     :- throw(error(check(instantiation     , _Expected , Msg , Culprit)  , _)). % ISO's "instantiation error"
   89throw_2(random,Msg,_)                  :- throw(error(check(random            , _Expected , Msg , _Culprit) , _)).
   90throw_2(explicit_fail,Msg,Culprit)     :- throw(error(check(explicit_fail     , _Expected , Msg , Culprit)  , _)).
   91throw_2(call,Msg,Culprit)              :- throw(error(check(call              , _Expected , Msg , Culprit)  , _)).
   92throw_2(hard_check_fails,Msg,Culprit)  :- throw(error(check(hard_check_fails  , _Expected , Msg , Culprit)  , _)).
   93throw_2(syntax,Msg,Culprit)            :- throw(error(check(syntax            , _Expected , Msg , Culprit)  , _)).
   94throw_2(unknown_condition,Msg,Culprit) :- throw(error(check(unknown_condition , _Expected , Msg , Culprit)  , _)).
   95
   96% I'm not sure about whether these are a good idea:
   97
   98throw_2(passall,Msg,Culprit)           :- throw(error(check(passall          , _Expected , Msg , Culprit) , _)).
   99throw_2(passany,Msg,Culprit)           :- throw(error(check(passany          , _Expected , Msg , Culprit) , _)).
  100throw_2(passnone,Msg,Culprit)          :- throw(error(check(passnone         , _Expected , Msg , Culprit) , _)).
  101throw_2(forall,Msg,Culprit)            :- throw(error(check(forall           , _Expected , Msg , Culprit) , _)).
  102throw_2(forany,Msg,Culprit)            :- throw(error(check(forany           , _Expected , Msg , Culprit) , _)).
  103throw_2(fornone,Msg,Culprit)           :- throw(error(check(fornone          , _Expected , Msg , Culprit) , _)).
  104
  105% Having this at the end of throw_2/3 saves the day when debugging
  106
  107throw_2(T,M,C) :- 
  108   format(string(Msg),"Bug! You forgot a throw_2/3 clause in the source for term '~q', message '~q', culprit '~q'",[T,M,C]),
  109   throw(Msg).
  110
  111% Map the name of the variable to something sensible
  112
  113name_to_string(X  , "the value") :- var(X),!.
  114name_to_string('' , "the value") :- !.
  115name_to_string("" , "the value") :- !. 
  116name_to_string(X  , X          ) :- string(X),!.
  117name_to_string(X  , Str        ) :- atom(X),!,atom_string(X,Str).
  118name_to_string(X  , Str        ) :- format(string(Str),"~q",[X])