A Python representation of a Lean inductive constructor. Attributes: ctor: the constructor name (unqualified). tag: integer tag. fields: tuple of decoded Python field values in declaration order. Supports Python 3.10+ structural pattern matching:: matc
| 230 | |
| 231 | |
| 232 | class LeanInductiveValue: |
| 233 | """A Python representation of a Lean inductive constructor. |
| 234 | |
| 235 | Attributes: |
| 236 | ctor: the constructor name (unqualified). |
| 237 | tag: integer tag. |
| 238 | fields: tuple of decoded Python field values in declaration order. |
| 239 | |
| 240 | Supports Python 3.10+ structural pattern matching:: |
| 241 | |
| 242 | match name_value: |
| 243 | case Name.str(parent, leaf): |
| 244 | print(f"name: {leaf}") |
| 245 | case Name.anonymous(): |
| 246 | print("anonymous") |
| 247 | |
| 248 | Fields can also be accessed by index (``value._0``, ``value._1``, etc.). |
| 249 | """ |
| 250 | |
| 251 | __slots__ = ("ctor", "tag", "fields", "_type_name") |
| 252 | __match_args__ = ("ctor", "tag", "fields") |
| 253 | |
| 254 | def __init__(self, type_name: str, ctor: str, tag: int, fields: tuple) -> None: |
| 255 | self._type_name = type_name |
| 256 | self.ctor = ctor |
| 257 | self.tag = tag |
| 258 | self.fields = fields |
| 259 | |
| 260 | def __getattr__(self, name: str) -> Any: |
| 261 | if name.startswith("_") and name[1:].isdigit(): |
| 262 | idx = int(name[1:]) |
| 263 | if idx < len(self.fields): |
| 264 | return self.fields[idx] |
| 265 | raise AttributeError(f"field index {idx} out of range (have {len(self.fields)} fields)") |
| 266 | raise AttributeError(name) |
| 267 | |
| 268 | def __repr__(self) -> str: |
| 269 | if not self.fields: |
| 270 | return f"{self._type_name}.{self.ctor}" |
| 271 | return f"{self._type_name}.{self.ctor}({', '.join(repr(f) for f in self.fields)})" |
| 272 | |
| 273 | def __eq__(self, other: object) -> bool: |
| 274 | if isinstance(other, LeanInductiveValue): |
| 275 | return (self._type_name, self.tag, self.fields) == ( |
| 276 | other._type_name, |
| 277 | other.tag, |
| 278 | other.fields, |
| 279 | ) |
| 280 | if type(other) is _CtorMeta: |
| 281 | return ( |
| 282 | self._type_name == other._type_name # type: ignore[attr-defined] |
| 283 | and self.ctor == other._ctor_name # type: ignore[attr-defined] |
| 284 | and self.fields == () |
| 285 | ) |
| 286 | return NotImplemented |
| 287 | |
| 288 | def __hash__(self) -> int: |
| 289 | return hash((self._type_name, self.tag, self.fields)) |
no outgoing calls