|
10 | 10 | from ..kore.internal import CollectionKind |
11 | 11 | from ..kore.syntax import SortApp |
12 | 12 | 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 | +) |
14 | 28 |
|
15 | 29 | if TYPE_CHECKING: |
16 | 30 | from typing import Final |
17 | 31 |
|
18 | 32 | from ..kore.internal import KoreDefn |
19 | 33 | from ..kore.syntax import SymbolDecl |
20 | | - from .model import Command, Declaration |
| 34 | + from .model import Command, Declaration, FieldVal |
21 | 35 |
|
22 | 36 |
|
23 | 37 | _VALID_LEAN_IDENT: Final = re.compile( |
@@ -113,6 +127,53 @@ def _collection(self, sort: str) -> Inductive: |
113 | 127 | ctor = Ctor('mk', Signature((ExplBinder(('coll',), val),), Term(sort))) |
114 | 128 | return Inductive(sort, Signature((), Term('Type')), ctors=(ctor,)) |
115 | 129 |
|
| 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 | + |
116 | 177 |
|
117 | 178 | def _param_sorts(decl: SymbolDecl) -> list[str]: |
118 | 179 | from ..utils import check_type |
|
0 commit comments