-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | A numeric class hierarchy. -- -- This package provides alternative numeric classes over Prelude. -- -- The numeric class constellation looks somewhat like: -- -- --
-- >>> {-# LANGUAGE RebindableSyntax #-}
--
-- >>> import NumHask.Prelude
--
--
-- See NumHask for a detailed overview.
@package numhask
@version 0.8.1.0
-- | Additive classes
module NumHask.Algebra.Additive
-- | or Addition
--
-- For practical reasons, we begin the class tree with Additive.
-- Starting with Associative and Unital, or using
-- Semigroup and Monoid from base tends to confuse the
-- interface once you start having to disinguish between (say) monoidal
-- addition and monoidal multiplication.
--
-- -- \a -> zero + a == a -- \a -> a + zero == a -- \a b c -> (a + b) + c == a + (b + c) -- \a b -> a + b == b + a ---- -- By convention, (+) is regarded as commutative, but this is not -- universal, and the introduction of another symbol which means -- non-commutative addition seems a bit dogmatic. -- --
-- >>> zero + 1 -- 1 ---- --
-- >>> 1 + 1 -- 2 --class Additive a (+) :: Additive a => a -> a -> a zero :: Additive a => a infixl 6 + -- | Compute the sum of a Foldable. sum :: (Additive a, Foldable f) => f a -> a -- | or Subtraction -- --
-- \a -> a - a == zero -- \a -> negate a == zero - a -- \a -> negate a + a == zero -- \a -> a + negate a == zero ---- --
-- >>> negate 1 -- -1 ---- --
-- >>> 1 - 2 -- -1 --class (Additive a) => Subtractive a negate :: Subtractive a => a -> a (-) :: Subtractive a => a -> a -> a infixl 6 - instance NumHask.Algebra.Additive.Subtractive GHC.Types.Double instance NumHask.Algebra.Additive.Subtractive GHC.Types.Float instance NumHask.Algebra.Additive.Subtractive GHC.Types.Int instance NumHask.Algebra.Additive.Subtractive GHC.Integer.Type.Integer instance NumHask.Algebra.Additive.Subtractive GHC.Types.Bool instance NumHask.Algebra.Additive.Subtractive GHC.Natural.Natural instance NumHask.Algebra.Additive.Subtractive GHC.Int.Int8 instance NumHask.Algebra.Additive.Subtractive GHC.Int.Int16 instance NumHask.Algebra.Additive.Subtractive GHC.Int.Int32 instance NumHask.Algebra.Additive.Subtractive GHC.Int.Int64 instance NumHask.Algebra.Additive.Subtractive GHC.Types.Word instance NumHask.Algebra.Additive.Subtractive GHC.Word.Word8 instance NumHask.Algebra.Additive.Subtractive GHC.Word.Word16 instance NumHask.Algebra.Additive.Subtractive GHC.Word.Word32 instance NumHask.Algebra.Additive.Subtractive GHC.Word.Word64 instance NumHask.Algebra.Additive.Subtractive b => NumHask.Algebra.Additive.Subtractive (a -> b) instance NumHask.Algebra.Additive.Additive GHC.Types.Double instance NumHask.Algebra.Additive.Additive GHC.Types.Float instance NumHask.Algebra.Additive.Additive GHC.Types.Int instance NumHask.Algebra.Additive.Additive GHC.Integer.Type.Integer instance NumHask.Algebra.Additive.Additive GHC.Types.Bool instance NumHask.Algebra.Additive.Additive GHC.Natural.Natural instance NumHask.Algebra.Additive.Additive GHC.Int.Int8 instance NumHask.Algebra.Additive.Additive GHC.Int.Int16 instance NumHask.Algebra.Additive.Additive GHC.Int.Int32 instance NumHask.Algebra.Additive.Additive GHC.Int.Int64 instance NumHask.Algebra.Additive.Additive GHC.Types.Word instance NumHask.Algebra.Additive.Additive GHC.Word.Word8 instance NumHask.Algebra.Additive.Additive GHC.Word.Word16 instance NumHask.Algebra.Additive.Additive GHC.Word.Word32 instance NumHask.Algebra.Additive.Additive GHC.Word.Word64 instance NumHask.Algebra.Additive.Additive b => NumHask.Algebra.Additive.Additive (a -> b) -- | The Group hierarchy module NumHask.Algebra.Group -- | A Magma is a tuple (T,magma) consisting of -- --
-- ∀ a, b ∈ T: a ⊕ b ∈ T ---- -- These laws are true by construction in haskell: the type signature of -- ⊕ and the above mathematical laws are synonyms. class Magma a (⊕) :: Magma a => a -> a -> a infix 3 ⊕ -- | A Unital Magma is a magma with an identity element (the unit). -- --
-- unit ⊕ a = a -- a ⊕ unit = a --class Magma a => Unital a unit :: Unital a => a -- | An Associative Magma -- --
-- (a ⊕ b) ⊕ c = a ⊕ (b ⊕ c) --class Magma a => Associative a -- | A Commutative Magma is a Magma where the binary operation is -- commutative. -- --
-- a ⊕ b = b ⊕ a --class Magma a => Commutative a -- | An Absorbing is a Magma with an Absorbing Element -- --
-- a ⊕ absorb = absorb --class Magma a => Absorbing a absorb :: Absorbing a => a -- | An Invertible Magma -- --
-- ∀ a,b ∈ T: inv a ⊕ (a ⊕ b) = b = (b ⊕ a) ⊕ inv a --class Magma a => Invertible a inv :: Invertible a => a -> a -- | An Idempotent Magma is a magma where every element is -- Idempotent. -- --
-- a ⊕ a = a --class Magma a => Idempotent a -- | A Group is a Associative, Unital and Invertible Magma. class (Associative a, Unital a, Invertible a) => Group a -- | An Abelian Group is an Associative, Unital, Invertible and -- Commutative Magma . In other words, it is a Commutative Group class (Associative a, Unital a, Invertible a, Commutative a) => AbelianGroup a instance (NumHask.Algebra.Group.Associative a, NumHask.Algebra.Group.Unital a, NumHask.Algebra.Group.Invertible a, NumHask.Algebra.Group.Commutative a) => NumHask.Algebra.Group.AbelianGroup a instance NumHask.Algebra.Group.Idempotent b => NumHask.Algebra.Group.Idempotent (a -> b) instance NumHask.Algebra.Group.Absorbing b => NumHask.Algebra.Group.Absorbing (a -> b) instance (NumHask.Algebra.Group.Associative a, NumHask.Algebra.Group.Unital a, NumHask.Algebra.Group.Invertible a) => NumHask.Algebra.Group.Group a instance NumHask.Algebra.Group.Invertible b => NumHask.Algebra.Group.Invertible (a -> b) instance NumHask.Algebra.Group.Commutative b => NumHask.Algebra.Group.Commutative (a -> b) instance NumHask.Algebra.Group.Associative b => NumHask.Algebra.Group.Associative (a -> b) instance NumHask.Algebra.Group.Unital b => NumHask.Algebra.Group.Unital (a -> b) instance NumHask.Algebra.Group.Magma b => NumHask.Algebra.Group.Magma (a -> b) -- | Multiplicative classes module NumHask.Algebra.Multiplicative -- | or Multiplication -- -- For practical reasons, we begin the class tree with Additive -- and Multiplicative. Starting with Associative and -- Unital, or using Semigroup and Monoid from base -- tends to confuse the interface once you start having to disinguish -- between (say) monoidal addition and monoidal multiplication. -- --
-- \a -> one * a == a -- \a -> a * one == a -- \a b c -> (a * b) * c == a * (b * c) ---- -- By convention, (*) is regarded as not necessarily commutative, but -- this is not universal, and the introduction of another symbol which -- means commutative multiplication seems a bit dogmatic. -- --
-- >>> one * 2 -- 2 ---- --
-- >>> 2 * 3 -- 6 --class Multiplicative a (*) :: Multiplicative a => a -> a -> a one :: Multiplicative a => a infixl 7 * -- | Compute the product of a Foldable. product :: (Multiplicative a, Foldable f) => f a -> a -- | or Division -- -- Though unusual, the term Divisive usefully fits in with the grammer of -- other classes and avoids name clashes that occur with some popular -- libraries. -- --
-- \(a :: Double) -> a / a ~= one || a == zero -- \(a :: Double) -> recip a ~= one / a || a == zero -- \(a :: Double) -> recip a * a ~= one || a == zero -- \(a :: Double) -> a * recip a ~= one || a == zero ---- --
-- >>> recip 2.0 -- 0.5 ---- --
-- >>> 1 / 2 -- 0.5 --class (Multiplicative a) => Divisive a recip :: Divisive a => a -> a (/) :: Divisive a => a -> a -> a infixl 7 / instance NumHask.Algebra.Multiplicative.Divisive GHC.Types.Double instance NumHask.Algebra.Multiplicative.Divisive GHC.Types.Float instance NumHask.Algebra.Multiplicative.Divisive b => NumHask.Algebra.Multiplicative.Divisive (a -> b) instance NumHask.Algebra.Multiplicative.Multiplicative GHC.Types.Double instance NumHask.Algebra.Multiplicative.Multiplicative GHC.Types.Float instance NumHask.Algebra.Multiplicative.Multiplicative GHC.Types.Int instance NumHask.Algebra.Multiplicative.Multiplicative GHC.Integer.Type.Integer instance NumHask.Algebra.Multiplicative.Multiplicative GHC.Types.Bool instance NumHask.Algebra.Multiplicative.Multiplicative GHC.Natural.Natural instance NumHask.Algebra.Multiplicative.Multiplicative GHC.Int.Int8 instance NumHask.Algebra.Multiplicative.Multiplicative GHC.Int.Int16 instance NumHask.Algebra.Multiplicative.Multiplicative GHC.Int.Int32 instance NumHask.Algebra.Multiplicative.Multiplicative GHC.Int.Int64 instance NumHask.Algebra.Multiplicative.Multiplicative GHC.Types.Word instance NumHask.Algebra.Multiplicative.Multiplicative GHC.Word.Word8 instance NumHask.Algebra.Multiplicative.Multiplicative GHC.Word.Word16 instance NumHask.Algebra.Multiplicative.Multiplicative GHC.Word.Word32 instance NumHask.Algebra.Multiplicative.Multiplicative GHC.Word.Word64 instance NumHask.Algebra.Multiplicative.Multiplicative b => NumHask.Algebra.Multiplicative.Multiplicative (a -> b) -- | Ring classes module NumHask.Algebra.Ring -- | Distributive -- --
-- \a b c -> a * (b + c) == a * b + a * c -- \a b c -> (a + b) * c == a * c + b * c -- \a -> zero * a == zero -- \a -> a * zero == zero ---- -- The sneaking in of the Absorption laws here glosses over the -- possibility that the multiplicative zero element does not have to -- correspond with the additive unital zero. class (Additive a, Multiplicative a) => Distributive a -- | A Ring is an abelian group under addition (Unital, -- Associative, Commutative, Invertible) and -- monoidal under multiplication (Unital, Associative), and -- where multiplication distributes over addition. -- --
-- \a -> zero + a == a -- \a -> a + zero == a -- \a b c -> (a + b) + c == a + (b + c) -- \a b -> a + b == b + a -- \a -> a - a == zero -- \a -> negate a == zero - a -- \a -> negate a + a == zero -- \a -> a + negate a == zero -- \a -> one * a == a -- \a -> a * one == a -- \a b c -> (a * b) * c == a * (b * c) -- \a b c -> a * (b + c) == a * b + a * c -- \a b c -> (a + b) * c == a * c + b * c -- \a -> zero * a == zero -- \a -> a * zero == zero --class (Distributive a, Subtractive a) => Ring a -- | A StarSemiring is a semiring with an additional unary operator -- (star) satisfying: -- --
-- \a -> star a = one + a * star a --class (Distributive a) => StarSemiring a star :: StarSemiring a => a -> a plus :: StarSemiring a => a -> a -- | A Kleene Algebra is a Star Semiring with idempotent addition. -- --
-- a * x + x = a ==> star a * x + x = x -- x * a + x = a ==> x * star a + x = x --class (StarSemiring a, Idempotent a) => KleeneAlgebra a -- | Involutive Ring -- --
-- adj (a + b) ==> adj a + adj b -- adj (a * b) ==> adj a * adj b -- adj one ==> one -- adj (adj a) ==> a ---- -- Note: elements for which adj a == a are called -- "self-adjoint". class (Distributive a) => InvolutiveRing a adj :: InvolutiveRing a => a -> a -- | Defining two requires adding the multiplicative unital to -- itself. In other words, the concept of two is a Ring one. -- --
-- >>> two -- 2 --two :: (Multiplicative a, Additive a) => a instance NumHask.Algebra.Ring.InvolutiveRing GHC.Types.Double instance NumHask.Algebra.Ring.InvolutiveRing GHC.Types.Float instance NumHask.Algebra.Ring.InvolutiveRing GHC.Integer.Type.Integer instance NumHask.Algebra.Ring.InvolutiveRing GHC.Types.Int instance NumHask.Algebra.Ring.InvolutiveRing GHC.Natural.Natural instance NumHask.Algebra.Ring.InvolutiveRing GHC.Int.Int8 instance NumHask.Algebra.Ring.InvolutiveRing GHC.Int.Int16 instance NumHask.Algebra.Ring.InvolutiveRing GHC.Int.Int32 instance NumHask.Algebra.Ring.InvolutiveRing GHC.Int.Int64 instance NumHask.Algebra.Ring.InvolutiveRing GHC.Types.Word instance NumHask.Algebra.Ring.InvolutiveRing GHC.Word.Word8 instance NumHask.Algebra.Ring.InvolutiveRing GHC.Word.Word16 instance NumHask.Algebra.Ring.InvolutiveRing GHC.Word.Word32 instance NumHask.Algebra.Ring.InvolutiveRing GHC.Word.Word64 instance NumHask.Algebra.Ring.InvolutiveRing b => NumHask.Algebra.Ring.InvolutiveRing (a -> b) instance NumHask.Algebra.Ring.KleeneAlgebra b => NumHask.Algebra.Ring.KleeneAlgebra (a -> b) instance NumHask.Algebra.Ring.StarSemiring b => NumHask.Algebra.Ring.StarSemiring (a -> b) instance (NumHask.Algebra.Ring.Distributive a, NumHask.Algebra.Additive.Subtractive a) => NumHask.Algebra.Ring.Ring a instance NumHask.Algebra.Ring.Distributive GHC.Types.Double instance NumHask.Algebra.Ring.Distributive GHC.Types.Float instance NumHask.Algebra.Ring.Distributive GHC.Types.Int instance NumHask.Algebra.Ring.Distributive GHC.Integer.Type.Integer instance NumHask.Algebra.Ring.Distributive GHC.Natural.Natural instance NumHask.Algebra.Ring.Distributive GHC.Int.Int8 instance NumHask.Algebra.Ring.Distributive GHC.Int.Int16 instance NumHask.Algebra.Ring.Distributive GHC.Int.Int32 instance NumHask.Algebra.Ring.Distributive GHC.Int.Int64 instance NumHask.Algebra.Ring.Distributive GHC.Types.Word instance NumHask.Algebra.Ring.Distributive GHC.Word.Word8 instance NumHask.Algebra.Ring.Distributive GHC.Word.Word16 instance NumHask.Algebra.Ring.Distributive GHC.Word.Word32 instance NumHask.Algebra.Ring.Distributive GHC.Word.Word64 instance NumHask.Algebra.Ring.Distributive GHC.Types.Bool instance NumHask.Algebra.Ring.Distributive b => NumHask.Algebra.Ring.Distributive (a -> b) -- | Algebra for Modules module NumHask.Algebra.Module -- | Additive Action class (Additive a) => AdditiveAction m a | m -> a (.+) :: AdditiveAction m a => a -> m -> m (+.) :: AdditiveAction m a => m -> a -> m infixl 6 .+ infixl 6 +. -- | Subtractive Action class (Subtractive a) => SubtractiveAction m a | m -> a (.-) :: SubtractiveAction m a => a -> m -> m (-.) :: SubtractiveAction m a => m -> a -> m infixl 6 .- infixl 6 -. -- | Multiplicative Action class (Multiplicative a) => MultiplicativeAction m a | m -> a (.*) :: MultiplicativeAction m a => a -> m -> m (*.) :: MultiplicativeAction m a => m -> a -> m infixl 7 .* infixl 7 *. -- | Divisive Action class (Divisive a) => DivisiveAction m a | m -> a (./) :: DivisiveAction m a => a -> m -> m (/.) :: DivisiveAction m a => m -> a -> m infixl 7 ./ infixl 7 /. -- | A Module -- --
-- a .* one == a -- (a + b) .* c == (a .* c) + (b .* c) -- c *. (a + b) == (c *. a) + (c *. b) -- a .* zero == zero -- a .* b == b *. a --class (Distributive a, MultiplicativeAction m a) => Module m a -- | Integral classes module NumHask.Data.Integral -- | An Integral is anything that satisfies the law: -- --
-- b == zero || b * (a `div` b) + (a `mod` b) == a --class (Distributive a) => Integral a div :: Integral a => a -> a -> a mod :: Integral a => a -> a -> a divMod :: Integral a => a -> a -> (a, a) quot :: Integral a => a -> a -> a rem :: Integral a => a -> a -> a quotRem :: Integral a => a -> a -> (a, a) infixl 7 `div` infixl 7 `mod` -- | toIntegral is kept separate from Integral to help with compatability -- issues. -- --
-- toIntegral a == a --class ToIntegral a b toIntegral :: ToIntegral a b => a -> b toIntegral :: (ToIntegral a b, a ~ b) => a -> b -- | Polymorphic version of fromInteger -- --
-- fromIntegral a == a --class FromIntegral a b fromIntegral :: FromIntegral a b => b -> a fromIntegral :: (FromIntegral a b, a ~ b) => b -> a -- | fromInteger is special in two ways: -- --
-- >>> even 2 -- True --even :: (Eq a, Integral a) => a -> Bool -- |
-- >>> odd 3 -- True --odd :: (Eq a, Integral a) => a -> Bool -- | raise a number to an Integral power (^^) :: (Ord b, Divisive a, Subtractive b, Integral b) => a -> b -> a infixr 8 ^^ -- | raise a number to an Int power -- -- Note: This differs from (^) found in prelude which is a partial -- function (it errors on negative integrals). This monomorphic version -- is provided to help reduce ambiguous type noise in common usages of -- this sign. (^) :: Divisive a => a -> Int -> a infixr 8 ^ instance NumHask.Data.Integral.FromInteger GHC.Types.Double instance NumHask.Data.Integral.FromInteger GHC.Types.Float instance NumHask.Data.Integral.FromInteger GHC.Types.Int instance NumHask.Data.Integral.FromInteger GHC.Integer.Type.Integer instance NumHask.Data.Integral.FromInteger GHC.Natural.Natural instance NumHask.Data.Integral.FromInteger GHC.Int.Int8 instance NumHask.Data.Integral.FromInteger GHC.Int.Int16 instance NumHask.Data.Integral.FromInteger GHC.Int.Int32 instance NumHask.Data.Integral.FromInteger GHC.Int.Int64 instance NumHask.Data.Integral.FromInteger GHC.Types.Word instance NumHask.Data.Integral.FromInteger GHC.Word.Word8 instance NumHask.Data.Integral.FromInteger GHC.Word.Word16 instance NumHask.Data.Integral.FromInteger GHC.Word.Word32 instance NumHask.Data.Integral.FromInteger GHC.Word.Word64 instance NumHask.Data.Integral.FromIntegral a b => NumHask.Data.Integral.FromIntegral (c -> a) b instance NumHask.Data.Integral.FromIntegral GHC.Types.Double GHC.Integer.Type.Integer instance NumHask.Data.Integral.FromIntegral GHC.Types.Float GHC.Integer.Type.Integer instance NumHask.Data.Integral.FromIntegral GHC.Types.Int GHC.Integer.Type.Integer instance NumHask.Data.Integral.FromIntegral GHC.Integer.Type.Integer GHC.Integer.Type.Integer instance NumHask.Data.Integral.FromIntegral GHC.Natural.Natural GHC.Integer.Type.Integer instance NumHask.Data.Integral.FromIntegral GHC.Int.Int8 GHC.Integer.Type.Integer instance NumHask.Data.Integral.FromIntegral GHC.Int.Int16 GHC.Integer.Type.Integer instance NumHask.Data.Integral.FromIntegral GHC.Int.Int32 GHC.Integer.Type.Integer instance NumHask.Data.Integral.FromIntegral GHC.Int.Int64 GHC.Integer.Type.Integer instance NumHask.Data.Integral.FromIntegral GHC.Types.Word GHC.Integer.Type.Integer instance NumHask.Data.Integral.FromIntegral GHC.Word.Word8 GHC.Integer.Type.Integer instance NumHask.Data.Integral.FromIntegral GHC.Word.Word16 GHC.Integer.Type.Integer instance NumHask.Data.Integral.FromIntegral GHC.Word.Word32 GHC.Integer.Type.Integer instance NumHask.Data.Integral.FromIntegral GHC.Word.Word64 GHC.Integer.Type.Integer instance NumHask.Data.Integral.FromIntegral GHC.Types.Double GHC.Types.Int instance NumHask.Data.Integral.FromIntegral GHC.Types.Float GHC.Types.Int instance NumHask.Data.Integral.FromIntegral GHC.Types.Int GHC.Types.Int instance NumHask.Data.Integral.FromIntegral GHC.Integer.Type.Integer GHC.Types.Int instance NumHask.Data.Integral.FromIntegral GHC.Natural.Natural GHC.Types.Int instance NumHask.Data.Integral.FromIntegral GHC.Int.Int8 GHC.Types.Int instance NumHask.Data.Integral.FromIntegral GHC.Int.Int16 GHC.Types.Int instance NumHask.Data.Integral.FromIntegral GHC.Int.Int32 GHC.Types.Int instance NumHask.Data.Integral.FromIntegral GHC.Int.Int64 GHC.Types.Int instance NumHask.Data.Integral.FromIntegral GHC.Types.Word GHC.Types.Int instance NumHask.Data.Integral.FromIntegral GHC.Word.Word8 GHC.Types.Int instance NumHask.Data.Integral.FromIntegral GHC.Word.Word16 GHC.Types.Int instance NumHask.Data.Integral.FromIntegral GHC.Word.Word32 GHC.Types.Int instance NumHask.Data.Integral.FromIntegral GHC.Word.Word64 GHC.Types.Int instance NumHask.Data.Integral.FromIntegral GHC.Natural.Natural GHC.Natural.Natural instance NumHask.Data.Integral.FromIntegral GHC.Int.Int8 GHC.Int.Int8 instance NumHask.Data.Integral.FromIntegral GHC.Int.Int16 GHC.Int.Int16 instance NumHask.Data.Integral.FromIntegral GHC.Int.Int32 GHC.Int.Int32 instance NumHask.Data.Integral.FromIntegral GHC.Int.Int64 GHC.Int.Int64 instance NumHask.Data.Integral.FromIntegral GHC.Types.Word GHC.Types.Word instance NumHask.Data.Integral.FromIntegral GHC.Word.Word8 GHC.Word.Word8 instance NumHask.Data.Integral.FromIntegral GHC.Word.Word16 GHC.Word.Word16 instance NumHask.Data.Integral.FromIntegral GHC.Word.Word32 GHC.Word.Word32 instance NumHask.Data.Integral.FromIntegral GHC.Word.Word64 GHC.Word.Word64 instance NumHask.Data.Integral.ToIntegral GHC.Integer.Type.Integer GHC.Integer.Type.Integer instance NumHask.Data.Integral.ToIntegral GHC.Types.Int GHC.Integer.Type.Integer instance NumHask.Data.Integral.ToIntegral GHC.Natural.Natural GHC.Integer.Type.Integer instance NumHask.Data.Integral.ToIntegral GHC.Int.Int8 GHC.Integer.Type.Integer instance NumHask.Data.Integral.ToIntegral GHC.Int.Int16 GHC.Integer.Type.Integer instance NumHask.Data.Integral.ToIntegral GHC.Int.Int32 GHC.Integer.Type.Integer instance NumHask.Data.Integral.ToIntegral GHC.Int.Int64 GHC.Integer.Type.Integer instance NumHask.Data.Integral.ToIntegral GHC.Types.Word GHC.Integer.Type.Integer instance NumHask.Data.Integral.ToIntegral GHC.Word.Word8 GHC.Integer.Type.Integer instance NumHask.Data.Integral.ToIntegral GHC.Word.Word16 GHC.Integer.Type.Integer instance NumHask.Data.Integral.ToIntegral GHC.Word.Word32 GHC.Integer.Type.Integer instance NumHask.Data.Integral.ToIntegral GHC.Word.Word64 GHC.Integer.Type.Integer instance NumHask.Data.Integral.ToIntegral GHC.Types.Int GHC.Types.Int instance NumHask.Data.Integral.ToIntegral GHC.Integer.Type.Integer GHC.Types.Int instance NumHask.Data.Integral.ToIntegral GHC.Natural.Natural GHC.Types.Int instance NumHask.Data.Integral.ToIntegral GHC.Int.Int8 GHC.Types.Int instance NumHask.Data.Integral.ToIntegral GHC.Int.Int16 GHC.Types.Int instance NumHask.Data.Integral.ToIntegral GHC.Int.Int32 GHC.Types.Int instance NumHask.Data.Integral.ToIntegral GHC.Int.Int64 GHC.Types.Int instance NumHask.Data.Integral.ToIntegral GHC.Types.Word GHC.Types.Int instance NumHask.Data.Integral.ToIntegral GHC.Word.Word8 GHC.Types.Int instance NumHask.Data.Integral.ToIntegral GHC.Word.Word16 GHC.Types.Int instance NumHask.Data.Integral.ToIntegral GHC.Word.Word32 GHC.Types.Int instance NumHask.Data.Integral.ToIntegral GHC.Word.Word64 GHC.Types.Int instance NumHask.Data.Integral.ToIntegral GHC.Natural.Natural GHC.Natural.Natural instance NumHask.Data.Integral.ToIntegral GHC.Int.Int8 GHC.Int.Int8 instance NumHask.Data.Integral.ToIntegral GHC.Int.Int16 GHC.Int.Int16 instance NumHask.Data.Integral.ToIntegral GHC.Int.Int32 GHC.Int.Int32 instance NumHask.Data.Integral.ToIntegral GHC.Int.Int64 GHC.Int.Int64 instance NumHask.Data.Integral.ToIntegral GHC.Types.Word GHC.Types.Word instance NumHask.Data.Integral.ToIntegral GHC.Word.Word8 GHC.Word.Word8 instance NumHask.Data.Integral.ToIntegral GHC.Word.Word16 GHC.Word.Word16 instance NumHask.Data.Integral.ToIntegral GHC.Word.Word32 GHC.Word.Word32 instance NumHask.Data.Integral.ToIntegral GHC.Word.Word64 GHC.Word.Word64 instance NumHask.Data.Integral.Integral GHC.Types.Int instance NumHask.Data.Integral.Integral GHC.Integer.Type.Integer instance NumHask.Data.Integral.Integral GHC.Natural.Natural instance NumHask.Data.Integral.Integral GHC.Int.Int8 instance NumHask.Data.Integral.Integral GHC.Int.Int16 instance NumHask.Data.Integral.Integral GHC.Int.Int32 instance NumHask.Data.Integral.Integral GHC.Int.Int64 instance NumHask.Data.Integral.Integral GHC.Types.Word instance NumHask.Data.Integral.Integral GHC.Word.Word8 instance NumHask.Data.Integral.Integral GHC.Word.Word16 instance NumHask.Data.Integral.Integral GHC.Word.Word32 instance NumHask.Data.Integral.Integral GHC.Word.Word64 instance NumHask.Data.Integral.Integral b => NumHask.Data.Integral.Integral (a -> b) -- | Field classes module NumHask.Algebra.Field -- | A Field is a set on which addition, subtraction, -- multiplication, and division are defined. It is also assumed that -- multiplication is distributive over addition. -- -- A summary of the rules inherited from super-classes of Field. Floating -- point computation is a terrible, messy business and, in practice, only -- rough approximation can be achieve for association and distribution. -- --
-- zero + a == a -- a + zero == a -- ((a + b) + c) (a + (b + c)) -- a + b == b + a -- a - a == zero -- negate a == zero - a -- negate a + a == zero -- a + negate a == zero -- one * a == a -- a * one == a -- ((a * b) * c) == (a * (b * c)) -- (a * (b + c)) == (a * b + a * c) -- ((a + b) * c) == (a * c + b * c) -- a * zero == zero -- zero * a == zero -- a / a == one || a == zero -- recip a == one / a || a == zero -- recip a * a == one || a == zero -- a * recip a == one || a == zero --class (Distributive a, Subtractive a, Divisive a) => Field a -- | A hyperbolic field class -- --
-- sqrt . (**2) == id -- log . exp == id -- for +ive b, a != 0,1: a ** logBase a b ≈ b --class (Field a) => ExpField a exp :: ExpField a => a -> a log :: ExpField a => a -> a logBase :: ExpField a => a -> a -> a (**) :: ExpField a => a -> a -> a sqrt :: ExpField a => a -> a -- | Conversion from a Field to a Ring -- -- See Field of fractions -- --
-- a - one < floor a <= a <= ceiling a < a + one -- round a == floor (a + half) --class (Field a, Multiplicative b, Additive b) => QuotientField a b properFraction :: QuotientField a b => a -> (b, a) round :: QuotientField a b => a -> b round :: (QuotientField a b, Ord a, Ord b, Subtractive b, Integral b) => a -> b ceiling :: QuotientField a b => a -> b ceiling :: (QuotientField a b, Ord a) => a -> b floor :: QuotientField a b => a -> b floor :: (QuotientField a b, Ord a, Subtractive b) => a -> b truncate :: QuotientField a b => a -> b truncate :: (QuotientField a b, Ord a) => a -> b -- | A bounded field introduces the concepts of infinity and NaN. -- --
-- one / zero + infinity == infinity -- infinity + a == infinity -- zero / zero != nan ---- -- Note the tricky law that, although nan is assigned to zero/zero, they -- are never-the-less not equal. A committee decided this. class (Field a) => UpperBoundedField a infinity :: UpperBoundedField a => a nan :: UpperBoundedField a => a -- | Negative infinity. class (Subtractive a, Field a) => LowerBoundedField a negInfinity :: LowerBoundedField a => a -- | Trigonometric Field class (Field a) => TrigField a pi :: TrigField a => a sin :: TrigField a => a -> a cos :: TrigField a => a -> a tan :: TrigField a => a -> a asin :: TrigField a => a -> a acos :: TrigField a => a -> a atan :: TrigField a => a -> a atan2 :: TrigField a => a -> a -> a sinh :: TrigField a => a -> a cosh :: TrigField a => a -> a tanh :: TrigField a => a -> a asinh :: TrigField a => a -> a acosh :: TrigField a => a -> a atanh :: TrigField a => a -> a -- | A half is a Field because it requires addition, -- multiplication and division. half :: Field a => a instance NumHask.Algebra.Field.TrigField GHC.Types.Double instance NumHask.Algebra.Field.TrigField GHC.Types.Float instance NumHask.Algebra.Field.TrigField b => NumHask.Algebra.Field.TrigField (a -> b) instance NumHask.Algebra.Field.LowerBoundedField GHC.Types.Float instance NumHask.Algebra.Field.LowerBoundedField GHC.Types.Double instance NumHask.Algebra.Field.LowerBoundedField b => NumHask.Algebra.Field.LowerBoundedField (a -> b) instance NumHask.Algebra.Field.UpperBoundedField GHC.Types.Float instance NumHask.Algebra.Field.UpperBoundedField GHC.Types.Double instance NumHask.Algebra.Field.UpperBoundedField b => NumHask.Algebra.Field.UpperBoundedField (a -> b) instance NumHask.Algebra.Field.QuotientField GHC.Types.Float GHC.Integer.Type.Integer instance NumHask.Algebra.Field.QuotientField GHC.Types.Double GHC.Integer.Type.Integer instance NumHask.Algebra.Field.QuotientField GHC.Types.Float GHC.Types.Int instance NumHask.Algebra.Field.QuotientField GHC.Types.Double GHC.Types.Int instance NumHask.Algebra.Field.QuotientField b c => NumHask.Algebra.Field.QuotientField (a -> b) (a -> c) instance NumHask.Algebra.Field.ExpField GHC.Types.Double instance NumHask.Algebra.Field.ExpField GHC.Types.Float instance NumHask.Algebra.Field.ExpField b => NumHask.Algebra.Field.ExpField (a -> b) instance NumHask.Algebra.Field.Field GHC.Types.Double instance NumHask.Algebra.Field.Field GHC.Types.Float instance NumHask.Algebra.Field.Field b => NumHask.Algebra.Field.Field (a -> b) -- | Lattices module NumHask.Algebra.Lattice -- | A algebraic structure with element joins: See Semilattice -- --
-- Associativity: x \/ (y \/ z) == (x \/ y) \/ z -- Commutativity: x \/ y == y \/ x -- Idempotency: x \/ x == x --class (Eq a) => JoinSemiLattice a (\/) :: JoinSemiLattice a => a -> a -> a infixr 5 \/ -- | The partial ordering induced by the join-semilattice structure joinLeq :: JoinSemiLattice a => a -> a -> Bool -- | A algebraic structure with element meets: See Semilattice -- --
-- Associativity: x /\ (y /\ z) == (x /\ y) /\ z -- Commutativity: x /\ y == y /\ x -- Idempotency: x /\ x == x --class (Eq a) => MeetSemiLattice a (/\) :: MeetSemiLattice a => a -> a -> a infixr 6 /\ -- | The partial ordering induced by the meet-semilattice structure meetLeq :: MeetSemiLattice a => a -> a -> Bool -- | A join-semilattice with an identity element bottom for -- \/. -- --
-- Identity: x \/ bottom == x --class JoinSemiLattice a => BoundedJoinSemiLattice a bottom :: BoundedJoinSemiLattice a => a -- | A meet-semilattice with an identity element top for /\. -- --
-- Identity: x /\ top == x --class MeetSemiLattice a => BoundedMeetSemiLattice a top :: BoundedMeetSemiLattice a => a instance (NumHask.Algebra.Lattice.JoinSemiLattice a, NumHask.Algebra.Lattice.MeetSemiLattice a, NumHask.Algebra.Lattice.BoundedJoinSemiLattice a, NumHask.Algebra.Lattice.BoundedMeetSemiLattice a) => NumHask.Algebra.Lattice.BoundedLattice a instance NumHask.Algebra.Lattice.BoundedMeetSemiLattice GHC.Types.Float instance NumHask.Algebra.Lattice.BoundedMeetSemiLattice GHC.Types.Double instance NumHask.Algebra.Lattice.BoundedMeetSemiLattice GHC.Types.Int instance NumHask.Algebra.Lattice.BoundedMeetSemiLattice GHC.Types.Bool instance NumHask.Algebra.Lattice.BoundedMeetSemiLattice GHC.Int.Int8 instance NumHask.Algebra.Lattice.BoundedMeetSemiLattice GHC.Int.Int16 instance NumHask.Algebra.Lattice.BoundedMeetSemiLattice GHC.Int.Int32 instance NumHask.Algebra.Lattice.BoundedMeetSemiLattice GHC.Int.Int64 instance NumHask.Algebra.Lattice.BoundedMeetSemiLattice GHC.Types.Word instance NumHask.Algebra.Lattice.BoundedMeetSemiLattice GHC.Word.Word8 instance NumHask.Algebra.Lattice.BoundedMeetSemiLattice GHC.Word.Word16 instance NumHask.Algebra.Lattice.BoundedMeetSemiLattice GHC.Word.Word32 instance NumHask.Algebra.Lattice.BoundedMeetSemiLattice GHC.Word.Word64 instance (GHC.Classes.Eq (a -> b), NumHask.Algebra.Lattice.BoundedMeetSemiLattice b) => NumHask.Algebra.Lattice.BoundedMeetSemiLattice (a -> b) instance NumHask.Algebra.Lattice.BoundedJoinSemiLattice GHC.Types.Float instance NumHask.Algebra.Lattice.BoundedJoinSemiLattice GHC.Types.Double instance NumHask.Algebra.Lattice.BoundedJoinSemiLattice GHC.Types.Int instance NumHask.Algebra.Lattice.BoundedJoinSemiLattice GHC.Types.Bool instance NumHask.Algebra.Lattice.BoundedJoinSemiLattice GHC.Natural.Natural instance NumHask.Algebra.Lattice.BoundedJoinSemiLattice GHC.Int.Int8 instance NumHask.Algebra.Lattice.BoundedJoinSemiLattice GHC.Int.Int16 instance NumHask.Algebra.Lattice.BoundedJoinSemiLattice GHC.Int.Int32 instance NumHask.Algebra.Lattice.BoundedJoinSemiLattice GHC.Int.Int64 instance NumHask.Algebra.Lattice.BoundedJoinSemiLattice GHC.Types.Word instance NumHask.Algebra.Lattice.BoundedJoinSemiLattice GHC.Word.Word8 instance NumHask.Algebra.Lattice.BoundedJoinSemiLattice GHC.Word.Word16 instance NumHask.Algebra.Lattice.BoundedJoinSemiLattice GHC.Word.Word32 instance NumHask.Algebra.Lattice.BoundedJoinSemiLattice GHC.Word.Word64 instance (GHC.Classes.Eq (a -> b), NumHask.Algebra.Lattice.BoundedJoinSemiLattice b) => NumHask.Algebra.Lattice.BoundedJoinSemiLattice (a -> b) instance (NumHask.Algebra.Lattice.JoinSemiLattice a, NumHask.Algebra.Lattice.MeetSemiLattice a) => NumHask.Algebra.Lattice.Lattice a instance NumHask.Algebra.Lattice.MeetSemiLattice GHC.Types.Float instance NumHask.Algebra.Lattice.MeetSemiLattice GHC.Types.Double instance NumHask.Algebra.Lattice.MeetSemiLattice GHC.Types.Int instance NumHask.Algebra.Lattice.MeetSemiLattice GHC.Integer.Type.Integer instance NumHask.Algebra.Lattice.MeetSemiLattice GHC.Types.Bool instance NumHask.Algebra.Lattice.MeetSemiLattice GHC.Natural.Natural instance NumHask.Algebra.Lattice.MeetSemiLattice GHC.Int.Int8 instance NumHask.Algebra.Lattice.MeetSemiLattice GHC.Int.Int16 instance NumHask.Algebra.Lattice.MeetSemiLattice GHC.Int.Int32 instance NumHask.Algebra.Lattice.MeetSemiLattice GHC.Int.Int64 instance NumHask.Algebra.Lattice.MeetSemiLattice GHC.Types.Word instance NumHask.Algebra.Lattice.MeetSemiLattice GHC.Word.Word8 instance NumHask.Algebra.Lattice.MeetSemiLattice GHC.Word.Word16 instance NumHask.Algebra.Lattice.MeetSemiLattice GHC.Word.Word32 instance NumHask.Algebra.Lattice.MeetSemiLattice GHC.Word.Word64 instance (GHC.Classes.Eq (a -> b), NumHask.Algebra.Lattice.MeetSemiLattice b) => NumHask.Algebra.Lattice.MeetSemiLattice (a -> b) instance NumHask.Algebra.Lattice.JoinSemiLattice GHC.Types.Float instance NumHask.Algebra.Lattice.JoinSemiLattice GHC.Types.Double instance NumHask.Algebra.Lattice.JoinSemiLattice GHC.Types.Int instance NumHask.Algebra.Lattice.JoinSemiLattice GHC.Integer.Type.Integer instance NumHask.Algebra.Lattice.JoinSemiLattice GHC.Types.Bool instance NumHask.Algebra.Lattice.JoinSemiLattice GHC.Natural.Natural instance NumHask.Algebra.Lattice.JoinSemiLattice GHC.Int.Int8 instance NumHask.Algebra.Lattice.JoinSemiLattice GHC.Int.Int16 instance NumHask.Algebra.Lattice.JoinSemiLattice GHC.Int.Int32 instance NumHask.Algebra.Lattice.JoinSemiLattice GHC.Int.Int64 instance NumHask.Algebra.Lattice.JoinSemiLattice GHC.Types.Word instance NumHask.Algebra.Lattice.JoinSemiLattice GHC.Word.Word8 instance NumHask.Algebra.Lattice.JoinSemiLattice GHC.Word.Word16 instance NumHask.Algebra.Lattice.JoinSemiLattice GHC.Word.Word32 instance NumHask.Algebra.Lattice.JoinSemiLattice GHC.Word.Word64 instance (GHC.Classes.Eq (a -> b), NumHask.Algebra.Lattice.JoinSemiLattice b) => NumHask.Algebra.Lattice.JoinSemiLattice (a -> b) -- | Metric classes module NumHask.Algebra.Metric -- | signum from base is not an operator name in numhask and is -- replaced by sign. Compare with Norm where there is a -- change in codomain. -- --
-- abs a * sign a == a ---- -- abs zero == zero, so any value for sign zero is ok. We choose lawful -- neutral: -- --
-- sign zero == zero --class (Additive a, Multiplicative a) => Signed a sign :: Signed a => a -> a abs :: Signed a => a -> a -- | Norm is a slight generalisation of Signed. The class has the same -- shape but allows the codomain to be different to the domain. -- --
-- norm a >= zero -- norm zero == zero -- a == norm a .* basis a -- norm (basis a) == one --class (Additive a, Multiplicative b, Additive b) => Norm a b | a -> b -- | or length, or ||v|| norm :: Norm a b => a -> b -- | or direction, or v-hat basis :: Norm a b => a -> a -- | Distance, which combines the Subtractive notion of difference, with -- Norm. -- --
-- distance a b >= zero -- distance a a == zero -- distance a b .* basis (a - b) == a - b --distance :: (Norm a b, Subtractive a) => a -> a -> b -- | Convert between a "co-ordinated" or "higher-kinded" number and -- representations of an angle. Typically thought of as polar co-ordinate -- conversion. -- -- See Polar coordinate system -- --
-- ray . angle == basis -- norm (ray x) == one --class (Additive coord, Multiplicative coord, Additive dir, Multiplicative dir) => Direction coord dir | coord -> dir angle :: Direction coord dir => coord -> dir ray :: Direction coord dir => dir -> coord -- | Something that has a magnitude and a direction. data Polar mag dir Polar :: !mag -> !dir -> Polar mag dir [magnitude] :: Polar mag dir -> !mag [direction] :: Polar mag dir -> !dir -- | Convert from a number to a Polar. polar :: (Norm coord mag, Direction coord dir) => coord -> Polar mag dir -- | Convert from a Polar to a (coordinated aka higher-kinded) number. coord :: (MultiplicativeAction coord mag, Direction coord dir) => Polar mag dir -> coord -- | A small number, especially useful for approximate equality. class (Eq a, Additive a, Subtractive a, MeetSemiLattice a) => Epsilon a epsilon :: Epsilon a => a nearZero :: Epsilon a => a -> Bool aboutEqual :: Epsilon a => a -> a -> Bool -- | About equal. (~=) :: Epsilon a => a -> a -> Bool infixl 4 ~= instance GHC.Generics.Generic (NumHask.Algebra.Metric.Polar mag dir) instance (GHC.Show.Show mag, GHC.Show.Show dir) => GHC.Show.Show (NumHask.Algebra.Metric.Polar mag dir) instance (GHC.Classes.Eq mag, GHC.Classes.Eq dir) => GHC.Classes.Eq (NumHask.Algebra.Metric.Polar mag dir) instance NumHask.Algebra.Metric.Epsilon GHC.Types.Double instance NumHask.Algebra.Metric.Epsilon GHC.Types.Float instance NumHask.Algebra.Metric.Epsilon GHC.Types.Int instance NumHask.Algebra.Metric.Epsilon GHC.Integer.Type.Integer instance NumHask.Algebra.Metric.Epsilon GHC.Int.Int8 instance NumHask.Algebra.Metric.Epsilon GHC.Int.Int16 instance NumHask.Algebra.Metric.Epsilon GHC.Int.Int32 instance NumHask.Algebra.Metric.Epsilon GHC.Int.Int64 instance NumHask.Algebra.Metric.Epsilon GHC.Types.Word instance NumHask.Algebra.Metric.Epsilon GHC.Word.Word8 instance NumHask.Algebra.Metric.Epsilon GHC.Word.Word16 instance NumHask.Algebra.Metric.Epsilon GHC.Word.Word32 instance NumHask.Algebra.Metric.Epsilon GHC.Word.Word64 instance NumHask.Algebra.Metric.Norm GHC.Types.Double GHC.Types.Double instance NumHask.Algebra.Metric.Norm GHC.Types.Float GHC.Types.Float instance NumHask.Algebra.Metric.Norm GHC.Types.Int GHC.Types.Int instance NumHask.Algebra.Metric.Norm GHC.Integer.Type.Integer GHC.Integer.Type.Integer instance NumHask.Algebra.Metric.Norm GHC.Natural.Natural GHC.Natural.Natural instance NumHask.Algebra.Metric.Norm GHC.Int.Int8 GHC.Int.Int8 instance NumHask.Algebra.Metric.Norm GHC.Int.Int16 GHC.Int.Int16 instance NumHask.Algebra.Metric.Norm GHC.Int.Int32 GHC.Int.Int32 instance NumHask.Algebra.Metric.Norm GHC.Int.Int64 GHC.Int.Int64 instance NumHask.Algebra.Metric.Norm GHC.Types.Word GHC.Types.Word instance NumHask.Algebra.Metric.Norm GHC.Word.Word8 GHC.Word.Word8 instance NumHask.Algebra.Metric.Norm GHC.Word.Word16 GHC.Word.Word16 instance NumHask.Algebra.Metric.Norm GHC.Word.Word32 GHC.Word.Word32 instance NumHask.Algebra.Metric.Norm GHC.Word.Word64 GHC.Word.Word64 instance NumHask.Algebra.Metric.Signed GHC.Types.Double instance NumHask.Algebra.Metric.Signed GHC.Types.Float instance NumHask.Algebra.Metric.Signed GHC.Types.Int instance NumHask.Algebra.Metric.Signed GHC.Integer.Type.Integer instance NumHask.Algebra.Metric.Signed GHC.Natural.Natural instance NumHask.Algebra.Metric.Signed GHC.Int.Int8 instance NumHask.Algebra.Metric.Signed GHC.Int.Int16 instance NumHask.Algebra.Metric.Signed GHC.Int.Int32 instance NumHask.Algebra.Metric.Signed GHC.Int.Int64 instance NumHask.Algebra.Metric.Signed GHC.Types.Word instance NumHask.Algebra.Metric.Signed GHC.Word.Word8 instance NumHask.Algebra.Metric.Signed GHC.Word.Word16 instance NumHask.Algebra.Metric.Signed GHC.Word.Word32 instance NumHask.Algebra.Metric.Signed GHC.Word.Word64 -- | Complex numbers. module NumHask.Data.Complex -- | Complex numbers have real and imaginary parts. -- -- The Foldable and Traversable instances traverse the real -- part first. data Complex a -- | forms a complex number from its real and imaginary rectangular -- components. (:+) :: !a -> !a -> Complex a infix 6 :+ -- | Extracts the real part of a complex number. realPart :: Complex a -> a -- | Extracts the imaginary part of a complex number. imagPart :: Complex a -> a instance Data.Traversable.Traversable NumHask.Data.Complex.Complex instance Data.Foldable.Foldable NumHask.Data.Complex.Complex instance GHC.Base.Functor NumHask.Data.Complex.Complex instance GHC.Generics.Generic1 NumHask.Data.Complex.Complex instance GHC.Generics.Generic (NumHask.Data.Complex.Complex a) instance Data.Data.Data a => Data.Data.Data (NumHask.Data.Complex.Complex a) instance GHC.Read.Read a => GHC.Read.Read (NumHask.Data.Complex.Complex a) instance GHC.Show.Show a => GHC.Show.Show (NumHask.Data.Complex.Complex a) instance GHC.Classes.Eq a => GHC.Classes.Eq (NumHask.Data.Complex.Complex a) instance NumHask.Algebra.Additive.Additive a => NumHask.Algebra.Additive.Additive (NumHask.Data.Complex.Complex a) instance NumHask.Algebra.Additive.Subtractive a => NumHask.Algebra.Additive.Subtractive (NumHask.Data.Complex.Complex a) instance (NumHask.Algebra.Ring.Distributive a, NumHask.Algebra.Additive.Subtractive a) => NumHask.Algebra.Ring.Distributive (NumHask.Data.Complex.Complex a) instance (NumHask.Algebra.Additive.Subtractive a, NumHask.Algebra.Multiplicative.Multiplicative a) => NumHask.Algebra.Multiplicative.Multiplicative (NumHask.Data.Complex.Complex a) instance (NumHask.Algebra.Additive.Subtractive a, NumHask.Algebra.Multiplicative.Divisive a) => NumHask.Algebra.Multiplicative.Divisive (NumHask.Data.Complex.Complex a) instance (NumHask.Algebra.Additive.Additive a, NumHask.Data.Integral.FromIntegral a b) => NumHask.Data.Integral.FromIntegral (NumHask.Data.Complex.Complex a) b instance NumHask.Algebra.Field.ExpField a => NumHask.Algebra.Metric.Norm (NumHask.Data.Complex.Complex a) a instance NumHask.Algebra.Field.TrigField a => NumHask.Algebra.Metric.Direction (NumHask.Data.Complex.Complex a) a instance (GHC.Classes.Ord a, NumHask.Algebra.Metric.Signed a, NumHask.Algebra.Additive.Subtractive a, NumHask.Algebra.Metric.Epsilon a) => NumHask.Algebra.Metric.Epsilon (NumHask.Data.Complex.Complex a) instance NumHask.Algebra.Field.Field a => NumHask.Algebra.Field.Field (NumHask.Data.Complex.Complex a) instance (GHC.Classes.Ord a, NumHask.Algebra.Field.TrigField a, NumHask.Algebra.Field.ExpField a) => NumHask.Algebra.Field.ExpField (NumHask.Data.Complex.Complex a) instance (NumHask.Algebra.Ring.Distributive a, NumHask.Algebra.Additive.Subtractive a) => NumHask.Algebra.Ring.InvolutiveRing (NumHask.Data.Complex.Complex a) instance (NumHask.Algebra.Field.UpperBoundedField a, NumHask.Algebra.Additive.Subtractive a) => NumHask.Algebra.Field.UpperBoundedField (NumHask.Data.Complex.Complex a) instance NumHask.Algebra.Field.LowerBoundedField a => NumHask.Algebra.Field.LowerBoundedField (NumHask.Data.Complex.Complex a) instance NumHask.Algebra.Lattice.JoinSemiLattice a => NumHask.Algebra.Lattice.JoinSemiLattice (NumHask.Data.Complex.Complex a) instance NumHask.Algebra.Lattice.MeetSemiLattice a => NumHask.Algebra.Lattice.MeetSemiLattice (NumHask.Data.Complex.Complex a) instance NumHask.Algebra.Lattice.BoundedJoinSemiLattice a => NumHask.Algebra.Lattice.BoundedJoinSemiLattice (NumHask.Data.Complex.Complex a) instance NumHask.Algebra.Lattice.BoundedMeetSemiLattice a => NumHask.Algebra.Lattice.BoundedMeetSemiLattice (NumHask.Data.Complex.Complex a) -- | Rational classes module NumHask.Data.Rational -- | A rational number data Ratio a (:%) :: !a -> !a -> Ratio a -- | Arbitrary-precision rational numbers, represented as a ratio of two -- Integer values. A rational number may be constructed using the -- % operator. type Rational = Ratio Integer -- | toRatio is equivalent to Real in base, but is polymorphic in -- the Integral type. -- --
-- toRatio (3.1415927 :: Float) :: Ratio Integer ---- -- 13176795 :% 4194304 class ToRatio a b toRatio :: ToRatio a b => a -> Ratio b toRatio :: (ToRatio a b, Ratio c ~ a, FromIntegral b c, ToRatio (Ratio b) b) => a -> Ratio b -- | Fractional in base splits into fromRatio and Field -- --
-- >>> fromRatio (5 :% 2 :: Ratio Integer) :: Double -- 2.5 --class FromRatio a b fromRatio :: FromRatio a b => Ratio b -> a fromRatio :: (FromRatio a b, Ratio b ~ a) => Ratio b -> a -- | fromRational is special in two ways: -- --
-- \a -> a - a == zero -- \a -> negate a == zero - a -- \a -> negate a + a == zero -- \a -> a + negate a == zero ---- --
-- >>> negate 1 -- -1 ---- --
-- >>> 1 - 2 -- -1 --class (Additive a) => Subtractive a negate :: Subtractive a => a -> a (-) :: Subtractive a => a -> a -> a infixl 6 - -- | or Addition -- -- For practical reasons, we begin the class tree with Additive. -- Starting with Associative and Unital, or using -- Semigroup and Monoid from base tends to confuse the -- interface once you start having to disinguish between (say) monoidal -- addition and monoidal multiplication. -- --
-- \a -> zero + a == a -- \a -> a + zero == a -- \a b c -> (a + b) + c == a + (b + c) -- \a b -> a + b == b + a ---- -- By convention, (+) is regarded as commutative, but this is not -- universal, and the introduction of another symbol which means -- non-commutative addition seems a bit dogmatic. -- --
-- >>> zero + 1 -- 1 ---- --
-- >>> 1 + 1 -- 2 --class Additive a (+) :: Additive a => a -> a -> a zero :: Additive a => a infixl 6 + -- | Compute the sum of a Foldable. sum :: (Additive a, Foldable f) => f a -> a -- | or Division -- -- Though unusual, the term Divisive usefully fits in with the grammer of -- other classes and avoids name clashes that occur with some popular -- libraries. -- --
-- \(a :: Double) -> a / a ~= one || a == zero -- \(a :: Double) -> recip a ~= one / a || a == zero -- \(a :: Double) -> recip a * a ~= one || a == zero -- \(a :: Double) -> a * recip a ~= one || a == zero ---- --
-- >>> recip 2.0 -- 0.5 ---- --
-- >>> 1 / 2 -- 0.5 --class (Multiplicative a) => Divisive a recip :: Divisive a => a -> a (/) :: Divisive a => a -> a -> a infixl 7 / -- | or Multiplication -- -- For practical reasons, we begin the class tree with Additive -- and Multiplicative. Starting with Associative and -- Unital, or using Semigroup and Monoid from base -- tends to confuse the interface once you start having to disinguish -- between (say) monoidal addition and monoidal multiplication. -- --
-- \a -> one * a == a -- \a -> a * one == a -- \a b c -> (a * b) * c == a * (b * c) ---- -- By convention, (*) is regarded as not necessarily commutative, but -- this is not universal, and the introduction of another symbol which -- means commutative multiplication seems a bit dogmatic. -- --
-- >>> one * 2 -- 2 ---- --
-- >>> 2 * 3 -- 6 --class Multiplicative a (*) :: Multiplicative a => a -> a -> a one :: Multiplicative a => a infixl 7 * -- | Compute the product of a Foldable. product :: (Multiplicative a, Foldable f) => f a -> a -- | Involutive Ring -- --
-- adj (a + b) ==> adj a + adj b -- adj (a * b) ==> adj a * adj b -- adj one ==> one -- adj (adj a) ==> a ---- -- Note: elements for which adj a == a are called -- "self-adjoint". class (Distributive a) => InvolutiveRing a adj :: InvolutiveRing a => a -> a -- | A Kleene Algebra is a Star Semiring with idempotent addition. -- --
-- a * x + x = a ==> star a * x + x = x -- x * a + x = a ==> x * star a + x = x --class (StarSemiring a, Idempotent a) => KleeneAlgebra a -- | A StarSemiring is a semiring with an additional unary operator -- (star) satisfying: -- --
-- \a -> star a = one + a * star a --class (Distributive a) => StarSemiring a star :: StarSemiring a => a -> a plus :: StarSemiring a => a -> a -- | A Ring is an abelian group under addition (Unital, -- Associative, Commutative, Invertible) and -- monoidal under multiplication (Unital, Associative), and -- where multiplication distributes over addition. -- --
-- \a -> zero + a == a -- \a -> a + zero == a -- \a b c -> (a + b) + c == a + (b + c) -- \a b -> a + b == b + a -- \a -> a - a == zero -- \a -> negate a == zero - a -- \a -> negate a + a == zero -- \a -> a + negate a == zero -- \a -> one * a == a -- \a -> a * one == a -- \a b c -> (a * b) * c == a * (b * c) -- \a b c -> a * (b + c) == a * b + a * c -- \a b c -> (a + b) * c == a * c + b * c -- \a -> zero * a == zero -- \a -> a * zero == zero --class (Distributive a, Subtractive a) => Ring a -- | Distributive -- --
-- \a b c -> a * (b + c) == a * b + a * c -- \a b c -> (a + b) * c == a * c + b * c -- \a -> zero * a == zero -- \a -> a * zero == zero ---- -- The sneaking in of the Absorption laws here glosses over the -- possibility that the multiplicative zero element does not have to -- correspond with the additive unital zero. class (Additive a, Multiplicative a) => Distributive a -- | Defining two requires adding the multiplicative unital to -- itself. In other words, the concept of two is a Ring one. -- --
-- >>> two -- 2 --two :: (Multiplicative a, Additive a) => a -- | Trigonometric Field class (Field a) => TrigField a pi :: TrigField a => a sin :: TrigField a => a -> a cos :: TrigField a => a -> a tan :: TrigField a => a -> a asin :: TrigField a => a -> a acos :: TrigField a => a -> a atan :: TrigField a => a -> a atan2 :: TrigField a => a -> a -> a sinh :: TrigField a => a -> a cosh :: TrigField a => a -> a tanh :: TrigField a => a -> a asinh :: TrigField a => a -> a acosh :: TrigField a => a -> a atanh :: TrigField a => a -> a -- | Negative infinity. class (Subtractive a, Field a) => LowerBoundedField a negInfinity :: LowerBoundedField a => a -- | A bounded field introduces the concepts of infinity and NaN. -- --
-- one / zero + infinity == infinity -- infinity + a == infinity -- zero / zero != nan ---- -- Note the tricky law that, although nan is assigned to zero/zero, they -- are never-the-less not equal. A committee decided this. class (Field a) => UpperBoundedField a infinity :: UpperBoundedField a => a nan :: UpperBoundedField a => a -- | Conversion from a Field to a Ring -- -- See Field of fractions -- --
-- a - one < floor a <= a <= ceiling a < a + one -- round a == floor (a + half) --class (Field a, Multiplicative b, Additive b) => QuotientField a b properFraction :: QuotientField a b => a -> (b, a) round :: QuotientField a b => a -> b round :: (QuotientField a b, Ord a, Ord b, Subtractive b, Integral b) => a -> b ceiling :: QuotientField a b => a -> b ceiling :: (QuotientField a b, Ord a) => a -> b floor :: QuotientField a b => a -> b floor :: (QuotientField a b, Ord a, Subtractive b) => a -> b truncate :: QuotientField a b => a -> b truncate :: (QuotientField a b, Ord a) => a -> b -- | A hyperbolic field class -- --
-- sqrt . (**2) == id -- log . exp == id -- for +ive b, a != 0,1: a ** logBase a b ≈ b --class (Field a) => ExpField a exp :: ExpField a => a -> a log :: ExpField a => a -> a logBase :: ExpField a => a -> a -> a (**) :: ExpField a => a -> a -> a sqrt :: ExpField a => a -> a -- | A Field is a set on which addition, subtraction, -- multiplication, and division are defined. It is also assumed that -- multiplication is distributive over addition. -- -- A summary of the rules inherited from super-classes of Field. Floating -- point computation is a terrible, messy business and, in practice, only -- rough approximation can be achieve for association and distribution. -- --
-- zero + a == a -- a + zero == a -- ((a + b) + c) (a + (b + c)) -- a + b == b + a -- a - a == zero -- negate a == zero - a -- negate a + a == zero -- a + negate a == zero -- one * a == a -- a * one == a -- ((a * b) * c) == (a * (b * c)) -- (a * (b + c)) == (a * b + a * c) -- ((a + b) * c) == (a * c + b * c) -- a * zero == zero -- zero * a == zero -- a / a == one || a == zero -- recip a == one / a || a == zero -- recip a * a == one || a == zero -- a * recip a == one || a == zero --class (Distributive a, Subtractive a, Divisive a) => Field a -- | A half is a Field because it requires addition, -- multiplication and division. half :: Field a => a -- | A meet-semilattice with an identity element top for /\. -- --
-- Identity: x /\ top == x --class MeetSemiLattice a => BoundedMeetSemiLattice a top :: BoundedMeetSemiLattice a => a -- | A join-semilattice with an identity element bottom for -- \/. -- --
-- Identity: x \/ bottom == x --class JoinSemiLattice a => BoundedJoinSemiLattice a bottom :: BoundedJoinSemiLattice a => a -- | A algebraic structure with element meets: See Semilattice -- --
-- Associativity: x /\ (y /\ z) == (x /\ y) /\ z -- Commutativity: x /\ y == y /\ x -- Idempotency: x /\ x == x --class (Eq a) => MeetSemiLattice a (/\) :: MeetSemiLattice a => a -> a -> a infixr 6 /\ -- | A algebraic structure with element joins: See Semilattice -- --
-- Associativity: x \/ (y \/ z) == (x \/ y) \/ z -- Commutativity: x \/ y == y \/ x -- Idempotency: x \/ x == x --class (Eq a) => JoinSemiLattice a (\/) :: JoinSemiLattice a => a -> a -> a infixr 5 \/ -- | The partial ordering induced by the join-semilattice structure joinLeq :: JoinSemiLattice a => a -> a -> Bool -- | The partial ordering induced by the meet-semilattice structure meetLeq :: MeetSemiLattice a => a -> a -> Bool -- | A Module -- --
-- a .* one == a -- (a + b) .* c == (a .* c) + (b .* c) -- c *. (a + b) == (c *. a) + (c *. b) -- a .* zero == zero -- a .* b == b *. a --class (Distributive a, MultiplicativeAction m a) => Module m a -- | Divisive Action class (Divisive a) => DivisiveAction m a | m -> a (./) :: DivisiveAction m a => a -> m -> m (/.) :: DivisiveAction m a => m -> a -> m infixl 7 ./ infixl 7 /. -- | Multiplicative Action class (Multiplicative a) => MultiplicativeAction m a | m -> a (.*) :: MultiplicativeAction m a => a -> m -> m (*.) :: MultiplicativeAction m a => m -> a -> m infixl 7 .* infixl 7 *. -- | Subtractive Action class (Subtractive a) => SubtractiveAction m a | m -> a (.-) :: SubtractiveAction m a => a -> m -> m (-.) :: SubtractiveAction m a => m -> a -> m infixl 6 .- infixl 6 -. -- | Additive Action class (Additive a) => AdditiveAction m a | m -> a (.+) :: AdditiveAction m a => a -> m -> m (+.) :: AdditiveAction m a => m -> a -> m infixl 6 .+ infixl 6 +. -- | A small number, especially useful for approximate equality. class (Eq a, Additive a, Subtractive a, MeetSemiLattice a) => Epsilon a epsilon :: Epsilon a => a nearZero :: Epsilon a => a -> Bool aboutEqual :: Epsilon a => a -> a -> Bool -- | Something that has a magnitude and a direction. data Polar mag dir Polar :: !mag -> !dir -> Polar mag dir [magnitude] :: Polar mag dir -> !mag [direction] :: Polar mag dir -> !dir -- | Convert between a "co-ordinated" or "higher-kinded" number and -- representations of an angle. Typically thought of as polar co-ordinate -- conversion. -- -- See Polar coordinate system -- --
-- ray . angle == basis -- norm (ray x) == one --class (Additive coord, Multiplicative coord, Additive dir, Multiplicative dir) => Direction coord dir | coord -> dir angle :: Direction coord dir => coord -> dir ray :: Direction coord dir => dir -> coord -- | Norm is a slight generalisation of Signed. The class has the same -- shape but allows the codomain to be different to the domain. -- --
-- norm a >= zero -- norm zero == zero -- a == norm a .* basis a -- norm (basis a) == one --class (Additive a, Multiplicative b, Additive b) => Norm a b | a -> b -- | or length, or ||v|| norm :: Norm a b => a -> b -- | or direction, or v-hat basis :: Norm a b => a -> a -- | signum from base is not an operator name in numhask and is -- replaced by sign. Compare with Norm where there is a -- change in codomain. -- --
-- abs a * sign a == a ---- -- abs zero == zero, so any value for sign zero is ok. We choose lawful -- neutral: -- --
-- sign zero == zero --class (Additive a, Multiplicative a) => Signed a sign :: Signed a => a -> a abs :: Signed a => a -> a -- | Distance, which combines the Subtractive notion of difference, with -- Norm. -- --
-- distance a b >= zero -- distance a a == zero -- distance a b .* basis (a - b) == a - b --distance :: (Norm a b, Subtractive a) => a -> a -> b -- | Convert from a number to a Polar. polar :: (Norm coord mag, Direction coord dir) => coord -> Polar mag dir -- | Convert from a Polar to a (coordinated aka higher-kinded) number. coord :: (MultiplicativeAction coord mag, Direction coord dir) => Polar mag dir -> coord -- | About equal. (~=) :: Epsilon a => a -> a -> Bool infixl 4 ~= -- | An Abelian Group is an Associative, Unital, Invertible and -- Commutative Magma . In other words, it is a Commutative Group class (Associative a, Unital a, Invertible a, Commutative a) => AbelianGroup a -- | An Idempotent Magma is a magma where every element is -- Idempotent. -- --
-- a ⊕ a = a --class Magma a => Idempotent a -- | An Absorbing is a Magma with an Absorbing Element -- --
-- a ⊕ absorb = absorb --class Magma a => Absorbing a absorb :: Absorbing a => a -- | A Group is a Associative, Unital and Invertible Magma. class (Associative a, Unital a, Invertible a) => Group a -- | An Invertible Magma -- --
-- ∀ a,b ∈ T: inv a ⊕ (a ⊕ b) = b = (b ⊕ a) ⊕ inv a --class Magma a => Invertible a inv :: Invertible a => a -> a -- | A Commutative Magma is a Magma where the binary operation is -- commutative. -- --
-- a ⊕ b = b ⊕ a --class Magma a => Commutative a -- | An Associative Magma -- --
-- (a ⊕ b) ⊕ c = a ⊕ (b ⊕ c) --class Magma a => Associative a -- | A Unital Magma is a magma with an identity element (the unit). -- --
-- unit ⊕ a = a -- a ⊕ unit = a --class Magma a => Unital a unit :: Unital a => a -- | A Magma is a tuple (T,magma) consisting of -- --
-- ∀ a, b ∈ T: a ⊕ b ∈ T ---- -- These laws are true by construction in haskell: the type signature of -- ⊕ and the above mathematical laws are synonyms. class Magma a (⊕) :: Magma a => a -> a -> a infix 3 ⊕ -- | Complex numbers have real and imaginary parts. -- -- The Foldable and Traversable instances traverse the real -- part first. data Complex a -- | forms a complex number from its real and imaginary rectangular -- components. (:+) :: !a -> !a -> Complex a infix 6 :+ -- | Extracts the real part of a complex number. realPart :: Complex a -> a -- | Extracts the imaginary part of a complex number. imagPart :: Complex a -> a -- | fromInteger is special in two ways: -- --
-- fromIntegral a == a --class FromIntegral a b fromIntegral :: FromIntegral a b => b -> a fromIntegral :: (FromIntegral a b, a ~ b) => b -> a -- | toIntegral is kept separate from Integral to help with compatability -- issues. -- --
-- toIntegral a == a --class ToIntegral a b toIntegral :: ToIntegral a b => a -> b toIntegral :: (ToIntegral a b, a ~ b) => a -> b -- | An Integral is anything that satisfies the law: -- --
-- b == zero || b * (a `div` b) + (a `mod` b) == a --class (Distributive a) => Integral a div :: Integral a => a -> a -> a mod :: Integral a => a -> a -> a divMod :: Integral a => a -> a -> (a, a) quot :: Integral a => a -> a -> a rem :: Integral a => a -> a -> a quotRem :: Integral a => a -> a -> (a, a) infixl 7 `mod` infixl 7 `div` -- |
-- >>> even 2 -- True --even :: (Eq a, Integral a) => a -> Bool -- |
-- >>> odd 3 -- True --odd :: (Eq a, Integral a) => a -> Bool -- | raise a number to an Integral power (^^) :: (Ord b, Divisive a, Subtractive b, Integral b) => a -> b -> a infixr 8 ^^ -- | raise a number to an Int power -- -- Note: This differs from (^) found in prelude which is a partial -- function (it errors on negative integrals). This monomorphic version -- is provided to help reduce ambiguous type noise in common usages of -- this sign. (^) :: Divisive a => a -> Int -> a infixr 8 ^ -- | Arbitrary-precision rational numbers, represented as a ratio of two -- Integer values. A rational number may be constructed using the -- % operator. type Rational = Ratio Integer -- | fromRational is special in two ways: -- --
-- >>> fromRatio (5 :% 2 :: Ratio Integer) :: Double -- 2.5 --class FromRatio a b fromRatio :: FromRatio a b => Ratio b -> a fromRatio :: (FromRatio a b, Ratio b ~ a) => Ratio b -> a -- | toRatio is equivalent to Real in base, but is polymorphic in -- the Integral type. -- --
-- toRatio (3.1415927 :: Float) :: Ratio Integer ---- -- 13176795 :% 4194304 class ToRatio a b toRatio :: ToRatio a b => a -> Ratio b toRatio :: (ToRatio a b, Ratio c ~ a, FromIntegral b c, ToRatio (Ratio b) b) => a -> Ratio b -- | A rational number data Ratio a (:%) :: !a -> !a -> Ratio a -- | reduce normalises a ratio by dividing both numerator and -- denominator by their greatest common divisor. reduce :: (Eq a, Subtractive a, Signed a, Integral a) => a -> a -> Ratio a -- | gcd x y is the non-negative factor of both x -- and y of which every common factor of x and -- y is also a factor; for example gcd 4 2 = 2, -- gcd (-4) 6 = 2, gcd 0 4 = 4. -- gcd 0 0 = 0. (That is, the common divisor -- that is "greatest" in the divisibility preordering.) -- -- Note: Since for signed fixed-width integer types, abs -- minBound < 0, the result may be negative if one of the -- arguments is minBound (and necessarily is if the other -- is 0 or minBound) for such types. gcd :: (Eq a, Signed a, Integral a) => a -> a -> a -- | Throw an exception. Exceptions may be thrown from purely functional -- code, but may only be caught within the IO monad. throw :: forall (r :: RuntimeRep) (a :: TYPE r) e. Exception e => e -> a -- | A numhask exception. newtype NumHaskException NumHaskException :: String -> NumHaskException [errorMessage] :: NumHaskException -> String -- | A prelude composed by overlaying numhask on Prelude, together with a -- few minor tweaks needed for RebindableSyntax. module NumHask.Prelude -- | RebindableSyntax splats this, and I'm not sure where it exists in GHC -- land ifThenElse :: Bool -> a -> a -> a -- | The fromList function constructs the structure l from -- the given list of Item l fromList :: IsList l => [Item l] -> l -- | The fromListN function takes the input list's length as a hint. -- Its behaviour should be equivalent to fromList. The hint can be -- used to construct the structure l more efficiently compared -- to fromList. If the given hint does not equal to the input -- list's length the behaviour of fromListN is not specified. fromListN :: IsList l => Int -> [Item l] -> l -- | Type representing arbitrary-precision non-negative integers. -- --
-- >>> 2^100 :: Natural -- 1267650600228229401496703205376 ---- -- Operations whose result would be negative throw -- (Underflow :: ArithException), -- --
-- >>> -1 :: Natural -- *** Exception: arithmetic underflow --data Natural -- | in [0, maxBound::Word] NatS# :: GmpLimb# -> Natural -- | in ]maxBound::Word, +inf[ -- -- Invariant: NatJ# is used iff value doesn't fit in -- NatS# constructor. NB: Order of constructors *must* coincide -- with Ord relation NatJ# :: {-# UNPACK #-} !BigNat -> Natural -- | Append two lists, i.e., -- --
-- [x1, ..., xm] ++ [y1, ..., yn] == [x1, ..., xm, y1, ..., yn] -- [x1, ..., xm] ++ [y1, ...] == [x1, ..., xm, y1, ...] ---- -- If the first list is not finite, the result is the first list. (++) :: [a] -> [a] -> [a] infixr 5 ++ -- | The value of seq a b is bottom if a is bottom, and -- otherwise equal to b. In other words, it evaluates the first -- argument a to weak head normal form (WHNF). seq is -- usually introduced to improve performance by avoiding unneeded -- laziness. -- -- A note on evaluation order: the expression seq a b does -- not guarantee that a will be evaluated before -- b. The only guarantee given by seq is that the both -- a and b will be evaluated before seq -- returns a value. In particular, this means that b may be -- evaluated before a. If you need to guarantee a specific order -- of evaluation, you must use the function pseq from the -- "parallel" package. seq :: forall (r :: RuntimeRep) a (b :: TYPE r). a -> b -> b infixr 0 `seq` -- | <math>. filter, applied to a predicate and a list, -- returns the list of those elements that satisfy the predicate; i.e., -- --
-- filter p xs = [ x | x <- xs, p x] ---- --
-- >>> filter odd [1, 2, 3] -- [1,3] --filter :: (a -> Bool) -> [a] -> [a] -- | <math>. zip takes two lists and returns a list of -- corresponding pairs. -- --
-- zip [1, 2] ['a', 'b'] = [(1, 'a'), (2, 'b')] ---- -- If one input list is short, excess elements of the longer list are -- discarded: -- --
-- zip [1] ['a', 'b'] = [(1, 'a')] -- zip [1, 2] ['a'] = [(1, 'a')] ---- -- zip is right-lazy: -- --
-- zip [] _|_ = [] -- zip _|_ [] = _|_ ---- -- zip is capable of list fusion, but it is restricted to its -- first list argument and its resulting list. zip :: [a] -> [b] -> [(a, b)] -- | The print function outputs a value of any printable type to the -- standard output device. Printable types are those that are instances -- of class Show; print converts values to strings for -- output using the show operation and adds a newline. -- -- For example, a program to print the first 20 integers and their powers -- of 2 could be written as: -- --
-- main = print ([(n, 2^n) | n <- [0..19]]) --print :: Show a => a -> IO () -- | Extract the first component of a pair. fst :: (a, b) -> a -- | Extract the second component of a pair. snd :: (a, b) -> b -- | otherwise is defined as the value True. It helps to make -- guards more readable. eg. -- --
-- f x | x < 0 = ... -- | otherwise = ... --otherwise :: Bool -- | <math>. map f xs is the list obtained by -- applying f to each element of xs, i.e., -- --
-- map f [x1, x2, ..., xn] == [f x1, f x2, ..., f xn] -- map f [x1, x2, ...] == [f x1, f x2, ...] ---- --
-- >>> map (+1) [1, 2, 3] --map :: (a -> b) -> [a] -> [b] -- | Application operator. This operator is redundant, since ordinary -- application (f x) means the same as (f $ x). -- However, $ has low, right-associative binding precedence, so it -- sometimes allows parentheses to be omitted; for example: -- --
-- f $ g $ h x = f (g (h x)) ---- -- It is also useful in higher-order situations, such as map -- ($ 0) xs, or zipWith ($) fs xs. -- -- Note that ($) is levity-polymorphic in its result -- type, so that foo $ True where foo :: Bool -> -- Int# is well-typed. ($) :: forall (r :: RuntimeRep) a (b :: TYPE r). (a -> b) -> a -> b infixr 0 $ -- | general coercion to fractional types realToFrac :: (Real a, Fractional b) => a -> b -- | The Bounded class is used to name the upper and lower limits of -- a type. Ord is not a superclass of Bounded since types -- that are not totally ordered may also have upper and lower bounds. -- -- The Bounded class may be derived for any enumeration type; -- minBound is the first constructor listed in the data -- declaration and maxBound is the last. Bounded may also -- be derived for single-constructor datatypes whose constituent types -- are in Bounded. class Bounded a minBound :: Bounded a => a maxBound :: Bounded a => a -- | Class Enum defines operations on sequentially ordered types. -- -- The enumFrom... methods are used in Haskell's translation of -- arithmetic sequences. -- -- Instances of Enum may be derived for any enumeration type -- (types whose constructors have no fields). The nullary constructors -- are assumed to be numbered left-to-right by fromEnum from -- 0 through n-1. See Chapter 10 of the Haskell -- Report for more details. -- -- For any type that is an instance of class Bounded as well as -- Enum, the following should hold: -- --
-- enumFrom x = enumFromTo x maxBound -- enumFromThen x y = enumFromThenTo x y bound -- where -- bound | fromEnum y >= fromEnum x = maxBound -- | otherwise = minBound --class Enum a -- | the successor of a value. For numeric types, succ adds 1. succ :: Enum a => a -> a -- | the predecessor of a value. For numeric types, pred subtracts -- 1. pred :: Enum a => a -> a -- | Convert from an Int. toEnum :: Enum a => Int -> a -- | Convert to an Int. It is implementation-dependent what -- fromEnum returns when applied to a value that is too large to -- fit in an Int. fromEnum :: Enum a => a -> Int -- | Used in Haskell's translation of [n..] with [n..] = -- enumFrom n, a possible implementation being enumFrom n = n : -- enumFrom (succ n). For example: -- --
enumFrom 4 :: [Integer] = [4,5,6,7,...]
enumFrom 6 :: [Int] = [6,7,8,9,...,maxBound :: -- Int]
enumFromThen 4 6 :: [Integer] = [4,6,8,10...]
enumFromThen 6 2 :: [Int] = [6,2,-2,-6,...,minBound :: -- Int]
enumFromTo 6 10 :: [Int] = [6,7,8,9,10]
enumFromTo 42 1 :: [Integer] = []
enumFromThenTo 4 2 -6 :: [Integer] = -- [4,2,0,-2,-4,-6]
enumFromThenTo 6 8 2 :: [Int] = []
-- do a <- as -- bs a --(>>=) :: Monad m => m a -> (a -> m b) -> m b -- | Sequentially compose two actions, discarding any value produced by the -- first, like sequencing operators (such as the semicolon) in imperative -- languages. -- -- 'as >> bs' can be understood as the do -- expression -- --
-- do as -- bs --(>>) :: Monad m => m a -> m b -> m b -- | Inject a value into the monadic type. return :: Monad m => a -> m a infixl 1 >> infixl 1 >>= -- | A type f is a Functor if it provides a function fmap -- which, given any types a and b lets you apply any -- function from (a -> b) to turn an f a into an -- f b, preserving the structure of f. Furthermore -- f needs to adhere to the following: -- -- -- -- Note, that the second law follows from the free theorem of the type -- fmap and the first law, so you need only check that the former -- condition holds. class Functor (f :: Type -> Type) -- | Using ApplicativeDo: 'fmap f as' can be -- understood as the do expression -- --
-- do a <- as -- pure (f a) ---- -- with an inferred Functor constraint. fmap :: Functor f => (a -> b) -> f a -> f b -- | Replace all locations in the input with the same value. The default -- definition is fmap . const, but this may be -- overridden with a more efficient version. -- -- Using ApplicativeDo: 'a <$ bs' can be -- understood as the do expression -- --
-- do bs -- pure a ---- -- with an inferred Functor constraint. (<$) :: Functor f => a -> f b -> f a infixl 4 <$ -- | Basic numeric class. -- -- The Haskell Report defines no laws for Num. However, -- (+) and (*) are customarily expected -- to define a ring and have the following properties: -- --
-- abs x * signum x == x ---- -- For real numbers, the signum is either -1 (negative), -- 0 (zero) or 1 (positive). signum :: Num a => a -> a -- | The Ord class is used for totally ordered datatypes. -- -- Instances of Ord can be derived for any user-defined datatype -- whose constituent types are in Ord. The declared order of the -- constructors in the data declaration determines the ordering in -- derived Ord instances. The Ordering datatype allows a -- single comparison to determine the precise ordering of two objects. -- -- The Haskell Report defines no laws for Ord. However, -- <= is customarily expected to implement a non-strict partial -- order and have the following properties: -- --
-- infixr 5 :^: -- data Tree a = Leaf a | Tree a :^: Tree a ---- -- the derived instance of Read in Haskell 2010 is equivalent to -- --
-- instance (Read a) => Read (Tree a) where
--
-- readsPrec d r = readParen (d > app_prec)
-- (\r -> [(Leaf m,t) |
-- ("Leaf",s) <- lex r,
-- (m,t) <- readsPrec (app_prec+1) s]) r
--
-- ++ readParen (d > up_prec)
-- (\r -> [(u:^:v,w) |
-- (u,s) <- readsPrec (up_prec+1) r,
-- (":^:",t) <- lex s,
-- (v,w) <- readsPrec (up_prec+1) t]) r
--
-- where app_prec = 10
-- up_prec = 5
--
--
-- Note that right-associativity of :^: is unused.
--
-- The derived instance in GHC is equivalent to
--
-- -- instance (Read a) => Read (Tree a) where -- -- readPrec = parens $ (prec app_prec $ do -- Ident "Leaf" <- lexP -- m <- step readPrec -- return (Leaf m)) -- -- +++ (prec up_prec $ do -- u <- step readPrec -- Symbol ":^:" <- lexP -- v <- step readPrec -- return (u :^: v)) -- -- where app_prec = 10 -- up_prec = 5 -- -- readListPrec = readListPrecDefault ---- -- Why do both readsPrec and readPrec exist, and why does -- GHC opt to implement readPrec in derived Read instances -- instead of readsPrec? The reason is that readsPrec is -- based on the ReadS type, and although ReadS is mentioned -- in the Haskell 2010 Report, it is not a very efficient parser data -- structure. -- -- readPrec, on the other hand, is based on a much more efficient -- ReadPrec datatype (a.k.a "new-style parsers"), but its -- definition relies on the use of the RankNTypes language -- extension. Therefore, readPrec (and its cousin, -- readListPrec) are marked as GHC-only. Nevertheless, it is -- recommended to use readPrec instead of readsPrec -- whenever possible for the efficiency improvements it brings. -- -- As mentioned above, derived Read instances in GHC will -- implement readPrec instead of readsPrec. The default -- implementations of readsPrec (and its cousin, readList) -- will simply use readPrec under the hood. If you are writing a -- Read instance by hand, it is recommended to write it like so: -- --
-- instance Read T where -- readPrec = ... -- readListPrec = readListPrecDefault --class Read a -- | attempts to parse a value from the front of the string, returning a -- list of (parsed value, remaining string) pairs. If there is no -- successful parse, the returned list is empty. -- -- Derived instances of Read and Show satisfy the -- following: -- -- -- -- That is, readsPrec parses the string produced by -- showsPrec, and delivers the value that showsPrec started -- with. readsPrec :: Read a => Int -> ReadS a -- | The method readList is provided to allow the programmer to give -- a specialised way of parsing lists of values. For example, this is -- used by the predefined Read instance of the Char type, -- where values of type String should be are expected to use -- double quotes, rather than square brackets. readList :: Read a => ReadS [a] -- | Efficient, machine-independent access to the components of a -- floating-point number. class (RealFrac a, Floating a) => RealFloat a -- | a constant function, returning the radix of the representation (often -- 2) floatRadix :: RealFloat a => a -> Integer -- | a constant function, returning the number of digits of -- floatRadix in the significand floatDigits :: RealFloat a => a -> Int -- | a constant function, returning the lowest and highest values the -- exponent may assume floatRange :: RealFloat a => a -> (Int, Int) -- | The function decodeFloat applied to a real floating-point -- number returns the significand expressed as an Integer and an -- appropriately scaled exponent (an Int). If -- decodeFloat x yields (m,n), then x -- is equal in value to m*b^^n, where b is the -- floating-point radix, and furthermore, either m and -- n are both zero or else b^(d-1) <= abs m < -- b^d, where d is the value of floatDigits -- x. In particular, decodeFloat 0 = (0,0). If the -- type contains a negative zero, also decodeFloat (-0.0) = -- (0,0). The result of decodeFloat x is -- unspecified if either of isNaN x or -- isInfinite x is True. decodeFloat :: RealFloat a => a -> (Integer, Int) -- | encodeFloat performs the inverse of decodeFloat in the -- sense that for finite x with the exception of -0.0, -- uncurry encodeFloat (decodeFloat x) = x. -- encodeFloat m n is one of the two closest -- representable floating-point numbers to m*b^^n (or -- ±Infinity if overflow occurs); usually the closer, but if -- m contains too many bits, the result may be rounded in the -- wrong direction. encodeFloat :: RealFloat a => Integer -> Int -> a -- | exponent corresponds to the second component of -- decodeFloat. exponent 0 = 0 and for finite -- nonzero x, exponent x = snd (decodeFloat x) -- + floatDigits x. If x is a finite floating-point -- number, it is equal in value to significand x * b ^^ -- exponent x, where b is the floating-point radix. -- The behaviour is unspecified on infinite or NaN values. exponent :: RealFloat a => a -> Int -- | The first component of decodeFloat, scaled to lie in the open -- interval (-1,1), either 0.0 or of absolute -- value >= 1/b, where b is the floating-point -- radix. The behaviour is unspecified on infinite or NaN -- values. significand :: RealFloat a => a -> a -- | multiplies a floating-point number by an integer power of the radix scaleFloat :: RealFloat a => Int -> a -> a -- | True if the argument is an IEEE "not-a-number" (NaN) value isNaN :: RealFloat a => a -> Bool -- | True if the argument is an IEEE infinity or negative infinity isInfinite :: RealFloat a => a -> Bool -- | True if the argument is too small to be represented in -- normalized format isDenormalized :: RealFloat a => a -> Bool -- | True if the argument is an IEEE negative zero isNegativeZero :: RealFloat a => a -> Bool -- | True if the argument is an IEEE floating point number isIEEE :: RealFloat a => a -> Bool -- | Extracting components of fractions. class (Real a, Fractional a) => RealFrac a -- | Conversion of values to readable Strings. -- -- Derived instances of Show have the following properties, which -- are compatible with derived instances of Read: -- --
-- infixr 5 :^: -- data Tree a = Leaf a | Tree a :^: Tree a ---- -- the derived instance of Show is equivalent to -- --
-- instance (Show a) => Show (Tree a) where -- -- showsPrec d (Leaf m) = showParen (d > app_prec) $ -- showString "Leaf " . showsPrec (app_prec+1) m -- where app_prec = 10 -- -- showsPrec d (u :^: v) = showParen (d > up_prec) $ -- showsPrec (up_prec+1) u . -- showString " :^: " . -- showsPrec (up_prec+1) v -- where up_prec = 5 ---- -- Note that right-associativity of :^: is ignored. For example, -- --
-- showsPrec d x r ++ s == showsPrec d x (r ++ s) ---- -- Derived instances of Read and Show satisfy the -- following: -- -- -- -- That is, readsPrec parses the string produced by -- showsPrec, and delivers the value that showsPrec started -- with. showsPrec :: Show a => Int -> a -> ShowS -- | A specialised variant of showsPrec, using precedence context -- zero, and returning an ordinary String. show :: Show a => a -> String -- | The method showList is provided to allow the programmer to give -- a specialised way of showing lists of values. For example, this is -- used by the predefined Show instance of the Char type, -- where values of type String should be shown in double quotes, -- rather than between square brackets. showList :: Show a => [a] -> ShowS -- | When a value is bound in do-notation, the pattern on the left -- hand side of <- might not match. In this case, this class -- provides a function to recover. -- -- A Monad without a MonadFail instance may only be used in -- conjunction with pattern that always match, such as newtypes, tuples, -- data types with only a single data constructor, and irrefutable -- patterns (~pat). -- -- Instances of MonadFail should satisfy the following law: -- fail s should be a left zero for >>=, -- --
-- fail s >>= f = fail s ---- -- If your Monad is also MonadPlus, a popular definition is -- --
-- fail _ = mzero --class Monad m => MonadFail (m :: Type -> Type) fail :: MonadFail m => String -> m a -- | A functor with application, providing operations to -- --
-- (<*>) = liftA2 id ---- --
-- liftA2 f x y = f <$> x <*> y ---- -- Further, any definition must satisfy the following: -- --
pure id <*> v = -- v
pure (.) <*> u -- <*> v <*> w = u <*> (v -- <*> w)
pure f <*> -- pure x = pure (f x)
u <*> pure y = -- pure ($ y) <*> u
-- forall x y. p (q x y) = f x . g y ---- -- it follows from the above that -- --
-- liftA2 p (liftA2 q u v) = liftA2 f u . liftA2 g v ---- -- If f is also a Monad, it should satisfy -- -- -- -- (which implies that pure and <*> satisfy the -- applicative functor laws). class Functor f => Applicative (f :: Type -> Type) -- | Lift a value. pure :: Applicative f => a -> f a -- | Sequential application. -- -- A few functors support an implementation of <*> that is -- more efficient than the default one. -- -- Using ApplicativeDo: 'fs <*> as' can be -- understood as the do expression -- --
-- do f <- fs -- a <- as -- pure (f a) --(<*>) :: Applicative f => f (a -> b) -> f a -> f b -- | Sequence actions, discarding the value of the first argument. -- -- 'as *> bs' can be understood as the do -- expression -- --
-- do as -- bs ---- -- This is a tad complicated for our ApplicativeDo extension -- which will give it a Monad constraint. For an -- Applicative constraint we write it of the form -- --
-- do _ <- as -- b <- bs -- pure b --(*>) :: Applicative f => f a -> f b -> f b -- | Sequence actions, discarding the value of the second argument. -- -- Using ApplicativeDo: 'as <* bs' can be -- understood as the do expression -- --
-- do a <- as -- bs -- pure a --(<*) :: Applicative f => f a -> f b -> f a infixl 4 <* infixl 4 *> infixl 4 <*> -- | Data structures that can be folded. -- -- For example, given a data type -- --
-- data Tree a = Empty | Leaf a | Node (Tree a) a (Tree a) ---- -- a suitable instance would be -- --
-- instance Foldable Tree where -- foldMap f Empty = mempty -- foldMap f (Leaf x) = f x -- foldMap f (Node l k r) = foldMap f l `mappend` f k `mappend` foldMap f r ---- -- This is suitable even for abstract types, as the monoid is assumed to -- satisfy the monoid laws. Alternatively, one could define -- foldr: -- --
-- instance Foldable Tree where -- foldr f z Empty = z -- foldr f z (Leaf x) = f x z -- foldr f z (Node l k r) = foldr f (f k (foldr f z r)) l ---- -- Foldable instances are expected to satisfy the following -- laws: -- --
-- foldr f z t = appEndo (foldMap (Endo . f) t ) z ---- --
-- foldl f z t = appEndo (getDual (foldMap (Dual . Endo . flip f) t)) z ---- --
-- fold = foldMap id ---- --
-- length = getSum . foldMap (Sum . const 1) ---- -- sum, product, maximum, and minimum -- should all be essentially equivalent to foldMap forms, such -- as -- --
-- sum = getSum . foldMap Sum ---- -- but may be less defined. -- -- If the type is also a Functor instance, it should satisfy -- --
-- foldMap f = fold . fmap f ---- -- which implies that -- --
-- foldMap f . fmap g = foldMap (f . g) --class Foldable (t :: Type -> Type) -- | Map each element of the structure to a monoid, and combine the -- results. foldMap :: (Foldable t, Monoid m) => (a -> m) -> t a -> m -- | Right-associative fold of a structure. -- -- In the case of lists, foldr, when applied to a binary operator, -- a starting value (typically the right-identity of the operator), and a -- list, reduces the list using the binary operator, from right to left: -- --
-- foldr f z [x1, x2, ..., xn] == x1 `f` (x2 `f` ... (xn `f` z)...) ---- -- Note that, since the head of the resulting expression is produced by -- an application of the operator to the first element of the list, -- foldr can produce a terminating expression from an infinite -- list. -- -- For a general Foldable structure this should be semantically -- identical to, -- --
-- foldr f z = foldr f z . toList --foldr :: Foldable t => (a -> b -> b) -> b -> t a -> b -- | Left-associative fold of a structure. -- -- In the case of lists, foldl, when applied to a binary operator, -- a starting value (typically the left-identity of the operator), and a -- list, reduces the list using the binary operator, from left to right: -- --
-- foldl f z [x1, x2, ..., xn] == (...((z `f` x1) `f` x2) `f`...) `f` xn ---- -- Note that to produce the outermost application of the operator the -- entire input list must be traversed. This means that foldl' -- will diverge if given an infinite list. -- -- Also note that if you want an efficient left-fold, you probably want -- to use foldl' instead of foldl. The reason for this is -- that latter does not force the "inner" results (e.g. z `f` x1 -- in the above example) before applying them to the operator (e.g. to -- (`f` x2)). This results in a thunk chain <math> -- elements long, which then must be evaluated from the outside-in. -- -- For a general Foldable structure this should be semantically -- identical to, -- --
-- foldl f z = foldl f z . toList --foldl :: Foldable t => (b -> a -> b) -> b -> t a -> b -- | A variant of foldr that has no base case, and thus may only be -- applied to non-empty structures. -- --
-- foldr1 f = foldr1 f . toList --foldr1 :: Foldable t => (a -> a -> a) -> t a -> a -- | A variant of foldl that has no base case, and thus may only be -- applied to non-empty structures. -- --
-- foldl1 f = foldl1 f . toList --foldl1 :: Foldable t => (a -> a -> a) -> t a -> a -- | Test whether the structure is empty. The default implementation is -- optimized for structures that are similar to cons-lists, because there -- is no general way to do better. null :: Foldable t => t a -> Bool -- | Returns the size/length of a finite structure as an Int. The -- default implementation is optimized for structures that are similar to -- cons-lists, because there is no general way to do better. length :: Foldable t => t a -> Int -- | Does the element occur in the structure? elem :: (Foldable t, Eq a) => a -> t a -> Bool -- | The largest element of a non-empty structure. maximum :: (Foldable t, Ord a) => t a -> a -- | The least element of a non-empty structure. minimum :: (Foldable t, Ord a) => t a -> a infix 4 `elem` -- | Functors representing data structures that can be traversed from left -- to right. -- -- A definition of traverse must satisfy the following laws: -- --
-- t :: (Applicative f, Applicative g) => f a -> g a ---- -- preserving the Applicative operations, i.e. -- --
-- t (pure x) = pure x -- t (f <*> x) = t f <*> t x ---- -- and the identity functor Identity and composition functors -- Compose are from Data.Functor.Identity and -- Data.Functor.Compose. -- -- A result of the naturality law is a purity law for traverse -- --
-- traverse pure = pure ---- -- (The naturality law is implied by parametricity and thus so is the -- purity law [1, p15].) -- -- Instances are similar to Functor, e.g. given a data type -- --
-- data Tree a = Empty | Leaf a | Node (Tree a) a (Tree a) ---- -- a suitable instance would be -- --
-- instance Traversable Tree where -- traverse f Empty = pure Empty -- traverse f (Leaf x) = Leaf <$> f x -- traverse f (Node l k r) = Node <$> traverse f l <*> f k <*> traverse f r ---- -- This is suitable even for abstract types, as the laws for -- <*> imply a form of associativity. -- -- The superclass instances should satisfy the following: -- --
-- >>> [1,2,3] <> [4,5,6] -- [1,2,3,4,5,6] --(<>) :: Semigroup a => a -> a -> a infixr 6 <> -- | The class of monoids (types with an associative binary operation that -- has an identity). Instances should satisfy the following: -- --
-- >>> "Hello world" <> mempty -- "Hello world" --mempty :: Monoid a => a -- | An associative operation -- -- NOTE: This method is redundant and has the default -- implementation mappend = (<>) since -- base-4.11.0.0. Should it be implemented manually, since -- mappend is a synonym for (<>), it is expected that -- the two functions are defined the same way. In a future GHC release -- mappend will be removed from Monoid. mappend :: Monoid a => a -> a -> a -- | Fold a list using the monoid. -- -- For most types, the default definition for mconcat will be -- used, but the function is included in the class definition so that an -- optimized version can be provided for specific types. -- --
-- >>> mconcat ["Hello", " ", "Haskell", "!"] -- "Hello Haskell!" --mconcat :: Monoid a => [a] -> a -- | The character type Char is an enumeration whose values -- represent Unicode (or equivalently ISO/IEC 10646) code points (i.e. -- characters, see http://www.unicode.org/ for details). This set -- extends the ISO 8859-1 (Latin-1) character set (the first 256 -- characters), which is itself an extension of the ASCII character set -- (the first 128 characters). A character literal in Haskell has type -- Char. -- -- To convert a Char to or from the corresponding Int value -- defined by Unicode, use toEnum and fromEnum from the -- Enum class respectively (or equivalently ord and -- chr). data Char -- | Double-precision floating point numbers. It is desirable that this -- type be at least equal in range and precision to the IEEE -- double-precision type. data Double -- | Single-precision floating point numbers. It is desirable that this -- type be at least equal in range and precision to the IEEE -- single-precision type. data Float -- | A fixed-precision integer type with at least the range [-2^29 .. -- 2^29-1]. The exact range for a given implementation can be -- determined by using minBound and maxBound from the -- Bounded class. data Int -- | Arbitrary precision integers. In contrast with fixed-size integral -- types such as Int, the Integer type represents the -- entire infinite range of integers. -- -- For more information about this type's representation, see the -- comments in its implementation. data Integer -- | The Maybe type encapsulates an optional value. A value of type -- Maybe a either contains a value of type a -- (represented as Just a), or it is empty (represented -- as Nothing). Using Maybe is a good way to deal with -- errors or exceptional cases without resorting to drastic measures such -- as error. -- -- The Maybe type is also a monad. It is a simple kind of error -- monad, where all errors are represented by Nothing. A richer -- error monad can be built using the Either type. data Maybe a Nothing :: Maybe a Just :: a -> Maybe a -- | Arbitrary-precision rational numbers, represented as a ratio of two -- Integer values. A rational number may be constructed using the -- % operator. type Rational = Ratio Integer -- | A value of type IO a is a computation which, when -- performed, does some I/O before returning a value of type a. -- -- There is really only one way to "perform" an I/O action: bind it to -- Main.main in your program. When your program is run, the I/O -- will be performed. It isn't possible to perform I/O from an arbitrary -- function, unless that function is itself in the IO monad and -- called at some point, directly or indirectly, from Main.main. -- -- IO is a monad, so IO actions can be combined using -- either the do-notation or the >> and >>= -- operations from the Monad class. data IO a -- | A Word is an unsigned integral type, with the same size as -- Int. data Word -- | The Either type represents values with two possibilities: a -- value of type Either a b is either Left -- a or Right b. -- -- The Either type is sometimes used to represent a value which is -- either correct or an error; by convention, the Left constructor -- is used to hold an error value and the Right constructor is -- used to hold a correct value (mnemonic: "right" also means "correct"). -- --
-- >>> let s = Left "foo" :: Either String Int -- -- >>> s -- Left "foo" -- -- >>> let n = Right 3 :: Either String Int -- -- >>> n -- Right 3 -- -- >>> :type s -- s :: Either String Int -- -- >>> :type n -- n :: Either String Int ---- -- The fmap from our Functor instance will ignore -- Left values, but will apply the supplied function to values -- contained in a Right: -- --
-- >>> let s = Left "foo" :: Either String Int -- -- >>> let n = Right 3 :: Either String Int -- -- >>> fmap (*2) s -- Left "foo" -- -- >>> fmap (*2) n -- Right 6 ---- -- The Monad instance for Either allows us to chain -- together multiple actions which may fail, and fail overall if any of -- the individual steps failed. First we'll write a function that can -- either parse an Int from a Char, or fail. -- --
-- >>> import Data.Char ( digitToInt, isDigit )
--
-- >>> :{
-- let parseEither :: Char -> Either String Int
-- parseEither c
-- | isDigit c = Right (digitToInt c)
-- | otherwise = Left "parse error"
--
-- >>> :}
--
--
-- The following should work, since both '1' and '2'
-- can be parsed as Ints.
--
--
-- >>> :{
-- let parseMultiple :: Either String Int
-- parseMultiple = do
-- x <- parseEither '1'
-- y <- parseEither '2'
-- return (x + y)
--
-- >>> :}
--
--
-- -- >>> parseMultiple -- Right 3 ---- -- But the following should fail overall, since the first operation where -- we attempt to parse 'm' as an Int will fail: -- --
-- >>> :{
-- let parseMultiple :: Either String Int
-- parseMultiple = do
-- x <- parseEither 'm'
-- y <- parseEither '2'
-- return (x + y)
--
-- >>> :}
--
--
-- -- >>> parseMultiple -- Left "parse error" --data Either a b Left :: a -> Either a b Right :: b -> Either a b -- | The readIO function is similar to read except that it -- signals parse failure to the IO monad instead of terminating -- the program. readIO :: Read a => String -> IO a -- | The readLn function combines getLine and readIO. readLn :: Read a => IO a -- | The computation appendFile file str function appends -- the string str, to the file file. -- -- Note that writeFile and appendFile write a literal -- string to a file. To write a value of any printable type, as with -- print, use the show function to convert the value to a -- string first. -- --
-- main = appendFile "squares" (show [(x,x*x) | x <- [0,0.1..2]]) --appendFile :: FilePath -> String -> IO () -- | The computation writeFile file str function writes the -- string str, to the file file. writeFile :: FilePath -> String -> IO () -- | The readFile function reads a file and returns the contents of -- the file as a string. The file is read lazily, on demand, as with -- getContents. readFile :: FilePath -> IO String -- | The interact function takes a function of type -- String->String as its argument. The entire input from the -- standard input device is passed to this function as its argument, and -- the resulting string is output on the standard output device. interact :: (String -> String) -> IO () -- | The getContents operation returns all user input as a single -- string, which is read lazily as it is needed (same as -- hGetContents stdin). getContents :: IO String -- | Read a line from the standard input device (same as hGetLine -- stdin). getLine :: IO String -- | Read a character from the standard input device (same as -- hGetChar stdin). getChar :: IO Char -- | The same as putStr, but adds a newline character. putStrLn :: String -> IO () -- | Write a string to the standard output device (same as hPutStr -- stdout). putStr :: String -> IO () -- | Write a character to the standard output device (same as -- hPutChar stdout). putChar :: Char -> IO () -- | Raise an IOError in the IO monad. ioError :: IOError -> IO a -- | File and directory names are values of type String, whose -- precise meaning is operating system dependent. Files can be opened, -- yielding a handle which can then be used to operate on the contents of -- that file. type FilePath = String -- | Construct an IOError value with a string describing the error. -- The fail method of the IO instance of the Monad -- class raises a userError, thus: -- --
-- instance Monad IO where -- ... -- fail s = ioError (userError s) --userError :: String -> IOError -- | The Haskell 2010 type for exceptions in the IO monad. Any I/O -- operation may raise an IOError instead of returning a result. -- For a more general type of exception, including also those that arise -- in pure code, see Exception. -- -- In Haskell 2010, this is an opaque type. type IOError = IOException -- | notElem is the negation of elem. notElem :: (Foldable t, Eq a) => a -> t a -> Bool infix 4 `notElem` -- | Determines whether all elements of the structure satisfy the -- predicate. all :: Foldable t => (a -> Bool) -> t a -> Bool -- | Determines whether any element of the structure satisfies the -- predicate. any :: Foldable t => (a -> Bool) -> t a -> Bool -- | or returns the disjunction of a container of Bools. For the -- result to be False, the container must be finite; True, -- however, results from a True value finitely far from the left -- end. or :: Foldable t => t Bool -> Bool -- | and returns the conjunction of a container of Bools. For the -- result to be True, the container must be finite; False, -- however, results from a False value finitely far from the left -- end. and :: Foldable t => t Bool -> Bool -- | Map a function over all the elements of a container and concatenate -- the resulting lists. concatMap :: Foldable t => (a -> [b]) -> t a -> [b] -- | The concatenation of all the elements of a container of lists. concat :: Foldable t => t [a] -> [a] -- | Evaluate each monadic action in the structure from left to right, and -- ignore the results. For a version that doesn't ignore the results see -- sequence. -- -- As of base 4.8.0.0, sequence_ is just sequenceA_, -- specialized to Monad. sequence_ :: (Foldable t, Monad m) => t (m a) -> m () -- | Map each element of a structure to a monadic action, evaluate these -- actions from left to right, and ignore the results. For a version that -- doesn't ignore the results see mapM. -- -- As of base 4.8.0.0, mapM_ is just traverse_, specialized -- to Monad. mapM_ :: (Foldable t, Monad m) => (a -> m b) -> t a -> m () -- | unwords is an inverse operation to words. It joins words -- with separating spaces. -- --
-- >>> unwords ["Lorem", "ipsum", "dolor"] -- "Lorem ipsum dolor" --unwords :: [String] -> String -- | words breaks a string up into a list of words, which were -- delimited by white space. -- --
-- >>> words "Lorem ipsum\ndolor" -- ["Lorem","ipsum","dolor"] --words :: String -> [String] -- | unlines is an inverse operation to lines. It joins -- lines, after appending a terminating newline to each. -- --
-- >>> unlines ["Hello", "World", "!"] -- "Hello\nWorld\n!\n" --unlines :: [String] -> String -- | lines breaks a string up into a list of strings at newline -- characters. The resulting strings do not contain newlines. -- -- Note that after splitting the string at newline characters, the last -- part of the string is considered a line even if it doesn't end with a -- newline. For example, -- --
-- >>> lines "" -- [] ---- --
-- >>> lines "\n" -- [""] ---- --
-- >>> lines "one" -- ["one"] ---- --
-- >>> lines "one\n" -- ["one"] ---- --
-- >>> lines "one\n\n" -- ["one",""] ---- --
-- >>> lines "one\ntwo" -- ["one","two"] ---- --
-- >>> lines "one\ntwo\n" -- ["one","two"] ---- -- Thus lines s contains at least as many elements as -- newlines in s. lines :: String -> [String] -- | The read function reads input from a string, which must be -- completely consumed by the input process. read fails with an -- error if the parse is unsuccessful, and it is therefore -- discouraged from being used in real applications. Use readMaybe -- or readEither for safe alternatives. -- --
-- >>> read "123" :: Int -- 123 ---- --
-- >>> read "hello" :: Int -- *** Exception: Prelude.read: no parse --read :: Read a => String -> a -- | equivalent to readsPrec with a precedence of 0. reads :: Read a => ReadS a -- | Case analysis for the Either type. If the value is -- Left a, apply the first function to a; if it -- is Right b, apply the second function to b. -- --
-- >>> let s = Left "foo" :: Either String Int -- -- >>> let n = Right 3 :: Either String Int -- -- >>> either length (*2) s -- 3 -- -- >>> either length (*2) n -- 6 --either :: (a -> c) -> (b -> c) -> Either a b -> c -- | The lex function reads a single lexeme from the input, -- discarding initial white space, and returning the characters that -- constitute the lexeme. If the input string contains only white space, -- lex returns a single successful `lexeme' consisting of the -- empty string. (Thus lex "" = [("","")].) If there is -- no legal lexeme at the beginning of the input string, lex fails -- (i.e. returns []). -- -- This lexer is not completely faithful to the Haskell lexical syntax in -- the following respects: -- --
-- >>> zipWith (+) [1, 2, 3] [4, 5, 6] -- [5,7,9] ---- -- zipWith is right-lazy: -- --
-- zipWith f [] _|_ = [] ---- -- zipWith is capable of list fusion, but it is restricted to its -- first list argument and its resulting list. zipWith :: (a -> b -> c) -> [a] -> [b] -> [c] -- | zip3 takes three lists and returns a list of triples, analogous -- to zip. It is capable of list fusion, but it is restricted to -- its first list argument and its resulting list. zip3 :: [a] -> [b] -> [c] -> [(a, b, c)] -- | List index (subscript) operator, starting from 0. It is an instance of -- the more general genericIndex, which takes an index of any -- integral type. (!!) :: [a] -> Int -> a infixl 9 !! -- | <math>. lookup key assocs looks up a key in an -- association list. -- --
-- >>> lookup 2 [(1, "first"), (2, "second"), (3, "third")] -- Just "second" --lookup :: Eq a => a -> [(a, b)] -> Maybe b -- | reverse xs returns the elements of xs in -- reverse order. xs must be finite. reverse :: [a] -> [a] -- | break, applied to a predicate p and a list -- xs, returns a tuple where first element is longest prefix -- (possibly empty) of xs of elements that do not satisfy -- p and second element is the remainder of the list: -- --
-- break (> 3) [1,2,3,4,1,2,3,4] == ([1,2,3],[4,1,2,3,4]) -- break (< 9) [1,2,3] == ([],[1,2,3]) -- break (> 9) [1,2,3] == ([1,2,3],[]) ---- -- break p is equivalent to span (not . -- p). break :: (a -> Bool) -> [a] -> ([a], [a]) -- | span, applied to a predicate p and a list xs, -- returns a tuple where first element is longest prefix (possibly empty) -- of xs of elements that satisfy p and second element -- is the remainder of the list: -- --
-- span (< 3) [1,2,3,4,1,2,3,4] == ([1,2],[3,4,1,2,3,4]) -- span (< 9) [1,2,3] == ([1,2,3],[]) -- span (< 0) [1,2,3] == ([],[1,2,3]) ---- -- span p xs is equivalent to (takeWhile p xs, -- dropWhile p xs) span :: (a -> Bool) -> [a] -> ([a], [a]) -- | splitAt n xs returns a tuple where first element is -- xs prefix of length n and second element is the -- remainder of the list: -- --
-- splitAt 6 "Hello World!" == ("Hello ","World!")
-- splitAt 3 [1,2,3,4,5] == ([1,2,3],[4,5])
-- splitAt 1 [1,2,3] == ([1],[2,3])
-- splitAt 3 [1,2,3] == ([1,2,3],[])
-- splitAt 4 [1,2,3] == ([1,2,3],[])
-- splitAt 0 [1,2,3] == ([],[1,2,3])
-- splitAt (-1) [1,2,3] == ([],[1,2,3])
--
--
-- It is equivalent to (take n xs, drop n xs) when
-- n is not _|_ (splitAt _|_ xs = _|_).
-- splitAt is an instance of the more general
-- genericSplitAt, in which n may be of any integral
-- type.
splitAt :: Int -> [a] -> ([a], [a])
-- | drop n xs returns the suffix of xs after the
-- first n elements, or [] if n > length
-- xs:
--
-- -- drop 6 "Hello World!" == "World!" -- drop 3 [1,2,3,4,5] == [4,5] -- drop 3 [1,2] == [] -- drop 3 [] == [] -- drop (-1) [1,2] == [1,2] -- drop 0 [1,2] == [1,2] ---- -- It is an instance of the more general genericDrop, in which -- n may be of any integral type. drop :: Int -> [a] -> [a] -- | take n, applied to a list xs, returns the -- prefix of xs of length n, or xs itself if -- n > length xs: -- --
-- take 5 "Hello World!" == "Hello" -- take 3 [1,2,3,4,5] == [1,2,3] -- take 3 [1,2] == [1,2] -- take 3 [] == [] -- take (-1) [1,2] == [] -- take 0 [1,2] == [] ---- -- It is an instance of the more general genericTake, in which -- n may be of any integral type. take :: Int -> [a] -> [a] -- | dropWhile p xs returns the suffix remaining after -- takeWhile p xs: -- --
-- dropWhile (< 3) [1,2,3,4,5,1,2,3] == [3,4,5,1,2,3] -- dropWhile (< 9) [1,2,3] == [] -- dropWhile (< 0) [1,2,3] == [1,2,3] --dropWhile :: (a -> Bool) -> [a] -> [a] -- | takeWhile, applied to a predicate p and a list -- xs, returns the longest prefix (possibly empty) of -- xs of elements that satisfy p: -- --
-- takeWhile (< 3) [1,2,3,4,1,2,3,4] == [1,2] -- takeWhile (< 9) [1,2,3] == [1,2,3] -- takeWhile (< 0) [1,2,3] == [] --takeWhile :: (a -> Bool) -> [a] -> [a] -- | cycle ties a finite list into a circular one, or equivalently, -- the infinite repetition of the original list. It is the identity on -- infinite lists. cycle :: [a] -> [a] -- | replicate n x is a list of length n with -- x the value of every element. It is an instance of the more -- general genericReplicate, in which n may be of any -- integral type. replicate :: Int -> a -> [a] -- | repeat x is an infinite list, with x the -- value of every element. repeat :: a -> [a] -- | iterate f x returns an infinite list of repeated -- applications of f to x: -- --
-- iterate f x == [x, f x, f (f x), ...] ---- -- Note that iterate is lazy, potentially leading to thunk -- build-up if the consumer doesn't force each iterate. See -- iterate' for a strict variant of this function. iterate :: (a -> a) -> a -> [a] -- | <math>. scanr1 is a variant of scanr that has no -- starting value argument. scanr1 :: (a -> a -> a) -> [a] -> [a] -- | <math>. scanr is the right-to-left dual of scanl. -- Note that -- --
-- head (scanr f z xs) == foldr f z xs. --scanr :: (a -> b -> b) -> b -> [a] -> [b] -- | <math>. scanl1 is a variant of scanl that has no -- starting value argument: -- --
-- scanl1 f [x1, x2, ...] == [x1, x1 `f` x2, ...] --scanl1 :: (a -> a -> a) -> [a] -> [a] -- | <math>. scanl is similar to foldl, but returns a -- list of successive reduced values from the left: -- --
-- scanl f z [x1, x2, ...] == [z, z `f` x1, (z `f` x1) `f` x2, ...] ---- -- Note that -- --
-- last (scanl f z xs) == foldl f z xs. --scanl :: (b -> a -> b) -> b -> [a] -> [b] -- | <math>. Return all the elements of a list except the last one. -- The list must be non-empty. init :: [a] -> [a] -- | <math>. Extract the last element of a list, which must be finite -- and non-empty. last :: [a] -> a -- | <math>. Extract the elements after the head of a list, which -- must be non-empty. tail :: [a] -> [a] -- | <math>. Extract the first element of a list, which must be -- non-empty. head :: [a] -> a -- | The maybe function takes a default value, a function, and a -- Maybe value. If the Maybe value is Nothing, the -- function returns the default value. Otherwise, it applies the function -- to the value inside the Just and returns the result. -- --
-- >>> maybe False odd (Just 3) -- True ---- --
-- >>> maybe False odd Nothing -- False ---- -- Read an integer from a string using readMaybe. If we succeed, -- return twice the integer; that is, apply (*2) to it. If -- instead we fail to parse an integer, return 0 by default: -- --
-- >>> import Text.Read ( readMaybe ) -- -- >>> maybe 0 (*2) (readMaybe "5") -- 10 -- -- >>> maybe 0 (*2) (readMaybe "") -- 0 ---- -- Apply show to a Maybe Int. If we have Just n, -- we want to show the underlying Int n. But if we have -- Nothing, we return the empty string instead of (for example) -- "Nothing": -- --
-- >>> maybe "" show (Just 5) -- "5" -- -- >>> maybe "" show Nothing -- "" --maybe :: b -> (a -> b) -> Maybe a -> b -- | An infix synonym for fmap. -- -- The name of this operator is an allusion to $. Note the -- similarities between their types: -- --
-- ($) :: (a -> b) -> a -> b -- (<$>) :: Functor f => (a -> b) -> f a -> f b ---- -- Whereas $ is function application, <$> is function -- application lifted over a Functor. -- --
-- >>> show <$> Nothing -- Nothing -- -- >>> show <$> Just 3 -- Just "3" ---- -- Convert from an Either Int Int to an -- Either Int String using show: -- --
-- >>> show <$> Left 17 -- Left 17 -- -- >>> show <$> Right 17 -- Right "17" ---- -- Double each element of a list: -- --
-- >>> (*2) <$> [1,2,3] -- [2,4,6] ---- -- Apply even to the second element of a pair: -- --
-- >>> even <$> (2,2) -- (2,True) --(<$>) :: Functor f => (a -> b) -> f a -> f b infixl 4 <$> -- | uncurry converts a curried function to a function on pairs. -- --
-- >>> uncurry (+) (1,2) -- 3 ---- --
-- >>> uncurry ($) (show, 1) -- "1" ---- --
-- >>> map (uncurry max) [(1,2), (3,4), (6,8)] -- [2,4,8] --uncurry :: (a -> b -> c) -> (a, b) -> c -- | curry converts an uncurried function to a curried function. -- --
-- >>> curry fst 1 2 -- 1 --curry :: ((a, b) -> c) -> a -> b -> c -- | asTypeOf is a type-restricted version of const. It is -- usually used as an infix operator, and its typing forces its first -- argument (which is usually overloaded) to have the same type as the -- second. asTypeOf :: a -> a -> a -- | until p f yields the result of applying f -- until p holds. until :: (a -> Bool) -> (a -> a) -> a -> a -- | Strict (call-by-value) application operator. It takes a function and -- an argument, evaluates the argument to weak head normal form (WHNF), -- then calls the function with that value. ($!) :: forall (r :: RuntimeRep) a (b :: TYPE r). (a -> b) -> a -> b infixr 0 $! -- | flip f takes its (first) two arguments in the reverse -- order of f. -- --
-- >>> flip (++) "hello" "world" -- "worldhello" --flip :: (a -> b -> c) -> b -> a -> c -- | Function composition. (.) :: (b -> c) -> (a -> b) -> a -> c infixr 9 . -- | const x is a unary function which evaluates to x for -- all inputs. -- --
-- >>> const 42 "hello" -- 42 ---- --
-- >>> map (const 42) [0..3] -- [42,42,42,42] --const :: a -> b -> a -- | Identity function. -- --
-- id x = x --id :: a -> a -- | Same as >>=, but with the arguments interchanged. (=<<) :: Monad m => (a -> m b) -> m a -> m b infixr 1 =<< -- | A String is a list of characters. String constants in Haskell -- are values of type String. -- -- See Data.List for operations on lists. type String = [Char] -- | A special case of error. It is expected that compilers will -- recognize this and insert error messages which are more appropriate to -- the context in which undefined appears. undefined :: forall (r :: RuntimeRep) (a :: TYPE r). HasCallStack => a -- | A variant of error that does not produce a stack trace. errorWithoutStackTrace :: forall (r :: RuntimeRep) (a :: TYPE r). [Char] -> a -- | error stops execution and displays an error message. error :: forall (r :: RuntimeRep) (a :: TYPE r). HasCallStack => [Char] -> a -- | Boolean "and", lazy in the second argument (&&) :: Bool -> Bool -> Bool infixr 3 && -- | Boolean "or", lazy in the second argument (||) :: Bool -> Bool -> Bool infixr 2 || -- | Boolean "not" not :: Bool -> Bool -- | Data structures that can be folded. -- -- For example, given a data type -- --
-- data Tree a = Empty | Leaf a | Node (Tree a) a (Tree a) ---- -- a suitable instance would be -- --
-- instance Foldable Tree where -- foldMap f Empty = mempty -- foldMap f (Leaf x) = f x -- foldMap f (Node l k r) = foldMap f l `mappend` f k `mappend` foldMap f r ---- -- This is suitable even for abstract types, as the monoid is assumed to -- satisfy the monoid laws. Alternatively, one could define -- foldr: -- --
-- instance Foldable Tree where -- foldr f z Empty = z -- foldr f z (Leaf x) = f x z -- foldr f z (Node l k r) = foldr f (f k (foldr f z r)) l ---- -- Foldable instances are expected to satisfy the following -- laws: -- --
-- foldr f z t = appEndo (foldMap (Endo . f) t ) z ---- --
-- foldl f z t = appEndo (getDual (foldMap (Dual . Endo . flip f) t)) z ---- --
-- fold = foldMap id ---- --
-- length = getSum . foldMap (Sum . const 1) ---- -- sum, product, maximum, and minimum -- should all be essentially equivalent to foldMap forms, such -- as -- --
-- sum = getSum . foldMap Sum ---- -- but may be less defined. -- -- If the type is also a Functor instance, it should satisfy -- --
-- foldMap f = fold . fmap f ---- -- which implies that -- --
-- foldMap f . fmap g = foldMap (f . g) --class Foldable (t :: Type -> Type) -- | Combine the elements of a structure using a monoid. fold :: (Foldable t, Monoid m) => t m -> m -- | Map each element of the structure to a monoid, and combine the -- results. foldMap :: (Foldable t, Monoid m) => (a -> m) -> t a -> m -- | A variant of foldMap that is strict in the accumulator. foldMap' :: (Foldable t, Monoid m) => (a -> m) -> t a -> m -- | Right-associative fold of a structure. -- -- In the case of lists, foldr, when applied to a binary operator, -- a starting value (typically the right-identity of the operator), and a -- list, reduces the list using the binary operator, from right to left: -- --
-- foldr f z [x1, x2, ..., xn] == x1 `f` (x2 `f` ... (xn `f` z)...) ---- -- Note that, since the head of the resulting expression is produced by -- an application of the operator to the first element of the list, -- foldr can produce a terminating expression from an infinite -- list. -- -- For a general Foldable structure this should be semantically -- identical to, -- --
-- foldr f z = foldr f z . toList --foldr :: Foldable t => (a -> b -> b) -> b -> t a -> b -- | Right-associative fold of a structure, but with strict application of -- the operator. foldr' :: Foldable t => (a -> b -> b) -> b -> t a -> b -- | Left-associative fold of a structure. -- -- In the case of lists, foldl, when applied to a binary operator, -- a starting value (typically the left-identity of the operator), and a -- list, reduces the list using the binary operator, from left to right: -- --
-- foldl f z [x1, x2, ..., xn] == (...((z `f` x1) `f` x2) `f`...) `f` xn ---- -- Note that to produce the outermost application of the operator the -- entire input list must be traversed. This means that foldl' -- will diverge if given an infinite list. -- -- Also note that if you want an efficient left-fold, you probably want -- to use foldl' instead of foldl. The reason for this is -- that latter does not force the "inner" results (e.g. z `f` x1 -- in the above example) before applying them to the operator (e.g. to -- (`f` x2)). This results in a thunk chain <math> -- elements long, which then must be evaluated from the outside-in. -- -- For a general Foldable structure this should be semantically -- identical to, -- --
-- foldl f z = foldl f z . toList --foldl :: Foldable t => (b -> a -> b) -> b -> t a -> b -- | Left-associative fold of a structure but with strict application of -- the operator. -- -- This ensures that each step of the fold is forced to weak head normal -- form before being applied, avoiding the collection of thunks that -- would otherwise occur. This is often what you want to strictly reduce -- a finite list to a single, monolithic result (e.g. length). -- -- For a general Foldable structure this should be semantically -- identical to, -- --
-- foldl' f z = foldl' f z . toList --foldl' :: Foldable t => (b -> a -> b) -> b -> t a -> b -- | A variant of foldr that has no base case, and thus may only be -- applied to non-empty structures. -- --
-- foldr1 f = foldr1 f . toList --foldr1 :: Foldable t => (a -> a -> a) -> t a -> a -- | A variant of foldl that has no base case, and thus may only be -- applied to non-empty structures. -- --
-- foldl1 f = foldl1 f . toList --foldl1 :: Foldable t => (a -> a -> a) -> t a -> a -- | List of elements of a structure, from left to right. toList :: Foldable t => t a -> [a] -- | Test whether the structure is empty. The default implementation is -- optimized for structures that are similar to cons-lists, because there -- is no general way to do better. null :: Foldable t => t a -> Bool -- | Returns the size/length of a finite structure as an Int. The -- default implementation is optimized for structures that are similar to -- cons-lists, because there is no general way to do better. length :: Foldable t => t a -> Int -- | Does the element occur in the structure? elem :: (Foldable t, Eq a) => a -> t a -> Bool -- | The largest element of a non-empty structure. maximum :: (Foldable t, Ord a) => t a -> a -- | The least element of a non-empty structure. minimum :: (Foldable t, Ord a) => t a -> a infix 4 `elem` -- | The find function takes a predicate and a structure and returns -- the leftmost element of the structure matching the predicate, or -- Nothing if there is no such element. find :: Foldable t => (a -> Bool) -> t a -> Maybe a -- | notElem is the negation of elem. notElem :: (Foldable t, Eq a) => a -> t a -> Bool infix 4 `notElem` -- | The least element of a non-empty structure with respect to the given -- comparison function. minimumBy :: Foldable t => (a -> a -> Ordering) -> t a -> a -- | The largest element of a non-empty structure with respect to the given -- comparison function. maximumBy :: Foldable t => (a -> a -> Ordering) -> t a -> a -- | Determines whether all elements of the structure satisfy the -- predicate. all :: Foldable t => (a -> Bool) -> t a -> Bool -- | Determines whether any element of the structure satisfies the -- predicate. any :: Foldable t => (a -> Bool) -> t a -> Bool -- | or returns the disjunction of a container of Bools. For the -- result to be False, the container must be finite; True, -- however, results from a True value finitely far from the left -- end. or :: Foldable t => t Bool -> Bool -- | and returns the conjunction of a container of Bools. For the -- result to be True, the container must be finite; False, -- however, results from a False value finitely far from the left -- end. and :: Foldable t => t Bool -> Bool -- | Map a function over all the elements of a container and concatenate -- the resulting lists. concatMap :: Foldable t => (a -> [b]) -> t a -> [b] -- | The concatenation of all the elements of a container of lists. concat :: Foldable t => t [a] -> [a] -- | The sum of a collection of actions, generalizing concat. As of -- base 4.8.0.0, msum is just asum, specialized to -- MonadPlus. msum :: (Foldable t, MonadPlus m) => t (m a) -> m a -- | The sum of a collection of actions, generalizing concat. -- --
-- >>> asum [Just "Hello", Nothing, Just "World"] -- Just "Hello" --asum :: (Foldable t, Alternative f) => t (f a) -> f a -- | Evaluate each monadic action in the structure from left to right, and -- ignore the results. For a version that doesn't ignore the results see -- sequence. -- -- As of base 4.8.0.0, sequence_ is just sequenceA_, -- specialized to Monad. sequence_ :: (Foldable t, Monad m) => t (m a) -> m () -- | Evaluate each action in the structure from left to right, and ignore -- the results. For a version that doesn't ignore the results see -- sequenceA. sequenceA_ :: (Foldable t, Applicative f) => t (f a) -> f () -- | forM_ is mapM_ with its arguments flipped. For a version -- that doesn't ignore the results see forM. -- -- As of base 4.8.0.0, forM_ is just for_, specialized to -- Monad. forM_ :: (Foldable t, Monad m) => t a -> (a -> m b) -> m () -- | Map each element of a structure to a monadic action, evaluate these -- actions from left to right, and ignore the results. For a version that -- doesn't ignore the results see mapM. -- -- As of base 4.8.0.0, mapM_ is just traverse_, specialized -- to Monad. mapM_ :: (Foldable t, Monad m) => (a -> m b) -> t a -> m () -- | for_ is traverse_ with its arguments flipped. For a -- version that doesn't ignore the results see for. -- --
-- >>> for_ [1..4] print -- 1 -- 2 -- 3 -- 4 --for_ :: (Foldable t, Applicative f) => t a -> (a -> f b) -> f () -- | Map each element of a structure to an action, evaluate these actions -- from left to right, and ignore the results. For a version that doesn't -- ignore the results see traverse. traverse_ :: (Foldable t, Applicative f) => (a -> f b) -> t a -> f () -- | Monadic fold over the elements of a structure, associating to the -- left, i.e. from left to right. foldlM :: (Foldable t, Monad m) => (b -> a -> m b) -> b -> t a -> m b -- | Monadic fold over the elements of a structure, associating to the -- right, i.e. from right to left. foldrM :: (Foldable t, Monad m) => (a -> b -> m b) -> b -> t a -> m b