1/*  Part of SWI-Prolog
    2    Author:        Douglas R. Miles, Jan Wielemaker
    3    E-mail:        logicmoo@gmail.com, jan@swi-prolog.org
    4    WWW:           http://www.swi-prolog.org http://www.logicmoo.org
    5    Copyright (C): 2015, University of Amsterdam
    6                                    VU University Amsterdam
    7    This program is free software; you can redistribute it and/or
    8    modify it under the terms of the GNU General Public License
    9    as published by the Free Software Foundation; either version 2
   10    of the License, or (at your option) any later version.
   11    This program is distributed in the hope that it will be useful,
   12    but WITHOUT ANY WARRANTY; without even the implied warranty of
   13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   14    GNU General Public License for more details.
   15    You should have received a copy of the GNU General Public
   16    License along with this library; if not, write to the Free Software
   17    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
   18    As a special exception, if you link this library with other files,
   19    compiled with a Free Software compiler, to produce an executable, this
   20    library does not by itself cause the resulting executable to be covered
   21    by the GNU General Public License. This exception does not however
   22    invalidate any other reasons why the executable file might be covered by
   23    the GNU General Public License.
   24*/
   25
   26:- module(subvar_inherit,[subvar_inherit/1]).   27
   28subvar_inherit(Var,Which):-term_variables(Var,Vars),maplist(subvar_inherit1(Which),Vars).
   29subvar_inherit(Var):-term_variables(Var,Vars),maplist(subvar_inherit1,Vars).
   30
   31subvar_inherit1(Var):- (get_attr(Var,subvar_inherit,v(Var,_Already))->true;put_attr(Var,subvar_inherit,v(Var,_NewList))).
   32subvar_inherit1(Which,Var):- 
   33  (get_attr(Var,subvar_inherit,v(Var,Already))->true;Already=[]),
   34   update_list(Which,Already,NewList),put_attr(Var,subvar_inherit,v(Var,NewList)).
   35
   36subvar_inherit:attr_unify_hook(v(Var,NewList),Value):- term_variables(Value,Vars),maplist(subvar_copy_to(Var,NewList),Vars).
   37
   38
   39invert_pn_0(+,-).
   40invert_pn_0(-,+).
   41
   42subvar_copy_to(Var,NewList,Value):- var(NewList),get_attrs(Var,Atts),copy_atts_to(Atts,Value).
   43subvar_copy_to(_,att(M,V,Atts),Value):- copy_atts_to(att(M,V,Atts),Value).
   44subvar_copy_to(Var,[New|List],Value):- !, subvar_copy1_to(Var,[New|List],Value).
   45
   46subvar_copy1_to(Var,[M|List],Value):- !,get_attr(Var,M,V),put_attr(Value,M,V),subvar_copy1_to(Var,List,Value).
   47subvar_copy1_to(_,_,_).
   48
   49copy_atts_to(att(M,V,Atts),Value):-!,put_attr(Value,M,V),copy_atts_to(Atts,Value).
   50copy_atts_to(_,_).
   51
   52update_list(At,In,Out):- InOut=ul(v(M,In)),update_list(+,InOut,M,At),InOut=ul(v(M,Out)).
   53
   54update_list(PN,Var,M,At):-var(At),!,throw(error(instantiation_error, M:update_list(Var,PN:At))).
   55%update_list(PN,Var,user,Atts):-!, update_list(PN,Var,tst,Atts).
   56update_list(PN,Var,M, X+Y):-!, update_list(PN,Var,M, X),update_list(PN,Var,M,+Y).
   57update_list(PN,Var,M, X-Y):-!, update_list(PN,Var,M, X),update_list(PN,Var,M,-Y).
   58update_list(PN,Var,M, +X+Y):-!, update_list(PN,Var,M, +X),update_list(PN,Var,M,+Y).
   59update_list(PN,Var,M, +X-Y):-!, update_list(PN,Var,M, +X),update_list(PN,Var,M,-Y).
   60update_list(PN,Var,M, List):- is_list(List),!,maplist(update_list(PN,Var,M),List).
   61update_list(_, Var,M,  +At):-!, update_list(+,Var,M,At).
   62update_list(PN,Var,M,  -At):- invert_pn_0(PN,NP),!,update_list(NP,Var,M,At).
   63%update_list(PN,Var,_,(M:At)):- \+ meta_handler_name(M), !,update_list(PN,Var,M,At).
   64%update_list(PN,Var,M, Meta):- \+ \+ clause(M:meta_hook(Meta,_,_),_), !, forall(M:meta_hook(Meta,P,A),update_list(PN,Var,M,P=A)).
   65% =(+a,b) -->   +(A=B).
   66update_list(PN,Var,M, Pair):- compound(Pair),Pair=..[P,Arg1,Arg2],listep(P),compound(Arg1),call((Arg1=..List,append(Head,[Last],List),At=..[P,Last,Arg2],append(Head,[At],ListNew),Try=..ListNew,!,update_list(PN,Var,M, Try))).
   67% update_list(PN,Var,_, Hook):-  handler_fbs(+ Hook,Number), Number>0, !,PNHook=..[PN,Hook], update_dlist(Var, PNHook).
   68
   69update_list(PN,Var,M,Pair):- !,
   70  list_to_lst(Pair,Tmpl),
   71 % update_hooks(PN,Var,M,Tmpl),
   72   % list_exist(PN,Tmpl),
   73   exec_list_update(PN,Var,M,Tmpl).
   74
   75
   76
   77exec_list_update(-,Var,M,Tmpl):-
   78   (get_lstr(Var,M,Cur)->
   79     (delete(Cur,Tmpl,Upd),update_lstr(Var,M,Upd)) ;
   80    true).
   81
   82exec_list_update(+,Var,M,At):-
   83   (get_lstr(Var,M,Cur) ->
   84    (functor(At,Tmpl,_),
   85     delete(Cur,Tmpl,Mid), % ord_del_element wont work here because -a(_) stops short of finding a(1).
   86     ord_add_element(Mid,At,Upd),
   87     update_lstr(Var,M,Upd));
   88    update_lstr(Var,M,[At])).
   89
   90get_lstr(InVar,M,Upd):-arg(1,InVar,v(M,Upd)).
   91update_lstr(InVar,M,Upd):- setarg(1,InVar,v(M,Upd)).
   92
   93listep('=').
   94listep(':').
   95listep('-').
   96
   97list_to_lst(Var,Var):-var(Var),!.
   98list_to_lst(N-V,Tmpl):-!,list_to_lst(N=V,Tmpl).
   99list_to_lst(N:V,Tmpl):-!,list_to_lst(N=V,Tmpl).
  100list_to_lst(N=V,Tmpl):-!,assertion(atom(N)),!,Tmpl=..[N,V].
  101list_to_lst(F/A,Tmpl):-!,assertion((atom(F),integer(A))),functor(Tmpl,F,A).
  102list_to_lst(Tmpl,Tmpl)