1% ===================================================================
    2% File 'logicmoo_module_aiml_shared.pl'
    3% Purpose: An Implementation in SWI-Prolog of AIML
    4% Maintainer: Douglas Miles
    5% Contact: $Author: dmiles $@users.sourceforge.net ;
    6% Version: 'logicmoo_module_aiml_shared.pl' 1.0.0
    7% Revision:  $Revision: 1.7 $
    8% Revised At:   $Date: 2002/07/11 21:57:28 $
    9% ===================================================================
   10
   11% :- use_module(library(logicmoo/util_strings)).
   12% is_string
   13
   14%:-module()
   15%:-include('logicmoo_utils_header.pl'). %<?
   16%:- style_check(-singleton).
   17%%:- style_check(-discontiguous).
   18%:- if((current_prolog_flag(version,MMmmPP),MMmmPP<70000)).
   19%:- style_check(-atom).
   20%:- style_check(-string).
   21%:- endif.
   22
   23:- current_prolog_flag(version,MMmmPP),
   24   (MMmmPP<70000 -> 
   25      consult(logicmoo_module_aiml_include_547); 
   26      consult(logicmoo_module_aiml_include_700)).
   27
   28debugFmt(x(_Stuff)):-!.
   29debugFmt(Stuff):- debugFmt('~N~n% ~q',[Stuff]),!.
   30debugFmt(F,A):- hide_complex_ctx(A,AA),!, once(lmdebugFmt(F,AA)).
   31debugFmt(F,A):- once(lmdebugFmt(F,A)).
   32
   33
   34hide_complex_ctx(I,O):- \+ compound(I),!,O=I.
   35hide_complex_ctx(I,O):- is_list(I),!,maplist(hide_complex_ctx,I,O).
   36hide_complex_ctx(I,'$..$'(A1)):- functor(I,F,_),F==frame,!,arg(1,I,A0),functor(A0,A1,_).
   37hide_complex_ctx(I,O):- I=..M,hide_complex_ctx(M,N),!,O=..N.
   38
   39:- dynamic(noConsoleDebug/0).
   40%noConsoleDebug:- current_prolog_flag(debug,false).
   41noConsoleDebug:- debugging(programk,true),!,fail.
   42noConsoleDebug:- debugging(programk,false),!.
   43:- nodebug(programk).
   44
   45
   46%lmdebugFmt(Stuff):- noConsoleDebug,Stuff \= say(_),!.
   47lmdebugFmt(Stuff):- once((fresh_line,debugFmtS(Stuff),fresh_line)),!.
   48
   49lmdebugFmt(_,_):- noConsoleDebug,!.
   50lmdebugFmt(F,A):- 
   51        fresh_line(user_error),
   52        format(user_error,F,A),
   53        fresh_line(user_error),
   54        flush_output_safe(user_error),!.
   55
   56debugFmtS(XXX):- XXX == [], !.
   57debugFmtS([A|L]):-!,debugFmt('% ~q',[[A|L]]).
   58debugFmtS(Comp):-toReadableObject(Comp,Comp2),!,debugFmt('% ~q',[Comp2]).
   59debugFmtS(Stuff):-!,debugFmt('% ~q',[Stuff]).
   60
   61%alldiscontiguous:-!.
   62cyc:debugFmt(Stuff):-once(lmdebugFmt(Stuff)).
   63cyc:debugFmt(F,A):-once(lmdebugFmt(F,A)).
   64:- if( \+ predicate_property(is_string(_),defined)).
   65 is_string(X):-string(X),!.
   66 is_string(X):-atom(X),!,atom_length(X,L),L>1,atom_concat('"',_,X),atom_concat(_,'"',X),!.
   67 is_string(X):-var(X),!,fail.
   68 is_string(string(_)):-!.
   69 is_string("").
   70 is_string(L):-is_charlist(L),!.
   71 is_string(L):-is_codelist(L),!.
   72:- endif.
   73
   74%================================================================
   75:-multifile(expire1Cache/0).
   76%================================================================
   77
   78:- op(1100,xfy,(=>)).
   79
   80% ( Antecedent => Consequent ) :-
   81%     \+ ( Antecedent,
   82%          \+ Consequent
   83%        ).
   84( Antecedent => Consequent ) :- forall(( Antecedent ),( Consequent )).
   85
   86
   87devmode:-fail.
   88
   89atrace:- not(devmode),!.
   90atrace:- cyc:ctrace.
   91unused:atrace:-prolog_is_vetted_safe->notrace;(willTrace->trace;notrace).
   92
   93
   94f0rmt(A,B,C):-'format'(A,B,C).
   95
   96%%% Modified version of <http://pastebin.com/GvmVQ1f1>
   97/*
   98%================================================================
   99prolog_trace_interception_pce(A, B, C, E) :- true,
  100%================================================================
  101    pce_prolog_tracer:
  102    (   current_prolog_flag(gui_tracer, true),
  103        (   notrace(intercept(A, B, C, D)),
  104            map_action(D, B, E)
  105        ->  true
  106        ;   print_message(warning, noguitracer(intercept_failed(A, B, C, E))),
  107            E = continue
  108        )
  109    ).
  110
  111
  112define_self_trace :-
  113    (   \+ clause(prolog_trace_interception(_, _, _, _),pce_prolog_tracer:_)
  114    ->  f0rmt(user_error, '~N % already defineSelfTrace~n',[]) % done already?
  115    ;   abolish(prolog_trace_interception,4),
  116        asserta(( prolog_trace_interception(A, B, C, E) :-
  117                      prolog_trace_interception_pce(A, B, C, E) ))
  118    ).
  119
  120:- initialization define_self_trace.
  121*/  
  122%================================================================
  123%% print_stack_trace(+Stream,[+Option,...],+Depth)
  124% `Option' being one of
  125% `goal',`level',`context_module',`has_alternatives',`show_hidden'
  126%================================================================
  127
  128print_stack_trace :-
  129    print_stack_trace(user_error
  130                     ,[goal,show_hidden,level
  131                      ,has_alternatives,alternative
  132                      ,hide(hmod:_),hide(_:hpred)]
  133                     ,10).
  134
  135print_stack_trace(Stream,Options,Depth):-
  136    prolog_current_frame(Frame),
  137    print_stack_trace(Stream,Options,Depth,frame(Frame),1).
  138
  139%%% Modified from older pastes
  140%% <http://pastebin.com/Tq7eQqDT>,<http://pastebin.com/fk5agLgE>
  141% print_stack_trace(Stream,Options,Depth,frame(Frame) :-
  142%     (   Depth = 0
  143%     ->  f0rmt(Stream,'~N<toodeep/>~n',[])
  144%     ;   print_stack_trace1(Stream,Options,Depth,frame(Frame)),
  145%         parent_frame_of(Frame,Parent),Depth2 is Depth -1,
  146%         print_stack_trace(Stream,Options,Depth2,Parent)
  147%     ).
  148%
  149% :- index(print_stack_trace1(0,0,0,1)).
  150% print_stack_trace1(Stream,Options,Depth,top         ) :-
  151%     f0rmt(Stream,'~N<top/>~n',[]).
  152% print_stack_trace1(Stream,Options,Depth,frame(Frame)) :-
  153%     f0rmt(Stream,'~N<frame id="~w">~n',[Frame]),
  154%     (   prolog_frame_attribute(Frame,hidden,true),
  155%         \+ memberchk(show_hidden,Options)
  156%     ->  parent_frame_of(Frame,Parent),
  157%         print_stack_trace(Stream,Options,Depth,Parent)
  158%     ;   (   member(Opt,Options),
  159%             \+ memberchk(Opt,[show_hidden,other_fake_properties]),
  160%         =>  prolog_frame_attribute(Frame,Opt,Value),
  161%             f0rmt(Stream,' ~w = ~q',[Opt,Value])
  162%         )
  163%     ),
  164%     f0rmt(Stream,'~N</frame>~n',[]).
  165
  166print_stack_trace(Stream,Options,Depth,PFrame,PD) :-
  167    (   Depth = 0
  168    ->  pd(Stream,PD),f0rmt(Stream,'<toodeep/>~n',[])
  169    ;   print_stack_trace_aux(Stream,Options,Depth,PFrame,PD)
  170    ).
  171
  172%:- if((current_prolog_flag(version,MMmmPP),MMmmPP<70000)).
  173%:- index(print_stack_trace_aux(0,0,0,1,0)).
  174%:- endif.
  175print_stack_trace_aux(Stream,_Options, _Depth,top         ,PD) :-
  176    pd(Stream,PD),f0rmt(Stream,'<top/>~n',[]).
  177print_stack_trace_aux(Stream,Options,Depth,frame(Frame),PD) :-
  178    (   (   prolog_frame_attribute(Frame,predicate_indicator,MFA),
  179            pred_mf(MFA,MF),
  180            memberchk(hide(MF),Options)
  181        ;   prolog_frame_attribute(Frame,hidden,true),
  182            \+ memberchk(show_hidden,Options)
  183        )
  184    ->  Depth2  = Depth
  185    ;   print_stack_frame(Stream,Options,Frame,PD),
  186        Depth2 is Depth - 1
  187    ),
  188    parent_frame_of(Frame,Parent),
  189    PD2 is PD + 2,
  190    print_stack_trace(Stream,Options,Depth2,Parent,PD2).
  191
  192parent_frame_of(Frame,Parent0) :-
  193    (  prolog_frame_attribute(Frame,parent,Parent)
  194    -> Parent0 = frame(Parent)
  195    ;  Parent0 = top
  196    ).
  197
  198pred_mf((M: F)/_A ,M   :F) :- !.
  199pred_mf( M:(F /_A),M   :F) :- !.
  200pred_mf(    F/ _A ,user:F).
  201
  202print_stack_frame(Stream,Options,Frame,PD) :-
  203    pd(Stream,PD),f0rmt(Stream,'<frame id="~w">~n',[Frame]),
  204    (   member(Opt,Options), 
  205        \+ memberchk(Opt
  206                    ,[show_hidden,alternative,other_fake_properties,hide(_)])
  207    =>  prolog_frame_attribute(Frame,Opt,Value),
  208        pd(Stream,PD),f0rmt(Stream,' ~w = ~q',[Opt,Value])
  209    ),
  210    (   memberchk(alternative,Options)
  211    =>  delete(Options,alternative,Options2),
  212        prolog_frame_attribute(Frame,alternative,Alt),
  213        PD1 is PD + 1,PD3 is PD + 3,
  214        pd(Stream,PD1),f0rmt(Stream,'<alt>~n',[]),
  215            print_stack_frame(Stream,Options2,Alt,PD3),
  216        pd(Stream,PD1),f0rmt(Stream,'</alt>~n',[])
  217    ),
  218    pd(Stream,PD),f0rmt(Stream,'</frame>~n',[]).
  219
  220pd(Stream,PD) :- f0rmt(Stream,'~N',[]),tab(Stream,PD + 1).
  221
  222%================================================================
  223%% printStackTrace(+Stream,[+Options..,goal,level,context_module,has_alternatives,show_hidden],+Depth). 
  224%================================================================
  225
  226printStackTrace:-printStackTrace(user_error).
  227
  228printStackTrace(Stream):-printStackTrace(Stream,[goal,show_hidden,level,has_alternatives,alternative,hide(hmod:_),hide(_:printStackTrace)],10).
  229
  230printStackTrace(Stream,Options,Depth):-prolog_current_frame(Frame),printStackTrace(Stream,Options,Depth,Frame,1).
  231
  232printStackTrace(Stream,_Options,_Depth,top,PD):-!,sindent(Stream,PD),f0rmt(Stream,'<top/>~n',[]).
  233printStackTrace(Stream,_Options,Depth,_Frame,PD):- 0 is Depth,!,sindent(Stream,PD),f0rmt(Stream,'<toodeep/>~n',[]).
  234printStackTrace(Stream,Options,Depth,Frame,PD):- 
  235    ( (prolog_frame_attribute(Frame,predicate_indicator,MFA),pred_mf(MFA,MF),memberchk(hide(MF),Options)) ;
  236      (prolog_frame_attribute(Frame,hidden,true), \+ memberchk(show_hidden,Options)))
  236,!,
  237   parentFrameOf(Frame,Parent),printStackTrace(Stream,Options,Depth,Parent,PD+2)
  237.
  238printStackTrace(Stream,Options,Depth,Frame,PD):-
  239         printStackFrame(Stream,Options,Frame,PD),
  240         parentFrameOf(Frame,Parent),
  241         printStackTrace(Stream,Options,Depth-1,Parent,PD+2).
  242
  243parentFrameOf(Frame,Parent):-prolog_frame_attribute(Frame,parent,Parent),!.
  244parentFrameOf(_,top).
  245
  246printStackFrame(Stream,_Options,Frame,PD):-sindent(Stream,PD),f0rmt(Stream,'<frame id="~w">~n',[Frame]),fail.
  247printStackFrame(Stream,Options,Frame,PD):-member(Opt,Options), 
  248     \+ memberchk(Opt,[show_hidden,alternative,other_fake_properties,hide(_)]), 
  249     prolog_frame_attribute(Frame,Opt,Value),sindent(Stream,PD),f0rmt(Stream,' ~w = ~q',[Opt,Value]),fail.
  250printStackFrame(Stream,Options,Frame,PD):-memberchk(alternative,Options),delete(Options,alternative,Options2),
  251     prolog_frame_attribute(Frame,alternative,Alt),
  252     sindent(Stream,PD+1),
  253     f0rmt(Stream,'<alt>~n',[]),
  254     printStackFrame(Stream,Options2,Alt,PD+3),
  255     sindent(Stream,PD+1),
  256
  257     f0rmt(Stream,'</alt>~n',[]),
  258     fail.
  259printStackFrame(Stream,_Options,_Frame,PD):-sindent(Stream,PD),f0rmt(Stream,'</frame>~n',[]).
  260
  261sindent(Stream,PD):-f0rmt(Stream,'~N',[]),sindent(Stream,PD,' ').
  262sindent(Stream,PD,Txt):-PD2 is PD,forall(between(0,PD2,_),f0rmt(Stream,Txt,[])).
  263
  264
  265
  266%================================================================
  267:-dynamic(prolog_is_vetted_safe/0).
  268%================================================================
  269%% True means the program skips many many runtime safety checks (runs faster)
  270prolog_is_vetted_safe:-fail.
  271
  272% tryHide(_MFA):-!.
  273tryHide(MFA):- ignore(catch(module_transparent(MFA),_,fail)),asserta(remember_tryHide(MFA)).
  274
  275:-tryHide(tryCatchIgnore/1).
  276tryCatchIgnore(MFA):- error_catch(MFA,_E,true). %%debugFmt(tryCatchIgnoreError(MFA:E))),!.
  277tryCatchIgnore(MFA):- !,debugFmt(tryCatchIgnoreFailed(MFA)).
  278
  279:-dynamic(remember_tryHide/1).
  280
  281inThreadJoin(Goal):-thread_create(Goal,Id,[]),thread_join(Id,_).
  282
  283:-tryHide(prolog_may/1).
  284prolog_may(Call):-notrace((prolog_is_vetted_safe)),!,call(Call).
  285prolog_may(Call):-debugOnError(Call).
  286
  287:-tryHide(prolog_mustEach/1).
  288:- meta_predicate prolog_mustEach(0).
  289prolog_mustEach(Call):-notrace((prolog_is_vetted_safe)),!,call(Call).
  290prolog_mustEach(Call):-prolog_Each(prolog_must,Call).
  291
  292
  293prolog_Each(Pred,(A,B)):- !,prolog_Each(Pred,A),prolog_Each(Pred,B).
  294prolog_Each(Pred,notrace(A)):-!, hotrace(prolog_Each(Pred,A)).
  295prolog_Each(Pred,hotrace(A)):-!, hotrace(prolog_Each(Pred,A)).
  296prolog_Each(Pred,Call):- prolog_call(Pred,Call).
  297
  298:-tryHide(prolog_must/1).
  299:- meta_predicate prolog_must(0).
  300prolog_must(Call):-tracing,!,call(Call).
  301prolog_must(Call):-tracing,!,debugOnError(Call).
  302%%prolog_must(Call):- prolog_is_vetted_safe,!,debugOnError(Call).
  303prolog_must(OneA):- !, (OneA *-> true ; (atrace,OneA)).
  304prolog_must(Call):-prolog_must_call(Call).
  305
  306
  307%%%term_expansion_call(Call, Call0):- term_expansion_safe(Call, Call0),!.
  308%%term_expansion_call(Call, Call0):- term_expansion(Call, Call0),!.
  309term_expansion_call(Call, Call):- (var(Call);atomic(Call)),!.
  310term_expansion_call([A|B],[AA|BB]):-!,term_expansion_call(A,AA),term_expansion_call(B,BB).
  311term_expansion_call(Call0, Call):-compound(Call0),Call0=..[A|RGS],term_expansion_call(RGS,RGS1),RGS\==RGS1,Call=..[A|RGS1].
  312term_expansion_call(Call, Call).
  313
  314%%term_expansion_call(Call, Call0):-atomic(Call),!,Call=Call0.
  315
  316term_expansion_safe(Call, _):-atomic(Call),!,fail.
  317term_expansion_safe(prolog_must(Call), Call0):-!,term_expansion_call(Call, Call0).
  318term_expansion_safe(once(Call), (Call0,!)):-!,term_expansion_call(Call, Call0).
  319term_expansion_safe(prolog_may(Call), Call0):-!,term_expansion_call(Call, Call0).
  320term_expansion_safe(prolog_mustEach(Call), Call0):-!,term_expansion_call(Call, Call0).
  321term_expansion_safe((A,B),(AA,BB)):-!,term_expansion_call(A,AA),term_expansion_call(B,BB).
  322term_expansion_safe((:- G),:- G0):- !,term_expansion_call(G,G0).
  323term_expansion_safe((?- G),:- G0):- !,term_expansion_call(G,G0).
  324term_expansion_safe((_:-true),_):- !,fail.
  325term_expansion_safe((A:-B),(A:-BB)):- term_expansion_call(B,BB),!,B\=BB.
  326
  327term_expansion_safe(Call, Call0):-term_expansion_call(Call, Call0).
  328
  329%%user:term_expansion(Call, Call0):-trace,prolog_is_vetted_safe,!,term_expansion_safe(Call, Call0),!.
  330%%user:expand_goal(Call, Call0):-prolog_is_vetted_safe,!,term_expansion_safe(Call, Call0),!.
  331
  332
  333error_catch(C,E,F):-E=error(E1,E2),!,catch(C,error(E1,E2),F).
  334error_catch(C,E,F):-nonvar(E),!,catch(C,E,F).
  335error_catch(C,E,F):-catch(C,E,(needs_rethrown(E),F)).
  336needs_rethrown(E):- functor(aiml_goto,E,_),!,throw(E).
  337needs_rethrown(E):- functor(aiml_novalue,E,_),!,throw(E).
  338needs_rethrown(_).
  339
  340:-tryHide(tryCatchIgnore/1).
  341:-tryHide(error_catch/3).
  342:-tryHide(prolog_is_vetted_safe/0).
  343
  344prolog_extra_checks:-true.
  345
  346:-tryHide(findall/3).
  347:-tryHide(catch/3).
  348:-tryHide(not/1).
  349:-tryHide(call/1).
  350:-'tryHide'('$bags':findall/3).
  351
  352
  353%:-set_prolog_flag(debug,true).
  354:-set_prolog_flag(debug_on_error,true).
  355:-set_prolog_flag(write_attributes,write).
  356
  357user:prolog_exception_hook(A, B, C, D) :-A=error(evaluation_error(ErrorType), _), writeq(prolog_exception_hook(A, B, C, D)),interactStep(ErrorType,atrace,fail).
  358
  359aiml_error(EE):-copy_term(EE,E),randomVars(E),debugFmt('~q~n',[error(E)]),!,interactStep(E,throw_safe(error(evaluation_error(E),E)),debugFmt(aiml_error(E))).
  360
  361frame_depth(Depth):-prolog_current_frame(Frame),prolog_frame_attribute(Frame,level,Depth).
  362
  363throw_aiml_goto(Output,VotesO):- throw(aiml_goto(Output,VotesO)).
  364throw_aiml_novalue(Output,VotesO):- throw(aiml_novalue(Output,VotesO)).
  365
  366
  367thread_local_flag(F,B,A):-flag(F,B,A).
  368
  369debugFmtList(ListI):-notrace((copy_term(ListI,List),debugFmtList0(List,List0),randomVars(List0),debugFmt(List0))),!.
  370debugFmtList0([],[]):-!.
  371debugFmtList0([A|ListA],[B|ListB]):-debugFmtList1(A,B),!,debugFmtList0(ListA,ListB),!.
  372
  373debugFmtList1(Value,Value):-var(Value),!.
  374debugFmtList1(Name=Number,Name=Number):-atomic(Number).
  375debugFmtList1(Name=Value,Name=Value):-var(Value),!.
  376debugFmtList1(Name=Value,Name=(len:Len)):-copy_term(Value,ValueO),append(ValueO,[],ValueO),is_list(ValueO),length(ValueO,Len),Len>9,!.
  377debugFmtList1(Name=Value,Name=(F:A)):-not(is_list(Value)),functor(Value,F,A).
  378debugFmtList1(Value,shown(Value)).
  379
  380
  381
  382mapsome_openlist(_Pred,EndOfList):-endOfList(EndOfList),!.
  383mapsome_openlist(Pred,[Item|List]):-call(Pred,Item),!,mapsome_openlist(Pred,List).
  384mapsome_openlist(Pred,[_|List]):- mapsome_openlist(Pred,List).
  385mapsome_openlist(_Pred,_):-!.
  386
  387
  388/*
  389dynamic_transparent([]):-!.
  390dynamic_transparent([X]):-dynamic_transparent(X),!.
  391dynamic_transparent([X|Xs]):-!,dynamic_transparent(X),dynamic_transparent(Xs),!.
  392dynamic_transparent(M:F/A):-!, module_transparent(M:F/A),dynamic(M:F/A).
  393dynamic_transparent(F/A):-!,multi_transparent(user:F/A).
  394dynamic_transparent(X):-functor(X,F,A),dynamic_transparent(F/A),!.
  395
  396multi_transparent([]):-!.
  397multi_transparent([X]):-multi_transparent(X),!.
  398multi_transparent([X|Xs]):-!,multi_transparent(X),multi_transparent(Xs),!.
  399multi_transparent(M:F/A):-!, module_transparent(M:F/A),dynamic(M:F/A),multifile(M:F/A).
  400multi_transparent(F/A):-!,multi_transparent(user:F/A).
  401multi_transparent(X):-functor(X,F,A),multi_transparent(F/A),!.
  402*/

  403
  404:- module_transparent(user:library_directory/1).
  405
  406/*
  407throw_safe(Exc):-atrace,throw(Exc).
  408string_to_atom_safe(ISO,LISTO):-LISTO==[],!,string_to_atom(ISO,'').
  409string_to_atom_safe(ISO,LISTO):-string_to_atom(ISO,LISTO).
  410atom_concat_safe(L,R,A):- ((atom(A),(atom(L);atom(R))) ; ((atom(L),atom(R)))), !, atom_concat(L,R,A),!.
  411exists_file_safe(File):-prolog_must(atomic(File)),exists_file(File).
  412exists_directory_safe(File):-prolog_must(atomic(File)),exists_directory(File).
  413concat_atom_safe(List,Sep,[Atom]):-atom(Atom),!,atomic_list_concat_aiml(List,Sep,Atom),!.
  414concat_atom_safe(List,Sep,Atom):-atom(Atom),!,atomic_list_concat_aiml(ListM,Sep,Atom),!,List = ListM.
  415concat_atom_safe(List,Sep,Atom):- atomic_list_concat_aiml(List,Sep,Atom),!.
  416upcase_atom_safe(A,B):-atom(A),upcase_atom(A,B),!.
  417time_file_safe(F,INNER_XML):-exists_file_safe(F),time_file(F,INNER_XML).
  418list_to_set_safe(A,A):-(var(A);atomic(A)),!.
  419list_to_set_safe([A|AA],BB):- (not(not(lastMember(A,AA))) -> list_to_set_safe(AA,BB) ; (list_to_set_safe(AA,NB),BB=[A|NB])),!.
  420*/

  421
  422%================================================================
  423% maplist/[2,3]
  424% this must succeed  maplist_safe(=,[X,X,X],[1,2,3]).
  425% well if its not "maplist" what shall we call it?
  426%================================================================
  427% so far only the findall version works .. the other runs out of local stack!?
  428/*
  429maplist_safe(_Pred,[]):-!.
  430maplist_safe(Pred,LIST):-findall(E,(member(E,LIST),prolog_must(apply(Pred,[E]))),LISTO),!, ignore(LIST=LISTO),!.
  431%% though this should been fine %%  maplist_safe(Pred,[A|B]):- copy_term(Pred+A, Pred0+A0), prolog_must(once(call(Pred0,A0))),     maplist_safe(Pred,B),!.
  432
  433maplist_safe(_Pred,[],[]):-!.
  434maplist_safe(Pred,LISTIN, LIST):-!, findall(EE, ((member(E,LISTIN),prolog_must(apply(Pred,[E,EE])))), LISTO),  ignore(LIST=LISTO),!.
  435%% though this should been fine %% maplist_safe(Pred,[A|B],OUT):- copy_term(Pred+A, Pred0+A0), prolog_mustEach(once(call(Pred0,A0,AA))),  maplist_safe(Pred,B,BB), !, ignore(OUT=[AA|BB]).
  436*/

  437
  438:- dynamic(buggerDir/1).
  439:- abolish(buggerDir/1),prolog_load_context(directory,D),asserta(buggerDir(D)).
  440:- dynamic(buggerFile/1).
  441:- abolish(buggerFile/1),prolog_load_context(file,D),asserta(buggerFile(D)).
  442
  443
  444hasLibraryBuggerySupport :- absolute_file_name(library('logicmoo/logicmoo_util_library.pl'),File),exists_file(File).
  445
  446throwNoLibBugger:- atrace,absolute_file_name('.',Here), buggerFile(BuggerFile), listing(user:library_directory/1), throw(error(existence_error(url, BuggerFile), context(_, status(404, [BuggerFile, from( Here) ])))).
  447
  448addLibraryDir :- buggerDir(Here),atom_concat(Here,'/..',UpOne), absolute_file_name(UpOne,AUpOne),asserta(user:library_directory(AUpOne)).
  449
  450% if not has library suport, add this direcotry as a library directory
  451% :-not(hasLibraryBuggerySupport) -> addLibraryDir ; true .
  452
  453%:-hasLibraryBuggerySupport->true;throwNoLibBugger.
  454
  455% TODO remove this next line
  456% :-ensure_loaded(library('logicmoo/logicmoo_util_bugger.pl')).
  457% and replace with...
  458
  459
  460% ==========================================================
  461%  can/will Tracer.
  462% ==========================================================
  463
  464:- if( \+ predicate_property(canTrace,defined)).
  465:- dynamic(canTrace/0).
  466canTrace.
  467:- endif.
  468
  469%isConsole :- telling(user).
  470isConsole :- current_output(X),!,stream_property(X,alias(user_output)).
  471
  472willTrace:-not(isConsole),!,fail.
  473willTrace:-canTrace.
  474
  475hideTrace:-
  476   hideTrace([hotrace/1], -all),
  477   %%hideTrace(computeInnerEach/4, -all),
  478
  479   hideTrace(
  480     [maplist_safe/2, 
  481              maplist_safe/3], -all),
  482
  483
  484   hideTrace([hideTrace/0,
  485         canTrace/0,
  486         atrace/0,         
  487         willTrace/0], -all),
  488
  489   hideTrace([
  490         traceafter_call/1,
  491
  492         notrace_call/1], -all),
  493
  494   hideTrace(user:[
  495      call/1,
  496      call/2,
  497      apply/2,
  498      '$bags':findall/3,
  499      '$bags':findall/4,
  500      notrace/1,
  501      ','/2,
  502      catch/3,
  503      member/2], -all),
  504
  505   hideTrace(user:setup_call_catcher_cleanup/4,-all),
  506
  507   hideTrace(system:throw/1, +all),
  508   %%hideTrace(system:print_message/2, +all),
  509   hideTrace(user:message_hook/3 , +all),
  510   hideTrace(system:message_to_string/2, +all),
  511   !,hideRest,!.
  512   %%findall(File-F/A,(functor_source_file(M,P,F,A,File),M==user),List),sort(List,Sort),debugFmt(Sort),!.
  513
  514hideRest:- fail, logicmoo_util_library:buggerDir(BuggerDir),
  515      functor_source_file(M,_P,F,A,File),atom_concat(BuggerDir,_,File),hideTraceMFA(M,F,A,-all),
  516      fail.
  517hideRest:- functor_source_file(system,_P,F,A,_File),hideTraceMFA(system,F,A,-all), fail.
  518hideRest:-doTryHides.
  519
  520:- meta_predicate(hideTrace(:,+)).
  521
  522functor_source_file(M,P,F,A,File):-functor_source_file0(M,P,F,A,File). % prolog_must(ground((M,F,A,File))),prolog_must(user:nonvar(P)).
  523functor_source_file0(M,P,F,A,File):-current_predicate(F/A),functor(P,F,A),source_file(P,File),predicate_module(P,M).
  524
  525predicate_module(P,M):- predicate_property(P,imported_from(M)),!.
  526predicate_module(M:_,M):-!. %strip_module(P,M,_F),!.
  527predicate_module(_P,user):-!. %strip_module(P,M,_F),!.
  528%predicate_module(P,M):- strip_module(P,M,_F),!.
  529
  530hideTrace(_:A, _) :-
  531        var(A), !, atrace, fail,
  532        throw(error(instantiation_error, _)).
  533hideTrace(_:[], _) :- !.
  534hideTrace(A:[B|D], C) :- !,
  535        hideTrace(A:B, C),
  536        hideTrace(A:D, C),!.
  537
  538hideTrace(M:A,T):-!,hideTraceMP(M,A,T),!.
  539hideTrace(MA,T):-hideTraceMP(_,MA,T),!.
  540
  541hideTraceMP(M,F/A,T):-!,hideTraceMFA(M,F,A,T),!.
  542hideTraceMP(M,P,T):-functor(P,F,0),atrace,hideTraceMFA(M,F,_A,T),!.
  543hideTraceMP(M,P,T):-functor(P,F,A),hideTraceMFA(M,F,A,T),!.
  544
  545hideTraceMFA(_,M:F,A,T):-!,hideTraceMFA(M,F,A,T),!. 
  546hideTraceMFA(M,F,A,T):-user:nonvar(A),functor(P,F,A),predicate_property(P,imported_from(IM)),IM \== M,!,nop(debugFmt(doHideTrace(IM,F,A,T))),hideTraceMFA(IM,F,A,T),!.
  547hideTraceMFA(M,F,A,T):-hideTraceMFAT(M,F,A,T),!.
  548
  549hideTraceMFAT(M,F,A,T):-doHideTrace(M,F,A,T),!.
  550
  551doHideTrace(_M,_F,_A,[]):-!.
  552doHideTrace(M,F,A,[hide|T]):- tryHide(M:F/A),!,doHideTrace(M,F,A,T),!.
  553doHideTrace(M,F,A,ATTRIB):- tryHide(M:F/A),!, tryCatchIgnore(trace(M:F/A,ATTRIB)),!.
  554
  555
  556
  557bugger:-hideTrace,traceAll,error_catch(noguitracer,_,true),debug,list_undefined.
  558
  559singletons(_).
  560
  561% ===================================================================
  562:-thread_local(lineInfoElement/4).
  563
  564nthAnswerOf(Call,Nth):-flag(nthAnswerOf,_,Nth), Call,flag(nthAnswerOf,R,R-1),R=1,!.
  565
  566toReadableObject(I,I):- (var(I);atomic(I)),!.
  567toReadableObject([I|_],[[fctx]]):-nonvar(I),I=frame(_,_,_),!.
  568toReadableObject(I,fctxa(Name)):-nonvar(I),I=frame(Name,_,_),!.
  569toReadableObject([I|N],[I0|N0]):-!,toReadableObject(I,I0),toReadableObject(N,N0),!.
  570toReadableObject(Comp,Comp2):-compound(Comp),Comp=..[L,I|ST],toReadableObject([I|ST],[OI|OIST]),debugOnError(Comp2=..[L,OI|OIST]),!.
  571toReadableObject(I,I):-!.
  572
  573:- if( \+ predicate_property(exists_file_safe(_),defined)).
  574exists_file_safe(File):-prolog_must(atomic(File)),exists_file(File).
  575exists_directory_safe(File):-prolog_must(atomic(File)),exists_directory(File).
  576:- endif.
  577% ===============================================================================================
  578% listify/ unlistify / unresultify
  579% ===============================================================================================
  580
  581%listify(OUT,OUT):-not(not(is_list(OUT))),!.
  582%listify(OUT,[OUT]).
  583
  584unlistify([L],O):-nonvar(L),unlistify(L,O),!.
  585unlistify(L,L).
  586
  587%%:-traceLevel(unresultify/2,+enter).
  588unresultify(Var,Var):-(var(Var);atomic(Var)),!.
  589unresultify([DictI|NV],Dict):-nonvar(DictI),NV==[],!,unresultify(DictI,Dict),!.
  590unresultify(Fun,Dict):-resultOrProof(Fun,DictI),!,unresultify(DictI,Dict),!.
  591unresultify(Var,Var).
  592
  593resultOrProof(element(_,_,_),_):-!,fail.
  594resultOrProof(Term,Mid):-compound(Term),resultOrProof0(Term,Mid).
  595resultOrProof0(_=Mid,Mid):-!.
  596resultOrProof0(Term,Mid):-Term=..[RP,Mid|_],member(RP,[result,proof,fromTo,verbatum]),!.
  597
  598%%unresultifyC(DictI,[Dict]):-atom(DictI),member(Chop,['0','1']),atom_concat(Dict,Chop,DictI),!.
  599unresultifyC(DictI,Dict):-unresultify(DictI,Dict),DictI\==Dict,!.
  600
  601deleteAll(A,[],A):-!.
  602deleteAll(A,[L|List],AA):-delete(A,L,AAA),deleteAll(AAA,List,AA),!.
  603
  604
  605% ===============================================================================================
  606%% join_path(CurrentDir,Filename,Name)
  607% ===============================================================================================
  608
  609join_path(CurrentDir,Filename,Name):-
  610         atom_ensure_endswith(CurrentDir,'/',Out),atom_ensure_endswith('./',Right,Filename),
  611         atom_concat(Out,Right,Name),!.
  612join_path(CurrentDir,Filename,Name):- directory_file_path(CurrentDir,Filename,Name).
  613
  614atom_ensure_endswith(A,E,A):-atom(E),atom_concat(_Left,E,A),!.
  615atom_ensure_endswith(A,E,O):-atom(A),atom(E),atom_concat(A,E,O),!.
  616atom_ensure_endswith(A,E,O):-atom(A),atom(O),atom_concat(A,E,O),!.
  617atom_ensure_endswith(A,O,O):-atom(A),atom(O),!.
  618
  619
  620atomic_list_concat_aiml(A,C):-  
  621 catch(concat_atom(A,C),_,
  622  catch(atomic_list_concat(A,C),_,
  623    (atomic_list_concat_safe(A,C)))),!.
  624
  625atomic_list_concat_aiml(A,B,C):- B=='', !,atomic_list_concat_aiml(A,C).
  626atomic_list_concat_aiml(A,B,C):-  
  627 catch(atomic_list_concat(A,B,C),_,
  628  catch(concat_atom(A,B,C),_,
  629    atomic_list_concat_safe(A,B,C))),!.
  630
  631
  632os_to_prolog_filename(OS,_PL):-prolog_must(atom(OS)),fail.
  633os_to_prolog_filename(_OS,PL):-prolog_must(var(PL)),fail.
  634os_to_prolog_filename(OS,PL):-exists_file_safe(OS),!,PL=OS.
  635os_to_prolog_filename(OS,PL):-exists_directory_safe(OS),!,PL=OS.
  636os_to_prolog_filename(OS,PL):-local_directory_search_combined(CurrentDir),join_path(CurrentDir,OS,PL),(exists_file_safe(PL);exists_directory_safe(PL)),!.
  637os_to_prolog_filename(OS,PL):- debugOnError(os_to_prolog_filename0(OS,PL)).
  638os_to_prolog_filename0(OS,PL):-atom(OS),atomic_list_concat_aiml([X,Y|Z],'\\',OS),
  639  atomic_list_concat_aiml([X,Y|Z],'/',OPS),!,
  640  os_to_prolog_filename(OPS,PL).
  641os_to_prolog_filename0(OS,PL):-atom_concat_safe(BeforeSlash,'/',OS),os_to_prolog_filename(BeforeSlash,PL).
  642os_to_prolog_filename0(OS,PL):-absolute_file_name(OS,OSP),OS \= OSP,!,os_to_prolog_filename(OSP,PL).
  643
  644
  645% ===============================================================================================
  646%% dont really call_with_depth_limit/3 as it spoils the debugger
  647% ===============================================================================================
  648call_with_depth_limit_traceable(G,Depth,Used):-tracing,!,G,ignore(Depth=1),ignore(Used=1).
  649%%call_with_depth_limit_traceable(G,Depth,Used):-call_with_depth_limit(G,Depth,Used),debugFmt(depth=Used),Used\==depth_limit_exceeded,!.
  650call_with_depth_limit_traceable(G,_Depth,_Used):-G.
  651
  652throw_safe(Error):-var(Error),atrace,debugFmt('~N throwing VAR?! ~N'),throw(Error). 
  653throw_safe(aiml_goto(A,B)):- throw(aiml_goto(A,B)).
  654throw_safe(aiml_novalue(A,B)):- throw(aiml_novalue(A,B)).
  655throw_safe(error(A,B)):- atrace, throw(error(A,B)).
  656throw_safe(Exc):-throw(error(Exc,Exc)).
  657
  658:-op(1150,fx,meta_predicate_transparent).
  659
  660must_assign(From,To):-To=From,!.
  661must_assign(From,To):-atrace,To=From.
  662
  663:-tryHide(system:catch/3).
  664:-tryHide(system:not/1).
  665
  666:-tryHide(cyc:ctrace/0).
  667
  668:-tryHide(debugOnError/1).
  669:-tryHide(debugOnError0/1).
  670debugOnError(Call):-notrace((prolog_is_vetted_safe)),!,call(Call).
  671debugOnError(Call):-prolog_ecall(debugOnError0,Call).
  672debugOnError0(Call):- E = error(_,_),error_catch(Call,E,(notrace(debugFmt(caugth1st(Call,E))),debugOnError1((atrace,Call)))).
  673debugOnError1(Call):- E = error(_,_),error_catch(Call,E,(notrace(debugFmt(caugth2nd(Call,E))),throw(E))).
  674
  675:-tryHide(prolog_must_call/1).
  676:-tryHide(prolog_must_call0/1).
  677prolog_must_call(Call):-notrace((prolog_is_vetted_safe)),!,call(Call).
  678prolog_must_call(Call):- prolog_ecall(prolog_must_call0,Call).   
  679prolog_must_call0(Call):- atLeastOne(Call,interactStep(prolog_must_call0,Call,true)).
  680
  681
  682:-tryHide(prolog_must_tracing/1).
  683:-tryHide(prolog_must_tracing0/1).
  684prolog_must_tracing(Call):- prolog_ecall(prolog_must_tracing0,Call).   
  685%%%prolog_must_tracing0(Call):-trace(Call,[-all,+fail]), atLeastOne(Call,atrace,interactStep(prolog_must_tracing0,aiml_error(Call),aiml_error(Call))).
  686prolog_must_tracing0(Call):-Call.
  687
  688
  689datatypeMustPred(prolog_must_call0,Call):-functor(Call,F,A),((A=1,test_pred(F));member(F,[=,==,\==])).
  690
  691test_pred(T):-member(T,[var,nonvar,atom,atomic,number,is_list,compound,ground,not]),!.
  692
  693:-tryHide(prolog_ecall/2).
  694prolog_ecall(Pred,Call):-notrace((prolog_is_vetted_safe)),!,call(Pred,Call).
  695prolog_ecall(Pred,Call):-prolog_ecall(call,Pred,Call).
  696
  697prolog_ecall(_Conj,_Pred,Call):-var(Call),!,atrace,randomVars(Call).
  698prolog_ecall(Conj,Pred,Call):-tracing,!,prolog_ecall_conj(Conj,Pred,Call).
  699prolog_ecall(call,Pred,notrace(Call)):-prolog_ecall(notrace,Pred,Call).
  700prolog_ecall(Conj,Pred,(X->Y;Z)):-!,(prolog_call(Conj,X) -> prolog_ecall(Conj,Pred,Y) ; prolog_ecall(Conj,Pred,Z)).
  701prolog_ecall(Conj,Pred,(X->Y)):-!,(prolog_call(Conj,X)->prolog_ecall(Conj,Pred,Y)).
  702prolog_ecall(Conj,Pred,catch(C,E,H)):-!,catch(prolog_ecall(Conj,Pred,C),E,prolog_ecall(Conj,Pred,H)).
  703prolog_ecall(Conj,Pred,error_catch(C,E,H)):-!,error_catch(prolog_ecall(Conj,Pred,C),E,prolog_ecall(Conj,Pred,H)).
  704prolog_ecall(Conj,Pred,(X;Y)):-!,prolog_ecall(Conj,Pred,X);prolog_ecall(Conj,Pred,Y).
  705prolog_ecall(Conj,Pred,(X,Y)):- !,
  706                             debugOnError(prolog_call(Conj,X)), %%prolog_ecall(Conj,Pred,X),
  707                             prolog_ecall(Conj,Pred,Y).
  708prolog_ecall(Conj,Pred,prolog_must(Call)):-!,prolog_must(prolog_ecall(Conj,Pred,Call)).
  709prolog_ecall(Conj,_Pred,prolog_may(Call)):-!,prolog_may(prolog_call(Conj,Call)).
  710%%prolog_ecall(Conj,Pred,Call):- datatypeMustPred(Pred,Call),!,((prolog_call(Pred,Call),!);atrace).
  711prolog_ecall(_Conj,_Pred,Call):- fail, ignore((Call=atom(_),atrace)), 
  712    predicate_property(Call,number_of_clauses(_Count)),
  713    error_catch((clause(Call,AB),AB\==true),_,((atrace,predicate_property(Call,number_of_clauses(_Count2)),fail))),!,
  714    clause(Call,Body),debugOnError(Body).
  715prolog_ecall(Conj,Pred,Call):-prolog_ecall_conj(Conj,Pred,Call).
  716
  717:-tryHide(prolog_ecall_conj/3).
  718prolog_ecall_conj(call,call,Call):- !, Call.
  719prolog_ecall_conj(call,Pred,Call):- !, prolog_call(Pred,Call).
  720prolog_ecall_conj(Pred,call,Call):- !, prolog_ecall(call,Pred,Call).
  721prolog_ecall_conj(Conj,Pred,Call):- !, prolog_call(Pred,prolog_call(Conj,Call)).
  722prolog_ecall_conj(Conj,Pred,Call):- debugOnError0(prolog_call(Pred,prolog_call(Conj,Call))).
  723
  724prolog_call(call,Call):-!,Call.
  725prolog_call(Pred,Call):-call(Pred,Call).
  726
  727:-tryHide(atLeastOne/1).
  728:-tryHide(atLeastOne/2).
  729:-tryHide(atLeastOne0/3).
  730atLeastOne(Call):-notrace((prolog_is_vetted_safe)),!,call(Call).
  731atLeastOne(OneA):- atLeastOne(OneA,(atrace,OneA)).
  732atLeastOne(OneA,Else):- !, (OneA *-> true ; Else).
  733atLeastOne(OneA,Else):- gensym(atLeastOne,AtLeast),flag(AtLeast,_,0),atLeastOne0(AtLeast,OneA,Else).
  734
  735atLeastOne0(AtLeast,OneA,_Else):- OneA, flag(AtLeast,X,X+1).
  736atLeastOne0(AtLeast,OneA,Else):- flag(AtLeast,X,X),!,X=0,debugFmt(notAtLeastOnce(OneA)),tryCatchIgnore(Else).
  737
  738%%atLeastOne0(OneA,_Else):-copy_term(OneA,One),findall(One,call(One),OneL),[_|_]=OneL,!,member(OneA,OneL).
  739%%atLeastOne0(OneA,Else):-debugFmt(failed(OneA)),!,Else,!,fail.
  740
  741
  742atLeastN(OneA,N):- atLeastN(OneA,N,(atrace,OneA)).
  743atLeastN(OneA,1,Else):-!,atLeastOne(OneA,Else).
  744atLeastN(OneA,N,Else):- gensym(atLeastN,AtLeast),flag(AtLeast,_,0),atLeastN0(AtLeast,N,OneA,Else).
  745atLeastN0(AtLeast,_N,OneA,_Else):- OneA, flag(AtLeast,X,X+1).
  746atLeastN0(AtLeast,N,OneA,Else):- flag(AtLeast,X,X),!,X<N,debugFmt(atLeastN(OneA,X>=N)),tryCatchIgnore(Else).
  747
  748
  749
  750randomVars(Term):- R is random(10000)/10000, Start is round('*'(R,1000000)), !,
  751  numbervars(Term, Start, _End, [attvar(skip),functor_name('$VAR')]).
  752
  753prolog_must_not(Call):-Call,!,atrace,!,aiml_error(prolog_must_not(Call)).
  754prolog_must_not(_Call):-!.
  755
  756%:- meta_predicate dynamic_if_missing(:).
  757%:- meta_predicate meta_predicate_transparent(:).
  758
  759
  760dynamic_if_missing(F/A):-functor(X,F,A),predicate_property(X,_),!.
  761dynamic_if_missing(F/A):- 
  762  dynamic([F/A]).
  763
  764meta_predicate_transparent(X):-strip_module(X,M,F),!, meta_predicate_transparent(M,F).
  765meta_predicate_transparent(M,(X,Y)):-!,meta_predicate_transparent(M,X),meta_predicate_transparent(M,Y),!.
  766meta_predicate_transparent(_M,X):-atom(X),!.
  767meta_predicate_transparent(_M,X):- 
  768   prolog_mustEach((   
  769   arg(1,X,A),functor(X,F,_),
  770   FA=F/A,
  771   dynamic_if_missing(FA),
  772   %module_transparent(FA),
  773   %%meta_predicate(X),
  774   %trace(FA, -all),
  775   %%tryHide(FA),
  776   !)).
  777
  778
  779asserta_new(_Ctx,NEW):-ignore(retract(NEW)),asserta(NEW).
  780writeqnl(_Ctx,NEW):- debugFmt('~N%%LOADING ~q.~N',[NEW]),!.
  781
  782
  783revappend([], Ys, Ys).
  784revappend([X|Xs], Ys, Zs) :- revappend(Xs, [X|Ys], Zs).
  785
  786reverseA(Xs,Ys) :- revappend(Xs,[],Ys).
  787
  788appendAttributes(_Ctx,L,R,AA):-hotrace((mergeAppend0(L,R,A),list_to_set_safe(A,AA))),!.
  789mergeAppend0(L,R,R):-var(L),!,var(R),!.
  790mergeAppend0(L,R,A):-var(R),append(L,R,A),!.
  791mergeAppend0(L,R,A):-var(L),append(L,R,A),!.
  792mergeAppend0(L,[R|RR],A):-eqmember(R,L),mergeAppend0(L,RR,A).
  793mergeAppend0([L|LL],R,A):-eqmember(L,R),mergeAppend0(LL,R,A).
  794mergeAppend0(L,R,A):-append(L,R,A).
  795
  796eqmember(E,List):-copy_term_numvars(E:List,E0:List0),member(E0,List0).
  797
  798list_to_set_safe(A,A):-(var(A);atomic(A)),!.
  799list_to_set_safe([A|AA],BB):- (not(not(lastMember(A,AA))) -> list_to_set_safe(AA,BB) ; (list_to_set_safe(AA,NB),BB=[A|NB])),!.
  800
  801
  802lastMember(E,List):-hotrace(lastMember0(E,List)).
  803
  804lastMember0(_E,List):-var(List),!,fail.
  805lastMember0(E,[H|List]):-lastMember0(E,List);E=H.
  806
  807lastMember(E,List,Rest):-hotrace(lastMember0(E,List,Rest)).
  808
  809lastMember0(E,List,Rest):-lastMember0(E,List),!,delete_safe(List,E,Rest),!.
  810lastMember0(E,List,Rest):-lastMember0(EE,List),!,lastMember0(E,EE,Rest),!,atrace. %%delete_safe(List,EE,Rest),!.
  811
  812delete_safe(List,_E,Rest):-var(List),!,Rest=List.
  813delete_safe(List,E,Rest):-is_list(List),!,delete(List,E,Rest).
  814delete_safe([H|List],E,Rest):- H==E,!,delete_safe(List,E,Rest).
  815delete_safe([H|List],E,[H|Rest]):-delete_safe(List,E,Rest).
  816
  817
  818getKeyValue(FullList,N=V):-lastMember(N=V,FullList),!.
  819%%addKeyValue(FullList,N=V):-nonvar(N),!,append(_Closed,[N=V|_],FullList),!.
  820addKeyValue(FullList,NV):- prolog_must((not(ground(FullList)),nonvar(NV))),append(_Closed,[NV|_],FullList),!.
  821
  822
  823lastMember2(E,List):-to_open_list(_,Closed,_Open,List),reverse(Closed,Rev),member(E,Rev).
  824
  825%lastMember(End,List) :- append(_,[End|_],List).
  826
  827
  828to_open_list(FullList,Closed,Open,FullList) :- append(Closed,Open,FullList),var(Open),!.
  829to_open_list(Closed,Closed,Open,FullList) :- append(Closed,Open,FullList),!.
  830
  831
  832copy_term_numvars(OLD,NEW):-copy_term(OLD,NEW),numbervars(NEW,0,_).
  833
  834%%%retractall(E):- retractall(E),functor(E,File,A),dynamic(File/A),!.
  835
  836pp_listing(_Pred):-!. %%functor(Pred,File,A),functor(FA,File,A),listing(File),nl,findall(NV,predicate_property(FA,NV),LIST),writeq(LIST),nl,!.
  837
  838% =================================================================================
  839% Utils
  840% =================================================================================
  841
  842printPredCount(Msg,Pred,N1):- compound(Pred), prolog_mustEach((arg(_,Pred,NG))),nonvar(NG),!,
  843   findall(Pred,Pred,LEFTOVERS),length(LEFTOVERS,N1),debugFmt(num_clauses(Msg,Pred,N1)),!.
  844
  845printPredCount(Msg,Pred,N1):-!,functor(Pred,File,A),functor(FA,File,A), predicate_property(FA,number_of_clauses(N1)),debugFmt(num_clauses(Msg,File/A,N1)),!.
  846
  847:- if( \+ current_predicate(fresh_line/0)).
  848fresh_line:-current_output(Strm),fresh_line(Strm),!.
  849fresh_line(Strm):-stream_property(Strm,position('$stream_position'(_,_,POS,_))),ifThen(POS>0,nl(Strm)),!.
  850fresh_line(Strm):-atrace,nl(Strm),!.
  851:- endif.
  852
  853% =================================================================================
  854% Loader Utils
  855% =================================================================================
  856
  857dynamic_load(Key,PLNAME):- creating_aiml_file(Key,PLNAME),throw_safe(creating_aiml_file(Key,PLNAME)),assert(pending_aiml_file(Key,PLNAME)).
  858dynamic_load(Key,PLNAME):- not(ground(dynamic_load(Key,PLNAME))),throw_safe(not(ground(dynamic_load(Key,PLNAME)))).
  859dynamic_load(Key,PLNAME):- loaded_aiml_file(Key,PLNAME,Time),!,debugFmt(loaded_aiml_file(Key,PLNAME,Time)).
  860dynamic_load(Key,_PLNAME):- nonvar(Key), once(cateForFile(_,Key,Cate)),Cate,!.
  861
  862dynamic_load(Key,PLNAME):- 
  863   global_pathname(PLNAME,PN),
  864   exists_file_safe(PLNAME),
  865   time_file_safe(PN,Time),
  866   assert(loaded_aiml_file(Key,PLNAME,Time)),
  867   dynamic_load2(Key,PLNAME).
  868
  869dynamic_load2(_Key,PLNAME):-consult(PLNAME),!.
  870
  871dynamic_load2(_Key,PLNAME):-
  872     open(PLNAME, read, In, []),
  873     repeat,
  874      line_count(In,Lineno),
 double_quotes(_DQBool)
  876      Options = [variables(_Vars),variable_names(_VarNames),singletons(_Singletons),comment(_Comment)],
  877      error_catch((read_term(In,Term,[syntax_errors(error)|Options])),E,(debugFmt(E),fail)),      
  878      load_term(Term,[line_count(Lineno),file(PLNAME),stream(In)|Options]),
  879     Term==end_of_file,
  880     close(In)
  880.
  881
  882load_term(end_of_file,_Options):-!.
  883load_term(Term,Options):-error_catch(load_term2(Term,Options),E,(debugFmt(error(load_term(Term,Options,E))),throw_safe(E))).
  884
  885load_term2(':-'(Term),Options):-!,load_dirrective(Term,Options),!.
  886load_term2(:-(H,B),Options):-!,load_assert(H,B,Options).
  887load_term2(Fact,Options):-!,load_assert(Fact,true,Options).
  888
  889load_assert(H,B,_Options):-assert((H:-B)),!.
  890
  891load_dirrective(include(PLNAME),_Options):-  (atom_concat_safe(Key,'.pl',PLNAME);Key=PLNAME),!, dynamic_load(Key,PLNAME).
  892load_dirrective(module(M,Preds),_Options):-!,module(M),maplist(M:export,Preds).
  893load_dirrective(Term,_Options):-!,Term.
  894
  895showProfilerStatistics(FileMatch):-
  896   statistics(global,Mem), MU is (Mem / 1024 / 1024),
  897   printPredCount('showProfilerStatistics: '(MU),FileMatch,_N1).
  898
  899aimlPredCount:-printAll(aimlPredCount(_,_,0)),printAll((aimlPredCount(Pred,File,Count),Count>0),aimlPredCount(Pred,File,Count)).
  900aimlPredCount(Pred,File,Count):-source_file(File),atom_contains(File,'aiml'),source_file(Pred,File),functor(Pred,F,A),current_predicate(F/A),
  901    predicate_property(Pred,dynamic),predicate_property(Pred,number_of_clauses(Count)).
  902
  903% =================================================================================
  904% list_to_set_preserve_order/2
  905% =================================================================================
  906list_to_set_preserve_order([],[]):-!.
  907list_to_set_preserve_order([H|T],[H|TT]):-delete(T,H,M),list_to_set_preserve_order(M,TT).
  908
  909% =================================================================================
  910% Utils
  911% =================================================================================
  912
  913% ===============================================================================================
  914% UTILS
  915% ===============================================================================================
  916
  917callInteractive(Term,Var):-time(error_catch(callInteractive0(Term,Var),E,aiml_error(E))),!.
  918
  919%callInteractive0(Term,_):-atom(Term),!,Term,!,writeln(called(Term)),!.
  920callInteractive0(Term,Var):- fresh_line,programk:call(Term), (writeq(answer(Term:Var)), fresh_line,fail).
  921callInteractive0(_,_):-fresh_line,!.
  922
  923%getWordTokens(WORDS,TOKENS):-atomic_list_concat_aiml(TOKENS,' ',WORDS).
  924%is_string(S):-string(S).
  925
  926
  927sentenceBreakChar(Last):-member(Last,[?]);sentenceEnder_NoQuestion(Last).
  928sentenceEnderOrPunct(Last):-member(Last,[?]);sentenceEnderOrPunct_NoQuestion(Last).
  929sentenceEnderOrPunct_NoQuestion(Last):-member(Last,[(',')]);sentenceEnder_NoQuestion(Last).
  930sentenceEnder_NoQuestion(Last):-member(Last,[('.'),('!'),('\n'),('\n\n'),('\r\n')]).
  931
  932removePMark(UCase,Atoms):-append(AtomsPre,[Last],UCase),sentenceEnderOrPunct(Last),!,removePMark(AtomsPre,Atoms).
  933removePMark(Atoms,Atoms).
  934
  935leftTrim([B|Before],ToRemove,After):-call(ToRemove,B),!,leftTrim(Before,ToRemove,After).
  936leftTrim(After,_ToRemove,After):-!.
  937leftTrim([B|Before],ToRemove,[B|After]):-leftTrim(Before,ToRemove,After).
  938leftTrim([],_ToRemove,[]):-!.
  939
  940rightTrim(Before,ToRemove,After):-append(LeftOf,[B],Before),call(ToRemove,B),!,rightTrim(LeftOf,ToRemove,After).
  941rightTrim(After,_ToRemove,After):-!.
  942
  943randomPick(List,Ele):-length(List,Len),Pick is random(Len),nth0(Pick,List,Ele),!.
  944
  945%================================================================
  946% Atom / String functions
  947%================================================================
  948atomsSameCI(Name1,Name1):-!.
  949atomsSameCI(Name1,Name2):-atom(Name1),atom(Name2),literal_atom(Name1,D1),literal_atom(Name2,D2),!,D1=D2.
  950
  951clean_codes(X,Y):-trim(X,Y),!.  % actually cyc:trim/2
  952clean_codes(X,X).
  953
  954%clean_out_atom(X,Y):-atomWSplit(X,C),delete(C,'',O),concat_atom_safe(C,' ',Y).
  955clean_out_atom(X,Y):-atom_codes(X,C),clean_codes(C,D),!,atom_codes(X,D),!,Y=X.
  956
  957%%%%%% everything tries to lowercase (fails case-sensitive tests)
  958%%atomWSplit(A,B):-atom(A),literal_atom(A,L),hotrace((cyc:atomSplit(L,BB),!,BB=B)).
  959%%%%%% puts backspaces in places of no spaces
  960:-dynamic(atomWSplit_cached/2).
  961:-volatile(atomWSplit_cached/2).
  962expire1Cache:-retractall(atomWSplit_cached(_,_)).
  963%%atomWSplit(A,B):- hotrace((cyc:atomWSplit(A,BB),!,BB=B)).
  964atomWSplit(A,B):-prolog_must(ground(A)),atomWSplit_cached(A,B),!.
  965%atomWSplit(A,B):- hotrace((cyc:atomSplit(A,BB),!,BB=B,asserta(atomWSplit_cached(A,B)))).
  966atomWSplit(A,B):- hotrace((atomSplit_oldy(A,BB),!,BB=B,asserta(atomWSplit_cached(A,B)))).
  967
  968
  969atomSplit_oldy(Atom,WordsO):- atomSplit_oldy(Atom,WordsO,[' ','\t','\n','\r',' ','!','"','#','$','%','&','\'',
  970     '\v','\f',
  971    '(',')','*','+',',','-','.','/',':',';','<','=','>','?','@','[','\\',']','^','_','`','{','|','}','~']
  972    ).     
  973
  974:- expire1Cache.
  975
  976atomSplit_oldy(Atom,WordsO,List):- 
  977  notrace((atom(Atom), atomic_list_concat(Words1,' ',Atom),!, 
  978   atomSplit_oldy2(Words1,Words,List),!,Words=WordsO)).
  979%atomSplit_oldy(Atom,Words,[Space|List]):-notrace((var(Atom),ground(Words),!,atomic_list_concat(Words,Space,AtomO),!,Atom=AtomO)).
  980
  981atomSplit_oldy2([],[],_List):-!.
  982atomSplit_oldy2([Mark|S],[Mark|Words],List):- member(Mark,List),!,atomSplit_oldy2(S,Words,List),!.
  983atomSplit_oldy2([W|S],[A,Mark|Words],List):- member(Mark,List),atom_concat(A,Mark,W),!,atomSplit_oldy2(S,Words,List),!.
  984atomSplit_oldy2([W|S],[Mark,A|Words],List):- member(Mark,List),atom_concat(Mark,A,W),!,atomSplit_oldy2(S,Words,List),!.
  985atomSplit_oldy2([Word|S],Words,List):- member(Space,List),Atoms=[_,_|_],atomic_list_concat(Atoms,Space,Word),!,
  986                  interleave(Atoms,Space,Left),
  987                  atomSplit_oldy2(S,Right,List),append(Left,Right,WordsM),!,atomSplit_oldy2(WordsM,Words,List),!.
  988atomSplit_oldy2([W|S],[W|Words],List):-atomSplit_oldy2(S,Words,List),!.
  989
  990
  991expireCaches:-expire1Cache,fail.
  992expireCaches:-garbage_collect_atoms,garbage_collect.
  993
  994
  995
  996%atomWSplit(A,B):-token_stream_of(A,AA),findall(B0,arg(1,AA,B),B).
  997
  998atom_concat_safe(L,R,A):- ((atom(A),(atom(L);atom(R))) ; ((atom(L),atom(R)))), !, atom_concat(L,R,A),!.
  999
 1000concat_atom_safe(List,Sep,[Atom]):-atom(Atom),!,atomic_list_concat_aiml(List,Sep,Atom),!.
 1001concat_atom_safe(List,Sep,Atom):-atom(Atom),!,atomic_list_concat_aiml(ListM,Sep,Atom),!,List = ListM.
 1002concat_atom_safe(List,Sep,Atom):- atomic_list_concat_aiml(List,Sep,Atom),!.
 1003
 1004:- if( \+ predicate_property(atom_contains(_,_),defined)).
 1005atom_contains(F,C):- hotrace((atom(F),atom(C),sub_atom(F,_,_,_,C))).
 1006:- endif.
 1007
 1008toCodes(B,A):-cyc:stringToCodelist(B,AO),(is_list(A) -> A=AO ; string_to_list(AO,A)),!.
 1009
 1010% convert any term to 'atom' string
 1011convert_to_string(I,ISO):- I \= [],
 1012                term_to_string(I,IS),!,
 1013		string_to_list(IS,LIST),!,
 1014		list_replace(LIST,92,[92,92],LISTM),
 1015		list_replace(LISTM,34,[92,34],LISTO),!,
 1016		string_to_atom_safe(ISO,LISTO),!.
 1017convert_to_string([],[]).
 1018
 1019string_to_atom_safe(ISO,LISTO):-LISTO==[],!,string_to_atom(ISO,'').
 1020string_to_atom_safe(ISO,LISTO):-string_to_atom(ISO,LISTO).
 1021
 1022list_replace(List,Char,Replace,NewList):-
 1023	append(Left,[Char|Right],List),
 1024	append(Left,Replace,NewLeft),
 1025	list_replace(Right,Char,Replace,NewRight),
 1026	append(NewLeft,NewRight,NewList),!.
 1027list_replace(List,_Char,_Replace,List):-!.
 1028
 1029
 1030%================================================================
 1031% maplist/[2,3]
 1032% this must succeed  maplist_safe(=,[X,X,X],[1,2,3]).
 1033% well if its not "maplist" what shall we call it?
 1034%================================================================
 1035% so far only the findall version works .. the other runs out of local stack!?
 1036
 1037:-tryHide(maplist_safe/2).
 1038maplist_safe(_Pred,[]):-!.
 1039maplist_safe(Pred,LIST):-findall(E,(member(E,LIST),prolog_must(apply(Pred,[E]))),LISTO), prolog_must(LIST=LISTO),!.
 1042:-tryHide(maplist_safe/3).
 1043maplist_safe(_Pred,[],[]):-!.
 1044maplist_safe(Pred,LISTIN, LIST):-!, findall(EE, ((member(E,LISTIN),prolog_must(apply(Pred,[E,EE])))), LISTO),  prolog_must(LIST=LISTO),!.
 1047%================================================================
 1048% decends tree
 1049%================================================================
 1050
 1051map_tree_to_list(_,PATTERN,Output):- (var(PATTERN)),!,must_assign([PATTERN],Output).
 1052map_tree_to_list(_,NPATTERN,Output):- (number(NPATTERN),atom_number(PATTERN,NPATTERN)),!,must_assign([PATTERN],Output).
 1053map_tree_to_list(_,[],OUT):-!,must_assign([],OUT).
 1054map_tree_to_list(Pred,IN,Output):- once(call(Pred,IN,MID)),prolog_must((MID=IN -> flatten([MID],OUT) ; map_tree_to_list(Pred,MID,OUT))),!,must_assign(OUT,Output).
 1055map_tree_to_list(Pred,[I|IN],Output):-!,prolog_must((map_tree_to_list(Pred,I,O1),map_tree_to_list(Pred,IN,O2),!,append(O1,O2,OUT))),!,must_assign(OUT,Output).
 1056map_tree_to_list(Pred,IN,Output):-atom(IN),prolog_must((atomWSplit(IN,MID),!,map_tree_to_list(Pred,MID,OUT))),!,must_assign(OUT,Output).
 1057map_tree_to_list(Pred,IN,Output):-
 1058  prolog_must((compound(IN), IN=..INP, append(Left,[Last],INP), map_tree_to_list(Pred,Last,UT),!, 
 1059   append(Left,[UT],OUTP),!, OUT =.. OUTP)),must_assign([OUT],Output).
 1060map_tree_to_list(_,IN,IN):-atrace,must_assign([IN],IN).
 1061
 1062
 1063dcg_maplist(_DCGPred,[    ],[    ]) --> [].
 1064dcg_maplist( DCGPred,[A|As],[B|Bs]) --> call(DCGPred,A,B),dcg_maplist(DCGPred,As,Bs).
 1065
 1066
 1067dumpList(B):-currentContext(dumpList,Ctx),dumpList(Ctx,B).
 1068dumpList(_,AB):-debugFmt(dumpList(AB)),!.
 1069
 1070dumpList(_,[]):-!.
 1071%dumpList(Ctx,[A|B]):-!,say(Ctx,A),dumpList(Ctx,B),!.
 1072%dumpList(Ctx,B):-say(Ctx,dumpList(B)).
 1073
 1074
 1075ifThen(When,Do):-When->Do;true.
 1076
 1077traceCall(A):-!,A.
 1078traceCall(A):-trace(A,[-all,+fail]),A,!.
 1079
 1080/*
 1081
 1082This stuff was not really good as it surrounded everying with a once/1 secretly
 1083
 1084:-tryHide(prolog_must/1).
 1085:-tryHide(prolog_must0/1).
 1086prolog_must(Call):-prolog_ecall(prolog_must0,Call).
 1087
 1088%%%%%%%%%%%%%5%%prolog_must(Call):- clause(Call,(_A,_B)),!,clause(Call,Body),atrace,prolog_must(Body),!.
 1089prolog_must0(Call):-  Call,!.
 1090%%%%%%%%%%%%%%prolog_must(Call):- prolog_mustTrace(Call),!.
 1091prolog_must0(Call):- beenCaught(Call),!.
 1092
 1093prolog_mustTrace(prolog_must(Call)):-!,prolog_mustTrace(Call),!.
 1094prolog_mustTrace((A,B)):- !,prolog_mustTrace(A),!,prolog_mustTrace(B),!.
 1095prolog_mustTrace(Call):- prolog_mustTrace1(Call),debugFmt(success(Call)),!.
 1096prolog_mustTrace(Call):- debugFmt(faild(Call)),!,atrace,Call.
 1097
 1098
 1099prolog_mustTrace1((A,B)):- !,prolog_mustTrace1(A),!,prolog_mustTrace1(B),!.
 1100prolog_mustTrace1(Call):- functor(Call,F,A),member(F/A,[retract/_,retractall/_]),!,debugFmt(fakingCall(Call)),numbervars(Call,0,_),!.
 1101prolog_mustTrace1(Call):- Call,!.
 1102
 1103beenCaught(prolog_must(Call)):- !, beenCaught(Call).
 1104beenCaught((A,B)):- !,beenCaught(A),beenCaught(B).
 1105beenCaught(Call):- fail, predicate_property(Call,number_of_clauses(_Count)), clause(Call,(_A,_B)),!,clause(Call,Body),beenCaught(Body).
 1106beenCaught(Call):- E=error(_,_), catch(once(Call),E,(debugFmt(caugth(Call,E)),beenCaught(Call))),!.
 1107beenCaught(Call):- traceAll,debugFmt(tracing(Call)),debug,atrace,Call.
 1108*/

 1109
 1110takeout(_,[],[]):-!.
 1111takeout(X,[Y|R],RR):-not(not(X=Y)),!,takeout(X,R,RR),!.
 1112takeout(X,[F|R],[F|S]) :- takeout(X,R,S),!.
 1113takeout(_,X,X).
 1114
 1115local_predicate(_,_/0):-!,fail.
 1116local_predicate(_,_/N):-N>7,!,fail.
 1117local_predicate(P,_):-predicate_property(P,built_in),!,fail.
 1118local_predicate(P,_):-predicate_property(P,imported_from(_)),!,fail.
 1119local_predicate(P,_):-predicate_property(P,file(F)),!,atom_contains(F,'aiml_'),!.
 1120local_predicate(P,F/N):-functor(P,F,N),!,fail.
 1121
 1122
 1123time_file_safe(F,INNER_XML):-exists_file_safe(F),time_file(F,INNER_XML).
 1124
 1125
 1126%%:- current_predicate(F/N),trace(F/N, -all),fail.
 1127/*
 1128traceAll:- current_predicate(user:F/N),
 1129   functor(P,F,N),
 1130   local_predicate(P,F/N),
 1131   trace(F/N, +fail),fail.
 1132traceAll:- not((predicate_property(clearCateStack/1,_))),!.
 1133traceAll:-findall(_,(member(F,[member/2,debugFmt/1,takeout/3,findall/3,clearCateStack/1]),trace(F, -all)),_).
 1134*/

 1135traceAll:-doTryHides.
 1136
 1137
 1138
 1139% ===================================================================
 1140% When you trust the code enough you dont to debug it
 1141%  but if that code does something wrong while your not debugging, you want to see the error
 1142% ===================================================================
 1143
 1144:-tryHide(hotrace/1).
 1145hotrace(X):- tracing,!, notrace(X).
 1146hotrace(X):- call(X).
 1147
 1148:-tryHide(lotrace/1).
 1149lotrace(X):-tracing,!,catchAnRethrow(notrace(X)).
 1150lotrace(X):-catchAnRethrow(X).
 1151
 1152catchAnRethrow(X):-catch(X,E,(debugFmt(X->E),throw(E))).
 1153
 1154% ===================================================================
 1155% tracing durring notrace
 1156% ===================================================================
 1157%% "trace,tracing" .. detects if we are in a notrace/1
 1158%% prolog_exception_hook
 1159interactStep(String):-interactStep(String,true,true).
 1160interactStep(String,CallYes,CallNo):-not(devmode),debugFmt(promptUser(String,[call,-,CallYes,-,or,-,CallNo])),!,CallYes.
 1161interactStep(String,CallYes,CallNo):-debugFmt(promptUser(String,[call,-,CallYes,-,or,-,CallNo])),trace,tracing,CallYes.
 1162interactStep(_String,CallYes,CallNo):-printStackTrace,prompt1('>>>>>>>>>>>>>>'),read(YN),debugFmt(red(YN)),YN=yes->CallYes;CallNo.
 1163
 1164% ===================================================================
 1165% traceIf/warnIf(_Call):-!.
 1166% ===================================================================
 1167:-tryHide(traceIf/1).
 1168
 1169traceIf(Call):-copy_term(Call,Call0),Call0->debugFmt(traceIf0(Call0));true.
 1170
 1171:-tryHide(warnIf/1).
 1172warnIf(Call):-hotrace(ignore((Call,debugFmt(warnIf(Call))))).
 1173
 1174% ===================================================================
 1175% Usage: pred_subst(+Pred, +Fml,+X,+Sk,?FmlSk)
 1176% ===================================================================
 1177
 1178pred_subst(Pred,A,[B],C,D):- nonvar(B),!,pred_subst(Pred,A,B,C,D).
 1179pred_subst(Pred,A,B,C,D):- error_catch(lotrace(nd_subst(Pred,A,B,C,D)),E,(debugFmt(E),fail)),!.
 1180pred_subst(_Pred,A,_B,_C,A).
 1181
 1182nd_subst(Pred,  Var, VarS,SUB,SUB ) :- call(Pred,Var,VarS),!.
 1183nd_subst(Pred,  P, X, Sk, P1 ) :- functor(P,_,N),nd_subst1(Pred, X, Sk, P, N, P1 ).
 1184
 1185nd_subst1(_Pred, _,  _, P, 0, P  ).
 1186nd_subst1(Pred, X, Sk, P, N, P1 ) :- N > 0, P =.. [F|Args], 
 1187            nd_subst2(Pred, X, Sk, Args, ArgS ),
 1188            nd_subst2(Pred, X, Sk, [F], [FS] ),  
 1189            P1 =.. [FS|ArgS].
 1190
 1191nd_subst2(_Pred, _,  _, [], [] ).
 1192nd_subst2(Pred, X, Sk, [A|As], [A|AS]  ) :- nonvar(A), A=verbatum(_), !, nd_subst2(Pred, X, Sk, As, AS).
 1193nd_subst2(Pred, X, Sk, [A|As], [Sk|AS] ) :- call(Pred, X, A), !, nd_subst2(Pred, X, Sk, As, AS).
 1194nd_subst2(Pred, X, Sk, [A|As], [A|AS]  ) :- var(A), !, nd_subst2(Pred, X, Sk, As, AS).
 1195nd_subst2(Pred, X, Sk, [A|As], [Ap|AS] ) :- nd_subst(Pred, A, X, Sk, Ap ),nd_subst2(Pred, X, Sk, As, AS).
 1196nd_subst2(_Pred, _X, _Sk, L, L ).
 1197
 1198
 1199%%% peekAttributes/2,pushAttributes/2,pushCateElement/2.
 1200
 1201
 1202lengthAtLeast(N,GE):-atom(N),atom_length(N,L),L>=GE.
 1203/*
 1204neverUse:- meta_predicate_transparent
 1205	maplist_safe(2,:),
 1206	maplist_safe(3,:,:),
 1207        asserta_new(2,:),
 1208        writeqnl(2,:),
 1209        prolog_mustTrace(1),
 1210        prolog_must(1),
 1211        beenCaught(1),
 1212        prolog_mustEach(1),
 1213        prolog_must(1),ignore(1), %%withAttributes(3,:,:),call_cleanup(0,0),call_cleanup(0,?,0),
 1214        !.
 1215*/

 1216
 1217
 1218%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5
 1219%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5
 1220%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5
 1221
 1222
 1223:- use_module(library(memfile)).
 1226string_to_stream(String,InStream):- string(String),string_to_atom(String,Atom),!,string_to_stream(Atom,InStream).
 1227string_to_stream(Atom,InStream):- atom_to_memory_file(Atom, Handle),open_memory_file(Handle,read,InStream).
 1228
 1229            
 1230stt:- string_to_stream('hi.\nthere.\n',X),read(X,Y),read(X,Z),close(X),writeln([hi=Y,there=Z]).
 1231
 1232tryHideProc(_MFA):-!.
 1233tryHideProc(MFA):-tryCatchIgnore('$hide'(MFA)),tryCatchIgnore('trace'(MFA,[-all])),tryCatchIgnore(noprofile(MFA)).
 1234
 1235doTryHides:-retract(remember_tryHide(MFA)),once(tryHideProc(MFA)),fail.
 1236doTryHides