1:- module(onepointfour_basics_dict_pp_decision,
    2          [
    3          decision_how_to_display/5 % decision_how_to_display(+Tag,+SettingsDict,-DecisionForTag,-DecisionForBorder,-DecisionForPadding)
    4          ]).    5
    6:- use_module(library('onepointfour_basics/checks.pl')).    7:- use_module(library('onepointfour_basics/dict_settings.pl')).    8:- use_module(library('onepointfour_basics/dict_pp/helpers.pl')).    9
   10/*  MIT License Follows (https://opensource.org/licenses/MIT)
   11
   12    Copyright 2021 David Tonhofer <ronerycoder@gluino.name>
   13
   14    Permission is hereby granted, free of charge, to any person obtaining
   15    a copy of this software and associated documentation files
   16    (the "Software"), to deal in the Software without restriction,
   17    including without limitation the rights to use, copy, modify, merge,
   18    publish, distribute, sublicense, and/or sell copies of the Software,
   19    and to permit persons to whom the Software is furnished to do so,
   20    subject to the following conditions:
   21
   22    The above copyright notice and this permission notice shall be
   23    included in all copies or substantial portions of the Software.
   24
   25    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
   26    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
   27    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
   28    IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
   29    CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
   30    TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
   31    SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
   32*/
   33
   34/*
   35The homepage for this module is at
   36
   37https://github.com/dtonhofer/prolog_code/blob/main/unpacked/onepointfour_basics/README_dict_pp.md
   38*/
 decision_how_to_display(+Tag, +SettingsDict, -DecisionForTag, -DecisionForBorder, -DecisionForPadding)
Determine what to do with a dict that shall be transformed into lines, given its Tag, which may be an unbound variable, and the settings in SettingsDict.

Instantiates as follows:

