creatur-5.9.30: src/ALife/Creatur/Counter.hs
------------------------------------------------------------------------
-- |
-- Module : ALife.Creatur.Counter
-- Copyright : (c) 2012-2021 Amy de Buitléir
-- License : BSD-style
-- Maintainer : amy@nualeargais.ie
-- Stability : experimental
-- Portability : portable
--
-- A simple counter which persists between runs.
--
------------------------------------------------------------------------
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE FlexibleInstances #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module ALife.Creatur.Counter
(
Counter(..),
PersistentCounter,
mkPersistentCounter
) where
import ALife.Creatur.Clock (Clock, currentTime, incTime)
import ALife.Creatur.Persistent (Persistent, mkPersistent, getPS, putPS)
import Control.Monad.State (StateT)
class Counter c where
current :: StateT c IO Int
increment :: StateT c IO ()
type PersistentCounter = Persistent Int
-- | Creates a counter that will store its value in the specified file.
mkPersistentCounter :: FilePath -> PersistentCounter
mkPersistentCounter = mkPersistent 0
instance Counter PersistentCounter where
current = getPS
increment = do
k <- getPS
putPS (k+1)
instance Clock PersistentCounter where
currentTime = current
incTime = increment