From d71a3ce1fe440e65e654d7baee3924aebcaa5cc5 Mon Sep 17 00:00:00 2001 From: Andrei <16517508+anvacaru@users.noreply.github.com> Date: Tue, 10 Dec 2024 11:14:58 +0200 Subject: [PATCH] refactoring _exec_forget_custom_step --- src/kontrol/foundry.py | 112 ++++++++++++++++++++++++----------------- src/kontrol/prove.py | 2 - 2 files changed, 65 insertions(+), 49 deletions(-) diff --git a/src/kontrol/foundry.py b/src/kontrol/foundry.py index b2ddc782d..a97e8deb2 100644 --- a/src/kontrol/foundry.py +++ b/src/kontrol/foundry.py @@ -107,7 +107,7 @@ def cut_point_rules( break_on_basic_blocks: bool, break_on_load_program: bool, ) -> list[str]: - return ['FOUNDRY-CHEAT-CODES.rename'] + KEVMSemantics.cut_point_rules( + return ['FOUNDRY-CHEAT-CODES.rename', 'FOUNDRY-ACCOUNTS.forget'] + KEVMSemantics.cut_point_rules( break_on_jumpi, break_on_jump, break_on_calls, @@ -182,8 +182,65 @@ def _exec_forget_custom_step(self, cterm: CTerm, cterm_symbolic: CTermSymbolic) of the K cell and is removed from the initial path constraints if it existed, together with information that the `cheatcode_forget` rule has been applied. """ - _LOGGER.info('forgetBranch: pattern confirmed') - _operators = ['_==Int_', '_!=Int_', '_<=Int_', '_=Int_', '_>Int_'] + + def _find_constraints_to_keep(cterm: CTerm, constraint_vars: frozenset[str]) -> set[KInner]: + range_patterns: list[KInner] = [ + mlEqualsTrue(KApply('_ set[KInner]: + for constraint_variant in constraints_to_remove: + simplification_cterm = initial_cterm.add_constraint(constraint_variant) + result_cterm, _ = cterm_symbolic.simplify(simplification_cterm) + # Extract constraints that appear after simplification but are not in the 'to keep' set + result_constraints = set(result_cterm.constraints).difference(constraints_to_keep) + + if len(result_constraints) == 1: + target_constraint = single(result_constraints) + if target_constraint in constraints: + _LOGGER.info(f'forgetBranch: removing constraint: {target_constraint}') + constraints.remove(target_constraint) + break + else: + _LOGGER.info(f'forgetBranch: constraint: {target_constraint} not found in current constraints') + else: + # If no constraints or multiple constraints appear, log this scenario. + if len(result_constraints) == 0: + _LOGGER.info(f'forgetBranch: constraint {constraint_variant} entailed by remaining constraints') + else: + _LOGGER.info( + f'forgetBranch: more than one constraint found after simplification and removal:\n{result_constraints}' + ) + return constraints + + _operators = ['_==Int_', '_=/=Int_', '_<=Int_', '_=Int_', '_>Int_'] subst = self._cached_subst assert subst is not None # Extract the terms and operator from the substitution @@ -196,58 +253,19 @@ def _exec_forget_custom_step(self, cterm: CTerm, cterm_symbolic: CTermSymbolic) neg_constraint = mlEqualsTrue(notBool(KApply(_operators[int(operator.token)], fst_term, snd_term))) # To be able to better simplify, we maintain range constraints on the variables present in the constraint constraint_vars: frozenset[str] = free_vars(fst_term).union(free_vars(snd_term)) - range_patterns: list[KInner] = [ - mlEqualsTrue(KApply('_ KCFGExplore: rule.label for rule in foundry.kevm.definition.all_modules_dict['KONTROL-ASSERTIONS'].rules ) - cut_point_rules.append('FOUNDRY-ACCOUNTS.forget') - extra_lemmas_module: KFlatModule | None = None if options.extra_module: extra_module_file, extra_module_name, *_ = options.extra_module.split(':')