[Git][ghc/ghc][wip/T26961] Add Infinity/NegInfinity/NaN pattern synonyms (#26961)
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 Add Infinity/NegInfinity/NaN pattern synonyms (#26961) - - - - - 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: ===================================== libraries/base/changelog.md ===================================== @@ -10,6 +10,7 @@ * 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)) * Ensure that `rationalToFloat` and `rationalToDouble` always inline in the end. ([CLC proposal #356](https://github.com/haskell/core-libraries-committee/issues/356)) * Add new `Data.RealFloat` module re-exporting `RealFloat` from `GHC.Float` + * Add `Infinity`, `NegInfinity`, and `NaN` pattern synonyms to `Data.RealFloat` * Modify the implementation of `Data.List.sortOn` to use `(>)` instead of `compare`. ([CLC proposal #332](https://github.com/haskell/core-libraries-committee/issues/332)) * Add `thenA` and `thenM`. ([CLC proposal #351](https://github.com/haskell/core-libraries-committee/issues/351)) * Fix bug where `naturalAndNot` was incorrectly truncating results ([CLC proposal #350](github.com/haskell/core-libraries-committee/issues/350)) ===================================== libraries/base/src/Data/RealFloat.hs ===================================== @@ -1,4 +1,6 @@ +{-# LANGUAGE PatternSynonyms #-} {-# LANGUAGE Safe #-} +{-# LANGUAGE ViewPatterns #-} -- | -- @@ -13,6 +15,41 @@ module Data.RealFloat ( RealFloat (..), + + -- * Infinity + NaN + pattern Infinity, + pattern NegInfinity, + pattern NaN, ) where +import Data.Bool +import GHC.Internal.Data.Ord import GHC.Internal.Float +import GHC.Internal.Real + +pattern Infinity :: (RealFloat a) => a +pattern Infinity <- ((\x -> isInfinite x && x > 0) -> True) where Infinity = 1/0 + +-- | Negative infinity +-- +-- Provided for convenience. Could also use the following instead: +-- * Pattern matching: @(negate -> Infinity)@ +-- * Expressions: @-Infinity@ +pattern NegInfinity :: (RealFloat a) => a +pattern NegInfinity <- ((\x -> isInfinite x && x < 0) -> True) where NegInfinity = -1/0 + +-- | A pattern synonym for NaN values. +-- +-- Note: Per IEEE 754, NaN is never equal to itself, thus these two snippets +-- have different behavior: +-- +-- @ +-- -- foo1 NaN == "a" +-- foo1 NaN = "a" +-- foo1 _ = "b" +-- +-- -- foo2 NaN == "b" +-- foo2 x = if x == NaN then "a" else "b" +-- @ +pattern NaN :: (RealFloat a) => a +pattern NaN <- (isNaN -> True) where NaN = 0/0 ===================================== testsuite/tests/interface-stability/base-exports.stdout ===================================== @@ -1599,6 +1599,9 @@ module Data.Ratio where module Data.RealFloat where -- Safety: Safe + pattern Infinity :: forall a. RealFloat a => a + pattern NaN :: forall a. RealFloat a => a + pattern NegInfinity :: forall a. RealFloat a => a type RealFloat :: * -> Constraint class (GHC.Internal.Real.RealFrac a, GHC.Internal.Float.Floating a) => RealFloat a where floatRadix :: a -> GHC.Internal.Bignum.Integer.Integer ===================================== testsuite/tests/interface-stability/base-exports.stdout-javascript-unknown-ghcjs ===================================== @@ -1599,6 +1599,9 @@ module Data.Ratio where module Data.RealFloat where -- Safety: Safe + pattern Infinity :: forall a. RealFloat a => a + pattern NaN :: forall a. RealFloat a => a + pattern NegInfinity :: forall a. RealFloat a => a type RealFloat :: * -> Constraint class (GHC.Internal.Real.RealFrac a, GHC.Internal.Float.Floating a) => RealFloat a where floatRadix :: a -> GHC.Internal.Bignum.Integer.Integer ===================================== testsuite/tests/interface-stability/base-exports.stdout-mingw32 ===================================== @@ -1599,6 +1599,9 @@ module Data.Ratio where module Data.RealFloat where -- Safety: Safe + pattern Infinity :: forall a. RealFloat a => a + pattern NaN :: forall a. RealFloat a => a + pattern NegInfinity :: forall a. RealFloat a => a type RealFloat :: * -> Constraint class (GHC.Internal.Real.RealFrac a, GHC.Internal.Float.Floating a) => RealFloat a where floatRadix :: a -> GHC.Internal.Bignum.Integer.Integer ===================================== testsuite/tests/interface-stability/base-exports.stdout-ws-32 ===================================== @@ -1599,6 +1599,9 @@ module Data.Ratio where module Data.RealFloat where -- Safety: Safe + pattern Infinity :: forall a. RealFloat a => a + pattern NaN :: forall a. RealFloat a => a + pattern NegInfinity :: forall a. RealFloat a => a type RealFloat :: * -> Constraint class (GHC.Internal.Real.RealFrac a, GHC.Internal.Float.Floating a) => RealFloat a where floatRadix :: a -> GHC.Internal.Bignum.Integer.Integer View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/9fed4b7596602f50d3b76ff0ffd849d9... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/9fed4b7596602f50d3b76ff0ffd849d9... You're receiving this email because of your account on gitlab.haskell.org.
participants (1)
-
Brandon Chinn (@brandonchinn178)