-- |
-- Module      :  FRP.Yampa.Random
-- Copyright   :  (c) Ivan Perez, 2014-2022
--                (c) George Giorgidze, 2007-2012
--                (c) Henrik Nilsson, 2005-2006
--                (c) Antony Courtney and Henrik Nilsson, Yale University, 2003-2004
-- License     :  BSD-style (see the LICENSE file in the distribution)
--
-- Maintainer  :  ivan.perez@keera.co.uk
-- Stability   :  provisional
-- Portability :  non-portable (GHC extensions)
--
-- Signals and signal functions with noise and randomness.
--
-- The Random number generators are re-exported from "System.Random".
module FRP.Yampa.Random
    (
      -- * Random number generators
      RandomGen(..)
    , Random(..)

      -- * Noise, random signals, and stochastic event sources
    , noise
    , noiseR
    , occasionally
    )
  where

import System.Random (Random (..), RandomGen (..))

import FRP.Yampa.Diagnostics
import FRP.Yampa.Event
import FRP.Yampa.InternalCore (SF (..), SF' (..), Time)

-- * Noise (i.e. random signal generators) and stochastic processes

-- | Noise (random signal) with default range for type in question;
-- based on "randoms".
noise :: (RandomGen g, Random b) => g -> SF a b
noise :: forall g b a. (RandomGen g, Random b) => g -> SF a b
noise g
g0 = forall b a. [b] -> SF a b
streamToSF (forall a g. (Random a, RandomGen g) => g -> [a]
randoms g
g0)

-- | Noise (random signal) with specified range; based on "randomRs".
noiseR :: (RandomGen g, Random b) => (b,b) -> g -> SF a b
noiseR :: forall g b a. (RandomGen g, Random b) => (b, b) -> g -> SF a b
noiseR (b, b)
range g
g0 = forall b a. [b] -> SF a b
streamToSF (forall a g. (Random a, RandomGen g) => (a, a) -> g -> [a]
randomRs (b, b)
range g
g0)

streamToSF :: [b] -> SF a b
streamToSF :: forall b a. [b] -> SF a b
streamToSF []     = forall a. String -> String -> String -> a
intErr String
"Yampa" String
"streamToSF" String
"Empty list!"
streamToSF (b
b:[b]
bs) = SF {sfTF :: a -> Transition a b
sfTF = forall {p} {a}. p -> (SF' a b, b)
tf0}
  where
    tf0 :: p -> (SF' a b, b)
tf0 p
_ = (forall {b} {a}. [b] -> SF' a b
stsfAux [b]
bs, b
b)

    stsfAux :: [b] -> SF' a b
stsfAux []     = forall a. String -> String -> String -> a
intErr String
"Yampa" String
"streamToSF" String
"Empty list!"
    -- Invarying since stsfAux [] is an error.
    stsfAux (b
b:[b]
bs) = forall a b. (DTime -> a -> Transition a b) -> SF' a b
SF' forall {p} {p}. p -> p -> (SF' a b, b)
tf -- True
      where
        tf :: p -> p -> (SF' a b, b)
tf p
_ p
_ = ([b] -> SF' a b
stsfAux [b]
bs, b
b)

-- | Stochastic event source with events occurring on average once every t_avg
-- seconds. However, no more than one event results from any one sampling
-- interval in the case of relatively sparse sampling, thus avoiding an
-- "event backlog" should sampling become more frequent at some later
-- point in time.

occasionally :: RandomGen g => g -> Time -> b -> SF a (Event b)
occasionally :: forall g b a. RandomGen g => g -> DTime -> b -> SF a (Event b)
occasionally g
g DTime
t_avg b
x | DTime
t_avg forall a. Ord a => a -> a -> Bool
> DTime
0 = SF {sfTF :: a -> Transition a (Event b)
sfTF = forall {p} {a} {a}. p -> (SF' a (Event b), Event a)
tf0}
                       | Bool
otherwise = forall a. String -> String -> String -> a
usrErr String
"Yampa" String
"occasionally"
                                            String
"Non-positive average interval."
  where
    -- Generally, if events occur with an average frequency of f, the
    -- probability of at least one event occurring in an interval of t is given
    -- by (1 - exp (-f*t)). The goal in the following is to decide whether at
    -- least one event occurred in the interval of size dt preceding the
    -- current sample point. For the first point, we can think of the preceding
    -- interval as being 0, implying no probability of an event occurring.

    tf0 :: p -> (SF' a (Event b), Event a)
tf0 p
_ = (forall {a}. [DTime] -> SF' a (Event b)
occAux (forall a g. (Random a, RandomGen g) => g -> [a]
randoms g
g :: [Time]), forall a. Event a
NoEvent)

    occAux :: [DTime] -> SF' a (Event b)
occAux [] = forall a. HasCallStack => a
undefined
    occAux (DTime
r:[DTime]
rs) = forall a b. (DTime -> a -> Transition a b) -> SF' a b
SF' forall {p}. DTime -> p -> (SF' a (Event b), Event b)
tf -- True
      where
        tf :: DTime -> p -> (SF' a (Event b), Event b)
tf DTime
dt p
_ = let p :: DTime
p = DTime
1 forall a. Num a => a -> a -> a
- forall a. Floating a => a -> a
exp (-(DTime
dtforall a. Fractional a => a -> a -> a
/DTime
t_avg)) -- Probability for at least one
                                                -- event.
                  in ([DTime] -> SF' a (Event b)
occAux [DTime]
rs, if DTime
r forall a. Ord a => a -> a -> Bool
< DTime
p then forall a. a -> Event a
Event b
x else forall a. Event a
NoEvent)