Skip to content
This repository was archived by the owner on Apr 25, 2024. It is now read-only.

Commit 8b0b6d7

Browse files
ehildenbrv-auditor
andauthored
Remove manip.get_cell, add CTerm.free_vars (#1040)
Inspired while reviewing: #1009 This makes two changes which hopefully tighten the `CTerm` abstraction slightly by reducing the places where `CTerm.kast` is used: - Places that were doing `get_cell(CTerm.kast, CELL_NAME)` now say `CTerm.cell(CELL_NAME)`. There are no more uses of `get_cell` here or in downstream repos, so it's removed. - A new `@cached_property CTerm.free_vars` is added, which is used to replace the pattern `free_vars(CTerm.kast)`. This means that the remaining uses of `CTerm.kast` are doing something weird with the resulting kast, or printing it, or converting it to Kore. --------- Co-authored-by: devops <[email protected]>
1 parent ed553c5 commit 8b0b6d7

File tree

9 files changed

+21
-24
lines changed

9 files changed

+21
-24
lines changed

docs/conf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
project = 'pyk'
1010
author = 'Runtime Verification, Inc'
1111
copyright = '2024, Runtime Verification, Inc'
12-
version = '0.1.754'
13-
release = '0.1.754'
12+
version = '0.1.755'
13+
release = '0.1.755'
1414

1515
# -- General configuration ---------------------------------------------------
1616
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration

package/version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.1.754
1+
0.1.755

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"
44

55
[tool.poetry]
66
name = "pyk"
7-
version = "0.1.754"
7+
version = "0.1.755"
88
description = ""
99
authors = [
1010
"Runtime Verification, Inc. <[email protected]>",

src/pyk/cterm/cterm.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,11 @@ def kast(self) -> KInner:
148148
"""Return the unstructured bare `KInner` representation of a `CTerm` (see `CTerm.from_kast`)."""
149149
return mlAnd(self, GENERATED_TOP_CELL)
150150

151+
@cached_property
152+
def free_vars(self) -> frozenset[str]:
153+
"""Return the set of free variable names contained in this `CTerm`"""
154+
return frozenset(free_vars(self.kast))
155+
151156
@property
152157
def hash(self) -> str:
153158
"""Unique hash representing the contents of this `CTerm`."""
@@ -235,7 +240,7 @@ def anti_unify(
235240
new_cterm = new_cterm.add_constraint(mlEqualsTrue(orBool([disjunct_lhs, disjunct_rhs])))
236241

237242
new_constraints = []
238-
fvs = free_vars(new_cterm.kast)
243+
fvs = list(new_cterm.free_vars)
239244
len_fvs = 0
240245
while len_fvs < len(fvs):
241246
len_fvs = len(fvs)

src/pyk/cterm/symbolic.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from ..cterm import CSubst, CTerm
99
from ..kast.inner import KApply, KLabel, KRewrite, KVariable, Subst
10-
from ..kast.manip import flatten_label, free_vars, sort_ac_collections
10+
from ..kast.manip import flatten_label, sort_ac_collections
1111
from ..kast.pretty import PrettyPrinter
1212
from ..konvert import kast_to_kore, kore_to_kast
1313
from ..kore.rpc import (
@@ -186,8 +186,7 @@ def implies(
186186
) -> CTermImplies:
187187
_LOGGER.debug(f'Checking implication: {antecedent} #Implies {consequent}')
188188
_consequent = consequent.kast
189-
fv_antecedent = free_vars(antecedent.kast)
190-
unbound_consequent = [v for v in free_vars(_consequent) if v not in fv_antecedent]
189+
unbound_consequent = [v for v in consequent.free_vars if v not in antecedent.free_vars]
191190
if len(unbound_consequent) > 0:
192191
bind_text, bind_label = ('existentially', '#Exists')
193192
if bind_universally:

src/pyk/kast/manip.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -565,12 +565,6 @@ def is_anon_var(kast: KInner) -> bool:
565565
return type(kast) is KVariable and kast.name.startswith('_')
566566

567567

568-
def get_cell(constrained_term: KInner, cell_variable: str) -> KInner:
569-
state, _ = split_config_and_constraints(constrained_term)
570-
_, subst = split_config_from(state)
571-
return subst[cell_variable]
572-
573-
574568
def set_cell(constrained_term: KInner, cell_variable: str, cell_value: KInner) -> KInner:
575569
state, constraint = split_config_and_constraints(constrained_term)
576570
config, subst = split_config_from(state)

src/pyk/kcfg/kcfg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -932,7 +932,7 @@ def lift_split(self, id: NodeIdLike) -> None:
932932
split_from_b = single(self.splits(source_id=id))
933933
ci, csubsts = list(split_from_b.splits.keys()), list(split_from_b.splits.values())
934934
# If any of the `cond_I` contains variables not present in `A`, the lift cannot be performed soundly.
935-
fv_a = set(free_vars(a.cterm.kast))
935+
fv_a = a.cterm.free_vars
936936
for subst in csubsts:
937937
assert set(free_vars(mlAnd(subst.constraints))).issubset(
938938
fv_a

src/tests/integration/cterm/test_simple.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
from pyk.cterm import CTerm
88
from pyk.kast.inner import KApply, KSequence, KToken, KVariable
9-
from pyk.kast.manip import get_cell
109
from pyk.prelude.ml import mlEqualsTrue, mlTop
1110
from pyk.testing import CTermSymbolicTest, KPrintTest
1211

@@ -116,8 +115,8 @@ def test_simplify(
116115

117116
# When
118117
actual_post, _logs = cterm_symbolic.simplify(self.config(kprint, *pre))
119-
actual_k = kprint.pretty_print(get_cell(actual_post.kast, 'K_CELL'))
120-
actual_state = kprint.pretty_print(get_cell(actual_post.kast, 'STATE_CELL'))
118+
actual_k = kprint.pretty_print(actual_post.cell('K_CELL'))
119+
actual_state = kprint.pretty_print(actual_post.cell('STATE_CELL'))
121120

122121
# Then
123122
assert actual_k == expected_k

src/tests/integration/proof/test_imp.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from pyk.cterm import CSubst, CTerm
1010
from pyk.kast.inner import KApply, KSequence, KSort, KToken, KVariable, Subst
11-
from pyk.kast.manip import get_cell, minimize_term
11+
from pyk.kast.manip import minimize_term
1212
from pyk.kcfg.semantics import KCFGSemantics
1313
from pyk.kcfg.show import KCFGShow
1414
from pyk.prelude.kbool import BOOL, andBool, notBool, orBool
@@ -1208,7 +1208,7 @@ def test_anti_unify_forget_values(
12081208

12091209
anti_unifier, subst1, subst2 = cterm1.anti_unify(cterm2, keep_values=False, kdef=kprove.definition)
12101210

1211-
k_cell = get_cell(anti_unifier.kast, 'STATE_CELL')
1211+
k_cell = anti_unifier.cell('STATE_CELL')
12121212
assert type(k_cell) is KApply
12131213
assert k_cell.label.name == '_|->_'
12141214
assert type(k_cell.args[1]) is KVariable
@@ -1221,7 +1221,7 @@ def test_anti_unify_forget_values(
12211221
constraint=mlEqualsTrue(KApply('_>Int_', [KVariable('N', 'Int'), KToken('1', 'Int')])),
12221222
)
12231223

1224-
assert anti_unifier.kast == expected_anti_unifier.kast
1224+
assert anti_unifier == expected_anti_unifier
12251225

12261226
def test_anti_unify_keep_values(
12271227
self,
@@ -1259,7 +1259,7 @@ def test_anti_unify_keep_values(
12591259

12601260
anti_unifier, subst1, subst2 = cterm1.anti_unify(cterm2, keep_values=True, kdef=kprove.definition)
12611261

1262-
k_cell = get_cell(anti_unifier.kast, 'STATE_CELL')
1262+
k_cell = anti_unifier.cell('STATE_CELL')
12631263
assert type(k_cell) is KApply
12641264
assert k_cell.label.name == '_|->_'
12651265
assert type(k_cell.args[1]) is KVariable
@@ -1296,7 +1296,7 @@ def test_anti_unify_keep_values(
12961296
),
12971297
)
12981298

1299-
assert anti_unifier.kast == expected_anti_unifier.kast
1299+
assert anti_unifier == expected_anti_unifier
13001300

13011301
def test_anti_unify_subst_true(
13021302
self,
@@ -1318,7 +1318,7 @@ def test_anti_unify_subst_true(
13181318

13191319
anti_unifier, _, _ = cterm1.anti_unify(cterm2, keep_values=True, kdef=kprove.definition)
13201320

1321-
assert anti_unifier.kast == cterm1.kast
1321+
assert anti_unifier == cterm1
13221322

13231323
@pytest.mark.parametrize(
13241324
'test_id,antecedent,consequent,expected',

0 commit comments

Comments
 (0)