1/*  Logicmoo Debug Tools
    2% ===================================================================
    3% File 'listing_vars.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: 'listing_vars.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/listing_vars.pl
   13:- module(listing_vars,
   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(rtrace)).
  110
  111:- multifile
  112        prolog:make_hook/2.
  113:- meta_predicate
  114       ain00(:),
  115        call_not_not(0),
  116        call_return_tf(0, ?),
  117        contains_singletons(*),
  118        get_clause_vars(:),
  119        get_clause_vars(:, ?),
  120        no_varnaming(0),
  121        without_varname_scan(0),
  122        dcall_when(2, ?, ?),
  123        snumbervars4(*, ?, ?, ?),
  124        snumbervars5(*, ?, ?, ?),
  125        try_save_vars(:),
  126        all_different_vals(2,*),
  127        all_disjoint_in_sets(2,*,*),
  128        init_varname_stores(?),
  129        vmust(0).
  130
  131:- module_transparent((
  132            all_different_vals/1,
  133            all_different_vals/2,
  134            set_varname/2,
  135            set_varname/3,
  136            atom_subst_frak_0/4,
  137            b_implode_varnames/1,
  138            b_implode_varnames0/1,
  139            bad_varnamez/1,
  140            call_not_not/1,
  141            call_return_tf/2,
  142            check_varnames/1,
  143            clause_ref_file/2,
  144            clause_ref_vars/2,
  145            contains_badvarnames/1,
  146            contains_singletons/1,
  147            del_attr_type/2,
  148            ensure_vars_labled/2,
  149            ensure_vars_labled_r/2,
  150            fix_varcase_name/2,
  151            get_clause_vars/1,
  152            get_clause_vars/2,
  153            get_clause_vars_copy/2,
  154            get_clause_vars_hb_int/2,
  155            imploded_copyvars/2,
  156            locate_clause_ref/5,
  157            
  158            listing_vars_file/0,
  159            make_subterm_path/3,
  160            maybe_record_scanned_file/0,
  161            no_varnaming/1,
  162            without_varname_scan/1,
  163            no_vars_needed/1,
  164            
  165   print_numbervars_maybe/1,
  166   print_numbervars/1,
  167            read_source_file_vars/1,
  168            read_source_file_vars_1/1,
  169            read_source_vars/2,
  170            replace_in_string_frak_0/4,
  171            save_clause_vars/2,
  172            save_clause_vars/3,
  173            save_clause_vars/6,
  174            save_file_source_vars/3,
  175            save_to_clause_ref/3,
  176            scan_for_varnames/0,
  177            dcall_if_verbose/1,
  178            dcall_when/3,
  179            snumbervars4/4,
  180            snumbervars5/4,
  181            subterm_path/3,
  182            term_expansion_save_vars/1,
  183            try_get_body_vars/1,
  184            try_get_head_vars/1,
  185            try_get_inner_vars/1,
  186            set_varname/2,
  187            try_save_vars/1,
  188            
  189            v_dif_rest/2,
  190            % name_variable/2, variable_name/2,
  191            vmust/1,
  192            init_varname_stores/1
  193 )).
  194:- meta_predicate dcall_if_verbose(0).
  195
  196:- reexport(library(lockable_vars)).
  197:- system:use_module(library(with_thread_local)).
  198% :- 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).
  207%name_variable(Var,_Name) :- nonvar(Var),!.
  208%name_variable(Var,Name) :- !, put_attr(Var,vn,Name).
  209
  210name_variable(Var, Name1) :- get_attr(Var,vn,Name2),
  211        combine_names(Name1,Name2,Name),
  212	put_attr(Var, vn, Name). % add_var_to_env(Name,Var),!.
  213name_variable(Var, Name) :- var(Var), !,
  214	put_attr(Var, vn, Name).
  215
  216name_variable('$VAR'(Var), Name):- Name==Var, !.
  217name_variable('$VAR'(Var), Name):- var(Var),Name=Var,!.
  218% name_variable('$VAR'(Var), Name) :- trace_or_throw(numbervars_name_variable(Var, Name)),!.
  219name_variable(_, _).
  220
  221:- nodebug(logicmoo(varnames)).
 variable_name(+Var, -Name) is semidet
True if Var has been assigned Name.
  229variable_name(Var, Name) :- must(var(Var)),(get_attr(Var, vn, Name);var_property(Var,name(Name));get_attr(Var, varnames, Name)),!.
  230
  231variable_name_or_ref(Var, Name) :- get_var_name(Var, Name),!.
  232variable_name_or_ref(Var, Name) :- format(atom(Name),'~q',[Var]).
 project_attributes(?QueryVars, ?ResidualVars) is semidet
Project Attributes.
  239vn: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.
  248vn:attribute_goals(Var) --> {get_var_name(Var,  Name)},!,[name_variable(Var,  Name)],!.
  249
  250get_var_name(V,N):-quietly(get_var_name0(V,N)),!.
  251
  252get_var_name0(Var,Name):- nonvar(Name),!,must(get_var_name0(Var, NameO)),!,Name=NameO.
  253get_var_name0(Var,Name):- nonvar(Var),!,get_var_name1(Var,Name),!.
  254get_var_name0(Var,Name):- get_attr(Var, vn, Name),!.
  255get_var_name0(Var,Name):- var_property(Var,name(Name)),!.
  256get_var_name0(Var,Name):- nb_current('$variable_names', Vs),member(Name=V,Vs),atomic(Name),V==Var,!.
  257get_var_name0(Var,Name):- get_attr(Var, varnames, Name),!.
  258get_var_name0(Var,Name):- nb_current('$old_variable_names', Vs),member(Name=V,Vs),atomic(Name),V==Var,!.
  259get_var_name0(Var,Name):- get_varname_list(Vs),member(Name=V,Vs),atomic(Name),V==Var,!.
  260% get_var_name0(Var,Name):- attvar(Var),get_varname_list(Vs),format(atom(Name),'~W',[Var, [variable_names(Vs)]]).
  261
  262get_var_name1('$VAR'(Name),Name):- atom(Name),!.
  263get_var_name1('$VAR'(Var),Name):- (var(Var)->get_var_name0(Var,Name);Name=Var),!.
  264get_var_name1('$VAR'(Att3),Name):- !, get_var_name1(Att3,Name).
  265get_var_name1('avar'(Att3),Name):- !, get_var_name1(Att3,Name).
  266get_var_name1('avar'(Name,Att3),Value):- !, get_var_name1('$VAR'(Name),Value); get_var_name1('avar'(Att3),Value).
  267get_var_name1(att(vn,Name,_),Name):- !.
  268get_var_name1(att(_,_,Rest),Name):- Rest\==[],get_var_name1(Rest,Name).
  269get_var_name1(Var,Name):- nb_current('$variable_names', Vs),member(Name=V,Vs),atomic(Name),V==Var,!.
  270get_var_name1(Var,Name):- oo_get_attr(Var, vn, Name),!. % ground(Name),!.
  271
  272
  273
  274%:- public ((attr_unify_hook/2,attr_portray_hook/2)).
  275% :- public portray_attvar/1.
  276% :- export(portray_attvar/1).
  277%:- export(attr_unify_hook/2).
  278%:- export(attr_portray_hook/2).
  279%:- export(attribute_goals/3).
  280
  281:- thread_local(t_l:varname_lock/1).
  282when_var_locked(What):- t_l:varname_lock(What),!.
  283
  284:- 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.
  291vn:attr_unify_hook(X,Y):- quietly(must(vn:attr_unify_hook0(X,Y))).
  292vn:attr_unify_hook0(_, Var):- cyclic_term(Var),!,fail.
  293vn:attr_unify_hook0(Var,_):- cyclic_term(Var),!,fail.
  294vn:attr_unify_hook0(Name1, Var):- when_var_locked(What),!,(unify_name_based(Name1, Var)->true;call(What,Var)).
  295vn:attr_unify_hook0(Name1, Var):- var(Var),unify_name_based(Name1, Var),!.
  296vn:attr_unify_hook0(_Form, _OtherValue):-!.
  297
  298
  299
  300unify_name_based(Var1, Var2):- \+ atom(Var1),variable_name_or_ref(Var1,Name),!,unify_name_based(Name, Var2).
  301unify_name_based(_Form, _OtherValue):- local_override(no_kif_var_coroutines,G),!,call(G).
  302unify_name_based(Name1, Var):-  get_var_name(Var,Name2),!,Name1=Name2,!.
  303unify_name_based(Name1, Var):- get_attr(Var, vn, Name2),!,combine_names(Name1,Name2,Name),(Name2==Name->true;put_attr(Var,vn,Name)).
  304unify_name_based(Name1, Var):- var(Var),!,put_attr(Var, vn, Name1).
  305unify_name_based(_, Var):- nonvar(Var),!.
  306%unify_name_based(_, Var):- cyclic_term(Var),!,fail.
  307%unify_name_based(_, Var):- cyclic_term(Var),!.
  308%unify_name_based(_, Var):- cyclic_break(Var),!,fail.
  309
  310combine_names(Name1,Name2,Name1):-Name1==Name2,!.
  311combine_names(Name1,Name2,Name):-
  312 ((atom_concat(_,Name1,Name2);atom_concat(Name1,_,Name2)) -> Name=Name2 ; (
  313   ((atom_concat(Name2,_,Name1);atom_concat(_,Name2,Name1)) -> Name=Name1 ; (
  314   (combine_names_dash(Name1,Name2,Name)))))).
  315
  316combine_names_dash(Name1,Name2,Name):- number_name(Name2),!,Name=Name1.
  317combine_names_dash(Name1,Name2,Name):- number_name(Name1),!,Name=Name2.
  318combine_names_dash(Name1,Name2,Name):- word_name(Name2),!,atomic_list_concat([Name2,'_',Name1],Name),!.
  319combine_names_dash(Name1,Name2,Name):- atomic_list_concat([Name1,'_',Name2],Name),!.
  320
  321number_name(Name):- name(Name,[95,Number|_]),char_type(Number,digit).
  322word_name(Name):- name(Name,[A,_|_]),char_type(A,upper).
  323
  324
  325/*
  326
  327%=
  328
  329%% attr_portray_hook( ?Value, ?Var) is semidet.
  330%
  331% Attr Portray Hook.
  332%
  333 % vn:attr_portray_hook(Name, _) :- write('???'), write(Name),!.
  334
  335:- multifile(user:portray/1).
  336:- dynamic(user:portray/1).
  337user:portray(Sk):- get_attr(Sk, vn, Name), get_attrs(Sk,att(vn,Name,[])),write(Name),!,write('{}').
  338
  339
  340%% portray_attvar( ?Var) is semidet.
  341%
  342% Hook To [portray_attvar/1] For Module Logicmoo_varnames.
  343% Portray Attribute Variable.
  344%
  345portray_attvar(Var) :-
  346	write('{<'),
  347        ((get_attr(Var,vn, VarName))->true;sformat(VarName,'~q',[Var])),
  348	get_attrs(Var, Attr),
  349	catch(writeq('??'(VarName,Attr)),_,'$attvar':portray_attrs(Attr, Var)),
  350	write('>}').
  351*/

  352
  353
  354
  355:- set_module(class(library)).
  356
  357:- use_module(library(when)).
  358
  359:- prolog_clause:multifile((
  360	unify_goal/5,			% +Read, +Decomp, +M, +Pos, -Pos
  361	unify_clause_hook/5,
  362	make_varnames_hook/5,
  363	open_source/2)).			% +Input, -Stream
  364
  365:- predicate_options(prolog_clause:clause_info/5, 5,
  366		     [ variable_names(-list)
  367		     ]).
  368
  369:- use_module(library(prolog_source)).
  370:- use_module(library(prolog_clause)). % read_term_at_line/6
  371
  372
  373:- meta_predicate vmust(0).
  374
  375%=
 vmust(:GoalG) is semidet
