Skip to content

Commit 1f7f4a9

Browse files
authored
Allow special characters in generated Lean 4 identifiers (#4729)
Closes #4722
1 parent 8c40191 commit 1f7f4a9

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

pyk/src/pyk/k2lean4/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .k2lean4 import K2Lean4

pyk/src/pyk/k2lean4/k2lean4.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,27 @@
11
from __future__ import annotations
22

3+
import re
34
from dataclasses import dataclass
45
from typing import TYPE_CHECKING
56

7+
from ..konvert import unmunge
68
from ..kore.internal import CollectionKind
79
from ..kore.syntax import SortApp
810
from ..utils import check_type
911
from .model import Abbrev, Ctor, ExplBinder, Inductive, Module, Signature, Term
1012

1113
if TYPE_CHECKING:
14+
from typing import Final
15+
1216
from ..kore.internal import KoreDefn
1317
from .model import Command
1418

1519

20+
_VALID_LEAN_IDENT: Final = re.compile(
21+
"_[a-zA-Z0-9_?!']+|[a-zA-Z][a-zA-Z0-9_?!']*"
22+
) # Simplified to characters permitted in KORE in the first place
23+
24+
1625
@dataclass(frozen=True)
1726
class K2Lean4:
1827
defn: KoreDefn
@@ -46,10 +55,19 @@ def _symbol_ctor(self, sort: str, symbol: str) -> Ctor:
4655
param_sorts = (
4756
check_type(sort, SortApp).name for sort in self.defn.symbols[symbol].param_sorts
4857
) # TODO eliminate check_type
58+
symbol = self._symbol_ident(symbol)
4959
binders = tuple(ExplBinder((f'x{i}',), Term(sort)) for i, sort in enumerate(param_sorts))
50-
symbol = symbol.replace('-', '_')
5160
return Ctor(symbol, Signature(binders, Term(sort)))
5261

62+
@staticmethod
63+
def _symbol_ident(symbol: str) -> str:
64+
if symbol.startswith('Lbl'):
65+
symbol = symbol[3:]
66+
symbol = unmunge(symbol)
67+
if not _VALID_LEAN_IDENT.fullmatch(symbol):
68+
symbol = f'«{symbol}»'
69+
return symbol
70+
5371
def _collections(self) -> list[Command]:
5472
return [self._collection(sort) for sort in sorted(self.defn.collections)]
5573

pyk/src/pyk/k2lean4/model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def __init__(
7373
def __str__(self) -> str:
7474
modifiers = f'{self.modifiers} ' if self.modifiers else ''
7575
signature = f' {self.signature}' if self.signature else ''
76-
return f'{modifiers} abbrev {self.ident}{signature} := {self.val}'
76+
return f'{modifiers}abbrev {self.ident}{signature} := {self.val}'
7777

7878

7979
@final

0 commit comments

Comments
 (0)