Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用C+表示通用的一元接口(如Monad类)+; 是否甚至可以表达一个单元格“C++”? 我开始写这样的东西,但还是坚持: #include <iostream> template <typename a, typename b> struct M; template <typename a, typename b> struct M { virtual M<b>& operator>>( M<b>& (*fn)(M<a> &m, const a &x) ) = 0; }; template <typename a, typename b> struct MSome : public M<a> { virtual M<b>& operator>>( M<a>& (*fn)(M<a> &m, const a &x) ) { return fn(*this, x); } private: a x; }; M<int, int>& wtf(M<int> &m, const int &v) { std::cout << v << std::endl; return m; } int main() { // MSome<int> v; // v >> wtf >> wtf; return 0; } #包括 模板结构M; 模板结构{ 虚拟M&M运营商>>(M&(*fn)(M&M,常数a&x))=0; }; 模板 结构MSome:public M{ 虚拟M&M运营商>>(M&(*fn)(M&M,const a&x)){ 返回fn(*此,x); } 私人: a x; }; M&wtf(M&M、const int&v){ 标准::cout-wtf>>wtf; 返回0; } 但是,由于缺乏多态性,实际上它可能是我的非主流C++,因为我8年前使用过它。也许可以用一些新的C++特征来表达一般的单数接口,比如类型推理。它只是为了好玩,为了解释单子对非哈希和非数学家。 < P>我会这样做: template<class T> class IO { public: virtual T get() const=0; }; template<class T, class K> class C : public IO<K> { public: C(IO<T> &io1, IO<K> &io2) : io1(io1), io2(io2) { } K get() const { io1.get(); return io2.get(); } private: IO<T> &io1; IO<K> &io2; }; int main() { IO<float> *io = new YYYY; IO<int> *io2 = new XXX; C<float,int> c(*io, *io2); return c.get(); } 模板 IO类{ 公众: 虚拟T get()常量=0; }; 模板 C类:公共IO{ 公众: C(IO&io1,IO&io2):io1(io1),io2(io2){ K get()常量{ io1.get(); 返回io2.get(); } 私人: IO&io1; IO&io2; }; int main(){ IO*IO=新的YYYY; IO*io2=新的XXX; C(*io,*io2); 返回c.get(); }_C++_Haskell_Monads - Fatal编程技术网

用C+表示通用的一元接口(如Monad类)+; 是否甚至可以表达一个单元格“C++”? 我开始写这样的东西,但还是坚持: #include <iostream> template <typename a, typename b> struct M; template <typename a, typename b> struct M { virtual M<b>& operator>>( M<b>& (*fn)(M<a> &m, const a &x) ) = 0; }; template <typename a, typename b> struct MSome : public M<a> { virtual M<b>& operator>>( M<a>& (*fn)(M<a> &m, const a &x) ) { return fn(*this, x); } private: a x; }; M<int, int>& wtf(M<int> &m, const int &v) { std::cout << v << std::endl; return m; } int main() { // MSome<int> v; // v >> wtf >> wtf; return 0; } #包括 模板结构M; 模板结构{ 虚拟M&M运营商>>(M&(*fn)(M&M,常数a&x))=0; }; 模板 结构MSome:public M{ 虚拟M&M运营商>>(M&(*fn)(M&M,const a&x)){ 返回fn(*此,x); } 私人: a x; }; M&wtf(M&M、const int&v){ 标准::cout-wtf>>wtf; 返回0; } 但是,由于缺乏多态性,实际上它可能是我的非主流C++,因为我8年前使用过它。也许可以用一些新的C++特征来表达一般的单数接口,比如类型推理。它只是为了好玩,为了解释单子对非哈希和非数学家。 < P>我会这样做: template<class T> class IO { public: virtual T get() const=0; }; template<class T, class K> class C : public IO<K> { public: C(IO<T> &io1, IO<K> &io2) : io1(io1), io2(io2) { } K get() const { io1.get(); return io2.get(); } private: IO<T> &io1; IO<K> &io2; }; int main() { IO<float> *io = new YYYY; IO<int> *io2 = new XXX; C<float,int> c(*io, *io2); return c.get(); } 模板 IO类{ 公众: 虚拟T get()常量=0; }; 模板 C类:公共IO{ 公众: C(IO&io1,IO&io2):io1(io1),io2(io2){ K get()常量{ io1.get(); 返回io2.get(); } 私人: IO&io1; IO&io2; }; int main(){ IO*IO=新的YYYY; IO*io2=新的XXX; C(*io,*io2); 返回c.get(); }

