Skip to content

Instantly share code, notes, and snippets.

@bryant
Last active August 29, 2015 13:57
Show Gist options
  • Save bryant/9498327 to your computer and use it in GitHub Desktop.
Save bryant/9498327 to your computer and use it in GitHub Desktop.
rank-2 polymorphism, c++ edition.
#include <iostream>
#include <vector>
#include <typeinfo>
using std::vector;
template <typename T>
const char *print_type() { return __PRETTY_FUNCTION__; }
struct LengthOf {
template <typename T>
int operator () (const vector<T> &w) { /* [w] -> Int */
std::cout << print_type<vector<T>>() << std::endl;
return static_cast<int>(w.size());
}
};
template <typename F, typename U, typename V>
bool same(F f, U u, V v) { /* Eq t => (forall f. f -> t) -> u -> v -> Bool */
return f(u) == f(v);
}
int main() {
std::cout << same(LengthOf(), vector<char> {'n', 'y', 'c'},
vector<int> {2, 1}) << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment