diff --git a/reactive-banana/CHANGELOG.md b/reactive-banana/CHANGELOG.md index a37c63cf..39c7c16e 100644 --- a/reactive-banana/CHANGELOG.md +++ b/reactive-banana/CHANGELOG.md @@ -1,6 +1,12 @@ Changelog for the `reactive-banana** package ------------------------------------------- +**unreleased** + +* Add `Num`, `Floating`, `Fractional`, and `IsString` instances for `Behavior`. [#34][] + + [#34]: https://github.com/HeinrichApfelmus/reactive-banana/pull/34 + **version 1.2.0.0** * Make `MonadFix` superclass of `MonadMoment`. [#128][] diff --git a/reactive-banana/src/Reactive/Banana/Combinators.hs b/reactive-banana/src/Reactive/Banana/Combinators.hs index 82f2bc52..e6caf17c 100644 --- a/reactive-banana/src/Reactive/Banana/Combinators.hs +++ b/reactive-banana/src/Reactive/Banana/Combinators.hs @@ -276,20 +276,6 @@ switchB b = liftMoment . M . fmap B . Prim.switchB (unB b) . Prim.mapE (unB) . u {----------------------------------------------------------------------------- Derived Combinators ------------------------------------------------------------------------------} -{- - -Unfortunately, we can't make a Num instance because that would -require Eq and Show . - -instance Num a => Num (Behavior t a) where - (+) = liftA2 (+) - (-) = liftA2 (-) - (*) = liftA2 (*) - negate = fmap negate - abs = fmap abs - signum = fmap signum - fromInteger = pure . fromInteger --} infixl 4 <@>, <@ -- | Infix synonym for the 'apply' combinator. Similar to '<*>'. diff --git a/reactive-banana/src/Reactive/Banana/Types.hs b/reactive-banana/src/Reactive/Banana/Types.hs index 5a6ef5f1..b02f6e44 100644 --- a/reactive-banana/src/Reactive/Banana/Types.hs +++ b/reactive-banana/src/Reactive/Banana/Types.hs @@ -13,6 +13,7 @@ import Control.Applicative import Control.Monad import Control.Monad.IO.Class import Control.Monad.Fix +import Data.String (IsString(..)) import qualified Reactive.Banana.Internal.Combinators as Prim @@ -93,6 +94,40 @@ instance Applicative Behavior where instance Functor Behavior where fmap = liftA +instance Num a => Num (Behavior a) where + (+) = liftA2 (+) + (-) = liftA2 (-) + (*) = liftA2 (*) + abs = fmap abs + signum = fmap signum + fromInteger = pure . fromInteger + negate = fmap negate + +instance Fractional a => Fractional (Behavior a) where + (/) = liftA2 (/) + fromRational = pure . fromRational + recip = fmap recip + +instance Floating a => Floating (Behavior a) where + (**) = liftA2 (**) + acos = fmap acos + acosh = fmap acosh + asin = fmap asin + asinh = fmap asinh + atan = fmap atan + atanh = fmap atanh + cos = fmap cos + cosh = fmap cosh + exp = fmap exp + log = fmap log + logBase = liftA2 logBase + pi = pure pi + sin = fmap sin + sinh = fmap sinh + sqrt = fmap sqrt + +instance IsString a => IsString (Behavior a) where + fromString = pure . fromString -- | The 'Future' monad is just a helper type for the 'changes' function. --