Vmust.
  381vmust(G):-!,must(G).
  382vmust(G):-!,call(G).
  383
  384
  385%=
 dcall_when(:PRED2P, ?In, ?Out) is semidet
Dirrectly Call When.
  391dcall_when(P,In,Out):- must(call(P,In,Out)),ignore((In\=@=Out,dmsg((dcall_when(P) :- (In,Out))))).
  392
  393:- create_prolog_flag(source_variables, false, [type(boolean)]).
  394:- thread_local(t_l:dont_varname/0).
  395:- thread_local(t_l:dont_varname_te/0).
  396:- thread_local(t_l:try_varname_clause_next/1).
  397
  398
  399%=
 no_varnaming(:GoalG) is semidet
No Varnaming.
  406no_varnaming(G):-locally(t_l:dont_varname,G).
  407
  408
  409%=
 all_different_vals(+P2, ?Term) is semidet
All Different Vals.
  415all_different_vals(Term):-all_different_vals(dif_matrix,Term).
 all_different_vars(?A) is semidet
All Different Variables.
  422all_different_vars(_):- t_l:dont_varname,!.
  423all_different_vars(A):-(quietly((all_disjoint_in_sets(dif_matrix,A,A)))),!.
  424all_different_vars(A):-must(quietly((all_disjoint_in_sets(dif_matrix,A,A)))),!.
  425all_different_vars(A):-all_different_vals(v_dif_rest,A),!.
 all_different_vals(:PRED2, +Term) is semidet
