{"id":26076871,"url":"https://github.com/purescript-contrib/purescript-machines","last_synced_at":"2026-02-04T10:07:19.706Z","repository":{"id":21536246,"uuid":"24855745","full_name":"purescript-contrib/purescript-machines","owner":"purescript-contrib","description":"Finite state machines, including Mealy machines, for modeling computations","archived":false,"fork":false,"pushed_at":"2022-04-27T21:03:36.000Z","size":196,"stargazers_count":37,"open_issues_count":4,"forks_count":16,"subscribers_count":7,"default_branch":"main","last_synced_at":"2025-06-18T05:42:24.058Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"PureScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/purescript-contrib.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-10-06T17:17:52.000Z","updated_at":"2024-03-07T00:31:52.000Z","dependencies_parsed_at":"2022-08-30T11:11:39.906Z","dependency_job_id":null,"html_url":"https://github.com/purescript-contrib/purescript-machines","commit_stats":null,"previous_names":[],"tags_count":27,"template":false,"template_full_name":null,"purl":"pkg:github/purescript-contrib/purescript-machines","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/purescript-contrib%2Fpurescript-machines","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/purescript-contrib%2Fpurescript-machines/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/purescript-contrib%2Fpurescript-machines/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/purescript-contrib%2Fpurescript-machines/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/purescript-contrib","download_url":"https://codeload.github.com/purescript-contrib/purescript-machines/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/purescript-contrib%2Fpurescript-machines/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262179158,"owners_count":23271174,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-03-09T02:36:40.425Z","updated_at":"2026-02-04T10:07:14.668Z","avatar_url":"https://github.com/purescript-contrib.png","language":"PureScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Machines\n\n[![CI](https://github.com/purescript-contrib/purescript-machines/workflows/CI/badge.svg?branch=main)](https://github.com/purescript-contrib/purescript-machines/actions?query=workflow%3ACI+branch%3Amain)\n[![Release](http://img.shields.io/github/release/purescript-contrib/purescript-machines.svg)](https://github.com/purescript-contrib/purescript-machines/releases)\n[![Pursuit](http://pursuit.purescript.org/packages/purescript-machines/badge)](http://pursuit.purescript.org/packages/purescript-machines)\n[![Maintainer: paluh](https://img.shields.io/badge/maintainer-paluh-teal.svg)](http://github.com/paluh)\n\nMachines is a library for building finite state machines. Finite state machines are useful for modeling many concerns that developers face. They help you describe a set of possible states and rules for how to transition from one state to another. The result is a complete view of possible states and transitions between states.\n\nCurrently this library implements [Mealy machines](https://en.wikipedia.org/wiki/Mealy_machine) with halting.\n\n## Installation\n\nInstall `machines` with [Spago](https://github.com/purescript/spago):\n\n```sh\nspago install machines\n```\n\n## Quick start\n\nMealy machines are finite state machines. The `MealyT f i o` type represents a machine where `f` is the effect used for evaluation, `i` is the input, and `o` is the output value. The examples here use `Identity` as the effect type for simplicity, but you would usually use a different `Monad` such as `Effect`, `Aff`, or `State`.\n\nThere are several ways to build machines. One way is to use `do` syntax,\nfor example:\n\n```purescript\nimport Prelude\n\nimport Control.MonadZero (guard)\nimport Data.Machine.Mealy (MealyT, fromArray, toUnfoldable)\nimport Data.Identity (Identity)\n\nmachine1 :: MealyT Identity Unit String\nmachine1 = do\n  number \u003c- fromArray [10, 20, 30, 40, 50, 0, 60, 70]\n  guard (number /= 0)\n  let scaled = div number 2\n  pure $ show scaled\n```\n\nThis will create a machine `machine1` which goes through the \"inputs\"\nfrom the array. It then checks and halts on any zero input, and otherwise\nscales the inputs (by dividing by 2). The result is then transformed into a string.\n\nThe resulting machine can be materialized via\n\n```purescript\n\u003e toUnfoldable unit machine1 :: Array String\n[\"5\",\"10\",\"15\",\"20\",\"25\"]\n```\n\nAnother way to write the same machine is using machine composition. In this example, we will be creating multiple machines using `pureMealy`, which relies on `Step`s.\n\nA `Step f i o` represents a state transition in the machine. When you run a machine you are executing a series of steps. At each step the machine can stop via the `Halt` constructor or `Emit` a value and construct the rest of the machine.\n\n```purescript\nimport Prelude\n\nimport Data.Identity (Identity)\nimport Data.Machine.Mealy (MealyT, Step(..), fromArray, pureMealy)\n\nmachine2 :: MealyT Identity Unit String\nmachine2 =\n    fromArray [10, 20, 30, 40, 50, 0, 60, 70]\n        \u003e\u003e\u003e pureMealy haltOn0\n        \u003e\u003e\u003e pureMealy scale\n        \u003e\u003e\u003e pureMealy pretty\n  where\n    haltOn0 :: Int -\u003e Step Identity Int Int\n    haltOn0 0 = Halt\n    haltOn0 n = Emit n $ pureMealy haltOn0\n\n    scale :: Int -\u003e Step Identity Int Int\n    scale n = Emit (n `div` 2) $ pureMealy scale\n\n    pretty :: Int -\u003e Step Identity Int String\n    pretty n = Emit (show n) $ pureMealy pretty\n```\n\nThis machine does the same thing, except it creates multiple machines:\n\n- `fromArray [10, 20 ...` is a `MealyT Identity Unit Int` which generates\n    the integerers in the provided array,\n- `pureMealy haltOn0` is a `MealyT Int Int` which halts on 0,\n- `pureMealy scale` is a `MealyT Int Int` which scales the inputs, and\n- `pureMealy pretty` is a `MealyT Int String` which converts inputs\n    from integers to strings.\n\n## Documentation\n\n`machines` documentation is stored in a few places:\n\n1. Module documentation is [published on Pursuit](https://pursuit.purescript.org/packages/purescript-machines).\n2. Written documentation is kept in [the docs directory](./docs).\n3. Usage examples can be found in [the test suite](./test).\n\nIf you get stuck, there are several ways to get help:\n\n- [Open an issue](https://github.com/purescript-contrib/purescript-machines/issues) if you have encountered a bug or problem.\n- Ask general questions on the [PureScript Discourse](https://discourse.purescript.org) forum or the [PureScript Discord](https://purescript.org/chat) chat.\n\n## Contributing\n\nYou can contribute to `machines` in several ways:\n\n1. If you encounter a problem or have a question, please [open an issue](https://github.com/purescript-contrib/purescript-machines/issues). We'll do our best to work with you to resolve or answer it.\n\n2. If you would like to contribute code, tests, or documentation, please [read the contributor guide](./CONTRIBUTING.md). It's a short, helpful introduction to contributing to this library, including development instructions.\n\n3. If you have written a library, tutorial, guide, or other resource based on this package, please share it on the [PureScript Discourse](https://discourse.purescript.org)! Writing libraries and learning resources are a great way to help this library succeed.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpurescript-contrib%2Fpurescript-machines","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpurescript-contrib%2Fpurescript-machines","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpurescript-contrib%2Fpurescript-machines/lists"}