{"id":25900405,"url":"https://github.com/kabeech/monads-are-easy","last_synced_at":"2025-03-03T02:16:43.416Z","repository":{"id":280340929,"uuid":"941669776","full_name":"kaBeech/monads-are-easy","owner":"kaBeech","description":"A brief cheatsheet for monads and related concepts","archived":false,"fork":false,"pushed_at":"2025-03-03T02:14:08.000Z","size":54,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-03T02:16:40.699Z","etag":null,"topics":["category-theory","endofunctors","functional-programming","functor","functors","haskell","haskell-learning","monad","monads","monoid","monoids","type-theory"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kaBeech.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,"publiccode":null,"codemeta":null}},"created_at":"2025-03-02T20:29:46.000Z","updated_at":"2025-03-03T02:14:12.000Z","dependencies_parsed_at":"2025-03-02T21:39:18.351Z","dependency_job_id":null,"html_url":"https://github.com/kaBeech/monads-are-easy","commit_stats":null,"previous_names":["kabeech/monad-notes","kabeech/monads-are-easy"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kaBeech%2Fmonads-are-easy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kaBeech%2Fmonads-are-easy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kaBeech%2Fmonads-are-easy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kaBeech%2Fmonads-are-easy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kaBeech","download_url":"https://codeload.github.com/kaBeech/monads-are-easy/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241596277,"owners_count":19988044,"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":["category-theory","endofunctors","functional-programming","functor","functors","haskell","haskell-learning","monad","monads","monoid","monoids","type-theory"],"created_at":"2025-03-03T02:16:42.575Z","updated_at":"2025-03-03T02:16:43.399Z","avatar_url":"https://github.com/kaBeech.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Monads Are Easy!\n\nI made this when the ideas of monads and related concepts \"fully\" clicked in my head.\n\nI hope this helps show how simple these concepts really are!\n\nMy goal is not to provide a complete introduction to monads, but to summarize the\nimportant points in a way that is easy to understand for someone who already has some\nexperience working with or learning about monads.\n\nFor a more complete introduction, please see the [sources](#Sources) below. Alternatively,\nask your local functional programming nerd/type theorist and they likely have even better\nresources to recommend!\n\n## Summary\n\nA monadic type is the base type plus some extra information/effects.\n\nHaving a monadic type `M a` simply means that you can make some functions that take an `a`\nand return an `a`, potentially with some extra information/effects that can be safely ignored\nwhile still guaranteeing some essential properties (i.e. associativity, identity, and\ntotality*).\n\n*Totality is redundant here, as it's already implied by taking and returning an `a`.\n\n## Images\n\n\"A monad (in `a`) is a monoid in the category of endofunctors (of `a`)\":\n\n![monads_monoids](https://github.com/user-attachments/assets/4d4e2715-8fca-4eb9-9536-9e9fe0f0e956)\n\nMonoids:\n\n![monoids](https://github.com/user-attachments/assets/d13d4c3a-18a3-440f-8126-1fb48700215b)\n\nMonads:\n\n![monads](https://github.com/user-attachments/assets/4a0465fc-cad7-44e4-a2cd-989ef464cb7a)\n\n## Compact Pseudo-Haskell\n\n    type Functor a b = a -\u003e b\n\n    type Endofunctor a = a -\u003e a\n\n    type Monad (a) = Monoid (a -\u003e a)\n\n    type Monoid a\n        where\n          op :: a -\u003e a -\u003e a\n            where\n              op(x, op(y, z)) == op(op(x, y), z)\n          id :: a\n            where\n              op(id, x) == x\n              op(x, id) == x\n\n    TypeConstructor :: a -\u003e M a\n    TypeConstructor x = M x\n\n    unit :: a -\u003e M a\n        where\n            unit(x) \u003e\u003e= f == f(x)\n            M x \u003e\u003e= unit == M x\n            \n    `\u003e\u003e=` :: M a -\u003e (a -\u003e M b) -\u003e M b\n    M x \u003e\u003e= f = f(x) :: M b\n        where\n            M x \u003e\u003e= (\\x' -\u003e (f(x') \u003e\u003e= g)) == (M x \u003e\u003e= f) \u003e\u003e= g\n\n## Verbose Pseudo-Haskell\n\n    type Functor a b = a -\u003e b\n\n    type Endofunctor a = a -\u003e a\n\n    -- (Potentially) extra info/effects!\n    --                          |\n    --                          v  \n    type Monad (a) = Monoid (a -\u003e a)\n\n    type Monoid a\n        where\n          -- Totality (per type definition)\n          op :: a -\u003e a -\u003e a\n            where\n              -- Associativity\n              op(x, op(y, z)) == op(op(x, y), z)\n          id :: a\n            where\n              -- Left identity\n              op(id, x) == x\n              -- Left identity\n              op(x, id) == x\n\n    -- Monads must implement\n    TypeConstructor :: a -\u003e M a\n    TypeConstructor x = M x\n\n    -- Monads must implement.\n    -- AKA type converter or return.\n    -- Congruent to id in Monoid\n    unit :: a -\u003e M a\n        where\n            -- Left identity\n            unit(x) \u003e\u003e= f == f(x)\n            -- Right identity\n            M x \u003e\u003e= unit == M x\n\n    -- Monads must implement.\n    -- AKA combinator, map, or flatmap.\n    -- Congruent to op in Monoid.\n    -- Totality (per type definition)   \n    bind :: M a -\u003e (a -\u003e M b) -\u003e M b\n    bind (M x) f = f(x) :: M b\n        where\n            -- Associativity\n            bind (M x) (\\x' -\u003e (bind (f(x')) g)) == bind (bind (M x) f) g\n\n    -- Alternative bind - infix of above\n    -- Totality (per type definition)   \n    `\u003e\u003e=` :: M a -\u003e (a -\u003e M b) -\u003e M b\n    M x \u003e\u003e= f = f(x) :: M b\n        where\n            -- Associativity\n            M x \u003e\u003e= (\\x' -\u003e (f(x') \u003e\u003e= g)) == (M x \u003e\u003e= f) \u003e\u003e= g\n            \n    -- Example: Function returning a monadic type.\n    -- Gives the first element of a list with at least\n    -- one element.\n    -- See note* below if this doesn't make sense\n    safeHead :: [a] -\u003e Maybe a\n    safeHead [] = Nothing\n    safeHead (x : _) = Just x\n\n    -- Example: Chaining binds.\n    -- Both these examples give the second element of a \n    -- list with at least two elements\n    safeNeck x = safeHead x \u003e\u003e= safeHead\n    safeNeck' x = bind (safeHead x) safeHead\n\n*For more information on the Maybe monad, see pretty much any introductory source on monads below, like [Learn You A Haskell](https://learnyouahaskell.github.io/a-fistful-of-monads.html#getting-our-feet-wet-with-maybe) or [Wikipedia](https://en.wikipedia.org/wiki/Monad_(functional_programming)#Overview)\n\n## Sources\n\nSome of these sources are more reliable than others, but I've found them all helpful in gaining a more complete understanding\n\n- https://github.com/haskell/mtl\n- https://github.com/haskell/random\n- https://github.com/ekmett/free\n- https://hackage.haskell.org/package/ghc-internal-9.1201.0/docs/src/GHC.Internal.Data.Maybe.html#maybe\n- https://learnyouahaskell.github.io/\n- https://en.wikibooks.org/wiki/Write_Yourself_a_Scheme_in_48_Hours\n- https://www.youtube.com/watch?v=ENo_B8CZNRQ\n- https://www.youtube.com/watch?v=VgA4wCaxp-Q\n- https://en.wikipedia.org/wiki/Monad_(functional_programming)\n- https://en.wikipedia.org/wiki/Monad_(category_theory)\n- https://en.wikipedia.org/wiki/Monoid\n- https://wiki.haskell.org/index.php?title=Monad\n- https://www.youtube.com/watch?v=t1e8gqXLbsU\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkabeech%2Fmonads-are-easy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkabeech%2Fmonads-are-easy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkabeech%2Fmonads-are-easy/lists"}