-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathsetSubstitutionSystem$wl.m
389 lines (347 loc) · 15.7 KB
/
setSubstitutionSystem$wl.m
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
Package["SetReplace`"]
PackageImport["GeneralUtilities`"]
PackageScope["setSubstitutionSystem$wl"]
(* This is the implementation of setSubstitutionSystem in Wolfram Language. Works better with larger vertex degrees,
but is otherwise much slower. Supports arbitrary pattern rules with conditions. Does not support multiway systems. *)
(* We are going to transform set substitution rules into a list of n! normal rules, where elements of the input subset
are arranged in every possible order with blank null sequences in between. *)
allLeftHandSidePermutations[input_Condition :> output_List] := ModuleScope[
inputLength = Length @ input[[1]];
inputPermutations = Permutations @ input[[1]];
heldOutput = Thread @ Hold @ output;
With[{right = heldOutput, condition = input[[2]]}, (* condition is already held before it's passed here *)
# /; condition :> right & /@ inputPermutations] /. Hold[expr_] :> expr
];
(* Now, if there are new vertices that need to be created, we will disassemble the Module remembering which variables
it applies to, and then reassemble it for the output. *)
allLeftHandSidePermutations[input_Condition :> output_Module] := ModuleScope[
ruleInputOriginal = input[[1]];
ruleCondition = heldPart[input, 2];
heldModule = mapHold[output, {0, 1}];
moduleInputContents = heldModule[[1, 2]];
With[{ruleInputFinal = #[[1]],
moduleArguments = heldModule[[1, 1]],
moduleOutputContents = (Hold /@ #)[[2]]},
ruleInputFinal :> Module[moduleArguments, moduleOutputContents]
] & /@ allLeftHandSidePermutations[
ruleInputOriginal /; ruleCondition :> Evaluate @ moduleInputContents /.
Hold[expr_] :> expr] //.
Hold[expr_] :> expr
];
(* toNormalRules turns set substitution rules into normal Wolfram Language rules for use in, i.e., Replace. *)
(* If there are multiple rules, we want to turn them into one because we need to maintain a specific
(oldest-expressions-first) evolution order. *)
(* The idea is to find the shortest matching sequence from the beginning of the set using Shortest of Alternatives
among all rules arranged in all possible orders. *)
(* For example, the two rules
{{{a_, b_}} /; True :> Module[{c}, {{a, c}, {c, b}}],
{{a_, b_}, {b_, c_}} /; True :> Module[{}, {{a, c}}]}
would turn into something like
{match : Shortest[(rule1 : PatternSequence[untouched1___, {a_, b_}] /; True) |
(rule2 : PatternSequence[untouched1___, {a_, b_}, untouched2___, {b_, c_}] /; True) |
(rule2 : PatternSequence[untouched1___, {b_, c_}, untouched2___, {a_, b_}] /; True)],
untouched3___} :>
Catenate[{{untouched1, untouched2, untouched3},
Replace[{rule1}, {match} :> Module[{c}, {{a, c}, {c, b}}]],
Replace[{rule2}, {match} :> Module[{}, {{a, c}}]]}] *)
(* untouched are the expressions that were not used in this event. Note that the Replace[...] arguments of Catenate
effectively choose which rule should be used because all but one of the rule patterns will be empty sequences. *)
toNormalRules[rules_List] := ModuleScope[
ruleNames = Table[Unique["rule", {Temporary}], Length[rules]];
separateNormalRules = allLeftHandSidePermutations /@ rules;
longestRuleLength = Max[Map[Length, separateNormalRules[[All, All, 1, 1]], {2}]];
untouchedNames = Table[Unique["untouched", {Temporary}], longestRuleLength + 1];
finalMatchName = Unique["match", {Temporary}];
input = With[{match = finalMatchName}, List[
match : Shortest[Alternatives @@ Catenate[Transpose @ PadRight[
MapIndexed[
With[{patternName = ruleNames[[#2[[1]]]]},
Function[{patternContent, condition},
inertCondition[patternName : patternContent, condition] /. inertCondition -> Condition] @@@ #] &,
Map[
With[{input = #[[1]], condition = heldPart[#, 2]},
PatternSequence @@ If[input == {}, input, Riffle[
input,
Pattern[#, ___] & /@ untouchedNames,
{1, 2 Length[input] - 1, 2}]] /; condition
] &,
separateNormalRules[[All, All, 1]],
{2}]],
Automatic,
nothing] /. nothing -> Nothing]],
With[{lastPatternName = Last @ untouchedNames}, Pattern[lastPatternName, ___]]]];
output = Hold @ Catenate @ # & @ Prepend[
With[{ruleName = #[[1]], outputRule = #[[2]]},
Hold[Replace[{ruleName}, outputRule]]] & /@
Transpose[{
ruleNames,
With[{outputExpression = (Hold /@ #)[[2]]},
{finalMatchName} :> outputExpression] & /@
separateNormalRules[[All, 1]]}],
untouchedNames];
With[{evaluatedOutput = output}, input :> evaluatedOutput] //. Hold[expr_] :> expr
];
(* This function just does the replacements, but it does not keep track of any metadata (generations and events).
Returns {finalState, terminationReason}, and sows deleted expressions. *)
setReplace$wl[set_, rules_, stepSpec_, vertexIndex_, returnOnAbortQ_, timeConstraint_] := ModuleScope[
eventsCount = 0;
normalRules = toNormalRules @ rules;
previousResult = set;
Catch[
TimeConstrained[
CheckAbort[
{FixedPoint[
AbortProtect[Module[{newResult, deletedExpressions, events},
If[eventsCount++ == Lookup[stepSpec, $maxEvents, Infinity],
Throw[{previousResult, $maxEvents}, $$setReplaceResult];
];
{newResult, {deletedExpressions, events}} = Reap[
Catch[
Replace[#, normalRules]
,
$$reachedAtomDegreeLimit
,
Throw[{previousResult, $maxFinalVertexDegree}, $$setReplaceResult] &
],
{$$deletedExpressions, $$events}];
If[vertexCount[vertexIndex] > Lookup[stepSpec, $maxFinalVertices, Infinity],
Throw[{previousResult, $maxFinalVertices}, $$setReplaceResult];
];
If[Length[newResult] > Lookup[stepSpec, $maxFinalExpressions, Infinity],
Throw[{previousResult, $maxFinalExpressions}, $$setReplaceResult];
];
Map[Sow[#, $$deletedExpressions] &, deletedExpressions, {2}];
Map[Sow[#, $$events] &, events, {2}];
previousResult = newResult]] &,
List @@ set], $fixedPoint},
If[returnOnAbortQ,
{previousResult, $Aborted}
,
Abort[]
]],
timeConstraint,
If[returnOnAbortQ,
{previousResult, $timeConstraint}
,
Return[$Aborted]
]]
,
$$setReplaceResult
]
];
(* We need to not only perform the evolution, but also keep track of the metadata such as generation numbers, creator
events, etc. The way we do it is by implementing the metadata tracking itself as pattern rules.
Each expression becomes the list {id, generation, original expression (atoms)} *)
(* Then a rule
{{a_, b_}, {b_, c_}} /; True :> Module[{d}, {{a, d}, {d, b}}]
becomes something like
{{id1_, generation1_ ? (#1 < maxGeneration &), inputExpression1 : {a_, b_}},
{id2_, generation2_ ? (#1 < maxGeneration &), inputExpression2 : {b_, c_}}} /; True :>
Module[{
d, newExpressionIDs = Table[getNextExpression[], 2]},
{Sow[{ruleID, {id1, id2} -> newExpressionIDs, 1 + Max[0, generation1, generation2]}, $$events]; Nothing,
Sow[{id1, generation1, inputExpression1}, $$deletedExpressions];
deleteFromVertexIndex[vertexIndex, inputExpression1]; Nothing,
Sow[{id2, generation2, inputExpression2}, $$deletedExpressions];
deleteFromVertexIndex[vertexIndex, inputExpression2]; Nothing,
{newExpressionIDs[[1]],
1 + Max[0, generation1, generation2],
addToVertexIndex[vertexIndex, {a, d}, maxVertexDegree]},
{newExpressionIDs[[2]],
1 + Max[0, generation1, generation2],
addToVertexIndex[vertexIndex, {d, b}, maxVertexDegree]}}] *)
(* This function adds metadata management to the rules. I.e., the rules will not only perform the replacements, but will
also keep track of generations, events, etc. *)
addMetadataManagement[
input_Condition :> output_Module,
ruleID_,
getNextExpression_,
maxGeneration_,
maxVertexDegree_,
vertexIndex_] := ModuleScope[
inputIDs = Table[Unique["id", {Temporary}], Length[input[[1]]]];
wholeInputPatternNames = Table[Unique["inputExpression", {Temporary}], Length[input[[1]]]];
inputGenerations = Table[Unique["generation", {Temporary}], Length[input[[1]]]];
With[{
heldModule = mapHold[output, {0, 1}]},
With[{
moduleArguments = Append[
ReleaseHold @ Map[Hold, heldModule[[1, 1]], {2}],
With[{outputExpressionCount = Length[heldModule[[1, 2]][[1]]]},
Hold[newExpressionIDs = Table[getNextExpression[], outputExpressionCount]]]],
moduleOutput = heldModule[[1, 2]]},
With[{
newModuleContents = Join[
(* event *)
(* Given that these look just like normal expressions,
which just output Nothing at the end,
they pass just fine through all the transformation. *)
{With[{event = {ruleID, inputIDs -> newExpressionIDs, Max[0, inputGenerations] + 1}},
Hold[Sow[event, $$events]; Nothing]]},
(* old expressions *)
(* don't put them in the main set, sow them instead,
that's much faster. *)
With[{expr = #[[3]]},
Hold[Sow[#, $$deletedExpressions]; deleteFromVertexIndex[vertexIndex, expr]; Nothing]] & /@
Transpose[{
inputIDs,
inputGenerations,
wholeInputPatternNames}],
(* new expressions *)
ReleaseHold @ MapIndexed[
Function[
{expr, index},
{Hold[newExpressionIDs[[index[[-1]]]]],
Max[0, inputGenerations] + 1,
Hold[addToVertexIndex[vertexIndex, expr, maxVertexDegree]]},
HoldAll],
moduleOutput,
{2}]],
originalInput = input[[1]],
condition = heldPart[input, 2]},
{Pattern[Evaluate[#[[1]]], Blank[]],
Pattern[Evaluate[#[[2]]], Blank[]] ? (# < maxGeneration &),
Pattern[Evaluate[#[[4]]], #[[3]]]} & /@
Transpose[{
inputIDs,
inputGenerations,
originalInput,
wholeInputPatternNames}] /; condition :>
Module[moduleArguments, newModuleContents]]] //.
Hold[expr_] :> expr]
];
$generationMetadataIndex = 2; (* {id, generation, atoms} *)
(* Determines maximal completed generation by trying to run the rules and checking the generation of the first match
obtained. Note, matching is necessary to determine that because it's impossible to determine if the last generation
is done otherwise. *)
maxCompleteGeneration[output_, rulesNoMetadata_] := ModuleScope[
patternToMatch = toNormalRules[
addMetadataManagement[#, Infinity, Infinity &, Infinity, Infinity, $noIndex] & /@ rulesNoMetadata];
matches = Reap[
FirstCase[
SortBy[output, #[[$generationMetadataIndex]] &], patternToMatch, Sow[$noMatch, $$deletedExpressions], {0}],
$$deletedExpressions][[2]];
Switch[matches,
{{$noMatch}},
Infinity,
{}, (* nothing -> something rule *)
0,
_,
Max[matches[[1, All, $generationMetadataIndex]]]
]
];
(* This function renames all rule inputs to avoid collisions with outputs from other rules. *)
renameRuleInputs[patternRules_] := Catch[Module[{pattern, inputAtoms, newInputAtoms},
SetAttributes[pattern, HoldFirst];
inputAtoms = Union[
Quiet[
Cases[
# /. Pattern -> pattern,
p : pattern[s_, rest___] :> If[MatchQ[Hold[s], Hold[_Symbol]],
Hold[s]
,
With[{originalP = p /. pattern -> Pattern}, Message[Pattern::patvar, originalP]]; Throw[$Failed]
],
All]
,
{RuleDelayed::rhs}
]];
newInputAtoms = Table[Unique["inputAtom", {Temporary}], Length[inputAtoms]];
# /. (((HoldPattern[#1] /. Hold[s_] :> s) -> #2) & @@@ Thread[inputAtoms -> newInputAtoms])
] & /@ patternRules];
(* This yields unique elements in the expressions upto level 1. *)
expressionVertices[expr_] := If[ListQ[expr], Union[expr], Throw[expr, $$nonListExpression]];
(* The following is used to keep track of how many times vertices appear in the set.
All operations here should evaluate in O(1). *)
Attributes[$vertexIndex] = {HoldAll};
initVertexIndex[$vertexIndex[index_], set_] := (
index = Counts[Catenate[expressionVertices /@ set]];
set
);
initVertexIndex[$noIndex, set_] := set;
deleteFromVertexIndex[$vertexIndex[index_], expr_] := (
Scan[
index[#] = Lookup[index, Key[#], 0] - 1;
If[index[#] == 0, index[#] =.]; &,
expressionVertices[expr]];
expr
);
deleteFromVertexIndex[$noIndex, expr_] := expr;
addToVertexIndex[$vertexIndex[index_], expr_, limit_] := (
Scan[
index[#] = Lookup[index, Key[#], 0] + 1;
If[index[#] > limit, Throw[#, $$reachedAtomDegreeLimit]]; &,
expressionVertices[expr]];
expr
);
addToVertexIndex[$noIndex, expr_, limit_] := expr;
vertexCount[$vertexIndex[index_]] := Length[index];
vertexCount[$noIndex] := 0;
(* This function runs a modified version of the set substitution system that also keeps track of metadata such as
generations and events. It uses setReplace$wl to evaluate that modified system. *)
setSubstitutionSystem$wl[
caller_, rules_, init_, stepSpec_, returnOnAbortQ_, timeConstraint_] := ModuleScope[
nextExpressionID = 1;
nextExpression = nextExpressionID++ &;
(* {id, generation, atoms} *)
initWithMetadata = {nextExpression[], 0, #} & /@ init;
renamedRules = renameRuleInputs[toCanonicalRules[rules]];
If[renamedRules === $Failed, Return[$Failed]];
vertexIndex = If[MissingQ[stepSpec[$maxFinalVertices]] && MissingQ[stepSpec[$maxFinalVertexDegree]],
$noIndex
,
$vertexIndex[expressionsCountsPerVertex]
];
initVertexIndex[vertexIndex, init];
rulesWithMetadata = MapIndexed[
addMetadataManagement[
#,
#2[[1]],
nextExpression,
Lookup[stepSpec, $maxGenerationsLocal, Infinity],
Lookup[stepSpec, $maxFinalVertexDegree, Infinity],
vertexIndex] &,
renamedRules];
outputWithMetadata = Catch[
Reap[
setReplace$wl[initWithMetadata, rulesWithMetadata, stepSpec, vertexIndex, returnOnAbortQ, timeConstraint],
{$$deletedExpressions, $$events}]
,
$$nonListExpression
,
(Message[caller::nonListExpressions, #]; Return[$Failed]) &
]; (* {{finalState, terminationReason}, {{deletedExpressions}, {events}}} *)
If[outputWithMetadata[[1]] === $Aborted, Return[$Aborted]];
allExpressions = SortBy[
Join[
outputWithMetadata[[1, 1]],
If[outputWithMetadata[[2, 1]] == {}, {}, outputWithMetadata[[2, 1, 1]]]],
First];
initialEvent = {0, {} -> initWithMetadata[[All, 1]], 0};
allEvents = Join[{initialEvent}, If[outputWithMetadata[[2, 2]] == {}, {}, outputWithMetadata[[2, 2, 1]]]];
generationsCount = Max[allEvents[[All, 3]], 0];
maxCompleteGenerationResult = CheckAbort[
Min[maxCompleteGeneration[outputWithMetadata[[1, 1]], renamedRules], generationsCount],
If[returnOnAbortQ,
Missing["Unknown", $Aborted]
,
Return[$Aborted]
]];
WolframModelEvolutionObject[<|
$version -> 2,
$rules -> rules,
$maxCompleteGeneration -> maxCompleteGenerationResult,
$terminationReason -> Replace[
outputWithMetadata[[1, 2]],
$fixedPoint ->
If[generationsCount == Lookup[stepSpec, $maxGenerationsLocal, Infinity],
$maxGenerationsLocal
,
$fixedPoint
]],
$atomLists -> allExpressions[[All, 3]],
$eventRuleIDs -> allEvents[[All, 1]],
$eventInputs -> allEvents[[All, 2, 1]],
$eventOutputs -> allEvents[[All, 2, 2]],
$eventGenerations -> allEvents[[All, 3]]|>]
];