Skip to content

Instantly share code, notes, and snippets.

@andrewthad
Last active August 17, 2018 12:52
Show Gist options
  • Save andrewthad/2caf9f00a89fa168c92b204c7ef3f823 to your computer and use it in GitHub Desktop.
Save andrewthad/2caf9f00a89fa168c92b204c7ef3f823 to your computer and use it in GitHub Desktop.
Using unlifted types where lifted types are expected
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeInType #-}
import Data.Primitive
import Data.Primitive.UnliftedArray
import GHC.Types
import GHC.Exts
main :: IO ()
main = do
a@(Array myArr) <- newArray 1 ("foo" :: String) >>= unsafeFreezeArray
UnliftedArray myArrArr <- newUnliftedArray 1 a >>= unsafeFreezeUnliftedArray
putStrLn (myFunc show (2 :: Integer))
putStrLn (myFunc2 (\x -> show (Array x)) myArr)
putStrLn (myBigFunc (+1) show show (2 :: Integer))
putStrLn (myBigFunc2 (\x -> array# (indexUnliftedArray (UnliftedArray x) 0 :: Array String)) (\x -> show (Array x)) (\x -> show (UnliftedArray x :: UnliftedArray (Array String))) myArrArr)
{-# NOINLINE myFunc #-}
myFunc :: (a -> String) -> a -> String
myFunc f a =
let x = f a
in x ++ x
myFunc2 :: forall (a :: TYPE 'UnliftedRep). (a -> String) -> a -> String
myFunc2 = unsafeCoerce# myFunc
{-# NOINLINE myBigFunc #-}
myBigFunc :: (a -> b) -> (b -> String) -> (a -> String) -> a -> String
myBigFunc f g h a =
let y = f a
x = g y
in x ++ x ++ h a
myBigFunc2 :: forall (a :: TYPE 'UnliftedRep) (b :: TYPE 'UnliftedRep). (a -> b) -> (b -> String) -> (a -> String) -> a -> String
myBigFunc2 = unsafeCoerce# myBigFunc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment