betaFeedback

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

KRX≡(X→R)→RK_R X \equiv(X \to R) \to R

generalises the types of boolean quantifiers ∃\exists, ∀\forall: (X→B)→B(X \to\mathbb{B}) \to\mathbb{B}, we have referred to elements of KRXK_R X as quantifiers, and used them to model players in so-called higher-order games. In KRXK_R X, we think of XX as the type of available moves for the player, and RR 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 α\alpha and β\beta are “encoded” into the types of moves, so that the extended move type becomes X×N×NX \times\mathbb{N} \times\mathbb{N}, where XX is the actual type of moves, and N×N\mathbb{N} \times\mathbb{N} 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:

  1. Instead of considering moves of type X×N×NX \times\mathbb{N} \times\mathbb{N}, as in [8], we show (§ 3) that the values of alpha and beta can be “stored” on monadic outcomes. Formally, let TT be a monad. We call quantifiers of type KTRXK_{TR}X monadic quantifiers or monadic continuation operators since the result type TRTR is itself monadic. We will show that by taking TT to be the reader monad

TX=R×R→XTX = R \times R \to X

we can use the monadic outcomes in TRTR to keep track of the alpha-beta values.

  1. 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).

  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 RR 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 MM, the Haskell “sequence” operation allows one to combine a sequence of monadic effects into a single effect that “produces” a list:

sequence⁡:(MX)n→MXn\operatorname{sequence} : (MX)^n \to MX^n

We have shown in [2] that the particular instance of this for the continuation monad (i.e. when M=KRM = K_R) 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 KRK_R quantifiers, and the above construction the product of quantifiers, using the notation

⨂:(KRX)n→KRXn\bigotimes: (K_R X)^n \to K_R X^n

which is defined by

(⨂[])(q):=q([])(\bigotimes[])(q) := q([])
(⨂(ϕ0:ϕ))(q):=ϕ0(λx.(⨂ϕ)(λt.q(x⋅t))),(\bigotimes(\phi_0 : \phi))(q) := \phi_0(\lambda x.(\bigotimes\phi)(\lambda t.q(x \cdot t))),

