Skip to content

Instantly share code, notes, and snippets.

@graninas
Last active September 13, 2023 18:51
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/32a2cd52c062c42bccbad9e8b69b6360 to your computer and use it in GitHub Desktop.
Save graninas/32a2cd52c062c42bccbad9e8b69b6360 to your computer and use it in GitHub Desktop.
Path 2 to implement the State monad in C++ using Church Free
# This is an idea of how to merge the State monad and the Church Free monad
# without interferring with the inner eDSL.
# This code is incomplete and requires a further development.
# The idea is that we pass the state around with the Church Free functions
# as we would do it with a regular State monad.
# The overal concept is based on my Free monadic STM implementation:
# https://github.com/graninas/cpp_stm_free
#include <functional>
#include <variant>
#include <any>
#include <tuple>
using Any = std::any;
template <typename State>
using StateAny = std::tuple<State, Any>;
using Unit = int;
template <typename State, typename A, typename Next>
struct InnerLanguageMethod
{
std::function<Next(Unit)> next;
};
template <typename State, typename Next>
using InnerLanguageMethodA = InnerLanguageMethod<State, Any, Next>;
template <typename State, class Ret>
struct STMF
{
std::variant<
InnerLanguageMethodA<State, Ret>
> stmf;
};
template <typename State, typename A>
using StmlFunc = std::function<
StateAny<State>(
std::function<StateAny<State>(A)>,
std::function<StateAny<State>(STMF<State, Any>)>
)>;
template <typename State, typename A>
struct STML
{
StmlFunc<State, A> runF;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment