1/*  Logicmoo Debug Tools
    2% ===================================================================
    3% File 'util_varnames.pl'
    4% Purpose: An Implementation in SWI-Prolog of certain debugging tools
    5% Maintainer: Douglas Miles
    6% Contact: $Author: dmiles $@users.sourceforge.net ;
    7% Version: 'util_varnames.pl' 1.0.0
    8% Revision: $Revision: 1.1 $
    9% Revised At:  $Date: 2002/07/11 21:57:28 $
   10% ===================================================================
   11*/
   12% File: /opt/PrologMUD/pack/logicmoo_base/prolog/logicmoo/util/util_varnames.pl
   13:- module(util_varnames,
   14          [ ain00/1,
   15            contains_ftVar/1,
   16            count_members_eq/3,
   17            all_different_vars/1,
   18            all_different_vals/1,
   19            all_different_vals/2,
   20            all_disjoint_in_sets/3,
   21            del_each_attr/2,
   22            get_random_headvars/1,
   23            get_random_headvars/4,
   24            get_1head_arg_var/5,
   25            term_slots/2,
   26            never_bound/1,
   27            dif_matrix/2,
   28            term_singleslots/2,
   29          term_singleslots/3,
   30          term_singleslots/5,
   31            set_varname/2,
   32            set_varname/3,
   33            write_functor/2,
   34            atom_subst_frak_0/4,
   35            arg_varname/3,
   36            variable_name_or_ref/2,
   37            renumbervars/3,
   38            b_implode_varnames/1,
   39            b_implode_varnames0/1,
   40            bad_varnamez/1,
   41            call_not_not/1,
   42            call_return_tf/2,
   43            check_varnames/1,
   44            clause_ref_file/2,
   45            clause_ref_vars/2,
   46            contains_badvarnames/1,
   47            contains_dvar/1,
   48            contains_singletons/1,
   49            del_attr_type/2,
   50            copy_term_and_varnames/2,
   51            mpred_numbervars_with_names/1,
   52            ensure_vars_labled/2,
   53            ensure_vars_labled_r/2,
   54            fix_varcase_name/2,
   55            get_clause_vars/1,
   56            get_clause_vars/2,
   57            get_clause_vars_copy/2,
   58            get_clause_vars_hb_int/2,
   59            imploded_copyvars/2,
   60            locate_clause_ref/5,
   61            not_member_eq_local/2,
   62            listing_vars_file/0,
   63            make_subterm_path/3,
   64            maybe_record_scanned_file/0,
   65
   66            without_varname_scan/1,
   67
   68            no_varnaming/1,
   69            no_vars_needed/1,
   70            
   71   print_numbervars_maybe/1,
   72   print_numbervars/1,
   73   scan_for_varnames0/0,
   74   que_read_source_file_vars/1,
   75            read_source_file_vars/1,
   76            read_source_file_vars_1/1,
   77            read_source_vars/2,
   78            replace_in_string_frak_0/4,
   79            save_clause_vars/2,
   80            save_clause_vars/3,
   81            save_clause_vars/6,
   82            save_file_source_vars/3,
   83            save_to_clause_ref/3,
   84            scan_for_varnames/0,
   85            dcall_if_verbose/1,
   86            dcall_when/3,
   87            snumbervars4/4,
   88            snumbervars5/4,
   89            subterm_path/3,
   90            term_expansion_save_vars/1,
   91            try_get_body_vars/1,
   92            try_get_head_vars/1,
   93            try_get_inner_vars/1,
   94            set_varname/2,
   95            try_save_vars/1,
   96            
   97            v_dif_rest/2,
   98            vmust/1,
   99            name_variable/2, variable_name/2,
  100            init_varname_stores/1,
  101            maybe_scan_for_varnames/0
  102
  103          ]).

Name Prolog variables (debugging)

*/

  109:- use_module(library(must_sanity)).  110:- use_module(library(logicmoo/misc_terms)).  111
  112:- multifile
  113        prolog:make_hook/2.  114:- meta_predicate
  115       ain00(:),
  116        call_not_not(0),
  117        call_return_tf(0, ?),
  118        contains_singletons(*),
  119        get_clause_vars(:),
  120        get_clause_vars(:, ?),
  121        no_varnaming(0),
  122        without_varname_scan(0),
  123        dcall_when(2, ?, ?),
  124        snumbervars4(*, ?, ?, ?),
  125        snumbervars5(*, ?, ?, ?),
  126        try_save_vars(:),
  127        all_different_vals(2,*),
  128        all_disjoint_in_sets(2,*,*),
  129        init_varname_stores(?),
  130        vmust(0).  131
  132:- module_transparent((
  133            all_different_vals/1,
  134            all_different_vals/2,
  135            set_varname/2,
  136            set_varname/3,
  137            atom_subst_frak_0/4,
  138            b_implode_varnames/1,
  139            b_implode_varnames0/1,
  140            bad_varnamez/1,
  141            call_not_not/1,
  142            call_return_tf/2,
  143            check_varnames/1,
  144            clause_ref_file/2,
  145            clause_ref_vars/2,
  146            contains_badvarnames/1,
  147            contains_singletons/1,
  148            del_attr_type/2,
  149            ensure_vars_labled/2,
  150            ensure_vars_labled_r/2,
  151            fix_varcase_name/2,
  152            get_clause_vars/1,
  153            get_clause_vars/2,
  154            get_clause_vars_copy/2,
  155            get_clause_vars_hb_int/2,
  156            imploded_copyvars/2,
  157            locate_clause_ref/5,  
  158            
  159            listing_vars_file/0,
  160            make_subterm_path/3,
  161            maybe_record_scanned_file/0,
  162            no_varnaming/1,
  163            without_varname_scan/1,
  164            no_vars_needed/1,
  165            
  166   print_numbervars_maybe/1,
  167   print_numbervars/1,
  168            read_source_file_vars/1,
  169            read_source_file_vars_1/1,
  170            read_source_vars/2,
  171            replace_in_string_frak_0/4,
  172            save_clause_vars/2,
  173            save_clause_vars/3,
  174            save_clause_vars/6,
  175            save_file_source_vars/3,
  176            save_to_clause_ref/3,
  177            scan_for_varnames/0,
  178            dcall_if_verbose/1,
  179            dcall_when/3,
  180            snumbervars4/4,
  181            snumbervars5/4,
  182            subterm_path/3,
  183            term_expansion_save_vars/1,
  184            try_get_body_vars/1,
  185            try_get_head_vars/1,
  186            try_get_inner_vars/1,
  187            set_varname/2,
  188            try_save_vars/1,
  189            
  190            v_dif_rest/2,
  191            % name_variable/2, variable_name/2,
  192            vmust/1,
  193            init_varname_stores/1
  194 )).  195:- meta_predicate dcall_if_verbose(0).  196
  197:- reexport((lockable_vars)).  198:- system:use_module(library(logicmoo/each_call)).  199% :- use_module(library(dictoo_lib)).
 name_variable(+Var, +Name) is det
Assign a name to a variable. Succeeds silently if Var is not a variable (anymore).
  208%name_variable(Var,_Name) :- nonvar(Var),!.
  209%name_variable(Var,Name) :- !, put_attr(Var,vn,Name).
  210
  211name_variable(Var, Name1) :- get_attr(Var,vn,Name2),
  212        combine_names(Name1,Name2,Name),
  213	put_attr(Var, vn, Name). % add_var_to_env(Name,Var),!.
  214name_variable(Var, Name) :- var(Var), !,
  215	put_attr(Var, vn, Name).
  216
  217name_variable('$VAR'(Var), Name):- Name==Var, !.
  218name_variable('$VAR'(Var), Name):- var(Var),Name=Var,!.
  219% name_variable('$VAR'(Var), Name) :- trace_or_throw(numbervars_name_variable(Var, Name)),!.
  220name_variable(_, _).
  221
  222:- nodebug(logicmoo(varnames)).
 variable_name(+Var, -Name) is semidet
True if Var has been assigned Name.
  230variable_name(Var, Name) :- must(var(Var)),(get_attr(Var, vn, Name);var_property(Var,name(Name));get_attr(Var, varnames, Name)),!.
  231
  232variable_name_or_ref(Var, Name) :- get_var_name(Var, Name),!.
  233variable_name_or_ref(Var, Name) :- format(atom(Name),'~q',[Var]).
 project_attributes(?QueryVars, ?ResidualVars) is semidet
Project Attributes.
  240vn:project_attributes(QueryVars, ResidualVars):- fail,dmsg(vn:proj_attrs(vn,QueryVars, ResidualVars)),fail.
 attribute_goals(@V)// is det
copy_term/3, which also determines the toplevel printing of residual constraints. Hook To [attribute_goals/3] For Module Logicmoo_varnames. Attribute Goals.
  249vn:attribute_goals(Var) --> {get_var_name(Var,  Name)},!,[name_variable(Var,  Name)],!.
  250
  251get_var_name(V,N):-quietly(get_var_name0(V,N)),!.
  252
  253get_var_name0(Var,Name):- nonvar(Name),!,must(get_var_name0(Var, NameO)),!,Name=NameO.
  254get_var_name0(Var,Name):- nonvar(Var),!,get_var_name1(Var,Name),!.
  255get_var_name0(Var,Name):- get_attr(Var, vn, Name),!.
  256get_var_name0(Var,Name):- var_property(Var,name(Name)),!.
  257get_var_name0(Var,Name):- nb_current('$variable_names', Vs),member(Name=V,Vs),atomic(Name),V==Var,!.
  258get_var_name0(Var,Name):- get_attr(Var, varnames, Name),!.
  259get_var_name0(Var,Name):- nb_current('$old_variable_names', Vs),member(Name=V,Vs),atomic(Name),V==Var,!.
  260get_var_name0(Var,Name):- get_varname_list(Vs),member(Name=V,Vs),atomic(Name),V==Var,!.
  261% get_var_name0(Var,Name):- attvar(Var),get_varname_list(Vs),format(atom(Name),'~W',[Var, [variable_names(Vs)]]).
  262
  263get_var_name1('$VAR'(Name),Name):- atom(Name),!.
  264get_var_name1('$VAR'(Var),Name):- (var(Var)->get_var_name0(Var,Name);Name=Var),!.
  265get_var_name1('$VAR'(Att3),Name):- !, get_var_name1(Att3,Name).
  266get_var_name1('avar'(Att3),Name):- !, get_var_name1(Att3,Name).
  267get_var_name1('avar'(Name,Att3),Value):- !, get_var_name1('$VAR'(Name),Value); get_var_name1('avar'(Att3),Value).
  268get_var_name1(att(vn,Name,_),Name):- !.
  269get_var_name1(att(_,_,Rest),Name):- Rest\==[],get_var_name1(Rest,Name).
  270get_var_name1(Var,Name):- nb_current('$variable_names', Vs),member(Name=V,Vs),atomic(Name),V==Var,!.
  271get_var_name1(Var,Name):- oo_get_attr(Var, vn, Name),!. % ground(Name),!.
  272
  273
  274
  275%:- public ((attr_unify_hook/2,attr_portray_hook/2)).
  276% :- public portray_attvar/1.
  277% :- export(portray_attvar/1).
  278%:- export(attr_unify_hook/2).
  279%:- export(attr_portray_hook/2).
  280%:- export(attribute_goals/3).
  281
  282:- thread_local(t_l:varname_lock/1).  283when_var_locked(What):- t_l:varname_lock(What),!.
  284
  285:- thread_local(t_l:no_kif_var_coroutines/1).
 attr_unify_hook(?X, ?Other) is semidet
Hook To [attr_unify_hook/2] For Module Logicmoo_varnames. Attr Unify Hook.
  292vn:attr_unify_hook(X,Y):- quietly(must(vn:attr_unify_hook0(X,Y))).
  293vn:attr_unify_hook0(_, Var):- cyclic_term(Var),!,fail.
  294vn:attr_unify_hook0(Var,_):- cyclic_term(Var),!,fail.
  295vn:attr_unify_hook0(Name1, Var):- when_var_locked(What),!,(unify_name_based(Name1, Var)->true;call(What,Var)).
  296vn:attr_unify_hook0(Name1, Var):- var(Var),unify_name_based(Name1, Var),!.
  297vn:attr_unify_hook0(_Form, _OtherValue):-!.
  298
  299
  300
  301unify_name_based(Var1, Var2):- \+ atom(Var1),variable_name_or_ref(Var1,Name),!,unify_name_based(Name, Var2).
  302unify_name_based(_Form, _OtherValue):- local_override(no_kif_var_coroutines,G),!,call(G).
  303unify_name_based(Name1, Var):-  get_var_name(Var,Name2),!,Name1=Name2,!.
  304unify_name_based(Name1, Var):- get_attr(Var, vn, Name2),!,combine_names(Name1,Name2,Name),(Name2==Name->true;put_attr(Var,vn,Name)).
  305unify_name_based(Name1, Var):- var(Var),!,put_attr(Var, vn, Name1).
  306unify_name_based(_, Var):- nonvar(Var),!.
  307%unify_name_based(_, Var):- cyclic_term(Var),!,fail.
  308%unify_name_based(_, Var):- cyclic_term(Var),!.
  309%unify_name_based(_, Var):- cyclic_break(Var),!,fail.
  310
  311combine_names(Name1,Name2,Name1):-Name1==Name2,!.
  312combine_names(Name1,Name2,Name):-
  313 ((atom_concat(_,Name1,Name2);atom_concat(Name1,_,Name2)) -> Name=Name2 ; (
  314   ((atom_concat(Name2,_,Name1);atom_concat(_,Name2,Name1)) -> Name=Name1 ; (
  315   (combine_names_dash(Name1,Name2,Name)))))).
  316
  317combine_names_dash(Name1,Name2,Name):- number_name(Name2),!,Name=Name1.
  318combine_names_dash(Name1,Name2,Name):- number_name(Name1),!,Name=Name2.
  319combine_names_dash(Name1,Name2,Name):- word_name(Name2),!,atomic_list_concat([Name2,'_',Name1],Name),!.
  320combine_names_dash(Name1,Name2,Name):- atomic_list_concat([Name1,'_',Name2],Name),!.
  321
  322number_name(Name):- name(Name,[95,Number|_]),char_type(Number,digit).
  323word_name(Name):- name(Name,[A,_|_]),char_type(A,upper).
  324
  325
  326/*
  327
  328%=
  329
  330%% attr_portray_hook( ?Value, ?Var) is semidet.
  331%
  332% Attr Portray Hook.
  333%
  334 % vn:attr_portray_hook(Name, _) :- write('???'), write(Name),!.
  335
  336:- multifile(user:portray/1).
  337:- dynamic(user:portray/1).
  338user:portray(Sk):- get_attr(Sk, vn, Name), get_attrs(Sk,att(vn,Name,[])),write(Name),!,write('{}').
  339
  340
  341%% portray_attvar( ?Var) is semidet.
  342%
  343% Hook To [portray_attvar/1] For Module Logicmoo_varnames.
  344% Portray Attribute Variable.
  345%
  346portray_attvar(Var) :-
  347	write('{<'),
  348        ((get_attr(Var,vn, VarName))->true;sformat(VarName,'~q',[Var])),
  349	get_attrs(Var, Attr),
  350	catch(writeq('??'(VarName,Attr)),_,'$attvar':portray_attrs(Attr, Var)),
  351	write('>}').
  352*/
  353
  354
  355
  356:- set_module(class(library)).  357
  358:- use_module(library(when)).  359
  360:- prolog_clause:multifile((
  361	unify_goal/5,			% +Read, +Decomp, +M, +Pos, -Pos
  362	unify_clause_hook/5,
  363	make_varnames_hook/5,
  364	open_source/2)).			% +Input, -Stream
  365
  366:- predicate_options(prolog_clause:clause_info/5, 5,
  367		     [ variable_names(-list)
  368		     ]).  369
  370:- use_module(library(prolog_source)).  371:- use_module(library(prolog_clause)). % read_term_at_line/6
  372
  373
  374:- meta_predicate vmust(0).  375
  376%=
 vmust(:GoalG) is semidet
Vmust.
  382vmust(G):-!,must(G).
  383vmust(G):-!,call(G).
  384
  385
  386%=
 dcall_when(:PRED2P, ?In, ?Out) is semidet
Dirrectly Call When.
  392dcall_when(P,In,Out):- must(call(P,In,Out)),ignore((In\=@=Out,dmsg((dcall_when(P) :- (In,Out))))).
  393
  394:- create_prolog_flag(source_variables, false, [type(boolean)]).  395:- thread_local(t_l:dont_varname/0).  396:- thread_local(t_l:dont_varname_te/0).  397:- thread_local(t_l:try_varname_clause_next/1).  398
  399
  400%=
 no_varnaming(:GoalG) is semidet
No Varnaming.
  407no_varnaming(G):-locally(t_l:dont_varname,G).
  408
  409
  410%=
 all_different_vals(+P2, ?Term) is semidet
All Different Vals.
  416all_different_vals(Term):-all_different_vals(dif_matrix,Term).
 all_different_vars(?A) is semidet
All Different Variables.
  423all_different_vars(_):- t_l:dont_varname,!.
  424all_different_vars(A):-(quietly((all_disjoint_in_sets(dif_matrix,A,A)))),!.
  425all_different_vars(A):-must(quietly((all_disjoint_in_sets(dif_matrix,A,A)))),!.
  426all_different_vars(A):-all_different_vals(v_dif_rest,A),!.
 all_different_vals(:PRED2, +Term) is semidet
All Different Vals.
  434all_different_vals(Pred,Term):-
  435 must(quietly(( (is_list(Term)-> Slots = Term ; term_slots(Term,Slots)),!,
  436                                 all_disjoint_in_sets(Pred,Slots,Slots)))).
 all_different_vals(:PRED2, +SET1, +SET2) is semidet
All Different Vals.
  442all_disjoint_in_sets(_,[],_):-!.
  443all_disjoint_in_sets(_,[_],_):-!.
  444all_disjoint_in_sets(P,[V|Vs],SET):-delete_eq(SET,V,REST),!,call(P,V,REST),all_disjoint_in_sets(P,Vs,SET).
  445
  446/*
  447delete_eq([],Item,[]):-!,dmsg(warn(delete_eq([],Item,[]))).
  448delete_eq([L|List],Item,List):-Item==L,!.
  449delete_eq([L|List],Item,[L|ListO]):-delete_eq(List,Item,ListO),!.
  450*/
 v_dif_rest(?V, ?REST) is semidet
V not member_eq Rest.
  456v_dif_rest(V,REST):- when('?='(V,_),not_member_eq_local(V,REST)).
 not_member_eq_local(?E, ?REST) is semidet
Not Member Using (==/2) (or =@=/2) ).
  462not_member_eq_local(_,[]):-!.
  463not_member_eq_local(E,REST):- \+ identical_memberchk(E,REST).
 dif_matrix(?V, ?REST) is semidet
V dif Rest of
  469dif_matrix(List,V):- is_list(List),!,list_to_set(List,SET), maplist(dif_matrix(V),SET).
  470dif_matrix(V,List):- is_list(List),!,list_to_set(List,SET), maplist(dif_matrix_hopfully(V),SET).
  471dif_matrix(A,B):- dif_matrix_hopfully(A,B).
  472dif_matrix_hopfully(A,B):-A==B,!.
  473dif_matrix_hopfully(A,B):- dif:dif(A,B),!.
  474
  475
  476%=
 make_subterm_path(?Sub, ?Term, ?PathO) is semidet
Make Subterm Path.
  482make_subterm_path(Sub,Term,PathO):-vmust(subterm_path(Sub,Term,Path)),!,PathO=Path.
  483
  484
  485%=
 subterm_path(?Sub, ?Term, :TermARG3) is semidet
Subterm Path.
  491subterm_path(Sub,Term,[]):-Sub==Term,!.
  492subterm_path(Sub,Term,[arg(N)|Path]):-compound(Term),!,arg(N,Term,TermE),subterm_path(Sub,TermE,Path),!.
  493
  494
  495%=
 get_clause_vars(?CV) is semidet
Get Clause Variables.
  501get_clause_vars(CV):- quietly(get_clause_vars_nontraced(CV)).
  502:- export(get_clause_vars_nontraced/1).  503
  504%=
 get_clause_vars_nontraced(:TermMHB) is semidet
Get Clause Variables Nontraced.
  510get_clause_vars_nontraced(_):- t_l:dont_varname,!.
  511get_clause_vars_nontraced(V):- var(V),!.
  512get_clause_vars_nontraced('$VAR'(_)):- !.
  513get_clause_vars_nontraced(_:V):- var(V),!,ignore((get_varname_list(Vs),member(N=V0,Vs),V0==V,set_varname(write_functor,N,V))).
  514get_clause_vars_nontraced(MHB):- term_variables(MHB,Vs),must(get_clause_vars(MHB,Vs)).
  515
  516:- '$set_predicate_attribute'(get_clause_vars(_), trace, 1).  517:- '$set_predicate_attribute'(get_clause_vars(_), hide_childs, 1).  518
  519
  520%=
 del_attr_type(?Type, ?Var) is semidet
Remove/erase Attr Type.
  526del_attr_type(Type,Var):-ignore(del_attr(Var,Type)).
  527
  528
  529%=
 get_clause_vars(?MHB, ?Vs) is semidet
Get Clause Variables.
  535get_clause_vars(_,[]):-!.
  536get_clause_vars(MHB,[V|Vs]):- all_different_vars([V|Vs]),vmust((get_clause_vars_copy(MHB,WVARS),!,
  537   vmust(MHB=WVARS),unlock_vars(MHB),nop(sanity(check_varnames(MHB))))),!,
  538   maplist(del_attr_type(vl),[V|Vs]).
  539get_clause_vars(MHB,Vs):- vmust((get_clause_vars_copy(MHB,WVARS),!,vmust(MHB=WVARS),unlock_vars(MHB),must(check_varnames(Vs)))),!.
  540get_clause_vars(_,_):- !.
  541
  542
  543
  544%=
 get_clause_vars_copy(?HB, ?HB) is semidet
