nLab state monad

Contents

Context

Type theory

natural deduction metalanguage, practical foundations

  1. type formation rule
  2. term introduction rule
  3. term elimination rule
  4. computation rule

type theory (dependent, intensional, observational type theory, homotopy type theory)

syntax object language

computational trinitarianism =
propositions as types +programs as proofs +relation type theory/category theory

logicset theory (internal logic of)category theorytype theory
propositionsetobjecttype
predicatefamily of setsdisplay morphismdependent type
proofelementgeneralized elementterm/program
cut rulecomposition of classifying morphisms / pullback of display mapssubstitution
introduction rule for implicationcounit for hom-tensor adjunctionlambda
elimination rule for implicationunit for hom-tensor adjunctionapplication
cut elimination for implicationone of the zigzag identities for hom-tensor adjunctionbeta reduction
identity elimination for implicationthe other zigzag identity for hom-tensor adjunctioneta conversion
truesingletonterminal object/(-2)-truncated objecth-level 0-type/unit type
falseempty setinitial objectempty type
proposition, truth valuesubsingletonsubterminal object/(-1)-truncated objecth-proposition, mere proposition
logical conjunctioncartesian productproductproduct type
disjunctiondisjoint union (support of)coproduct ((-1)-truncation of)sum type (bracket type of)
implicationfunction set (into subsingleton)internal hom (into subterminal object)function type (into h-proposition)
negationfunction set into empty setinternal hom into initial objectfunction type into empty type
universal quantificationindexed cartesian product (of family of subsingletons)dependent product (of family of subterminal objects)dependent product type (of family of h-propositions)
existential quantificationindexed disjoint union (support of)dependent sum ((-1)-truncation of)dependent sum type (bracket type of)
logical equivalencebijection setobject of isomorphismsequivalence type
support setsupport object/(-1)-truncationpropositional truncation/bracket type
n-image of morphism into terminal object/n-truncationn-truncation modality
equalitydiagonal function/diagonal subset/diagonal relationpath space objectidentity type/path type
completely presented setsetdiscrete object/0-truncated objecth-level 2-type/set/h-set
setset with equivalence relationinternal 0-groupoidBishop set/setoid with its pseudo-equivalence relation an actual equivalence relation
equivalence class/quotient setquotientquotient type
inductioncolimitinductive type, W-type, M-type
higher inductionhigher colimithigher inductive type
-0-truncated higher colimitquotient inductive type
coinductionlimitcoinductive type
presettype without identity types
set of truth valuessubobject classifiertype of propositions
domain of discourseuniverseobject classifiertype universe
modalityclosure operator, (idempotent) monadmodal type theory, monad (in computer science)
linear logic(symmetric, closed) monoidal categorylinear type theory/quantum computation
proof netstring diagramquantum circuit
(absence of) contraction rule(absence of) diagonalno-cloning theorem
synthetic mathematicsdomain specific embedded programming language

homotopy levels

semantics

Modalities, Closure and Reflection

Contents

Idea

The state monad is the name for the monad in computer science which is used to implement the functionality of read/write on a global “mutable state” (a global variable) in the context of functional programming languages.

A functional program with

  1. input of (data-)type XX,

  2. output of type YY

  3. and “mutable state” of type WW

    (e.g. the state of a “random access memory” device, cf. Yates (2019), p. 26 & Fig. 1.10)

is clearly a function (morphism) of function type between the product types with WW (also known as a Mealy machine, see there and cf. Oliveira & Miraldo 2016, p. 462):

prog:X×WY×W. prog \;\colon\; X \times W \longrightarrow Y \times W \,.

Now, under the hom-isomorphism of the (Cartesian product \dashv internal hom)-adjunction, this is equivalently given by its adjunct, which is a function into the function type [,][-,-] (internal hom):

X[W,W×Y]. X \longrightarrow [W, W \times Y ] \,.

Here the operation [W,W×()][W, W\times (-)] is the monad on the type system which is induced by the above adjunction; and this latter function is naturally regarded as a morphism in the Kleisli category of this monad.

In the context of monads in computer science, this is called the state monad for mutable states of type WW, such as for a data type WW of Random-Access-Memory:

Definition

Concretely, with the underlying functor being

WState:D[W,W×D] W State \;\colon\; D \,\mapsto\, \big[W,\, W \times D \big]

the join operation of the WW-state monad is given by evaluation on the intermediate WW-variable, hence the bind operation is given as follows:

To see more in detail how this encodes access to a global variable w:Ww \colon W in the sense of programming languages consider the following operations which isolate the processes of reading from and writing to that variable, respectively:

read : WState(W) read w(w,w)write : WWState(*) write w(ww) \array{ read &\colon& W State(W) \\ read &\equiv& w \mapsto (w,w) } \;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\; \array{ write &\colon& W \to W State(\ast) \\ write &\equiv& w \mapsto (w' \mapsto w) }

With these, for instance the procedure

inc : State(*) inc nn+1 \array{ inc &\colon& \mathbb{N}State(\ast) \\ inc &\equiv& n \mapsto n+1 }

which first reads-in the state of a global natural number variable and then overwrites it by its increment may be constructed as shown (on the right in do-notation) here:

(This is essentially the example from Benton, Hughes & Moggi 2002 p. 68 & 71)

Properties

Realization in dependent type theory

In a locally Cartesian closed category/dependent type theory H\mathbf{H}, then to every type WW is associated its base change adjoint triple

H /W WW * WH. \mathbf{H}_{/W} \stackrel{\overset{\sum_W}{\longrightarrow}}{\stackrel{\overset{W^\ast}{\longleftarrow}}{\underset{\prod_W}{\longrightarrow}}} \mathbf{H} \,.

In terms of this the state monad is the composite

State= WW * WW * State = \prod_W W^\ast \sum_W W^\ast

of context extension followed by dependent sum, followed by context extension, followed by dependent product.

Here WW *=[W,]\prod_W W^\ast = [W,-] is called the function monad or reader monad and WW *=W×()\sum_W W^\ast = W \times (-) is the coreader comonad.

References

Original discussion of the state/side-effect monad as a monad in computer science:

Exposition:

On the modules (“algebras”) of the state monad:

Concrete applications via implementation in Haskell:

Lecture notes:

In the generality of the local state monad:

Explicit understanding of Kleisli morphisms for the state moand as Mealy machines:

  • José Nuno Oliveira, Victor Cacciari Miraldo, p. 462 in: A practical approach to state-based system calculi, Journal of Logical and Algebraic Methods in Programming 85 4 (2016) 449-474 [doi:10.1016/j.jlamp.2015.11.007]

and (almost):

  • Marco Perone, Georgios Karachalias, Composable Representable Executable Machines [arXiv:2307.09090]

Last revised on August 28, 2023 at 10:39:32. See the history of this page for a list of all contributions to it.