{-
	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@]	The state which is threaded through successive calls to 'Search.Search.search'.
-}

module BishBosh.Search.SearchState(
-- * Types
-- ** Data-types
	SearchState(
--		MkSearchState,
		getPositionHashQuantifiedGameTree,
		getDynamicMoveData
	),
-- * Functions
-- ** Constructors
	mkSearchState,
	initialise
 ) where

import qualified	BishBosh.Data.Exception					as Data.Exception
import qualified	BishBosh.Evaluation.PositionHashQuantifiedGameTree	as Evaluation.PositionHashQuantifiedGameTree
import qualified	BishBosh.Evaluation.QuantifiedGame			as Evaluation.QuantifiedGame
import qualified	BishBosh.Model.Game					as Model.Game
import qualified	BishBosh.Property.Empty					as Property.Empty
import qualified	BishBosh.Search.DynamicMoveData				as Search.DynamicMoveData
import qualified	BishBosh.Search.EphemeralData				as Search.EphemeralData
import qualified	Control.Exception

-- | The data which is both received & returned by 'Search.Search.search', so that it is transported through the entire game.
data SearchState x y positionHash criterionValue weightedMean	= MkSearchState {
	SearchState x y positionHash criterionValue weightedMean
-> PositionHashQuantifiedGameTree
     x y positionHash criterionValue weightedMean
getPositionHashQuantifiedGameTree	:: Evaluation.PositionHashQuantifiedGameTree.PositionHashQuantifiedGameTree x y positionHash criterionValue weightedMean,
	SearchState x y positionHash criterionValue weightedMean
-> DynamicMoveData x y positionHash
getDynamicMoveData			:: Search.DynamicMoveData.DynamicMoveData x y positionHash
}

instance (
	Enum	x,
	Enum	y,
	Ord	x,
	Ord	y,
	Show	x,
	Show	y,
	Show	criterionValue,
	Show	positionHash,
	Show	weightedMean
 ) => Show (SearchState x y positionHash criterionValue weightedMean) where
	showsPrec :: Int
-> SearchState x y positionHash criterionValue weightedMean
-> ShowS
showsPrec Int
_ SearchState x y positionHash criterionValue weightedMean
_	= String -> ShowS
showString String
"SearchState {...}"

-- | Constructor.
mkSearchState
	:: Evaluation.PositionHashQuantifiedGameTree.PositionHashQuantifiedGameTree x y positionHash criterionValue weightedMean
	-> Search.DynamicMoveData.DynamicMoveData x y positionHash
	-> SearchState x y positionHash criterionValue weightedMean
mkSearchState :: PositionHashQuantifiedGameTree
  x y positionHash criterionValue weightedMean
-> DynamicMoveData x y positionHash
-> SearchState x y positionHash criterionValue weightedMean
mkSearchState	= PositionHashQuantifiedGameTree
  x y positionHash criterionValue weightedMean
-> DynamicMoveData x y positionHash
-> SearchState x y positionHash criterionValue weightedMean
forall x y positionHash criterionValue weightedMean.
PositionHashQuantifiedGameTree
  x y positionHash criterionValue weightedMean
-> DynamicMoveData x y positionHash
-> SearchState x y positionHash criterionValue weightedMean
MkSearchState

-- | Smart constructor.
initialise :: Evaluation.PositionHashQuantifiedGameTree.PositionHashQuantifiedGameTree x y positionHash criterionValue weightedMean -> SearchState x y positionHash criterionValue weightedMean
initialise :: PositionHashQuantifiedGameTree
  x y positionHash criterionValue weightedMean
-> SearchState x y positionHash criterionValue weightedMean
initialise PositionHashQuantifiedGameTree
  x y positionHash criterionValue weightedMean
positionHashQuantifiedGameTree
	| Game x y -> Bool
