{-
	Copyright (C) 2021 Dr. Alistair Ward

	This file is part of BishBosh.

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

	BishBosh 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 General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with BishBosh.  If not, see <http://www.gnu.org/licenses/>.
-}
{- |
 [@AUTHOR@]	Dr. Alistair Ward

 [@DESCRIPTION@]	Defines a class to which IO data-types which can be switched between binary states, can conform.
-}

module BishBosh.Property.Switchable(
-- * Type-classes
	Switchable(..),
-- * Functions
	flick
 ) where

import qualified	Data.Foldable

-- | For data which operate like binary switches.
class Switchable a where
	on	:: IO a		-- ^ Returns a switch in the /on/ state, regardless of any previous state.

	toggle	:: a -> IO a	-- ^ Switch the binary state.

	switchOff	:: a -> IO a	-- ^ Turn the switch off, which has no effect if already in the /off/ state.
	switchOff a
a
		| a -> Bool
forall a. Switchable a => a -> Bool
isOn a
a	= a -> IO a
forall a. Switchable a => a -> IO a
toggle a
a
		| Bool
otherwise	= a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return {-to IO-monad-} a
a

	isOn	:: a -> Bool	-- ^ Predicate: whether the switch is currently in the /on/ state.

	isOff	:: a -> Bool	-- ^ Predicate: whether the switch is currently in the /off/ state.
	isOff	= Bool -> Bool
not (Bool -> Bool) -> (a -> Bool) -> a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Bool
forall a. Switchable a => a -> Bool
isOn

-- | Toggle the switch the specified number of times.
flick :: Switchable switchable => Int -> switchable -> IO switchable
flick :: Int -> switchable -> IO switchable
flick Int
n switchable
switchable	= ((switchable -> IO switchable) -> switchable -> IO switchable)
-> switchable -> [switchable -> IO switchable] -> IO switchable
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> b -> m b) -> b -> t a -> m b
Data.Foldable.foldrM (switchable -> IO switchable) -> switchable -> IO switchable
forall a b. (a -> b) -> a -> b
($) switchable
switchable ([switchable -> IO switchable] -> IO switchable)
-> [switchable -> IO switchable] -> IO switchable
forall a b. (a -> b) -> a -> b
$ Int
-> (switchable -> IO switchable) -> [switchable -> IO switchable]
forall a. Int -> a -> [a]
replicate Int
n switchable -> IO switchable
forall a. Switchable a => a -> IO a
toggle