Skip to content

Commit 45c0b09

Browse files
nishantjrehildenb
andauthored
PyK: Misc enhancements to KCFG and anti-unification (#4775)
* KCFG: Shorten log message for easier readability This brings the more important parts of the messages into focus. * CTerm.anti_unify: Don't call ml_pred_to_bool unless necessary Since these values are only used if keep_values is set, we do not need to always compute them. In addition, it avoids exceptions when the predicate cannot be converted to a bool expression: e.g. when using #Ceil as a side condition This is only a work-around (it would still break for a #Ceil side condition, if keep_values=True), but lets us move forward without risking breaking other projects that rely on the original behaviour. * pyk:KCFGShow.to_module: Thread imports and defunc_with parameters through to KCFG.to_module This lets us use those options for printing the modules. --------- Co-authored-by: Everett Hildenbrandt <[email protected]>
1 parent 2501a51 commit 45c0b09

File tree

3 files changed

+24
-14
lines changed

3 files changed

+24
-14
lines changed

pyk/src/pyk/cterm/cterm.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -204,18 +204,18 @@ def anti_unify(
204204
- ``csubst2``: Constrained substitution to apply to `cterm` to obtain `other`.
205205
"""
206206
new_config, self_subst, other_subst = anti_unify(self.config, other.config, kdef=kdef)
207-
# todo: It's not able to distinguish between constraints in different cterms,
208-
# because variable names may be used inconsistently in different cterms.
209207
common_constraints = [constraint for constraint in self.constraints if constraint in other.constraints]
210-
self_unique_constraints = [
211-
ml_pred_to_bool(constraint) for constraint in self.constraints if constraint not in other.constraints
212-
]
213-
other_unique_constraints = [
214-
ml_pred_to_bool(constraint) for constraint in other.constraints if constraint not in self.constraints
215-
]
216208

217209
new_cterm = CTerm(config=new_config, constraints=())
218210
if keep_values:
211+
# todo: It's not able to distinguish between constraints in different cterms,
212+
# because variable names may be used inconsistently in different cterms.
213+
self_unique_constraints = [
214+
ml_pred_to_bool(constraint) for constraint in self.constraints if constraint not in other.constraints
215+
]
216+
other_unique_constraints = [
217+
ml_pred_to_bool(constraint) for constraint in other.constraints if constraint not in self.constraints
218+
]
219219
disjunct_lhs = andBool([self_subst.pred] + self_unique_constraints)
220220
disjunct_rhs = andBool([other_subst.pred] + other_unique_constraints)
221221
if KToken('true', 'Bool') not in [disjunct_lhs, disjunct_rhs]:

pyk/src/pyk/kcfg/kcfg.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,7 @@ def log(message: str, *, warning: bool = False) -> None:
568568
result_info_message = f': {result_info}' if result_info else ''
569569
_LOGGER.log(
570570
logging.WARNING if warning else logging.INFO,
571-
f'Extending current KCFG with the following: {message}{result_info_message}',
571+
f'Extending KCFG with: {message}{result_info_message}',
572572
)
573573

574574
match extend_result:
@@ -711,9 +711,14 @@ def to_json(self) -> str:
711711
def from_json(s: str, optimize_memory: bool = True) -> KCFG:
712712
return KCFG.from_dict(json.loads(s), optimize_memory=optimize_memory)
713713

714-
def to_rules(self, _id: str | None = None, priority: int = 20) -> list[KRuleLike]:
714+
def to_rules(
715+
self,
716+
_id: str | None = None,
717+
priority: int = 20,
718+
defunc_with: KDefinition | None = None,
719+
) -> list[KRuleLike]:
715720
_id = 'BASIC-BLOCK' if _id is None else _id
716-
return [e.to_rule(_id, priority=priority) for e in self.edges()] + [
721+
return [e.to_rule(_id, priority=priority, defunc_with=defunc_with) for e in self.edges()] + [
717722
m.to_rule(_id, priority=priority) for m in self.merged_edges()
718723
]
719724

@@ -723,9 +728,12 @@ def to_module(
723728
imports: Iterable[KImport] = (),
724729
priority: int = 20,
725730
att: KAtt = EMPTY_ATT,
731+
defunc_with: KDefinition | None = None,
726732
) -> KFlatModule:
727733
module_name = 'KCFG' if module_name is None else module_name
728-
return KFlatModule(module_name, self.to_rules(priority=priority), imports=imports, att=att)
734+
return KFlatModule(
735+
module_name, self.to_rules(priority=priority, defunc_with=defunc_with), imports=imports, att=att
736+
)
729737

730738
def _resolve_or_none(self, id_like: NodeIdLike) -> int | None:
731739
if type(id_like) is int:

pyk/src/pyk/kcfg/show.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
from ..cterm import CSubst
3131
from ..kast import KInner
32-
from ..kast.outer import KFlatModule, KSentence
32+
from ..kast.outer import KDefinition, KFlatModule, KImport, KSentence
3333
from ..ktool.kprint import KPrint
3434
from .kcfg import NodeIdLike
3535

@@ -311,6 +311,8 @@ def to_module(
311311
module_name: str | None = None,
312312
omit_cells: Iterable[str] = (),
313313
parseable_output: bool = True,
314+
defunc_with: KDefinition | None = None,
315+
imports: Iterable[KImport] = (),
314316
) -> KFlatModule:
315317
def _process_sentence(sent: KSentence) -> KSentence:
316318
if type(sent) is KRule:
@@ -320,7 +322,7 @@ def _process_sentence(sent: KSentence) -> KSentence:
320322
sent = minimize_rule_like(sent)
321323
return sent
322324

323-
module = cfg.to_module(module_name)
325+
module = cfg.to_module(module_name, defunc_with=defunc_with, imports=imports)
324326
return module.let(sentences=[_process_sentence(sent) for sent in module.sentences])
325327

326328
def show(

0 commit comments

Comments
 (0)