All Different Vals.
  433all_different_vals(Pred,Term):-
  434 must(quietly(( (is_list(Term)-> Slots = Term ; term_slots(Term,Slots)),!,
  435                                 all_disjoint_in_sets(Pred,Slots,Slots)))).
 all_different_vals(:PRED2, +SET1, +SET2) is semidet
All Different Vals.
  441all_disjoint_in_sets(_,[],_):-!.
  442all_disjoint_in_sets(_,[_],_):-!.
  443all_disjoint_in_sets(P,[V|Vs],SET):-delete_eq(SET,V,REST),!,call(P,V,REST),all_disjoint_in_sets(P,Vs,SET).
 v_dif_rest(?V, ?REST) is semidet
V not member_eq Rest.
  450v_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) ).
  456not_member_eq_local(_,[]):-!.
  457not_member_eq_local(E,REST):- \+ identical_memberchk(E,REST).
 dif_matrix(?V, ?REST) is semidet
V dif Rest of
  463dif_matrix(List,V):- is_list(List),!,list_to_set(List,SET), maplist(dif_matrix(V),SET).
  464dif_matrix(V,List):- is_list(List),!,list_to_set(List,SET), maplist(dif_matrix_hopfully(V),SET).
  465dif_matrix(A,B):- dif_matrix_hopfully(A,B).
  466dif_matrix_hopfully(A,B):-A==B,!.
  467dif_matrix_hopfully(A,B):- dif:dif(A,B),!.
  468
  469
  470%=
 make_subterm_path(?Sub, ?Term, ?PathO) is semidet
Make Subterm Path.
  476make_subterm_path(Sub,Term,PathO):-vmust(subterm_path(Sub,Term,Path)),!,PathO=Path.
  477
  478
  479%=
 subterm_path(?Sub, ?Term, :TermARG3) is semidet
Subterm Path.
  485subterm_path(Sub,Term,[]):-Sub==Term,!.
  486subterm_path(Sub,Term,[arg(N)|Path]):-compound(Term),!,arg(N,Term,TermE),subterm_path(Sub,TermE,Path),!.
  487
  488
  489%=
 get_clause_vars(?CV) is semidet
Get Clause Variables.
  495get_clause_vars(CV):- quietly(get_clause_vars_nontraced(CV)).
  496:- export(get_clause_vars_nontraced/1).
  497
  498%=
 get_clause_vars_nontraced(:TermMHB) is semidet
Get Clause Variables Nontraced.
  504get_clause_vars_nontraced(_):- t_l:dont_varname,!.
  505get_clause_vars_nontraced(V):- var(V),!.
  506get_clause_vars_nontraced('$VAR'(_)):- !.
  507get_clause_vars_nontraced(_:V):- var(V),!,ignore((get_varname_list(Vs),member(N=V0,Vs),V0==V,set_varname(write_functor,N,V))).
  508get_clause_vars_nontraced(MHB):- term_variables(MHB,Vs),must(get_clause_vars(MHB,Vs)).
  509
  510:- '$set_predicate_attribute'(get_clause_vars(_), trace, 1).
  511:- '$set_predicate_attribute'(get_clause_vars(_), hide_childs, 1).
  512
  513
  514%=
 del_attr_type(?Type, ?Var) is semidet
Remove/erase Attr Type.
  520del_attr_type(Type,Var):-ignore(del_attr(Var,Type)).
  521
  522
  523%=
 get_clause_vars(?MHB, ?Vs) is semidet
Get Clause Variables.
  529get_clause_vars(_,[]):-!.
  530get_clause_vars(MHB,[V|Vs]):- all_different_vars([V|Vs]),vmust((get_clause_vars_copy(MHB,WVARS),!,
  531   vmust(MHB=WVARS),unlock_vars(MHB),nop(sanity(check_varnames(MHB))))),!,
  532   maplist(del_attr_type(vl),[V|Vs]).
  533get_clause_vars(MHB,Vs):- vmust((get_clause_vars_copy(MHB,WVARS),!,vmust(MHB=WVARS),unlock_vars(MHB),must(check_varnames(Vs)))),!.
  534get_clause_vars(_,_):- !.
  535
  536
  537
  538%=
 get_clause_vars_copy(?HB, ?HB) is semidet
Get Clause Variables Copy.
  544get_clause_vars_copy(HB,HB):- ground(HB),!.
  545get_clause_vars_copy(HH,HH):- sub_term(S,HH),compound(S),S='$VAR'(_),!. % already labled
  546get_clause_vars_copy(H0,MHB):- copy_term(H0,MHB),!.
  547get_clause_vars_copy(H0,MHB):-
  548    source_variables_lv(AllS),
  549    must((copy_term(H0+AllS,MHB+CAllS),
  550    term_slots(MHB,Slots),
  551   % all_different_vars(Slots),
  552    lock_vars(Slots),
  553    as_clause_no_m( MHB,  H, B),
  554    must_maplist(set_varname(write_functor),CAllS),
  555    get_clause_vars_hb_int(H,B))),!.
  556
  557get_clause_vars_copy(H0,MHB):-
  558    must((copy_term_and_varnames(H0,MHB),lock_vars(MHB),as_clause_no_m( MHB,  H, B),
  559    get_clause_vars_hb_int(H,B))),!.
  560
  561
  562
  563
  564%=
 get_clause_vars_hb_int(?H, ?B) is semidet
Get Clause Variables Head+body Int.
  570get_clause_vars_hb_int(H,B):- varname_cache:varname_info(H,B,Vs,_),must_maplist(set_varname(write_functor),Vs),!.
  571get_clause_vars_hb_int(H,B):- call_return_tf(try_get_body_vars(B),_TF1),call_return_tf(try_get_head_vars(H),_TF2),!.
  572
  573
  574
  575%=
 atom_subst_frak_0(?A, ?F, ?R, ?K) is semidet
atom subst frak Primary Helper.
  581atom_subst_frak_0(A,F,R,K):-replace_in_string_frak_0(F,R,A,K),!.
  582
  583%=
 replace_in_string_frak_0(?F, ?R, ?A, ?K) is semidet
replace in string frak Primary Helper.
  589replace_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).
  590replace_in_string_frak_0(SepChars,Repl,A,C):- atomics_to_string(B,SepChars,A),atomics_to_string(B,Repl,C).
  591
  592
  593
  594%=
 fix_varcase_name(?N, ?VN) is semidet
