From f0e20640f5472d5551fc8dfa51ea6871d9966a34 Mon Sep 17 00:00:00 2001 From: Rebecca Ghidini Date: Mon, 17 Feb 2025 12:09:16 +0100 Subject: [PATCH] disable initial mixture in KaSim if the guard is false --- core/cli/cli_init.ml | 5 +- core/grammar/lKappa_compiler.ml | 91 +++++++++++++++++--------------- core/grammar/lKappa_compiler.mli | 1 + 3 files changed, 52 insertions(+), 45 deletions(-) diff --git a/core/cli/cli_init.ml b/core/cli/cli_init.ml index 8b326444e..7e0f446dc 100644 --- a/core/cli/cli_init.ml +++ b/core/cli/cli_init.ml @@ -64,7 +64,8 @@ let preprocess_ast ~warning ~debug_mode ?kasim_args cli_args ast_compiled_data.agents_sig ast_compiled_data.counters_info ast_compiled_data.contact_map ast_compiled_data.token_names.NamedDecls.finder - ast_compiled_data.alg_vars_finder compil.Ast.init), + ast_compiled_data.alg_vars_finder compil.Ast.init + compil.Ast.guard_param_values), conf.Configuration.initial ) in { @@ -193,7 +194,7 @@ let get_pack_from_marshalizedfile ~warning kasim_args cli_args marshalized_file compilation_result.counters_info compilation_result.contact_map (Model.tokens_finder compilation_result.env) (Model.algs_finder compilation_result.env) - compil.Ast.init + compil.Ast.init compil.Ast.guard_param_values in let init_l = Eval.compile_inits ~debug_mode:!Parameter.debug_modeOn ~warning diff --git a/core/grammar/lKappa_compiler.ml b/core/grammar/lKappa_compiler.ml index 04b7f8968..5389e2006 100644 --- a/core/grammar/lKappa_compiler.ml +++ b/core/grammar/lKappa_compiler.ml @@ -1907,15 +1907,54 @@ let create_sigs (l : Ast.agent_sig list) : Signature.s = (* TODO see agent_sigs namings *) Signature.create ~counters_per_agent agent_sigs +type bool_or_error = Value of bool | Error of Loc.t + +let evaluate_guard_opt guard guard_param_values = + let rec evaluate_guard = function + | LKappa.True -> Value true + | LKappa.False -> Value false + | LKappa.Param (p, pos) -> + (match Ast.StringMap.find_opt p guard_param_values with + | None -> Error pos + | Some value -> Value value) + | Not guard -> + (match evaluate_guard guard with + | Value value -> Value (not value) + | Error pos -> Error pos) + | And (g1, g2) -> + (match evaluate_guard g1, evaluate_guard g2 with + | Value v1, Value v2 -> Value (v1 && v2) + | Value false, _ | _, Value false -> Value false + | Error pos, _ | _, Error pos -> Error pos) + | Or (g1, g2) -> + (match evaluate_guard g1, evaluate_guard g2 with + | Value v1, Value v2 -> Value (v1 || v2) + | Value true, _ | _, Value true -> Value true + | Error pos, _ | _, Error pos -> Error pos) + in + match guard with + | None -> true + | Some guard -> + (match evaluate_guard guard with + | Error pos -> + raise + (ExceptionDefn.Malformed_Decl + ("Undefined value for guard parameter ", pos)) + | Value value -> value) + let init_of_ast ~warning ~syntax_version sigs counters_info contact_map tok algs - inits = - List.map - (fun (g, (*rTODO*) expr, ini) -> - ( g, - alg_expr_of_ast ~warning ~syntax_version sigs counters_info tok algs - expr, - init_of_ast ~warning ~syntax_version sigs counters_info tok contact_map - ini )) + inits guard_param_values = + List.filter_map + (fun (guard, expr, ini) -> + if evaluate_guard_opt guard guard_param_values then + Some + ( guard, + alg_expr_of_ast ~warning ~syntax_version sigs counters_info tok algs + expr, + init_of_ast ~warning ~syntax_version sigs counters_info tok + contact_map ini ) + else + None) inits type ast_compiled_data = { @@ -2542,41 +2581,6 @@ let conflicts_to_id agents_sig conflicts = (agent_id, snd agent), (site1_id, snd site1), (site2_id, snd site2)) conflicts -type bool_or_error = Value of bool | Error of Loc.t - -let evaluate_guard_opt guard guard_param_values = - let rec evaluate_guard = function - | LKappa.True -> Value true - | LKappa.False -> Value false - | LKappa.Param (p, pos) -> - (match Ast.StringMap.find_opt p guard_param_values with - | None -> Error pos - | Some value -> Value value) - | Not guard -> - (match evaluate_guard guard with - | Value value -> Value (not value) - | Error pos -> Error pos) - | And (g1, g2) -> - (match evaluate_guard g1, evaluate_guard g2 with - | Value v1, Value v2 -> Value (v1 && v2) - | Value false, _ | _, Value false -> Value false - | Error pos, _ | _, Error pos -> Error pos) - | Or (g1, g2) -> - (match evaluate_guard g1, evaluate_guard g2 with - | Value v1, Value v2 -> Value (v1 || v2) - | Value true, _ | _, Value true -> Value true - | Error pos, _ | _, Error pos -> Error pos) - in - match guard with - | None -> true - | Some guard -> - (match evaluate_guard guard with - | Error pos -> - raise - (ExceptionDefn.Malformed_Decl - ("Undefined value for guard parameter ", pos)) - | Value value -> value) - let compil_of_ast ~warning ~debug_mode ~syntax_version ~var_overwrite ast_compil = (* TODO test this *) @@ -2780,6 +2784,7 @@ let compil_of_ast ~warning ~debug_mode ~syntax_version ~var_overwrite ast_compil let init = init_of_ast ~warning ~syntax_version agents_sig counters_info contact_map tokens_finder alg_vars_finder ast_compil.init + ast_compil.guard_param_values in { agents_sig; diff --git a/core/grammar/lKappa_compiler.mli b/core/grammar/lKappa_compiler.mli index d6e688954..af5b015ad 100644 --- a/core/grammar/lKappa_compiler.mli +++ b/core/grammar/lKappa_compiler.mli @@ -39,6 +39,7 @@ val init_of_ast : int Mods.StringMap.t -> int Mods.StringMap.t -> (Ast.mixture, Ast.mixture, string) Ast.init_statement list -> + bool Ast.StringMap.t -> (LKappa.rule_agent list, Raw_mixture.t, int) Ast.init_statement list type ast_compiled_data = {