papa-0.7.0: src/Papa.hs
{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -Wall #-}
-- | Papa: a total, lens-based, type class-driven prelude.
--
-- > {-# LANGUAGE NoImplicitPrelude #-}
-- > import Papa
--
-- == Principles
--
-- * __No partial functions.__ Nothing that @import Papa@ brings into scope
-- throws, loops on finite input, or returns @undefined@: no @head@ of a
-- list, @fromJust@, @read@, @error@, @div@ by zero, lazy I/O or
-- @fromIntegral@ that wraps. The test suite checks this: it does not
-- compile if "Papa" exports one of the denied names, or a reused name at
-- its partial type.
--
-- * __Names that should never have been partial are reused for total
-- functions.__ 'head', 'last', 'maximum' take a
-- 'Data.List.NonEmpty.NonEmpty' (any 'Data.Semigroup.Foldable.Foldable1');
-- ('!!'), 'succ', 'pred', 'chr', 'cycle' return a 'Data.Maybe.Maybe'.
--
-- * __Partial functions that ought to exist are in "Papa.Unsafe", under
-- their own names.__ @div@, @(/)@, @truncate@, @printf@, @error@, … are in
-- "Papa.Unsafe" (which is "Papa" and all of them), or one area at a time
-- in "Papa.Unsafe.Numeric", "Papa.Unsafe.Format", "Papa.Unsafe.Exception"
-- and "Papa.Unsafe.IO". "Papa" has total versions under other names:
-- 'divNonZero' takes a 'NonZero' divisor; 'roundMaybe' returns 'Nothing'
-- for NaN, infinities and values out of range; 'intCast' compiles only
-- where it is lossless.
--
-- * __I\/O errors are values.__ An I\/O action that can fail has type
-- @'IOExcept' a@, which is @'ExceptT' 'IOFailure' 'IO' a@: its failure is
-- the 'Data.Either.Left' of 'runExceptT', not an exception. An
-- 'IOFailure' is the operating system's 'IOException' ('_IOFailed'; take
-- it apart with 'errorType', '_NoSuchThing', …) or bytes that are not
-- UTF-8 ('_DecodeFailed'). 'exceptIO' brings any other library's I\/O
-- into 'IOExcept'. Asynchronous exceptions are never caught; 'catch',
-- 'try', 'handle' and 'bracket' are safe with them.
--
-- * __Text first.__ Strings are 'Text'; 'putStrLn', 'readFile', 'lines',
-- 'words' are the 'Text' ones. Text I\/O is strict and UTF-8 whatever the
-- locale.
--
-- * __Optics first.__ Papa exports the core of lens ("Papa.Lens.Core";
-- "Papa.Lens" is all of lens less its partial combinators), and its own
-- functions are optics or classy optics where it can.
--
-- == Example
--
-- > {-# LANGUAGE NoImplicitPrelude #-}
-- > {-# LANGUAGE OverloadedStrings #-}
-- > import Papa
-- >
-- > -- | The number of lines of a file, or why not.
-- > countLines :: FilePath -> IOExcept Int
-- > countLines p =
-- > length . lines <$> readFile p
-- >
-- > main :: IO ()
-- > main = do
-- > r <- runExceptT (countLines "/etc/hosts" >>= print)
-- > either (die . pack . displayException) pure r
--
-- == What it re-exports
--
-- The total part of base ("Papa.Base"), the core of lens, bifunctors
-- ("Papa.Bifunctors"), semigroupoids ("Papa.Semigroupoids"), the
-- 'Control.Comonad.Comonad' and 'Control.Comonad.Store.ComonadStore'
-- comonads, mtl's 'StateT', 'ReaderT' and 'ExceptT', STM, time, and the
-- types (only) of text, bytestring, containers, vector and
-- unordered-containers, whose functions are imported qualified.
--
-- == Modules that import unqualified alongside "Papa"
--
-- > import Papa.Base.Control.Arrow -- Arrow (first/second as firstA/secondA)
-- > import Papa.Base.Control.Monad.Reader -- MonadReader
-- > import Papa.Base.Control.Monad.State -- MonadState
-- > import Papa.Base.Control.Monad.Writer -- MonadWriter
-- > import Papa.Base.Control.Monad.Error -- MonadError
-- > import Papa.Base.Control.Monad.Cont -- MonadCont
-- > import Papa.Base.Control.Monad.Trans -- MonadTrans
-- > import Papa.Contravariant -- Contravariant, Divisible, Decidable
-- > import Papa.Distributive -- Distributive
-- > import Papa.Adjunctions -- Adjunction, Representable
-- > import Papa.Polytree -- Polytree
-- > import Papa.Selective -- Selective
-- > import Papa.Associative.Renamed -- hides null
-- > import Papa.One.Renamed -- hides nonEmpty
-- > import Papa.Profunctors.Renamed -- hides WrappedArrow
-- > import Papa.Witherable.Renamed -- hides filter, mapMaybe, catMaybes
--
-- "Papa.Lens" (all of lens, less its partial combinators) imports alongside
-- "Papa", but its names clash with "Papa.Adjunctions" (@index@),
-- "Papa.Polytree" (@levels@) and "Papa.Selective" (@Over@).
--
-- == Modules to import qualified
--
-- Their names clash with each other or with "Papa". Each replaces the
-- partial functions of its library module (@Map.!@, @Set.findMin@,
-- @Vector.head@, @Text.head@, …) with total ones of the same names, which
-- return 'Data.Maybe.Maybe', or hides them where a total one already exists
-- (@ix@, 'Control.Lens.At').
--
-- > import qualified Papa.Text.Data.Text as Text
-- > import qualified Papa.Text.Data.Text.Lazy as LazyText
-- > import qualified Papa.Text.Data.Text.IO as TextIO
-- > import qualified Papa.Text.Data.Text.Read as TextRead
-- > import qualified Papa.Bytestring.Data.ByteString as ByteString
-- > import qualified Papa.Bytestring.Data.ByteString.Char8 as Char8
-- > import qualified Papa.Bytestring.Data.ByteString.Lazy as LazyByteString
-- > import qualified Papa.Bytestring.Data.ByteString.Builder as Builder
-- > import qualified Papa.Vector.Data.Vector as Vector
-- > import qualified Papa.Vector.Data.Vector.Unboxed as Unboxed
-- > import qualified Papa.UnorderedContainers.Data.HashMap as HashMap
-- > import qualified Papa.UnorderedContainers.Data.HashSet as HashSet
-- > import qualified Papa.Containers.Data.Map as Map
-- > import qualified Papa.Containers.Data.Set as Set
-- > import qualified Papa.Containers.Data.IntMap as IntMap
-- > import qualified Papa.Containers.Data.IntSet as IntSet
-- > import qualified Papa.Containers.Data.Seq as Seq
-- > import qualified Papa.Containers.Data.Tree as Tree
-- > import qualified Papa.Comonad.Control.Comonad.Env as Env -- ask, asks, local clash with MonadReader
-- > import qualified Papa.Comonad.Control.Comonad.Traced as Traced
-- > import qualified Papa.Free.Control.Monad.Free as Free
-- > import qualified Papa.Free.Control.Applicative.Free as FreeAp
-- > import qualified Papa.Free.Control.Comonad.Cofree as Cofree
-- > import qualified Papa.Id.Data.Id as Id
-- > import qualified Papa.Id.Data.BiId as BiId
--
-- == Partial functions
--
-- > import Papa.Unsafe -- Papa, and every partial function
-- > import Papa.Unsafe.Numeric -- div, (/), truncate, fromIntegral, …
-- > import Papa.Unsafe.Format -- printf
-- > import Papa.Unsafe.Exception -- throw, error, undefined
-- > import qualified Papa.Unsafe.IO as UnsafeIO -- System.IO, lazy and throwing
--
-- == Known remaining partiality
--
-- Class methods that Papa exports with their class, and syntax that GHC
-- desugars whatever is in scope, are partial at some types: @(-)@ and
-- @negate@ at @Natural@ throw (Papa exports neither the type nor
-- @fromInteger@), fixed-size arithmetic wraps, and a numeric literal that
-- does not fit its type wraps (@-Woverflowed-literals@ warns). Range syntax
-- (@[a, b .. c]@) uses the methods of 'Enum', which Papa does not export, and is
-- surprising at 'Double'.
module Papa (
module Papa.Base,
module Papa.Async.Control.Concurrent.Async,
module Papa.Directory.System.Directory,
module Papa.Filepath.System.FilePath,
module Papa.Bifunctors,
module Papa.Comonad.Control.Comonad,
module Papa.Comonad.Control.Comonad.Store,
module Papa.Lens.Core,
module Papa.Lens.Control.Exception,
module Papa.Lens.System.IO.Error,
module Papa.Lens.Data.List,
module Papa.Lens.Data.Tuple,
module Papa.Containers,
module Papa.Lens.Optic,
module Papa.Text.Data.Text,
module Papa.Text.Data.Text.IO,
module Papa.Text.Data.Text.Encoding,
module Papa.Bytestring.Data.ByteString,
module Papa.Stm.Control.Concurrent.STM,
module Papa.Time.Data.Time,
module Papa.Semigroupoids,
module Papa.Hashable.Data.Hashable,
module Papa.UnorderedContainers.Data.HashMap,
module Papa.UnorderedContainers.Data.HashSet,
module Papa.Vector.Data.Vector,
) where
import Papa.Async.Control.Concurrent.Async
import Papa.Base hiding (die, readEither, readIO, readMaybe)
import Papa.Bifunctors
import Papa.Bytestring.Data.ByteString (ByteString)
import Papa.Comonad.Control.Comonad
import Papa.Comonad.Control.Comonad.Store
import Papa.Containers
import Papa.Directory.System.Directory
import Papa.Filepath.System.FilePath
import Papa.Hashable.Data.Hashable (Hashable)
import Papa.Lens.Control.Exception
import Papa.Lens.Core
import Papa.Lens.Data.List
import Papa.Lens.Data.Tuple
import Papa.Lens.Optic
import Papa.Lens.System.IO.Error
import Papa.Semigroupoids
import Papa.Stm.Control.Concurrent.STM
import Papa.Text.Data.Text (Text, display, displayExponent, displayFixed, lines, pack, readEither, readMaybe, unlines, unpack, unwords, words)
import Papa.Text.Data.Text.Encoding (decodeUtf8', encodeUtf8)
import Papa.Text.Data.Text.IO (
appendFile,
die,
getContents,
getLine,
hGetContents,
hGetLine,
hPrint,
hPutStr,
hPutStrLn,
interact,
print,
putStr,
putStrLn,
readFile,
readIO,
writeFile,
)
import Papa.Time.Data.Time
import Papa.UnorderedContainers.Data.HashMap (HashMap)
import Papa.UnorderedContainers.Data.HashSet (HashSet)
import Papa.Vector.Data.Vector (Vector)