Skip to content

Commit

Permalink
Change system parameters to symbols.
Browse files Browse the repository at this point in the history
  • Loading branch information
Max Piskunov committed Jul 8, 2021
1 parent b4c2104 commit 3679dc9
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 33 deletions.
40 changes: 18 additions & 22 deletions Kernel/A1$generatorSystem.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@
(* Parameters in the fourth argument should be declared with declareSystemParameter. Their values are guaranteed to
match the pattern from the declaration. Further, the logical expression in the last argument will check if all
required parameters are specified. Some parameters may require others to be specified, e.g.,
Implies["MaxDestroyerEvents" || "MaxEvents", "EventOrder"] means that if "MaxDestroyerEvents" or "MaxEvents" is
specified, "EventOrder" must be specified as well. The implementation function can expect all specified parameters
present (substituted with defaults if missing) and all values satisfying the constraints (substituted with defaults
if missing). *)
Implies[MaxDestroyerEvents || MaxEvents, EventOrder] means that if MaxDestroyerEvents or MaxEvents is specified, \
EventOrder must be specified as well. The implementation function can expect all specified parameters present \
(substituted with defaults if missing) and all values satisfying the constraints (substituted with defaults if \
missing). *)

(* For example,
declareSystem[MultisetSubstitutionSystem,
generateMultisetSubstitutionSystem,
_List,
{"MaxGeneration", "MinEventInputs", "MaxDestroyerEvents", "MaxEvents", "EventOrder"},
Implies["MaxDestroyerEvents" || "MaxEvents", "EventOrder"]] *)
{MaxGeneration, MinEventInputs, MaxDestroyerEvents, MaxEvents, EventOrder},
Implies[MaxDestroyerEvents || MaxEvents, EventOrder]] *)

(* The implementation function is then called as
generateMultisetSubstitutionSystem[MultisetSubstitutionSystem[rules], init, parameters] *)
Expand Down Expand Up @@ -78,12 +78,12 @@
declareSystemGenerator[EvaluateSingleHistory,
evaluateSingleHistory,
<|"MaxDestroyerEvents" -> 1|>,
<|MaxDestroyerEvents -> 1|>,
FinalState,
"yields a single history object."]
Note that the constraint in the last argument of declareSystemGenerator still needs to be specified, which means
"EventOrder" is now a required parameter. *)
EventOrder is now a required parameter. *)

(* evaluateSingleHistory is a PackageScope symbol that will throw exceptions instead of returning unevaluated.
It cannot be used in operator form. *)
Expand Down Expand Up @@ -114,11 +114,15 @@
to be supported and define values for them. To declare a new parameter, one needs to specify a default value (which
usually disables whatever the parameter is doing) and a pattern the parameter value should match. *)

(* declareSystemParameter["MaxGeneration", Infinity, _ ? (GreaterEqualThan[0])] *)
(* declareSystemParameter[MaxGeneration,
Infinity,
_ ? (GreaterEqualThan[0]),
"is a parameter specifying the maximum generations of tokens that will be created."] *)

