base-compat: A compatibility layer for base
Provides functions available in later versions of base to
a wider range of compilers, without requiring you to use CPP
pragmas in your code. See the
README
for what is covered. Also see the
changelog
for recent changes.
Note that base-compat does not add any orphan instances.
There is a separate package,
base-orphans ,
for that.
In addition, base-compat does not backport any data types
or type classes. See
this section of the README
for more info.
base-compat is designed to have zero dependencies. For a
version of base-compat that depends on compatibility
libraries for a wider support window, see the
base-compat-batteries
package. Most of the modules in this library have the same
names as in base-compat-batteries to make it easier to
switch between the two. There also exist versions of each
module with the suffix .Repl, which are distinct from
anything in base-compat-batteries, to allow for easier
use in GHCi.
[Skip to Readme]
Modules
[Index] [Quick Jump]
- Control
- Concurrent
- Exception
- Monad
- Control.Monad.Compat
- Fail
- IO
- ST
- Data
- Bifoldable
- Bifoldable1
- Bifunctor
- Bitraversable
- Bits
- Bool
- Complex
- Either
- Foldable
- Foldable1
- Function
- Functor
- Data.Functor.Compat
- Compose
- Const
- Contravariant
- Identity
- Product
- Sum
- IORef
- List
- Monoid
- Proxy
- Ratio
- STRef
- Semigroup
- String
- Traversable
- Tuple
- Type
- Typeable
- Version
- Void
- Word
- Debug
- Foreign
- Foreign.Compat
- ForeignPtr
- Marshal
- Alloc
- Array
- Foreign.Marshal.Compat
- Safe
- Unsafe
- Utils
- Numeric
- Prelude
- System
- Text
- Type
Downloads
- base-compat-0.14.1.tar.gz [browse] (Cabal source package)
- Package description (as included in the package)
Maintainer's Corner
For package maintainers and hackage trustees
Candidates
- No Candidates
| Versions [RSS] | 0.0.0, 0.1.0, 0.1.0.1, 0.2.0, 0.2.0.1, 0.2.0.2, 0.2.1, 0.3.0, 0.4.0, 0.4.1, 0.4.2, 0.5.0, 0.6.0, 0.7.0, 0.7.1, 0.8.0, 0.8.0.1, 0.8.1, 0.8.1.1, 0.8.2, 0.9.0, 0.9.1, 0.9.2, 0.9.3, 0.10.0, 0.10.1, 0.10.2, 0.10.3, 0.10.4, 0.10.5, 0.11.0, 0.11.1, 0.11.2, 0.12.0, 0.12.1, 0.12.2, 0.12.3, 0.13.0, 0.13.1, 0.14.0, 0.14.1 (info) |
|---|---|
| Change log | CHANGES.markdown |
| Dependencies | base (>=4.9 && <5), ghc-prim , unix [details] |
| Tested with | ghc ==8.0.2, ghc ==8.2.2, ghc ==8.4.4, ghc ==8.6.5, ghc ==8.8.4, ghc ==8.10.7, ghc ==9.0.2, ghc ==9.2.8, ghc ==9.4.8, ghc ==9.6.6, ghc ==9.8.2, ghc ==9.10.1, ghc ==9.12.1 |
| License | MIT |
| Copyright | (c) 2012-2018 Simon Hengel, (c) 2014-2018 João Cristóvão, (c) 2015-2018 Ryan Scott |
| Author | Simon Hengel <sol@typeful.net>, João Cristóvão <jmacristovao@gmail.com>, Ryan Scott <ryan.gl.scott@gmail.com> |
| Maintainer | Simon Hengel <sol@typeful.net>, João Cristóvão <jmacristovao@gmail.com>, Ryan Scott <ryan.gl.scott@gmail.com> |
| Category | Compatibility |
| Source repo | head: git clone https://github.com/haskell-compat/base-compat(base-compat) |
| Uploaded | by ryanglscott at 2024年12月06日T12:21:27Z |
| Distributions | Arch:0.14.1, Debian:0.11.1, Fedora:0.13.1, LTSHaskell:0.14.1, NixOS:0.14.1, Stackage:0.14.1, openSUSE:0.14.1 |
| Reverse Dependencies | 166 direct, 8831 indirect [details] |
| Downloads | 217506 total (181 in the last 30 days) |
| Rating | 2.0 (votes: 1) [estimated by Bayesian average] |
| Your Rating |
|
| Status | Docs available [build log] Last success reported on 2024年12月06日 [all 1 reports] |
Readme for base-compat-0.14.1
[back to package description]A compatibility layer for base
Hackage Hackage Dependencies Haskell Programming Language BSD3 License
Scope
The scope of base-compat is to provide functions available in later versions
of base to a wider (older) range of compilers.
In addition, successful library proposals that have been accepted to be part of
upcoming versions of base are also included. This package is not intended to
replace base, but to complement it.
Note that base-compat does not add any orphan instances. There is a separate
package base-orphans for
that.
In addition, base-compat only backports functions. In particular, we
purposefully do not backport data types or type classes introduced in newer
versions of base. For more info, see the
Data types and type classes
section.
base-compat is intentionally designed to have zero dependencies. As a
consequence, there are some modules that can only be backported up to certain
versions of base. If an even wider support window is desired in these
scenarios, there also exists a base-compat-batteries package which augments
base-compat with certain compatibility package dependencies. For more info,
see the Dependencies section.
Basic usage
In your cabal file, you should have something like this:
build-depends: base >= 4.3
, base-compat >= 0.9.0
Then, lets say you want to use the isRight function introduced with
base-4.7.0.0. Replace:
import Data.Either
with
import Data.Either.Compat
Note (1): There is no need to import both unqualified. The .Compat modules
re-exports the original module.
Note (2): If a given module .Compat version is not defined, that either
means that:
- The module has not changed in recent base versions, thus no
.Compatis needed. - The module has changed, but the changes depend on newer versions of GHC, and thus are not portable.
- The module has changed, but those changes have not yet been merged in
base-compat: patches are welcomed!
Using Prelude.Compat
If you want to use Prelude.Compat (which provides all the
AMP/Traversable/Foldable changes from base-4.8.0.0), it's best to hide
Prelude, e.g.:
import Prelude ()
import Prelude.Compat
main :: IO ()
main = mapM_ print (Just 23)
Alternatively, you can use the NoImplicitPrelude language extension:
{-# LANGUAGE NoImplicitPrelude #-}
import Prelude.Compat
main :: IO ()
main = mapM_ print (Just 23)
Note that we use
mapM_ :: (Foldable t, Monad m) => (a -> m b) -> t a -> m ()
from Data.Foldable here, which is only exposed from Prelude since
base-4.8.0.0.
Using this approach allows you to write code that works seamlessly with all
versions of GHC that are supported by base-compat.
What is covered
So far the following is covered.
For compatibility with the latest released version of base
Prelude.Compatincorporates the AMP/Foldable/Traversable changes and exposes the same interface asPreludefrombase-4.9.0.0System.IO.Error.catchis not re-exported fromPrelude.Compatfor older versions ofbaseText.Read.Compat.readMaybeText.Read.Compat.readEitherData.Monoid.Compat.<>- Added
bitDefault,testBitDefault,popCountDefault,(.^.),(.>>.),(.<<.),(!>>.), and(!<<.)toData.Bits.Compat - Added
toIntegralSizedandoneBitstoData.Bits.Compat(if usingbase-4.7or later) - Added
firstAandsecondAtoData.Bitraversable.Compat - Added
boolfunction toData.Bool.Compat - Added
isLeft,isRight,fromLeft, andfromRighttoData.Either.Compat - Added
forkFinallytoControl.Concurrent.Compat - Added
withMVarMaskedfunction toControl.Concurrent.MVar.Compat - Added
(<$!>)function toControl.Monad.Compat - Weakened
RealFloatconstraints onrealPart,imagPart,conjugate,mkPolar, andcisinData.Complex.Compat - Added more efficient
maximumBy/minimumBytoData.Foldable.Compat - Added
($>)andvoidfunctions toData.Functor.Compat (&)andapplyWhenfunctions toData.Function.Compat($>)andvoidfunctions toData.Functor.CompatmodifyIORef',atomicModifyIORef'andatomicWriteIOReftoData.IORef.CompatdropWhileEnd,isSubsequenceOf,sortOn, andunconsfunctions toData.List.Compat- Correct versions of
nub,nubBy,union, andunionBytoData.List.Compat inits1andtails1toData.List.CompatcompareLengthtoData.List.CompatandData.List.NonEmpty.CompatasProxyTypeOfwith a generalized type signature toData.Proxy.CompatmodifySTRef'toData.STRef.CompatString,lines,words,unlines, andunwordstoData.String.CompatgcoerceWithtoData.Type.Coercion.CompatmakeVersionfunction toData.Version.CompattraceId,traceShowId,traceM,traceShowM,traceWith,traceShowWith, andtraceEventWithfunctions toDebug.Trace.CompatbyteSwap16,byteSwap32, andbyteSwap64toData.Word.CompatplusForeignPtrtoForeign.ForeignPtr.CompatcallocandcallocBytesfunctions toForeign.Marshal.Alloc.CompatcallocArrayandcallocArray0functions toForeign.Marshal.Array.CompatfillBytestoForeign.Marshal.Utils.Compat- Added
Data.List.Compat.scanl' showFFloatAlt,showGFloatAlt,readBin, andshowBintoNumeric.CompatlookupEnv,setEnvandunsetEnvtoSystem.Environment.CompatunsafeFixIOandunsafeDupablePerformIOtoSystem.IO.Unsafe.IORuntimeRep-polymorphic($!)toPrelude.CompatliftA2is re-exported fromPrelude.Compatfoldl'is re-exported fromPrelude.CompatRuntimeRep-polymorphicthrowtoControl.Exception.CompatisResourceVanishedError,resourceVanishedErrorType, andisResourceVanishedErrorTypetoSystem.IO.Error.CompatsingletontoData.List.CompatandData.List.NonEmpty.Compatinits1andtails1toData.List.NonEmpty.Compatpermutations,permutations1, andsortOntoData.List.NonEmpty.CompathGetContents',getContents', andreadFile'toSystem.IO.CompatreadBinPtoText.Read.Lex.CompatwithTypeableandpattern TypeReptoType.Reflection.CompatminusNaturalMaybetoNumeric.Natural.CompatmapAccumMandforAccumMtoData.Traversable.CompatheqTtoData.Typeable.CompatunziptoData.Functor.Compat(!?)andunsnoctoData.List.CompatListtoData.List.Compat(when building with GHC 9.6 or later)getSolotoData.Tuple.CompatdecTandhdecTtoData.Typeable.CompatdecTypeReptoType.Reflection.Compat
What is not covered
Data types and type classes
base-compat purposefully does not export any data types or type classes that
were introduced in more recent versions of base. The main reasoning for this
policy is that it is not some data types and type classes have had their APIs
change in different versions of base, which makes having a consistent
compatibility API for them practically impossible.
As an example, consider the FiniteBits type class. It was introduced in
base-4.7.0.0
with the following API:
class Bits b => FiniteBits b where
finiteBitSize :: b -> Int
However, in base-4.8.0.0,
FiniteBits gained additional functions:
class Bits b => FiniteBits b where
finiteBitSize :: b -> Int
countLeadingZeros :: b -> Int -- ^ @since 4.8.0.0
countTrailingZeros :: b -> Int -- ^ @since 4.8.0.0
This raises the question: how can FiniteBits be backported consistently
across all versions of base? One approach is to backport the API exposed in
base-4.8.0.0 on versions prior to 4.7.0.0. The problem with this is that
countLeadingZeros and countTrailingZeros are not exposed in base-4.7.0.0,
so instances of FiniteBits would have to be declared like this:
instance FiniteBits Foo where
finiteBitSize = ...
#if MIN_VERSION_base(4,8,0) || !(MIN_VERSION_base(4,7,0))
countLeadingZeros = ...
countTrailingZeros = ...
#endif
Another approach is to backport the API from base-4.7.0.0 and to declare
additional methods outside of the class:
#if MIN_VERSION_base(4,7,0) && !(MIN_VERSION_base(4,8,0))
countLeadingZeros :: FiniteBits b => b -> Int
countLeadingZeros = {- default implementation #-}
#endif
The situation is only slightly better for classes which exist across all versions of base,
but have grown their API. For example, it's tempting to define
#if !(MIN_VERSION_base(4,8,0))
displayException :: Exception e => e -> String
displayException = show
#endif
As with the previous approach, you won't be able to define new members of the type class without CPP guards. In other words, the non-CPP approach would limit uses to the lowest common denominator.
As neither approach is a very satisfactory solution, and to embrace consistency, we do not pursue either approach. For similar reasons, we do not backport data types.
Dependencies
base-compat is designed to have zero dependencies (besides libraries that
ship with GHC itself). A consequence of this choice is that there are certain
modules that have a "limited" support window. An important example of this is
Prelude.Compat, which backports the Semigroup class to versions of base
older than 4.11 (when it was added to the Prelude). Because Semigroup was
not added to base until base-4.9, base-compat cannot backport it to
any earlier version of base than this.
If you would instead desire to be able to use a version of Prelude.Compat
that does backport Semigroup to even older versions of base, even if it
means pulling in other dependencies, then you are in luck. There also exists
a base-compat-batteries package, which exposes a strict superset of the API
in base-compat. base-compat-batteries has all the same modules as
base-compat, but exposes more functionality on more versions of base by
reexporting things from compatibility libraries whenever necessary. (For
instance, base-compat-batteries exports the Semigroup class from the
semigroups library when built against versions of base older than 4.9.)
Because base-compat and base-compat-batteries have the same module names,
they are quite easy to switch out for one another in library projects, at the
expense of having clashing names if one tries to import them in GHCi. To
work around this issue, base-compat and base-compat-batteries also provide
copies of each module with the suffix .Repl (for base-compat) and
.Repl.Batteries (for base-compat-batteries) to give them globally unique
namespaces in the event one wants to import them into GHCi.
Here is a list of compatibility libraries that base-compat-batteries depends
on, paired with the things that each library backports:
bifunctor-classes-compatfor theBifoldableandBitraversabletype classes, introduced inbase-4.10.0.0contravariantfor theContravarianttype class, introduced inbase-4.12.0.0.foldable1-classes-compatfor theFoldable1andBifoldable1type classes, introduced inbase-4.18.0.0OneTuplefor theSolodata type, introduced inghc-prim-0.7.0
Version policy
This package follows the Haskell Package Versioning
Policy. As such, if a new base-compat release only
adds new exports, then as a general rule, we will release it with a minor
version bump. Moreover, since base-compat does not backport data type or
class definitions (see the "What is not covered" section above), base-compat
usually has fewer major version bumps than base itself.
An exception to the general rule about adding new exports is the
Prelude.Compat module. If a new base-compat release adds any new exports,
then it will always be accompanied by a major version bump, even if there are
no other API changes. This is because of the special nature of
Prelude.Compat, which is designed to be imported unqualified. Pre-emptively
bumping the major version number is meant to signal to downstream libraries
that they should check to see if the new Prelude.Compat additions will clash
with identifiers of the same names in their code.
Supported versions of GHC/base
ghc-9.12.*/base-4.21.*ghc-9.10.*/base-4.20.*ghc-9.8.*/base-4.19.*ghc-9.6.*/base-4.18.*ghc-9.4.*/base-4.17.*ghc-9.2.*/base-4.16.*ghc-9.0.*/base-4.15.*ghc-8.10.*/base-4.14.*ghc-8.8.*/base-4.13.*ghc-8.6.*/base-4.12.*ghc-8.4.*/base-4.11.*ghc-8.2.*/base-4.10.*ghc-8.0.*/base-4.9.*
We also make an attempt to keep base-compat building with GHC HEAD, but due
to its volatility, it may not work at any given point in time. If it doesn't,
please report it!
Patches are welcome; add tests for new code!