{-
  This module is part of Chatty.
  Copyleft (c) 2014 Marvin Cohrs

  All wrongs reversed. Sharing is an act of love, not crime.
  Please share Antisplice with everyone you like.

  Chatty is free software: you can redistribute it and/or modify
  it under the terms of the GNU Affero General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  Chatty is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  GNU Affero General Public License for more details.

  You should have received a copy of the GNU Affero General Public License
  along with Chatty. If not, see <http://www.gnu.org/licenses/>.
-}

-- | Provides typeclasses for clocks and randomizer environments
module System.Chatty.Misc where

import Data.Time.Clock
import Data.Time.Calendar
import System.Random

-- | Typeclass for all monads that know the time
class (Functor m,Monad m) => MonadClock m where
  -- | Get UTC Time
  mutctime :: m UTCTime
  -- | Get timestamp, guaranteed to grow
  mgetstamp :: m NominalDiffTime
  mgetstamp = fmap (flip diffUTCTime (UTCTime (fromGregorian 1970 1 1) (secondsToDiffTime 0))) mutctime

instance MonadClock IO where
  mutctime = getCurrentTime

-- | Typeclass for all monads that may provide random numbers
class Monad m => MonadRandom m where
  -- | Get a single random number
  mrandom :: Random r => m r
  -- | Get a single random number in the given range
  mrandomR :: Random r => (r,r) -> m r

instance MonadRandom IO where
  mrandom = randomIO
  mrandomR rs = randomRIO rs