Skip to content

Commit

Permalink
Merge pull request #174 from mitchellwrosen/behavior-instances
Browse files Browse the repository at this point in the history
Add some missing Behavior instances (fix #34).

PVP requires only minor version bump.
  • Loading branch information
HeinrichApfelmus committed May 31, 2018
2 parents a60e40b + d7d3e6f commit ee9d5a3
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 14 deletions.
6 changes: 6 additions & 0 deletions 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][]
Expand Down
14 changes: 0 additions & 14 deletions reactive-banana/src/Reactive/Banana/Combinators.hs
Expand Up @@ -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 '<*>'.
Expand Down
35 changes: 35 additions & 0 deletions reactive-banana/src/Reactive/Banana/Types.hs
Expand Up @@ -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

Expand Down Expand Up @@ -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.
--
Expand Down

0 comments on commit ee9d5a3

Please sign in to comment.