1from __future__ import annotations
2
3from typing import TYPE_CHECKING
4
5from .syntax import And, App, EVar, MLQuant, Top
6
7if TYPE_CHECKING:
8 from collections.abc import Collection
9
10 from .syntax import Pattern
11
12
[docs]
13def conjuncts(pattern: Pattern) -> tuple[Pattern, ...]:
14 if isinstance(pattern, Top):
15 return ()
16 if isinstance(pattern, And):
17 return tuple(conjunct for op in pattern.ops for conjunct in conjuncts(op))
18 return (pattern,)
19
20
[docs]
21def free_occs(pattern: Pattern, *, bound_vars: Collection[str] = ()) -> dict[str, list[EVar]]:
22 occurrences: dict[str, list[EVar]] = {}
23
24 def collect(pattern: Pattern, bound_vars: set[str]) -> None:
25 if type(pattern) is EVar and pattern.name not in bound_vars:
26 if pattern.name in occurrences:
27 occurrences[pattern.name].append(pattern)
28 else:
29 occurrences[pattern.name] = [pattern]
30
31 elif isinstance(pattern, MLQuant):
32 new_bound_vars = {pattern.var.name}.union(bound_vars)
33 collect(pattern.pattern, new_bound_vars)
34
35 else:
36 for sub_pattern in pattern.patterns:
37 collect(sub_pattern, bound_vars)
38
39 collect(pattern, set(bound_vars))
40 return occurrences
41
42
[docs]
43def collect_symbols(pattern: Pattern) -> set[str]:
44 """Return the set of all symbols referred to in a pattern.
45
46 Args:
47 pattern: Pattern to collect symbols from.
48 """
49 res: set[str] = set()
50
51 def add_symbol(pattern: Pattern) -> None:
52 match pattern:
53 case App(symbol):
54 res.add(symbol)
55
56 pattern.collect(add_symbol)
57 return res