aboutsummaryrefslogtreecommitdiff
path: root/Tools/CodeGen/KrancGroups.m
blob: d49b21f1f7f46f50b145a0b9957e57fed30a34b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
(* $Id$ *)

(*  Copyright 2004 Sascha Husa, Ian Hinder, Christiane Lechner

    This file is part of Kranc.

    Kranc is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    Kranc is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Kranc; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*)

(****************************************************************************)
(* Manipulate Kranc group structures                                        *)
(****************************************************************************)

BeginPackage["KrancGroups`", 
             {"Kranc`", "Errors`", "MapLookup`"}];

CreateGroup;
groupsFromGFs::usage = "";
variablesInGroup::usage = "";
variablesFromGroups::usage = "";
groupFromName::usage = "";
groupName::usage = "";
renameGroup::usage = "";
qualifyGroupName::usage = "";
qualifyGFName::usage = "";
unqualifiedGroupName::usage = "";
implementationFromGroupName::usage = "";
qualifyGroups::usage = "";
containingGroups::usage = "";
groupVariables::usage = "";
GroupTags::usage = "";
SetGroupVariables;
VerifyGroup;
VerifyGroupName;
SetGroupName;
AddGroupTag;
AddGroupExtra;
GroupTimelevels;
allGroupVariables;
NonevolvedTimelevels;
CheckGroups;
VerifyGroupNames;
VerifyGroups;
EnsureInterfaceTimelevels;
GroupExtras;
DeleteGroupExtra;

Begin["`Private`"];

(* The Group structure is of the following form, but the user should
not assume this. All interaction with Group structures should be
through this file.  This way, we can modify the underlying
representation without other code having to be rewritten.

{name, {vars}, extras...}

The extras can be any of the following:

Tags -> {tag1, tag2, ...}
Timelevels -> x


  *)

(*********************************************************************)
(* The following functions know about the internal form of a Group
   structure*)
(*********************************************************************)

CreateGroup[name_, vars_, extras_] :=
  Module[{g},
    VerifyGroupName[name];
    VerifyList[vars];
    VerifyList[extras];
    g = Join[{name, vars}, extras];
    InfoMessage[InfoFull, "Created group: ", g];
    Return[g]];

AddGroupExtra[group_, extra_] :=
  Append[group, extra];

DefFn[
  DeleteGroupExtra[group_, extra_Symbol] :=
  DeleteCases[group, extra -> _]];

VerifyGroup[group_] :=
  Module[{},
    If[!ListQ[group] || Length[group] < 2 || !StringQ[group[[1]]] || ! ListQ[group[[2]]],
      ThrowError["Not a group definition:", group],
      True]];

VerifyGroupName[groupName_] :=
  If[!StringQ[groupName],
    ThrowError["Not a group name:", groupName],
    True];

GroupTimelevels[g_] :=
  Module[{extras},
    extras = Drop[g, 2];
    lookupDefault[extras, Timelevels, False]];

GroupExtras[g_] :=
  Drop[g, 2];

NonevolvedTimelevels[group_] :=
  Module[{tls = GroupTimelevels[group]},
    If[ tls === False, 1, tls]];



groupName[g_] := First[g];

groupVariables[group_] :=
  group[[2]];

GroupTags[g_] :=
  Module[{extras},
    extras = Drop[g, 2];
    lookupDefault[extras, Tags, {}]];

AddGroupTag[g_, t_] :=
  Module[{extras, tags},
    extras = Drop[g, 2];
    tags = lookupDefault[extras, Tags, {}];
    tags = Join[tags, {t}];
    Join[DeleteCases[g, Tags->_], {Tags -> tags}]];

SetGroupName[g_, n_] :=
  Join[{n}, Drop[g, 1]];

SetGroupVariables[g_, vars_] :=
  Join[{groupName[g], vars}, Drop[g, 2]];

(*********************************************************************)
(* The following functions DO NOT KNOW about the internal form of a Group
   structure*)
(*********************************************************************)


(* Return those group structures which contain any variables in GFs *)
groupsFromGFs[groups_, GFs_] := Module[{inter, check},
  (* Given a group structure, return those variables from it that are in
     GFs *)
  inter[y_] :=  Intersection[groupVariables[y], GFs]; (* The last arg was flattened; why? *)

  (* Check whether the group structure y contains any variables in GFs *)
  check[y_] := TrueQ[Length@inter[y] > 0];

  Select[groups, check]
];

renameGroup[g_, newName_] :=
  SetGroupName[g, newName];

variablesInGroup[name_, groups_] :=
  groupVariables[groupFromName[name, groups]];

variablesFromGroups[groupNames_, groups_] := 
  Flatten[Map[variablesInGroup[#, groups] &, groupNames], 1];

groupFromName[name_, groups_] :=
  Module[{gs},
    gs = Select[groups, groupName[#] === name &];
    If[Length[gs] == 0,
       ThrowError["Cannot find group ", name, "in", groups]];
    If[Length[gs] > 1,
       ThrowError["Group", name, "appears multiple times in", groups]];

    First[gs]];

qualifyGroupName[name_, defaultImp_] :=
  If[StringMatchQ[name, "*::*"],
     name,
     defaultImp <> "::" <> name];


unqualifiedGroupName[name_] :=
  If[StringQ@name && StringMatchQ[ToString@name, "*::*"],
     Module[{colon = First[First[StringPosition[name, ":", 1]]]},
       StringDrop[name, colon + 1]],
     name];

implementationFromGroupName[name_] :=
  If[StringQ@name && StringMatchQ[ToString@name, "*::*"],
     Module[{colon = First[First[StringPosition[name, ":", 1]]]},
       StringDrop[name, colon - 1 - StringLength@name]],
     name];

(* Given a list of group definitions, and a list of group names, and
   an implementation name, return a new list of group definitions
   where the groups specified have had the implemention name added to
   them.  Those groups not in the list of names will have the defImp
   name added to them.  The names should be unqualified.  If the
   groups already have implementations, they are left untouched. *)

qualifyGroups[groups_, names_, imp_, defImp_] :=
  Module[{namedGroups = Select[groups, MemberQ[names, groupName[#]] &],
          otherGroups = Select[groups, ! MemberQ[names, groupName[#]] &],
          renamedNamedGroups, renamedOtherGroups},
    renamedNamedGroups = Map[renameGroup[#, qualifyGroupName[groupName[#], imp]] &, 
                        namedGroups];
    renamedOtherGroups = Map[renameGroup[#, qualifyGroupName[groupName[#], defImp]] &, 
                        otherGroups];

    Join[renamedNamedGroups, renamedOtherGroups]];

(* This is in krancthorns as well *)

containingGroups[vars_, groups_] :=
  Module[{allVars},
    allVars = Apply[Join, Map[groupVariables, groups]];
    Map[If[!MemberQ[allVars, #],
           ThrowError[ToString[#] <> 
                 " is not a member of any of the following groups: ",
                 groups]] &, vars];

    Union[Map[groupName, Select[groups, Intersection[groupVariables[#], vars] != {} &]]]];


qualifyGFName[gfname_, allgroups_, defaultImp_] := 
  If[StringQ@gfname && StringMatchQ[gfname, "*::*"],
     gfname,
     
     Module[{groupName, imp, newGFname},
       groupName = First[containingGroups[{gfname}, allgroups]];
       imp = implementationFromGroupName[qualifyGroupName[groupName, defaultImp]];
       newGFname = imp <> "::" <> ToString[gfname];
       newGFname
]
  ];

allGroupVariables[groups_] :=
  Flatten[Map[groupVariables, groups], 1];

CheckGroups[groups_] :=
  Module[{vs, names},
(*    If[!MatchQ[{_String, {_Symbol ...}, {_Symbol -> _} ...}],
        ThrowError["Groups structure should be of the form {name, {vars, ...}, extras}"]]; *)

    vs = Map[ToString, Union[Flatten[Map[groupVariables, groups]]]];
    names = Map[groupName, groups];

    If[(int = Intersection[vs,names]) =!= {},
      ThrowError["Variable names and group names must be distinct.  Group names which are also variable names:", int]];
  ];

VerifyGroups[gs_] := 
  If[!ListQ[gs],
   ThrowError["Not a list of group definitions: ", gs],
   Map[VerifyGroup, gs]];

VerifyGroupNames[gns_] := 
  If[!ListQ[gns],
   ThrowError["Not a list of group names: ", gns],
   Map[VerifyGroupName, gns]];

EnsureInterfaceTimelevels[g_, n_] :=
  Module[
    {tls, g2},
    tls = lookup[GroupExtras[g], InterfaceTimelevels, False];
    If[tls === False,
       AddGroupExtra[g, InterfaceTimelevels -> n],
       (* else *)
       g /. {(InterfaceTimelevels -> x_) :> (InterfaceTimelevels -> Max[tls, n])}]];

End[];

EndPackage[];