|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
| 3 | +import re |
3 | 4 | from dataclasses import dataclass |
4 | 5 | from typing import TYPE_CHECKING |
5 | 6 |
|
| 7 | +from ..konvert import unmunge |
6 | 8 | from ..kore.internal import CollectionKind |
7 | 9 | from ..kore.syntax import SortApp |
8 | 10 | from ..utils import check_type |
9 | 11 | from .model import Abbrev, Ctor, ExplBinder, Inductive, Module, Signature, Term |
10 | 12 |
|
11 | 13 | if TYPE_CHECKING: |
| 14 | + from typing import Final |
| 15 | + |
12 | 16 | from ..kore.internal import KoreDefn |
13 | 17 | from .model import Command |
14 | 18 |
|
15 | 19 |
|
| 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 | + |
16 | 25 | @dataclass(frozen=True) |
17 | 26 | class K2Lean4: |
18 | 27 | defn: KoreDefn |
@@ -46,10 +55,19 @@ def _symbol_ctor(self, sort: str, symbol: str) -> Ctor: |
46 | 55 | param_sorts = ( |
47 | 56 | check_type(sort, SortApp).name for sort in self.defn.symbols[symbol].param_sorts |
48 | 57 | ) # TODO eliminate check_type |
| 58 | + symbol = self._symbol_ident(symbol) |
49 | 59 | binders = tuple(ExplBinder((f'x{i}',), Term(sort)) for i, sort in enumerate(param_sorts)) |
50 | | - symbol = symbol.replace('-', '_') |
51 | 60 | return Ctor(symbol, Signature(binders, Term(sort))) |
52 | 61 |
|
| 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 | + |
53 | 71 | def _collections(self) -> list[Command]: |
54 | 72 | return [self._collection(sort) for sort in sorted(self.defn.collections)] |
55 | 73 |
|
|
0 commit comments