File size: 2,422 Bytes
e1ced61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"""``table_dsl_v1`` operator signature table — grammar only (``docs/02`` §7,
``docs/07`` §4 P2).

This third DSL covers lookups/aggregations over a table's rows and columns. P2
ships the **signature table + canonicalization profile** so the type checker and
canonicalizer cover the full ``dsl`` enum and so ChartQA's underlying table
(hashed in ``provenance.table_sha256``) has a target grammar. No per-source
compiler is implemented here: tables are not compiled deterministically in P2.
ChartQA is a C2/P4 model-compiled source — its program is proposed/verified by
the dual model families, not built in code. The compiler is therefore P4.
"""

from __future__ import annotations

from .typecheck import OpSignature

# DSL marker (the ``dsl`` enum value); kept for parity with the other DSL modules.
DSL = "table_dsl_v1"

# --- operator signature table ---------------------------------------------
#
# ``COL(label)`` selects a column (scalar); ``ROW(...)`` selects a row. ``LOOKUP``
# reads one cell. ``SUM``/``AVG`` are variadic-commutative aggregations over a
# column (set) of values; ``COUNT``/``EXIST`` are set selectors over a column.
SIGNATURES: dict[str, OpSignature] = {
    "COL": OpSignature(arg_types=("string",), return_type="column", selector_kind="scalar"),
    "ROW": OpSignature(arg_types=("string",), return_type="row", selector_kind="scalar"),
    "LOOKUP": OpSignature(arg_types=("row", "column"), return_type="value", selector_kind="none"),
    "SUM": OpSignature(
        arg_types=("value",),
        return_type="value",
        commutative=True,
        variadic=True,
        selector_kind="none",
    ),
    "AVG": OpSignature(
        arg_types=("value",),
        return_type="value",
        commutative=True,
        variadic=True,
        selector_kind="none",
    ),
    "COUNT": OpSignature(arg_types=("column",), return_type="integer", selector_kind="set"),
    "EXIST": OpSignature(arg_types=("column",), return_type="boolean", selector_kind="set"),
}

# Canonicalization profile (§7.2 steps 3–5). ``SUM``/``AVG`` are commutative;
# table aliases/casts are not introduced in P2 (the compiler is P4).
COMMUTATIVE_OPS: frozenset[str] = frozenset({"SUM", "AVG"})
ALIASES: dict[str, str] = {"GET_CELL": "LOOKUP", "TOTAL": "SUM"}
IDENTITY_CASTS: frozenset[str] = frozenset()

__all__ = [
    "ALIASES",
    "COMMUTATIVE_OPS",
    "DSL",
    "IDENTITY_CASTS",
    "SIGNATURES",
]