FELN: proving that a predicted map query is right on every database

I want people to ask questions about their geographic data in plain text: "find suspended wells within 5 km of an oil pipeline". A model reads the question and writes a query. Then the query runs on the data.

The hard part is not the model. The hard part is the measurement. If I cannot measure a model, I cannot tune it. This post is about how I measure it, and about a new piece: a checker that Lean proves correct.

The code is at github.com/mraad/feln. It is a Python library with a command-line tool. It needs Python 3.11+, and it does not need GDAL or ArcGIS.

Why not let the model write SQL

SQL is the obvious target. But two SQL texts are hard to compare. depth > 100 and 100 < "DEPTH" are the same test. A text diff says that they are different. An exact-match score then calls a correct answer wrong.

So I made a smaller target. I call it FELN, "Find Existing Location with N layers". A FELN query is JSON with three lists:

{
  "layers": ["Wells", "Counties", "Pipelines"],
  "where": ["STATUS = 1", "NAME = 'County A'", "MEDIUM = 'GAS'"],
  "relations": ["within", "withinDistance 2 kilometers"]
}

The three lists of a FELN query, aligned by index

The rules are fixed:

  • layers[0] is the primary layer. The query returns its features only.
  • where[i] filters layers[i].
  • relations[i] relates the primary layer to layers[i + 1].

The relations are intersects, within, contains, withinDistance and notWithinDistance. The last one compiles to NOT EXISTS, not to NOT ST_DWithin in a join. That second form is a classic bug: it keeps a well when one pipeline is far away, not when all of them are.

The five relation kinds: intersects, within, contains, withinDistance, notWithinDistance

What the package does

Data flow: request text, model, FELN query, DuckDB SQL, features

It does four things:

  • Generate. It makes example pairs of text and FELN from a catalog file, Layers.json. The catalog lists the layers, the columns, the coded values and the units. A sibling project, layers-json, writes that file from an ArcGIS project.
  • Compile. It turns a FELN query into DuckDB spatial SQL. Each WHERE goes through an AST allowlist first: no functions, no subqueries, no LIMIT riding in a filter.
  • Compare. It scores a prediction against a reference that a person approved: an exact match, a graded score (0.4 layers, 0.3 WHERE, 0.3 relations) and a cost for tuning.
  • Paraphrase. It rewrites the generated text into more natural sentences with a local model (Ollama). A paraphrase that drops a number or a quoted value is rejected.

The package does not hold the model and does not run SQL. It gives the format, the data, the compiler and the measurements.

Two measurements, two weak points

Until now I had two ways to say that a prediction is right.

  • Exact match. The normalized queries are the same. This is too strict. Two queries with different text can always return the same features.
  • Execution match. Both queries return the same features on my test database. This is too lenient. A 3 km query and a 5 km query can return the same wells by chance. On the next database they do not.

I wanted something between the two: "these two queries return the same features on every database". A test cannot tell me that. A test looks at one database. A proof can.

Exact match implies the proved verdict, and the proved verdict implies execution match

The proved verdict

I wrote a small checker in Lean 4. Lean is a programming language and a proof checker in one. I write a function, I write a statement about it, and I write the proof. If one step of the proof is wrong, the build fails. If the build passes, the statement is true for all inputs.

The checker reads a pair and gives one of four verdicts:

Verdict True on every database Meaning
equiv P = R The prediction is correct.
subset P is inside R Never a wrong feature. Precision is 1.
superset R is inside P Never a missed feature. Recall is 1.
unknown nothing No proof found. Not an error.

R is the result of the reference. P is the result of the prediction.

The four verdicts shown as sets

The rules are simple:

  • A tighter number test forces a wider one: depth >= 100.5 forces depth > 100.
  • A smaller distance forces a larger one: within 3 km forces within 5 km.
  • For notWithinDistance the direction turns around: not within 2 miles forces not within 1 mile.
  • Any other test (=, LIKE, IN, dates) must have the same normalized text.
  • Secondary layers can be in any order.

Lean proves three theorems about the checker: if it says equiv, then P = R; if it says subset, then P is inside R; if it says superset, then R is inside P. The proofs have no sorry and use only Lean's three standard axioms. The project has no Mathlib and no other Lean library.

I made a short explainer page with eight example predictions. Click one, and it shows the verdict and why. You do not need to know Lean to read it.

What is proved and what is not

I want to be honest about the line.

Lean proves the checker. Lean does not prove the Python step that turns SQL text into checker input. That step is trusted: the sqlglot parse, the text normalization, the unit table. Lean also does not prove that my model of a query matches the SQL that the compiler writes. Tests cover those parts.

Data flow: pair, exporter, checker input, checker, verdict

Two details took me some time:

  • SQL NULL. SQL has TRUE, FALSE and NULL. A WHERE keeps a row only when it is TRUE. "Is TRUE" passes cleanly through AND and OR, but not through NOT. So the checker splits AND and OR, and keeps anything under a NOT as one opaque test.
  • No integer steps. x > 5 does not force x >= 6, because the column can hold 5.5. The checker does not know column types, so it does not take that step.

And the obvious limit: the checker is sound, not complete. Many pairs that are in fact equal get unknown.

What it found

I ran the checker on 5,208 real reference and prediction pairs from model runs.

  • Every pair that exact match accepted got equiv.
  • No equiv pair had an execution mismatch.
  • It found a bug in my own exact match. When a WHERE kept a value-changing cast, the code returned the raw string, so "DEPTH" and depth did not match. Seven pairs moved to exact match after the fix.
  • 101 pairs matched on execution but were proved only one way (subset or superset). Those are execution matches by chance. Now I know which test data to add.
  • On the pairs that exact match rejected, the checker proved a verdict for about 14%. That is less than I hoped. The rest stays unknown, and I count it as "no information", never as a miss.

The useful part for tuning is the direction. Many superset verdicts mean that the model drops conditions. Many subset verdicts mean that it adds them.

Try it

Install the Lean version manager and build the checker:

brew install elan-init
cd lean && lake build && cd ..

The build is the proof check. Then add verdicts to a predictions file:

uv run python scripts/verdict.py predictions.jsonl -o with-verdict.jsonl

scripts/predict.py adds the verdict to each row by itself when the checker is built. Without Lean, everything else still works.

The docs have the details: overview.md for the project, and lean-verdict.md for the checker rules, the theorems and the wire format.

ArcGIS is a trademark of Esri. This project is not affiliated with Esri.

Comments

Popular posts from this blog

How to use Esri Flex API on Android and iPhone

Augmented Reality, iPhone and ArcGIS Server

Image Flare Symbol For Clustered Graphics