Skip to main content

PureCARD

A grammar- and schema-constrained decoder for Legend Pure.

PureCARD sits between a language model's logits and its sampler and masks next tokens that cannot continue an emitted-subset Pure query. It is the Pure analogue of PICARD, implemented as a byte-level Rust decoder with an optional Python boundary.

PureCARD is an independent sibling product colocated with Pure Analyzer in this monorepo. One name covers everything a consumer names it by: the Cargo package, the Rust library, and the Python module are all purecard, and only the workspace directory is crates/pure-analyzer-purecard/. The crate publishes to crates.io and its abi3 wheels to PyPI. Neither product depends on the other's internals. They share repository governance and automation, not a runtime architecture.

Validity is not faithfulness. PureCARD constrains membership in its fixed emitted-subset recognizer and narrows at implemented schema positions. Those constraints are not a general compiler-validity guarantee, and they cannot decide whether a query means what the user asked.

The guarantee boundary

The constraint levels form a strict containment hierarchy:

Level Contract Scope
L1 · Syntactic Output belongs to the hand-written emitted-subset Pure grammar Fixed-PDA membership
L2 · SchemaConsistent Covered identifier/type positions narrow against the supplied model schema Selected schema positions only
L3 · Faithful The query answers the question that was asked Out of scope

See the product reference for the emitted-Pure grammar, the byte-level masking algorithm, the implemented schema overlay, the public API, and the oracle-driven test strategy.

How it works

At each decode step the host applies PureCARD's mask before sampling:

logits  = model.forward(...)
mask    = session.allowed_mask()
logits[!mask] = -inf
tok     = sample(logits)
session.accept_token(tok)

The recognizer is a hand-written byte-level pushdown automaton (L1). A typed scope overlay narrows selected identifier and type positions against a supplied schema (L2). A lazy per-state mask cache keeps repeated mask generation off the critical path.

Operating limits

PureCARD does not include a model-inference runner, a full Pure compiler, or a full type checker. CompiledGrammar::from_spec selects the fixed PDA and the walker is schema-agnostic. The Qwen lane verifies tokenizer token-ID replay; it does not establish host inference-loop behavior or compiler validity of every accepted query. See the product reference for the precise boundary.

Corpus

corpus/ ships the test inputs inside this crate:

  • gold_queries.jsonl — 5,034 execution-verified Pure queries across 161 databases, used as the frozen offline soundness oracle;
  • modern_dialect_seeds.jsonl — provenance-distinct seeds for newer emitted Legend constructs;
  • schemas/*.md — eight database schemas used by the implemented L2 tests; and
  • legend-stack/ — the pinned Legend engine stack for the opt-in live lane.

Python and distribution

The python Cargo feature exposes the Rust library through a thin PyO3 module named purecard. Python owns model inference, tokenization orchestration, and sampling; the boundary constrains only the final-query span.

The Cargo package has publish = true — alone in this workspace — and releases to crates.io as purecard; its maturin wheels release to PyPI under the same name. Both are cut from release-plz's release PR, on a published GitHub Release, so no ordinary merge to main ships anything.

Development and governance

Run PureCARD from the repository root through the shared just frontend:

mise install && mise run install-cargo-tools
just ci
just qwen-oracle
just test-legend
just wheel

PureCARD follows the root constitution, contribution guide, and methodology. Its decoder-specific specification, ADRs, and testing guidance remain under this crate. The placement and boundary decision is recorded in ADR-0009.

Contributing

See the root contribution guide, code of conduct, and security policy. Contributions use the same root gates as the analyzer.

License

Apache-2.0. See the root license.

Metadata

Release files for purecard 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distributions (wheels)

Table of built distributions (wheels) for purecard 0.3.0
File
purecard-0.3.0-cp39-abi3-win_amd64.whl CPython 3.9 abi3 Windows x86-64 Details
purecard-0.3.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.9 abi3 Linux glibc 2.17+ x86-64 Details
purecard-0.3.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl CPython 3.9 abi3 Linux glibc 2.17+ ARM64 Details
purecard-0.3.0-cp39-abi3-macosx_11_0_arm64.whl CPython 3.9 abi3 macOS 11.0+ ARM64 Details
purecard-0.3.0-cp39-abi3-macosx_10_12_x86_64.whl CPython 3.9 abi3 macOS 10.12+ x86-64 Details

Total release size: 2.0 MB

Release files / purecard-0.3.0-cp39-abi3-win_amd64.whl

Download URL purecard-0.3.0-cp39-abi3-win_amd64.whl
Size 359.2 kB
Tags CPython 3.9 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
55eaa98bf77f2d813c2dde0f8f16a211d2a7fddf7cc1f1703b4de94a8956efac
BLAKE2b-256 checksum
How to use checksums
ce27ac91e4a33d224e58449991a71bd175a2ccb18a8cda9117e2bdfb56e854b0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / purecard-0.3.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL purecard-0.3.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 442.6 kB
Tags CPython 3.9 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
1686a8950f6827cf3e908aa5864fb0a6dc5f3d24e73a81657b8b8731dd663d0f
BLAKE2b-256 checksum
How to use checksums
a2627109be4081e3adc63b0cd82e6548ef7de84ba55993398383608dd54a03b7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / purecard-0.3.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl

Download URL purecard-0.3.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Size 412.6 kB
Tags CPython 3.9 Linux glibc 2.17+ ARM64 abi3
SHA-256 checksum
How to use checksums
d81d4eb766d766fa47374440217149c1dc14ee241d313861e0fe7bb144d29806
BLAKE2b-256 checksum
How to use checksums
64e072e4bdc36ca1acca5f642f3eb5e6a61666d52df3a591b1ea27a7012f28a7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / purecard-0.3.0-cp39-abi3-macosx_11_0_arm64.whl

Download URL purecard-0.3.0-cp39-abi3-macosx_11_0_arm64.whl
Size 395.6 kB
Tags CPython 3.9 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
95327f2bd0608ad8cfdd85cdbe7005ae401e9671bebdc14ac1fbd3e7074cc66c
BLAKE2b-256 checksum
How to use checksums
ce6048ce3638b0158a2dcdcc09af9620cb5d797068b7e9e87861ebfcc40c09e1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / purecard-0.3.0-cp39-abi3-macosx_10_12_x86_64.whl

Download URL purecard-0.3.0-cp39-abi3-macosx_10_12_x86_64.whl
Size 421.8 kB
Tags CPython 3.9 abi3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
4788fd1e0b8f7cab0f0730a4d19052106a385edcac5a6bd39b1d904657c44639
BLAKE2b-256 checksum
How to use checksums
4fdc53c8d12d7ca1fe82dc4f48d6a717ec7a3b7d30d761dbab41d2ae82366173
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Security for purecard 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page