Last active
September 13, 2023 18:51
-
-
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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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