Brandon Chinn pushed to branch wip/T26961 at Glasgow Haskell Compiler / GHC
Commits:
-
9fed4b75
by Brandon Chinn at 2026-02-27T23:53:17-08:00
6 changed files:
- libraries/base/changelog.md
- libraries/base/src/Data/RealFloat.hs
- testsuite/tests/interface-stability/base-exports.stdout
- testsuite/tests/interface-stability/base-exports.stdout-javascript-unknown-ghcjs
- testsuite/tests/interface-stability/base-exports.stdout-mingw32
- testsuite/tests/interface-stability/base-exports.stdout-ws-32
Changes:
| ... | ... | @@ -10,6 +10,7 @@ |
| 10 | 10 | * Fix issues with toRational for types capable to represent infinite and not-a-number values ([CLC proposal #338](https://github.com/haskell/core-libraries-committee/issues/338))
|
| 11 | 11 | * Ensure that `rationalToFloat` and `rationalToDouble` always inline in the end. ([CLC proposal #356](https://github.com/haskell/core-libraries-committee/issues/356))
|
| 12 | 12 | * Add new `Data.RealFloat` module re-exporting `RealFloat` from `GHC.Float`
|
| 13 | + * Add `Infinity`, `NegInfinity`, and `NaN` pattern synonyms to `Data.RealFloat`
|
|
| 13 | 14 | * Modify the implementation of `Data.List.sortOn` to use `(>)` instead of `compare`. ([CLC proposal #332](https://github.com/haskell/core-libraries-committee/issues/332))
|
| 14 | 15 | * Add `thenA` and `thenM`. ([CLC proposal #351](https://github.com/haskell/core-libraries-committee/issues/351))
|
| 15 | 16 | * Fix bug where `naturalAndNot` was incorrectly truncating results ([CLC proposal #350](github.com/haskell/core-libraries-committee/issues/350))
|
| 1 | +{-# LANGUAGE PatternSynonyms #-}
|
|
| 1 | 2 | {-# LANGUAGE Safe #-}
|
| 3 | +{-# LANGUAGE ViewPatterns #-}
|
|
| 2 | 4 | |
| 3 | 5 | -- |
|
| 4 | 6 | --
|
| ... | ... | @@ -13,6 +15,41 @@ |
| 13 | 15 | |
| 14 | 16 | module Data.RealFloat (
|
| 15 | 17 | RealFloat (..),
|
| 18 | + |
|
| 19 | + -- * Infinity + NaN
|
|
| 20 | + pattern Infinity,
|
|
| 21 | + pattern NegInfinity,
|
|
| 22 | + pattern NaN,
|
|
| 16 | 23 | ) where
|
| 17 | 24 | |
| 25 | +import Data.Bool
|
|
| 26 | +import GHC.Internal.Data.Ord
|
|
| 18 | 27 | import GHC.Internal.Float
|
| 28 | +import GHC.Internal.Real
|
|
| 29 | + |
|
| 30 | +pattern Infinity :: (RealFloat a) => a
|
|
| 31 | +pattern Infinity <- ((\x -> isInfinite x && x > 0) -> True) where Infinity = 1/0
|
|
| 32 | + |
|
| 33 | +-- | Negative infinity
|
|
| 34 | +--
|
|
| 35 | +-- Provided for convenience. Could also use the following instead:
|
|
| 36 | +-- * Pattern matching: @(negate -> Infinity)@
|
|
| 37 | +-- * Expressions: @-Infinity@
|
|
| 38 | +pattern NegInfinity :: (RealFloat a) => a
|
|
| 39 | +pattern NegInfinity <- ((\x -> isInfinite x && x < 0) -> True) where NegInfinity = -1/0
|
|
| 40 | + |
|
| 41 | +-- | A pattern synonym for NaN values.
|
|
| 42 | +--
|
|
| 43 | +-- Note: Per IEEE 754, NaN is never equal to itself, thus these two snippets
|
|
| 44 | +-- have different behavior:
|
|
| 45 | +--
|
|
| 46 | +-- @
|
|
| 47 | +-- -- foo1 NaN == "a"
|
|
| 48 | +-- foo1 NaN = "a"
|
|
| 49 | +-- foo1 _ = "b"
|
|
| 50 | +--
|
|
| 51 | +-- -- foo2 NaN == "b"
|
|
| 52 | +-- foo2 x = if x == NaN then "a" else "b"
|
|
| 53 | +-- @
|
|
| 54 | +pattern NaN :: (RealFloat a) => a
|
|
| 55 | +pattern NaN <- (isNaN -> True) where NaN = 0/0 |
| ... | ... | @@ -1599,6 +1599,9 @@ module Data.Ratio where |
| 1599 | 1599 | |
| 1600 | 1600 | module Data.RealFloat where
|
| 1601 | 1601 | -- Safety: Safe
|
| 1602 | + pattern Infinity :: forall a. RealFloat a => a
|
|
| 1603 | + pattern NaN :: forall a. RealFloat a => a
|
|
| 1604 | + pattern NegInfinity :: forall a. RealFloat a => a
|
|
| 1602 | 1605 | type RealFloat :: * -> Constraint
|
| 1603 | 1606 | class (GHC.Internal.Real.RealFrac a, GHC.Internal.Float.Floating a) => RealFloat a where
|
| 1604 | 1607 | floatRadix :: a -> GHC.Internal.Bignum.Integer.Integer
|
| ... | ... | @@ -1599,6 +1599,9 @@ module Data.Ratio where |
| 1599 | 1599 | |
| 1600 | 1600 | module Data.RealFloat where
|
| 1601 | 1601 | -- Safety: Safe
|
| 1602 | + pattern Infinity :: forall a. RealFloat a => a
|
|
| 1603 | + pattern NaN :: forall a. RealFloat a => a
|
|
| 1604 | + pattern NegInfinity :: forall a. RealFloat a => a
|
|
| 1602 | 1605 | type RealFloat :: * -> Constraint
|
| 1603 | 1606 | class (GHC.Internal.Real.RealFrac a, GHC.Internal.Float.Floating a) => RealFloat a where
|
| 1604 | 1607 | floatRadix :: a -> GHC.Internal.Bignum.Integer.Integer
|
| ... | ... | @@ -1599,6 +1599,9 @@ module Data.Ratio where |
| 1599 | 1599 | |
| 1600 | 1600 | module Data.RealFloat where
|
| 1601 | 1601 | -- Safety: Safe
|
| 1602 | + pattern Infinity :: forall a. RealFloat a => a
|
|
| 1603 | + pattern NaN :: forall a. RealFloat a => a
|
|
| 1604 | + pattern NegInfinity :: forall a. RealFloat a => a
|
|
| 1602 | 1605 | type RealFloat :: * -> Constraint
|
| 1603 | 1606 | class (GHC.Internal.Real.RealFrac a, GHC.Internal.Float.Floating a) => RealFloat a where
|
| 1604 | 1607 | floatRadix :: a -> GHC.Internal.Bignum.Integer.Integer
|
| ... | ... | @@ -1599,6 +1599,9 @@ module Data.Ratio where |
| 1599 | 1599 | |
| 1600 | 1600 | module Data.RealFloat where
|
| 1601 | 1601 | -- Safety: Safe
|
| 1602 | + pattern Infinity :: forall a. RealFloat a => a
|
|
| 1603 | + pattern NaN :: forall a. RealFloat a => a
|
|
| 1604 | + pattern NegInfinity :: forall a. RealFloat a => a
|
|
| 1602 | 1605 | type RealFloat :: * -> Constraint
|
| 1603 | 1606 | class (GHC.Internal.Real.RealFrac a, GHC.Internal.Float.Floating a) => RealFloat a where
|
| 1604 | 1607 | floatRadix :: a -> GHC.Internal.Bignum.Integer.Integer
|