用C+表示通用的一元接口(如Monad类)+; 是否甚至可以表达一个单元格“C++”? 我开始写这样的东西,但还是坚持: #include <iostream> template <typename a, typename b> struct M; template <typename a, typename b> struct M { virtual M<b>& operator>>( M<b>& (*fn)(M<a> &m, const a &x) ) = 0; }; template <typename a, typename b> struct MSome : public M<a> { virtual M<b>& operator>>( M<a>& (*fn)(M<a> &m, const a &x) ) { return fn(*this, x); } private: a x; }; M<int, int>& wtf(M<int> &m, const int &v) { std::cout << v << std::endl; return m; } int main() { // MSome<int> v; // v >> wtf >> wtf; return 0; } #包括 模板结构M; 模板结构{ 虚拟M&M运营商>>(M&(*fn)(M&M,常数a&x))=0; }; 模板 结构MSome:public M{ 虚拟M&M运营商>>(M&(*fn)(M&M,const a&x)){ 返回fn(*此,x); } 私人: a x; }; M&wtf(M&M、const int&v){ 标准::cout-wtf>>wtf; 返回0; } 但是,由于缺乏多态性,实际上它可能是我的非主流C++,因为我8年前使用过它。也许可以用一些新的C++特征来表达一般的单数接口,比如类型推理。它只是为了好玩,为了解释单子对非哈希和非数学家。 < P>我会这样做: template<class T> class IO { public: virtual T get() const=0; }; template<class T, class K> class C : public IO<K> { public: C(IO<T> &io1, IO<K> &io2) : io1(io1), io2(io2) { } K get() const { io1.get(); return io2.get(); } private: IO<T> &io1; IO<K> &io2; }; int main() { IO<float> *io = new YYYY; IO<int> *io2 = new XXX; C<float,int> c(*io, *io2); return c.get(); } 模板 IO类{ 公众: 虚拟T get()常量=0; }; 模板 C类:公共IO{ 公众: C(IO&io1,IO&io2):io1(io1),io2(io2){ K get()常量{ io1.get(); 返回io2.get(); } 私人: IO&io1; IO&io2; }; int main(){ IO*IO=新的YYYY; IO*io2=新的XXX; C(*io,*io2); 返回c.get(); },c++,haskell,monads,C++,Haskell,Monads,C++类型系统的功能不足以抽象更高级的类型,但由于模板是鸭型的,您可以忽略这一点,只需单独实现各种单子,然后将单子操作表示为SFINAE模板。虽然很难看,但它是最好的 这个评论真是太棒了。我一次又一次地看到人们试图使模板专门化“协变”和/或滥用继承。无论是好是坏,我认为面向概念的泛型编程更明智。这里有一个快速演示,它将使用C++11功能,以简洁明了,尽管它应该可以实现在C++03中实现相同的功能: (*:要获得竞争性意见,请参考我引文中的“丑陋,但最好!) #包括 #包括 //SFINAE实用程

C++类型系统的功能不足以抽象更高级的类型,但由于模板是鸭型的,您可以忽略这一点,只需单独实现各种单子,然后将单子操作表示为SFINAE模板。虽然很难看,但它是最好的

这个评论真是太棒了。我一次又一次地看到人们试图使模板专门化“协变”和/或滥用继承。无论是好是坏,我认为面向概念的泛型编程更明智。这里有一个快速演示,它将使用C++11功能,以简洁明了,尽管它应该可以实现在C++03中实现相同的功能:

(*:要获得竞争性意见,请参考我引文中的“丑陋,但最好!)

#包括
#包括
//SFINAE实用程序
模板结构void_{using type=void;};
模板使用Void=typename Void\:type;
/*
*在理想情况下,std::result_of只会起作用,而不是所有这些。
*将此视为一次写入(直到STD::ReultToof是固定的),使用许多
*情况。
*/    
{}的模板结构结果_;
模板
结构结果<
F(Args…)

VoIDAT第一个视图,如果用一个<代码>操作器()()<< /代码>(也更现代C++),将得到更好的结果。。这可以由一元类型接受的类型参数化,因此类将接受
作为模板参数,并使用类型
T
M
。但是,甚至可以实现实际的一元函数(a->M b)吗没有a和b的多态性?如何在没有void*的情况下表达这种多态性?是否有可能?C++类型系统的功能不足以抽象更高级的类型,但由于模板是鸭型的,您可以忽略这一点,只需单独实现各种单子,然后将单子操作表示为SFINAE模板。很难看,但效果最好。在谷歌上搜索“c++中的monads”会得到一些结果,例如and(最终导致)。您的
get()
操作不是monadic(实际上是一个comonadic操作)。您需要提供
bind()
操作(您似乎正在
get()中尝试执行此操作)
实现…但是
io1
的结果应该提供给
io2
),以及
pure()
操作(通常称为
return
,但显然这是C++中的一个关键字)。或者您可以提供
fmap()
join()
,而不是
bind()
。不可以提供get()对用户完全隐藏。应仅使用C的构造函数。只有main()需要调用get()。
#include <utility>
#include <type_traits>

// SFINAE utility
template<typename...> struct void_ { using type = void; };
template<typename... T> using Void = typename void_<T...>::type;

/*
 * In an ideal world std::result_of would just work instead of all that.
 * Consider this as a write-once (until std::result_of is fixed), use-many
 * situation.
 */    
template<typename Sig, typename Sfinae = void> struct result_of {};
template<typename F, typename... Args>
struct result_of<
    F(Args...)
    , Void<decltype(std::declval<F>()(std::declval<Args>()...))>
> {
    using type = decltype(std::declval<F>()(std::declval<Args>()...));
};
template<typename Sig> using ResultOf = typename result_of<Sig>::type;

/*
 * Note how both template parameters have kind *, MonadicValue would be
 * m a, not m. We don't whether MonadicValue is a specialization of some M<T>
 * or not (or derived from a specialization of some M<T>). Note that it is
 * possible to retrieve the a in m a via typename MonadicValue::value_type
 * if MonadicValue is indeed a model of the proper concept.
 *
 * Defer actual implementation to the operator() of MonadicValue,
 * which will do the monad-specific operation
 */
template<
    typename MonadicValue
    , typename F
    /* It is possible to put a self-documenting assertion here
       that will *not* SFINAE out but truly result in a hard error
       unless some conditions are not satisfied -- I leave this out
       for brevity
    , Requires<
        MonadicValueConcept<MonadicValue>
        // The two following constraints ensure that
        // F has signature a -> m b
        , Callable<F, ValueType<MonadicValue>>
        , MonadicValueConcept<ResultOf<F(ValueType<MonadicValue>)>>
    >...
    */
>
ResultOf<MonadicValue(F)>
bind(MonadicValue&& value, F&& f)
{ return std::forward<MonadicValue>(value)(std::forward<F>(f)); }

// Picking Maybe as an example monad because it's easy
template<typename T>
struct just_type {
    using value_type = T;

    // Encapsulation omitted for brevity
    value_type value;

    template<typename F>
    // The use of ResultOf means that we have a soft contraint
    // here, but the commented Requires clause in bind happens
    // before we would end up here
    ResultOf<F(value_type)>
    operator()(F&& f)
    { return std::forward<F>(f)(value); }
};

template<typename T>
just_type<T> just(T&& t)
{ return { std::forward<T>(t) }; }

template<typename T>
just_type<typename std::decay<T>::type> make_just(T&& t)
{ return { std::forward<T>(t) }; }

struct nothing_type {
    // Note that because nothing_type and just_type<T>
    // are part of the same concept we *must* put in
    // a value_type member type -- whether you need
    // a value member or not however is a design
    // consideration with trade-offs
    struct universal { template<typename T> operator T(); };
    using value_type = universal;

    template<typename F>
    nothing_type operator()(F const&) const
    { return {}; }
};
constexpr nothing_type nothing;