Fix Varcase Name.
  600fix_varcase_name(N,VN):-atom_subst_frak_0(N,'-','_',O),atom_subst_frak_0(O,'?','_',VN).
  601
  602
  603%=
 no_vars_needed(?H) is semidet
No Variables Needed.
  609no_vars_needed(H):- (t_l:dont_varname; ( ground(H) ; \+ compound(H))) ,!.
  610no_vars_needed(A:H):-atom(A),!,no_vars_needed(H).
  611no_vars_needed(H):-var(H),!.
  612no_vars_needed('$VAR'(_)):-!.
  613no_vars_needed(H):- compound(H),H=varname_info(_,_,_,_),!.
  614
  615%=
 try_get_inner_vars(?H) is semidet
Try Get Inner Variables.
  621try_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).
  622
  623
  624
  625%=
 term_slots(?Term, ?Slots) is semidet
Hook To [term_slots/2] For Module Logicmoo_varnames. Term Slots.
  632term_slots(Term,Slots):-term_singleslots(Term, [],NS, [],S),append(NS,S,Slots).
  633
  634
  635%=
 term_singleslots(?A, ?Vs) is semidet
Hook To [term_singleslots/2] For Module Logicmoo_varnames. Term Singletons.
  642term_singleslots(A,Vs):- quietly(term_singleslots(A,[],_,[],Vs)).
  643%= %= :- was_export(term_singleslots/3).
  644
  645%=
 term_singleslots(?Term, ?NonSingle, ?Singles) is semidet
Term Singletons.
  651term_singleslots(Term,NonSingle,Singles):- quietly(term_singleslots(Term,[],NonSingle,[],Singles)).
  652%= %= :- was_export(term_singleslots/5).
  653
  654%=
 term_singleslots(:TermFml, +NonSinglesIn, -NonSinglesOut, +SinglesIn, -SinglesOut) is semidet
Term Singletons.
  660term_singleslots(Fml, NS,NS, S,S):- atomic(Fml),!.
  661term_singleslots(Fml, NS,NS, S,S):- identical_memberchk(Fml,NS),!.
  662term_singleslots(Fml, NS, [Fml|NS], S, NSV):- is_ftVar(Fml),identical_memberchk(Fml,S),!,delete_eq(S,Fml,NSV),!.
  663term_singleslots(Fml, NS, NS, S, [Fml|S]):- is_ftVar(Fml),!.
  664term_singleslots([H|T],NS,NSO,S,NSV):- !, term_singleslots(H,NS,NSM,S,M),term_singleslots(T,NSM,NSO,M,NSV).
  665term_singleslots(Fml, NS,NSO, S,NSV):- compound(Fml),Fml=..[_|T],!, term_singleslots(T, NS,NSO, S,NSV).
  666
  667
  668%=
 call_return_tf(:Goal, ?TF) is semidet
Call Return True/false.
  674call_return_tf(Call,TF):- ((Call-> TF = t ; TF = nil)).
  675
  676
  677
  678%=
 try_get_varname_cache(?H) is semidet
Try Get Varname Cache.
  684try_get_varname_cache(H):- no_vars_needed(H),!.
  685try_get_varname_cache(H):- varname_cache:varname_info(H,_,Vs,_),maplist(set_varname(write_functor),Vs),!.
  686try_get_varname_cache(H):- varname_cache:varname_info(_,H,Vs,_),maplist(set_varname(write_functor),Vs),!.
  687
  688%=
 try_get_head_vars(?H) is semidet
Try Get Head Variables.
  694try_get_head_vars(H):- no_vars_needed(H),!.
  695try_get_head_vars(H):- varname_cache:varname_info(H,_,Vs,_),maplist(set_varname(write_functor),Vs),!.
  696try_get_head_vars(H):- try_get_inner_vars(H).
  697try_get_head_vars(H):- varname_cache:varname_info(_,H,Vs,_),maplist(set_varname(write_functor),Vs),!.
  698try_get_head_vars(B):- must(get_random_headvars(B)),!.
  699
  700
  701
  702%=
 try_varname_infos(?H) is semidet
Try Varname Infos.
  708try_varname_infos(H):- varname_cache:varname_info(H,_,Vs,_),maplist(set_varname(write_functor),Vs),!.
  709
  710
  711%=
 get_random_headvars(?H) is semidet
Get Random Headvars.
  717get_random_headvars(H):- \+ compound(H),!.
  718get_random_headvars(H):- functor(H,F,A),get_random_headvars(H,F,A,A).
  719
  720
  721%=
 get_random_headvars(?H, ?F, ?A, ?N) is semidet
Get Random Headvars.
  727get_random_headvars(_H,_F,A,N):- (N < 1 ; N>A),!.
  728get_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).
  729
  730
  731%=
 get_1head_arg_var(?H, ?F, ?N, ?A, ?HA) is semidet
Get 1head Argument Variable.
  737get_1head_arg_var(_H,F,N,A,HA):-
  738 functor(HH,F,A),
  739 (nonvar(HA)->!;
  740 (arg(N,HH,COMP),
  741  (( arg_varname(HH,N,Name),HA='$VAR'(Name));(try_get_varname_cache(HH),sub_ft_var(HA,COMP))))).
  742
  743
  744%=
 sub_ft_var(?HA, ?COMP) is semidet
Sub Format Type Variable.
  750sub_ft_var(HA,COMP):-
  751  (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.
  758arg_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).
  759
  760
  761%=
 try_get_body_vars(:TermH) is semidet
Try Get Body Variables.
  767try_get_body_vars(H):- no_vars_needed(H),!.
  768try_get_body_vars(H):- varname_cache:varname_info(_,H,Vs,_),maplist(set_varname(write_functor),Vs),!.
  769try_get_body_vars(H):- try_get_inner_vars(H),!.
  770try_get_body_vars(H):- varname_cache:varname_info(H,_,Vs,_),maplist(set_varname(write_functor),Vs),!.
  771try_get_body_vars((A,B)):-!,try_get_head_vars(A),try_get_head_vars(B).
  772try_get_body_vars((A;B)):-!,try_get_head_vars(A),try_get_head_vars(B).
  773try_get_body_vars(C):- C=..[_,L],maplist(try_get_body_vars,L).
  774try_get_body_vars(B):- must(get_random_headvars(B)),!.
  775try_get_body_vars(_).
  776
  777:- multifile(varname_cache:varname_info/4).
  778:- dynamic(varname_cache:varname_info/4).
  779
  780:- meta_predicate renumbervars(*,*,*).
  781
  782:- meta_predicate set_varname(+,*,*).
  783:- meta_predicate set_varname(+,*).
  784% set_varname(How,B):-var(B),!.
  785
  786%=
 set_varname(+How, ?B) is semidet
