[ConceptGCC] |
ConceptGCC :Haskell type constructor classes and ConceptC++ |
From: Marcin Zalewski (zalewski_at_[hidden])
Date: 2007-04-30 19:06:55
I have been thinking for some time whether Haskell type classes such as
Monad can be translated into C++ concept. The Monad class looks like that:
class Monad m where
(>>=) :: forall a b . m a -> (a -> m b) -> m b
return :: a -> m a
More details can be found at
http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Monad.html
The meaning of the particular operations is not very important here.
What matters is that Monad class is defined for type *constructors*,
i.e., types of kind (*->*). What this allows one to do is:
1. To define operations that return a type dependent on the type of
argument (like return in Monad).
2. One can define operations such as (>>=) that talk about the same
monad around two different values, here (m a) and (m b).
My first try in writing a Monad looks like that:
concept Monad<typename M>
{
typename return_monad;
requires Monad<return_monad>;
typename value_type;
template<typename A>
return_monad ret(A& a);
template<typename MA, typename F>
requires Monad<MA>,
std::Callable1<F, Monad<MA>::value_type>,
Monad<std::Callable1<F, Monad<MA>::value_type>::result_type>
std::Callable1<F, Monad<MA>::value_type>::result_type
bind(const M& m);
};
First of all, I am not sure how and if recursive constraints will work.
Then there are two other problems. The first problem is that one cannot
say what will be the return type of "ret" before the template argument
is supplied. The second problem is that it would be nice to say that
bind has to return the same monad as one started with, only around
different value. It is possible to do that in Haskell but I wonder how
to do that in C++.
Has anyone on the list thought about such concepts? I would appreciate
any insights.
Marcin