From d1e81e7363a60c6a541f99a0dcc2046f1b3b92c9 Mon Sep 17 00:00:00 2001 From: Rebecca Ghidini Date: Wed, 8 Jan 2025 10:00:50 +0100 Subject: [PATCH] convert guard parameters from string to int --- core/KaSa_rep/frontend/cckappa_sig.ml | 2 +- core/KaSa_rep/frontend/cckappa_sig.mli | 2 +- core/KaSa_rep/frontend/handler.ml | 13 +++++++++++ core/KaSa_rep/frontend/handler.mli | 10 +++++++++ core/KaSa_rep/frontend/preprocess.ml | 10 +++++++-- core/grammar/ast.ml | 2 +- core/grammar/ast.mli | 2 +- core/grammar/lKappa_compiler.ml | 31 +++++++++++++++++++++++--- core/grammar/lKappa_compiler.mli | 8 ++++--- core/term/model.ml | 4 +--- core/term/model.mli | 4 +--- 11 files changed, 70 insertions(+), 18 deletions(-) diff --git a/core/KaSa_rep/frontend/cckappa_sig.ml b/core/KaSa_rep/frontend/cckappa_sig.ml index 011841562..987b54c32 100644 --- a/core/KaSa_rep/frontend/cckappa_sig.ml +++ b/core/KaSa_rep/frontend/cckappa_sig.ml @@ -158,7 +158,7 @@ type rule = { diff_direct: diff_views; diff_reverse: diff_views; actions: actions; - guard: string LKappa.guard option; + guard: int LKappa.guard option; } type modif_expr = diff --git a/core/KaSa_rep/frontend/cckappa_sig.mli b/core/KaSa_rep/frontend/cckappa_sig.mli index 4333cfa7b..3ac820210 100644 --- a/core/KaSa_rep/frontend/cckappa_sig.mli +++ b/core/KaSa_rep/frontend/cckappa_sig.mli @@ -151,7 +151,7 @@ type rule = { diff_direct: diff_views; diff_reverse: diff_views; actions: actions; - guard: string LKappa.guard option; + guard: int LKappa.guard option; } type modif_expr = diff --git a/core/KaSa_rep/frontend/handler.ml b/core/KaSa_rep/frontend/handler.ml index 4ccc23d14..32c7baacf 100644 --- a/core/KaSa_rep/frontend/handler.ml +++ b/core/KaSa_rep/frontend/handler.ml @@ -570,6 +570,19 @@ let string_of_site parameter error handler_kappa ?state in error, print_site parameter ?state ~add_parentheses site_type +let string_of_guard g guard_params error = + error, List.nth guard_params (Ckappa_sig.int_of_guard_parameter g) +(*rTODO error handling*) + +let string_of_site_or_guard parameter error handler_kappa ?state + ?(add_parentheses = false) agent_type site_int = + match site_int with + | Ckappa_sig.Site s -> + string_of_site parameter error handler_kappa ?state ~add_parentheses + agent_type s + | Ckappa_sig.Guard_p g -> + string_of_guard g handler_kappa.Cckappa_sig.guard_parameters error + (*this function used in views_domain*) let string_of_site_update_views parameter error handler_kappa agent_type site_int = diff --git a/core/KaSa_rep/frontend/handler.mli b/core/KaSa_rep/frontend/handler.mli index 1eaffa108..f68739b60 100644 --- a/core/KaSa_rep/frontend/handler.mli +++ b/core/KaSa_rep/frontend/handler.mli @@ -212,6 +212,16 @@ val string_of_site : Ckappa_sig.c_site_name -> Exception_without_parameter.exceptions_caught_and_uncaught * string +val string_of_site_or_guard : + Remanent_parameters_sig.parameters -> + Exception_without_parameter.exceptions_caught_and_uncaught -> + Cckappa_sig.kappa_handler -> + ?state:Ckappa_sig.c_state -> + ?add_parentheses:bool -> + Quark_type.agent_quark -> + Ckappa_sig.c_site_or_guard_p -> + Exception_without_parameter.exceptions_caught_and_uncaught * string + val string_of_site_in_file_name : Remanent_parameters_sig.parameters -> Exception_without_parameter.exceptions_caught_and_uncaught -> diff --git a/core/KaSa_rep/frontend/preprocess.ml b/core/KaSa_rep/frontend/preprocess.ml index b08c0b61b..00e8b4f03 100644 --- a/core/KaSa_rep/frontend/preprocess.ml +++ b/core/KaSa_rep/frontend/preprocess.ml @@ -2241,10 +2241,16 @@ let translate_c_compil parameters error handler compil = error, var :: list) (error, []) compil.Ast.variables in + let guard_params = Ast.get_list_of_guard_parameters compil.Ast.rules in let error, c_rules = List.fold_left - (fun (error, list) rule -> - let error, c_rule = translate_rule parameters error handler rule in + (fun (error, list) (r1, guard, r2) -> + let error, c_rule = + translate_rule parameters error handler + ( r1, + LKappa_compiler.guard_params_to_int_option guard_params guard, + r2 ) + in error, c_rule :: list) (error, []) compil.Ast.rules in diff --git a/core/grammar/ast.ml b/core/grammar/ast.ml index 51f9f5f00..2dad7147e 100644 --- a/core/grammar/ast.ml +++ b/core/grammar/ast.ml @@ -179,7 +179,7 @@ type ('agent, 'agent_sig, 'pattern, 'mixture, 'id, 'rule) compil = { or kinetic rate *) signatures: 'agent_sig list; (** agent signature declaration *) rules: - (string Loc.annoted option * string LKappa.guard option * 'rule Loc.annoted) + (string Loc.annoted option * 'id LKappa.guard option * 'rule Loc.annoted) list; (** rules (possibly named, possibly with a guard): [name_option * rule_definition] *) observables: ('pattern, 'id) Alg_expr.e Loc.annoted list; diff --git a/core/grammar/ast.mli b/core/grammar/ast.mli index 821decd01..eebe93f88 100644 --- a/core/grammar/ast.mli +++ b/core/grammar/ast.mli @@ -157,7 +157,7 @@ type ('agent, 'agent_sig, 'pattern, 'mixture, 'id, 'rule) compil = { (** pattern declaration for reusing as variable in perturbations or kinetic rate *) signatures: 'agent_sig list; (** agent signature declarations *) rules: - (string Loc.annoted option * string LKappa.guard option * 'rule Loc.annoted) + (string Loc.annoted option * 'id LKappa.guard option * 'rule Loc.annoted) list; (**rules (possibly named)*) observables: ('pattern, 'id) Alg_expr.e Loc.annoted list; diff --git a/core/grammar/lKappa_compiler.ml b/core/grammar/lKappa_compiler.ml index 92063cbd1..c70414fc1 100644 --- a/core/grammar/lKappa_compiler.ml +++ b/core/grammar/lKappa_compiler.ml @@ -1923,7 +1923,7 @@ type ast_compiled_data = { token_names: unit NamedDecls.t; alg_vars_finder: int Mods.StringMap.t; updated_alg_vars: int list; - guard_params: string list; + nr_guard_params: int; result: ( Ast.agent, Ast.agent_sig, @@ -2531,6 +2531,30 @@ let translate_clte_into_cgte (ast_compil : Ast.parsing_compil) = }, counter_conversion_info_map ) +let rec guard_param_to_int guard_params g = + match g with + | LKappa.True -> LKappa.True + | LKappa.False -> LKappa.False + | Param p -> + (match List.find_index (fun x -> String.equal p x) guard_params with + | Some i -> Param i + | None -> + raise + (ExceptionDefn.Malformed_Decl ("Unknown guard parameter", Loc.dummy))) + | Not g1 -> Not (guard_param_to_int guard_params g1) + | And (g1, g2) -> + And (guard_param_to_int guard_params g1, guard_param_to_int guard_params g2) + | Or (g1, g2) -> + Or (guard_param_to_int guard_params g1, guard_param_to_int guard_params g2) + +let guard_params_to_int_option guard_params g = + Option.map (guard_param_to_int guard_params) g + +let guard_params_to_int_in_rules guard_params rules = + List.map + (fun (r1, g, r2) -> r1, guard_params_to_int_option guard_params g, r2) + rules + let compil_of_ast ~warning ~debug_mode ~syntax_version ~var_overwrite ast_compil = (* TODO test this *) @@ -2728,8 +2752,9 @@ let compil_of_ast ~warning ~debug_mode ~syntax_version ~var_overwrite ast_compil init_of_ast ~warning ~syntax_version agents_sig counters_info contact_map tokens_finder alg_vars_finder ast_compil.init in - let guard_params = Ast.get_list_of_guard_parameters ast_compil.rules in + (*rTODO this is never really used you have to rethink this*) + let rules = guard_params_to_int_in_rules guard_params rules in { agents_sig; @@ -2738,7 +2763,7 @@ let compil_of_ast ~warning ~debug_mode ~syntax_version ~var_overwrite ast_compil token_names; alg_vars_finder; updated_alg_vars; - guard_params; + nr_guard_params = List.length guard_params; result = { filenames = ast_compil.filenames; diff --git a/core/grammar/lKappa_compiler.mli b/core/grammar/lKappa_compiler.mli index a26d0f803..de94a4642 100644 --- a/core/grammar/lKappa_compiler.mli +++ b/core/grammar/lKappa_compiler.mli @@ -48,9 +48,8 @@ type ast_compiled_data = { token_names: unit NamedDecls.t; alg_vars_finder: int Mods.StringMap.t; updated_alg_vars: int list; (** alg vars with forbidden constant prop *) - guard_params: string list; - (** all guard parameters that are present in any guard in the code *) - (*rTODO make it an int list*) + nr_guard_params: int; + (** how many guard parameters are present in any guard in the code (numbered from 0 to guard_params - 1) *) result: ( Ast.agent, Ast.agent_sig, @@ -64,6 +63,9 @@ type ast_compiled_data = { * (syntactic sugar on mixture are not) *) } +val guard_params_to_int_option : + string list -> string LKappa.guard option -> int LKappa.guard option + val compil_of_ast : warning:(pos:Loc.t -> (Format.formatter -> unit) -> unit) -> debug_mode:bool -> diff --git a/core/term/model.ml b/core/term/model.ml index e6a20bf23..8f83071af 100644 --- a/core/term/model.ml +++ b/core/term/model.ml @@ -7,9 +7,7 @@ (******************************************************************************) type rule_with_label_and_guard = - string Loc.annoted option - * string LKappa.guard option - * LKappa.rule Loc.annoted + string Loc.annoted option * int LKappa.guard option * LKappa.rule Loc.annoted type t = { filenames: string list; diff --git a/core/term/model.mli b/core/term/model.mli index cbf25176d..c4230ad68 100644 --- a/core/term/model.mli +++ b/core/term/model.mli @@ -9,9 +9,7 @@ (** Compiled representation of a full Kappa model *) type rule_with_label_and_guard = - string Loc.annoted option - * string LKappa.guard option - * LKappa.rule Loc.annoted + string Loc.annoted option * int LKappa.guard option * LKappa.rule Loc.annoted type t