
% Instantiate preferred embeddings at subscription nodes, and do
% embedding propagation.

% ---------------------------------------------------------------------
 
% $Id$
 
% $Log$

% ---------------------------------------------------------------------

% ``meta-syntax'' is
%
%  <expression>  ::=
%    expRef(<exp_handle>)                                            |
%    idxRef(<exp_handle>, <idx_handle>)                              |
%    varRef(<var_handle>)                                            |
%    funRef(<fun_handle>, <tag>, List(<exp_handle>))                 |
%    subscript(<exp_handle>, List(<exp_handle>))                     |
%    arrall(List(<idx_handle>), List(<exp_handle>), <exp_handle>)    |
%    forall(List(<idx_handle>), List(<exp_handle>), List(<exp_handle>),
%           List(<exp_handle>))                                      |
%    size(<literal>, <exp_handle>)                                   |
%    literal(<literal>)                                              |
%    true                                                            |
%    false                                                           |
%    any
%
%  <index_declaration> ::= index(<range>, <idx_attr>)
%
%  <tag>         ::= <predef> | user
%
%  <predef>      ::= ...

% Attribute meta-syntax...
%
%  <fun_attr> ::= syms         :: <sym_tab>               #
%                 declarations :: List(<var_handle> |
%                                      <fun_handle> |
%                                      <exp_handle>)      #
%                 statements   :: List(<exp_handle>)      #
%                 dummies      :: List(<var_handle>)      #
%                 result       :: <var_handle>            #
%                 class        :: elemental | array       #
%                 dumShps      :: List(<extern_shape>)    #
%                 resShp       :: <extern_shape>          #
%                 dumMaps      :: List(<extern_mapping>)  #
%                 resMap       :: <extern_mapping>        #
%                 dumAlnGrps   :: List(List(<literal>))   #
%                 resAlnGrp    :: List(<literal>)
%
%  <var_attr> ::= shape    :: List(<exp_handle>)         #
%                 ishape   :: List(<exp_handle>)         #
%                 mapDesc  :: <var_mapping_desc>         #
%                 imapDesc :: <var_mapping_desc>
%
%  <exp_attr> ::= shape :: List(<range>)

% Syntax of mapping descriptors
%
%
%  <exp_mapping_desc>  ::=  align(<template>,
%                                 List(<arr_idx_mapping_desc),
%                                 List(<idx_mapping_desc),
%                                 List(<embedding_desc>))
%
%  <arr_idx_mapping_desc>  ::=  <map> :: <dimension>
%
%  <template>          ::= def | <var_handle>
%
%  <idx_mapping_desc>  ::= <map> :: <idx_handle> -> <dimension>
%
%  <map>               ::= map(<exp_handle>, <exp_handle>)  |
%                          id                               |
%                          any
%
%  <embedding_desc>    ::=  emb(<exp_handle>) :: <dimension>  |
%                           rep :: <dimension>


embedSubsExpressionHandleList([], _).
embedSubsExpressionHandleList([EH | EHs], Syms) :-
    embedSubsExpressionHandle(EH, Syms),
    embedSubsExpressionHandleList(EHs, Syms).

embedSubsExpressionHandle(EH, Syms) :-
    symLookUp(EH, expression(E, Atr), Syms),
    embedSubsExpression(E, Atr, Syms).

embedSubsExpression(varRef(_), Atr, Syme) :-
    expAtr(Atr),
      UnifiedIM := Atr -> unifiedIM,
      CtxMap    := Atr -> ctxMap,  LocMap := Atr -> locMap,
    embedProp(CtxMap, LocMap, UnifiedIM).
embedSubsExpression(funRef(_, _, _), Atr, Syms) :-
    expAtr(Atr),
      UnifiedIM := Atr -> unifiedIM,
      EHs       := Atr -> mapDaughts,
      CtxMap    := Atr -> ctxMap,  LocMap := Atr -> locMap,
    embedSubsExpressionHandleList(EHs, Syms),
    embedProp(CtxMap, LocMap, UnifiedIM).
embedSubsExpression(subscript(_, _), Atr, Syms) :-
    expAtr(Atr),
      false  := Atr -> locRefMap,   UnifiedIM := Atr -> unifiedIM,
      EHs    := Atr -> mapDaughts,
      CtxMap := Atr -> ctxMap,      LocMap := Atr -> locMap,
    embedSubsExpressionHandleList(EHs, Syms),
    embedProp(CtxMap, LocMap, UnifiedIM).
embedSubsExpression(subscript(ArrH, SubHs), Atr, Syms) :-
    expAtr(Atr),
      true   := Atr -> locRefMap,   UnifiedIM := Atr -> unifiedIM,
      EHs    := Atr -> mapDaughts,
      CtxMap := Atr -> ctxMap,      LocMap := Atr -> locMap,
    expGetCtxMap(ArrMap, ArrH, Syms),
    embedSubs(LocMap, SubHs, ArrMap, Syms),
    embedSubsExpressionHandleList(EHs, Syms),
    embedProp(CtxMap, LocMap, UnifiedIM).
embedSubsExpression(forall(_, Shape, _, EHs), _, Syms) :-
    embedSubsExpressionHandleList(Shape, Syms),
    embedSubsExpressionHandleList(EH, Syms).
embedSubsExpression(arrall(_, Shape, EH), _, Syms) :-
    embedSubsExpressionHandleList(Shape, Syms),
    embedSubsExpressionHandleList(EH, Syms).
embedSubsExpression(idxRef(SizeH, IH), Atr, Syms) :-
    embedSubsExpressionHandle(SizeH, Syms).
embedSubsExpression(size(_, EH), Atr, Syms) :-
    expAtr(Atr),
      CtxMap    := Atr -> ctxMap,      LocMap := Atr -> locMap,
      UnifiedIM := Atr -> unifiedIM,
    embedSubsExpressionHandle(SizeH, Syms),
    embedProp(CtxMap, LocMap, UnifiedIM).
embedSubsExpression(expRef(_), _, _).
embedSubsExpression(E, _, _) :- isLiteral(E).


% ----- Embed linear subscript expressions -----


embedSubs(Map, SubHs, ArrMap, Syms) :-
    getEmbedding(Embed, Map),
    getEmbedding(ArrEmbed, ArrMap),
    getAIMap(ArrAIMap, ArrMap),
    embedLinSubs(Embed, SubHs, ArrAIMap, ArrEmbed, Syms).


embedLinSubs([], _, _, _, _).
embedLinSubs([Embedding | Embeddings], SubHs, ArrAIMap, ArrEmbed, Syms) :-
    embedLinSub(Embedding, SubHs, ArrAIMap, ArrEmbed, Syms),
    embedLinSubs(Embeddings, SubHs, ArrAIMap, ArrEmbed, Syms).

embedLinSub(Embedding, _, _, ArrEmbed, _) :-
    member(Embedding, ArrEmbed),
    !. 
embedLinSub(emb(EH) -> Dim, SubHs, ArrAIMap, _, Syms) :-
    member(M :: _ -> Dim, ArrAIMap),
    !,
    isNth(Dim, SubHs, SubH),
    expGetDepen([], _, SubH, Syms),  % Subscript is ``scalar'' expression
    applyLin(EH, M, SubH, Syms),
embedLinSub(_, _, _, _, _).


% ----- Propagation of embeddings -----


embedProp(CtxMap, LocMap, true) :-
    getEmbedding(LocEmbed, LocMap),
    getEmbedding(CtxEmbed, CtxMap),
    unifyEmbed(LocEmbed, CtxEmbed). 
embedProp(_, _, false).

unifyEmbed([], _).
unifyEmbed([Embedding | Embeddings], CtxEmbed) :-
    unifyEmbedding(Embedding, CtxEmbed),
    unifyEmbed(Embeddings, CtxEmbed).

unifyEmbedding(Embedding, CtxEmbed) :-
    member(Embedding, CtxEmbed),
    !.
unifyEmbedding(_, _).