where x⋅tx \cdot t adds an element xx in front of a sequence tt.

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 qq:

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 KRX≡(X→R)→RK_R X \equiv(X \to R) \to R by taking the type of outcomes to be TRT R, where TT is the reader monad, so that we work with KTRXK_{T R} X. 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 (RR) 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 (α,β)(\alpha, \beta) into account. The maximising player stops when the value being considered is greater than or equal to β\beta, and updates α\alpha (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 α\alpha, and updates β\beta (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 TRT R:

-- 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 qq “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 val⁡:R→V\operatorname{val}: R \to V into a type VV of values with a decidable strict weak order <<, that is, a decidable relation that is irreflexive, transitive and cotransitive, where cotransitive means that a<ba < b implies a<ca < c or c<bc < b. We write a≤ba \le b for ¬(b<a)\neg(b < a). 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 aa and bb are tied when neither a<ba < b nor b<ab < a. The bounds α\alpha and β\beta are values. In § 2–§ 4, V=RV = R and val⁡\operatorname{val} is the identity, and in § 5, R=V×PathR = V \times\mathit{Path} and val⁡\operatorname{val} is the first projection. Comparisons of outcomes, with each other or with values, are comparisons of their values, and max⁡(α,r)\max(\alpha,r) is max⁡(α,val⁡(r))\max(\alpha,\operatorname{val}(r)). 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 [X]+[X]^+ denote non-empty lists of XX’s, and let us write

ϕmin⁡,ϕmax⁡:[X]+→(X→R)→R\phi_{\min},\phi_{\max}: [X]^+ \to(X \to R) \to R

for the standard quantifiers min and max (§ 2.1), and

ϕmin⁡∗,ϕmax⁡∗:[X]+→(X→TR)→TR\phi_{\min}^{*},\phi_{\max}^{*}: [X]^+ \to(X \to TR) \to TR

for the monadic versions which perform α\alpha-β\beta pruning where TT, given by TX=V×V→XTX = V \times V \to X, is the reader monad (§ 3.1). We will also write ϕ\phi or ϕ∗\phi^{*} as a placeholder for either the min or the max version of the corresponding quantifiers.

Given a game G=(ϕ,q)G = (\phi,q), where ϕ\phi is a sequence of nn quantifiers and q:Xn→Rq: X^n \to R is the outcome function, the optimal game outcome can be calculated as (cf. Remark 1)

out⁡(G):=(⨂ϕ)(q).\operatorname{out}(G) := \left(\bigotimes\phi\right)(q).

The same can be said for the monadic game G∗=(ϕ∗,η∘q)G^{*} = (\phi^{*},\eta\circ q), where η(r)(α,β)=r\eta(r)(\alpha,\beta) = r is the unit of the reader monad. Given concrete values of (α,β)(\alpha,\beta) we can also obtain a game outcome as

out⁡α,β∗(G):=(⨂ϕ∗)(η∘q)(α,β),\operatorname{out}^{*}_{\alpha,\beta}(G) := \left(\bigotimes\phi^{*}\right)(\eta\circ q)(\alpha,\beta),

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 GsG_s, given an initial play ss, for the game (ϕs,qs)(\phi_s,q_s) where ϕs\phi_s is the list of quantifiers that remain after playing the moves ss, and qsq_s, given by qs(t)=q(s⋅t)q_s(t) = q(s \cdot t), is the outcome function of the subgame defined by ss. Here s⋅ts \cdot t is the concatenation of ss and $t (overloading notation). By the definition of ⨂\bigotimes, if ϕ\phi is the quantifier at ss, then

out⁡(Gs)=ϕ(λx.out⁡(Gs⋅x)),out⁡α,β∗(Gs)=ϕ∗(λx,α′,β′.out⁡α′,β′∗(Gs⋅x))(α,β).\begin{aligned} \operatorname{out}(G_s) &= \phi(\lambda x.\operatorname{out}(G_{s\cdot x})),\\ \operatorname{out}^{*}_{\alpha,\beta}(G_s) &= \phi^{*}(\lambda x,\alpha',\beta'.\operatorname{out}^{*}_{\alpha',\beta'}(G_{s\cdot x}))(\alpha,\beta). \end{aligned}

§ 6 discusses how one can easily extend these definitions and proofs to games in which the quantifier at a position depends on the moves ss played so far.

Since we want to prove the correctness of the monadic quantifiers ϕ∗\phi^{*} of § 3.1, let us also define them in standard mathematical notation:

Definition 1 (ϕmin⁡∗\phi^{*}_{\min} and ϕmax⁡∗\phi^{*}_{\max}). Let us first define

ϕmax⁡∗:[X]+→(X→TR)→TR\phi^{*}_{\max} : [X]^{+} \to(X \to TR) \to TR

where [X]+[X]^{+} is the type of non-empty lists over XX, as follows:

ϕmax⁡∗(xs,p)(α,β):=max⁡(takeUntil⁡(≥β) ℓ(xs)(α,β))\phi^{*}_{\max}(xs,p)(\alpha,\beta) := \max(\operatorname{takeUntil}(\geq\beta)\ \ell(xs)(\alpha,\beta))

where ℓ:[X]→T[R]\ell: [X] \to T[R]

ℓ([])(α,β):=[]\ell([])(\alpha,\beta) := []
ℓ(x:xs)(α,β):=v:ℓ(xs)(max⁡(α,v),β),where v=p(x)(α,β).\ell(x : xs)(\alpha,\beta) := v : \ell(xs)(\max(\alpha,v),\beta), \quad\text{where } v = p(x)(\alpha,\beta).

Here takeUntil⁡ c ys\operatorname{takeUntil}\ c\ ys is the initial segment of ysys up to and including the first element that satisfies cc, and the whole of ysys if there is none, so that it is non-empty when ysys is. The definition of ϕmin⁡∗\phi^{*}_{\min} is dual to the above (cf. Haskell code of § 3.1).

From now on we focus on ϕmax⁡∗\phi^{*}_{\max}. The dual results hold for ϕmin⁡∗\phi^{*}_{\min}, since ϕmin⁡∗\phi^{*}_{\min} is ϕmax⁡∗\phi^{*}_{\max} for the reversed order with α\alpha and β\beta exchanged, which exchanges clauses (i) and (ii) of Theorem 1 below and preserves clause (iii).

Note that the list ℓ(xs)(α,β)\ell(xs)(\alpha,\beta) in the definition of ϕmax⁡∗\phi^{*}_{\max} contains elements of the form p(x)(α′,β)p(x)(\alpha',\beta) for different values α′\alpha'. For a list xs=[x0,…,xn−1]xs = [x_0,\ldots,x_{n-1}], which the search considers in this order, define α0,…,αn−1\alpha_0,\ldots,\alpha_{n-1} by

α0:=α,αi+1:=max⁡(αi,p(xi)(αi,β)).\alpha_0 := \alpha,\qquad\alpha_{i+1} := \max(\alpha_i,p(x_i)(\alpha_i,\beta)).

Lemma 1 (Characterisation of ℓ(xs)(α,β)\ell(xs)(\alpha,\beta)). Let p:X→TRp : X \to TR and xs=[x0,…,xn−1]:[X]xs = [x_0,\ldots,x_{n-1}] : [X] and α,β\alpha,\beta be given. Then

ℓ(xs)(α,β)=[p(x0)(α0,β),…,p(xn−1)(αn−1,β)].\ell(xs)(\alpha,\beta) = [p(x_0)(\alpha_0,\beta),\ldots,p(x_{n-1})(\alpha_{n-1},\beta)].

The above lemma shows that the ii-th member of ℓ(xs)(α,β)\ell(xs)(\alpha,\beta) is p(xi)(αi,β)p(x_i)(\alpha_i,\beta). 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 ℓ(xs)(α,β)\ell(xs)(\alpha,\beta):

Lemma 2. Let p:X→TRp : X \to TR, a non-empty list xs=[x0,…,xn−1]xs = [x_0,\ldots,x_{n-1}] and (α,β)(\alpha,\beta) be given.

(i) if p(xi)(α,β)≤αp(x_i)(\alpha,\beta) \leq\alpha, for all i<ni < n, then αi=α\alpha_i = \alpha for all i<ni < n, and ϕmax⁡∗(xs,p)(α,β)≤α\phi^{*}_{\max}(xs,p)(\alpha,\beta) \leq\alpha,

(ii) if p(xi)(αi,β)≥βp(x_i)(\alpha_i,\beta) \geq\beta, for some i<ni < n, then ϕmax⁡∗(xs,p)(α,β)≥β\phi^{*}_{\max}(xs,p)(\alpha,\beta) \geq\beta.

As noted above, we can also prove similar (dual) lemmas for ϕmin⁡∗\phi^{*}_{\min} and a sequence βi\beta_i.

Theorem 1. For every partial play ss and every pair (α,β)(\alpha,\beta)

(i) out⁡(Gs)≤α\operatorname{out}(G_s) \le\alpha implies out⁡α,β∗(Gs)≤α\operatorname{out}^{*}_{\alpha,\beta}(G_s) \le\alpha,

(ii) out⁡(Gs)≥β\operatorname{out}(G_s) \ge\beta implies out⁡α,β∗(Gs)≥β\operatorname{out}^{*}_{\alpha,\beta}(G_s) \ge\beta,

(iii) α<out⁡(Gs)<β\alpha< \operatorname{out}(G_s) < \beta implies out⁡α,β∗(Gs)=out⁡(Gs)\operatorname{out}^{*}_{\alpha,\beta}(G_s) = \operatorname{out}(G_s).

Proof. By induction on the game tree.

(1) If ss is a final position, then out⁡α,β∗(Gs)=q(s)=out⁡(Gs)\operatorname{out}^{*}_{\alpha,\beta}(G_s) = q(s) = \operatorname{out}(G_s), and (i)–(iii) hold trivially.

(2) Let ss be an intermediate position, and suppose w.l.o.g. that max plays at ss. Let xs=[x0,…,xn−1]xs = [x_0,\ldots,x_{n-1}] be the list of moves at ss, and p=λx,α′,β′.out⁡α′,β′∗(Gs⋅x)p = \lambda x,\alpha',\beta'.\operatorname{out}^{*}_{\alpha',\beta'}(G_{s\cdot x}). We consider three cases:

(i) out⁡(Gs)≤α\operatorname{out}(G_s) \le\alpha. This is the simplest case because if out⁡(Gs)≤α\operatorname{out}(G_s) \le\alpha then out⁡(Gs⋅x)≤α\operatorname{out}(G_{s\cdot x}) \le\alpha, for all moves xx (since we are considering the max player). By induction hypothesis, we have that out⁡α,β∗(Gs⋅x)≤α\operatorname{out}^{*}_{\alpha,\beta}(G_{s\cdot x}) \le\alpha, for all xx. In this case, in the definition of the recursive function ℓ\ell, the values of α\alpha and β\beta are not updated. Hence

out⁡α,β∗(Gs)=ϕmax⁡∗(xs,p)(α,β)≤α.\begin{aligned} \operatorname{out}^{*}_{\alpha,\beta}(G_s) = \phi^{*}_{\max}(xs,p)(\alpha,\beta) \\ &\le\alpha. \end{aligned}

(ii) out⁡(Gs)≥β\operatorname{out}(G_s) \ge\beta. Let ii be such that out⁡(Gs⋅xi)≥β\operatorname{out}(G_{s\cdot x_i}) \ge\beta. Since max updates α\alpha but not β\beta, IH (ii) at (αi,β)(\alpha_i,\beta) gives out⁡αi,β∗(Gs⋅xi)≥β\operatorname{out}^{*}_{\alpha_i,\beta}(G_{s\cdot x_i}) \ge\beta, and hence, by Lemma 2(ii),

out⁡α,β∗(Gs)=ϕmax⁡∗(xs,p)(α,β)≥β.\begin{aligned} \operatorname{out}^{*}_{\alpha,\beta}(G_s) = \phi^{*}_{\max}(xs,p)(\alpha,\beta) \\ &\ge\beta. \end{aligned}

(iii) α<out⁡(Gs)<β\alpha< \operatorname{out}(G_s) < \beta. Let v=out⁡(Gs)v = \operatorname{out}(G_s), and let kk be the index at which this maximum is attained first, so that out⁡(Gs⋅xk)=v\operatorname{out}(G_{s\cdot x_k}) = v. Then out⁡(Gs⋅xi)≤v<β\operatorname{out}(G_{s\cdot x_i}) \le v < \beta for every ii, and out⁡(Gs⋅xi)<v\operatorname{out}(G_{s\cdot x_i}) < v for i<ki < k. Each index ii falls under one of the following cases:

(a) out⁡(Gs⋅xi)≤αi\operatorname{out}(G_{s\cdot x_i}) \le\alpha_i, and then, by IH (i), out⁡αi,β∗(Gs⋅xi)≤αi\operatorname{out}^{*}_{\alpha_i,\beta}(G_{s\cdot x_i}) \le\alpha_i;

(b) αi<out⁡(Gs⋅xi)\alpha_i < \operatorname{out}(G_{s\cdot x_i}), and then, since out⁡(Gs⋅xi)<β\operatorname{out}(G_{s\cdot x_i}) < \beta, IH (iii) gives out⁡αi,β∗(Gs⋅xi)=out⁡(Gs⋅xi)\operatorname{out}^{*}_{\alpha_i,\beta}(G_{s\cdot x_i}) = \operatorname{out}(G_{s\cdot x_i}).

Since α<v\alpha< v, induction on ii gives αi<v\alpha_i < v and out⁡αi,β∗(Gs⋅xi)<v\operatorname{out}^{*}_{\alpha_i,\beta}(G_{s\cdot x_i}) < v for every i<ki < k. Hence αk<v\alpha_k < v, so (b) applies to kk, and

out⁡αk,β∗(Gs⋅xk)=out⁡(Gs⋅xk)=v.\operatorname{out}^{*}_{\alpha_k,\beta}(G_{s\cdot x_k}) = \operatorname{out}(G_{s\cdot x_k}) = v.

For k<ik < i we have αi=v\alpha_i = v, and so out⁡αi,β∗(Gs⋅xi)≤v\operatorname{out}^{*}_{\alpha_i,\beta}(G_{s\cdot x_i}) \le v, by (a) or (b). Therefore no member of the list is ≥β\ge\beta, takeUntil⁡(≥β)\operatorname{takeUntil}(\ge\beta) keeps the whole list, and its maximum is vv, attained first at the index kk. Hence,

out⁡α,β∗(Gs)=ϕmax⁡∗(xs,p)(α,β)=max⁡(takeUntil⁡(≥β) ℓ(xs)(α,β))=L1max⁡(takeUntil⁡(≥β) […,out⁡αk,β∗(Gs⋅xk),…])=out⁡αk,β∗(Gs⋅xk)=out⁡(Gs⋅xk)=ϕmax⁡(λx.out⁡(Gs⋅x))=out⁡(Gs).\begin{aligned} \operatorname{out}^{*}_{\alpha,\beta}(G_s) &= \phi^{*}_{\max}(x_s,p)(\alpha,\beta)\\ &= \max(\operatorname{takeUntil}(\geq\beta)\,\ell(x_s)(\alpha,\beta))\\ &\mathrel{\overset{\mathrm{L1}}{=}} \max(\operatorname{takeUntil}(\geq\beta)\,[\ldots,\operatorname{out}^{*}_{\alpha_k,\beta}(G_s\cdot x_k),\ldots])\\ &= \operatorname{out}^{*}_{\alpha_k,\beta}(G_s\cdot x_k)\\ &= \operatorname{out}(G_s\cdot x_k)\\ &= \phi_{\max}(\lambda x.\operatorname{out}(G_s\cdot x))\\ &= \operatorname{out}(G_s). \end{aligned}

Corollary 1. (a) If α<q(t)<β\alpha< q(t) < \beta for every play tt, then out⁡α,β∗(G)=out⁡(G)\operatorname{out}^{*}_{\alpha,\beta}(G) = \operatorname{out}(G). (b) If α<out⁡α,β∗(G)<β\alpha< \operatorname{out}^{*}_{\alpha,\beta}(G) < \beta, then out⁡α,β∗(G)=out⁡(G)\operatorname{out}^{*}_{\alpha,\beta}(G) = \operatorname{out}(G).

Proof. (a) out⁡(G)\operatorname{out}(G) is one of the values q(t)q(t), so Theorem 1(iii) applies. (b) By Theorem 1(i) and (ii), out⁡(G)≤α\operatorname{out}(G) \leq\alpha and out⁡(G)≥β\operatorname{out}(G) \geq\beta are impossible, so (iii) applies. □\square

Computing Optimal Plays

In § 3 we showed how to use the following instance of the continuation monad

(X→TR)→TR,(X \to TR) \to TR,

with RR the type of game outcomes and TT 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 RR 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 RR 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 t=x0x1⋯xn−1t = x_0x_1 \cdots x_{n-1} of a game GG is optimal if out⁡(Gsk⋅xk)=out⁡(Gsk)\operatorname{out}(G_{s_k \cdot x_k}) = \operatorname{out}(G_{s_k}) for every k<nk < n, where sk=x0⋯xk−1s_k = x_0 \cdots x_{k-1}.

Corollary 2. Let G′G' be the game GG with the outcome function q′(t)=(q(t),t)q'(t) = (q(t), t), and with outcomes compared by their first components. Then out⁡(G′)=(out⁡(G),t)\operatorname{out}(G') = (\operatorname{out}(G), t) for an optimal play tt of GG. Hence, if α<out⁡(G)<β\alpha< \operatorname{out}(G) < \beta, then out⁡α,β∗(G′)=(out⁡(G),t)\operatorname{out}^{*}_{\alpha,\beta}(G') = (\operatorname{out}(G), t).

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 out⁡(Gs′)\operatorname{out}(G'_s) is out⁡(Gs)\operatorname{out}(G_s) for every partial play ss. The second claim follows from the first and Theorem 1(iii) for G′G'.

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

ϕi:Xi→KRX(i<n)\phi_i : X^i \to K_R X \qquad(i < n)

together with an outcome function q:Xn→Rq : X^n \to R. The product of quantifiers is then defined for every partial play ss of length i≤ni \le n by

(⨂sϕ)(q)≔{q(s)if i=n,ϕi(s)(λx.(⨂s⋅xϕ)(q))if i<n.\left(\bigotimes_s \phi\right)(q) \coloneqq \begin{cases} q(s) & \text{if } i = n, \\ \phi_i(s)\left(\lambda x.\left(\bigotimes_{s \cdot x} \phi\right)(q)\right) & \text{if } i < n. \end{cases}

and the optimal outcome is out⁡(G)≔(⨂[ ]ϕ)(q)\operatorname{out}(G) \coloneqq\left(\bigotimes_{[\,]} \phi\right)(q), as computed by the function hsequence of Appendix B. The monadic quantifiers ϕi∗:Xi→KTRX\phi_i^* : X^i \to K_{T_R}X give the monadic outcome out⁡α,β∗(G)\operatorname{out}_{\alpha,\beta}^*(G) in the same way.

For the results of Sections 4 and 5, each ϕi(s)\phi_i(s) is ϕmax⁡\phi_{\max} or ϕmin⁡\phi_{\min} applied to a non-empty list of the moves available at ss, and ϕi∗(s)\phi_i^*(s) is the corresponding ϕmax⁡∗\phi_{\max}^* or ϕmin⁡∗\phi_{\min}^*. With ϕi\phi_i the quantifier ϕi(s)\phi_i(s) at a partial play ss of length ii, the definitions of out⁡(Gs)\operatorname{out}(G_s) and out⁡α,β∗(Gs)\operatorname{out}_{\alpha,\beta}^*(G_s), 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 ss 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 ϕi(s)\phi_i(s) is ϕmax⁡(xs)\phi_{\max}(xs) for even ii and ϕmin⁡(xs)\phi_{\min}(xs) for odd ii, where xsxs is the list of the squares not in ss. 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 α\alpha and β\beta 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 9!9! = 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. [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. [2]Escardó, M., Oliva, P.: Selection functions, bar recursion, and backward induction. Mathematical Structures in Computer Science 20(2), 127–168 (2010).DOI
  3. [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. [4]Escardó, M., Oliva, P.: Sequential games and optimal strategies. Royal Society Proceedings A 467, 1519–1545 (2011)DOI
  5. [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. [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. [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. [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. [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. [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. [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

Paper details

vlnc:W_p24e8tca
Version
v1 · latest
Published
Fresh — added to the Hub within the last 30 days
Added
License
CC BY 4.0
Usage

views · 1

downloads · 0

A view counts once per browser per day, a download each time the PDF is requested.

  • Computer Science

Not recorded for this paper

View statement
Looking for related papers…
Contents