WorksComputer Science
Alpha-Beta Pruning via Monadic Continuations
Abstract
Alpha-beta pruning is a fundamental technique in game-playing AI, enabling efficient minimax search by avoiding the exploration of branches that cannot affect the optimal decision. We present a monadic formulation of alpha-beta pruning based on the combination of the continuation monad and the reader monad. Unlike previous approaches that encode alpha-beta bounds into the types of available moves, our construction records these bounds within monadic outcomes, leaving the move types unchanged. We further show that the ordinary continuation monad suffices to compute not only optimal outcomes but also optimal plays, by extending the outcome type to include the corresponding sequences of moves. This provides an alternative to generalisations of the continuation monad proposed to address the inefficiencies associated with the selection monad in alpha-beta search. We illustrate our approach with an implementation of Tic-Tac-Toe, observing a 39-fold reduction in the number of plays evaluated, compared with exhaustive search, in computing an optimal play. Finally, we formally establish the correctness of our construction by proving that the monadic alpha-beta search computes the same optimal outcomes and plays as exhaustive minimax search. Our results demonstrate how a foundational algorithm in AI can be expressed and verified using standard monadic abstractions, providing a basis for further development of compositional approaches to adversarial search.
Introduction
Alpha-beta pruning is one of the foundational search techniques in artificial intelligence, particularly in adversarial game playing. One of the earliest explicit descriptions of the method was given by Hart and Edwards in 1961 in the
MIT Artificial Intelligence Project memo The Tree Prune (TP) Algorithm [7]. The method was subsequently described as the Alpha-Beta Heuristic, giving rise to the terminology now generally used. Alpha-beta pruning improves minimax search by maintaining bounds on the values available to the maximising and minimising players and terminating the exploration of branches that cannot affect the final decision. The basic algorithm preserves the result of minimax search while avoiding the evaluation of positions that have been shown to be irrelevant. Its theoretical foundations were later developed more formally by Knuth and Moore [10], establishing alpha-beta pruning as a central technique in game-tree search.
We have shown in [2, 3] that minimax-type searches can be performed using monadic programming, via the continuation monad for calculating optimal outcomes, and the selection monad for optimal plays and strategies (cf. also [1, 4, 5]). Since the type of the continuation monad
generalises the types of boolean quantifiers , : , we have referred to elements of as quantifiers, and used them to model players in so-called higher-order games. In , we think of as the type of available moves for the player, and as the type of game outcomes.
In a recent work, Hartmann [8] proposed an extension of this construction to cater for alpha-beta type optimisations. This builds on the work of Hartmann et al. on a generalisation of the continuation monad [9]. In Hartmann [8], the values of and are “encoded” into the types of moves, so that the extended move type becomes , where is the actual type of moves, and record the current values of alpha and beta.
In the present paper we propose an alternative approach for the monadic alpha-beta optimisation by combining the continuation monad with the reader monad. Our main contributions are as follows:
Instead of considering moves of type , as in [8], we show (§ 3) that the values of alpha and beta can be “stored” on monadic outcomes. Formally, let be a monad. We call quantifiers of type monadic quantifiers or monadic continuation operators since the result type is itself monadic. We will show that by taking to be the reader monad
we can use the monadic outcomes in to keep track of the alpha-beta values.
In § 4 we give a formal proof that the monadic continuations with the reader monad indeed calculate the same optimal outcome as the exhaustive search (Corollary 1).
Hartmann [8] and Hartmann et al. [9] correctly point out that when carrying the extra information for alpha and beta down the search tree, the selection monad seems to recalculate subgames again and again, as the lazy
evaluation that allows for efficient selection search is blocked. They suggest a generalisation of the continuation monad (making it polymorphic on the type of outcomes) that enables one to use it to calculate optimal moves. We show here that the usual continuation monad can already be used for such purposes by simply extending the outcome type to include the play that leads to the outcome (see § 5).
(4) Finally, in § 6 we extend the method and its correctness proof to history-dependent games, in which the moves available at a round depend on the moves played so far [3, 5]. We then apply the alpha-beta pruning search in order to compute the optimal outcome and play for Tic-Tac-Toe, and measure the performance gain of using the pruning mechanism.
The definitions, results and examples of § 4 and § 5, and the outcomes of the example of § 2 and § 3, have been formalized in the proof assistant Agda, in the TypeTopology library, in the module Games.AlphaBeta.index and the modules it imports [6]. It is developed in the generality of the history-dependent games of § 6.1. Its module Games.AlphaBeta.Article follows the paper item by item, and its module Games.AlphaBeta.Summary states Theorem 1 and Corollaries 1 and 2 together with every definition their statements use, so that the statements can be compared with the paper without reading the proofs.
Exhaustive Search via the Continuation Monad
We start with an overview of how the continuation monad can be used to perform an exhaustive search on a game tree, using alternating max and min quantifiers (cf. [2]). We first define the min and max quantifiers, then give an example game tree, and finally compose the (local) quantifiers into a (global) quantifier for the whole game.
The min and max quantifiers
For the sake of the running example, we take integers as moves, lists of integers as the lists of available moves and as plays, and integers as outcomes:
import K
type Move = Int
type Path = [Move]
type Moves = [Int]
type R = Int
We can then define min and max quantifiers (players) over these types as:
-- maximising player quantifier
phiMax :: Moves -> K R Move
phiMax xs = K (\p -> maximum $ map p xs)
-- minimising player quantifier
phiMin :: Moves -> K R Move
phiMin xs = K (\p -> minimum $ map p xs)
An example game tree
In order to illustrate and test the search, we consider the following simple game tree (Figure 1), based on the example given in the Wikipedia article on alpha-beta pruning [11]:
Figure 1. The example game tree. Each node shows its optimal outcome. The outcomes in grey are those that the alpha-beta search of § 3 does not evaluate.
import Debug.Trace
-- Example, based on:
-- https://en.wikipedia.org/w/index.php?oldid=1380302874
-- Outcome function for the game tree in Example
q' :: Path -> R
q' [0,0,0,0] = 5
q' [0,0,0,_] = 6
q' [0,0,1,0] = 7
q' [0,0,1,1] = 4
q' [0,0,1,2] = 5 -- can be pruned
q' [0,1,_,_] = 3 -- can be partially pruned
q' [1,0,0,_] = 6
q' [1,0,1,0] = 6
q' [1,0,1,_] = 9 -- can be pruned
q' [1,1,_,_] = 7 -- can be partially pruned
q' [2,0,_,_] = 5 -- can be partially pruned
q' [2,_,_,_] = 9 -- can be pruned
-- Add tracing to monitor evaluated branches
-- and order of evaluation
q :: Path -> R
q xs = trace (show xs ++ " -> " ++ show (q' xs)) (q' xs)
Composing quantifiers
These quantifiers can then be “sequenced” to obtain a global quantifier over plays (paths) in the game tree:
-- game quantifiers for the max-min experiment game trees
phis :: [K R Move]
phis = [phiMax [0,1,2], phiMin [0,1], phiMax [0,1], phiMin [0,1,2]]
-- sequencing quantifiers to obtain a 'global' game quantifier
bigPhi :: K R Path
bigPhi = sequence phis
Remark 1 (Haskell “sequence” and the product of quantifiers). For a monad , the Haskell “sequence” operation allows one to combine a sequence of monadic effects into a single effect that “produces” a list:
We have shown in [2] that the particular instance of this for the continuation monad (i.e. when ) allows one to compute optimal outcomes in general higher-order games (cf. also [3, 4]). As mentioned above, we have called elements of the continuation monad quantifiers, and the above construction the product of quantifiers, using the notation
which is defined by
where adds an element in front of a sequence .
We can then apply the global game quantifier to the (traceable) outcome function to obtain the optimal outcome of the game together with the sequence of evaluations of the experimental outcome function :
optimal_outcome :: R
optimal_outcome = quantifier bigPhi q
-- Test run (all paths are considered):
-- ghci> optimal_outcome
-- [0,0,0,0] -> 5
-- [0,0,0,1] -> 6
-- ...
-- [2,1,1,1] -> 9
-- [2,1,1,2] -> 9
-- 6
In this case we see that the optimal outcome of the max min game was calculated correctly, but all 36 paths were explored.
Adding Alpha-Beta Pruning via the Reader Monad
Building on the work of Hartmann [8], we now show how one can implement the alpha-beta optimisation using the standard continuation monad by taking the type of outcomes to be , where is the reader monad, so that we work with . In [8], instead, the values of alpha and beta were encoded in the type of moves. In our case, the type of moves in the search corresponds to the actual moves of the game, but the monadic outcomes capture the side-effects of updating the values of alpha and beta. We first define the optimised min and max quantifiers, then compose them, and finally run the alpha-beta search on the example game tree.
The optimised min and max quantifiers
Let us start by importing some useful functions, the standard continuation monad (Appendix B) and the reader monad (from the module Control.Monad.Trans.Reader). The types of moves, paths and outcomes () are as above.
import K
import Control.Monad.Trans.Reader
import Debug.Trace
import Utils
-- Value to be used as -inf and +inf
inf :: Int
inf = 1000
-- Reader Monad
type T = Reader (Int, Int)
We can then define quantifiers for the maximising and the minimising player that take the values of into account. The maximising player stops when the value being considered is greater than or equal to , and updates (highest guaranteed outcome):
phiMax :: Moves -> (Move -> T R) -> T R
phiMax xs p = do
(_, b) <- ask
rs <- l xs
return $ maximum $ takeUntil (>= b) rs
where
l :: Moves -> T [R]
l [] = return []
l (x:xs) = do
(a, b) <- ask
r <- p x
rs <- local (const (max a r, b)) (l xs)
return (r : rs)
The minimising player quantifier, on the other hand, stops when the value being considered is less than or equal to , and updates (lowest guaranteed outcome):
phiMin :: Moves -> (Move -> T R) -> T R
phiMin xs p = do
(a, _) <- ask
rs <- l xs
return $ minimum $ takeUntil (<= a) rs
where
l :: Moves -> T [R]
l [] = return []
l (x:xs) = do
(a, b) <- ask
r <- p x
rs <- local (const (a, min b r)) (l xs)
return (r : rs)
Composing optimised quantifiers
As in § 2.3, we also use here the monadic “sequence” to obtain a single (global) quantifier for the whole game, but now over monadic outcomes :
-- game quantifiers for max-min game trees
phisT :: [K (T R) Move]
phisT = [K (phiMax [0,1,2]), K (phiMin [0,1]),
K (phiMax [0,1]), K (phiMin [0,1,2])]
-- sequencing quantifier to obtain 'global' game quantifier
bigPhi :: K (T R) Path
bigPhi = sequence phisT
Alpha-beta search of the example game tree
We can then test this on the example game of § 2.2. We need to make the outcome function “monadic”:
-- Make it monadic and add tracing to monitor evaluated branches
-- and order of evaluation
q :: Path -> T R
q xs = return $ let r = q' xs in
trace (show xs ++ " -> " ++ show r) r
optimal_outcome :: R
optimal_outcome = (runReader $ quantifier bigPhi q) (-inf, inf)
-- Output:
-- ghci> optimal_outcome
-- [0,0,0,0] -> 5
-- [0,0,0,1] -> 6
-- [0,0,0,2] -> 6
-- [0,0,1,0] -> 7
-- [0,0,1,1] -> 4
-- [0,1,0,0] -> 3
-- [0,1,0,1] -> 3
-- [0,1,0,2] -> 3
-- [0,1,1,0] -> 3
-- [1,0,0,0] -> 6
-- [1,0,0,1] -> 6
-- [1,0,0,2] -> 6
-- [1,0,1,0] -> 6
-- [1,1,0,0] -> 7
-- [1,1,0,1] -> 7
-- [1,1,0,2] -> 7
-- [2,0,0,0] -> 5
-- [2,0,1,0] -> 5
-- 6
We can see that pruning of the irrelevant branches indeed took place and only the 18 essential plays were considered during the search.
Correctness of Alpha-Beta Optimisation
We now give a formal proof that the alpha-beta optimisation achieved by the monadic continuations of § 3 indeed (always) computes the same optimal game outcomes as the original (un-optimised) construction.
We compare outcomes through a map into a type of values with a decidable strict weak order , that is, a decidable relation that is irreflexive, transitive and cotransitive, where cotransitive means that implies or . We write for . Decidable linear orders, such as that of the integers, are examples, and in general a strict weak order is a linear order of classes of tied values, where and are tied when neither nor . The bounds and are values. In § 2–§ 4, and is the identity, and in § 5, and is the first projection. Comparisons of outcomes, with each other or with values, are comparisons of their values, and is . The maximum of a non-empty list of outcomes is its first element of largest value, as computed by pickMax of Appendix § 7, and dually for the minimum. At every position that is not final, the list of available moves is non-empty.
Let denote non-empty lists of ’s, and let us write
for the standard quantifiers min and max (§ 2.1), and
for the monadic versions which perform - pruning where , given by , is the reader monad (§ 3.1). We will also write or as a placeholder for either the min or the max version of the corresponding quantifiers.
Given a game , where is a sequence of quantifiers and is the outcome function, the optimal game outcome can be calculated as (cf. Remark 1)
The same can be said for the monadic game , where is the unit of the reader monad. Given concrete values of we can also obtain a game outcome as
which we will call the monadic outcome. We will show that the optimal game outcome indeed coincides with the monadic outcome. Since we will do this by induction on the game tree, we will use the notation , given an initial play , for the game where is the list of quantifiers that remain after playing the moves , and , given by , is the outcome function of the subgame defined by . Here is the concatenation of and $t (overloading notation). By the definition of , if is the quantifier at , then
§ 6 discusses how one can easily extend these definitions and proofs to games in which the quantifier at a position depends on the moves played so far.
Since we want to prove the correctness of the monadic quantifiers of § 3.1, let us also define them in standard mathematical notation:
Definition 1 ( and ). Let us first define
where is the type of non-empty lists over , as follows:
where
Here is the initial segment of up to and including the first element that satisfies , and the whole of if there is none, so that it is non-empty when is. The definition of is dual to the above (cf. Haskell code of § 3.1).
From now on we focus on . The dual results hold for , since is for the reversed order with and exchanged, which exchanges clauses (i) and (ii) of Theorem 1 below and preserves clause (iii).
Note that the list in the definition of contains elements of the form for different values . For a list , which the search considers in this order, define by
Lemma 1 (Characterisation of ). Let and and be given. Then
The above lemma shows that the -th member of is . This will be important for our proof by induction on the game tree. The following lemma is a trivial consequence of the above characterisation of :
Lemma 2. Let , a non-empty list and be given.
(i) if , for all , then for all , and ,
(ii) if , for some , then .
As noted above, we can also prove similar (dual) lemmas for and a sequence .
Theorem 1. For every partial play and every pair
(i) implies ,
(ii) implies ,
(iii) implies .
Proof. By induction on the game tree.
(1) If is a final position, then , and (i)–(iii) hold trivially.
(2) Let be an intermediate position, and suppose w.l.o.g. that max plays at . Let be the list of moves at , and . We consider three cases:
(i) . This is the simplest case because if then , for all moves (since we are considering the max player). By induction hypothesis, we have that , for all . In this case, in the definition of the recursive function , the values of and are not updated. Hence
(ii) . Let be such that . Since max updates but not , IH (ii) at gives , and hence, by Lemma 2(ii),
(iii) . Let , and let be the index at which this maximum is attained first, so that . Then for every , and for . Each index falls under one of the following cases:
(a) , and then, by IH (i), ;
(b) , and then, since , IH (iii) gives .
Since , induction on gives and for every . Hence , so (b) applies to , and
For we have , and so , by (a) or (b). Therefore no member of the list is , keeps the whole list, and its maximum is , attained first at the index . Hence,
Corollary 1. (a) If for every play , then . (b) If , then .
Proof. (a) is one of the values , so Theorem 1(iii) applies. (b) By Theorem 1(i) and (ii), and are impossible, so (iii) applies.
Computing Optimal Plays
In § 3 we showed how to use the following instance of the continuation monad
with the type of game outcomes and the reader monad, to simulate the alpha-beta pruning optimisation of the exhaustive search. This allowed us to compute the optimal outcome of a game in a more efficient way.
We now show that with a few minor changes we can compute not just the optimal outcome but also an optimal play that leads to it, while still making use of the alpha-beta optimisation.
Our first change is to extend the outcome type to include the play that leads to the outcome:
-- Extended outcome type to include play leading to the outcome
type R = (Int, Path)
We then need to make four minor changes to the minmax quantifiers as the type is no longer just an integer, but a pair of an integer and a path. For instance, the amended max quantifier would become (changes annotated with comments):
phiMax :: Moves -> K (T R) Move
phiMax = K . phiMax'
where
phiMax' :: Moves -> (Move -> T R) -> T R
phiMax' xs p = do
(_, b) <- ask
rs <- results xs p
-- changed (>=b) to \(r, ms) -> r >= b
-- changed maximum to pickMax
return $ pickMax $ takeUntil \(r, ms) -> r >= b) rs
results :: Moves -> (Move -> T R) -> T [R]
results [] _ = return []
results (y:ys) p = do
(a, b) <- ask
-- changed r <- p y to (r, ms) <- p y
(r, ms) <- p y
rs <- local (const (max a r, b)) (results ys p)
-- changed r to (r, ms)
return ((r, ms) : rs)
The function pickMax chooses the first of two outcomes with the same value. This matters: a pruned subgame returns an outcome whose value is only a bound, which must not displace an earlier outcome with the same value. The amended min quantifier is obtained in the same way from the phiMin of § 3.1, with pickMin in place of minimum. Since phiMax and phiMin now return elements of K, the list of quantifiers becomes:
phisT :: [K (T R) Move]
phisT = [phiMax [0,1,2], phiMin [0,1], phiMax [0,1], phiMin [0,1,2]]
We then need to modify the outcome function q so that it returns not just the game outcome, but also the play that led to that outcome:
-- Make it monadic and add tracing to monitor evaluated branches
-- and order of evaluation
q :: Path -> T R
q xs = return $ let r = q' xs in
trace (show xs ++ " -> " ++ show r) (r, xs)
A run of the game quantifier on this outcome function will not just compute the optimal outcome but also an optimal play (while still performing the alpha-beta pruning):
optimal_outcome_and_play :: R
optimal_outcome_and_play =
(runReader $ quantifier bigPhi q) (-inf, inf)
-- ghci> optimal_outcome_and_play
-- [0,0,0,0] -> 5
-- [0,0,0,1] -> 6
-- [0,0,0,2] -> 6
-- [0,0,1,0] -> 7
-- [0,0,1,1] -> 4
-- [0,1,0,0] -> 3
-- [0,1,0,1] -> 3
-- [0,1,0,2] -> 3
-- [0,1,1,0] -> 3
-- [1,0,0,0] -> 6
-- [1,0,0,1] -> 6
-- [1,0,0,2] -> 6
-- [1,0,1,0] -> 6
-- [1,1,0,0] -> 7
-- [1,1,0,1] -> 7
-- [1,1,0,2] -> 7
-- [2,0,0,0] -> 5
-- [2,0,1,0] -> 5
-- (6,[1,0,0,0])
Definition 2. A play of a game is optimal if for every , where .
Corollary 2. Let be the game with the outcome function , and with outcomes compared by their first components. Then for an optimal play of . Hence, if , then .
Proof. The first claim is by induction on the game tree, since the maximum and the minimum of a non-empty list of outcomes are members of the list, and since the first component of is for every partial play . The second claim follows from the first and Theorem 1(iii) for .
History-Dependent Games
So far we have built the alpha-beta search one step at a time. In § 2 the exhaustive minimax search of a game tree is the product of the min and max quantifiers of its rounds. In § 3 we obtained the alpha-beta search by replacing these quantifiers with monadic ones over the reader monad, without changing how they are composed (using the same product of quantifiers). § 4 proved that the two searches compute the same optimal outcome, and § 5 extended both to compute optimal plays as well. In all of these, however, the game has a fixed list of quantifiers. The moves available at a round, and hence the quantifier there, are the same whatever moves were played before. These are history-free games.
In this section we go one step further and consider alpha-beta pruning for history-dependent games, in which the moves available at a round depend on the moves played so far. Most games of interest are of this kind: in board games, for instance, a move is usually only available if the square or the piece it involves is still free. The monadic quantifiers of Sections 3 and 5 need no change. What changes is how they are composed: the product of quantifiers becomes the product of history-dependent quantifiers [3, 5], in which the quantifier at each round is chosen as a function of the partial play leading to it. The correctness results of Sections 4 and 5 carry over to this generality.
We first introduce products of history-dependent quantifiers, and explain why the correctness proofs still apply (§ 6.1). We then illustrate the approach with Tic-Tac-Toe, in which each player may only occupy a square that is still free (§ 6.2). We use it to compute the optimal outcome and an optimal play, and compare the number of plays evaluated by the alpha-beta search with that of the exhaustive search.
Products of history-dependent quantifiers
In the games considered so far, the quantifier at each round is fixed in advance. In many games, such as Tic-Tac-Toe, the moves available at a round depend on the moves played so far. Following [3, 5], we model such a game by letting the quantifier at each round depend on the partial play that leads to it, so that a game is given by quantifiers
together with an outcome function . The product of quantifiers is then defined for every partial play of length by
and the optimal outcome is , as computed by the function hsequence of Appendix B. The monadic quantifiers give the monadic outcome in the same way.
For the results of Sections 4 and 5, each is or applied to a non-empty list of the moves available at , and is the corresponding or . With the quantifier at a partial play of length , the definitions of and , from Section 4, should then use the dependent product of quantifiers. The proofs of Theorem 1 and Corollaries 1 and 2 are by induction on the game tree, and at each position they use the properties of the quantifier there, and so they apply to history-dependent games without change.
In [5], to which we refer the reader for the details, the type of moves at a round, and the number of rounds, may also depend on the moves played so far. A game is then given by a tree of types, whose paths are the plays, together with a quantifier at each internal node and an outcome function on the paths. Our Agda formalization is developed in this generality, with each internal node labelled by a player and the non-empty list of the moves available there, and Theorem 1 and Corollaries 1 and 2 are proved for all such games.
Alpha-beta search of Tic-Tac-Toe
In Tic-Tac-Toe, the moves are the nine squares of the board, numbered 0, . . . , 8 row by row, and a partial play is the list of the squares occupied so far, in the order in which they were occupied. The first player maximises and moves at the even rounds, the second minimises and moves at the odd rounds, and each may choose any square not yet occupied. Thus is for even and for odd , where is the list of the squares not in . We use the min and max quantifiers of § 5 to calculate (with alpha-beta pruning) the optimal outcome and an optimal play:
type Move = Int
type Path = [Move]
type Moves = [Int]
type Board = ([Move], [Move])
data Player = X | O
type Three = Int
type R = (Three, Path)
type T = Reader (Int, Int) -- Reader Monad
-- In this case +1 and -1 can work as upper and lower
-- bounds for the possible game outcomes
inf :: Int
inf = 1
-- Defining the outcome function q for Tic-Tac-Toe
wins :: Path -> Bool
wins xs = some
[[0,1,2],[3,4,5],[6,7,8],[0,3,6],[1,4,7],[2,5,8],[0,4,8],[2,4,6]]
(\ys -> contained ys xs)
value :: Board -> Three
value (x,o) = if wins x then 1 else if wins o then -1 else 0
outcome :: Player -> [Move] -> Board -> Board
outcome whoever [] board = board
outcome X (m : ms) (x, o) =
if wins o then (x, o) else outcome O ms (m : x, o)
outcome O (m : ms) (x, o) =
if wins x then (x, o) else outcome X ms (x, m : o)
q' :: Path -> Three
q' ms = value $ outcome X ms ([],[])
q :: Path -> T R
q xs = return $ let r = q' xs in
trace (show xs ++ " -> " ++ show r) (r, xs)
-- Defining the (history-dependent) quantifiers
-- game quantifiers for max-min players
phisT :: [Path -> K (T R) Move]
phisT = (phiMax . minus [0..8]) : (phiMin . minus [0..8]) : phisT
-- global quantifier for the game tree
-- we use hsequence since the available moves at any point in
-- the game depend on what has been played before
bigPhi :: K (T R) Path
bigPhi = hsequence $ take 9 phisT
-- Calculating optimal outcome and an optimal play
optimal_outcome_and_play :: R
optimal_outcome_and_play = (runReader $ quantifier bigPhi q) (-inf, inf)
-- ghci> optimal_outcome_and_play
-- [0,1,2,3,4,5,6,7,8] -> 1
-- [0,1,2,3,4,5,6,8,7] -> 1
-- [0,1,2,3,4,6,5,7,8] -> 1
-- ...
-- [8,4,7,6,5,2,1,0,3] -> -1
-- [8,4,7,6,5,2,3,0,1] -> -1
-- (0,[0,4,1,2,6,3,5,7,8])
Note that we are taking the initial values of and to be −1 and 1, respectively. Since the bounds −1 and 1 are also outcomes of the game, Corollary 1(a) does not apply. But the computed outcome 0 lies strictly between them, so it is the optimal outcome, by Corollary 1(b).
In this Tic-Tac-Toe example the exhaustive search evaluates the outcome function on all = 362 880 full plays of the game tree, whereas the alpha-beta search evaluates it on only 9 335 of them, about 39 times fewer, and computes the same optimal outcome and optimal play:
(0,[0,4,1,2,6,3,5,7,8])
Haskell – Utils
module Utils where
takeUntil :: (a -> Bool) -> [a] -> [a]
takeUntil _ [] = []
takeUntil p (x:xs) = x : if p x then [] else takeUntil p xs
pickMax :: (Ord a) => [(a, b)] -> (a, b)
pickMax [v] = v
pickMax ((v, x) : (w, y) : zs) =
if v >= w then pickMax ((v, x) : zs) else pickMax ((w, y) : zs)
pickMin :: (Ord a) => [(a, b)] -> (a, b)
pickMin [v] = v
pickMin ((v, x) : (w, y) : zs) =
if v <= w then pickMin ((v, x) : zs) else pickMin ((w, y) : zs)
some, every :: [a] -> (a -> Bool) -> Bool
some xs p = any p xs
every xs p = all p xs
contained :: Eq x => [x] -> [x] -> Bool
contained xs ys = every xs (\x -> elem x ys)
minus :: Eq x => [x] -> [x] -> [x]
minus xs ys = [x | x<-xs, not(elem x ys)]
Haskell – Continuation Monad
module K where
data K r x = K {quantifier :: (x -> r) -> r}
instance Functor (K r) where
fmap f phi = K (\p -> quantifier phi $ p . f)
instance Applicative (K r) where
pure x = K (\p -> p x)
fs <*> phis = do
f <- fs
x <- phis
return (f x)
instance Monad (K r) where
return = pure
phi >>= f = K (\p -> quantifier phi (\x -> quantifier (f x) p))
-- sequence for quantifiers that depend on the moves played so far
hsequence :: Monad m => [[x] -> m x] -> m [x]
hsequence = h []
where
h _ [] = return []
h s (f : fs) = do
x <- f s
xs <- h (s ++ [x]) fs
return (x : xs)References
- [1]Escardó, M., Oliva, P.: The Peirce translation and the double negation shift. In: Ferreira, F., Löwe, B., Mayordomo, E., Gomes, L.M. (eds.) Programs, Proofs, Pro- cesses - CiE 2010, LNCS 6158, pp. 151–161. Springer (2010)DOI
- [2]Escardó, M., Oliva, P.: Selection functions, bar recursion, and backward induction. Mathematical Structures in Computer Science 20(2), 127–168 (2010).DOI
- [3]Escardó, M., Oliva, P.: What sequential games, the Tychonoff theorem and the double-negation shift have in common. In: Proceedings of the Third ACM SIG-PLAN Workshop on Mathematically Structured Functional Programming (MSFP 2010). pp. 21–32. ACM (2010). https://doi.org/10.1145/1863597.1863605
- [4]Escardó, M., Oliva, P.: Sequential games and optimal strategies. Royal Society Proceedings A 467, 1519–1545 (2011)DOI
- [5]Escardó, M., Oliva, P.: Higher-order games with dependent types. Theoretical Computer Science 974, 114111 (2023). https://doi.org/10.1016/j.tcs.2023.114111
- [6]Escardó, M., Oliva, P.: Agda formalization of Alpha-Beta Pruning via Monadic Continuations. https://github.com/martinescardo/TypeTopology/tree/master/source/Games/AlphaBeta (2026), in the TypeTopology developmentarxiv.org/abs/2611.32320
- [7]Hart, T.P., Edwards, D.J.: The tree prune (TP) algorithm. Tech. Rep. AIM-030, Massachusetts Institute of Technology, Artificial Intelligence Project, Cambridge, Massachusetts (Dec 1961), later revised under the title The Alpha-Beta Heuristic
- [8]Hartmann, J.: Alpha beta pruning with the selection monad. In: Gibbons, J. (ed.) Trends in Functional Programming. pp. 378–391. Springer Nature Switzerland, Cham (2026)DOI
- [9]Hartmann, J., Schrijvers, T., Gibbons, J.: Towards a more efficient selection monad. In: Hemann, J., Chang, S. (eds.) Trends in Functional Programming. pp. 55–74. Springer Nature Switzerland, Cham (2025)DOI
- [10]Knuth, D.E., Moore, R.W.: An analysis of alpha-beta pruning. Artificial Intelligence 6(4), 293–326 (1975). https://doi.org/10.1016/0004-3702(75)90019-3
- [11]Alpha-beta pruning. https://en.wikipedia.org/w/index.php?title=Alpha%E2%80%93beta_pruning&oldid=138302874, Wikipedia, version of 14 October 2026
