Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {-# LANGUAGE NoImplicitPrelude #-}
- {-# LANGUAGE MagicHash #-}
- {-# LANGUAGE UnboxedTuples #-}
- module Unsafe.Maybe where
- import GHC.Prim (Addr#,nullAddr#,addrToAny#,anyToAddr#,eqAddr#,unsafeCoerce#)
- import GHC.Magic (runRW#)
- import GHC.Types (Any)
- import Unsafe.Coerce (unsafeCoerce)
- newtype Maybe a = UnsafeMaybe Any
- just :: a -> Maybe a
- just a = UnsafeMaybe (unsafeCoerce a)
- nothing :: Maybe a
- nothing = case addrToAny# nullAddr# of
- (# m #) -> UnsafeMaybe m
- maybe :: b -> (a -> b) -> Maybe a -> b
- maybe n f (UnsafeMaybe pt) =
- case runRW# (anyToAddr# pt) of
- (# _, addr# #) -> case eqAddr# nullAddr# addr# of
- 0# -> case addrToAny# addr# of
- (# a #) -> f a
- _ -> n
Add Comment
Please, Sign In to add comment