|
| 1 | +# `ask()` / `verify()` plan |
| 2 | + |
| 3 | +## Background |
| 4 | + |
| 5 | +Today, `ce.ask(pattern)` is _not_ “ask if this is true”. It is an **assumptions |
| 6 | +DB query**: |
| 7 | + |
| 8 | +- It boxes `pattern` (non-canonical) and structurally matches it against entries |
| 9 | + in `ce.context.assumptions`. |
| 10 | +- It returns `BoxedSubstitution[]` (pattern match bindings), not a truth value. |
| 11 | + |
| 12 | +That makes issue #225 unsurprising: declarations (symbol types/values) and many |
| 13 | +derived facts are not stored in `context.assumptions`, and inequality |
| 14 | +assumptions are stored in normalized forms that won’t match “user-shaped” |
| 15 | +patterns. |
| 16 | + |
| 17 | +SymPy’s `ask()` is closer to “can I prove/disprove this predicate?” and returns |
| 18 | +**`True/False/None`** (unknown). We can offer the same UX by making |
| 19 | +`ce.verify()` the SymPy-like API, while keeping `ce.ask()` as an explicit |
| 20 | +assumptions query tool. |
| 21 | + |
| 22 | +## Goals |
| 23 | + |
| 24 | +- Implement `ce.verify(query): boolean | undefined` |
| 25 | + - Returns `true` if the predicate is provably true with current knowledge. |
| 26 | + - Returns `false` if it is provably false with current knowledge. |
| 27 | + - Returns `undefined` if it cannot be proven either way. |
| 28 | +- Expand the set of predicates that can be proven/disproven using: |
| 29 | + - explicit assumptions (`ce.assume(...)`), |
| 30 | + - symbol declarations/definitions (`ce.declare(...)`, inferred types, assigned |
| 31 | + values), |
| 32 | + - existing predicate evaluation rules (e.g. inequality evaluation already uses |
| 33 | + assumptions). |
| 34 | +- Keep `ce.ask(pattern)` as an “assumptions DB matcher”. |
| 35 | + |
| 36 | +## Non-goals (for now) |
| 37 | + |
| 38 | +- Returning witnesses/substitutions/bounds from `verify()` (SymPy `ask()` |
| 39 | + doesn’t do this). |
| 40 | +- Full SAT-style cross-predicate inference like SymPy’s known-facts system. |
| 41 | +- Proving general quantified statements over infinite domains (only |
| 42 | + finite-domain evaluation when available). |
| 43 | + |
| 44 | +## Proposed public semantics |
| 45 | + |
| 46 | +### `verify()` |
| 47 | + |
| 48 | +Signature: |
| 49 | + |
| 50 | +- `verify(query: BoxedExpression): boolean | undefined` |
| 51 | + - (Optional follow-up) accept a LaTeX string like `assume()` does. |
| 52 | + |
| 53 | +Behavior: |
| 54 | + |
| 55 | +1. Box/parse the input similarly to `assume()` (i.e., accept `LatexString` too, |
| 56 | + if we add it). |
| 57 | +2. Try to evaluate `query` to a boolean: |
| 58 | + - If it evaluates to `True` → `true` |
| 59 | + - If it evaluates to `False` → `false` |
| 60 | + - Otherwise → `undefined` |
| 61 | +3. Support boolean connectives (`Not`, `And`, `Or`) by evaluating operands |
| 62 | + recursively when needed. |
| 63 | + |
| 64 | +Examples: |
| 65 | + |
| 66 | +- `ce.verify(['Greater', 'x', 0])` after `ce.assume(['Greater', 'x', 4])` → |
| 67 | + `true` |
| 68 | +- `ce.verify(['Less', 'x', 0])` after `ce.assume(['Greater', 'x', 4])` → `false` |
| 69 | +- `ce.verify(['Greater', 'x', 0])` with no facts about `x` → `undefined` |
| 70 | + |
| 71 | +### `ask()` |
| 72 | + |
| 73 | +Keep behavior: `ask(pattern)` returns a list of structural matches against |
| 74 | +`context.assumptions`. |
| 75 | + |
| 76 | +Documentation update: |
| 77 | + |
| 78 | +- Encourage “truth queries” to use `verify()`. |
| 79 | +- Encourage “show me matching stored assumptions” to use `ask()`. |
| 80 | + |
| 81 | +## Predicate support roadmap |
| 82 | + |
| 83 | +This is ordered by “high value, low complexity” first. |
| 84 | + |
| 85 | +### Phase 1: Make `verify()` useful immediately |
| 86 | + |
| 87 | +1. Implement `verify()` in `src/compute-engine/index.ts` using evaluation: |
| 88 | + - Evaluate `query` (with a “predicate evaluation” helper). |
| 89 | + - Map `True`/`False` to `boolean`, else `undefined`. |
| 90 | +2. Add tests (new file `test/compute-engine/verify.test.ts` or extend |
| 91 | + `test/compute-engine/assumptions.test.ts`): |
| 92 | + - inequality truthiness via assumptions (already covered by evaluate() tests, |
| 93 | + but add explicit `verify()` coverage) |
| 94 | + - equality truthiness via assumed equalities |
| 95 | + - boolean connectives over known/unknown components (e.g. |
| 96 | + `And(True, unknown)` → `undefined`) |
| 97 | + |
| 98 | +### Phase 2: Type/domain predicates (`Element`, `NotElement`) |
| 99 | + |
| 100 | +Problem: `Element(value, collection)` currently only works when `collection` is |
| 101 | +a _collection/set_ with a `contains()` implementation. It does **not** work for |
| 102 | +type-like RHS such as `'finite_real'` or `'any'`. |
| 103 | + |
| 104 | +Plan: |
| 105 | + |
| 106 | +- Extend `Element.evaluate` / `NotElement.evaluate` (in |
| 107 | + `src/compute-engine/library/sets.ts`) to recognize a “type RHS”: |
| 108 | + - If `collection.contains(value)` is `undefined` and `collection` looks like a |
| 109 | + type token, interpret it as a `BoxedType`. |
| 110 | + - Proposed rule: if `collection.symbol` corresponds to a known type name (e.g. |
| 111 | + `any`, `number`, `real`, `finite_real`, …), then: |
| 112 | + - `Element(value, collectionType)` is `true` when |
| 113 | + `value.type.matches(collectionType)` is `true` |
| 114 | + - `false` when it is definitively incompatible |
| 115 | + - `undefined` otherwise |
| 116 | +- Add tests: |
| 117 | + - `ce.declare('x', 'finite_real')` then |
| 118 | + `ce.verify(['Element', 'x', 'finite_real'])` → `true` |
| 119 | + - `ce.declare('x', 'real')` then `ce.verify(['Element', 'x', 'finite_real'])` |
| 120 | + → `undefined` (can’t prove “finite”) |
| 121 | + - `ce.declare('x', 'integer')` then `ce.verify(['Element', 'x', 'real'])` → |
| 122 | + `true` |
| 123 | + |
| 124 | +Notes: |
| 125 | + |
| 126 | +- Also consider aligning domains with existing set symbols (`RealNumbers`, |
| 127 | + `Integers`, etc.) and treating them as the preferred spelling in docs, while |
| 128 | + still supporting type spellings for ergonomics. |
| 129 | + |
| 130 | +### Phase 3: More “SymPy-style” predicate surface area |
| 131 | + |
| 132 | +SymPy exposes a large predicate set via `Q.<predicate>` (see |
| 133 | +`sympy/assumptions/ask.py` around `AssumptionKeys`). |
| 134 | + |
| 135 | +We don’t need the same surface API (`Q.*`) to get comparable utility; we need |
| 136 | +comparable _facts_. |
| 137 | + |
| 138 | +Suggested mapping to existing Compute Engine capabilities: |
| 139 | + |
| 140 | +- Set/type predicates: |
| 141 | + - `real`, `complex`, `imaginary`, `rational`, `integer`, `finite`, `infinite` |
| 142 | + - Implement via `Element(expr, <TypeOrSet>)` or via `expr.type.matches(...)` / |
| 143 | + `expr.isFinite`. |
| 144 | +- Order/sign predicates: |
| 145 | + - `positive`, `negative`, `zero`, `nonzero`, `nonpositive`, `nonnegative` |
| 146 | + - Implement via `expr.isPositive/isNegative/isNonNegative/isNonPositive`, or |
| 147 | + by rewriting to comparisons to `0`. |
| 148 | +- Parity predicates: |
| 149 | + - `even`, `odd` (already present as `expr.isEven`/`expr.isOdd` for many |
| 150 | + expression kinds). |
| 151 | + |
| 152 | +Concrete work items: |
| 153 | + |
| 154 | +- Ensure evaluation for basic comparisons (`Equal`, `NotEqual`, `Less`, |
| 155 | + `LessEqual`, `Greater`, `GreaterEqual`) continues to consult: |
| 156 | + - symbol values (from equality assumptions / declarations), |
| 157 | + - inequality assumptions (already in place), |
| 158 | + - simplification rules. |
| 159 | +- Consider adding lightweight predicate operators (optional): |
| 160 | + - e.g. `Positive(x)` as sugar for `Greater(x, 0)` (only if it improves |
| 161 | + readability and doesn’t bloat the library surface). |
| 162 | + |
| 163 | +### Phase 4: Quantifiers (finite domains only) |
| 164 | + |
| 165 | +Compute Engine already supports finite-domain evaluation patterns for |
| 166 | +quantifiers in some contexts. |
| 167 | + |
| 168 | +Plan: |
| 169 | + |
| 170 | +- Ensure `verify(ForAll(...))` and `verify(Exists(...))` return |
| 171 | + `true/false/undefined`: |
| 172 | + - `true/false` only when the quantifier can be evaluated over a finite domain |
| 173 | + (e.g., `Element(x, Set(...))`). |
| 174 | + - `undefined` otherwise (avoid pretending to prove over infinite domains). |
| 175 | + |
| 176 | +## Documentation updates |
| 177 | + |
| 178 | +- Update the API docs for `ask()` to clearly state it queries |
| 179 | + `context.assumptions` and returns substitutions. |
| 180 | +- Document `verify()` as the truth-query API with `boolean | undefined`. |
| 181 | +- Add a short migration note: |
| 182 | + - “If you were using `ask()` to check truthiness, use `verify()` instead.” |
0 commit comments