Skip to content

Instantly share code, notes, and snippets.

@graninas
Last active September 12, 2023 17:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save graninas/ad194e732f382fb282d037c404da0ee0 to your computer and use it in GitHub Desktop.
Save graninas/ad194e732f382fb282d037c404da0ee0 to your computer and use it in GitHub Desktop.
Path 1 to implement the State monad in C++ using Church Free
# This is a sample of how to implement the State monad with Church Free monad.
# This sample is incomplete, but its concept is taken from my Free-monad based STM library.
# Consider the library to implement the rest of the code. The idea is the same.
# https://github.com/graninas/cpp_stm_free
#include <functional>
#include <variant>
#include <any>
using Any = std::any;
using Unit = int;
template <typename A, typename Next>
struct Put
{
A val;
std::function<Next(Unit)> next;
};
template <typename A, typename Next>
struct Get
{
std::function<Next(A)> next;
};
template <typename Next>
using PutA = Put<Any, Next>;
template <typename Next>
using GetA = Get<Any, Next>;
template <class Ret>
struct STMF
{
std::variant<
GetA<Ret>,
PutA<Ret>
> stmf;
};
template <typename A>
using StmlFunc = std::function<
Any(
std::function<Any(A)>,
std::function<Any(STMF<Any>)>
)>;
template <typename A>
struct STML
{
StmlFunc<A> runF;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment