Skip to content

P1: fix Zig FFI to build and match the Idris2 ABI#37

Merged
hyperpolymath merged 1 commit into
mainfrom
claude/new-session-1fphit
Jun 26, 2026
Merged

P1: fix Zig FFI to build and match the Idris2 ABI#37
hyperpolymath merged 1 commit into
mainfrom
claude/new-session-1fphit

Conversation

@hyperpolymath

Copy link
Copy Markdown
Owner

Summary

The Zig FFI (src/interface/ffi/src/main.zig) failed to compile under Zig 0.14.0. This PR fixes the two compile errors with minimal, behaviour-preserving changes. Only src/interface/ffi/src/main.zig is touched; the Idris2 ABI is the source of truth and was left untouched.

Errors (from zig test src/main.zig -lc)

  1. src/main.zig:118@bitCast size mismatch: destination type 'u64' has 64 bits but source type 'i128' has 128 bits. In betlangiser_init, std.time.nanoTimestamp() returns i128, but it was @bitCast directly into the u64 PRNG seed.
  2. src/main.zig:558name shadows primitive 'u1'. In sampleDistribution (Box-Muller normal sampling), the locals u1/u2 shadow the built-in primitive type u1, which is an error in Zig 0.14.

Fixes

  1. Reinterpret the i128 timestamp as u128, then truncate to u64:
    @truncate(@as(u128, @bitCast(std.time.nanoTimestamp()))).
  2. Rename the Box-Muller locals u1/u2unif1/unif2.

Net diff: 4 insertions, 4 deletions in one file.

ABI fidelity (preserved)

No exported C symbol names, signatures, or integer encodings changed:

  • All 26 C:betlangiser_* symbols in Foreign.idr still map to matching export fns.
  • Result enum ok=0 … sampling_failed=6 matches Types.idr resultToInt.
  • TernaryBool encoding t_false=0, t_true=1, t_unknown=2 matches ternaryToInt.
  • DistributionTag encoding normal=0 … custom=4 matches distributionTag.

Verification

  • cd src/interface/ffi && zig test src/main.zig -lcall 9 tests pass, zero errors/warnings, exit 0.
  • cd src/interface/abi && idris2 --build betlangiser-abi.ipkgexit 0 (build dir removed afterwards).
  • Symbol-coverage check: every C:<name> in Foreign.idr has a matching export fn <name> in main.zig (26/26 OK).

Note: any rust-ci / Hypatia / governance red checks are pre-existing estate-infra issues unrelated to this Zig-only change.

🤖 Generated with Claude Code

https://claude.ai/code/session_019xMKB3T4Vo5FYC7Czx3JSH


Generated by Claude Code

The Zig FFI (src/interface/ffi/src/main.zig) failed to compile under
Zig 0.14.0 with two errors, both in functions reached from
betlangiser_init / sampleDistribution:

1. betlangiser_init: `@bitCast(std.time.nanoTimestamp())` was assigned
   to the u64 PRNG seed, but nanoTimestamp() returns i128 — a @bitcast
   size mismatch (128 -> 64 bits). Fixed by reinterpreting the i128 as
   u128 and truncating to u64:
   `@truncate(@as(u128, @bitcast(std.time.nanoTimestamp())))`.

2. sampleDistribution (Box-Muller normal sampling): local variables
   `u1` and `u2` shadowed the built-in primitive type `u1`, which is an
   error in Zig 0.14. Renamed to `unif1`/`unif2`.

Both fixes are local and behaviour-preserving. No exported C symbol
names, signatures, or integer encodings changed: all 26 `C:betlangiser_*`
symbols in the Idris2 ABI (Foreign.idr) still map to matching
`export fn`s, the Result enum (ok=0..sampling_failed=6) still matches
Types.idr resultToInt, the TernaryBool encoding (t_false=0, t_true=1,
t_unknown=2) matches ternaryToInt, and the DistributionTag encoding
(normal=0..custom=4) matches distributionTag. The Idris2 ABI remains the
source of truth and was left untouched.

Verification:
- `zig test src/main.zig -lc` -> all 9 tests pass, zero errors/warnings.
- `idris2 --build betlangiser-abi.ipkg` -> exit 0 (build dir removed).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019xMKB3T4Vo5FYC7Czx3JSH
@hyperpolymath hyperpolymath marked this pull request as ready for review June 26, 2026 22:08
@hyperpolymath hyperpolymath merged commit eec7d55 into main Jun 26, 2026
20 of 22 checks passed
@hyperpolymath hyperpolymath deleted the claude/new-session-1fphit branch June 26, 2026 22:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants