summaryrefslogtreecommitdiff
path: root/testsuite/tests/generics/GFullyStrict.hs
blob: 7c879d9877c732dc9b84c5046e0b66767e529a2f (plain)
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeOperators #-}
module Main where

import Data.Proxy (Proxy(..))
import GHC.Generics

main :: IO ()
main = do
  print (fullyStrict (Proxy :: Proxy (StrictMaybe Bool)))
  print (fullyStrict (Proxy :: Proxy (Maybe Bool)))

data StrictMaybe a = StrictNothing | StrictJust !a
  deriving (FullyStrict, Generic)

instance FullyStrict Bool
instance FullyStrict a => FullyStrict (Maybe a)

class FullyStrict a where
  fullyStrict :: proxy a -> Bool
  default fullyStrict :: (GFullyStrict (Rep a)) => proxy a -> Bool
  fullyStrict _ = gfullyStrict (Proxy :: Proxy (Rep a p))

class GFullyStrict f where
  gfullyStrict :: proxy (f p) -> Bool

instance GFullyStrict V1 where
  gfullyStrict _ = True

instance GFullyStrict U1 where
  gfullyStrict _ = True

instance FullyStrict c => GFullyStrict (Rec0 c) where
  gfullyStrict _ = fullyStrict (Proxy :: Proxy c)

instance GFullyStrict f => GFullyStrict (D1 c f) where
  gfullyStrict _ = gfullyStrict (Proxy :: Proxy (f p))

instance GFullyStrict f => GFullyStrict (C1 c f) where
  gfullyStrict _ = gfullyStrict (Proxy :: Proxy (f p))

instance (GFullyStrict f, Selector c) => GFullyStrict (S1 c f) where
  gfullyStrict _ = gfullyStrict (Proxy :: Proxy (f p))
                && selDecidedStrictness (undefined :: S1 c f p) /= DecidedLazy

instance (GFullyStrict f, GFullyStrict g) => GFullyStrict (f :+: g) where
  gfullyStrict _ =
    gfullyStrict (Proxy :: Proxy (f p)) && gfullyStrict (Proxy :: Proxy (g p))

instance (GFullyStrict f, GFullyStrict g) => GFullyStrict (f :*: g) where
  gfullyStrict _ =
    gfullyStrict (Proxy :: Proxy (f p)) && gfullyStrict (Proxy :: Proxy (g p))