1:- module(onepointfour_basics_stringy_concat,
    2          [
    3            stringy_concat/3         % stringy_concat(ListOfStringys,?Result,+ResultType)
    4           ,stringy_concat/4         % stringy_concat(ListOfStringys,?Result,+ResultType,@Tuned)
    5          ]).    6
    7:- use_module(library('onepointfour_basics/checks.pl')).    8:- use_module(library('onepointfour_basics/stringy_length.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/* pldoc ==================================================================== */

Simply concatenate stringys

Homepage for this code

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

*/

 stringy_concat(ListOfStringy, ?Result, ?ResultType)
Succeeds if Result is the concatenation of the elements of ListOfStringy, coerced to ResultType (one of 'string' or 'atom'). If Result is nonvar, the ResultType is unified to the actual type of Result, so no need to determine it first.
   51stringy_concat(ListOfStringy,Result,ResultType) :-
   52   stringy_concat(ListOfStringy,Result,ResultType,soft).
 stringy_concat(ListOfStringy, ?Result, ?ResultType, @Tuned)
As stringy_concat/3, but you can make the call throw instead of just fail if Result and ResultType are out-of-type or out-of-domain, by passing hard for Tuned.
   60stringy_concat(ListOfStringy,Result,ResultType,Tuned) :-
   61   check_that(Result,[break(var),tuned(stringy)],Tuned),
   62   check_that(ResultType,[break(var),tuned(stringy_typeid)],Tuned),
   63   check_that(ListOfStringy,hard(proper_list)), % this may be costly
   64   var_tag(Result,TaggedResult),
   65   var_tag(ResultType,TaggedResultType),
   66   instantiate_stringy_type(TaggedResult,TaggedResultType), % fails if Result and ResultType are incompatible
   67   var_tag(ResultType,RetaggedResultType),                  % ResultType may have been instantiated, so retag
   68   stringy_concat_2(TaggedResult,RetaggedResultType,ListOfStringy).
   69
   70stringy_concat_2(var(Result),var(ResultType),ListOfStringy) :-
   71   !,
   72   stringy_concat_over_list(ListOfStringy,"",TmpResult),
   73   (ResultType=atom;ResultType=string),                     % yields two possibilites
   74   convert_maybe(ResultType,TmpResult,Result).
   75stringy_concat_2(var(Result),nonvar(ResultType),ListOfStringy) :-
   76   !,
   77   stringy_concat_over_list(ListOfStringy,"",TmpResult),
   78   convert_maybe(ResultType,TmpResult,Result).
   79stringy_concat_2(nonvar(Result),nonvar(ResultType),ListOfStringy) :-
   80   !,
   81   quick_length_check(ListOfStringy,Result),               % may fail due to length mismatch, in which case no need to continue
   82   stringy_concat_over_list(ListOfStringy,"",TmpResult),
   83   convert_maybe(ResultType,TmpResult,Result).
   84
   85stringy_concat_over_list([Stringy|More],RunningString,FinalString) :-
   86   check_that(Stringy,hard(stringy)),
   87   string_concat(RunningString,Stringy,NewRunningString),
   88   stringy_concat_over_list(More,NewRunningString,FinalString).
   89stringy_concat_over_list([],String,String).
   90
   91% this code is also used in space_string.pl
   92
   93instantiate_stringy_type(var(_Stringy),nonvar(_StringyType)) :- !.                   % Do nothing, decision on type to generate has been provided
   94instantiate_stringy_type(var(_Stringy),var(_StringyType))    :- !.                   % Do nothing, leaving indeterminism on StringyType
   95instantiate_stringy_type(nonvar(Stringy),var(atom))          :- atom(Stringy),!.     % Instantiate type inside var/1 tag to 'atom'
   96instantiate_stringy_type(nonvar(Stringy),var(string))        :- string(Stringy),!.   % Instantiate type inside var/1 tag to 'string'
   97instantiate_stringy_type(nonvar(Stringy),nonvar(atom))       :- atom(Stringy),!.     % Accept only if type is 'atom'
   98instantiate_stringy_type(nonvar(Stringy),nonvar(string))     :- string(Stringy).     % Accept only if type is 'string'
   99
  100var_tag(X,var(X))    :- var(X),!.
  101var_tag(X,nonvar(X)).
  102
  103% Quick length testing in case "Result" was already instantiated
  104
  105quick_length_check(ListOfStringy,Result) :-
  106   stringy_length(Result,MaxLength),
  107   quick_length_check_2(ListOfStringy,0,MaxLength).
  108
  109quick_length_check_2([S|More],RunningLength,MaxLength) :-
  110  stringy_length(S,AddLength), % will throw on bad S
  111  NewRunningLength is RunningLength + AddLength,
  112  NewRunningLength =< MaxLength,
  113  quick_length_check_2(More,NewRunningLength,MaxLength).
  114
  115quick_length_check_2([],Length,Length). % length must match at the end
  116
  117% convert_maybe(ResultType,In,Out)
  118%
  119% Is called at the end of concatenation to transform the
  120% concatenated stringy into the string of the desired ResultType.
  121% Out is actually the Result passed in and may be instantiated
  122% to a unifiable or non-unifiable stringy.
  123
  124convert_maybe(atom,In,Out)    :- atom(In)  ,!,In=Out.
  125convert_maybe(atom,In,Out)    :- string(In),!,atom_string(Out,In).
  126convert_maybe(string,In,Out)  :- atom(In)  ,!,atom_string(In,Out).
  127convert_maybe(string,In,Out)  :- string(In),!,In=Out