1/*  Logicmoo Debug Tools
    2% ===================================================================
    3% File 'logicmoo_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: 'logicmoo_util_varnames.pl' 1.0.0
    8% Revision: $Revision: 1.1 $
    9% Revised At:  $Date: 2002/07/11 21:57:28 $
   10% ===================================================================
   11*/
   12% NEW
   13:- module(attvar_serializer, [
   14          deserialize_attvars/2,deserialize_attvars/3,
   15          serialize_attvars/2,serialize_attvars_now/2,
   16          put_dyn_attrs/2,
   17          find_or_create_var/3,
   18          verbatum_var/1,
   19          holds_attrs/1,
   20          system_expanded_attvars/4,
   21          is_term_expanding_in_file/2,
   22          system_expanded_attvars/2]).   23
   24:- set_module(class(library)).   25
   26:- module_transparent((deserialize_attvars/2,deserialize_attvars/3,
   27          serialize_attvars_now/2,
   28          put_dyn_attrs/2,
   29          find_or_create_var/3,
   30          system_expanded_attvars/4,
   31          system_expanded_attvars/2)).   32
   33
   34:- multifile(lmcache:use_attvar_expander/1).   35:- dynamic(lmcache:use_attvar_expander/1).   36
   37:- multifile(lmcache:never_use_attvar_expander/1).   38:- dynamic(lmcache:never_use_attvar_expander/1).   39
   40lmcache:never_use_attvar_expander(attvar_serializer).
   41
   42:- use_module(library(gvar_lib)).   43%:- reexport(library(hook_database)).
   44%:- reexport(library(listing_vars)).
   45
   46:- if( \+ prolog_load_context(reload, true)).   47:- multifile(baseKB:mpred_is_impl_file/1).   48:- dynamic(baseKB:mpred_is_impl_file/1).   49:- prolog_load_context(file,F),call(assert,baseKB:mpred_is_impl_file(F)).   50:- endif.   51
   52% use this module to avoid some later conflicts
   53%:- if(exists_source(library(base32))).
   54%:- use_module(library(base32)).
   55%:- endif.
   56
   57find_or_create_var(_Vs,N,V):-var(N),variable_name(N,_Name),N=V.
   58find_or_create_var(Vs,N,V):-var(N),!,find_or_create_named_var(Vs,N,V).
   59find_or_create_var(Vs,L,V):-is_list(L),!,member(VN=N,L),VN=vn,!,find_or_create_named_var(Vs,N,V).
   60find_or_create_var(Vs,'$VAR'(A),V):-
   61   (atom(A)->find_or_create_named_var(Vs,A,V);
   62   (atomic(A)-> (format(atom(N),'~w',[A]),find_or_create_named_var(Vs,N,V));
   63   find_or_create_named_var(Vs,A,V))).
   64find_or_create_var(Vs,N,V):-!,find_or_create_named_var(Vs,N,V).
   65
   66find_named_var(member,Vs,N,V):-member(NN=VV,Vs),V==VV,!,must(NN=N).
   67find_named_var(member,Vs,N,V):-member(NN=VV,Vs),NN==N,!,must(VV=V).
   68find_named_var(variable_name,_Vs,N,V):- var(V),variable_name(V,Name),N==Name.
   69find_named_var(get_varname_list,_Vs,N,V):-get_varname_list(VsE),find_named_var(member,VsE,N,V).
   70% find_named_var(vnn(Name,How),Vs,N,V):- var(N),variable_name(N,Name),!,find_named_var(How,Vs,Name,V).
   71
   72put_into_list(Vs,N,V):- Vs = [_|VT], nb_setarg(2,Vs,[N=V|VT]).
   73
   74find_or_create_named_var(Vs,N,V):-  find_named_var(How,Vs,N,V),
   75   (How==member -> true; put_into_list(Vs,N,V)).
   76   % (member(localvs,Vs)->put_attr(V,vn,N);true),!.
   77find_or_create_named_var(Vs,N,V):- var(N),variable_name(N,Name), put_into_list(Vs,N,V), put_into_list(Vs,Name,V),!.
   78find_or_create_named_var(Vs,N,V):- variable_name(V,Name), put_into_list(Vs,N,V), put_into_list(Vs,Name,V),!.
   79find_or_create_named_var(Vs,N,V):- put_into_list(Vs,N,V),!,(member('$variable_names',Vs)->quietly(add_var_to_env(N,V);true)).
   80
   81
   82deserialize_attvars(V,O):- \+compound(V),!,O=V.
   83deserialize_attvars(V,O):- get_varname_list(Vs),!,loop_check(deserialize_attvars([localvs,'$variable_names'|Vs], V,O)),!.
   84
   85deserialize_attvars(_Vs, V,O):- cyclic_term(V),!,O=V.
   86deserialize_attvars(Vs, V,O):-  var(V), get_var_name(V,N),!,find_or_create_var(Vs,N,V),!,V=O.
   87deserialize_attvars(_Vs, V,O):- \+ compound(V),!,O=V.
   88deserialize_attvars(_ ,(H:-BI),O):- fail, split_attrs(BI,AV,BO),AV\==true,AV\=bad:_,term_attvars((H:-BO),[]),must(call(AV)),!,(BO==true->(O=H);O=(H:-BO)).
   89deserialize_attvars(Vs,Dict,V):- is_dict(Dict,M),!,dict_pairs(Dict,M,Pairs),
   90   (atom(M)->find_or_create_var(Vs,V,M);V=M),
   91   put_dyn_attrs_1(V,Pairs).
   92deserialize_attvars(Vs,'$VAR'(N),V):- !, find_or_create_var(Vs,'$VAR'(N),V),put_dyn_attrs(V,N).
   93deserialize_attvars(Vs,'avar'(N),V):- !, find_or_create_var(Vs,N,V),put_dyn_attrs(V,N).
   94deserialize_attvars(Vs,'avar'(N,S),V):- !, find_or_create_var(Vs,N,V),put_dyn_attrs(V,N),put_dyn_attrs(V,S),!.
   95deserialize_attvars(Vs,C,A):- compound_name_arguments(C,F,Args),
   96  maplist(deserialize_attvars(Vs),Args,OArgs),compound_name_arguments(A,F,OArgs).
   97
   98
   99:- meta_predicate put_dyn_attrs(?,*).  100put_dyn_attrs(V,S):- V==S,!.
  101put_dyn_attrs(V,S):- attvar(S),get_attrs(S,SS),!,put_dyn_attrs(V,SS),!.
  102put_dyn_attrs(_,S):- \+ compound(S),!.
  103put_dyn_attrs(V,S):- S=att(_,_,_), \+ attvar(V),!, put_attrs(V,S).
  104put_dyn_attrs(V,S):- put_dyn_attrs_1(V,S),!.
  105put_dyn_attrs(V,S):- trace_or_throw(bad_put_dyn_attrs(V,S)),!.
  106
  107:- meta_predicate put_dyn_attrs_1(?,*).  108put_dyn_attrs_1(_V,NC):- \+ compound(NC),!.
  109put_dyn_attrs_1(V,att(N,V,Rest)):- !, put_attr(V,N,V),put_dyn_attrs_1(V,Rest).
  110put_dyn_attrs_1(V,M=AV):- atom(M), ensure_attr_setup(M),!, must(put_attr(V,M,AV)).
  111put_dyn_attrs_1(V,M-AV):- ensure_attr_setup(M),!, must(put_attr(V,M,AV)).
  112put_dyn_attrs_1(V,M:AV):- atom(M),!, M:put_dyn_attrs_1(V,AV).
  113put_dyn_attrs_1(V,[H|T]):- !, put_dyn_attrs(V,H),put_dyn_attrs_1(V,T),!.
  114put_dyn_attrs_1(V,S):- is_dict(S),dict_keys(S,Keys),!,put_dyn_attrs_1(V,Keys).
  115put_dyn_attrs_1(_V,MAV):- must(MAV),!.
  116
  117ensure_attr_setup(M):- atom(M),current_predicate(attribute_goals,M:attribute_goals(_,_,_)),!.
  118ensure_attr_setup(M):- atom(M),assert_if_new((M:attribute_goals(V,[put_attr(V,M,A)|R],R):- get_attr(V, M,A))).
  119
  120is_term_expanding_in_file(I,_):- var(I),!,fail.
  121is_term_expanding_in_file(I,Type):- source_file(_,_),nb_current('$term',CT),(CT==I->Type=term;((CT=(_:-B),B==I,Type=goal))).
  122
  123system_expanded_attvars(M:goal,P,I,O):- var(P),
  124   \+ is_term_expanding_in_file(I,_),
  125   \+ lmcache:never_use_attvar_expander(M),
  126   prolog_load_context(module,LC),
  127   \+ lmcache:never_use_attvar_expander(LC),
  128   current_prolog_flag(read_attvars,true), 
  129   \+ current_prolog_flag(xref,true), 
  130   system_expanded_attvars(I,O),
  131   wdmsg(goal_xform(I --> O)).
  132
  133system_expanded_attvars(M:term,P,I,CO):- nonvar(P), compound(I), I\= (:-(_)),
  134   \+ lmcache:never_use_attvar_expander(M),
  135   current_prolog_flag(read_attvars,true), 
  136   \+ current_prolog_flag(xref,true), 
  137   is_term_expanding_in_file(I,term),
  138   prolog_load_context(module,LC),
  139   \+ lmcache:never_use_attvar_expander(LC),
  140   (prolog_load_context(source,LC1)-> (\+ lmcache:never_use_attvar_expander(LC1)) ; true),
  141   system_expanded_attvars(I,O),
  142   clausify_attributes(O,CO),
  143   wdmsg(term_xform(I --> CO)),
  144   % update what we just read 
  145   b_setval('$term',CO).
  146
  147:- user:dynamic(expand_query/4).  148:- user:multifile(expand_query/4).  149   
  150free_of_attvars(Term):- term_attvars(Term,Vs),!,Vs==[].
  151
  152free_of_attrs(Term):- var(Term),!,(get_attrs(Term,Attrs)-> Attrs==[] ; true).
  153free_of_attrs(Term):- term_attvars(Term,Vs),!,(Vs==[]->true;maplist(free_of_attrs,Vs)).
 serialize_attvars(+AttvarTerm, -PrintableTerm) is semidet
