P1: fix Zig FFI to build and match the Idris2 ABI#37
Merged
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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. Onlysrc/interface/ffi/src/main.zigis touched; the Idris2 ABI is the source of truth and was left untouched.Errors (from
zig test src/main.zig -lc)src/main.zig:118—@bitCast size mismatch: destination type 'u64' has 64 bits but source type 'i128' has 128 bits. Inbetlangiser_init,std.time.nanoTimestamp()returnsi128, but it was@bitCastdirectly into theu64PRNG seed.src/main.zig:558—name shadows primitive 'u1'. InsampleDistribution(Box-Muller normal sampling), the localsu1/u2shadow the built-in primitive typeu1, which is an error in Zig 0.14.Fixes
i128timestamp asu128, then truncate tou64:@truncate(@as(u128, @bitCast(std.time.nanoTimestamp()))).u1/u2→unif1/unif2.Net diff: 4 insertions, 4 deletions in one file.
ABI fidelity (preserved)
No exported C symbol names, signatures, or integer encodings changed:
C:betlangiser_*symbols inForeign.idrstill map to matchingexport fns.Resultenumok=0 … sampling_failed=6matchesTypes.idrresultToInt.TernaryBoolencodingt_false=0, t_true=1, t_unknown=2matchesternaryToInt.DistributionTagencodingnormal=0 … custom=4matchesdistributionTag.Verification
cd src/interface/ffi && zig test src/main.zig -lc→ all 9 tests pass, zero errors/warnings, exit 0.cd src/interface/abi && idris2 --build betlangiser-abi.ipkg→ exit 0 (build dir removed afterwards).C:<name>inForeign.idrhas a matchingexport fn <name>inmain.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