|
| 1 | +<!-- |
| 2 | +SPDX-License-Identifier: CC-BY-SA-4.0 |
| 3 | +SPDX-FileCopyrightText: 2025-2026 Jonathan D.A. Jewell <j.d.a.jewell@open.ac.uk> |
| 4 | +--> |
| 5 | + |
| 6 | +[](https://github.com/sponsors/hyperpolymath) |
| 7 | + |
| 8 | +# What Is This? |
| 9 | + |
| 10 | +**BQNiser** scans your existing code for array computation |
| 11 | +patterns — loops, maps, folds, reductions, comprehensions — and rewrites |
| 12 | +them as optimised [BQN](https://mlochbaum.github.io/BQN/) array |
| 13 | +primitives. |
| 14 | + |
| 15 | +BQN (by Marshall Lochbaum) is an array language descended from APL/J/K. |
| 16 | +It brings first-class functions, trains (`∘` `○` `⊸` `⟜`), |
| 17 | +under/structural-under combinators (`⌾`), rank polymorphism, and |
| 18 | +leading-axis theory to array processing. BQNiser lets you harness |
| 19 | +10-100x speedups on array-heavy workloads **without learning BQN |
| 20 | +yourself**. |
| 21 | + |
| 22 | +Where your Python loop takes 200ms to filter-then-reduce a million-row |
| 23 | +column, a single BQN expression using `/` (replicate), `⌽` (reverse), or |
| 24 | +`⍋` (grade-up) completes in under 2ms via |
| 25 | +[CBQN](https://github.com/dzaima/CBQN)’s vectorised runtime. |
| 26 | + |
| 27 | +# How It Works |
| 28 | + |
| 29 | +BQNiser follows the [-iser |
| 30 | +pattern](https://github.com/hyperpolymath/iseriser): you describe |
| 31 | +**what** you want; the tool generates **everything**. |
| 32 | + |
| 33 | +1. **Manifest** (`bqniser.toml`) — describe your workload, entry |
| 34 | + points, data shapes |
| 35 | + |
| 36 | +2. **Source Analysis** — AST-based detection of array patterns in Rust, |
| 37 | + Python, or Julia source |
| 38 | + |
| 39 | +3. **Pattern Matching** — map detected patterns to BQN primitives: |
| 40 | + |
| 41 | + - Nested loops over arrays → `¨` (each), `⌜` (table) |
| 42 | + |
| 43 | + - Accumulate/reduce → `´` (fold), `` ` `` (scan) |
| 44 | + |
| 45 | + - Filter → `/` (replicate) with boolean mask |
| 46 | + |
| 47 | + - Sort/rank → `⍋` (grade-up), `⍒` (grade-down) |
| 48 | + |
| 49 | + - Reshape/transpose → `⥊` (reshape), `⍉` (transpose) |
| 50 | + |
| 51 | + - Concatenation → `∾` (join) |
| 52 | + |
| 53 | + - Index selection → `⊏` (select) |
| 54 | + |
| 55 | + - Reversal → `⌽` (reverse), `⌾` (under) for structural transforms |
| 56 | + |
| 57 | +4. **Idris2 ABI** (`src/interface/abi/`) — formal proofs that each |
| 58 | + rewrite preserves input-output equivalence (dependent types |
| 59 | + guarantee correctness) |
| 60 | + |
| 61 | +5. **BQN Codegen** — emit optimised BQN source using trains and tacit |
| 62 | + style |
| 63 | + |
| 64 | +6. **CBQN FFI Bridge** (`src/interface/ffi/`) — Zig-based C-ABI bridge |
| 65 | + to the [CBQN](https://github.com/dzaima/CBQN) runtime, so generated |
| 66 | + BQN runs in-process with zero serialisation overhead |
| 67 | + |
| 68 | +# Key Value |
| 69 | + |
| 70 | +- **10-100x on array operations** — vectorised BQN primitives beat |
| 71 | + scalar loops |
| 72 | + |
| 73 | +- **Automatic pattern detection** — finds optimisable code via AST |
| 74 | + analysis |
| 75 | + |
| 76 | +- **Equivalence proofs** — Idris2 dependent types prove rewrites |
| 77 | + preserve semantics |
| 78 | + |
| 79 | +- **Gradual adoption** — replace hot loops one at a time, keep the rest |
| 80 | + untouched |
| 81 | + |
| 82 | +- **Zero BQN knowledge required** — the tool generates all BQN and FFI |
| 83 | + code |
| 84 | + |
| 85 | +- **Rank polymorphism** — BQN’s leading-axis theory means primitives |
| 86 | + generalise across any number of dimensions automatically |
| 87 | + |
| 88 | +# BQN Primitives Reference |
| 89 | + |
| 90 | +BQNiser targets these core primitives and combinators: |
| 91 | + |
| 92 | +| Glyph | Name | Detected Pattern | |
| 93 | +|---------|------------|------------------------------------------| |
| 94 | +| `∾` | Join | `concat`, `append`, `extend` | |
| 95 | +| `⌽` | Reverse | `reversed()`, `[::-1]` | |
| 96 | +| `⍋` | Grade Up | `sorted()`, `argsort` | |
| 97 | +| `⍒` | Grade Down | `sorted(reverse=True)` | |
| 98 | +| `/` | Replicate | `filter`, boolean indexing | |
| 99 | +| `⊏` | Select | `arr[indices]`, `take`, `gather` | |
| 100 | +| `⥊` | Reshape | `reshape`, `flatten`, `ravel` | |
| 101 | +| `⍉` | Transpose | `transpose`, `T`, axis permutation | |
| 102 | +| `¨` | Each | `map`, list comprehension | |
| 103 | +| `⌜` | Table | nested `map`, outer product | |
| 104 | +| `´` | Fold | `reduce`, `foldl`, `inject` | |
| 105 | +| `` ` `` | Scan | `accumulate`, `scanl`, prefix sums | |
| 106 | +| `⌾` | Under | structural transforms, lens-like updates | |
| 107 | +| `∘○⊸⟜` | Trains | function composition, tacit pipelines | |
| 108 | + |
| 109 | +# Architecture |
| 110 | + |
| 111 | + bqniser.toml src/interface/abi/ src/interface/ffi/ |
| 112 | + ┌──────────┐ ┌─────────────────┐ ┌─────────────────┐ |
| 113 | + │ Manifest │──parse──→│ Idris2 ABI │──gen──→│ Zig FFI │ |
| 114 | + │ (TOML) │ │ (Types, Layout, │ │ (CBQN embedding │ |
| 115 | + └──────────┘ │ Foreign) │ │ C-ABI bridge) │ |
| 116 | + │ └────────┬────────┘ └────────┬────────┘ |
| 117 | + │ │ │ |
| 118 | + ▼ ▼ ▼ |
| 119 | + ┌──────────┐ ┌─────────────────┐ ┌─────────────────┐ |
| 120 | + │ Rust CLI │──scan──→ │ Pattern Matcher │──emit─→│ BQN Codegen │ |
| 121 | + │ (clap) │ │ (AST → BQN map)│ │ (.bqn files) │ |
| 122 | + └──────────┘ └─────────────────┘ └─────────────────┘ |
| 123 | + |
| 124 | +- **Rust CLI** — parses manifest, invokes AST analysis, orchestrates |
| 125 | + generation |
| 126 | + |
| 127 | +- **Pattern Matcher** — maps source-level patterns to BQN primitive |
| 128 | + candidates |
| 129 | + |
| 130 | +- **Idris2 ABI** — proves BQN value layout (array header + shape |
| 131 | + vector + data cells), proves rewrite equivalence for each |
| 132 | + transformation |
| 133 | + |
| 134 | +- **BQN Codegen** — emits idiomatic BQN using trains, under, and tacit |
| 135 | + definitions |
| 136 | + |
| 137 | +- **Zig FFI** — bridges CBQN’s C API (`BQN_NewEval`, `BQN_Call`, value |
| 138 | + accessors), handles BQN value lifecycle and memory management |
| 139 | + |
| 140 | +Part of the [-iser family](https://github.com/hyperpolymath/iseriser) of |
| 141 | +acceleration frameworks. |
| 142 | + |
| 143 | +# Use Cases |
| 144 | + |
| 145 | +- **Data processing pipelines** — ETL stages with heavy filtering, |
| 146 | + sorting, reshaping |
| 147 | + |
| 148 | +- **Scientific computing** — matrix operations, statistical reductions, |
| 149 | + signal processing |
| 150 | + |
| 151 | +- **Financial analytics** — rolling windows, aggregations, time-series |
| 152 | + transforms |
| 153 | + |
| 154 | +- **Compiler/transpiler backends** — batch AST transformations as array |
| 155 | + operations |
| 156 | + |
| 157 | +- **ETL optimisation** — replace row-by-row processing with bulk array |
| 158 | + primitives |
| 159 | + |
| 160 | +# Quick Start |
| 161 | + |
| 162 | +```bash |
| 163 | +# Initialise a manifest |
| 164 | +bqniser init |
| 165 | + |
| 166 | +# Edit bqniser.toml to describe your workload |
| 167 | +# (see examples/ for patterns) |
| 168 | + |
| 169 | +# Generate BQN + FFI artifacts |
| 170 | +bqniser generate |
| 171 | + |
| 172 | +# Build everything |
| 173 | +bqniser build |
| 174 | + |
| 175 | +# Run |
| 176 | +bqniser run |
| 177 | +``` |
| 178 | + |
| 179 | +# Status |
| 180 | + |
| 181 | +**Codebase in progress.** Architecture defined, CLI scaffolded, Idris2 |
| 182 | +ABI types and Zig FFI bridge stubbed. Pattern detection engine and BQN |
| 183 | +codegen are the active development frontier. |
| 184 | + |
| 185 | +# License |
| 186 | + |
| 187 | +SPDX-License-Identifier: CC-BY-SA-4.0 |
0 commit comments