File: Types.hs

package info (click to toggle)
haskell-th-compat 0.1.6-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 112 kB
  • sloc: haskell: 499; makefile: 3
file content (41 lines) | stat: -rw-r--r-- 1,373 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
{-# LANGUAGE CPP #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE TemplateHaskell #-}

#if MIN_VERSION_template_haskell(2,16,0)
{-# LANGUAGE UnliftedNewtypes #-}
#endif
module Types (Foo(..)) where

import Language.Haskell.TH.Syntax hiding (newName)

#if MIN_VERSION_template_haskell(2,16,0)
import GHC.Exts (Int#)
import Language.Haskell.TH.Syntax.Compat
#endif

data Foo = MkFoo deriving (Eq, Show)

-- An example of how to use liftTypedFromUntypedSplice to minimize the amount
-- of CPP one has to use when manually defining `liftTyped` in `Lift` instance.
-- This example is contrived, since you could just as well derive this
-- particular `Lift` instance, but the same template will carry over to `Lift`
-- instances that cannot be derived.
instance Lift Foo where
  lift MkFoo = [| MkFoo |]
#if MIN_VERSION_template_haskell(2,16,0)
  liftTyped = liftTypedFromUntypedSplice
#endif

#if MIN_VERSION_template_haskell(2,16,0)
newtype UN = MkUN Int#

-- An example of how to use unsafeSpliceCoerce to manually define liftTyped
-- for an unlifted type in a backwards-compatible way. This example is
-- contrived, since you could just as well derive this particular `Lift`
-- instance, but the same template will carry over to `Lift` instances that
-- cannot be derived.
instance Lift UN where
  lift (MkUN i#) = [| MkUN i# |]
  liftTyped x = unsafeSpliceCoerce (lift x)
#endif