DecisionForTagtrueprint the dict's tag
falsedo not print the dict's tag (the tag is never printed if it is an unbound variable)
DecisionForBordertruedecorate output with an ASCII border
falsedo not decorate output
DecisionForPaddingtruepad around content lines with a (possibly zero-thickness) border of whitespace
falsedo not pad
   55decision_how_to_display(Tag,SettingsDict,DecisionForTag,DecisionForBorder,DecisionForPadding) :-
   56   get_setting(SettingsDict, pad,        PadFlag,       false),
   57   get_setting(SettingsDict, sub_pad,    SubPadFlag,    inherit),
   58   get_setting(SettingsDict, border,     BorderFlag,    false),
   59   get_setting(SettingsDict, sub_border, SubBorderFlag, inherit),
   60   get_setting(SettingsDict, tag,        TagFlag,       true),
   61   get_setting(SettingsDict, sub_tag,    SubTagFlag,    inherit),
   62   get_setting(SettingsDict, depth,      Depth),                  % must exist in "SettingsDict", so no default
   63   % 
   64   % we don't want surprises or arbitrary failures later, so make sure we are "on the rails"
   65   %
   66   assertion(check_that(PadFlag       ,[hard(boolean)])),
   67   assertion(check_that(SubPadFlag    ,[hard(member(true,false,inherit))])),
   68   assertion(check_that(BorderFlag    ,[hard(boolean)])),
   69   assertion(check_that(SubBorderFlag ,[hard(member(true,false,inherit))])),
   70   assertion(check_that(TagFlag       ,[hard(boolean)])),
   71   assertion(check_that(SubTagFlag    ,[hard(member(true,false,inherit))])),
   72   %
   73   % do we print the tag or not?
   74   %
   75   decision_for_tag(Depth,Tag,TagFlag,SubTagFlag,DecisionForTag),
   76   assertion(check_that(DecisionForTag,[hard(boolean)])),
   77   %
   78   % do we print an ASCII border or not?
   79   %
   80   decision_for_border(Depth,BorderFlag,SubBorderFlag,DecisionForBorder),
   81   assertion(check_that(DecisionForBorder,[hard(boolean)])),
   82   %
   83   % do we do padding or not?
   84   %
   85   get_padding_settings(SettingsDict,PadTop,PadBottom,PadLeft,PadRight),
   86   all_zero_flag(PadTop,PadBottom,PadLeft,PadRight,AllPadsZero),
   87   decision_for_padding(Depth,PadFlag,SubPadFlag,AllPadsZero,DecisionForPadding),
   88   %
   89   % postcondition
   90   %
   91   assertion(check_that(DecisionForPadding,[hard(boolean)])),
   92   assertion(check_that(DecisionForTag,[hard(boolean)])),
   93   assertion(check_that(DecisionForBorder,[hard(boolean)])).
   94
   95% decision_for_tag(+Depth,+Tag,+TagFlag,+SubTagFlag,-Decision)
   96%
   97% If "Tag" is an unbound variable -> do not print the tag
   98% else
   99% If we are at depth 0 (outermost dict) -> just do what "TagFlag" says
  100% else
  101% If we are at depth > 0 and the "SubTagFlag" is 'inherit' -> just do what "TagFlag" says
  102% else
  103% Do what the "SubTagFlag" says
  104%
  105% Note the reification of the boolean outcome in "Decision".
  106% Note that the guard conditions are superfluous due to the use of the cut.
  107% Leave them in anyway for readability.
  108
  109decision_for_tag(_     , Tag ,       _ ,          _ , false)      :- var(Tag),!.
  110decision_for_tag(0     , Tag , TagFlag ,          _ , TagFlag)    :- nonvar(Tag),!.
  111decision_for_tag(Depth , Tag , TagFlag ,    inherit , TagFlag)    :- nonvar(Tag),Depth>0,!.
  112decision_for_tag(Depth , Tag ,       _ , SubTagFlag , SubTagFlag) :- nonvar(Tag),Depth>0,SubTagFlag\==inherit.
  113
  114% decision_for_border(+Depth,+BorderFlag,+SubBorderFlag,-Decision)
  115% 
  116% If we are at depth 0 (outermost dict) -> just do what "BorderFlag" says
  117% else 
  118% If the "SubBorderFlag" is 'inherit' -> just do what "BorderFlag" says
  119% else
  120% Do what the "SubBorderFlag" says
  121%
  122% Note the reification of the boolean outcome in "Decision".
  123% Note that the guard conditions are superfluous due to the use of the cut.
  124% Leave them in anyway for readability.
  125
  126decision_for_border(0     , BorderFlag ,             _ , BorderFlag)    :- !.
  127decision_for_border(Depth , BorderFlag ,       inherit , BorderFlag)    :- Depth>0,!.
  128decision_for_border(Depth ,          _ , SubBorderFlag , SubBorderFlag) :- Depth>0,SubBorderFlag\==inherit.
  129
  130% decision_for_padding(+Depth,+PadFlag,+SubPadFlag,+AllPadsZero,-Decision)
  131%
  132% If we are at depth 0 (outermost dict) -> just do what "PadFlag" says
  133% ... because if "PadFlag" is 'true', then we want to pad, regardless of whether
  134%     "all pads are zero" or not (if they are all zero, we get a nice tight
  135%     rectangle of whitespace)
  136% ... and if "PadFlag" is 'false' then we explicitly don't want to pad
  137%     (padding by 0 will still be done if a border decoration was ordered)
  138% else 
  139% If "all pads are zero" then we don't want to pad
  140% ... because padding is useless: the text will be integrated into the 
  141%     representation of a higher dict and whitespace-padded in any case
  142% else
  143% If "SubPadFlag" is 'inherit' -> just do what "PadFlag" says
  144% else 
  145% Do what the "SubPadFlag" says
  146%
  147% Note the reification of the boolean outcome in "Decision".
  148% Note that the guard conditions are superfluous due to the use of the cut.
  149% Leave them in anyway for readability.
  150
  151decision_for_padding(0     , PadFlag ,          _ ,     _ , PadFlag)    :- !.
  152decision_for_padding(Depth ,       _ ,          _ ,  true , false)      :- Depth>0,!.
  153decision_for_padding(Depth , PadFlag ,    inherit , false , PadFlag)    :- Depth>0,!.
  154decision_for_padding(Depth ,       _ , SubPadFlag , false , SubPadFlag) :- Depth>0,SubPadFlag\==inherit.
  155
  156% Decide whether for a set of 4 numbers, are all 0, and reify the outcome
  157% in the last argument.
  158
  159all_zero_flag(0,0,0,0,true) :- !.
  160all_zero_flag(_,_,_,_,false)