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

Commit fa9051a

Browse files
ehildenbrv-auditor
andauthored
Implement first past of KFlatModule conversion to Kore (#835)
This PR is to begin a refactoring needed for CSE, and also begin work on implementing the skeleton of ModuleToKORE in pyk. - A new routine is added to `konvert` for `kflatmodule_to_kore`, which converts modules which contain only rules and imports to Kore. - This routine is used for building up the dependencies and circularities modules in initializing the `APRProver`, and then putting them into the `add_module` endpoint directly. - The code `KCFG.as_claim` is turned into a simpler `KCFG.as_rule`, which uses existing routines instead of making a new one. This uses the `cterm` modules `build_rule` functionality, which is adjusted to not strip out information needed for making backend-compatible rules (previously this routine was used for display purposes only). - Old code for handling the dependencies/circularities modules in `APRProver` is removed. --------- Co-authored-by: devops <[email protected]>
1 parent 32d93a5 commit fa9051a

File tree

7 files changed

+50
-63
lines changed

7 files changed

+50
-63
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.592'
13-
release = '0.1.592'
12+
version = '0.1.593'
13+
release = '0.1.593'
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.592
1+
0.1.593

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.592"
7+
version = "0.1.593"
88
description = ""
99
authors = [
1010
"Runtime Verification, Inc. <[email protected]>",

src/pyk/cterm.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@
1313
count_vars,
1414
flatten_label,
1515
free_vars,
16-
minimize_rule,
1716
ml_pred_to_bool,
1817
push_down_rewrites,
19-
remove_generated_cells,
2018
simplify_bool,
2119
split_config_and_constraints,
2220
split_config_from,
@@ -429,13 +427,13 @@ def build_rule(
429427
(init_config, init_constraint) = split_config_and_constraints(init_term)
430428
(final_config, final_constraint) = split_config_and_constraints(final_term)
431429

432-
rule_body = remove_generated_cells(push_down_rewrites(KRewrite(init_config, final_config)))
430+
rule_body = push_down_rewrites(KRewrite(init_config, final_config))
433431
rule_requires = simplify_bool(ml_pred_to_bool(init_constraint))
434432
rule_ensures = simplify_bool(ml_pred_to_bool(final_constraint))
435433
att_dict = {} if priority is None else {'priority': str(priority)}
436434
rule_att = KAtt(atts=att_dict)
437435

438436
rule = KRule(rule_body, requires=rule_requires, ensures=rule_ensures, att=rule_att)
439437
rule = rule.update_atts({'label': rule_id})
440-
new_keep_vars = [v_subst[v].name if v in v_subst else v for v in keep_vars]
441-
return (minimize_rule(rule, keep_vars=new_keep_vars), Subst(vremap_subst))
438+
439+
return (rule, Subst(vremap_subst))

src/pyk/kcfg/explore.py

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@
1818
ml_pred_to_bool,
1919
push_down_rewrites,
2020
)
21-
from ..kast.outer import KRule
22-
from ..konvert import krule_to_kore
2321
from ..kore.rpc import AbortedResult, RewriteSuccess, SatResult, StopReason, UnknownResult, UnsatResult
24-
from ..kore.syntax import Import, Module
2522
from ..prelude import k
2623
from ..prelude.k import GENERATED_TOP_CELL
2724
from ..prelude.kbool import notBool
@@ -36,10 +33,8 @@
3633
from typing import Final
3734

3835
from ..kast import KInner
39-
from ..kast.outer import KClaim
4036
from ..kcfg.exploration import KCFGExploration
4137
from ..kore.rpc import KoreClient, LogEntry
42-
from ..kore.syntax import Sentence
4338
from ..ktool.kprint import KPrint
4439
from .kcfg import NodeIdLike
4540
from .semantics import KCFGSemantics
@@ -525,22 +520,6 @@ def extract_rule_labels(_logs: tuple[LogEntry, ...]) -> list[str]:
525520
case _:
526521
raise AssertionError()
527522

528-
def add_dependencies_module(
529-
self, old_module_name: str, new_module_name: str, dependencies: Iterable[KClaim], priority: int = 1
530-
) -> None:
531-
kast_rules = [
532-
KRule(body=c.body, requires=c.requires, ensures=c.ensures, att=c.att.update({'priority': priority}))
533-
for c in dependencies
534-
]
535-
kore_axioms: list[Sentence] = [
536-
krule_to_kore(self.kprint.definition, self.kprint.kompiled_kore, r) for r in kast_rules
537-
]
538-
sentences: list[Sentence] = [Import(module_name=old_module_name, attrs=())]
539-
sentences = sentences + kore_axioms
540-
m = Module(name=new_module_name, sentences=sentences)
541-
_LOGGER.info(f'Adding dependencies module {self.id}: {new_module_name}')
542-
self._kore_client.add_module(m)
543-
544523

545524
class ExtendResult(ABC):
546525
...

src/pyk/konvert.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from .cterm import CTerm
88
from .kast.inner import KApply, KLabel, KSequence, KSort, KToken, KVariable
99
from .kast.manip import bool_to_ml_pred, extract_lhs, extract_rhs
10+
from .kast.outer import KRule
1011
from .kore.prelude import BYTES as KORE_BYTES
1112
from .kore.prelude import DOTK, SORT_K
1213
from .kore.prelude import STRING as KORE_STRING
@@ -22,8 +23,10 @@
2223
EVar,
2324
Exists,
2425
Implies,
26+
Import,
2527
MLPattern,
2628
MLQuant,
29+
Module,
2730
Not,
2831
Rewrites,
2932
SortApp,
@@ -41,9 +44,9 @@
4144
from typing import Final
4245

4346
from .kast import KInner
44-
from .kast.outer import KDefinition, KRule
47+
from .kast.outer import KDefinition, KFlatModule, KImport
4548
from .kore.kompiled import KompiledKore
46-
from .kore.syntax import Pattern, Sort
49+
from .kore.syntax import Pattern, Sentence, Sort
4750

4851
_LOGGER: Final = logging.getLogger(__name__)
4952

@@ -138,6 +141,20 @@ def krule_to_kore(kast_defn: KDefinition, kompiled_kore: KompiledKore, krule: KR
138141
return axiom
139142

140143

144+
def kflatmodule_to_kore(kast_defn: KDefinition, kompiled_kore: KompiledKore, kflatmodule: KFlatModule) -> Module:
145+
kore_axioms: list[Sentence] = []
146+
for sent in kflatmodule.sentences:
147+
if type(sent) is not KRule:
148+
raise ValueError(f'Cannot convert sentence to Kore: {sent}')
149+
kore_axioms.append(krule_to_kore(kast_defn, kompiled_kore, sent))
150+
imports: list[Sentence] = [_kimport_to_kore(kimport) for kimport in kflatmodule.imports]
151+
return Module(name=kflatmodule.name, sentences=(imports + kore_axioms))
152+
153+
154+
def _kimport_to_kore(kimport: KImport) -> Import:
155+
return Import(module_name=kimport.name, attrs=())
156+
157+
141158
def _ksort_to_kore(ksort: KSort) -> SortApp:
142159
return SortApp('Sort' + ksort.name)
143160

src/pyk/proof/reachability.py

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@
33
import graphlib
44
import json
55
import logging
6+
import re
67
from dataclasses import dataclass
78
from typing import TYPE_CHECKING
89

910
from pyk.kore.rpc import LogEntry
1011

11-
from ..kast.inner import KInner, KRewrite, KSort, Subst
12+
from ..kast.inner import KInner, Subst
1213
from ..kast.manip import flatten_label, ml_pred_to_bool
13-
from ..kast.outer import KClaim
14+
from ..kast.outer import KFlatModule, KImport, KRule
1415
from ..kcfg import KCFG
1516
from ..kcfg.exploration import KCFGExploration
17+
from ..konvert import kflatmodule_to_kore
1618
from ..prelude.kbool import BOOL, TRUE
1719
from ..prelude.ml import mlAnd, mlEquals, mlTop
1820
from ..utils import FrozenDict, ensure_dir_path, hash_str, shorten_hashes, single
@@ -24,11 +26,9 @@
2426
from pathlib import Path
2527
from typing import Any, Final, TypeVar
2628

27-
from ..cterm import CTerm
28-
from ..kast.outer import KDefinition, KFlatModuleList
29+
from ..kast.outer import KClaim, KDefinition, KFlatModuleList
2930
from ..kcfg import KCFGExplore
3031
from ..kcfg.kcfg import NodeIdLike
31-
from ..ktool.kprint import KPrint
3232

3333
T = TypeVar('T', bound='Proof')
3434

@@ -206,17 +206,11 @@ def from_claim(
206206
**kwargs,
207207
)
208208

209-
def as_claim(self, kprint: KPrint) -> KClaim:
210-
fr: CTerm = self.kcfg.node(self.init).cterm
211-
to: CTerm = self.kcfg.node(self.target).cterm
212-
fr_config_sorted = kprint.definition.sort_vars(fr.config, sort=KSort('GeneratedTopCell'))
213-
to_config_sorted = kprint.definition.sort_vars(to.config, sort=KSort('GeneratedTopCell'))
214-
kc = KClaim(
215-
body=KRewrite(fr_config_sorted, to_config_sorted),
216-
requires=ml_pred_to_bool(mlAnd(fr.constraints)),
217-
ensures=ml_pred_to_bool(mlAnd(to.constraints)),
218-
)
219-
return kc
209+
def as_rule(self, priority: int = 20) -> KRule:
210+
_edge = KCFG.Edge(self.kcfg.node(self.init), self.kcfg.node(self.target), depth=0, rules=())
211+
_rule = _edge.to_rule('BASIC-BLOCK', priority=priority)
212+
assert type(_rule) is KRule
213+
return _rule
220214

221215
@staticmethod
222216
def from_spec_modules(
@@ -644,6 +638,13 @@ def __init__(
644638
always_check_subsumption: bool = True,
645639
fast_check_subsumption: bool = False,
646640
) -> None:
641+
def _inject_module(module_name: str, import_name: str, sentences: list[KRule]) -> None:
642+
_module = KFlatModule(module_name, sentences, [KImport(import_name)])
643+
_kore_module = kflatmodule_to_kore(
644+
self.kcfg_explore.kprint.definition, self.kcfg_explore.kprint.kompiled_kore, _module
645+
)
646+
self.kcfg_explore._kore_client.add_module(_kore_module)
647+
647648
super().__init__(kcfg_explore)
648649
self.proof = proof
649650
self.main_module_name = self.kcfg_explore.kprint.definition.main_module_name
@@ -659,22 +660,14 @@ def __init__(
659660

660661
apr_subproofs: list[APRProof] = [pf for pf in subproofs if isinstance(pf, APRProof)]
661662

662-
dependencies_as_claims: list[KClaim] = [d.as_claim(self.kcfg_explore.kprint) for d in apr_subproofs]
663+
dependencies_as_rules: list[KRule] = [d.as_rule(priority=20) for d in apr_subproofs]
664+
circularity_rule = proof.as_rule(priority=20)
663665

664-
self.dependencies_module_name = self.main_module_name + '-DEPENDS-MODULE'
665-
self.kcfg_explore.add_dependencies_module(
666-
self.main_module_name,
667-
self.dependencies_module_name,
668-
dependencies_as_claims,
669-
priority=1,
670-
)
671-
self.circularities_module_name = self.main_module_name + '-CIRCULARITIES-MODULE'
672-
self.kcfg_explore.add_dependencies_module(
673-
self.main_module_name,
674-
self.circularities_module_name,
675-
dependencies_as_claims + ([proof.as_claim(self.kcfg_explore.kprint)] if proof.circularity else []),
676-
priority=1,
677-
)
666+
module_name = re.sub(r'[%().:,]+', '-', self.proof.id.upper())
667+
self.dependencies_module_name = module_name + '-DEPENDS-MODULE'
668+
self.circularities_module_name = module_name + '-CIRCULARITIES-MODULE'
669+
_inject_module(self.dependencies_module_name, self.main_module_name, dependencies_as_rules)
670+
_inject_module(self.circularities_module_name, self.dependencies_module_name, [circularity_rule])
678671

679672
self._checked_for_terminal = set()
680673
self._checked_for_subsumption = set()

0 commit comments

Comments
 (0)