Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Higher-kinded Types with C++

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. List<A> can have a reverse function that works on any element type (the A).

  • 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

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); } 
like image 872
Venkat Shiva Avatar asked Apr 02 '10 05:04

Venkat Shiva


People also ask

What are higher Kinded types Haskell?

A higher kinded type is a concept that reifies a type constructor as an actual type. A type constructor can be thought of in these analogies: like a function in the type universe. as a type with a "hole" in it.

Why are there high Kinded types?

Higher-kinded types are useful when we want to create a container that can hold any type of items; we don't need a different type for each specific content type.

Does typescript have higher Kinded types?

HKTs are a powerful abstraction. Just as there are different types of higher-order functions, so are there so-called 'higher-kinded types'.

What is a kind in Haskell?

From HaskellWiki. Wikipedia says, "In type theory, a kind is the type of a type constructor or, less commonly, the type of a higher-order type operator.


1 Answers

Template-template parameters?

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);     } }; 
like image 113
kennytm Avatar answered Oct 17 '22 04:10

kennytm