-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Staircase functions or piecewise constant functions -- -- Step functions, staircase functions or piecewise constant functions. -- Implemented as a default value and a series of transitions. Supports -- merging two step functions using a supplied merging function. @package step-function @version 0.2 module Data.Function.Step -- | Step function. Piecewise constant function, having finitely many -- pieces. See https://en.wikipedia.org/wiki/Step_function. -- -- SF (fromList [(Open k1, v1), (Closed k2, -- v2)]) v3 :: SF k v describes a piecewise constant function -- <math>: -- -- <math> -- -- or as you would write in Haskell -- --
-- f x | x < k1 = v1 -- | x <= k2 = v2 -- | otherwise = v3 ---- -- Note: total-map package, which provides function with -- finite support. -- -- Constructor is exposed as you cannot construct non-valid SF. -- --
-- >>> putSF $ liftA2 (+) (step 0 0 1) (step 1 0 1) -- \x -> if -- | x < 0 -> 0 -- | x < 1 -> 1 -- | otherwise -> 2 ---- -- Following property holds, i.e. SF and ordinary function -- Applicative instances are compatible (and ! is a -- homomorphism). -- --
-- liftA2 f g h ! x == liftA2 f (g !) (h !) x ---- -- Recall that for ordinary functions liftA2 f g h x = f (g x) -- (h x). -- --
-- >>> let s = fromList [(Open 0, -1),(Closed 0, 0)] 1 :: SF Rational Int -- -- >>> putSF s -- \x -> if -- | x < 0 % 1 -> -1 -- | x <= 0 % 1 -> 0 -- | otherwise -> 1 ---- --
-- >>> import Data.Ratio ((%)) -- -- >>> map (s !) [-1, -0.5, 0, 0.5, 1] -- [-1,-1,0,1,1] --data SF k v SF :: !(Map (Bound k) v) -> !v -> SF k v -- | Bound operations data Bound k -- | less-than, < Open :: k -> Bound k -- | less-than-or-equal, ≤. Closed :: k -> Bound k -- | Constant function -- --
-- >>> putSF $ constant 1 -- \_ -> 1 --constant :: a -> SF k a -- | Step function. -- -- step k v1 v2 = \ x -> if x < k then v1 else v2. -- --
-- >>> putSF $ step 1 2 3 -- \x -> if -- | x < 1 -> 2 -- | otherwise -> 3 --step :: k -> v -> v -> SF k v -- | Create function from list of cases and default value. -- --
-- >>> let f = fromList [(Open 1,2),(Closed 3,4),(Open 4,5)] 6 -- -- >>> putSF f -- \x -> if -- | x < 1 -> 2 -- | x <= 3 -> 4 -- | x < 4 -> 5 -- | otherwise -> 6 ---- --
-- >>> map (f !) [0..10] -- [2,4,4,4,6,6,6,6,6,6,6] --fromList :: Ord k => [(Bound k, v)] -> v -> SF k v -- | Merge adjustent pieces with same values. -- -- Note: SF isn't normalised on construction. Values don't -- necessarily are Eq. -- --
-- >>> putSF $ normalise heaviside -- \x -> if -- | x < 0 -> -1 -- | otherwise -> 1 ---- --
-- >>> putSF $ normalise $ step 0 1 1 -- \_ -> 1 ---- --
-- normalise (liftA2 (+) p (fmap negate p)) == (pure 0 :: SF Int Int) --normalise :: Eq v => SF k v -> SF k v -- | Apply SF. -- --
-- >>> heaviside ! 2 -- 1 --(!) :: Ord k => SF k v -> k -> v infixl 9 ! -- | Possible values of SF -- --
-- >>> values heaviside -- [-1,1] --values :: SF k v -> [v] -- | Show SF as Haskell code showSF :: (Show a, Show b) => SF a b -> String -- |
-- putStrLn . showSF --putSF :: (Show a, Show b) => SF a b -> IO () instance Data.Traversable.Traversable (Data.Function.Step.SF k) instance Data.Foldable.Foldable (Data.Function.Step.SF k) instance GHC.Base.Functor (Data.Function.Step.SF k) instance (GHC.Classes.Ord v, GHC.Classes.Ord k) => GHC.Classes.Ord (Data.Function.Step.SF k v) instance (GHC.Classes.Eq v, GHC.Classes.Eq k) => GHC.Classes.Eq (Data.Function.Step.SF k v) instance Data.Traversable.Traversable Data.Function.Step.Bound instance Data.Foldable.Foldable Data.Function.Step.Bound instance GHC.Base.Functor Data.Function.Step.Bound instance GHC.Show.Show k => GHC.Show.Show (Data.Function.Step.Bound k) instance GHC.Classes.Eq k => GHC.Classes.Eq (Data.Function.Step.Bound k) instance GHC.Classes.Ord k => GHC.Base.Applicative (Data.Function.Step.SF k) instance GHC.Classes.Ord k => GHC.Base.Monad (Data.Function.Step.SF k) instance (GHC.Classes.Ord k, Data.Semigroup.Semigroup v) => Data.Semigroup.Semigroup (Data.Function.Step.SF k v) instance (GHC.Classes.Ord k, GHC.Base.Monoid v) => GHC.Base.Monoid (Data.Function.Step.SF k v) instance (GHC.Classes.Ord k, Test.QuickCheck.Arbitrary.Arbitrary k, Test.QuickCheck.Arbitrary.Arbitrary v) => Test.QuickCheck.Arbitrary.Arbitrary (Data.Function.Step.SF k v) instance (Control.DeepSeq.NFData k, Control.DeepSeq.NFData v) => Control.DeepSeq.NFData (Data.Function.Step.SF k v) instance Data.Functor.Classes.Show2 Data.Function.Step.SF instance GHC.Show.Show k => Data.Functor.Classes.Show1 (Data.Function.Step.SF k) instance (GHC.Show.Show k, GHC.Show.Show v) => GHC.Show.Show (Data.Function.Step.SF k v) instance GHC.Classes.Ord k => GHC.Classes.Ord (Data.Function.Step.Bound k) instance Test.QuickCheck.Arbitrary.Arbitrary k => Test.QuickCheck.Arbitrary.Arbitrary (Data.Function.Step.Bound k) instance Control.DeepSeq.NFData k => Control.DeepSeq.NFData (Data.Function.Step.Bound k) instance Data.Functor.Classes.Show1 Data.Function.Step.Bound module Data.Function.Step.Discrete.Closed -- | Step function. Piecewise constant function, having finitely many -- pieces. See https://en.wikipedia.org/wiki/Step_function. -- -- Note: this variant has discrete domain. It's enough to have -- only <$, without ≤, as there is a next -- element without any others in between. -- -- SF (fromList [(k1, v1), (k2, v2)]) v3 :: SF k v -- describes a piecewise constant function <math>: -- -- <math> -- -- or as you would write in Haskell -- --
-- f x | x <= k1 = v1 -- | x <= k2 = v2 -- | otherwise = v3 ---- -- Constructor is exposed as you cannot construct non-valid SF. data SF k v SF :: !(Map k v) -> !v -> SF k v -- | Constant function -- --
-- >>> putSF $ constant 1 -- \_ -> 1 --constant :: a -> SF k a -- | Step function. -- -- step k v1 v2 = \ x -> if x < k then v1 else v2. -- --
-- >>> putSF $ step 1 2 3 -- \x -> if -- | x <= 1 -> 2 -- | otherwise -> 3 --step :: k -> v -> v -> SF k v -- | Create function from list of cases and default value. -- --
-- >>> let f = fromList [(1,2),(3,4)] 5 -- -- >>> putSF f -- \x -> if -- | x <= 1 -> 2 -- | x <= 3 -> 4 -- | otherwise -> 5 ---- --
-- >>> map (f !) [0..10] -- [2,2,4,4,5,5,5,5,5,5,5] --fromList :: Ord k => [(k, v)] -> v -> SF k v -- | Merge adjustent pieces with same values. -- -- Note: SF isn't normalised on construction. Values don't -- necessarily are Eq. -- --
-- >>> putSF $ normalise heaviside -- \x -> if -- | x <= 0 -> -1 -- | otherwise -> 1 ---- --
-- >>> putSF $ normalise $ step 0 1 1 -- \_ -> 1 ---- --
-- normalise (liftA2 (+) p (fmap negate p)) == (pure 0 :: SF Int Int) --normalise :: Eq v => SF k v -> SF k v -- | Apply SF. -- --
-- >>> heaviside ! 2 -- 1 --(!) :: Ord k => SF k v -> k -> v infixl 9 ! -- | Possible values of SF -- --
-- >>> values heaviside -- [-1,1] --values :: SF k v -> [v] -- | Convert from discrete variant to more "dense" -- --
-- >>> SF.putSF $ toDense $ fromList [(1,2),(3,4)] 5 -- \x -> if -- | x <= 1 -> 2 -- | x <= 3 -> 4 -- | otherwise -> 5 --toDense :: SF a b -> SF a b -- | Convert from "dense" variant. <= k pieces will be -- converted to < succ k. There might be less pieces -- in the ressult SF, than in the original. -- --
-- >>> let f = SF.fromList [(SF.Open 1,2),(SF.Closed 3,4),(SF.Open 4,5)] 6 -- -- >>> SF.putSF f -- \x -> if -- | x < 1 -> 2 -- | x <= 3 -> 4 -- | x < 4 -> 5 -- | otherwise -> 6 ---- --
-- >>> putSF $ fromDense (Just . pred) f -- \x -> if -- | x <= 0 -> 2 -- | x <= 3 -> 4 -- | otherwise -> 6 --fromDense :: Ord a => (a -> Maybe a) -> SF a b -> SF a b -- | Show SF as Haskell code showSF :: (Show a, Show b) => SF a b -> String -- |
-- putStrLn . showSF --putSF :: (Show a, Show b) => SF a b -> IO () instance Data.Traversable.Traversable (Data.Function.Step.Discrete.Closed.SF k) instance Data.Foldable.Foldable (Data.Function.Step.Discrete.Closed.SF k) instance GHC.Base.Functor (Data.Function.Step.Discrete.Closed.SF k) instance (GHC.Classes.Ord v, GHC.Classes.Ord k) => GHC.Classes.Ord (Data.Function.Step.Discrete.Closed.SF k v) instance (GHC.Classes.Eq v, GHC.Classes.Eq k) => GHC.Classes.Eq (Data.Function.Step.Discrete.Closed.SF k v) instance GHC.Classes.Ord k => GHC.Base.Applicative (Data.Function.Step.Discrete.Closed.SF k) instance GHC.Classes.Ord k => GHC.Base.Monad (Data.Function.Step.Discrete.Closed.SF k) instance (GHC.Classes.Ord k, Data.Semigroup.Semigroup v) => Data.Semigroup.Semigroup (Data.Function.Step.Discrete.Closed.SF k v) instance (GHC.Classes.Ord k, GHC.Base.Monoid v) => GHC.Base.Monoid (Data.Function.Step.Discrete.Closed.SF k v) instance (GHC.Classes.Ord k, Test.QuickCheck.Arbitrary.Arbitrary k, Test.QuickCheck.Arbitrary.Arbitrary v) => Test.QuickCheck.Arbitrary.Arbitrary (Data.Function.Step.Discrete.Closed.SF k v) instance (Control.DeepSeq.NFData k, Control.DeepSeq.NFData v) => Control.DeepSeq.NFData (Data.Function.Step.Discrete.Closed.SF k v) instance Data.Functor.Classes.Show2 Data.Function.Step.Discrete.Closed.SF instance GHC.Show.Show k => Data.Functor.Classes.Show1 (Data.Function.Step.Discrete.Closed.SF k) instance (GHC.Show.Show k, GHC.Show.Show v) => GHC.Show.Show (Data.Function.Step.Discrete.Closed.SF k v) module Data.Function.Step.Discrete.Open -- | Step function. Piecewise constant function, having finitely many -- pieces. See https://en.wikipedia.org/wiki/Step_function. -- -- Note: this variant has discrete domain. It's enough to have -- only <$, without ≤, as there is a next -- element without any others in between. -- -- SF (fromList [(k1, v1), (k2, v2)]) v3 :: SF k v -- describes a piecewise constant function <math>: -- -- <math> -- -- or as you would write in Haskell -- --
-- f x | x < k1 = v1 -- | x < k2 = v2 -- | otherwise = v3 ---- -- Constructor is exposed as you cannot construct non-valid SF. data SF k v SF :: !(Map k v) -> !v -> SF k v -- | Constant function -- --
-- >>> putSF $ constant 1 -- \_ -> 1 --constant :: a -> SF k a -- | Step function. -- -- step k v1 v2 = \ x -> if x < k then v1 else v2. -- --
-- >>> putSF $ step 1 2 3 -- \x -> if -- | x < 1 -> 2 -- | otherwise -> 3 --step :: k -> v -> v -> SF k v -- | Create function from list of cases and default value. -- --
-- >>> putSF $ fromList [(1,2),(3,4)] 5 -- \x -> if -- | x < 1 -> 2 -- | x < 3 -> 4 -- | otherwise -> 5 ---- --
-- >>> map (fromList [(1,2),(3,4)] 5 !) [0..10] -- [2,4,4,5,5,5,5,5,5,5,5] --fromList :: Ord k => [(k, v)] -> v -> SF k v -- | Merge adjustent pieces with same values. -- -- Note: SF isn't normalised on construction. Values don't -- necessarily are Eq. -- --
-- >>> putSF $ normalise heaviside -- \x -> if -- | x < 0 -> -1 -- | otherwise -> 1 ---- --
-- >>> putSF $ normalise $ step 0 1 1 -- \_ -> 1 ---- --
-- normalise (liftA2 (+) p (fmap negate p)) == (pure 0 :: SF Int Int) --normalise :: Eq v => SF k v -> SF k v -- | Apply SF. -- --
-- >>> heaviside ! 2 -- 1 --(!) :: Ord k => SF k v -> k -> v infixl 9 ! -- | Possible values of SF -- --
-- >>> values heaviside -- [-1,1] --values :: SF k v -> [v] -- | Convert from discrete variant to more "dense" -- --
-- >>> SF.putSF $ toDense $ fromList [(1,2),(3,4)] 5 -- \x -> if -- | x < 1 -> 2 -- | x < 3 -> 4 -- | otherwise -> 5 --toDense :: SF a b -> SF a b -- | Convert from "dense" variant. <= k pieces will be -- converted to < succ k. There might be less pieces -- in the ressult SF, than in the original. -- --
-- >>> let f = SF.fromList [(SF.Open 1,2),(SF.Closed 3,4),(SF.Open 4,5)] 6 -- -- >>> SF.putSF f -- \x -> if -- | x < 1 -> 2 -- | x <= 3 -> 4 -- | x < 4 -> 5 -- | otherwise -> 6 ---- --
-- >>> putSF $ fromDense (Just . succ) f -- \x -> if -- | x < 1 -> 2 -- | x < 4 -> 4 -- | otherwise -> 6 --fromDense :: Ord a => (a -> Maybe a) -> SF a b -> SF a b -- | Show SF as Haskell code showSF :: (Show a, Show b) => SF a b -> String -- |
-- putStrLn . showSF --putSF :: (Show a, Show b) => SF a b -> IO () instance Data.Traversable.Traversable (Data.Function.Step.Discrete.Open.SF k) instance Data.Foldable.Foldable (Data.Function.Step.Discrete.Open.SF k) instance GHC.Base.Functor (Data.Function.Step.Discrete.Open.SF k) instance (GHC.Classes.Ord v, GHC.Classes.Ord k) => GHC.Classes.Ord (Data.Function.Step.Discrete.Open.SF k v) instance (GHC.Classes.Eq v, GHC.Classes.Eq k) => GHC.Classes.Eq (Data.Function.Step.Discrete.Open.SF k v) instance GHC.Classes.Ord k => GHC.Base.Applicative (Data.Function.Step.Discrete.Open.SF k) instance GHC.Classes.Ord k => GHC.Base.Monad (Data.Function.Step.Discrete.Open.SF k) instance (GHC.Classes.Ord k, Data.Semigroup.Semigroup v) => Data.Semigroup.Semigroup (Data.Function.Step.Discrete.Open.SF k v) instance (GHC.Classes.Ord k, GHC.Base.Monoid v) => GHC.Base.Monoid (Data.Function.Step.Discrete.Open.SF k v) instance (GHC.Classes.Ord k, Test.QuickCheck.Arbitrary.Arbitrary k, Test.QuickCheck.Arbitrary.Arbitrary v) => Test.QuickCheck.Arbitrary.Arbitrary (Data.Function.Step.Discrete.Open.SF k v) instance (Control.DeepSeq.NFData k, Control.DeepSeq.NFData v) => Control.DeepSeq.NFData (Data.Function.Step.Discrete.Open.SF k v) instance Data.Functor.Classes.Show2 Data.Function.Step.Discrete.Open.SF instance GHC.Show.Show k => Data.Functor.Classes.Show1 (Data.Function.Step.Discrete.Open.SF k) instance (GHC.Show.Show k, GHC.Show.Show v) => GHC.Show.Show (Data.Function.Step.Discrete.Open.SF k v) -- | This module re-exports Function.Step.Discrete.Open module Data.Function.Step.Discrete