-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcont.h
681 lines (565 loc) · 14.3 KB
/
cont.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
/**************************************************
**
** Copyright (c) 2015 TeamDrive Systems GmbH
**
** See the file LICENSE.txt for copying permission.
**
***************************************************/
#ifndef CONT_H
#define CONT_H
#include <functional>
// fold aka accumulate
#include <numeric>
#include <QtGlobal>
// decltype call expression
#if __GNUC__ > 4 || \
(__GNUC__ == 4 && (__GNUC_MINOR__ > 8 || \
(__GNUC_MINOR__ == 8 && __GNUC_PATCHLEVEL__ > 1)))
#define HAS_DECLTYPE_CALLEXPR
#endif
#if __clang_major__ > 3 || \
(__clang_major__ == 3 && (__clang_minor__ >= 4))
#define HAS_DECLTYPE_CALLEXPR
#endif
#if _MSC_VER >= 1600
#define HAS_DECLTYPE_CALLEXPR
#endif
// the empty struct. reason, void can be complicated as template parameter.
struct Unit {};
// Functor class
template<template <typename> class F, typename B>
struct Functor {
typedef B ValueT;
template<typename A>
static F<B> fmap(std::function<B(A)> f, F<A> s);
};
template<template <typename> class F, typename A, typename B>
F<B> fmap(std::function<B(A)> f, F<A> s)
{
return Functor<F,B>::fmap(f, s);
}
#ifdef HAS_DECLTYPE_CALLEXPR
template<template <typename> class M, typename A, typename F>
auto fmap (M<A> s, F f) -> M<decltype(f(A()))>
{
return fmap(std::function<decltype(f(A()))(A)>(f), s);
}
#endif
// Monad class
template<template <typename> class M, typename B>
struct Monad {
typedef B ValueT;
static M<B> pure(B);
template<typename A>
static M<B> bind(M<A>, std::function<M<B>(A)>);
};
template<template <typename> class M, typename A>
M<A> pure(A x)
{
return Monad<M,A>::pure(x);
}
template<template <typename> class M, typename A, typename B>
M<B> operator >>= (M<A> s, std::function<M<B>(A)> f)
{
return Monad<M,B>::bind(s, f);
}
#ifdef HAS_DECLTYPE_CALLEXPR
template<template <typename> class M, typename A, typename F>
auto operator >>= (M<A> s, F f) -> decltype(f(A()))
{
return s >>= std::function<decltype(f(A()))(A)>(f);
}
#endif
//join :: (Monad m) => m (m a) -> m a
//join x = x >>= id
template <template <typename> class M, typename A>
M<A> join(const M<M<A>> x)
{
return x >>= std::function<M<A>(M<A>)>([](M<A> y) -> M<A>{
return y;
});
}
// (>>) :: forall a b. m a -> m b -> m b
// m >> k = m >>= \_ -> k
// If you get this error here:
template<template <typename> class M, typename A, typename B>
M<B> operator >> (M<A> m, std::function<M<B>()> k)
{
return (m >>= std::function<M<B>(A)>([k](A) -> M<B> {
return k();
}));
}
#ifdef HAS_DECLTYPE_CALLEXPR
template<template <typename> class M, typename A, typename F>
auto operator >> (M<A> m, F f) -> decltype(f())
{
return m >> std::function<decltype(f())()>(f);
}
#endif
// sequence :: (Monad m, Traversable t) => t (m a) -> m (t a)
template<template <typename> class M, template <typename> class T, typename A>
M<T<A>> sequence(T<M<A>> x)
{
// sequence ms = foldr k (return []) ms
// where
// k m mm = do { x <- m; xs <- mm; return (x:xs) }
return std::accumulate(std::begin(x), std::end(x), pure<M, T<A>>(T<A>()),
[](const M<T<A>>& mm, M<A> m) -> M<T<A>>{
return ( m >>= std::function<M<T<A>>(A)> ([mm](A x) -> M<T<A>> {
return (mm >>= std::function<M<T<A>>(T<A>)>([x ](T<A> xs) -> M<T<A>> {
xs += x;
return pure<M,T<A>>(xs);
}));}));});
}
// Promote a function to a monad.
// liftM :: (Monad m) => (a1 -> r) -> m a1 -> m r
template<template <typename> class M, typename A, typename R>
M<R> liftM(std::function<R(A)> f, M<A> m1)
{
// liftM f m1 = do { x1 <- m1; return (f x1) }
return m1 >>= std::function<M<R>(A)>([f](A x1) -> M<R>{
return pure<M,R>(f(x1));
});
}
// mapM :: (Monad m, Traversable t) => (a -> m b) -> t a -> m t b
template<template <typename> class M, template <typename> class T, typename A, typename B>
M<T<B>> mapM(std::function<M<B>(A)> f, T<A> as)
{
// mapM f as = sequence (map f as)
return sequence<M,T,B>(fmap<T,A,M<B>>(f, as));
}
//
// Continuation monad:
//
// Current limitation: you can return somehing from the monad. Currently, r equals void.
// data Cont r a = Cont { runCont :: (a -> r) -> r }
template<typename A>
struct Cont {
typedef std::function<void(A)> Inner;
typedef std::function<void(Inner)> Type;
Type m_Cont;
Cont(const Type& func);
Cont(); // dont use. Requires default constructor for A
bool operator == (const Cont<A>& rhs) const {return this == &rhs;}
inline Q_CONSTEXPR Type runCont() const;
template <typename F>
inline void runCont1(F f) const;
void evalCont() const;
};
//cont :: ((a -> r) -> r) -> Cont r a
template<typename A>
Cont<A>::Cont(const typename Cont<A>::Type &func):
m_Cont(func)
{
}
// :: Cont r a
template<typename A>
Cont<A>::Cont():
m_Cont([](typename Cont<A>::Inner f) -> void { f(A()); })
{
}
//runCont :: Cont r a -> (a -> r) -> r
template<typename A>
inline Q_CONSTEXPR typename Cont<A>::Type Cont<A>::runCont() const
{
return m_Cont;
}
//runCont1 = uncurry runCont
//runCont1 :: (Cont r a, (a -> r)) -> r
template<typename A>
template<typename F>
inline void Cont<A>::runCont1(F f) const
{
runCont()(f);
}
/*!
evalCont :: Cont r a -> r
the return value is typically empty.
*/
template<typename A>
void Cont<A>::evalCont() const
{
runCont1([](A) -> void {
});
}
//instance Monad (Cont r) where
template<typename B>
struct Monad<Cont, B>
{
// return x = cont ($ x)
// s >>= f = cont $ \c -> runCont s $ \x -> runCont (f x) c
static inline Q_CONSTEXPR Cont<B> pure(B x)
{
return Cont<B>([x](typename Cont<B>::Inner f) -> void {
f(x);
});
}
// (>>=) :: Cont a -> (a -> Cont b) -> Cont b
template<typename A>
static inline Q_CONSTEXPR Cont<B> bind(Cont<A> s, std::function<Cont<B>(A)> f)
{
return Cont<B>([f, s](std::function<void(B)> c) -> void {
s.runCont1([f, c](A x) -> void {
f(x).runCont1(c);
});
});
}
};
template<typename A>
Cont<A> abortContWith()
{
return Cont<A>(std::function<void(typename Cont<A>::Inner)>([](typename Cont<A>::Inner){
}));
}
// callCC :: ((a -> Cont r b) -> Cont r a) -> Cont r a
// callCC f = cont $ \h -> runCont (f (\a -> cont $ \_ -> h a)) h
template<typename A, typename B, typename F>
Cont<A> callCC(F /*std::function<Cont<A>(std::function<Cont<B>(A)>)>*/ f)
{
return Cont<A>(
// \h -> runCont (f (\a -> cont $ \_ -> h a)) h
[f](typename Cont<A>::Inner h) -> void {
// (f (\a -> cont $ \_ -> h a))
f(std::function<Cont<B>(A)>([h](A a) -> Cont<B> {
return Cont<B>([h, a](typename Cont<B>::Inner) -> void {
h(a);
});
})).runCont1(h);
}
);
}
//tryCont :: MonadCont m => ((err -> m a) -> m a) -> (err -> m a) -> m a
//tryCont c h = callCC $ \ok -> do
// err <- callCC $ \notOk -> do
// x <- c notOk
// ok x
// h err
template<typename A, typename ERR>
Cont<A> tryCont_(/*C*/ std::function<Cont<A>(std::function<Cont<A>(ERR)>)> c, std::function<Cont<A>(ERR)> h)
{
return callCC<A, ERR>([c, h](std::function<Cont<ERR>(A)> ok) -> Cont<A> {
return callCC<ERR, A>([ok, c](std::function<Cont<A>(ERR)> notOk) -> Cont<ERR> {
return c(notOk) >>= [ok](A x) -> Cont<ERR> {
return ok(x);
};
}) >>= [h](ERR error) -> Cont<A> {
return h(error);
};
});
}
template<typename A, typename ERR, typename C, typename H>
Cont<A> tryCont(C c, H h)
{
return tryCont_<A, ERR>(std::function<Cont<A>(std::function<Cont<A>(ERR)>)>(c), std::function<Cont<A>(ERR)>(h));
}
#include <QHash>
template<typename T>
inline uint qHash(const Cont<T> &c, uint seed = 0)
// Q_DECL_NOEXCEPT_EXPR(noexcept(qHash(c)))
{
return qHash((quintptr)seed ^ reinterpret_cast<quintptr>(&c));
}
//
// Async is a Cont + Reader Monad
//
struct AsyncException {
virtual ~AsyncException(){}
};
// newtype ReaderT r m a = ReaderT { runReaderT :: r -> m a }
// specialized for
// r == ThrowF
// m == Cont
template<typename A>
struct Async
{
typedef std::function<Cont<A>(AsyncException*)> ThrowF; // ThrowF aka R
typedef std::function<Cont<A>(ThrowF)> Type;
Async(const Type& func);
Async(); // dont use. Requires default constructor for A
Type runAsync() const;
Cont<A> runAsync1(const ThrowF& throwF) const;
// bool operator == (const Cont<A>& rhs) const {return this == &rhs;}
Async<ThrowF> ask();
static Async<A> raise(AsyncException* e);
template<typename H>
Cont<A> tryAsync(H handler);
Type m_Cont;
// void evalCont() const;
};
template<typename A, typename L>
Async<A> lambdaToAsync(L l)
{
return Async<A>(typename Async<A>::Type(l));
}
template<typename A>
Async<A> contToAsync(Cont<A> c)
{
return lambdaToAsync<A>([c](typename Async<A>::ThrowF){return c;});
}
template<typename A>
Async<A>::Async(const typename Async<A>::Type& func):
m_Cont(func)
{
}
template<typename A>
Async<A>::Async():
m_Cont()
{
}
template<typename A>
typename Async<A>::Type Async<A>::runAsync() const
{
return m_Cont;
}
template<typename A>
Cont<A> Async<A>::runAsync1(const ThrowF& throwF) const
{
return runAsync()(throwF);
}
//instance (Monad m) => Monad (ReaderT r m) where
// return a = ReaderT $ \_ -> return a
// s >>= f = ReaderT $ \r -> do
// a <- runReaderT s r
// runReaderT (f a) r
// specialized for
// r == ThrowF
// m == Cont
template<typename B>
struct Monad<Async, B>
{
static inline Q_CONSTEXPR Async<B> pure(B x)
{
return contToAsync<B>(Cont<B>([x](typename Cont<B>::Inner f) -> void {
f(x);
}));
// return contToAsync<B>(pure<Cont>(x));
}
// (>>=) :: Async a -> (a -> Async b) -> Async b
template<typename A>
static inline Q_CONSTEXPR Async<B> bind(Async<A> s, std::function<Async<B>(A)> f)
{
return Async<B>([s, f](typename Async<B>::ThrowF throwF) -> Cont<B>{
return s.runAsync1(throwF) >>= [f, throwF](A a){
return f(a).runAsync1(throwF);
};
});
}
};
// ask = ReaderT $ \x -> return x
template<typename A>
Async<typename Async<A>::ThrowF> Async<A>::ask()
{
return lambdaToAsync<ThrowF>([](Async<A>::ThrowF f){
return pure<Async>(f);
});
}
//template<typename A, typename ERR>
//Cont<A> tryCont_(/*C*/ std::function<Cont<A>(std::function<Cont<A>(ERR)>)> c, std::function<Cont<A>(ERR)> h)
template<typename A>
Cont<A> tryAsync_(Async<A> block, typename Async<A>::ThrowF handler)
{
return tryCont<A, AsyncException*>([block](typename Async<A>::ThrowF throwF) -> Cont<A> {
return block.runAsync1(throwF);
}
,
handler
);
}
template<typename A>
Async<A> Async<A>::raise(AsyncException* e)
{
return lambdaToAsync<A>([e](Async<A>::ThrowF throwF) {
return throwF(e);
});
}
template<typename A>
template<typename H>
Cont<A> Async<A>::tryAsync(H handler)
{
return tryAsync_(*this, typename Async<A>::ThrowF(handler));
}
//
// Monad instance for boost::optional:
//
#include <boost/optional.hpp>
template<typename B>
struct Monad<boost::optional, B>
{
// pure :: a -> Maybe a
static boost::optional<B> pure(B x)
{
return boost::optional<B>(x);
}
template<typename A>
static boost::optional<B> bind(boost::optional<A> s, std::function<boost::optional<B>(A)> f)
{
if (s) {
return f(s.get());
} else {
return boost::optional<B>();
}
}
};
template <class T>
inline QDebug operator<<(QDebug debug, const boost::optional<T> &m)
{
if (m) {
debug.nospace() << "boost::optional(" << m.get() << ")";
} else {
debug.nospace() << "Nothing()";
}
return debug.maybeSpace();
}
//
// Monad instance for QSet:
//
#include <QSet>
template<typename T>
inline uint qHash(const QSet<T> &s, uint seed);
// functor instance
template<typename B>
struct Functor<QSet, B>
{
template<typename A>
static QSet<B> fmap(std::function<B(A)> f, QSet<A> as)
{
QSet<B> bs;
foreach(const A& a, as) {
bs.insert(f(a));
}
return bs;
}
};
template<typename B>
struct Monad<QSet, B>
{
// pure :: a -> QSet a
static QSet<B> pure(B x)
{
return QSet<B>() << x;
}
template<typename A>
static QSet<B> bind(QSet<A> s, std::function<QSet<B>(A)> f)
{
QSet<B> ret;
foreach (const QSet<B>& ss, fmap(f, s)) {
ret.unite(ss);
}
return ret;
}
};
//
// Monad instance for QList:
//
#include <QList>
template<typename T>
inline uint qHash(const QList<T> &s, uint seed)
// Q_DECL_NOEXCEPT_EXPR(noexcept(qHash(s)))
{
uint hash = seed;
foreach(const T& t, s){
return (hash = hash ^ qHash(t, seed));
}
return hash;
}
// functor instance
template<typename B>
struct Functor<QList, B>
{
template<typename A>
static QList<B> fmap(std::function<B(A)> f, QList<A> as)
{
QList<B> bs;
std::transform(std::begin(as), std::end(as), std::back_inserter(bs), f);
return bs;
}
};
template<typename B>
struct Monad<QList, B>
{
// pure :: a -> [a]
static QList<B> pure(B x)
{
return QList<B>() << x;
}
template<typename A>
static QList<B> bind(QList<A> s, std::function<QList<B>(A)> f)
{
QList<B> ret;
foreach (const QList<B>& ss, fmap(f, s)) {
ret.append(ss);
}
return ret;
}
};
// ---------------------------------
template<typename T>
inline uint qHash(const QSet<T> &s, uint seed)
// Q_DECL_NOEXCEPT_EXPR(noexcept(qHash(s)))
{
uint hash = seed;
foreach(const T& t, s){
return (hash = hash ^ qHash(t, seed));
}
return hash;
}
// ---------------------------
Q_DECLARE_METATYPE(Unit)
typedef Cont<int> IntCont;
typedef Cont<Unit> VoidCont;
template<typename Q, typename S>
VoidCont waitForQObjectSignal0(Q* obj, S signal)
{
return VoidCont([obj, signal](VoidCont::Inner inner){
QObject* guard = new QObject();
QObject::connect(obj, signal, guard, [guard, inner]() -> void {
delete guard;
inner(Unit());
});
});
}
template<typename R, typename Q, typename S>
Cont<R> waitForQObjectSignal1(Q* obj, S signal, QObject* parent = 0)
{
return Cont<R>([obj, signal, parent](typename Cont<R>::Inner inner) -> void {
QObject* guard = new QObject(parent);
QObject::connect(obj, signal, guard, [guard, inner](R r) -> void {
delete guard;
inner(r);
});
});
}
template<typename R, typename Q, typename S, typename C>
Cont<R> waitForQObjectSignal1c(Q* obj, S signal, C execAfter, QObject* parent = 0)
{
std::function<void(void)> execAfter_(execAfter);
return Cont<R>([obj, signal, parent, execAfter_](typename Cont<R>::Inner inner) -> void {
QObject* guard = new QObject(parent);
QObject::connect(obj, signal, guard, [guard, inner](R r) -> void {
delete guard;
inner(r);
});
execAfter_();
});
}
#include <QEventLoop>
template<typename R>
R evalWithEventLoop(Cont<R> cont)
{
QEventLoop loop;
bool isSet = false;
R result;
(
cont >>= [&result, &loop, &isSet](R r) -> Cont<Unit>{
result = r;
isSet = true;
loop.exit();
return pure<Cont>(Unit());
}
).evalCont();
if (!isSet) {
loop.exec();
}
return result;
}
#endif // CONT_H