serialize attributed variables (this is for printing and term_expansions currently)
  160serialize_attvars(I,O):- serialize_attvars_now(I,O),
  161  sanity(ignore(show_failure(free_of_attvars(O)))),
  162  sanity(show_failure(free_of_attrs(O))).
  163
  164serialize_attvars_now(V,S):- var(V),must(serialize_1v(V,S)),!.
  165serialize_attvars_now(I,O):- \+ compound(I),!,O=I.
  166serialize_attvars_now(C,A):- compound_name_arguments(C,F,Args),maplist(serialize_attvars_now,Args,OArgs),compound_name_arguments(A,F,OArgs).
  167
  168
  169serialize_attvar_term(I,O):- copy_term(I,O), serialize_attvar_term_now(O),
  170   sanity(ignore(show_failure(free_of_attvars(O)))),
  171   sanity(show_failure(free_of_attrs(O))).
  172
  173% ?- 
  174% rtrace(( X = hi(T),put_attr(T,sk,foo),serialize_attvar_term(X,SS), sanity(\+  has_skolem_attrvars(SS)))).
  175
  176
  177serialize_attvar_term_now(V):- attvar(V),trace_or_throw(serialize_attvar_term(V)).
  178serialize_attvar_term_now(I):- \+ compound(I),!.
  179serialize_attvar_term_now(C):- functor(C,_,A),serialize_attvar_term_now(A,C).
  180
  181serialize_attvar_term_now(0,_):-!.
  182serialize_attvar_term_now(A,C):- arg(A,C,E),serialize_attvar_term_now(A,C,E),Am1 is A-1,serialize_attvar_term_now(Am1,C).
  183
  184serialize_attvar_term_now(A,C,E):- attvar(E),!,get_put_attr_serial(E,New),setarg(A,C,New),!.
  185serialize_attvar_term_now(_,C,E):- compound(E)->serialize_attvar_term_now(C);true.
  186
  187get_put_attr_serial(E,New):- get_attr(E,'$$sv$$',New),!.
  188get_put_attr_serial(E,Next):- serialize_3(E,Name,Atts),MyCopy =_, subst(Atts,E,MyCopy,NewAtts),Next=avar(Name,att('$linkval',MyCopy,NewAtts)),del_attrs(E),put_attr(E,'$$sv$$',Next),!.
  189
  190serialize_1v(V,'$VAR'(Name)):- get_attrs(V, att(vn, Name, [])),!.
  191serialize_1v(V,avar('$VAR'(N),SO)):- get_attrs(V, S),variable_name_or_ref(V,N),put_attrs(TEMP,S),
  192   del_attr(TEMP,vn),!,remove_attrs(TEMP, SO),!.
  193serialize_1v(V,'$VAR'(N)):-  variable_name_or_ref(V,N).
  194serialize_1v(V,avar(S)):- remove_attrs(V, S),!.
  195serialize_1v(V,V).
  196
  197
  198serialize_3(V,N,Atts):- (get_var_name(V,N);variable_name_or_ref(V,N)),del_attr(V,vn),(get_attrs(V, Atts)->true;Atts=[]),!.
  199
  200
  201remove_attrs(Var,Attrs):-get_attrs(Var,Attrs),!,remove_all_attrs(Var).
  202remove_attrs(Var,Attrs):-copy_term(Var,Copy,Attrs),Copy=Var,remove_all_attrs(Var).
  203
  204% ?- rtrace(( X = hi(T),put_attr(T,sk,foo),serialize_attvar_term(X,SS), \+  has_skolem_attrvars(SS))).
  205% ?- X = hi(T),put_attr(T,sk,foo),rtrace(serialize_attvars(X,SS)), \+  has_skolem_attrvars(SS).
  206
  207
  208% ?- X = hi(T),put_attr(T,sk,foo),remove_all_attrs(T).
  209
  210remove_all_attrs(Var):- attvar(Var),del_attrs(Var), sanity( \+ attvar(Var)),!.
  211remove_all_attrs(Term):- term_attvars(Term,Vars),maplist(remove_all_attvrs,Vars).
 verbatum_term(TermT) is semidet
System Goal Expansion Sd.
  217verbatum_term(I):- attvar(I),!,fail.
  218verbatum_term(I):- \+ compound(I),!. % this is intended to include the non-attrbuted variables
  219verbatum_term('$was_imported_kb_content$'(_,_)).
  220verbatum_term('varname_info'(_,_,_,_)).
  221verbatum_term(V):-verbatum_var(V).
  222
  223holds_attrs(V):-var(V),!.
  224holds_attrs(V):-verbatum_var(V),!.
  225
  226
  227% move to logicmoo_utils_common.pl? 
  228verbatum_var(Var):-var(Var),!,fail.
  229verbatum_var('$VAR'(_)).
  230verbatum_var('avar'(_)).
  231verbatum_var('avar'(_,_)).
 system_expanded_attvars(:TermT, :TermARG2) is semidet
System Goal Expansion Sd.
  241system_expanded_attvars(I,O):- (var(I);compound(I)),!,loop_check((deserialize_attvars(I,O))),O\=@=I,!.
  242
  243:- fixup_exports.