Skip to content

Commit

Permalink
convert guard parameters from string to int
Browse files Browse the repository at this point in the history
  • Loading branch information
reb-ddm committed Jan 8, 2025
1 parent 656c969 commit d1e81e7
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 18 deletions.
2 changes: 1 addition & 1 deletion core/KaSa_rep/frontend/cckappa_sig.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
2 changes: 1 addition & 1 deletion core/KaSa_rep/frontend/cckappa_sig.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
13 changes: 13 additions & 0 deletions core/KaSa_rep/frontend/handler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
10 changes: 10 additions & 0 deletions core/KaSa_rep/frontend/handler.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand Down
10 changes: 8 additions & 2 deletions core/KaSa_rep/frontend/preprocess.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion core/grammar/ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion core/grammar/ast.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
31 changes: 28 additions & 3 deletions core/grammar/lKappa_compiler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 *)
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
8 changes: 5 additions & 3 deletions core/grammar/lKappa_compiler.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 ->
Expand Down
4 changes: 1 addition & 3 deletions core/term/model.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 1 addition & 3 deletions core/term/model.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit d1e81e7

Please sign in to comment.