Prelude.hs 5.17 KB
Newer Older
1
{-# LANGUAGE CPP #-}
2
{-# LANGUAGE TypeOperators #-}
3
4
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE FlexibleContexts #-}
5
6
7
8
9
10
11
12
13
14
15

#ifdef MIN_VERSION_base
#define MINVER_base_48 MIN_VERSION_base(4,8,0)
#define MINVER_base_47 MIN_VERSION_base(4,7,0)
#define MINVER_base_46 MIN_VERSION_base(4,6,0)
#else
#define MINVER_base_48 (__GLASGOW_HASKELL__ >= 710)
#define MINVER_base_47 (__GLASGOW_HASKELL__ >= 708)
#define MINVER_base_46 (__GLASGOW_HASKELL__ >= 706)
#endif

16
17
18
19
20
-- | This module does two things:
--
-- * Acts as a compatiblity layer, like @base-compat@.
--
-- * Provides commonly used imports.
21
22
23
module Distribution.Compat.Prelude (
    -- * Prelude
    --
24
    -- Prelude is re-exported, following is hidden:
25
26
27
28
29
30
31
32
33
34
35
36
37
38
    module BasePrelude,

#if !MINVER_base_48
    -- * base 4.8 shim
    Applicative(..), (<$), (<$>),
    Monoid(..),
#endif

    -- * Common type-classes
    Semigroup (..),
    gmappend, gmempty,
    Typeable,
    Data,
    Generic,
39
    NFData (..), genericRnf,
40
41
42
43
44
    Binary (..),
    Alternative (..),
    MonadPlus (..),

    -- * Some types
45
    IO, NoCallStackIO,
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
    Map,

    -- * Data.Maybe
    catMaybes, mapMaybe,
    fromMaybe,
    maybeToList, listToMaybe,
    isNothing, isJust,

    -- * Data.List
    unfoldr,
    isPrefixOf, isSuffixOf,
    intercalate, intersperse,
    sort, sortBy,
    nub, nubBy,

    -- * Data.Foldable
    Foldable, foldMap, foldr,
    null, length,
    find, foldl',
    traverse_, for_,

    -- * Data.Traversable
    Traversable, traverse, sequenceA,
    for,

    -- * Control.Arrow
    first,

    -- * Control.Monad
    liftM, liftM2,
    unless, when,
    ap, void,
    foldM, filterM,

    -- * Data.Char
    isSpace, isDigit, isUpper, isAlpha, isAlphaNum,
    chr, ord,
    toLower, toUpper,

    -- * Text.PrettyPrint
    (<<>>),
    ) where

-- We also could hide few partial function
import Prelude                       as BasePrelude hiding
91
  ( IO, mapM, mapM_, sequence, null, length, foldr
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#if MINVER_base_48
  -- We hide them, as we import only some members
  , Traversable, traverse, sequenceA
  , Foldable, foldMap
#endif
  )

#if !MINVER_base_48
import Control.Applicative           (Applicative (..), (<$), (<$>))
import Distribution.Compat.Semigroup (Monoid (..))
#else
import Data.Foldable                 (length, null)
#endif

import Data.Foldable                 (Foldable (foldMap, foldr), find, foldl', for_, traverse_)
import Data.Traversable              (Traversable (traverse, sequenceA), for)

import Control.Applicative           (Alternative (..))
import Control.DeepSeq               (NFData (..))
import Data.Data                     (Data)
import Data.Typeable                 (Typeable)
import Distribution.Compat.Binary    (Binary (..))
import Distribution.Compat.Semigroup (Semigroup (..), gmappend, gmempty)
115
116
117
import GHC.Generics                  (Generic, Rep(..),
                                      V1, U1(U1), K1(unK1), M1(unM1),
                                      (:*:)((:*:)), (:+:)(L1,R1))
118
119
120
121
122
123
124
125
126
127
128
129
130

import Data.Map                      (Map)

import Control.Arrow                 (first)
import Control.Monad                 hiding (mapM)
import Data.Char
import Data.List                     (intercalate, intersperse, isPrefixOf,
                                      isSuffixOf, nub, nubBy, sort, sortBy,
                                      unfoldr)
import Data.Maybe

import qualified Text.PrettyPrint as Disp

131
132
133
134
135
136
import qualified Prelude as OrigPrelude
import Distribution.Compat.Stack

type IO a = WithCallStack (OrigPrelude.IO a)
type NoCallStackIO a = OrigPrelude.IO a

137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
-- | New name for 'Text.PrettyPrint.<>'
(<<>>) :: Disp.Doc -> Disp.Doc -> Disp.Doc
(<<>>) = (Disp.<>)

#if !MINVER_base_48
-- | Test whether the structure is empty. The default implementation is
-- optimized for structures that are similar to cons-lists, because there
-- is no general way to do better.
null :: Foldable t => t a -> Bool
null = foldr (\_ _ -> False) True

-- | Returns the size/length of a finite structure as an 'Int'.  The
-- default implementation is optimized for structures that are similar to
-- cons-lists, because there is no general way to do better.
length :: Foldable t => t a -> Int
length = foldl' (\c _ -> c+1) 0
#endif
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195


-- | "GHC.Generics"-based 'rnf' implementation
--
-- This is needed in order to support @deepseq < 1.4@ which didn't
-- have a 'Generic'-based default 'rnf' implementation yet.
--
-- In order to define instances, use e.g.
--
-- > instance NFData MyType where rnf = genericRnf
--
-- The implementation has been taken from @deepseq-1.4.2@'s default
-- 'rnf' implementation.
genericRnf :: (Generic a, GNFData (Rep a)) => a -> ()
genericRnf = grnf . from

-- | Hidden internal type-class
class GNFData f where
  grnf :: f a -> ()

instance GNFData V1 where
  grnf = error "Control.DeepSeq.rnf: uninhabited type"

instance GNFData U1 where
  grnf U1 = ()

instance NFData a => GNFData (K1 i a) where
  grnf = rnf . unK1
  {-# INLINEABLE grnf #-}

instance GNFData a => GNFData (M1 i c a) where
  grnf = grnf . unM1
  {-# INLINEABLE grnf #-}

instance (GNFData a, GNFData b) => GNFData (a :*: b) where
  grnf (x :*: y) = grnf x `seq` grnf y
  {-# INLINEABLE grnf #-}

instance (GNFData a, GNFData b) => GNFData (a :+: b) where
  grnf (L1 x) = grnf x
  grnf (R1 x) = grnf x
  {-# INLINEABLE grnf #-}