maxitg / SetReplace

C++/Wolfram Language package for exploring set and graph rewriting systems
MIT License
219 stars 45 forks source link

Incorrect evaluation order in symbolic implementation #158

Open maxitg opened 4 years ago

maxitg commented 4 years ago

Compare

In[] := WolframModel[{{{1, 2}, {2, 3}} -> {{1, 3}, {2, 4}, {4, 3}}, {{1, 
     1}, {2, 1}} -> {{1, 1}}}, {{2, 2}, {1, 4}, {4, 2}, {1, 2}, {3, 
   5}, {5, 2}}, <|"MaxEvents" -> 1|>, "FinalState"]
Out[] = {{1, 4}, {1, 2}, {3, 5}, {5, 2}, {2, 2}}

and

In[] := WolframModel[{{{1, 2}, {2, 3}} -> {{1, 3}, {2, 4}, {4, 3}}, {{1, 
     1}, {2, 1}} -> {{1, 1}}}, {{2, 2}, {1, 4}, {4, 2}, {1, 2}, {3, 
   5}, {5, 2}}, <|"MaxEvents" -> 1|>, "FinalState", 
 Method -> "Symbolic"]
Out[] = {{2, 2}, {1, 2}, {3, 5}, {5, 2}, {1, 2}, {4, 6}, {6, 2}}

The issue is that while the symbolic code makes sure the most recent expression picked is least recent, it does not do anything with the expressions to the left of it.

This makes it different from LowLevel implementation which compares sorted indices of the matched subsets backwards.

Fixing this is not trivial, as it requires pattern matching constructions that might not exist in WL. Specifically, one would need to compare lengths of patterns across different alternatives.

Another way to solve this might be use multiple rules instead of alternatives in toNormalRules, evaluate all of them at each evolution step, and pick the best one at that point.

maxitg commented 4 years ago

Might not be worth fixing before #401.