Get Clause Variables Copy.
  550get_clause_vars_copy(HB,HB):- ground(HB),!.
  551get_clause_vars_copy(HH,HH):- sub_term(S,HH),compound(S),S='$VAR'(_),!. % already labled
  552get_clause_vars_copy(H0,MHB):- copy_term(H0,MHB),!.
  553get_clause_vars_copy(H0,MHB):-
  554    source_variables_lv(AllS),
  555    must((copy_term(H0+AllS,MHB+CAllS),
  556    term_slots(MHB,Slots),
  557   % all_different_vars(Slots),
  558    lock_vars(Slots),
  559    as_clause_no_m( MHB,  H, B),
  560    must_maplist(set_varname(write_functor),CAllS),
  561    get_clause_vars_hb_int(H,B))),!.
  562
  563get_clause_vars_copy(H0,MHB):-
  564    must((copy_term_and_varnames(H0,MHB),lock_vars(MHB),as_clause_no_m( MHB,  H, B),
  565    get_clause_vars_hb_int(H,B))),!.
  566
  567
  568
  569
  570%=
 get_clause_vars_hb_int(?H, ?B) is semidet
Get Clause Variables Head+body Int.
  576get_clause_vars_hb_int(H,B):- varname_cache:varname_info(H,B,Vs,_),must_maplist(set_varname(write_functor),Vs),!.
  577get_clause_vars_hb_int(H,B):- call_return_tf(try_get_body_vars(B),_TF1),call_return_tf(try_get_head_vars(H),_TF2),!.
  578
  579
  580
  581%=
 atom_subst_frak_0(?A, ?F, ?R, ?K) is semidet
atom subst frak Primary Helper.
  587atom_subst_frak_0(A,F,R,K):-replace_in_string_frak_0(F,R,A,K),!.
  588
  589%=
 replace_in_string_frak_0(?F, ?R, ?A, ?K) is semidet
replace in string frak Primary Helper.
  595replace_in_string_frak_0(F,R,A,K):-atom(A),!,atom_string(A,S),replace_in_string_frak_0(F,R,S,C),atom_string(K,C).
  596replace_in_string_frak_0(SepChars,Repl,A,C):- atomics_to_string(B,SepChars,A),atomics_to_string(B,Repl,C).
  597
  598
  599
  600%=
 fix_varcase_name(?N, ?VN) is semidet
Fix Varcase Name.
  606fix_varcase_name(N,VN):-atom_subst_frak_0(N,'-','_',O),atom_subst_frak_0(O,'?','_',VN).
  607
  608
  609%=
 no_vars_needed(?H) is semidet
No Variables Needed.
  615no_vars_needed(H):- (t_l:dont_varname; ( ground(H) ; \+ compound(H))) ,!.
  616no_vars_needed(A:H):-atom(A),!,no_vars_needed(H).
  617no_vars_needed(H):-var(H),!.
  618no_vars_needed('$VAR'(_)):-!.
  619no_vars_needed(H):- compound(H),H=varname_info(_,_,_,_),!.
  620
  621%=
 try_get_inner_vars(?H) is semidet
Try Get Inner Variables.
  627try_get_inner_vars(H):- once((functor(H,_,N),arg(N,H,List),member(vars(Vs),List))),is_list(Vs),term_variables(H,VL),must_maplist(set_varname(write_functor),Vs,VL).
  628
  629
  630
  631%=
 term_slots(?Term, ?Slots) is semidet
Hook To [term_slots/2] For Module Logicmoo_varnames. Term Slots.
  638term_slots(Term,Slots):-term_singleslots(Term, [],NS, [],S),append(NS,S,Slots).
  639
  640
  641%=
 term_singleslots(?A, ?Vs) is semidet
Hook To [term_singleslots/2] For Module Logicmoo_varnames. Term Singletons.
  648term_singleslots(A,Vs):- quietly(term_singleslots(A,[],_,[],Vs)).
  649%= %= :- was_export(term_singleslots/3).
  650
  651%=
 term_singleslots(?Term, ?NonSingle, ?Singles) is semidet
Term Singletons.
  657term_singleslots(Term,NonSingle,Singles):- quietly(term_singleslots(Term,[],NonSingle,[],Singles)).
  658%= %= :- was_export(term_singleslots/5).
  659
  660%=
 term_singleslots(:TermFml, +NonSinglesIn, -NonSinglesOut, +SinglesIn, -SinglesOut) is semidet
Term Singletons.
  666term_singleslots(Fml, NS,NS, S,S):- atomic(Fml),!.
  667term_singleslots(Fml, NS,NS, S,S):- identical_memberchk(Fml,NS),!.
  668term_singleslots(Fml, NS, [Fml|NS], S, NSV):- is_ftVar(Fml),identical_memberchk(Fml,S),!,delete_eq(S,Fml,NSV),!.
  669term_singleslots(Fml, NS, NS, S, [Fml|S]):- is_ftVar(Fml),!.
  670term_singleslots([H|T],NS,NSO,S,NSV):- !, term_singleslots(H,NS,NSM,S,M),term_singleslots(T,NSM,NSO,M,NSV).
  671term_singleslots(Fml, NS,NSO, S,NSV):- compound(Fml),Fml=..[_|T],!, term_singleslots(T, NS,NSO, S,NSV).
  672
  673
  674%=
 call_return_tf(:Goal, ?TF) is semidet
Call Return True/false.
  680call_return_tf(Call,TF):- ((Call-> TF = t ; TF = nil)).
  681
  682
  683
  684%=
 try_get_varname_cache(?H) is semidet
Try Get Varname Cache.
  690try_get_varname_cache(H):- no_vars_needed(H),!.
  691try_get_varname_cache(H):- varname_cache:varname_info(H,_,Vs,_),maplist(set_varname(write_functor),Vs),!.
  692try_get_varname_cache(H):- varname_cache:varname_info(_,H,Vs,_),maplist(set_varname(write_functor),Vs),!.
  693
  694%=
 try_get_head_vars(?H) is semidet
Try Get Head Variables.
  700try_get_head_vars(H):- no_vars_needed(H),!.
  701try_get_head_vars(H):- varname_cache:varname_info(H,_,Vs,_),maplist(set_varname(write_functor),Vs),!.
  702try_get_head_vars(H):- try_get_inner_vars(H).
  703try_get_head_vars(H):- varname_cache:varname_info(_,H,Vs,_),maplist(set_varname(write_functor),Vs),!.
  704try_get_head_vars(B):- must(get_random_headvars(B)),!.
  705
  706
  707
  708%=
 try_varname_infos(?H) is semidet
Try Varname Infos.
  714try_varname_infos(H):- varname_cache:varname_info(H,_,Vs,_),maplist(set_varname(write_functor),Vs),!.
  715
  716
  717%=
 get_random_headvars(?H) is semidet
Get Random Headvars.
  723get_random_headvars(H):- \+ compound(H),!.
  724get_random_headvars(H):- functor(H,F,A),get_random_headvars(H,F,A,A).
  725
  726
  727%=
 get_random_headvars(?H, ?F, ?A, ?N) is semidet
Get Random Headvars.
  733get_random_headvars(_H,_F,A,N):- (N < 1 ; N>A),!.
  734get_random_headvars( H, F,A,N):- arg(N,H,HA),ignore(get_1head_arg_var(H,F,N,A,HA)),N2 is N-1,get_random_headvars(H,F,A,N2).
  735
  736
  737%=
 get_1head_arg_var(?H, ?F, ?N, ?A, ?HA) is semidet
