Skip to content

Instantly share code, notes, and snippets.

@Lysxia
Last active August 2, 2020 01:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lysxia/b9e3e95a0711f87a84f72555da77801b to your computer and use it in GitHub Desktop.
Save Lysxia/b9e3e95a0711f87a84f72555da77801b to your computer and use it in GitHub Desktop.
Big types prevent inlining of their Generic instances. Test case for GHC issue https://gitlab.haskell.org/ghc/ghc/-/issues/18523
{-# OPTIONS_GHC -dsuppress-all -O #-}
{-# LANGUAGE
BangPatterns,
DeriveFunctor,
DeriveFoldable,
DeriveTraversable,
DeriveGeneric,
DerivingVia,
EmptyCase,
EmptyDataDeriving,
TemplateHaskell
#-}
{-# LANGUAGE TypeOperators, TypeFamilies #-}
import GHC.Generics
import Test.Inspection
-- A big sum of stuff
data Big a
= Big0
| Big1 a
| Big2 a a
| Big4 a a a a
| Big5 a a a a a
deriving Generic
{-
-- Handwritten to add INLINE pragmas
instance Generic (Big a) where
type Rep (Big a) =
U1
:+: K1 () a
:+: (K1 () a :*: K1 () a)
:+: (K1 () a :*: K1 () a :*: K1 () a :*: K1 () a)
:+: ( K1 () a :*: K1 () a :*: K1 () a :*: K1 () a :*: K1 () a)
from Big0 = L1 U1
from (Big1 x) = R1 (L1 (K1 x))
from (Big2 x1 x2) = R1 (R1 (L1 (K1 x1 :*: K1 x2)))
from (Big4 x1 x2 x3 x4) = R1 (R1 (R1 (L1 (K1 x1 :*: K1 x2 :*: K1 x3 :*: K1 x4))))
from (Big5 x1 x2 x3 x4 x5) = R1 (R1 (R1 (R1 (K1 x1 :*: K1 x2 :*: K1 x3 :*: K1 x4 :*: K1 x5))))
{-# INLINE from #-}
to (L1 _) = Big0
to (R1 (L1 (K1 x))) = Big1 x
to (R1 (R1 (L1 (K1 x1 :*: K1 x2)))) = Big2 x1 x2
to (R1 (R1 (R1 (L1 (K1 x1 :*: K1 x2 :*: K1 x3 :*: K1 x4))))) = Big4 x1 x2 x3 x4
to (R1 (R1 (R1 (R1 (K1 x1 :*: K1 x2 :*: K1 x3 :*: K1 x4 :*: K1 x5))))) = Big5 x1 x2 x3 x4 x5
{-# INLINE to #-}
-}
testi :: Big a -> Big a
testi = to . from
testj :: Big a -> Big a
testj !x = x
inspect $ 'testi === 'testj
main :: IO ()
main = pure ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment