Last active
September 12, 2023 17:18
-
-
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 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 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