Multi-Dimensional Reconciliation & Overrides¶
When generating polyglot bindings across varying target triples, versions, and feature flags, HeaderKit reconciles separate AST parses into a unified, conditionally guarded binding file.
1. Multi-Dimensional Reconciliation (reconcile_matrix)¶
The reconciliation engine merges $N$ AST compilation units into a single MultiDimensionalReconciledUnit:
- Universal Declarations: Symbols identical across all configurations are emitted unconditionally.
- Topological Sequence: Declaration sequence is strictly preserved using monotonic Longest Common Subsequence (LCS) anchor alignment.
- Divergent Blocks: Symbols present only on certain configurations are grouped into
MatrixGuardedBlockbranches.
from headerkit.matrix import ConfigCondition
from headerkit.reconcile import MultiDimensionalReconciledUnit, reconcile_matrix
matrix_units = [
(ConfigCondition({"target": "windows"}), win_unit),
(ConfigCondition({"target": "linux"}), linux_unit),
(ConfigCondition({"target": "darwin"}), mac_unit),
]
reconciled = reconcile_matrix(matrix_units, target_lang="nim")
2. Boolean Guard Reduction (Minimization)¶
HeaderKit's algebraic reduction engine (minimize_conditions) minimizes complex boolean guards:
- Quine-McCluskey Minimization: Collapses adjacent boolean feature terms ($(A \land B) \lor (A \land \neg B) \implies A$).
- Platform Set Deduplication & Operator Precedence: Groups matching targets into parenthesized disjunctions (e.g.
(defined(linux) or defined(macosx)) and defined(ENABLE_SSL)). - Version Interval Analysis: Reduces contiguous version ranges into monotonic comparisons (e.g.
version >= (2, 0)).
Polyglot conditionals are generated natively for each target language:
- Nim: when (defined(linux) or defined(macosx)):
- Cython / C: #if (defined(__linux__) || defined(__APPLE__))
- Python: if sys.platform in ('darwin', 'linux'):
3. Preserving Developer Overrides (@headerkit:override)¶
When bindings are regenerated, developers often need to inject custom helper procs, type conversions, or manual wrappers that must not be overwritten.
HeaderKit preserves manual blocks using polyglot comment tags:
# Generated by headerkit
proc play_sound*() {.importc.}
# @headerkit:override custom_helper
proc playSoundAsync*(filename: string) =
# User's manual helper implementation
echo "Playing: ", filename
# @headerkit:end-override custom_helper
Supported Language Delimiters¶
- Nim / Python:
# @headerkit:override <block_id> - C / C++ / Cython:
// @headerkit:override <block_id> - Lua:
-- @headerkit:override <block_id>
Strict Syntax Validation¶
HeaderKit raises MalformedOverrideError if:
- An override block is unclosed at end of file.
- Override blocks are illegally nested.
- Start and end tag identifiers do not match.
4. Incremental Expansion with headerkit add-target¶
When new platforms or target triples need to be supported, you do not need to rewrite configuration files or lose manual edits.
HeaderKit embeds a machine-readable @headerkit-matrix-manifest comment in the header of generated files. The CLI command headerkit add-target reads this manifest, probes only the new target, re-runs reconciliation, and seamlessly reinjects all manual override blocks:
# Add Linux aarch64 target to an existing Nim binding file
headerkit add-target bindings/audio.nim --target aarch64-unknown-linux-gnu
How add-target Works:¶
- Extracts the embedded JSON
@headerkit-matrix-manifest. - Extracts existing
@headerkit:overrideblocks into memory. - Probes the baseline header with
--target=aarch64-unknown-linux-gnu. - Executes
reconcile_matrixacross existing and new targets. - Re-emits code, updates the manifest comment, and reinjects preserved overrides.