Get 1head Argument Variable.
  743get_1head_arg_var(_H,F,N,A,HA):-
  744 functor(HH,F,A),
  745 (nonvar(HA)->!;
  746 (arg(N,HH,COMP),
  747  (( arg_varname(HH,N,Name),HA='$VAR'(Name));(try_get_varname_cache(HH),sub_ft_var(HA,COMP))))).
  748
  749
  750%=
 sub_ft_var(?HA, ?COMP) is semidet
Sub Format Type Variable.
  756sub_ft_var(HA,COMP):-
  757  (is_ftVar(COMP)-> HA=COMP; (compound(COMP),arg(_,COMP,FTVAR),sub_ft_var(HA,FTVAR))).
 arg_varname(+P, ?N, ?Name) is semidet
Argument variable name.
  764arg_varname(P,N,Name):- arg(N,P,Now),varname_cache:varname_info(P,_,List,_),var(Now),member(Nam=V,List),Now==V,must(Nam=Name).
  765
  766
  767%=
 try_get_body_vars(:TermH) is semidet
Try Get Body Variables.
  773try_get_body_vars(H):- no_vars_needed(H),!.
  774try_get_body_vars(H):- varname_cache:varname_info(_,H,Vs,_),maplist(set_varname(write_functor),Vs),!.
  775try_get_body_vars(H):- try_get_inner_vars(H),!.
  776try_get_body_vars(H):- varname_cache:varname_info(H,_,Vs,_),maplist(set_varname(write_functor),Vs),!.
  777try_get_body_vars((A,B)):-!,try_get_head_vars(A),try_get_head_vars(B).
  778try_get_body_vars((A;B)):-!,try_get_head_vars(A),try_get_head_vars(B).
  779try_get_body_vars(C):- C=..[_,L],maplist(try_get_body_vars,L).
  780try_get_body_vars(B):- must(get_random_headvars(B)),!.
  781try_get_body_vars(_).
  782
  783:- multifile(varname_cache:varname_info/4).  784:- dynamic(varname_cache:varname_info/4).  785
  786:- meta_predicate renumbervars(*,*,*).  787
  788:- meta_predicate set_varname(+,*,*).  789:- meta_predicate set_varname(+,*).  790% set_varname(How,B):-var(B),!.
  791
  792%=
 set_varname(+How, ?B) is semidet
Set Varname.
  798:- meta_predicate set_varname(:,*,*).  799:- meta_predicate set_varname(:,*).  800
  801set_varname(How,B):-var(B),writeq(set_varname(How,B)),nl,dtrace,trace_or_throw(var_assign_varname_vars(How,B)).
  802set_varname(How,N=V):-must(set_varname(How,N,V)),!.
  803
  804%set_varname(How,N,V):-var(V),var(N),!,V=N.
  805
  806%=
 set_varname(+How, ?N, ?V) is semidet
Set Varname.
  812set_varname(How,N,V):- (var(How);var(N)),trace_or_throw(var_var_set_varname(How,N,V)).
  813set_varname(_,_,NV):-nonvar(NV),ignore((NV='$VAR'(N),must(number(N);atom(N)))).
  814set_varname(_,_,V):-atom(V),!.
  815set_varname(How,'$VAR'(Name),V):- !, set_varname(How,Name,V).
  816set_varname(_:[How],N,V):- !, set_varname(How,N,V).
  817set_varname(_:[How|List],N,V):- !, set_varname(How,N,V),set_varname(List,N,V).
  818set_varname(How,N,V):- number(N),!,format(atom(VN),'~w',[N]),set_varname(How,VN,V).
  819set_varname(How,N,V):- atom(N),atom_concat('"?',LS,N),atom_concat(NN,'"',LS),fix_varcase_name(NN,VN),!,set_varname(How,VN,V).
  820set_varname(_M:write_functor,N,V):- !,ignore('$VAR'(N)=V),!.
  821set_varname(_M:write_attribute,N,V):-!,put_attr(V,vn,N).
  822set_varname(_M:put_attr,N,V):-!,put_attr(V,vn,N).
  823set_varname(Nb_setval,N,V):- get_varname_list(Vs),!,register_var(N,Vs,V,NewVs),call(call,Nb_setval,'$variable_names',NewVs).
  824set_varname(Nb_setval,N,V):- call(call,Nb_setval,'$variable_names',[N=V]).
  825%set_varname(Nb_setval,N,V):- must(call(call,Nb_setval,N,V)).
  826%set_varname(_How,_,_).
  827
  828
  829
  830write_functor(N=V):-write_functor(N,V).
  831
  832%=
 write_functor(?N, ?V) is semidet
Write Functor.
  838write_functor(N,V):-var(V),!,put_attr(V,vn,N).
  839write_functor(N,V):-ignore('$VAR'(N)=V),!.
  840
  841:- export(save_clause_vars/2).  842:-module_transparent(save_clause_vars/2).  843
  844%=
 save_clause_vars(?MHB, ?Vs) is semidet
Save Clause Variables.

save_clause_vars(_,[]):-!.

  851save_clause_vars(MHB,Vs):- ignore(maybe_record_scanned_file),ignore(current_why(Why)),
  852  ignore((var(Why),loading_file(Why))),!,save_clause_vars(MHB,Vs,Why).
  853
  854% ?- clause(pui_help:prolog_help_topic(A),B,ClauseRef), prolog_clause:clause_info(ClauseRef, File, TermPos, NameOffset, Options).
  855
  856
  857:- export(save_clause_vars/3).  858
  859%=
 save_clause_vars(?MHB, ?Vs, :TermWhy) is semidet
Save Clause Variables.
  865save_clause_vars(_, [],_):-!.
  866save_clause_vars(MHB,Vs,Why:_):-atom(Why),!,save_clause_vars(MHB,Vs,Why).
  867save_clause_vars(MHB,Vs,Why):-  ( \+ \+ (logicmoo_util_term_listing:as_clause_w_m(MHB, M, H, B, MB),save_clause_vars(M,H,MB,B,Vs,Why))).
  868
  869
  870
  871%=
 locate_clause_ref(?M, ?H, ?MB, ?B, ?ClauseRef) is semidet
Locate Clause Ref.
  877locate_clause_ref(M,H,_MB,_B,_ClauseRef):- ( \+ (predicate_property(M:H,number_of_clauses(_)))),( \+ (predicate_property(_:H,number_of_clauses(_)))),!,fail.
  878locate_clause_ref(M,H,MB,B,ClauseRef):-clause_asserted(M:H,MB:B,ClauseRef).
  879locate_clause_ref(_M,H,MB,B,ClauseRef):-clause_asserted(H,MB:B,ClauseRef).
  880locate_clause_ref(_M,H,MB,B,ClauseRef):-clause_asserted(_:H,MB:B,ClauseRef).
  881locate_clause_ref(M,H,_MB,B,ClauseRef):-clause_asserted(M:H,B,ClauseRef).
  882locate_clause_ref(_M,H,_MB,B,ClauseRef):-clause_asserted(H,B,ClauseRef).
  883locate_clause_ref(_M,H,_MB,B,ClauseRef):-clause_asserted(_:H,B,ClauseRef).
  884
  885
  886%=
 clause_ref_vars(?ClauseRef, ?Was) is semidet
Clause Ref Variables.
  892clause_ref_vars(ClauseRef,Was):-prolog_clause:clause_info(ClauseRef, _File, _TermPos, _NameOffset, [variable_names(Was)]).
  893
  894%=
 clause_ref_file(?ClauseRef, ?File) is semidet
