Created
November 18, 2025 11:55
-
-
Save LSLeary/91103a2fbb35d1c908802fa81d82c110 to your computer and use it in GitHub Desktop.
Arch-dependent coercions?
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
| {-# OPTIONS_GHC -Wno-inaccessible-code -Wno-overlapping-patterns #-} | |
| {-# LANGUAGE GHC2021, GADTs #-} | |
| module ArchWS (ArchWS(..), archWS) where | |
| -- base | |
| import Data.Coerce (Coercible, coerce) | |
| import Data.Type.Coercion (Coercion(..)) | |
| import Data.Word (Word32, Word64) | |
| import Data.Int (Int32, Int64) | |
| import Data.Bits (finiteBitSize) | |
| import Unsafe.Coerce (unsafeCoerce) | |
| data ArchWS where | |
| A32 :: (Coercible Word32 Word, Coercible Int32 Int) => ArchWS | |
| A64 :: (Coercible Word64 Word, Coercible Int64 Int) => ArchWS | |
| UNK :: ArchWS | |
| archWS :: ArchWS | |
| archWS = case wordSize of | |
| 32 -> case unsafeWord32 of | |
| Coercion -> case unsafeInt32 of | |
| Coercion -> A32 | |
| 64 -> case unsafeWord64 of | |
| Coercion -> case unsafeInt64 of | |
| Coercion -> A64 | |
| _ -> UNK | |
| where | |
| wordSize :: Int | |
| wordSize = finiteBitSize (0 :: Word) | |
| unsafeWord32 :: Coercion Word32 Word | |
| unsafeWord32 = unsafeCoerce (Coercion @Word @Word) | |
| unsafeInt32 :: Coercion Int32 Int | |
| unsafeInt32 = unsafeCoerce (Coercion @Int @Int) | |
| unsafeWord64 :: Coercion Word64 Word | |
| unsafeWord64 = unsafeCoerce (Coercion @Word @Word) | |
| unsafeInt64 :: Coercion Int64 Int | |
| unsafeInt64 = unsafeCoerce (Coercion @Int @Int) | |
| _test :: IO () | |
| _test = case archWS of | |
| A32 -> print (coerce (32 :: Word32) :: Word) | |
| A64 -> print (coerce (64 :: Word64) :: Word) | |
| UNK -> putStrLn "unknown" | |
| -- $> _test |
Author
LSLeary
commented
Nov 18, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment