on 1.0.2.0 help sign in
117 lines 3.6 kB history raw
1{-# LANGUAGE CPP #-}
2{-# LANGUAGE DeriveDataTypeable #-}
3{-# LANGUAGE DeriveGeneric #-}
4{-# LANGUAGE FlexibleInstances #-}
5{-# LANGUAGE GADTs #-}
6{-# LANGUAGE GeneralizedNewtypeDeriving #-}
7{-# LANGUAGE StandaloneDeriving #-}
9module Types where
11import Prelude ()
12import Prelude.Compat
14import Data.Data
15import Data.Functor.Compose (Compose (..))
16import Data.Functor.Identity (Identity (..))
17import Data.Text
18import GHC.Generics
19import Test.QuickCheck (Property, counterexample)
20import qualified Data.Map as Map
22type I = Identity
23type Compose3 f g h = Compose (Compose f g) h
24type Compose3' f g h = Compose f (Compose g h)
26data Foo = Foo {
27 fooInt :: Int
28 , fooDouble :: Double
29 , fooTuple :: (String, Text, Int)
30 -- This definition causes an infinite loop in genericTo and genericFrom!
31 -- , fooMap :: Map.Map String Foo
32 , fooMap :: Map.Map String (Text,Int)
33 } deriving (Show, Typeable, Data)
35data UFoo = UFoo {
36 _UFooInt :: Int
37 , uFooInt :: Int
38 } deriving (Show, Eq, Data, Typeable)
40data OneConstructor = OneConstructor
41 deriving (Show, Eq, Typeable, Data)
43data Product2 a b = Product2 a b
44 deriving (Show, Eq, Typeable, Data)
46data Product6 a b c d e f = Product6 a b c d e f
47 deriving (Show, Eq, Typeable, Data)
49data Sum4 a b c d = Alt1 a | Alt2 b | Alt3 c | Alt4 d
50 deriving (Show, Eq, Typeable, Data)
52class ApproxEq a where
53 (=~) :: a -> a -> Bool
55newtype Approx a = Approx { fromApprox :: a }
56 deriving (Show, Data, Typeable, ApproxEq, Num)
58instance (ApproxEq a) => Eq (Approx a) where
59 Approx a == Approx b = a =~ b
61data Nullary = C1 | C2 | C3 deriving (Eq, Show)
63data SomeType a = Nullary
64 | Unary Int
65 | Product String (Maybe Char) a
66 | Record { testOne :: Double
67 , testTwo :: Maybe Bool
68 , testThree :: Maybe a
69 }
70 | List [a]
71 deriving (Eq, Show)
73-- | This type requires IncoherentInstances for the instances of the type
74-- classes Data.Aeson.TH.LookupField and Data.Aeson.Types.FromJSON.FromRecord.
75--
76-- The minimum known requirements for this type are:
77-- * Record type with at least two fields
78-- * One field type is either a type parameter or a type/data family
79-- * Another field type is a @Maybe@ of the above field type
80data IncoherentInstancesNeeded a = IncoherentInstancesNeeded
81 { incoherentInstancesNeededMaybeNot :: a
82 , incoherentInstancesNeededMaybeYes :: Maybe a
83 } deriving Generic
85-- Used for testing UntaggedValue SumEncoding
86data EitherTextInt
87 = LeftBool Bool
88 | RightInt Int
89 | BothTextInt Text Int
90 | NoneNullary
91 deriving (Eq, Show)
93data GADT a where
94 GADT :: { gadt :: String } -> GADT String
95 deriving Typeable
97deriving instance Data (GADT String)
98deriving instance Eq (GADT a)
99deriving instance Show (GADT a)
101deriving instance Generic Foo
102deriving instance Generic UFoo
103deriving instance Generic OneConstructor
104deriving instance Generic (Product2 a b)
105deriving instance Generic (Product6 a b c d e f)
106deriving instance Generic (Sum4 a b c d)
107deriving instance Generic (Approx a)
108deriving instance Generic Nullary
109deriving instance Generic (SomeType a)
110#if __GLASGOW_HASKELL__ >= 706
111deriving instance Generic1 SomeType
112#endif
113deriving instance Generic EitherTextInt
115failure :: Show a => String -> String -> a -> Property
116failure func msg v = counterexample
117 (func ++ " failed: " ++ msg ++ ", " ++ show v) False