Guest User

Untitled

a guest
Nov 21st, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. {-# LANGUAGE NoImplicitPrelude #-}
  2. {-# LANGUAGE MagicHash #-}
  3. {-# LANGUAGE UnboxedTuples #-}
  4. module Unsafe.Maybe where
  5.  
  6. import GHC.Prim (Addr#,nullAddr#,addrToAny#,anyToAddr#,eqAddr#,unsafeCoerce#)
  7. import GHC.Magic (runRW#)
  8. import GHC.Types (Any)
  9. import Unsafe.Coerce (unsafeCoerce)
  10.  
  11. newtype Maybe a = UnsafeMaybe Any
  12.  
  13. just :: a -> Maybe a
  14. just a = UnsafeMaybe (unsafeCoerce a)
  15.  
  16. nothing :: Maybe a
  17. nothing = case addrToAny# nullAddr# of
  18. (# m #) -> UnsafeMaybe m
  19.  
  20. maybe :: b -> (a -> b) -> Maybe a -> b
  21. maybe n f (UnsafeMaybe pt) =
  22. case runRW# (anyToAddr# pt) of
  23. (# _, addr# #) -> case eqAddr# nullAddr# addr# of
  24. 0# -> case addrToAny# addr# of
  25. (# a #) -> f a
  26. _ -> n
Add Comment
Please, Sign In to add comment