declareSystemParameter[name_, defaultValue_, pattern_] := (
declareSystemParameter[name_, defaultValue_, pattern_, usage_] := (
$parameterDefaults[name] = defaultValue;
$parameterPatterns[name] = pattern;
SetUsage @ Evaluate[ToString[name] <> " " <> usage];
);

(* Initialization *)
Expand All @@ -141,31 +145,23 @@
declareMessage[
General::unknownGeneratorParameters, "Parameters `parameters` are set by `generator` but is not declared."];

initializeSystemGenerators[] := Module[{parameterKeys, maxParameterCount},
initializeSystemGenerators[] := (
$SetReplaceSystems = Sort @ Keys @ $systemImplementations;
$SetReplaceGenerators = Sort @ Keys @ $generatorParameters;
parameterKeys = Keys[$parameterDefaults];
With[{missingParameters = Complement[$systemParameters[#], parameterKeys]},
With[{missingParameters = Complement[$systemParameters[#], Keys[$parameterDefaults]]},
If[missingParameters =!= {},
message[SetReplace, Failure["unknownSystemParameters", <|"parameters" -> missingParameters, "system" -> #|>]];
];
] & /@ $SetReplaceSystems;
With[{missingParameters = Complement[Keys @ $generatorParameters[#], parameterKeys]},
With[{missingParameters = Complement[Keys @ $generatorParameters[#], Keys[$parameterDefaults]]},
If[missingParameters =!= {},
message[
SetReplace, Failure["unknownGeneratorParameters", <|"parameters" -> missingParameters, "generator" -> #|>]];
];
] & /@ $SetReplaceGenerators;
maxParameterCount = Length @ parameterKeys;
(* This has quadratic complexity in parameter count. But it does not seem to be possible to define the same set of
completion strings for a range of arguments. *)
With[{
completionSpec = Join[{0}, Table[parameterKeys, maxParameterCount + 1]]},
FE`Evaluate[FEPrivate`AddSpecialArgCompletion[# -> completionSpec]] & /@ ToString /@ $SetReplaceGenerators;
];
Scan[(SyntaxInformation[#] = {"ArgumentsPattern" -> {system_, init_., parameters___}}) &, $SetReplaceGenerators];
defineGeneratorImplementation /@ Keys @ $generatorParameters;
];
);

declareMessage[General::argNotInit, "The init `arg` in `expr` should match `pattern`."];
declareMessage[General::unknownSystem, "`system` is not a recognized SetReplace system."];
Expand Down
5 changes: 2 additions & 3 deletions Kernel/AtomicStateSystem.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@

SyntaxInformation[AtomicStateSystem] = {"ArgumentsPattern" -> {rules_}};

declareSystem[
AtomicStateSystem, generateAtomicStateSystem, _, {"MaxGeneration", "MaxDestroyerEvents", "MaxEvents"}, True];
declareSystem[AtomicStateSystem, generateAtomicStateSystem, _, {MaxGeneration, MaxDestroyerEvents, MaxEvents}, True];

generateAtomicStateSystem[AtomicStateSystem[rules___], init_, parameters_] := ModuleScope[
toAtomicStateMultihistory[rules] @ generateMultihistory[
MultisetSubstitutionSystem[toMultisetRules[rules]],
{init},
Join[parameters, <|"MinEventInputs" -> 1, "MaxEventInputs" -> 1|>]]
Join[parameters, <|MinEventInputs -> 1, MaxEventInputs -> 1|>]]
];

(* Parsing *)
Expand Down
2 changes: 1 addition & 1 deletion Kernel/MultisetSubstitutionSystem.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
declareSystem[MultisetSubstitutionSystem,
generateMultisetSubstitutionSystem,
_List,
{"MaxGeneration", "MaxDestroyerEvents", "MinEventInputs", "MaxEventInputs", "MaxEvents"},
{MaxGeneration, MaxDestroyerEvents, MinEventInputs, MaxEventInputs, MaxEvents},
True];

generateMultisetSubstitutionSystem[MultisetSubstitutionSystem[rawRules___], init_, parameters_] := Block[{
Expand Down
2 changes: 1 addition & 1 deletion Kernel/systemGenerators.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@
declareSystemGenerator[
GenerateSingleHistory,
generateSingleHistory,
<|"MaxDestroyerEvents" -> 1|>,
<|MaxDestroyerEvents -> 1|>,
Identity,
"yields a single history of the evaluation of a specified system$ starting from an initial state init$."];
19 changes: 13 additions & 6 deletions Kernel/systemParameters.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@

(* Event-selection parameters *)

declareSystemParameter[##, _ ? (GreaterEqualThan[0])] & @@@ {
{"MaxGeneration", Infinity},
{"MaxDestroyerEvents", Infinity},
{"MinEventInputs", 0},
{"MaxEventInputs", Infinity}
PackageExport["MaxGeneration"]
PackageExport["MaxDestroyerEvents"]
PackageExport["MinEventInputs"]
PackageExport["MaxEventInputs"]

declareSystemParameter[#, #2, _ ? (GreaterEqualThan[0]), #3] & @@@ {
{MaxGeneration, Infinity, "is a parameter specifying the maximum generations of tokens that will be created."},
{MaxDestroyerEvents, Infinity, "..."},
{MinEventInputs, 0, "..."},
{MaxEventInputs, Infinity, "..."}
};

(* Stopping-condition parameters *)

declareSystemParameter["MaxEvents", Infinity, _ ? (GreaterEqualThan[0])];
PackageExport["MaxEvents"]

declareSystemParameter[MaxEvents, Infinity, _ ? (GreaterEqualThan[0]), "..."];

0 comments on commit 3679dc9

Please sign in to comment.