高级类型与C ++ [英] Higher-kinded Types with C++
问题描述
这个问题是对于知道Haskell(或任何其他功能语言,支持高级类型)和C ++ ...的人。
使用C ++模板的更高类型的类型?如果是,那么如何?
编辑:
http://www.blumenfeld-maso.com/2010/01/what-does-monad-mean-by-tony-morris/\">此由Tony Morris介绍:
高阶多态性:
-
C#有
一阶多态性,因为他们
允许我们在类型上抽象。例如
List< A>
可以有一个reverse
函数
,更实用的编程语言
和类型系统(
A
) -
此功能称为高阶
-kinded)多态性。
<允许我们将类型构造函数的
抽象为
。
b $ b 具有用于高阶多态性的发明符号的伪Java 模板参数? / p> This question is for the people who know both Haskell (or any other functional language that supports Higher-kinded Types) and C++... Is it possible to model higher kinded types using C++ templates? If yes, then how? EDIT : From this presentation by Tony Morris: Higher-order Polymorphism : Languages such as Java and C# have
first-order polymorphism because they
allow us to abstract on types. e.g.
More practical programming languages
and type systems allow us to
abstract on type constructors as
well. This feature is called higher-order
(or higher-kinded) polymorphism. Example : Pseudo-Java with an invented notation for higher-order polymorphism
Template-template parameters?
这篇关于高级类型与C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
interface Transformer< X,Y& {
Y transform(X x);
}
interface Monad< M> {// M :: * - > *
< A> M A纯(A a);
< A,B> M B bind(Transformer< A,M B> t,M A a);
}
模板< template< typename>类m>
struct Monad {
template< typename a>
static m< a> mreturn(const a&);
template< typename a,typename b>
static m< b> mbind(const m a& m b(*)(const a&));
};
template< typename a>
struct Maybe {
bool isNothing;
a value;
};
模板<>
struct Monad< Maybe> {
template< typename a>
static Maybe< a> mreturn(const a& v){
Maybe< a> X;
x.isNothing = false;
x.value = v;
return x;
}
template< typename a,typename b>
static Maybe< b> mbind(const Maybe< a& action,Maybe< b>(* function)(const a&)){
if(action.isNothing)
return action;
else
return function(action.value);
}
};
List<A>
can have a reverse
function
that works on any element type (the
A
).interface Transformer<X, Y> {
Y transform(X x);
}
interface Monad<M> { // M :: * -> *
<A> M<A> pure(A a);
<A, B> M<B> bind(Transformer<A, M<B>> t, M<A> a);
}
template <template <typename> class m>
struct Monad {
template <typename a>
static m<a> mreturn(const a&);
template <typename a, typename b>
static m<b> mbind(const m<a>&, m<b>(*)(const a&));
};
template <typename a>
struct Maybe {
bool isNothing;
a value;
};
template <>
struct Monad<Maybe> {
template <typename a>
static Maybe<a> mreturn(const a& v) {
Maybe<a> x;
x.isNothing = false;
x.value = v;
return x;
}
template <typename a, typename b>
static Maybe<b> mbind(const Maybe<a>& action, Maybe<b>(*function)(const a&)) {
if (action.isNothing)
return action;
else
return function(action.value);
}
};