{
  "id": "mealy-machine",
  "title": "Mealy Machine",
  "lang": "en",
  "source_lang": "en",
  "summary": "A Mealy machine is a finite-state computational model whose output at each step depends on both its current state and its current input.",
  "category": "computer-science",
  "subcategory": "algorithms-theory",
  "type": "concept",
  "aliases": [
    "Mealy automaton"
  ],
  "generated_at": "2026-11-05",
  "model": "pro/gpt-6.1-sol",
  "url": "https://aiwiki.page/en/mealy-machine/",
  "translations": {
    "zh-Hans": {
      "url": "https://aiwiki.page/zh/mealy-machine/",
      "json": "https://aiwiki.page/zh/mealy-machine.json"
    }
  },
  "sections": [
    {
      "heading": "",
      "text": "A Mealy machine is a finite-state machine that produces an output determined jointly by its current state and the input it receives. Each input symbol causes a state transition and produces an output symbol, so outputs are conventionally attached to transitions rather than to states. Mealy machines are used to describe digital controllers, communication protocols, sequence detectors, and other systems that process inputs using a finite amount of memory. The model is named after George H. Mealy, whose 1955 paper “A Method for Synthesizing Sequential Circuits” presented this approach to sequential circuit design."
    },
    {
      "heading": "Formal definition",
      "text": "A deterministic Mealy machine is commonly defined as a six-tuple\n\n\\[M=(Q,\\Sigma,\\Gamma,\\delta,\\lambda,q_0),\\]\n\nwhere:\n\n- \\(Q\\) is a finite, nonempty set of states.\n- \\(\\Sigma\\) is a finite input alphabet.\n- \\(\\Gamma\\) is a finite output alphabet.\n- \\(\\delta:Q\\times\\Sigma\\to Q\\) is the transition function.\n- \\(\\lambda:Q\\times\\Sigma\\to\\Gamma\\) is the output function.\n- \\(q_0\\in Q\\) is the initial state.\n\nFor an input sequence \\(a_1a_2\\cdots a_n\\), the states and outputs satisfy\n\n\\[q_i=\\delta(q_{i-1},a_i), \\qquad b_i=\\lambda(q_{i-1},a_i), \\quad 1\\leq i\\leq n.\\]\n\nThus, the output \\(b_i\\) depends on the state before the transition and the input symbol responsible for that transition.\n\nIn the standard definition, both functions are total: every state–input pair has a specified next state and output. Partial Mealy machines leave some pairs undefined, for example when describing an incompletely specified controller or an interface with restricted legal inputs.\n\nA standard Mealy machine produces exactly one output symbol per input symbol. Its input–output behavior therefore defines a length-preserving function\n\n\\[f_M:\\Sigma^*\\to\\Gamma^*,\\]\n\nwhere \\(\\Sigma^*\\) and \\(\\Gamma^*\\) denote the sets of finite strings over the respective alphabets. The empty input produces the empty output. Unlike a recognizer for a formal language, a Mealy machine does not inherently require accepting or rejecting states."
    },
    {
      "heading": "Representation and operation",
      "text": "State diagrams and tables\n\nIn a state diagram, states are vertices and transitions are directed edges. An edge labeled \\(a/b\\) means: on input \\(a\\), produce output \\(b\\) and follow the edge to its destination state.\n\nA transition table gives the same information by listing, for every state and input, a pair consisting of the next state and the output. These representations separate the machine’s two central operations:\n\n- Updating memory: choosing the next state.\n- Producing a response: choosing the output for the current input.\n\nThe state summarizes the relevant input history. Different histories may lead to the same state when their distinctions no longer matter to future behavior.\n\nExample: detecting the sequence 101\n\nConsider a machine that reads a stream of binary symbols and outputs \\(1\\) whenever the latest input completes the pattern \\(101\\). It outputs \\(0\\) otherwise, and permits overlapping occurrences.\n\nThree states suffice:\n\n- \\(A\\): no nonempty suffix of the input so far matches a proper prefix of \\(101\\).\n- \\(B\\): the longest such matching suffix is \\(1\\).\n- \\(C\\): the longest such matching suffix is \\(10\\).\n\nThe initial state is \\(A\\).\n\nCurrent state | Input 0: next state/output | Input 1: next state/output\n\\(A\\) | \\(A/0\\) | \\(B/0\\)\n\\(B\\) | \\(C/0\\) | \\(B/0\\)\n\\(C\\) | \\(A/0\\) | \\(B/1\\)\n\nFor the input \\(10101\\), the output is \\(00101\\). After detecting \\(101\\), the machine enters \\(B\\), because the final \\(1\\) can also begin another occurrence. This preserves the information needed to recognize overlapping matches without storing the entire input history."
    },
    {
      "heading": "Relationship to other computational models",
      "text": "Moore machines\n\nA Moore machine differs in how it determines outputs. Its output function depends only on the current state:\n\n\\[g:Q\\to\\Gamma.\\]\n\nIn a Mealy machine, the output function depends on both state and input:\n\n\\[\\lambda:Q\\times\\Sigma\\to\\Gamma.\\]\n\nConsequently, a Mealy machine can produce different outputs for different inputs while remaining in the same state. A Moore machine must use states with different output labels to represent such differences.\n\nThe two models can represent the same finite-state input–output behavior when output timing and the initial output are aligned appropriately:\n\n- A Moore machine can be converted into a Mealy machine by labeling each transition with the output of its destination state, if outputs are compared after each input is processed.\n- A Mealy machine can be converted into a Moore machine by splitting destination states according to the outputs on incoming transitions. The resulting states can be represented as pairs \\((q,b)\\), recording both the destination state and the output just produced.\n\nThe second construction can increase the number of states. It also requires an explicit convention for the Moore machine’s output before the first input, because a standard Mealy machine has no corresponding initial output.\n\nClaims that one model responds “one clock cycle faster” depend on implementation and observation conventions, not on an unconditional difference in computational power.\n\nFinite-state transducers and Turing machines\n\nA Mealy machine is a restricted finite-state transducer. More general transducers may produce empty strings or multiple output symbols on a transition, include transitions that consume no input, or allow nondeterministic choices.\n\nIts memory is limited to its finite state set. Unlike a Turing machine, it has no unbounded work tape. It cannot, for example, maintain an exact counter of arbitrary size or recognize arbitrarily nested structures without a fixed bound.\n\nDespite this limitation, Mealy machines can process streams of unbounded length: the restriction concerns retained memory, not how many input symbols may be processed."
    },
    {
      "heading": "Digital circuit implementation",
      "text": "In synchronous hardware, a Mealy controller is commonly implemented using:\n\n- A state register, typically built from flip-flops.\n- Combinational logic that computes the next state from the current state and inputs.\n- Combinational logic that computes outputs from the current state and inputs.\n\nFor \\(N\\) states, a binary state encoding requires at least\n\n\\[\\left\\lceil\\log_2 N\\right\\rceil\\]\n\nstate bits. Other encodings, including one-hot encoding, trade additional storage elements for different logic and timing characteristics.\n\nIn an unregistered Mealy implementation, an input change can affect an output before the next clock edge, after propagation through the output logic. This can provide a direct response to an input, but it also exposes outputs to input instability and combinational glitches. Asynchronous inputs require suitable synchronization or interface circuitry; the abstract machine itself does not model metastability or electrical timing.\n\nRegistering the outputs changes their timing and must be reflected in the specification. Whether a Mealy or Moore implementation is preferable depends on state count, logic depth, timing constraints, and interface requirements. Neither model is inherently superior for every circuit.\n\nElectronic design automation tools can synthesize hardware descriptions of these controllers into registers and logic gates."
    },
    {
      "heading": "Equivalence and minimization",
      "text": "Two states of a deterministic Mealy machine are behaviorally equivalent if every finite future input sequence produces the same output sequence from either state. Equivalent states may be merged without changing observable behavior.\n\nA standard minimization procedure uses partition refinement:\n\n- Remove states unreachable from the initial state.\n- Group states that produce the same output for every individual input symbol.\n- Split a group whenever two of its states transition, on the same input, into different groups.\n- Repeat until no further split is possible.\n\nThe resulting equivalence classes become the states of a reduced machine. For reachable, completely specified deterministic Mealy machines, the minimal realization of a given behavior is unique up to renaming states.\n\nMachine equivalence can also be checked by exploring pairs of states in a product construction. If a reachable pair produces different outputs for some input, the input sequence leading to that discrepancy is a distinguishing test. This supports formal verification and the comparison of implementations with their specifications."
    },
    {
      "heading": "Applications and limitations",
      "text": "Mealy machines are useful when responses depend on both a finite interaction history and the next input. Applications include:\n\n- Sequence detection: recognizing patterns in serial data streams.\n- Digital control: generating control signals from a controller’s state and current inputs.\n- Protocol modeling: describing how a participant responds to messages in different protocol states.\n- Software testing: generating input sequences and checking expected output traces.\n- Interface modeling: representing bounded request–response behavior.\n\nThey can also serve as models for model checking, although practical specifications often add data variables, timing constraints, or environmental assumptions.\n\nThe principal limitation is finite memory. Encoding many independent variables directly as states can produce a very large state space. Moreover, a basic Mealy machine does not express real-time deadlines, probability, continuous physical quantities, or unrestricted concurrency. Such features require extensions or other modeling frameworks.\n\nThe interpretation of an input or output symbol is another important modeling choice. A symbol may represent a single electrical bit, a vector of signals, or an abstract event such as receiving a message. Correctness at the abstract level does not by itself establish that electrical timing, message transport, or environmental assumptions are satisfied."
    }
  ],
  "links": [
    "finite-state-machine",
    "formal-language",
    "turing-machine",
    "flip-flop",
    "combinational-logic",
    "one-hot-encoding",
    "electronic-design-automation",
    "logic-gate",
    "formal-verification",
    "model-checking"
  ],
  "sources": [],
  "license": "https://creativecommons.org/licenses/by/4.0/"
}