Set Varname.
  792:- meta_predicate set_varname(:,*,*).
  793:- meta_predicate set_varname(:,*).
  794
  795set_varname(How,B):-var(B),writeq(set_varname(How,B)),nl,dtrace,trace_or_throw(var_assign_varname_vars(How,B)).
  796set_varname(How,N=V):-must(set_varname(How,N,V)),!.
  797
  798%set_varname(How,N,V):-var(V),var(N),!,V=N.
  799
  800%=
 set_varname(+How, ?N, ?V) is semidet
Set Varname.
  806set_varname(How,N,V):- (var(How);var(N)),trace_or_throw(var_var_set_varname(How,N,V)).
  807set_varname(_,_,NV):-nonvar(NV),ignore((NV='$VAR'(N),must(number(N);atom(N)))).
  808set_varname(_,_,V):-atom(V),!.
  809set_varname(How,'$VAR'(Name),V):- !, set_varname(How,Name,V).
  810set_varname(_:[How],N,V):- !, set_varname(How,N,V).
  811set_varname(_:[How|List],N,V):- !, set_varname(How,N,V),set_varname(List,N,V).
  812set_varname(How,N,V):- number(N),!,format(atom(VN),'~w',[N]),set_varname(How,VN,V).
  813set_varname(How,N,V):- atom(N),atom_concat('"?',LS,N),atom_concat(NN,'"',LS),fix_varcase_name(NN,VN),!,set_varname(How,VN,V).
  814set_varname(_M:write_functor,N,V):- !,ignore('$VAR'(N)=V),!.
  815set_varname(_M:write_attribute,N,V):-!,put_attr(V,vn,N).
  816set_varname(_M:put_attr,N,V):-!,put_attr(V,vn,N).
  817set_varname(Nb_setval,N,V):- get_varname_list(Vs),!,register_var(N,Vs,V,NewVs),call(call,Nb_setval,'$variable_names',NewVs).
  818set_varname(Nb_setval,N,V):- call(call,Nb_setval,'$variable_names',[N=V]).
  819%set_varname(Nb_setval,N,V):- must(call(call,Nb_setval,N,V)).
  820%set_varname(_How,_,_).
  821
  822
  823
  824write_functor(N=V):-write_functor(N,V).
  825
  826%=
 write_functor(?N, ?V) is semidet
Write Functor.
  832write_functor(N,V):-var(V),!,put_attr(V,vn,N).
  833write_functor(N,V):-ignore('$VAR'(N)=V),!.
  834
  835:-export(save_clause_vars/2).
  836:-module_transparent(save_clause_vars/2).
  837
  838%=
 save_clause_vars(?MHB, ?Vs) is semidet
Save Clause Variables.

save_clause_vars(_,[]):-!.

  845save_clause_vars(MHB,Vs):- ignore(maybe_record_scanned_file),ignore(current_why(Why)),
  846  ignore((var(Why),loading_file(Why))),!,save_clause_vars(MHB,Vs,Why).
  847
  848% ?- clause(pui_help:prolog_help_topic(A),B,ClauseRef), prolog_clause:clause_info(ClauseRef, File, TermPos, NameOffset, Options).
  849
  850
  851:-export(save_clause_vars/3).
  852
  853%=
 save_clause_vars(?MHB, ?Vs, :TermWhy) is semidet
Save Clause Variables.
  859save_clause_vars(_, [],_):-!.
  860save_clause_vars(MHB,Vs,Why:_):-atom(Why),!,save_clause_vars(MHB,Vs,Why).
  861save_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))).
  862
  863
  864
  865%=
 locate_clause_ref(?M, ?H, ?MB, ?B, ?ClauseRef) is semidet
Locate Clause Ref.
  871locate_clause_ref(M,H,_MB,_B,_ClauseRef):- ( \+ (predicate_property(M:H,number_of_clauses(_)))),( \+ (predicate_property(_:H,number_of_clauses(_)))),!,fail.
  872locate_clause_ref(M,H,MB,B,ClauseRef):-clause_asserted(M:H,MB:B,ClauseRef).
  873locate_clause_ref(_M,H,MB,B,ClauseRef):-clause_asserted(H,MB:B,ClauseRef).
  874locate_clause_ref(_M,H,MB,B,ClauseRef):-clause_asserted(_:H,MB:B,ClauseRef).
  875locate_clause_ref(M,H,_MB,B,ClauseRef):-clause_asserted(M:H,B,ClauseRef).
  876locate_clause_ref(_M,H,_MB,B,ClauseRef):-clause_asserted(H,B,ClauseRef).
  877locate_clause_ref(_M,H,_MB,B,ClauseRef):-clause_asserted(_:H,B,ClauseRef).
  878
  879
  880%=
 clause_ref_vars(?ClauseRef, ?Was) is semidet
Clause Ref Variables.
  886clause_ref_vars(ClauseRef,Was):-prolog_clause:clause_info(ClauseRef, _File, _TermPos, _NameOffset, [variable_names(Was)]).
  887
  888%=
 clause_ref_file(?ClauseRef, ?File) is semidet
Clause Ref File.
  894clause_ref_file(ClauseRef,File):-prolog_clause:clause_info(ClauseRef, File, _TermPos, _NameOffset, []).
  895
  896:-export(save_to_clause_ref/3).
  897
  898%=
 save_to_clause_ref(?ClauseRef, ?Vs, ?Why) is semidet
Save Converted To Clause Ref.
  904save_to_clause_ref(ClauseRef,Vs,Why):- ain00(names(ClauseRef,Vs)),ain00(names_why(ClauseRef,Why)),!.
  905
  906:-export(save_clause_vars/6).
  907
  908%=
 save_clause_vars(?M, ?H, ?MB, ?B, ?Vs, ?Why) is semidet
Save Clause Variables.
  914save_clause_vars(M,H,MB,B,Vs,Why:_):-atom(Why),!,save_clause_vars(M,H,MB,B,Vs,Why).
  915save_clause_vars(M,H,MB,B,Vs,Why):- fail, locate_clause_ref(M,H,MB,B,ClauseRef),clause_ref_vars(ClauseRef,Was),
  916   ((Was=Vs) -> fail ; save_to_clause_ref(ClauseRef,Vs,Why)),!.
  917save_clause_vars(_M,H,_MB,B,Vs,Why):- ain00(varname_cache:varname_info(H,B,Vs,Why)).
  918
  919
  920%=
 ain00(?A) is semidet
Assert If New Primary Helper Primary Helper.
  926ain00(A):- clause_asserted(A),!.
  927ain00(A):- assertz(A).
 contains_ftVar(+Term) is semidet
