1from __future__ import annotations
2
3from typing import TYPE_CHECKING
4
5from ..kast.inner import KApply, KLabel, KSort, build_assoc
6
7if TYPE_CHECKING:
8 from collections.abc import Iterable
9 from typing import Final
10
11 from ..kast import KInner
12
13SET: Final = KSort('Set')
14LIST: Final = KSort('List')
15MAP: Final = KSort('Map')
16RANGEMAP: Final = KSort('RangeMap')
17BAG: Final = KSort('Bag')
18
19
[docs]
20def set_empty() -> KInner:
21 return KApply('.Set')
22
23
[docs]
24def set_item(k: KInner) -> KInner:
25 return KApply('SetItem', [k])
26
27
[docs]
28def set_of(ks: Iterable[KInner]) -> KInner:
29 return build_assoc(set_empty(), KLabel('_Set_'), map(set_item, ks))
30
31
[docs]
32def list_empty() -> KInner:
33 return KApply('.List')
34
35
[docs]
36def list_item(k: KInner) -> KInner:
37 return KApply('ListItem', [k])
38
39
[docs]
40def list_of(ks: Iterable[KInner]) -> KInner:
41 return build_assoc(list_empty(), KLabel('_List_'), map(list_item, ks))
42
43
[docs]
44def map_empty() -> KInner:
45 return KApply('.Map')
46
47
[docs]
48def map_item(k: KInner, v: KInner) -> KInner:
49 return KApply('_|->_', [k, v])
50
51
[docs]
52def map_of(ks: dict[KInner, KInner] | Iterable[tuple[KInner, KInner]]) -> KInner:
53 ks = dict(ks)
54 return build_assoc(map_empty(), KLabel('_Map_'), (map_item(k, v) for k, v in ks.items()))
55
56
[docs]
57def rangemap_empty() -> KInner:
58 return KApply('.RangeMap')
59
60
[docs]
61def rangemap_item(k: tuple[KInner, KInner], v: KInner) -> KInner:
62 return KApply('_r|->_', (KApply('RangeMap:Range', k), v))
63
64
[docs]
65def rangemap_of(ks: dict[tuple[KInner, KInner], KInner] | Iterable[tuple[tuple[KInner, KInner], KInner]]) -> KInner:
66 ks_dict: dict[tuple[KInner, KInner], KInner] = dict(ks)
67 return build_assoc(rangemap_empty(), KLabel('_RangeMap_'), (rangemap_item(k, v) for k, v in ks_dict.items()))