Skip to content

Commit

Permalink
Generate an abbrev for each collection
Browse files Browse the repository at this point in the history
  • Loading branch information
tothtamas28 committed Dec 20, 2024
1 parent e97e68c commit 0269c7a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
27 changes: 24 additions & 3 deletions pyk/src/pyk/k2lean4/k2lean4.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
from dataclasses import dataclass
from typing import TYPE_CHECKING

from ..kore.internal import CollectionKind
from ..kore.syntax import SortApp
from ..utils import check_type
from .model import Ctor, ExplBinder, Inductive, Module, Signature, Term
from .model import Abbrev, Ctor, ExplBinder, Inductive, Module, Signature, Term

if TYPE_CHECKING:
from ..kore.internal import KoreDefn
Expand All @@ -19,7 +20,8 @@ class K2Lean4:
def sort_module(self) -> Module:
commands = []
commands += self._inductives()
return Module(commands=self._inductives())
commands += self._collections()
return Module(commands=commands)

def _inductives(self) -> list[Command]:
def is_inductive(sort: str) -> bool:
Expand All @@ -41,7 +43,26 @@ def _inj_ctor(self, sort: str, subsort: str) -> Ctor:
return Ctor(f'inj_{subsort}', Signature((ExplBinder(('x',), Term(subsort)),), Term(sort)))

def _symbol_ctor(self, sort: str, symbol: str) -> Ctor:
param_sorts = (check_type(sort, SortApp).name for sort in self.defn.symbols[symbol].param_sorts)
param_sorts = (
check_type(sort, SortApp).name for sort in self.defn.symbols[symbol].param_sorts
) # TODO eliminate check_type
binders = tuple(ExplBinder((f'x{i}',), Term(sort)) for i, sort in enumerate(param_sorts))
symbol = symbol.replace('-', '_')
return Ctor(symbol, Signature(binders, Term(sort)))

def _collections(self) -> list[Command]:
return [self._collection(sort) for sort in sorted(self.defn.collections)]

def _collection(self, sort: str) -> Abbrev:
coll = self.defn.collections[sort]
elem = self.defn.symbols[coll.element]
sorts = ' '.join(check_type(sort, SortApp).name for sort in elem.param_sorts) # TODO eliminate check_type
assert sorts
match coll.kind:
case CollectionKind.LIST:
val = Term(f'ListHook {sorts}')
case CollectionKind.MAP:
val = Term(f'MapHook {sorts}')
case CollectionKind.SET:
val = Term(f'SetHook {sorts}')
return Abbrev(sort, val, Signature((), Term('Type')))
8 changes: 7 additions & 1 deletion pyk/src/pyk/kore/internal.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,13 @@ def _config_symbols(self) -> set[str]:
if sort in done:
continue
done.add(sort)
symbols = self.constructors.get(sort, ())

symbols: list[str] = []
if sort in self.collections:
coll = self.collections[sort]
symbols += (coll.concat, coll.element, coll.unit)
symbols += self.constructors.get(sort, ())

pending.extend(sort for symbol in symbols for sort in self._symbol_sorts(symbol))
res.update(symbols)
return res
Expand Down

0 comments on commit 0269c7a

Please sign in to comment.