{-
	Copyright (C) 2018 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@]

	* <https://www.chessprogramming.org/Transposition_Table>.

	* <https://en.wikipedia.org/wiki/Transposition_table>.

	* Valid move-sequences can be recorded against the hash of the position from which they start.
-}

module BishBosh.Search.Transpositions (
-- * Types
-- ** Type-synonyms
	Transformation,
-- ** Data-types
	Transpositions(),
-- * Functions
	find,
-- ** Mutators
	insert
 ) where

import qualified	BishBosh.Property.Empty			as Property.Empty
import qualified	BishBosh.Search.EphemeralData		as Search.EphemeralData
import qualified	BishBosh.Search.TranspositionValue	as Search.TranspositionValue
import qualified	Data.Map
import qualified	Data.Maybe

-- | Stores the result of an alpha-beta search from a /position/.
newtype Transpositions move positionHash	= MkTranspositions {
	Transpositions move positionHash -> Map positionHash (Value move)
deconstruct	:: Data.Map.Map positionHash (Search.TranspositionValue.Value move)
}

instance Property.Empty.Empty (Transpositions move positionHash) where
	empty :: Transpositions move positionHash
empty	= Map positionHash (Value move) -> Transpositions move positionHash
forall move positionHash.
Map positionHash (Value move) -> Transpositions move positionHash
MkTranspositions Map positionHash (Value move)
forall k a. Map k a
Data.Map.empty

instance Search.EphemeralData.EphemeralData (Transpositions move positionHash) where
	getSize :: Transpositions move positionHash -> NPlies
getSize	MkTranspositions { deconstruct :: forall move positionHash.
Transpositions move positionHash -> Map positionHash (Value move)
deconstruct = Map positionHash (Value move)
byPositionHash }		= Map positionHash (Value move) -> NPlies
forall k a. Map k a -> NPlies
Data.Map.size Map positionHash (Value move)
byPositionHash
	euthanise :: NPlies
-> Transpositions move positionHash
-> Transpositions move positionHash
euthanise NPlies
nPlies MkTranspositions { deconstruct :: forall move positionHash.
Transpositions move positionHash -> Map positionHash (Value move)
deconstruct = Map positionHash (Value move)
byPositionHash }	= Map positionHash (Value move) -> Transpositions move positionHash
forall move positionHash.
Map positionHash (Value move) -> Transpositions move positionHash
MkTranspositions (Map positionHash (Value move) -> Transpositions move positionHash)
-> Map positionHash (Value move)
-> Transpositions move positionHash
forall a b. (a -> b) -> a -> b
$ (Value move -> Bool)
-> Map positionHash (Value move) -> Map positionHash (Value move)
forall a k. (a -> Bool) -> Map k a -> Map k a
Data.Map.filter ((NPlies -> NPlies -> Bool
forall a. Ord a => a -> a -> Bool
> NPlies
nPlies) (NPlies -> Bool) -> (Value move -> NPlies) -> Value move -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Value move -> NPlies
forall move. Value move -> NPlies
Search.TranspositionValue.getNPlies) Map positionHash (Value move)
byPositionHash

-- | Returns any value previously recorded when searching from the specified /position/.
find
	:: Ord positionHash
	=> positionHash
	-> Transpositions move positionHash
	-> Maybe (Search.TranspositionValue.Value move)
find :: positionHash
-> Transpositions move positionHash -> Maybe (Value move)
find positionHash
positionHash MkTranspositions { deconstruct :: forall move positionHash.
Transpositions move positionHash -> Map positionHash (Value move)
deconstruct = Map positionHash (Value move)
byPositionHash }	= positionHash -> Map positionHash (Value move) -> Maybe (Value move)
forall k a. Ord k => k -> Map k a -> Maybe a
Data.Map.lookup positionHash
positionHash Map positionHash (Value move)
byPositionHash

-- | The type of a function which transforms 'Transpositions'.
type Transformation move positionHash	= Transpositions move positionHash -> Transpositions move positionHash

{- |
	* Optionally record a value found while searching for the optimal move from a position, against the position's hash.

	* If a matching key already exists, it's replaced if the new value is considered to be better.
-}
insert
	:: (Ord positionHash, Ord weightedMean)
	=> Search.TranspositionValue.FindFitness move weightedMean
	-> positionHash				-- ^ Represents the game from which the sequence of moves starts.
	-> Search.TranspositionValue.Value move	-- ^ The value to record.
	-> Transformation move positionHash
insert :: FindFitness move weightedMean
-> positionHash -> Value move -> Transformation move positionHash
insert FindFitness move weightedMean
findFitness positionHash
positionHash Value move
proposedValue MkTranspositions { deconstruct :: forall move positionHash.
Transpositions move positionHash -> Map positionHash (Value move)
deconstruct = Map positionHash (Value move)
byPositionHash }	= Map positionHash (Value move) -> Transpositions move positionHash
forall move positionHash.
Map positionHash (Value move) -> Transpositions move positionHash
MkTranspositions (Map positionHash (Value move) -> Transpositions move positionHash)
-> Map positionHash (Value move)
-> Transpositions move positionHash
forall a b. (a -> b) -> a -> b
$ (Maybe (Value move) -> Maybe (Value move))
-> positionHash
-> Map positionHash (Value move)
-> Map positionHash (Value move)
forall k a.
Ord k =>
(Maybe a -> Maybe a) -> k -> Map k a -> Map k a
Data.Map.alter (
	Maybe (Value move)
-> (Value move -> Maybe (Value move))
-> Maybe (Value move)
-> Maybe (Value move)
forall b a. b -> (a -> b) -> Maybe a -> b
Data.Maybe.maybe (Value move -> Maybe (Value move)
forall a. a -> Maybe a
Just Value move
proposedValue) {-there's no incumbent-} ((Value move -> Maybe (Value move))
 -> Maybe (Value move) -> Maybe (Value move))
-> (Value move -> Maybe (Value move))
-> Maybe (Value move)
-> Maybe (Value move)
forall a b. (a -> b) -> a -> b
$ \Value move
incumbentValue -> if FindFitness move weightedMean -> Value move -> Value move -> Bool
forall weightedMean move.
Ord weightedMean =>
FindFitness move weightedMean -> Value move -> Value move -> Bool
Search.TranspositionValue.isBetter FindFitness move weightedMean
findFitness Value move
proposedValue Value move
incumbentValue
		then Value move -> Maybe (Value move)
forall a. a -> Maybe a
Just Value move
proposedValue	-- Upgrade.
		else Maybe (Value move)
forall a. Maybe a
Nothing	-- Leave incumbent.
 ) positionHash
positionHash Map positionHash (Value move)
byPositionHash