Skip to content

Commit 068296e

Browse files
committed
Add function inj_module
1 parent fbc4ff2 commit 068296e

File tree

1 file changed

+63
-2
lines changed

1 file changed

+63
-2
lines changed

pyk/src/pyk/k2lean4/k2lean4.py

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,28 @@
1010
from ..kore.internal import CollectionKind
1111
from ..kore.syntax import SortApp
1212
from ..utils import POSet
13-
from .model import Ctor, ExplBinder, Inductive, Module, Mutual, Signature, Term
13+
from .model import (
14+
Alt,
15+
AltsFieldVal,
16+
Ctor,
17+
ExplBinder,
18+
Inductive,
19+
Instance,
20+
InstField,
21+
Module,
22+
Mutual,
23+
Signature,
24+
SimpleFieldVal,
25+
StructVal,
26+
Term,
27+
)
1428

1529
if TYPE_CHECKING:
1630
from typing import Final
1731

1832
from ..kore.internal import KoreDefn
1933
from ..kore.syntax import SymbolDecl
20-
from .model import Command, Declaration
34+
from .model import Command, Declaration, FieldVal
2135

2236

2337
_VALID_LEAN_IDENT: Final = re.compile(
@@ -113,6 +127,53 @@ def _collection(self, sort: str) -> Inductive:
113127
ctor = Ctor('mk', Signature((ExplBinder(('coll',), val),), Term(sort)))
114128
return Inductive(sort, Signature((), Term('Type')), ctors=(ctor,))
115129

130+
def inj_module(self) -> Module:
131+
return Module(commands=self._inj_commands())
132+
133+
def _inj_commands(self) -> tuple[Command, ...]:
134+
return tuple(
135+
self._inj_instance(subsort, supersort)
136+
for supersort, subsorts in self.defn.subsorts.items()
137+
for subsort in subsorts
138+
if not supersort.endswith('CellMap') # Strangely, cell collections can be injected from their value sort in KORE
139+
)
140+
141+
def _inj_instance(self, subsort: str, supersort: str) -> Instance:
142+
ty = Term(f'Inj {subsort} {supersort}')
143+
field = self._inj_field(subsort, supersort)
144+
return Instance(Signature((), ty), StructVal((field,)))
145+
146+
def _inj_field(self, subsort: str, supersort: str) -> InstField:
147+
val = self._inj_val(subsort, supersort)
148+
return InstField('inj', val)
149+
150+
def _inj_val(self, subsort: str, supersort: str) -> FieldVal:
151+
subsubsorts: list[str]
152+
if subsort.endswith('CellMap'):
153+
subsubsorts = [] # Disregard injection from value sort to cell map sort
154+
else:
155+
subsubsorts = sorted(self.defn.subsorts.get(subsort, []))
156+
157+
if not subsubsorts:
158+
return SimpleFieldVal(Term(f'{supersort}.inj_{subsort}'))
159+
else:
160+
return AltsFieldVal(self._inj_alts(subsort, supersort, subsubsorts))
161+
162+
def _inj_alts(self, subsort: str, supersort: str, subsubsorts: list[str]) -> list[Alt]:
163+
def inj(subsort: str, supersort: str, x: str) -> Term:
164+
return Term(f'{supersort}.inj_{subsort} {x}')
165+
166+
res = []
167+
for subsubsort in subsubsorts:
168+
res.append(Alt((inj(subsubsort, subsort, 'x'),), inj(subsubsort, supersort, 'x')))
169+
170+
if self.defn.constructors.get(subsort, []):
171+
# Has actual constructors, not only subsorts
172+
default = Alt((Term('x'),), inj(subsort, supersort, 'x'))
173+
res.append(default)
174+
175+
return res
176+
116177

117178
def _param_sorts(decl: SymbolDecl) -> list[str]:
118179
from ..utils import check_type

0 commit comments

Comments
 (0)