Contains Format Type Variable.
  934contains_ftVar(Term):- sub_term(Sub,Term),compound(Sub),Sub='$VAR'(_).
  935
  936
  937
  938%=
 ensure_vars_labled_r(?I, ?O) is semidet
Ensure Variables Labled R.
  944ensure_vars_labled_r(I,I):-!.
  945ensure_vars_labled_r(I,O):-
  946  once((((get_varname_list(Vs),Vs\==[])),
  947   copy_term(I:Vs,O:OVs),
  948    must_maplist(write_functor,OVs))),
  949   (O \=@= I ;  ground(O)),!.
  950
  951ensure_vars_labled_r(I,O):-
  952     once((get_clause_vars_copy(I,O),unlock_vars(O))),
  953     (O \=@= I ;  ground(O)),!.
  954
  955ensure_vars_labled_r(I,O):- copy_term_and_varnames(I,O),I\=@=O.
  956
  957:-export(copy_term_and_varnames/2).
  958
  959%=
 copy_term_and_varnames(?Term, ?Named) is semidet
Copy Term And Varnames.
  965copy_term_and_varnames(Term,Named):- quietly((unnumbervars(Term,UNV),copy_term(UNV,Named))),!.
  966copy_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))).
  967
  968
  969%=
 renumbervars(?How, ?Term, ?Named) is semidet
Renumbervars.
  975renumbervars(How,Term,Named):-
  976   quietly((
  977   must_det((source_variables_lv(AllS);AllS=[])),
  978   copy_term(Term+AllS,Named+CAllS),
  979   maplist(set_varname(How),CAllS))).
  980
  981
  982
  983
  984
  985%=
 source_variables_lv(?AllS) is semidet
Source Variables Lv.
  991source_variables_lv(AllS):-
  992  (prolog_load_context(variable_names,Vs1);Vs1=[]),
  993  (get_varname_list(Vs2);Vs2=[]),
  994  % quietly(catch((parent_goal('$toplevel':'$execute_goal2'(_, Vs3),_);Vs3=[]),E,(writeq(E),Vs3=[]))),
  995  ignore(Vs3=[]),
  996  append(Vs1,Vs2,Vs12),append(Vs12,Vs3,All),!,list_to_set(All,AllS),
  997  set_varname_list( AllS).
  998
  999
 1000
 1001%=
 contain_numbervars(?Term) is semidet
Contain Numbervars.
 1007contain_numbervars(Term):- sub_term(Sub,Term),compound(Sub),Sub='$VAR'(_),!.
 1008
 1009
 1010
 1011
 1012%=
 contains_singletons(?Term) is semidet
Contains Singletons.
 1018contains_singletons(Term):-contains_singletons(Term,N),N>0.
 1019
 1020
 1021%=
 contains_singletons(?Term, ?N) is semidet
Contains Singletons.
 1027contains_singletons(Term,N):-
 1028  % sanity(\+contain_numbervars(Term)),
 1029     \+ ground(Term),
 1030     copy_term_nat(Term,Nat),
 1031     term_variables(Nat,Vs),
 1032     numbervars(Nat,0,_,[attvar(bind),singletons(true)]),!,
 1033     count_members_eq('$VAR'('_'),Vs,N).
 count_members_eq(+Find, +TermE, -N) is det
Count Members Using (==/2) (or =@=/2) ).
 1041count_members_eq(_,[],0):-!.
 1042count_members_eq(Find,[E|List],N):-
 1043  count_members_eq(Find,List,NN),!,  (E == Find -> N is NN+1 ; NN=N).
 call_not_not(:Goal) is semidet
Call \+ \+ Goal
 1051call_not_not(Goal):- \+ \+ Goal.
 1052
 1053
 1054%=
 contains_badvarnames(?Term) is semidet
Contains Badvarnames.
 1060contains_badvarnames(Term):-  quietly((sub_term(SubV,Term),compound(SubV),SubV='$VAR'(Sub),bad_varnamez(Sub))),!.
 1061
 1062contains_dvar(Term):-quietly((sub_term(SubV,Term),compound(SubV),SubV='$VAR'(_),!)).
 1063
 1064%=
 bad_varnamez(?Sub) is semidet
Bad Varnamez.
 1070bad_varnamez(Sub):- atom(Sub),!,sub_string(Sub,_,_,_,'.').
 1071bad_varnamez(Sub):- var(Sub),!.
 1072bad_varnamez(Sub):- integer(Sub),!, (Sub < 0 ; Sub > 991000).
 1073bad_varnamez(Sub):- number(Sub).
 1074bad_varnamez(Sub):- string(Sub),!.
 1075bad_varnamez(Sub):- format(atom(A),'~w',['$VAR'(Sub)]),sub_string(A,_,_,_,'$').
 1076
 1077
 1078%=
 mpred_numbervars_with_names(?Term) is semidet
Managed Predicate Numbervars Using Names.
 1084mpred_numbervars_with_names(Term):- term_variables(Term,Vars),mpred_name_variables(Vars),!,numbervars(Vars,91,_,[attvar(skip),singletons(true)]),!.
 1085
 1086
 1087%=
 mpred_name_variables(:TermVar) is semidet
Managed Predicate Name Variables.
 1093mpred_name_variables([]).
 1094mpred_name_variables([Var|Vars]):-
 1095   (var_property(Var, name(Name)) -> Var = '$VAR'(Name) ; true),
 1096   mpred_name_variables(Vars).
 1097
 1098
 1099%=========================================
 1100% unnumbervars
 1101%=========================================
 1102
 1103
 1104%=
 b_implode_varnames(?VALUE1) is semidet
?- b_implode_varnames(Vs),display(Vs).

'$VAR'('Vs')

 1112b_implode_varnames(T):- imploded_copyvars(T,TT),T=TT.
 1113
 1114%=
 b_implode_varnames0(:TermN) is semidet
Backtackable Implode Varnames Primary Helper.
 1121b_implode_varnames0([N=V|Vs]):- sanity(is_list(Vs)),ignore((nonvar(N),V='$VAR'(N);V=N)),b_implode_varnames0(Vs),!.
 1122b_implode_varnames0(_).
 1123
 1124
 1125%=
 imploded_copyvars(?C, ?CT) is semidet
Imploded Copyvars.
 1131imploded_copyvars(C,CT):-vmust((source_variables(Vs),copy_term(C-Vs,CT-VVs),b_implode_varnames0(VVs))),!.
 source_variables(?Vs) is semidet