Clause Ref File.
  900clause_ref_file(ClauseRef,File):-prolog_clause:clause_info(ClauseRef, File, _TermPos, _NameOffset, []).
  901
  902:- export(save_to_clause_ref/3).  903
  904%=
 save_to_clause_ref(?ClauseRef, ?Vs, ?Why) is semidet
Save Converted To Clause Ref.
  910save_to_clause_ref(ClauseRef,Vs,Why):- ain00(names(ClauseRef,Vs)),ain00(names_why(ClauseRef,Why)),!.
  911
  912:- export(save_clause_vars/6).  913
  914%=
 save_clause_vars(?M, ?H, ?MB, ?B, ?Vs, ?Why) is semidet
Save Clause Variables.
  920save_clause_vars(M,H,MB,B,Vs,Why:_):-atom(Why),!,save_clause_vars(M,H,MB,B,Vs,Why).
  921save_clause_vars(M,H,MB,B,Vs,Why):- fail, locate_clause_ref(M,H,MB,B,ClauseRef),clause_ref_vars(ClauseRef,Was),
  922   ((Was=Vs) -> fail ; save_to_clause_ref(ClauseRef,Vs,Why)),!.
  923save_clause_vars(_M,H,_MB,B,Vs,Why):- ain00(varname_cache:varname_info(H,B,Vs,Why)).
  924
  925
  926%=
 ain00(?A) is semidet
Assert If New Primary Helper Primary Helper.
  932ain00(A):- clause_asserted(A),!.
  933ain00(A):- assertz(A).
 contains_ftVar(+Term) is semidet
Contains Format Type Variable.
  940contains_ftVar(Term):- sub_term(Sub,Term),compound(Sub),Sub='$VAR'(_).
  941
  942
  943
  944%=
 ensure_vars_labled_r(?I, ?O) is semidet
Ensure Variables Labled R.
  950ensure_vars_labled_r(I,I):-!.
  951ensure_vars_labled_r(I,O):-
  952  once((((get_varname_list(Vs),Vs\==[])),
  953   copy_term(I:Vs,O:OVs),
  954    must_maplist(write_functor,OVs))),
  955   (O \=@= I ;  ground(O)),!.
  956
  957ensure_vars_labled_r(I,O):-
  958     once((get_clause_vars_copy(I,O),unlock_vars(O))),
  959     (O \=@= I ;  ground(O)),!.
  960
  961ensure_vars_labled_r(I,O):- copy_term_and_varnames(I,O),I\=@=O.
  962
  963:- export(copy_term_and_varnames/2).  964
  965%=
 copy_term_and_varnames(?Term, ?Named) is semidet
Copy Term And Varnames.
  971copy_term_and_varnames(Term,Named):- quietly((unnumbervars(Term,UNV),copy_term(UNV,Named))),!.
  972copy_term_and_varnames(Term,Named):- quietly((ignore((source_variables_lv(AllS))), copy_term(Term+AllS,Named+CAllS),maplist(set_varname([write_functor,b_setarg]),CAllS))).
  973
  974
  975%=
 renumbervars(?How, ?Term, ?Named) is semidet
Renumbervars.
  981renumbervars(How,Term,Named):-
  982   quietly((
  983   must_det((source_variables_lv(AllS);AllS=[])),
  984   copy_term(Term+AllS,Named+CAllS),
  985   maplist(set_varname(How),CAllS))).
  986
  987
  988
  989
  990
  991%=
 source_variables_lv(?AllS) is semidet
Source Variables Lv.
  997source_variables_lv(AllS):-
  998  (prolog_load_context(variable_names,Vs1);Vs1=[]),
  999  (get_varname_list(Vs2);Vs2=[]),
 1000  % quietly(catch((parent_goal('$toplevel':'$execute_goal2'(_, Vs3),_);Vs3=[]),E,(writeq(E),Vs3=[]))),
 1001  ignore(Vs3=[]),
 1002  append(Vs1,Vs2,Vs12),append(Vs12,Vs3,All),!,list_to_set(All,AllS),
 1003  set_varname_list( AllS).
 1004
 1005
 1006
 1007%=
 contain_numbervars(?Term) is semidet
Contain Numbervars.
 1013contain_numbervars(Term):- sub_term(Sub,Term),compound(Sub),Sub='$VAR'(_),!.
 1014
 1015
 1016
 1017
 1018%=
 contains_singletons(?Term) is semidet
Contains Singletons.
 1024contains_singletons(Term):-contains_singletons(Term,N),N>0.
 1025
 1026
 1027%=
 contains_singletons(?Term, ?N) is semidet
Contains Singletons.
 1033contains_singletons(Term,N):-
 1034  % sanity(\+contain_numbervars(Term)),
 1035     \+ ground(Term),
 1036     copy_term_nat(Term,Nat),
 1037     term_variables(Nat,Vs),
 1038     numbervars(Nat,0,_,[attvar(bind),singletons(true)]),!,
 1039     count_members_eq('$VAR'('_'),Vs,N).
 count_members_eq(+Find, +TermE, -N) is det
Count Members Using (==/2) (or =@=/2) ).
 1047count_members_eq(_,[],0):-!.
 1048count_members_eq(Find,[E|List],N):-
 1049  count_members_eq(Find,List,NN),!,  (E == Find -> N is NN+1 ; NN=N).
 call_not_not(:Goal) is semidet
Call \+ \+ Goal
 1057call_not_not(Goal):- \+ \+ Goal.
 1058
 1059
 1060%=
 contains_badvarnames(?Term) is semidet
Contains Badvarnames.
 1066contains_badvarnames(Term):-  quietly((sub_term(SubV,Term),compound(SubV),SubV='$VAR'(Sub),bad_varnamez(Sub))),!.
 1067
 1068contains_dvar(Term):-quietly((sub_term(SubV,Term),compound(SubV),SubV='$VAR'(_),!)).
 1069
 1070%=
 bad_varnamez(?Sub) is semidet
Bad Varnamez.
 1076bad_varnamez(Sub):- atom(Sub),!,sub_string(Sub,_,_,_,'.').
 1077bad_varnamez(Sub):- var(Sub),!.
 1078bad_varnamez(Sub):- integer(Sub),!, (Sub < 0 ; Sub > 991000).
 1079bad_varnamez(Sub):- number(Sub).
 1080bad_varnamez(Sub):- string(Sub),!.
 1081bad_varnamez(Sub):- format(atom(A),'~w',['$VAR'(Sub)]),sub_string(A,_,_,_,'$').
 1082
 1083
 1084%=
 mpred_numbervars_with_names(?Term) is semidet
Managed Predicate Numbervars Using Names.
 1090mpred_numbervars_with_names(Term):- term_variables(Term,Vars),mpred_name_variables(Vars),!,numbervars(Vars,91,_,[attvar(skip),singletons(true)]),!.
 1091
 1092
 1093%=
 mpred_name_variables(:TermVar) is semidet
Managed Predicate Name Variables.
 1099mpred_name_variables([]).
 1100mpred_name_variables([Var|Vars]):-
 1101   (var_property(Var, name(Name)) -> Var = '$VAR'(Name) ; true),
 1102   mpred_name_variables(Vars).
 1103
 1104
 1105%=========================================
 1106% unnumbervars
 1107%=========================================
 1108
 1109
 1110%=
 b_implode_varnames(?VALUE1) is semidet
?- b_implode_varnames(Vs),display(Vs).

'$VAR'('Vs')

 1118b_implode_varnames(T):- imploded_copyvars(T,TT),T=TT.
 1119
 1120%=
 b_implode_varnames0(:TermN) is semidet
Backtackable Implode Varnames Primary Helper.
 1127b_implode_varnames0([N=V|Vs]):- sanity(is_list(Vs)),ignore((nonvar(N),V='$VAR'(N);V=N)),b_implode_varnames0(Vs),!.
 1128b_implode_varnames0(_).
 1129
 1130
 1131%=
 imploded_copyvars(?C, ?CT) is semidet
Imploded Copyvars.
 1137imploded_copyvars(C,CT):-vmust((source_variables(Vs),copy_term(C-Vs,CT-VVs),b_implode_varnames0(VVs))),!.
 source_variables(?Vs) is semidet
Source Variables.
 1144source_variables(Vs):- 
 1145 (prolog_load_context(variable_names,Vs2);Vs2=[]),
 1146 (parent_goal('$toplevel':'$execute_goal2'(_, Vs1),_);Vs1=[]),
 1147 append(Vs1,Vs2,Vs3),list_to_set(Vs3,Vs),
 1148 (Vs\==Vs2 -> b_setval('$variable_names',Vs) ; true).
 1149
 1150source_variables0(Vs):- 
 1151 (prolog_load_context(variable_names,Vs2);Vs2=[]),
 1152 (parent_goal('$toplevel':'$execute_goal2'(_, Vs1),_);Vs1=[]),
 1153 append(Vs1,Vs2,Vs3),list_to_set(Vs3,Vs).
 1154 
 1155
 1156
 1157
 1158% snumbervars(Term,Functor,Start,End,List):-vmust(( vmust(var(End);number(End)),snumbervars4(Term,Start,End,[functor_name(Functor)|List]))),check_varnames(Term).
 1159
 1160
 1161
 1162%=
 check_varnames(:TermVs) is semidet
Check Varnames.
 1168check_varnames(Vs):-var(Vs),!.
 1169check_varnames([]):-!.
 1170check_varnames([N=V|Vs]):-atom(N),var(V),!,check_varnames(Vs).
 1171check_varnames(Term):- contains_badvarnames(Term),!,dumpST0,dtrace,nortrace,dtrace,!,dtrace(contains_badvarnames(Term)).
 1172check_varnames(_).
 1173
 1174:-meta_predicate(snumbervars4(0,?,?,?)). 1175
 1176%=
 snumbervars4(:GoalTerm, ?Start, ?End, ?List) is semidet
Snumbervars4.
 1182snumbervars4(Term,Start,End,List):-  \+ member(attvar(_),List),!,snumbervars5(Term,Start,End,[attvar(skip)|List]).
 1183snumbervars4(Term,Start,End,List):- snumbervars5(Term,Start,End,List).
 1184
 1185:-meta_predicate(snumbervars5(0,?,?,?)). 1186
 1187%=
 snumbervars5(:GoalTerm, ?Start, ?End, ?List) is semidet
Snumbervars5.
 1193snumbervars5(Term,Start,End,List):-must_det_l((integer(Start),is_list(List), numbervars(Term,Start,End,List),check_varnames(Term))).
 1194
 1195:- export(try_save_vars/1). 1196
 1197%=
 try_save_vars(?HB) is semidet
Try Save Variables.
 1203try_save_vars(_):- t_l:dont_varname,!.
 1204try_save_vars(HB):-ignore((get_varname_list(Vs),Vs\==[],save_clause_vars(HB,Vs))),!.
 1205
 1206:- export(maybe_scan_for_varnames/0). 1207
 1208%=
 maybe_scan_for_varnames is semidet
Maybe Scan Source Files For Varnames.
 1214maybe_scan_for_varnames:- current_prolog_flag(source_variables, true)->scan_for_varnames;true.
 1215
 1216:- export(scan_for_varnames/0). 1217
 1218:- dynamic(thglobal:scanning_for_varnames_already/0). 1219
 1220%=
 scan_for_varnames is semidet
Scan For Varnames.
 1226scan_for_varnames:- thglobal:scanning_for_varnames_already,!.
 1227scan_for_varnames:- swc, 
 1228 setup_call_cleanup(
 1229   asserta(thglobal:scanning_for_varnames_already),
 1230   scan_for_varnames0,
 1231   retractall(thglobal:scanning_for_varnames_already)).
 1232
 1233scan_for_varnames0:-
 1234 set_prolog_flag(source_variables, true),
 1235 ensure_loaded(library(make)),
 1236 doall((make:modified_file(F),retractall(varname_cache:varname_info_file(F)))),
 1237 doall((filematch(swi('boot/*.pl'),F),que_read_source_file_vars(F))),
 1238 doall((source_file(F),que_read_source_file_vars(F))),!,
 1239 ignore((
 1240   ( \+ \+ varname_cache:queued_read_source_file_vars(_)),
 1241   dmsg("Begining grovel for vars..."),
 1242   gripe_time(1.0,doall((retract(varname_cache:queued_read_source_file_vars(F)),
 1243     read_source_file_vars(F)))))).
 1244
 1245que_read_source_file_vars(F):-varname_cache:varname_info_file(F),!.
 1246que_read_source_file_vars(F):-ain00(varname_cache:queued_read_source_file_vars(F)).
 1247
 1248%=
 dcall_if_verbose(:GoalG) is semidet
Dirrectly Call If While Being Descriptive.
 1254dcall_if_verbose(G):-!, quietly(G).
 1255dcall_if_verbose(G):-show_call(why,G).
 1256
 1257%  list_undefined([module_class([user,system,library,test,temporary,development])]).
 1258:- dynamic(varname_cache:varname_info_file/1). 1259:- dynamic(varname_cache:queued_read_source_file_vars/1). 1260
 1261%=
 read_source_file_vars(?F) is semidet
Read Source File Variables.
 1267read_source_file_vars(_):- ( \+ current_prolog_flag(source_variables, true)),!.
 1268read_source_file_vars(F):- \+ ((atom(F),exists_file(F))),!, forall(filematch(F,E),read_source_file_vars(E)).
 1269read_source_file_vars(F):- clause_asserted(varname_cache:varname_info_file(F)),!.
 1270read_source_file_vars(F):- asserta(varname_cache:varname_info_file(F),Ref), catch((read_source_file_vars_1(F)),E,(dmsg(E),erase(Ref))).
 1271
 1272
 1273%=
 save_file_source_vars(?F, ?T, ?Vs) is semidet
Save File Source Variables.
 1279save_file_source_vars(_F,end_of_file,_Vs):-!.
 1280save_file_source_vars(_F,_T,[]):-!.
 1281save_file_source_vars(F,T,Vs):- put_variable_names(Vs),!,locally(t_l:current_why_source(F),save_clause_vars(T,Vs)),!.
 1282
 1283
 1284
 1285%=
 read_source_vars(?File, ?In) is semidet
