{"id":15952725,"url":"https://github.com/h0tk3y/kotlin-monads","last_synced_at":"2026-03-06T15:03:52.425Z","repository":{"id":68794822,"uuid":"75232566","full_name":"h0tk3y/kotlin-monads","owner":"h0tk3y","description":"Monads for Kotlin","archived":false,"fork":false,"pushed_at":"2018-10-22T12:08:16.000Z","size":91,"stargazers_count":120,"open_issues_count":2,"forks_count":8,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-10-12T18:20:34.346Z","etag":null,"topics":["coroutines","do-notation","functional-programming","kotlin","monad"],"latest_commit_sha":null,"homepage":"","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/h0tk3y.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2016-11-30T22:24:25.000Z","updated_at":"2024-08-25T04:51:14.000Z","dependencies_parsed_at":"2023-02-21T22:45:20.952Z","dependency_job_id":null,"html_url":"https://github.com/h0tk3y/kotlin-monads","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/h0tk3y/kotlin-monads","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/h0tk3y%2Fkotlin-monads","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/h0tk3y%2Fkotlin-monads/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/h0tk3y%2Fkotlin-monads/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/h0tk3y%2Fkotlin-monads/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/h0tk3y","download_url":"https://codeload.github.com/h0tk3y/kotlin-monads/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/h0tk3y%2Fkotlin-monads/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30182686,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-06T14:42:24.748Z","status":"ssl_error","status_checked_at":"2026-03-06T14:42:14.925Z","response_time":250,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["coroutines","do-notation","functional-programming","kotlin","monad"],"created_at":"2024-10-07T13:09:39.748Z","updated_at":"2026-03-06T15:03:52.400Z","avatar_url":"https://github.com/h0tk3y.png","language":"Kotlin","funding_links":[],"categories":[],"sub_categories":[],"readme":"# kotlin-monads\n\n[![](https://jitpack.io/v/h0tk3y/kotlin-monads.svg)](https://jitpack.io/#h0tk3y/kotlin-monads) [![](https://img.shields.io/badge/kotlin-1.1.0-blue.svg)](http://kotlinlang.org/)\n\nAn attempt to implement monads in Kotlin, deeply inspired by Haskell monads, but restricted within the Kotlin type system.\n\n## The monad type\n\nMonadic types are represented by the `Monad\u003cM, T\u003e` interface, \nwhere `M` **should be the type of the implementation** with only its `T` star-projected. Examples: `Maybe\u003cT\u003e : Monad\u003cMaybe\u003c*\u003e, T\u003e`, `State\u003cS, T\u003e : Monad\u003cState\u003cS, *\u003e, T\u003e`. \n\nWith `Monad` defined in this way, we\nare almost able to say in terms of the Kotlin type system that a function returns the same `Monad` implementation but \nwith a different type argument `R` instead of `T`:\n\n    fun \u003cT, R, M : Monad\u003cM, *\u003e\u003e Monad\u003cM, T\u003e.map(f: (T) -\u003e R) = bind { returns(f(it)) }\n\n    val m = just(3).map { it * 2 } as Maybe\n    \nWe still need the downcast `as Maybe`, but at least it's checked.\n\n## Usage\n\nAdd as a dependency:\n\n    repositories {\n        ...\n        maven { url 'https://jitpack.io' }\n    }\n    \n    dependencies {\n        ...\n        compile 'com.github.h0tk3y:kotlin-monads:0.5'\n    }\n\nSee the usage examples in [tests](https://github.com/h0tk3y/kotlin-monads/tree/master/src/test/kotlin/com/github/h0tk3y/kotlinMonads).\n\n## How to implement a monad\n\n`Monad\u003cM, T\u003e` is defined as follows:\n\n    interface Return\u003cM\u003e {\n        fun \u003cT\u003e returns(t: T): Monad\u003cM, T\u003e\n    }\n\n    interface Monad\u003cThis, out T\u003e {\n        infix fun \u003cR\u003e bind(f: Return\u003cThis\u003e.(T) -\u003e Monad\u003cThis, R\u003e): Monad\u003cThis, R\u003e\n    }\n    \nThe monad implementation should only provide one function `bind` (Haskell: `\u003e\u003e=`), \nno separate `return` is there, instead, if you look at the signature of `bind`, you'll see that the function to bind with is `f: Return\u003cThis\u003e.(T) -\u003e Monad\u003cThis, R\u003e`. \nIt means that a `Monad\u003cM, T\u003e` implementation should provide the `Return\u003cM\u003e` as well and pass it to `f` each time, so that inside `f` its `returns` could be used:\n\n    just(3) bind { returns(it * it) }\n    \nThere seems to be no direct equivalent to Haskell `return`, which could be used outside any context like `bind` blocks. Outside the `bind` blocks, you should either\nwrap the values into your monads manually or require a `Return\u003cM\u003e`, which can wrap `T` into `Monad\u003cM, T\u003e` for you. \n\nMind the [monad laws](https://wiki.haskell.org/Monad_laws). A correct monad implementation follows these three rules (rephrased in terms of `kotlin-monads`):\n\n1. **Left identity**: `returns(x) bind { f(it) }` should be equivalent to `f(x)`\n \n2. **Right identity**: `m bind { returns(it) }` should be equivalent to `m`\n\n3. **Associativity**: `m bind { f(it) } bind { g(it) }` should be equivalent to `m bind { f(it) bind { g(it) } }`\n\nAlso, it's good to make the return type of `bind` narrower, e.g. `bind` of `Maybe\u003cT\u003e` would rather return `Maybe\u003cR\u003e` than `Monad\u003cMaybe\u003c*\u003e, R\u003e`, it allows not to cast \nthe result of a `bind` called on a known monad type.\n\n    val m = monadListOf(1, 2, 3) bind { monadListOf(\"$it\", \"$it\") } // already `MonadList\u003cString\u003e`, no need to cast\n\nExample implementation:\n\n    sealed class Maybe\u003cout T\u003e : Monad\u003cMaybe\u003c*\u003e, T\u003e {\n        class Just\u003cT\u003e(val value: T) : Maybe\u003cT\u003e()\n        class None : Maybe\u003cNothing\u003e()\n\n        override fun \u003cR\u003e bind(f: Binder\u003cMaybe\u003c*\u003e, T, R\u003e): Maybe\u003cR\u003e = when (this) {\n            is Just -\u003e f(MaybeReturn, value) as Maybe\n            is None -\u003e None()\n        }\n    }\n\n    object MaybeReturn : Return\u003cMaybe\u003c*\u003e\u003e {\n        override fun \u003cT\u003e returns(t: T) = Maybe.Just(t)\n    }\n\n## Monads implementations bundled\n\n* `Maybe\u003cT\u003e`\n* `Either\u003cF, T\u003e`\n* `MonadList\u003cT\u003e`\n* `Reader\u003cE, T\u003e`\n* `Writer\u003cT\u003e` (no monoid for now, just `String`)\n* `State\u003cS, T\u003e`\n\n## Do notation\n\nWith the power of Kotlin coroutines, we can even have an equivalent of the [*Haskell do notation*](https://en.wikibooks.org/wiki/Haskell/do_notation):\n\nSimple example that performs a monadic list nondeterministic expansion:\n\n    val m = doReturning(MonadListReturn) {\n        val x = bind(monadListOf(1, 2, 3))\n        val y = bind(monadListOf(x, x + 1))\n        monadListOf(y, x * y)\n    }\n    \n    assertEquals(monadListOf(1, 1, 2, 2, 2, 4, 3, 6, 3, 9, 4, 12), m)\n    \nOr applied to an existing monad for convenience:\n\n    val m = monadListOf(1, 2, 3).bindDo { x -\u003e\n        val y = bind(monadListOf(x, x + 1))\n        monadListOf(y, x * y)\n    }\n    \nThis is effectively equivalent to the following code written with only simple `bind`:\n\n    val m = monadListOf(1, 2, 3).bind { x -\u003e\n        monadListOf(x, x + 1).bind { y -\u003e \n            monadList(y, x * y)\n        }\n    }\n    \nNote that, with simple `bind`, each *transformation* requires another inner scope if it uses the variables bound outside, \nwhich would lead to some kind of callback hell. \nThis problem is effectively solved using the Kotlin coroutines: the compiler performs the CPS transformation of a plain\n code block under the hood. However, this coroutines use case is somewhat out of conventions: it might resume the same continuation\n several times and uses quite a dirty hack to do that.\n    \nThe result type parameter (`R` in `Monad\u003cM, R\u003e`) is usually inferred, and the compiler controls the flow inside a *do block*, but still you need to\n downcast the `Monad\u003cM, R\u003e` to your actual monad type (e.g. `Monad\u003cMaybe\u003c*\u003e, R\u003e` to `Maybe`), because the type system doesn't seem to allow this to be done\n automatically (if you know a way, please tell me).\n \n **Be careful with mutable state** in _do_ blocks, since all continuation calls will share it, sometimes resulting into counter-intuitive results:\n \n     val m = doReturning(MonadListReturn) {\n         for (i in 1..10)\n             bind(monadListOf(0, 0))\n         returns(0)\n     } as MonadList\n     \n One would expect 1024 items here, but the result only contains 11! That's because `i` is mutable and is shared between all the calls that `bind` makes.\n \n \n \n \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fh0tk3y%2Fkotlin-monads","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fh0tk3y%2Fkotlin-monads","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fh0tk3y%2Fkotlin-monads/lists"}