Source Variables.
 1138source_variables(Vs):- 
 1139 (prolog_load_context(variable_names,Vs2);Vs2=[]),
 1140 (parent_goal('$toplevel':'$execute_goal2'(_, Vs1),_);Vs1=[]),
 1141 append(Vs1,Vs2,Vs3),list_to_set(Vs3,Vs),
 1142 (Vs\==Vs2 -> b_setval('$variable_names',Vs) ; true).
 1143
 1144source_variables0(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 
 1149
 1150
 1151
 1152% snumbervars(Term,Functor,Start,End,List):-vmust(( vmust(var(End);number(End)),snumbervars4(Term,Start,End,[functor_name(Functor)|List]))),check_varnames(Term).
 1153
 1154
 1155
 1156%=
 check_varnames(:TermVs) is semidet
Check Varnames.
 1162check_varnames(Vs):-var(Vs),!.
 1163check_varnames([]):-!.
 1164check_varnames([N=V|Vs]):-atom(N),var(V),!,check_varnames(Vs).
 1165check_varnames(Term):- contains_badvarnames(Term),!,dumpST0,dtrace,nortrace,dtrace,!,dtrace(contains_badvarnames(Term)).
 1166check_varnames(_).
 1167
 1168:-meta_predicate(snumbervars4(0,?,?,?)).
 1169
 1170%=
 snumbervars4(:GoalTerm, ?Start, ?End, ?List) is semidet
Snumbervars4.
 1176snumbervars4(Term,Start,End,List):-  \+ member(attvar(_),List),!,snumbervars5(Term,Start,End,[attvar(skip)|List]).
 1177snumbervars4(Term,Start,End,List):- snumbervars5(Term,Start,End,List).
 1178
 1179:-meta_predicate(snumbervars5(0,?,?,?)).
 1180
 1181%=
 snumbervars5(:GoalTerm, ?Start, ?End, ?List) is semidet
Snumbervars5.
 1187snumbervars5(Term,Start,End,List):-must_det_l((integer(Start),is_list(List), numbervars(Term,Start,End,List),check_varnames(Term))).
 1188
 1189:-export(try_save_vars/1).
 1190
 1191%=
 try_save_vars(?HB) is semidet
Try Save Variables.
 1197try_save_vars(_):- t_l:dont_varname,!.
 1198try_save_vars(HB):-ignore((get_varname_list(Vs),Vs\==[],save_clause_vars(HB,Vs))),!.
 1199
 1200:-export(maybe_scan_for_varnames/0).
 1201
 1202%=
 maybe_scan_for_varnames is semidet
Maybe Scan Source Files For Varnames.
 1208maybe_scan_for_varnames:- current_prolog_flag(source_variables, true)->scan_for_varnames;true.
 1209
 1210:-export(scan_for_varnames/0).
 1211
 1212:- dynamic(thglobal:scanning_for_varnames_already/0).
 1213
 1214%=
 scan_for_varnames is semidet
Scan For Varnames.
 1220scan_for_varnames:- thglobal:scanning_for_varnames_already,!.
 1221scan_for_varnames:- swc, 
 1222 setup_call_cleanup(
 1223   asserta(thglobal:scanning_for_varnames_already),
 1224   scan_for_varnames0,
 1225   retractall(thglobal:scanning_for_varnames_already)).
 1226
 1227scan_for_varnames0:-
 1228 set_prolog_flag(source_variables, true),
 1229 ensure_loaded(library(make)),
 1230 doall((make:modified_file(F),retractall(varname_cache:varname_info_file(F)))),
 1231 doall((filematch(swi('boot/*.pl'),F),que_read_source_file_vars(F))),
 1232 doall((source_file(F),que_read_source_file_vars(F))),!,
 1233 ignore((
 1234   ( \+ \+ varname_cache:queued_read_source_file_vars(_)),
 1235   dmsg("Begining grovel for vars..."),
 1236   gripe_time(1.0,doall((retract(varname_cache:queued_read_source_file_vars(F)),
 1237     read_source_file_vars(F)))))).
 1238
 1239que_read_source_file_vars(F):-varname_cache:varname_info_file(F),!.
 1240que_read_source_file_vars(F):-ain00(varname_cache:queued_read_source_file_vars(F)).
 1241
 1242%=
 dcall_if_verbose(:GoalG) is semidet
Dirrectly Call If While Being Descriptive.
 1248dcall_if_verbose(G):-!, quietly(G).
 1249dcall_if_verbose(G):-show_call(why,G).
 1250
 1251%  list_undefined([module_class([user,system,library,test,temporary,development])]).
 1252:- dynamic(varname_cache:varname_info_file/1).
 1253:- dynamic(varname_cache:queued_read_source_file_vars/1).
 1254
 1255%=
 read_source_file_vars(?F) is semidet
Read Source File Variables.
 1261read_source_file_vars(_):- ( \+ current_prolog_flag(source_variables, true)),!.
 1262read_source_file_vars(F):- \+ ((atom(F),exists_file(F))),!, forall(filematch(F,E),read_source_file_vars(E)).
 1263read_source_file_vars(F):- clause_asserted(varname_cache:varname_info_file(F)),!.
 1264read_source_file_vars(F):- asserta(varname_cache:varname_info_file(F),Ref), catch((read_source_file_vars_1(F)),E,(dmsg(E),erase(Ref))).
 1265
 1266
 1267%=
 save_file_source_vars(?F, ?T, ?Vs) is semidet
Save File Source Variables.
 1273save_file_source_vars(_F,end_of_file,_Vs):-!.
 1274save_file_source_vars(_F,_T,[]):-!.
 1275save_file_source_vars(F,T,Vs):- put_variable_names(Vs),!,locally(t_l:current_why_source(F),save_clause_vars(T,Vs)),!.
 1276
 1277
 1278
 1279%=
 read_source_vars(?File, ?In) is semidet
Read Source Variables.
 1286read_source_vars(File,In):-
 1287   Module = module(_),
 1288	repeat,
 1289	  catch(prolog_read_source_term(In, Term, Expanded, [ variable_names(Vs), syntax_errors(error) , term_position(TermPos)  ]),
 1290		E,(nop((dmsg(E))),fail)),
 1291          arg(1,Module,M),
 1292          (Term = module(MM,_) -> (nb_setarg(1,Module,MM),fail);
 1293          ((stream_position_data(line_count, TermPos, LineNo),
 1294
 1295	  ignore(save_file_source_vars(mfl(M,File,LineNo),Term,Vs)),
 1296	  (   is_list(Expanded)
 1297	  ->  member(T, Expanded)
 1298	  ;   T = Expanded
 1299	  ),
 1300	(   T == end_of_file
 1301	->  ! ;
 1302           ( T\==Term, save_file_source_vars(mfl(M,File,LineNo),T,Vs),
 1303	    fail))))).
 1304
 1305
 1306% new method
 1307
 1308%=
 read_source_file_vars_1(?File) is semidet
read source file Variables Secondary Helper.
 1314read_source_file_vars_1('/usr/lib/swi-prolog/library/sandbox.pl'):-!.
 1315read_source_file_vars_1(File):-
 1316   once(current_prolog_flag(xref, Was);Was=false),
 1317   locally(set_prolog_flag(xref, true),
 1318	setup_call_cleanup(
 1319	    prolog_open_source(File, In),
 1320	    read_source_vars(File,In),
 1321	    (prolog_close_source(In),
 1322              current_prolog_flag(xref, Was)))),!.
 1323
 1324
 1325
 1326
 1327
 1328:-export(ensure_vars_labled/2).
 1329
 1330
 1331mfree:attr_unify_hook(This,That):-get_attr(That,eq,Thats),Thats==This,!.
 1332mfree:attr_unify_hook(This,That):-get_attrs(That,Thats),get_attrs(This,Value),Thats==Value,!.
 1333never_bound(V):- var(V),!,put_attr(V,eq,_).
 1334never_bound(_).
 1335
 1336star:attr_unify_hook(This,That):-This==That.
 1337star:attr_unify_hook(_,That):-compound(That),That='$VAR'(Atom),nonvar(Atom).
 1338only_stars(V):- var(V),!,put_attr(V,star,V).
 1339only_stars(_).
 1340
 1341del_each_attr(M,V):-del_attr(V,M).
 1342
 1343%=
 ensure_vars_labled(?I, ?O) is semidet
Ensure Variables Labled.
 1349ensure_vars_labled(I,O):-nonvar(O),!,must(ensure_vars_labled(I,M)),!,M=O.
 1350ensure_vars_labled(I,I):- (t_l:dont_varname;no_vars_needed(I)),!.
 1351% ensure_vars_labled(I,I):- term_variables(I,Vs),maplist(never_bound,Vs),!.
 1352% ensure_vars_labled(I,I):- !.
 1353ensure_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.
 1354ensure_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.
 1355ensure_vars_labled(I,I).
 1356
 1357
 1358:- multifile(user:portray/1).
 1359
 1360%=
 portray(:TermObj) is semidet
Hook To [portray/1] For Module Logicmoo_varnames. Portray.
 1367user:portray(A) :- fail, \+ tracing,
 1368  catch(quietly(((compound(A);var(A)), current_prolog_flag(source_variables, true), set_prolog_flag(source_variables, false),
 1369    call_cleanup((((user:portray(A) -> ! ; print_numbervars_maybe(A)))),set_prolog_flag(source_variables, true)))),E,(writeq(E),nl,fail)).
 1370
 1371
 1372%=
 print_numbervars(?H) is semidet
Print Numbervars.
 1378print_numbervars(H):- (tlbugger:no_slow_io; tracing),!, writeq(H),!.
 1379print_numbervars(H):- must( \+ \+ ((on_x_log_fail(print_numbervars_maybe(H));on_x_log_fail(print_numbervars_1(H))))),!.
 1380
 1381
 1382
 1383%=
 print_numbervars_maybe(?H) is semidet
Print Numbervars Maybe.
 1389print_numbervars_maybe(H):-(compound(H);var(H)), copy_term(H,HC), \+ \+ ((get_clause_vars(H), HC\=@=H, print_numbervars_1(H))),!.
 1390
 1391
 1392%=
 print_numbervars_1(?H) is semidet
print numbervars Secondary Helper.
 1398print_numbervars_1(H):- loop_check(print_numbervars_2(H),format('~N~q.~n',[H])).
 1399
 1400% print_numbervars_2(H):- dtrace,baseKB:portray_one_line_hook(H),!.
 1401
 1402%=
 print_numbervars_2(?H) is semidet
print numbervars Extended Helper.
 1408print_numbervars_2(H):- current_output(S),prolog_listing:portray_clause(S,H,[portrayed(true),singletons(false)]),!.
 1409print_numbervars_2(H):- write_term(H,[portrayed(false)]),nl,!.
 1410
 1411
 1412:- thread_local(t_l:disable_px/0).
 1413
 1414
 1415%=
 term_expansion_save_vars(?HB) is semidet
Term Expansion Save Variables.
 1421term_expansion_save_vars(HB):- \+ ground(HB),  \+ t_l:dont_varname_te,\+ t_l:dont_varname, % \+ current_prolog_flag(xref, true),
 1422   current_predicate(listing_vars_file/0), current_prolog_flag(source_variables,true),
 1423   source_context_module(M),init_varname_stores(M),
 1424   logicmoo_util_with_assertions:locally([t_l:dont_varname_te,t_l:disable_px],try_save_vars(M:HB)),!,fail.
 1425
 1426
 1427without_varname_scan(Goal):-
 1428  locally(current_prolog_flag(source_variables,false),
 1429   locally([ - t_l:dont_varname_te,- t_l:dont_varname],Goal)).
 1430
 1431%=
 maybe_record_scanned_file is semidet
Maybe Record Scanned File.
 1437maybe_record_scanned_file:-ignore((  source_location(F,_), \+ varname_cache:varname_info_file(F), asserta(varname_cache:varname_info_file(F)))).
 1438
 1439
 1440%=
 init_varname_stores(?M) is semidet
Init Varname Storages.
 1446init_varname_stores(_):- !.
 1447% init_varname_stores(M):- M:ensure_loaded(logicmoo_util_with_assertions), M:ensure_loaded(listing_vars).
 1448
 1449
 1450:- initialization(maybe_scan_for_varnames).
 1451% :- maybe_scan_for_varnames.
 1452
 1453
 1454%=
 listing_vars_file is semidet
Logicmoo Util Varnames File.
 1460listing_vars_file.
 1461
 1462
 1463%=
 make_hook(?VALUE1, ?Files) is semidet
Hook To [prolog:make_hook/2] For Module Logicmoo_varnames. Make Hook.
 1470prolog:make_hook(before, Files):-forall(member(File,Files),retractall(varname_cache:varname_info_file(File))).
 1471% prolog:make_hook(after, Files):- forall(member(File,Files),show_call(why,ain00(varname_cache:varname_info_file(File)))).
 1472
 1473%=
 term_expansion(:TermFDecl, ?Clause) is semidet
Hook To [term_expansion/2] For Module Logicmoo_varnames. Term Expansion.
 1480user:term_expansion(HB,_):- current_prolog_flag(source_variables,true),term_expansion_save_vars(HB),fail.
 1481
 1482% :- use_module(library(logicmoo_util_common)).
 1483:- fixup_exports.