1 {-# LANGUAGE FieldSelectors #-}
2 {-# LANGUAGE UndecidableInstances #-}
3 {-# LANGUAGE NoMonomorphismRestriction #-}
4 {-# OPTIONS_GHC -Wno-deprecations #-}
5 {-# OPTIONS_GHC -Wno-orphans #-}
7 module Literate.Prelude (
8 module Literate.Prelude,
9 module Literate.Rebindable,
131 import Control.Applicative (Applicative (..))
132 import Control.Arrow (first, second, (>>>))
133 import Control.DeepSeq (NFData (..))
134 import Control.Monad (foldM, foldM_, forM, forM_, mapM, mapM_, unless, void, when)
135 import Data.Bool (Bool (..), not, otherwise, (&&), (||))
136 import Data.Char (Char)
137 import Data.Either (Either (..), either, fromRight, lefts, partitionEithers, rights)
138 import Data.Eq (Eq (..))
139 import Data.Foldable (Foldable (..), all, and, any, fold, foldMap, foldr, maximum, minimum, null, or)
140 import Data.Function (const, flip, id, on, ($), (&), (.))
141 import Data.Functor (Functor (..), ($>), (<$), (<$>), (<&>))
142 import Data.Functor.Identity (Identity (..))
143 import Data.List ((++))
144 import Data.List qualified as List
145 import Data.List.NonEmpty (NonEmpty (..), nonEmpty)
146 import Data.List.NonEmpty qualified as NonEmpty
147 import Data.Map.Merge.Strict qualified as Map
148 import Data.Map.Strict (Map)
149 import Data.Map.Strict qualified as Map
150 import Data.Maybe (Maybe (..), catMaybes, fromMaybe, isJust, isNothing, listToMaybe, mapMaybe, maybe, maybeToList)
151 import Data.Monoid (Ap (..), Endo (..), Last (..), Monoid (..))
152 import Data.Ord (Down (..), Ord (..), Ordering (..))
153 import Data.Proxy (Proxy (..))
154 import Data.Ratio (Ratio, Rational)
155 import Data.Semigroup (Max (..), Min (..), Semigroup (..), Sum (..))
156 import Data.Set (Set)
157 import Data.Set qualified as Set
158 import Data.String (String)
159 import Data.Text (Text)
160 import Data.Text qualified as Text
161 import Data.Text.Lazy qualified as Text.Lazy
162 import Data.Text.Short (ShortText)
163 import Data.Text.Short qualified as Text.Short
164 import Data.Tuple (curry, fst, snd, uncurry)
165 import Debug.Pretty.Simple (pTrace, pTraceM, pTraceShow, pTraceShowId, pTraceShowM)
166 import GHC.Generics (Generic, Generically (..))
167 import GHC.OverloadedLabels (IsLabel (..))
168 import GHC.Stack (HasCallStack)
169 import GHC.TypeLits (KnownNat (..), KnownSymbol (..), Symbol, natVal, symbolVal)
170 import Literate.Rebindable
171 import Numeric.Natural (Natural)
172 import System.IO (FilePath, IO)
173 import Text.Pretty.Simple (pHPrint, pShow, pShowNoColor)
174 import Text.Show (Show (..))
175 import Type.Reflection (Typeable)
176 import Prelude (Double, Enum (..), Int, Integer, Real (..), error, even, fromIntegral, odd)
178 traceStringM = pTraceM
180 traceShow = pTraceShow
181 traceShowId = pTraceShowId
182 traceShowM = pTraceShowM
184 xtraceStringM _ = return ()
188 xtraceShowM _ = return ()
190 pattern (:=) :: a -> b -> (a, b)
191 pattern (:=) x y = (x, y)
194 class Assoc a b c where
196 instance Assoc a b (a, b) where
199 (<&) :: Functor f => a -> f b -> f a
200 (&>) :: Functor f => f b -> a -> f a
206 -- l <>~ n = over l (<> n)
207 -- {-# INLINE (<>~) #-}
209 -- instance IsList a => IsList (Last a) where
210 -- type Item (Last a) = Item a
211 -- fromList [] = Last Nothing
212 -- fromList xs = Last (Just (fromList xs))
213 -- toList (Last Nothing) = []
214 -- toList (Last (Just x)) = IsList.toList x
216 -- | Like `Last` but `mempty` is `[]` instead not `Nothing`.
217 -- Useful for deriving:
220 -- deriving Semigroup via (ProductSurgery (OnFields Lasts) Foo)
222 newtype Lasts a = Lasts a
223 deriving (Eq, Ord, Show, Generic)
225 instance Semigroup (Lasts [a]) where
229 instance (Monoid a, Semigroup (Lasts a)) => Monoid (Lasts a) where
230 mempty = Lasts mempty
232 newtype Newest a = Newest {unNewest :: a}
233 deriving (Eq, Ord, Generic)
234 deriving newtype (Show)
235 instance Semigroup (Newest a) where
238 newtype MapUnion k a = MapUnion {unMapUnion :: Map.Map k a}
239 deriving (Eq, Ord, Generic, Functor)
240 deriving newtype (Show)
242 -- CorrectionWarning: as of GHC 9.6.6, `Monoid` is not derived correctly via `Generically`:
243 -- it does not reuses `(<>)`.
244 -- See https://github.com/haskell/core-libraries-committee/issues/324
245 -- deriving (Monoid) via (Generically (MapUnion k a))
246 instance (Ord k, Semigroup a) => Semigroup (MapUnion k a) where
247 MapUnion x <> MapUnion y = MapUnion (Map.unionWith (<>) x y)
248 instance (Ord k, Semigroup a) => Monoid (MapUnion k a) where
249 mempty = MapUnion mempty
250 instance (Ord k, Semigroup a) => IsList (MapUnion k a) where
251 type Item (MapUnion k a) = (k, a)
252 fromList = MapUnion . Map.fromListWith (<>)
253 toList = Map.toList . unMapUnion
255 forMap :: (Foldable t, Monoid m) => t a -> (a -> m) -> m
256 forMap = flip foldMap
258 class DropPrefix a where
259 dropPrefix :: a -> a -> a
260 instance DropPrefix Text.Text where
261 dropPrefix p t = t & Text.stripPrefix p & fromMaybe t
262 instance DropPrefix ShortText where
263 dropPrefix p t = t & Text.Short.stripPrefix p & fromMaybe t
265 setSingle = Set.singleton
266 {-# INLINE setSingle #-}
267 setInsert = Set.insert
268 {-# INLINE setInsert #-}
270 {-# INLINE setSize #-}
272 {-# INLINE mapSize #-}
273 mapEachPiece f g h = Map.merge (Map.mapMissing f) (Map.mapMissing g) (Map.zipWithMatched h)
274 {-# NOINLINE mapEachPiece #-}
276 foldMapM :: (Applicative m, Foldable t, Monoid b) => (a -> m b) -> t a -> m b
277 foldMapM f = getAp <$> foldMap (Ap . f)
279 class ToMaybe a b where
280 toMaybe :: a -> Maybe b
281 instance ToMaybe Int Natural where
283 | x >= 0 = Just (fromIntegral x)
284 | otherwise = Nothing
286 -- | Useful to constrain a literal list to a bare list when using `OverloadedLists`.
291 enumAll = enumFrom (toEnum 0)
296 -- instance Fractional a => Fractional (Last a) where
297 -- fromRational = Last . Just . fromRational
299 -- recip = fmap recip
300 -- instance Num a => Num (Last a) where
305 -- signum = fmap signum
306 -- fromInteger = Last . Just . fromInteger
308 class Boolable a where
312 instance Boolable Bool where
315 instance Boolable a => Boolable (Last a) where
322 lookup :: Key a -> a -> Maybe (Value a)
323 instance Ord k => Lookup (Map k a) where
324 type Key (Map k a) = k
325 type Value (Map k a) = a
327 instance Ord a => Lookup (Set a) where
329 type Value (Set a) = ()
331 | Set.member k m = Just ()
332 | otherwise = Nothing
333 instance Ord a => Lookup [a] where
337 | List.elem k m = Just ()
338 | otherwise = Nothing
339 instance Ord k => Lookup (MapUnion k a) where
340 type Key (MapUnion k a) = Key (Map k a)
341 type Value (MapUnion k a) = Value (Map k a)
342 lookup k = unMapUnion >>> lookup k
343 instance Lookup a => Lookup (Last a) where
344 type Key (Last a) = Key a
345 type Value (Last a) = Value a
346 lookup k = getLast >>> maybe Nothing (lookup k)
348 lookupOrDefaultTo d k = lookup k >>> fromMaybe d
349 {-# INLINE lookupOrDefaultTo #-}
351 type Modifier a = a -> a
353 nonEmptyHead = NonEmpty.head
355 headMaybe = listToMaybe
358 | otherwise = Just (List.last xs)
360 chunksOf :: Int -> [a] -> [[a]]
362 chunksOf n xs = ys : chunksOf n zs
364 (ys, zs) = List.splitAt n xs
366 mapFromListCheckingDuplicates :: HasCallStack => Ord k => Show k => [(k, v)] -> Map k v
367 mapFromListCheckingDuplicates = Map.fromListWithKey (\key -> errorShow ("key duplicate" :: Text, key))
369 errorShow :: HasCallStack => Show a => a -> b
370 errorShow x = error $ pShowNoColor x & Text.Lazy.unpack
372 mapButLast :: (a -> a) -> [a] -> [a]
373 mapButLast f (x : y : xs) = f x : mapButLast f (y : xs)
374 mapButLast _f other = other
376 ol0 = List.zip [0 :: Natural ..]
377 ol1 = List.zip [1 :: Natural ..]