
% Ensure all template dimensions appear as targets of index mappings
% or embeddings.

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

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

% !!!! Record relevant syntax of symbol table !!!!

% ``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(<idx_mapping_desc>),
%                                 List(<idx_mapping_desc>),
%                                 List(<embedding_desc>))
%
%  <template>          ::= def | <var_handle>
%
%  <idx_mapping_desc>  ::= <map> :: <idx_domain> -> <dimension>
%
%  <idx_domain>        ::= <idx_handle> : <exp_handle>  |
%                          <exp_handle>
%
%  <map>               ::= map(<exp_handle>, <exp_handle>)  |
%                          id                               |
%                          any
%
%  <embedding_desc>    ::=  emb(<exp_handle>) :: <dimension>  |
%                           rep :: <dimension>


mapSurjectExpressionHandleList([], _).
mapSurjectExpressionHandleList([EH | EHs], Syms) :-
    mapSurjectExpressionHandle(EH, Syms),
    mapSurjectExpressionHandleList(EHs, Syms).

mapSurjectExpressionHandle(EH, Syms) :-
    symLookUp(EH, expression(E, Atr), Syms),
    mapSurjectExpression(E, Atr, Syms).

mapSurjectExpression(varRef(_), Atr, Syms) :-
    expAtr(Atr),
      CtxMap := Atr -> ctxMap,  LocMap := Atr -> locMap,
    mapSurject(CtxMap, Syms),
    mapSurject(LocMap, Syms).
mapSurjectExpression(funRef(_, _, _), Atr, Syms) :-
    expAtr(Atr),
      EHs    := Atr -> mapDaughts,
      CtxMap := Atr -> ctxMap,  LocMap := Atr -> locMap,
    mapSurject(CtxMap, Syms),
    mapSurject(LocMap, Syms),
    mapSurjectExpressionHandleList(EHs, Syms).
mapSurjectExpression(subscript(_, _), Atr, Syms) :-
    expAtr(Atr),
      EHs    := Atr -> mapDaughts,
      CtxMap := Atr -> ctxMap,      LocMap := Atr -> locMap,
    mapSurject(CtxMap, Syms),
    mapSurject(LocMap, Syms),
    mapSurjectExpressionHandleList(EHs, Syms).
mapSurjectExpression(forall(_, Shape, _, EHs), _, Syms) :-
    mapSurjectExpressionHandleList(Shape, Syms),
    mapSurjectExpressionHandleList(EH, Syms).
mapSurjectExpression(arrall(_, Shape, EH), _, Syms) :-
    mapSurjectExpressionHandleList(Shape, Syms),
    mapSurjectExpressionHandleList(EH, Syms).
mapSurjectExpression(idxRef(SizeH, IH), Atr, Syms) :-
    mapSurjectExpressionHandle(SizeH, Syms).
mapSurjectExpression(size(_, EH), Atr, Syms) :-
    expAtr(Atr),
      CtxMap := Atr -> ctxMap,      LocMap := Atr -> locMap,
    mapSurject(CtxMap, Syms),
    mapSurject(LocMap, Syms),
    mapSurjectExpressionHandle(SizeH, Syms).
mapSurjectExpression(expRef(_), _, _).
mapSurjectExpression(E, _, _) :- isLiteral(E).


% ----- `mapSurject' -----

mapSurject(Map, Syms) :-
    getTemplate(T, Map),
    getDimSpace(_, Dims, T, Syms),
    getAIMap(AIMap, Map),
    getIMap(IdxMap, Map),
    getEmbedding(Embed, Map),
    areTargetted(Dims, AIMap, IdxMap, Embed),
    isClosedList(Embed).


areTargetted([Dim | Dims], AIMap, IdxMap, Embed) :-
    isTargetted(Dim, AIMap, IdxMap, Embed),
    areTargetted(Dims, AIMap, IdxMap, Embed).

isTargetted(Dim, AIMap, _, _)  :- member(_ :: _ -> Dim, AIMap),  !.
isTargetted(Dim, _, IdxMap, _) :- member(_ :: _ -> Dim, IdxMap), !.
isTargetted(Dim, _, _, Embed)  :- member(_ :: Dim, Embed), !.


isClosedList([])      :- !.
isClosedList([_ | L]) :- isClosedList(L).

