Created
February 17, 2023 16:43
-
-
Save ncfavier/6dfe5bc3778350d58df8505815664706 to your computer and use it in GitHub Desktop.
Identity function with special treatment to Bool values, by @Player-205
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
{-# LANGUAGE MagicHash, UnboxedTuples #-} | |
import GHC.Prim | |
import Unsafe.Coerce | |
import Prelude hiding (id) | |
main :: IO () | |
main = do | |
print (id True) | |
print (id False) | |
print (id 15) | |
print (id 13.47) | |
print (id "one, two, three") | |
print (id (True, False)) | |
id :: a -> a | |
id !a = case (# eqT, eqF #) of | |
(# 0#, 1# #) -> t | |
(# 1#, 0# #) -> f | |
_ -> a | |
where | |
eqT = reallyUnsafePtrEquality# a t | |
eqF = reallyUnsafePtrEquality# a f | |
!t = unsafeCoerce True | |
!f = unsafeCoerce False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Backlink to Twitter: https://twitter.com/ncfavier/status/1626623354055557127