Indiana Unversity logo[ConceptGCC]

ConceptGCC :

Re: Haskell type constructor classes and ConceptC++

From: Doug Gregor (dgregor_at_[hidden])
Date: 2007-05-03 15:14:50


On Apr 30, 2007, at 7:06 PM, Marcin Zalewski wrote:
> 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.

That makes two of us :)

We want recursive constraints to work, and we're pretty certain that
we understand what the will. However, Ron Garcia noted how Haskell
implements recursive constraints, so concepts should follow their
lead. We're not there yet in ConceptGCC...

> 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.

Should the return type of "ret" be computed as a function of the
argument type? If so, you can do essentially what you do with bind...
placing constraints on the argument type, and using an associated
type for the result type.

> 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++.

I'm not sure how that would be expressed. However, my understanding
of Haskell is quite weak.

        - Doug