Read Source Variables.
 1292read_source_vars(File,In):-
 1293   Module = module(_),
 1294	repeat,
 1295	  catch(prolog_read_source_term(In, Term, Expanded, [ variable_names(Vs), syntax_errors(error) , term_position(TermPos)  ]),
 1296		E,(nop((dmsg(E))),fail)),
 1297          arg(1,Module,M),
 1298          (Term = module(MM,_) -> (nb_setarg(1,Module,MM),fail);
 1299          ((stream_position_data(line_count, TermPos, LineNo),
 1300
 1301	  ignore(save_file_source_vars(mfl4(Vs,M,File,LineNo),Term,Vs)),
 1302	  (   is_list(Expanded)
 1303	  ->  member(T, Expanded)
 1304	  ;   T = Expanded
 1305	  ),
 1306	(   T == end_of_file
 1307	->  ! ;
 1308           ( T\==Term, save_file_source_vars(mfl4(Vs,M,File,LineNo),T,Vs),
 1309	    fail))))).
 1310
 1311
 1312% new method
 1313
 1314%=
 read_source_file_vars_1(?File) is semidet
read source file Variables Secondary Helper.
 1320read_source_file_vars_1('/usr/lib/swi-prolog/library/sandbox.pl'):-!.
 1321read_source_file_vars_1(File):-
 1322   once(current_prolog_flag(xref, Was);Was=false),
 1323   locally(set_prolog_flag(xref, true),
 1324	setup_call_cleanup(
 1325	    prolog_open_source(File, In),
 1326	    read_source_vars(File,In),
 1327	    (prolog_close_source(In),
 1328              current_prolog_flag(xref, Was)))),!.
 1329
 1330
 1331
 1332
 1333
 1334:- export(ensure_vars_labled/2). 1335
 1336
 1337mfree:attr_unify_hook(This,That):-get_attr(That,eq,Thats),Thats==This,!.
 1338mfree:attr_unify_hook(This,That):-get_attrs(That,Thats),get_attrs(This,Value),Thats==Value,!.
 1339never_bound(V):- var(V),!,put_attr(V,eq,_).
 1340never_bound(_).
 1341
 1342star:attr_unify_hook(This,That):-This==That.
 1343star:attr_unify_hook(_,That):-compound(That),That='$VAR'(Atom),nonvar(Atom).
 1344only_stars(V):- var(V),!,put_attr(V,star,V).
 1345only_stars(_).
 1346
 1347del_each_attr(M,V):-del_attr(V,M).
 1348
 1349%=
 ensure_vars_labled(?I, ?O) is semidet
Ensure Variables Labled.
 1355ensure_vars_labled(I,O):-nonvar(O),!,must(ensure_vars_labled(I,M)),!,M=O.
 1356ensure_vars_labled(I,I):- (t_l:dont_varname;no_vars_needed(I)),!.
 1357% ensure_vars_labled(I,I):- term_variables(I,Vs),maplist(never_bound,Vs),!.
 1358% ensure_vars_labled(I,I):- !.
 1359ensure_vars_labled(I,OO):- acyclic_term(O),term_variables(I,Vs),all_different_vals(Vs),ensure_vars_labled_r(I,O),vmust(acyclic_term(O)),!,OO=O.
 1360ensure_vars_labled(I,OO):- vmust(acyclic_term(I)),term_variables(I,Vs),all_different_vars(Vs),ensure_vars_labled_r(I,O),vmust(acyclic_term(O)),!,OO=O.
 1361ensure_vars_labled(I,I).
 1362
 1363
 1364:- multifile(user:portray/1). 1365
 1366%=
 portray(:TermObj) is semidet
Hook To [portray/1] For Module Logicmoo_varnames. Portray.
 1373user:portray(A) :- fail, \+ tracing,
 1374  catch(quietly(((compound(A);var(A)), current_prolog_flag(source_variables, true), set_prolog_flag(source_variables, false),
 1375    call_cleanup((((user:portray(A) -> ! ; print_numbervars_maybe(A)))),set_prolog_flag(source_variables, true)))),E,(writeq(E),nl,fail)).
 1376
 1377
 1378%=
 print_numbervars(?H) is semidet
Print Numbervars.
 1384print_numbervars(H):- (tlbugger:no_slow_io; tracing),!, writeq(H),!.
 1385print_numbervars(H):- must( \+ \+ ((on_x_log_fail(print_numbervars_maybe(H));on_x_log_fail(print_numbervars_1(H))))),!.
 1386
 1387
 1388
 1389%=
 print_numbervars_maybe(?H) is semidet
Print Numbervars Maybe.
 1395print_numbervars_maybe(H):-(compound(H);var(H)), copy_term(H,HC), \+ \+ ((get_clause_vars(H), HC\=@=H, print_numbervars_1(H))),!.
 1396
 1397
 1398%=
 print_numbervars_1(?H) is semidet
print numbervars Secondary Helper.
 1404print_numbervars_1(H):- loop_check(print_numbervars_2(H),format('~N~q.~n',[H])).
 1405
 1406% print_numbervars_2(H):- dtrace,baseKB:portray_one_line_hook(H),!.
 1407
 1408%=
 print_numbervars_2(?H) is semidet
print numbervars Extended Helper.
 1414print_numbervars_2(H):- current_output(S),prolog_listing:portray_clause(S,H,[portrayed(true),singletons(false)]),!.
 1415print_numbervars_2(H):- write_term(H,[portrayed(false)]),nl,!.
 1416
 1417
 1418:- thread_local(t_l:disable_px/0). 1419
 1420
 1421%=
 term_expansion_save_vars(?HB) is semidet
Term Expansion Save Variables.
 1427term_expansion_save_vars(HB):- \+ ground(HB),  \+ t_l:dont_varname_te,\+ t_l:dont_varname, % \+ current_prolog_flag(xref, true),
 1428   current_predicate(listing_vars_file/0), current_prolog_flag(source_variables,true),
 1429   source_context_module(M),init_varname_stores(M),
 1430   logicmoo_util_with_assertions:locally([t_l:dont_varname_te,t_l:disable_px],try_save_vars(M:HB)),!,fail.
 1431
 1432
 1433without_varname_scan(Goal):-
 1434  locally(current_prolog_flag(source_variables,false),
 1435   locally([ - t_l:dont_varname_te,- t_l:dont_varname],Goal)).
 1436
 1437
 1438%=
 maybe_record_scanned_file is semidet
Maybe Record Scanned File.
 1444maybe_record_scanned_file:-ignore((  source_location(F,_), \+ varname_cache:varname_info_file(F), asserta(varname_cache:varname_info_file(F)))).
 1445
 1446
 1447%=
 init_varname_stores(?M) is semidet
Init Varname Storages.
 1453init_varname_stores(_):- !.
 1454% init_varname_stores(M):- M:ensure_loaded(logicmoo_util_with_assertions), M:ensure_loaded(util_varnames).
 1455
 1456
 1457:- initialization(maybe_scan_for_varnames). 1458% :- maybe_scan_for_varnames.
 1459
 1460
 1461%=
 listing_vars_file is semidet
Logicmoo Util Varnames File.
 1467listing_vars_file.
 1468
 1469
 1470%=
 make_hook(?VALUE1, ?Files) is semidet
Hook To [prolog:make_hook/2] For Module Logicmoo_varnames. Make Hook.
 1477prolog:make_hook(before, Files):-forall(member(File,Files),retractall(varname_cache:varname_info_file(File))).
 1478% prolog:make_hook(after, Files):- forall(member(File,Files),show_call(why,ain00(varname_cache:varname_info_file(File)))).
 1479
 1480%=
 term_expansion(:TermFDecl, ?Clause) is semidet
Hook To [term_expansion/2] For Module Logicmoo_varnames. Term Expansion.
 1487user:term_expansion(HB,_):- current_prolog_flag(source_variables,true),term_expansion_save_vars(HB),fail.
 1488
 1489% :- use_module(library(logicmoo_utils_all)).
 1490:- fixup_exports.