forall x y. Game x y -> Bool
Model.Game.isTerminated Game x y
game	= Exception
-> SearchState x y positionHash criterionValue weightedMean
forall a e. Exception e => e -> a
Control.Exception.throw (Exception
 -> SearchState x y positionHash criterionValue weightedMean)
-> Exception
-> SearchState x y positionHash criterionValue weightedMean
forall a b. (a -> b) -> a -> b
$ String -> Exception
Data.Exception.mkResultUndefined String
"BishBosh.Search.SearchState.initialise:\tcan't search for a move from a terminated game."
	| Bool
otherwise			= MkSearchState :: forall x y positionHash criterionValue weightedMean.
PositionHashQuantifiedGameTree
  x y positionHash criterionValue weightedMean
-> DynamicMoveData x y positionHash
-> SearchState x y positionHash criterionValue weightedMean
MkSearchState {
		getPositionHashQuantifiedGameTree :: PositionHashQuantifiedGameTree
  x y positionHash criterionValue weightedMean
getPositionHashQuantifiedGameTree	= PositionHashQuantifiedGameTree
  x y positionHash criterionValue weightedMean
positionHashQuantifiedGameTree,
		getDynamicMoveData :: DynamicMoveData x y positionHash
getDynamicMoveData			= DynamicMoveData x y positionHash
forall a. Empty a => a
Property.Empty.empty
	}
	where
		game :: Game x y
game	= QuantifiedGame x y criterionValue weightedMean -> Game x y
forall x y criterionValue weightedMean.
QuantifiedGame x y criterionValue weightedMean -> Game x y
Evaluation.QuantifiedGame.getGame (QuantifiedGame x y criterionValue weightedMean -> Game x y)
-> QuantifiedGame x y criterionValue weightedMean -> Game x y
forall a b. (a -> b) -> a -> b
$ PositionHashQuantifiedGameTree
  x y positionHash criterionValue weightedMean
-> QuantifiedGame x y criterionValue weightedMean
forall x y positionHash criterionValue weightedMean.
PositionHashQuantifiedGameTree
  x y positionHash criterionValue weightedMean
-> QuantifiedGame x y criterionValue weightedMean
Evaluation.PositionHashQuantifiedGameTree.getRootQuantifiedGame PositionHashQuantifiedGameTree
  x y positionHash criterionValue weightedMean
positionHashQuantifiedGameTree

instance Search.EphemeralData.MaybeEphemeralData (SearchState x y positionHash criterionValue weightedMean) where
	maybeEuthanise :: Int
-> MaybeRetireAfterNMoves
-> MaybeRetireAfterNMoves
-> SearchState x y positionHash criterionValue weightedMean
-> SearchState x y positionHash criterionValue weightedMean
maybeEuthanise Int
nPlies MaybeRetireAfterNMoves
maybeRetireKillerMovesAfter MaybeRetireAfterNMoves
maybeRetireTranspositionsAfter searchState :: SearchState x y positionHash criterionValue weightedMean
searchState@MkSearchState { getDynamicMoveData :: forall x y positionHash criterionValue weightedMean.
SearchState x y positionHash criterionValue weightedMean
-> DynamicMoveData x y positionHash
getDynamicMoveData = DynamicMoveData x y positionHash
dynamicMoveData }	= SearchState x y positionHash criterionValue weightedMean
searchState {
		getDynamicMoveData :: DynamicMoveData x y positionHash
getDynamicMoveData	= Int
-> MaybeRetireAfterNMoves
-> MaybeRetireAfterNMoves
-> DynamicMoveData x y positionHash
-> DynamicMoveData x y positionHash
forall a.
MaybeEphemeralData a =>
Int -> MaybeRetireAfterNMoves -> MaybeRetireAfterNMoves -> a -> a
Search.EphemeralData.maybeEuthanise Int
nPlies MaybeRetireAfterNMoves
maybeRetireKillerMovesAfter MaybeRetireAfterNMoves
maybeRetireTranspositionsAfter DynamicMoveData x y positionHash
dynamicMoveData	-- Forward the request.
	}