Skip to content

Replace all fromIntegral with unwitch conversions#210

Merged
jappeace merged 5 commits intojappeace:masterfrom
jappeace-sloth:unwitch-conversions
Apr 20, 2026
Merged

Replace all fromIntegral with unwitch conversions#210
jappeace merged 5 commits intojappeace:masterfrom
jappeace-sloth:unwitch-conversions

Conversation

@jappeace-sloth
Copy link
Copy Markdown

Summary

  • Replaced all 70+ fromIntegral calls across 20 files with explicit, type-safe unwitch conversion functions
  • Added unwitch >= 2.2.0 as a dependency to library, test-suite, and redraw-demo cabal stanzas
  • Added unwitch to both nix overlays (native hpkgs.nix and Android cross-compilation cross-deps.nix)

Conversion patterns

From → To Function Totality
Int32 → CInt Int32.toCInt Total
CInt → Int32 CInt.toInt32 Total
CInt → Int CInt.toInt Total
Int → CInt Int.toCInt Partial (Maybe)
Int → Word8 Int.toWord8 Partial (Maybe)
Word8 → Int Word8.toInt Total
Word8 → Double Word8.toDouble Total
Int32 → Int CInt.toInt . Int32.toCInt Total chain
Int32 → Double Int32.toDouble Total

Test plan

  • cabal build — all 24 modules compile, no warnings
  • cabal test — all tests pass
  • nix-build nix/ci.nix -A native — passes
  • grep -r fromIntegral src/ test/ — zero matches confirmed

🤖 Generated with Claude Code

jappeace-sloth and others added 4 commits April 19, 2026 18:56
Eliminate every fromIntegral call (70+) across 20 files by using
explicit unwitch conversion functions. This makes numeric type
conversions self-documenting and catches unsafe casts at compile time.

Conversion patterns used:
- Int32 ↔ CInt: Int32.toCInt / CInt.toInt32 (total)
- CInt → Int: CInt.toInt (total, for IntMap keys and BS lengths)
- Int → CInt: Int.toCInt with maybe 0 id (partial)
- Int → Word8: Int.toWord8 with maybe 0 id (partial, hex parsing)
- Word8 → Int/Double: Word8.toInt / Word8.toDouble (total)
- Int32 → Int (IntMap keys): CInt.toInt . Int32.toCInt (total chain,
  avoids Int32.toInt which returns Maybe due to Haskell spec)
- Int32/CInt → Double: Int32.toDouble / CInt.toDouble (total)

Added unwitch 2.2.0 to cabal deps (library, test-suite, redraw-demo)
and to both nix overlays (hpkgs.nix for native, cross-deps.nix for
Android cross-compilation).

Prompt: replace all primitive conversions in hatter with unwitch

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
iOS and watchOS use ios-deps.nix (native macOS GHC) rather than
cross-deps.nix (Android cross-GHC). The unwitch package was missing
from the iOS/watchOS haskellPackages overlay, causing "Could not find
module Unwitch.Convert.*" errors in CI.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
mkIOSLib compiles hatter with raw ghc -staticlib, not through cabal,
so its non-boot dependencies must be explicitly provided in the
crossDeps package DB. Before unwitch, hatter only used GHC boot
packages (base, containers, text, etc.). Now it also needs unwitch.

Add hatterOwnDeps list to ios-deps.nix so the unwitch .a/.hi/.conf
are collected even when no consumerCabalFile is provided (as in CI).
This fixes iOS and watchOS builds which both use ios-deps.nix.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
collect-deps.nix only processes packages explicitly in its deps list —
it doesn't follow propagatedBuildInputs. Since hatter's .conf now
references unwitch, unwitch's .a and .conf must also be collected.

Without this, Android builds fail with "unusable due to missing
dependencies: unwitch-2.2.0-..." when importing Hatter.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Comment thread src/Hatter/Action.hs Outdated
-- Total on all GHC-supported platforms (Int >= 32 bits).
-- Uses the total chain Int32 -> CInt -> Int.
int32ToIntKey :: Int32 -> Int
int32ToIntKey = CInt.toInt . Int32.toCInt
Copy link
Copy Markdown
Owner

@jappeace jappeace Apr 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unwitch 3.0.0 makes Int32.toInt total (Int32 -> Int) on GHC, so
the int32ToIntKey helper and CInt.toInt . Int32.toCInt chain are
no longer needed. Replace all 10 copies with direct Int32.toInt
calls and remove the CInt import from files that only used it for
the helper.

Bump unwitch dependency to >= 3.0.0, point nix overlays to the
jappeace/unwitch commit with the total conversion.

Prompt: go simplify conversions here with unwitch 3

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@jappeace jappeace merged commit c18025e into jappeace:master Apr 20, 2026
5 checks passed
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