core/tests/test_quantitative_comprehension.py
Shay a005a92fed feat(comprehend): arithmetic word-problems via binding_graph (5th domain, real admissibility)
The binding-graph's FIRST comprehension consumer (doctrine-aligned: quantities live
in binding_graph, NOT the MeaningGraph). generate/quantitative_comprehension.py
reads arithmetic prose into SymbolBinding/BoundFact/BoundEquation and runs the REAL
check_admissibility (shell -> verify -> rebuild with the actual UnitProof) — there
is NO stamped "admitted": an equation is admitted only if its operand units verify.
Then to_relational_metric projects the binding-graph to the independent
relational_metric oracle for the verdict.

Templates (digits only; non-digit quantity REFUSES):
  "<X> has <N> <unit>"                 -> BoundFact(X = N)
  "<Y> has <N> more <unit> than <X>"   -> BoundEquation(Y = X + N)  op=add
  "<Y> has <N> fewer <unit> than <X>"  -> BoundEquation(Y = X - N)  op=subtract
  "How many <unit> does <Y> have"      -> ask Y
  "How many <unit> do <X> and <Y> have"-> total = X + Y; ask total

Unit modelling (honest, not faked): a noun the closed en_units_v1 pack knows is
used verbatim (dollars -> dollar/money); an UNKNOWN sortal noun (stickers, coins)
is a count of discrete objects -> the existing 'item' lemma (dimension count). So
admissibility stays a REAL check: count+count admits, count+money (a mixed-unit
sum) REFUSES with unit_mismatch — verified to bite.

comprehension_relational_metric: 15/15 wrong=0 (full coverage). Located OUTSIDE
generate/meaning_graph (it targets binding_graph, not the MeaningGraph) so INV-28
neutrality stays intact; oracle imports none of the SUT (new INV-25 lane).
Capability index breadth 7->8, score 0.928622 -> 0.937258, wrong_total 0, digest
50e0675b…

Tests: reader templates + count/known-unit modelling + admissibility-bite (mixed
unit refuses) + non-digit refusal; end-to-end full-coverage wrong=0; arithmetic
added to the structure-preservation generative panel (projected relations+query ==
ground truth); capability breadth 7->8; INV-25 arithmetic lane. 93 targeted + 90
smoke green; lane SHAs 8/9 (sole miss = public_demo env flake; deductive_logic +
math_teaching unchanged -> no GSM8K coupling).
2026-06-06 00:43:16 -07:00

99 lines
4.3 KiB
Python

"""Unit tests for the arithmetic reader (prose -> binding_graph) + its projector.
Pins the templates, the count-vs-physical-unit modelling, and — load-bearing — the
REAL admissibility check: an equation is admitted only if its operand units verify,
so a mixed-unit sum REFUSES rather than fabricating a quantity. This is the
reviewer's "do not stamp admissibility" guard, made executable.
"""
from __future__ import annotations
from generate.binding_graph.model import SemanticSymbolicBindingGraph
from generate.meaning_graph.reader import Refusal
from generate.quantitative_comprehension import (
QuantComprehension,
comprehend_quantitative,
to_relational_metric,
)
def _comp(text: str) -> QuantComprehension:
comp = comprehend_quantitative(text)
assert isinstance(comp, QuantComprehension), comp
return comp
def test_fact_and_more_than_build_binding_graph() -> None:
comp = _comp("Liam has 6 stickers. Mia has 4 more stickers than Liam. How many stickers does Mia have?")
g = comp.binding_graph
assert isinstance(g, SemanticSymbolicBindingGraph)
assert {f.symbol_id: f.value for f in g.facts} == {"liam": "6"}
eq = next(e for e in g.equations if e.lhs_symbol_id == "mia")
assert eq.operation_kind == "add"
assert eq.rhs_canonical == "liam + 4"
assert eq.admissibility_status == "admitted" # from the REAL check, not stamped
assert comp.query.entity == "mia"
def test_count_nouns_resolve_to_item_dimension() -> None:
# Unknown sortal nouns become the count dimension (item); admissibility admits.
comp = _comp("Kim has 2 marbles. Leo has 3 more marbles than Kim. How many marbles does Leo have?")
units = {s.symbol_id: s.unit for s in comp.binding_graph.symbols}
assert units["kim"] == "item" and units["leo"] == "item"
def test_known_unit_is_used_verbatim() -> None:
comp = _comp("Iris has 100 dollars. Jack has 250 more dollars than Iris. How many dollars does Jack have?")
units = {s.symbol_id: s.unit for s in comp.binding_graph.symbols}
assert units["iris"] == "dollars" # parse_unit depluralizes dollars -> dollar (money)
def test_fewer_than_is_subtract() -> None:
comp = _comp("Noah has 15 cards. Olivia has 6 fewer cards than Noah. How many cards does Olivia have?")
eq = next(e for e in comp.binding_graph.equations if e.lhs_symbol_id == "olivia")
assert eq.operation_kind == "subtract" and eq.rhs_canonical == "noah - 6"
def test_sum_query_synthesizes_total() -> None:
comp = _comp("Dan has 7 coins. Eva has 9 more coins than Dan. How many coins do Dan and Eva have?")
assert comp.query.entity == "total"
total_eq = next(e for e in comp.binding_graph.equations if e.lhs_symbol_id == "total")
assert total_eq.operation_kind == "add"
assert set(total_eq.dependencies) == {"dan", "eva"}
def test_projection_shape() -> None:
comp = _comp("Liam has 6 stickers. Mia has 4 more stickers than Liam. How many stickers does Mia have?")
projected = to_relational_metric(comp)
assert projected is not None
relations, query = projected
assert {"kind": "fact", "entity": "liam", "value": 6} in relations
assert {"kind": "more_than", "entity": "mia", "ref": "liam", "delta": 4} in relations
assert query["entity"] == "mia"
# --------------------------------------------------------------------------- #
# Admissibility is REAL, not stamped (the reviewer's load-bearing guard)
# --------------------------------------------------------------------------- #
def test_mixed_unit_sum_refuses_via_admissibility() -> None:
# count (stickers -> item) + money (dollars) cannot be summed: the REAL
# admissibility check must REFUSE, not fabricate a total.
comp = comprehend_quantitative(
"Liam has 6 stickers. Mia has 4 dollars. How many things do Liam and Mia have?"
)
assert isinstance(comp, Refusal)
assert comp.reason == "admissibility_refused"
assert "unit_mismatch" in comp.detail
def test_non_digit_quantity_refuses() -> None:
comp = comprehend_quantitative("Liam has several stickers. How many stickers does Liam have?")
assert isinstance(comp, Refusal)
assert comp.reason == "non_digit_quantity"
def test_unreadable_clause_refuses() -> None:
comp = comprehend_quantitative("The weather is nice today.")
assert isinstance(comp, Refusal)