-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtype_classes.h
180 lines (140 loc) · 4.5 KB
/
type_classes.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#ifndef _TYPE_CLASSES_H_
#define _TYPE_CLASSES_H_
#include <type_traits>
//! \todo Default implementations?
//! \todo Compile time checking of requirements
//! \note Need a Rational type to implement Fractional, Integral, and Real
namespace prelude{
template<typename T>
struct Eq{
//bool operator==(T,T)
//bool operator!=(T,T)
//(x == y) = !(x != y)
static constexpr bool is_valid = false;
};
enum Ordering{
LT = -1,
EQ,
GT
};
template<typename T>
struct Ord : public Eq<T>{
//Ordering compare(T,T)
//bool operator<, operator<=, operator>=, operator>(T,T)
//T max,min(T,T)
static constexpr bool is_valid = false;
};
//! \todo implement Show and Read
//! \todo decide if Enum should be implemented
template<typename T>
struct Bounded{
//T minBound,maxBound()
static constexpr bool is_valid = false;
};
template<typename T>
struct Num : public Eq<T>{
//T operator+, operator-, operator*(T,T)
//T negate(T)
//T abs, signum(T)
//! \todo Figure out what Integer should be
static constexpr bool is_valid = false;
};
//! \note is not a subclass of Fractional since we haven't implemented Fractional
//! \note ** is not a valid operator in C++ so we use pow
template<typename T>
struct Floating{
//T pi()
//T exp, log, sqrt(T)
//T pow, logBase(T,T)
//T sin, cos, tan(T)
//T asin, acos, atan(T)
//T sinh, cosh, tanh(T)
//T asinh, acosh, atanh(T)
static constexpr bool is_valid = false;
};
template<template <typename> class T>
struct Functor{
//T<V> fmap(V(U), T<U>)
//fmap(id,x) = id(x)
//fmap(compose(g,h),x) = compose(fmap(g,x),fmap(h,x))
static constexpr bool is_valid = false;
};
//! \note <*> is not a valid operator in C++ so we use ap
template<template <typename> class T>
struct Applicative : public Functor<T>{
//T<U> pure(U)
//T<V> ap(T<V(U)>, T<U>)
//ap(pure(id), x) = x
//ap(pure(f), pure(x)) = pure(f(x))
//ap(u,pure(y)) = ap(pure($ y), u) I'm not actually sure about this
//ap(u,ap(v,w)) = ap(ap(ap(pure(compose),u),v),w)
static constexpr bool is_valid = false;
};
template<typename T, template <typename> class M>
typename std::enable_if<Applicative<M>::is_valid, M<T>>
pure(T a){
return M<T>(a);
}
//! \todo Implement Transformers at some point(after figuring out what they are)
template<template <typename> class T>
struct Monad : public Applicative<T>{
//T<V> operator>>=(T<U>, T<V>(U))
//T<V> operator>>(T<U>, T<V>)
//T<U> pure(U)
//pure(a) >>= k = k(a)
//m >>= pure = m
//m >>= [](auto x){k(x) >>= h} = (m >>= k) >>= h
//fmap(f, xs) = xs >>= compose(pure, f)
static constexpr bool is_valid = false;
};
template<typename T, typename U, template <typename> class M>
M<U> operator>>(M<T> a, M<U> b){
return a >>= [&](M<T> m){return b;};
}
template<typename T>
struct Semigroup{
//T mappend(T,T)
//T sconcat(std::vector<T>) non-empty input
//T times1p(int, T)
//mappend(mappend(a,b),c) = mappend(a,mappend(b,c))
static constexpr bool is_valid = false;
};
template<typename T>
struct Monoid{
//T mappend(T, T)
//T mempty()
//T mconcat(std::vector<T>)
//mappend(mempty(), x) = x
//mappend(x, mempty()) = x
//mappend(mappend(x,y),z) = mappend(x,mappend(y,z))
static constexpr bool is_valid = false;
};
template<template <typename> class T>
struct Foldable{
//Monoid<U> => U fold(T<M>)
//Monoid<U> => U foldMap(U(V), T<V>, U)
//V foldr(V(U,V), V, T<U>)
//U foldl(U(U,V), U, T<V>)
//U foldr1(U(U,U), T<U>)
//U foldl1(U(U,U), T<U>)
static constexpr bool is_valid = false;
};
template<template <typename> class T>
struct Traversable : public Functor<T>, public Foldable<T>{
//Applicative<W> => W<T<V> > traverse(W<V>(U), T<U>)
//Applicative<W> => W<T<U> > sequenceA(T<W<U> >)
//Monad<R> => R<T<V> > mapM(R<V>(U), T<U>)
//Monad<R> => R<T<U> > sequence(T<R<U> >)
//I don't even know them laws
static constexpr bool is_valid = false;
};
//! \todo Implement a Category and Arrow typeclass. They're scary
template<template <typename> class T>
struct Comonad : public Functor<T>{
//U extract(T<U>)
//T<T<U> > duplicate(T<U>)
//T<V> extend(V(T<U>), T<U>)
static constexpr bool is_valid = false;
};
}
#endif //_TYPE_CLASSES_H_