{-# OPTIONS_GHC -fno-warn-orphans #-}
-- |
-- Copyright  : (c) Ivan Perez and Manuel Baerenz, 2016
-- License    : BSD3
-- Maintainer : ivan.perez@keera.co.uk
--
-- Number instances for 'MSF's that produce numbers. This allows you to use
-- numeric operators with 'MSF's that output numbers, for example, you can
-- write:
--
-- @
-- msf1 :: MSF Input Double -- defined however you want
-- msf2 :: MSF Input Double -- defined however you want
-- msf3 :: MSF Input Double
-- msf3 = msf1 + msf2
-- @
--
-- instead of
--
-- @
-- msf3 = (msf1 &&& msf2) >>> arr (uncurry (+))
-- @
--
-- Instances are provided for the type classes 'Num', 'Fractional' and
-- 'Floating'.
module Data.MonadicStreamFunction.Instances.Num where

-- Internal imports
import Control.Arrow.Util              (constantly, elementwise, elementwise2)
import Data.MonadicStreamFunction.Core (MSF)

-- | 'Num' instance for 'MSF's.
instance (Monad m, Num b) => Num (MSF m a b) where
  + :: MSF m a b -> MSF m a b -> MSF m a b
(+)         = (b -> b -> b) -> MSF m a b -> MSF m a b -> MSF m a b
forall (a :: * -> * -> *) c d e b.
Arrow a =>
(c -> d -> e) -> a b c -> a b d -> a b e
elementwise2 b -> b -> b
forall a. Num a => a -> a -> a
(+)
  (-)         = (b -> b -> b) -> MSF m a b -> MSF m a b -> MSF m a b
forall (a :: * -> * -> *) c d e b.
Arrow a =>
(c -> d -> e) -> a b c -> a b d -> a b e
elementwise2 (-)
  * :: MSF m a b -> MSF m a b -> MSF m a b
(*)         = (b -> b -> b) -> MSF m a b -> MSF m a b -> MSF m a b
forall (a :: * -> * -> *) c d e b.
Arrow a =>
(c -> d -> e) -> a b c -> a b d -> a b e
elementwise2 b -> b -> b
forall a. Num a => a -> a -> a
(*)
  abs :: MSF m a b -> MSF m a b
abs         = (b -> b) -> MSF m a b -> MSF m a b
forall (a :: * -> * -> *) c d b.
Arrow a =>
(c -> d) -> a b c -> a b d
elementwise b -> b
forall a. Num a => a -> a
abs
  signum :: MSF m a b -> MSF m a b
signum      = (b -> b) -> MSF m a b -> MSF m a b
forall (a :: * -> * -> *) c d b.
Arrow a =>
(c -> d) -> a b c -> a b d
elementwise b -> b
forall a. Num a => a -> a
signum
  negate :: MSF m a b -> MSF m a b
negate      = (b -> b) -> MSF m a b -> MSF m a b
forall (a :: * -> * -> *) c d b.
Arrow a =>
(c -> d) -> a b c -> a b d
elementwise b -> b
forall a. Num a => a -> a
negate
  fromInteger :: Integer -> MSF m a b
fromInteger = b -> MSF m a b
forall (a :: * -> * -> *) b c. Arrow a => b -> a c b
constantly (b -> MSF m a b) -> (Integer -> b) -> Integer -> MSF m a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> b
forall a. Num a => Integer -> a
fromInteger

-- | 'Fractional' instance for 'MSF's.
instance (Monad m, Fractional b) => Fractional (MSF m a b) where
  fromRational :: Rational -> MSF m a b
fromRational = b -> MSF m a b
forall (a :: * -> * -> *) b c. Arrow a => b -> a c b
constantly (b -> MSF m a b) -> (Rational -> b) -> Rational -> MSF m a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rational -> b
forall a. Fractional a => Rational -> a
fromRational
  / :: MSF m a b -> MSF m a b -> MSF m a b
(/)          = (b -> b -> b) -> MSF m a b -> MSF m a b -> MSF m a b
forall (a :: * -> * -> *) c d e b.
Arrow a =>
(c -> d -> e) -> a b c -> a b d -> a b e
elementwise2 b -> b -> b
forall a. Fractional a => a -> a -> a
(/)
  recip :: MSF m a b -> MSF m a b
recip        = (b -> b) -> MSF m a b -> MSF m a b
forall (a :: * -> * -> *) c d b.
Arrow a =>
(c -> d) -> a b c -> a b d
elementwise b -> b
forall a. Fractional a => a -> a
recip

-- | 'Floating' instance for 'MSF's.
instance (Monad m, Floating b) => Floating (MSF m a b) where
  pi :: MSF m a b
pi      = b -> MSF m a b
forall (a :: * -> * -> *) b c. Arrow a => b -> a c b
constantly   b
forall a. Floating a => a
pi
  exp :: MSF m a b -> MSF m a b
exp     = (b -> b) -> MSF m a b -> MSF m a b
forall (a :: * -> * -> *) c d b.
Arrow a =>
(c -> d) -> a b c -> a b d
elementwise  b -> b
forall a. Floating a => a -> a
exp
  log :: MSF m a b -> MSF m a b
log     = (b -> b) -> MSF m a b -> MSF m a b
forall (a :: * -> * -> *) c d b.
Arrow a =>
(c -> d) -> a b c -> a b d
elementwise  b -> b
forall a. Floating a => a -> a
log
  sqrt :: MSF m a b -> MSF m a b
sqrt    = (b -> b) -> MSF m a b -> MSF m a b
forall (a :: * -> * -> *) c d b.
Arrow a =>
(c -> d) -> a b c -> a b d
elementwise  b -> b
forall a. Floating a => a -> a
sqrt
  ** :: MSF m a b -> MSF m a b -> MSF m a b
(**)    = (b -> b -> b) -> MSF m a b -> MSF m a b -> MSF m a b
forall (a :: * -> * -> *) c d e b.
Arrow a =>
(c -> d -> e) -> a b c -> a b d -> a b e
elementwise2 b -> b -> b
forall a. Floating a => a -> a -> a
(**)
  logBase :: MSF m a b -> MSF m a b -> MSF m a b
logBase = (b -> b -> b) -> MSF m a b -> MSF m a b -> MSF m a b
forall (a :: * -> * -> *) c d e b.
Arrow a =>
(c -> d -> e) -> a b c -> a b d -> a b e
elementwise2 b -> b -> b
forall a. Floating a => a -> a -> a
logBase
  sin :: MSF m a b -> MSF m a b
sin     = (b -> b) -> MSF m a b -> MSF m a b
forall (a :: * -> * -> *) c d b.
Arrow a =>
(c -> d) -> a b c -> a b d
elementwise  b -> b
forall a. Floating a => a -> a
sin
  cos :: MSF m a b -> MSF m a b
cos     = (b -> b) -> MSF m a b -> MSF m a b
forall (a :: * -> * -> *) c d b.
Arrow a =>
(c -> d) -> a b c -> a b d
elementwise  b -> b
forall a. Floating a => a -> a
cos
  tan :: MSF m a b -> MSF m a b
tan     = (b -> b) -> MSF m a b -> MSF m a b
forall (a :: * -> * -> *) c d b.
Arrow a =>
(c -> d) -> a b c -> a b d
elementwise  b -> b
forall a. Floating a => a -> a
tan
  asin :: MSF m a b -> MSF m a b
asin    = (b -> b) -> MSF m a b -> MSF m a b
forall (a :: * -> * -> *) c d b.
Arrow a =>
(c -> d) -> a b c -> a b d
elementwise  b -> b
forall a. Floating a => a -> a
asin
  acos :: MSF m a b -> MSF m a b
acos    = (b -> b) -> MSF m a b -> MSF m a b
forall (a :: * -> * -> *) c d b.
Arrow a =>
(c -> d) -> a b c -> a b d
elementwise  b -> b
forall a. Floating a => a -> a
acos
  atan :: MSF m a b -> MSF m a b
atan    = (b -> b) -> MSF m a b -> MSF m a b
forall (a :: * -> * -> *) c d b.
Arrow a =>
(c -> d) -> a b c -> a b d
elementwise  b -> b
forall a. Floating a => a -> a
atan
  sinh :: MSF m a b -> MSF m a b
sinh    = (b -> b) -> MSF m a b -> MSF m a b
forall (a :: * -> * -> *) c d b.
Arrow a =>
(c -> d) -> a b c -> a b d
elementwise  b -> b
forall a. Floating a => a -> a
sinh
  cosh :: MSF m a b -> MSF m a b
cosh    = (b -> b) -> MSF m a b -> MSF m a b
forall (a :: * -> * -> *) c d b.
Arrow a =>
(c -> d) -> a b c -> a b d
elementwise  b -> b
forall a. Floating a => a -> a
cosh
  tanh :: MSF m a b -> MSF m a b
tanh    = (b -> b) -> MSF m a b -> MSF m a b
forall (a :: * -> * -> *) c d b.
Arrow a =>
(c -> d) -> a b c -> a b d
elementwise  b -> b
forall a. Floating a => a -> a
tanh
  asinh :: MSF m a b -> MSF m a b
asinh   = (b -> b) -> MSF m a b -> MSF m a b
forall (a :: * -> * -> *) c d b.
Arrow a =>
(c -> d) -> a b c -> a b d
elementwise  b -> b
forall a. Floating a => a -> a
asinh
  acosh :: MSF m a b -> MSF m a b
acosh   = (b -> b) -> MSF m a b -> MSF m a b
forall (a :: * -> * -> *) c d b.
Arrow a =>
(c -> d) -> a b c -> a b d
elementwise  b -> b
forall a. Floating a => a -> a
acosh
  atanh :: MSF m a b -> MSF m a b
atanh   = (b -> b) -> MSF m a b -> MSF m a b
forall (a :: * -> * -> *) c d b.
Arrow a =>
(c -> d) -> a b c -> a b d
elementwise  b -> b
forall a. Floating a => a -> a
atanh