2013-11-22 17:40:18 +00:00
|
|
|
/*
|
|
|
|
Copyright (c) 2013 Microsoft Corporation. All rights reserved.
|
|
|
|
Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
|
|
|
|
Author: Leonardo de Moura
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <utility>
|
2013-11-23 23:54:26 +00:00
|
|
|
#include "util/interrupt.h"
|
2013-11-22 17:40:18 +00:00
|
|
|
#include "util/lazy_list.h"
|
|
|
|
#include "util/list.h"
|
|
|
|
|
|
|
|
namespace lean {
|
2013-11-23 23:33:25 +00:00
|
|
|
template<typename T, typename F>
|
|
|
|
void for_each(lazy_list<T> l, F && f) {
|
|
|
|
while (true) {
|
|
|
|
auto p = l.pull();
|
|
|
|
if (p) {
|
|
|
|
f(p->first);
|
|
|
|
l = p->second;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
2013-11-23 23:54:26 +00:00
|
|
|
check_interrupted();
|
2013-11-22 17:40:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-24 00:27:36 +00:00
|
|
|
/**
|
|
|
|
\brief Create a lazy list that contains the first \c sz elements in \c l.
|
|
|
|
*/
|
2013-11-23 23:33:25 +00:00
|
|
|
template<typename T>
|
|
|
|
lazy_list<T> take(unsigned sz, lazy_list<T> const & l) {
|
|
|
|
if (sz == 0) {
|
|
|
|
return lazy_list<T>();
|
2013-11-22 17:40:18 +00:00
|
|
|
} else {
|
2013-11-24 01:45:01 +00:00
|
|
|
return mk_lazy_list<T>([=]() {
|
2013-11-23 23:33:25 +00:00
|
|
|
auto p = l.pull();
|
|
|
|
if (p)
|
|
|
|
return some(mk_pair(p->first, take(sz - 1, p->second)));
|
|
|
|
else
|
|
|
|
return p;
|
|
|
|
});
|
2013-11-22 17:40:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-24 00:27:36 +00:00
|
|
|
/**
|
|
|
|
\brief Create a lazy list based on the list \c l.
|
|
|
|
*/
|
2013-11-22 17:40:18 +00:00
|
|
|
template<typename T>
|
2013-11-23 23:33:25 +00:00
|
|
|
lazy_list<T> to_lazy(list<T> l) {
|
|
|
|
if (l) {
|
2013-11-24 01:45:01 +00:00
|
|
|
return mk_lazy_list<T>([=]() {
|
2013-11-23 23:33:25 +00:00
|
|
|
return some(mk_pair(head(l), to_lazy(tail(l))));
|
|
|
|
});
|
|
|
|
} else {
|
2013-11-22 17:40:18 +00:00
|
|
|
return lazy_list<T>();
|
2013-11-23 23:33:25 +00:00
|
|
|
}
|
2013-11-22 17:40:18 +00:00
|
|
|
}
|
|
|
|
|
2013-11-24 00:27:36 +00:00
|
|
|
/**
|
|
|
|
\brief Appends the given lazy lists.
|
|
|
|
*/
|
2013-11-22 17:40:18 +00:00
|
|
|
template<typename T>
|
2013-12-07 22:59:21 +00:00
|
|
|
lazy_list<T> append(lazy_list<T> const & l1, lazy_list<T> const & l2, char const * cname = "lazy list") {
|
2013-11-24 01:45:01 +00:00
|
|
|
return mk_lazy_list<T>([=]() {
|
2013-11-23 23:33:25 +00:00
|
|
|
auto p = l1.pull();
|
2013-11-23 23:54:26 +00:00
|
|
|
if (!p) {
|
2013-12-07 22:59:21 +00:00
|
|
|
check_system(cname);
|
2013-11-23 23:33:25 +00:00
|
|
|
return l2.pull();
|
2013-11-23 23:54:26 +00:00
|
|
|
} else {
|
2013-12-07 22:59:21 +00:00
|
|
|
return some(mk_pair(p->first, append(p->second, l2, cname)));
|
2013-11-23 23:54:26 +00:00
|
|
|
}
|
2013-11-23 23:33:25 +00:00
|
|
|
});
|
2013-11-22 17:40:18 +00:00
|
|
|
}
|
|
|
|
|
2013-11-24 00:27:36 +00:00
|
|
|
/**
|
|
|
|
\brief Return \c l1 if l1 is not empty, and \c l2 otherwise.
|
|
|
|
*/
|
2013-11-23 23:33:25 +00:00
|
|
|
template<typename T>
|
2013-12-07 22:59:21 +00:00
|
|
|
lazy_list<T> orelse(lazy_list<T> const & l1, lazy_list<T> const & l2, char const * cname = "lazy list") {
|
2013-11-24 01:45:01 +00:00
|
|
|
return mk_lazy_list<T>([=]() {
|
2013-11-23 23:33:25 +00:00
|
|
|
auto p = l1.pull();
|
2013-11-23 23:54:26 +00:00
|
|
|
if (!p) {
|
2013-12-07 22:59:21 +00:00
|
|
|
check_system(cname);
|
2013-11-23 23:33:25 +00:00
|
|
|
return l2.pull();
|
2013-11-23 23:54:26 +00:00
|
|
|
} else {
|
2013-11-24 01:45:01 +00:00
|
|
|
return p;
|
2013-11-23 23:54:26 +00:00
|
|
|
}
|
2013-11-23 23:33:25 +00:00
|
|
|
});
|
2013-11-22 17:40:18 +00:00
|
|
|
}
|
|
|
|
|
2013-11-24 00:27:36 +00:00
|
|
|
/**
|
|
|
|
\brief "Fair" version of \c append. That is, the elements of \c l1 and \c l2
|
|
|
|
are interleaved.
|
|
|
|
*/
|
2013-11-22 17:40:18 +00:00
|
|
|
template<typename T>
|
2013-12-07 22:59:21 +00:00
|
|
|
lazy_list<T> interleave(lazy_list<T> const & l1, lazy_list<T> const & l2, char const * cname = "lazy list") {
|
2013-11-24 01:45:01 +00:00
|
|
|
return mk_lazy_list<T>([=]() {
|
2013-11-23 23:33:25 +00:00
|
|
|
auto p = l1.pull();
|
2013-11-23 23:54:26 +00:00
|
|
|
if (!p) {
|
2013-12-07 22:59:21 +00:00
|
|
|
check_system(cname);
|
2013-11-23 23:33:25 +00:00
|
|
|
return l2.pull();
|
2013-11-23 23:54:26 +00:00
|
|
|
} else {
|
2013-12-07 22:59:21 +00:00
|
|
|
return some(mk_pair(p->first, interleave(l2, p->second, cname)));
|
2013-11-23 23:54:26 +00:00
|
|
|
}
|
2013-11-23 23:33:25 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-11-24 00:27:36 +00:00
|
|
|
/**
|
|
|
|
\brief Create a lazy list by applying \c f to the elements of \c l.
|
|
|
|
*/
|
2013-11-23 23:33:25 +00:00
|
|
|
template<typename T, typename F>
|
2013-12-07 22:59:21 +00:00
|
|
|
lazy_list<T> map(lazy_list<T> const & l, F && f, char const * cname = "lazy list") {
|
2013-11-24 01:45:01 +00:00
|
|
|
return mk_lazy_list<T>([=]() {
|
2013-11-23 23:33:25 +00:00
|
|
|
auto p = l.pull();
|
2013-12-07 22:59:21 +00:00
|
|
|
if (!p) {
|
2013-11-23 23:33:25 +00:00
|
|
|
return p;
|
2013-12-07 22:59:21 +00:00
|
|
|
} else {
|
|
|
|
check_system(cname);
|
|
|
|
return some(mk_pair(f(p->first), map(p->second, f, cname)));
|
|
|
|
}
|
2013-11-23 23:33:25 +00:00
|
|
|
});
|
2013-11-22 17:40:18 +00:00
|
|
|
}
|
|
|
|
|
2014-06-30 01:26:07 +00:00
|
|
|
/**
|
|
|
|
\brief Create a lazy list by applying \c f to the elements of \c l.
|
|
|
|
*/
|
|
|
|
template<typename To, typename From, typename F>
|
|
|
|
lazy_list<To> map2(lazy_list<From> const & l, F && f, char const * cname = "lazy list") {
|
|
|
|
return mk_lazy_list<To>([=]() {
|
|
|
|
typename lazy_list<From>::maybe_pair p = l.pull();
|
|
|
|
if (!p) {
|
|
|
|
return typename lazy_list<To>::maybe_pair();
|
|
|
|
} else {
|
|
|
|
check_system(cname);
|
|
|
|
return some(mk_pair(f(p->first), map2<To>(p->second, f, cname)));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-11-24 00:27:36 +00:00
|
|
|
/**
|
|
|
|
\brief Create a lazy list that contains only the elements of \c l that satisfies \c pred.
|
|
|
|
|
|
|
|
\remark Lazy lists may be infinite, and none of them may satisfy \c pred.
|
|
|
|
|
2013-12-01 20:42:32 +00:00
|
|
|
\remark \c check_system() is invoked whenever \c pred returns false.
|
2013-11-24 00:27:36 +00:00
|
|
|
*/
|
2013-11-22 17:40:18 +00:00
|
|
|
template<typename T, typename P>
|
2013-12-07 22:59:21 +00:00
|
|
|
lazy_list<T> filter(lazy_list<T> const & l, P && pred, char const * cname = "lazy list") {
|
2013-11-24 01:45:01 +00:00
|
|
|
return mk_lazy_list<T>([=]() {
|
2013-11-23 23:33:25 +00:00
|
|
|
auto p = l.pull();
|
2013-11-23 23:54:26 +00:00
|
|
|
if (!p) {
|
2013-11-23 23:33:25 +00:00
|
|
|
return p;
|
2013-11-23 23:54:26 +00:00
|
|
|
} else if (pred(p->first)) {
|
2013-11-24 01:45:01 +00:00
|
|
|
return p;
|
2013-11-23 23:54:26 +00:00
|
|
|
} else {
|
2013-12-07 22:59:21 +00:00
|
|
|
check_system(cname);
|
|
|
|
return filter(p->second, pred, cname).pull();
|
2013-11-23 23:54:26 +00:00
|
|
|
}
|
2013-11-23 23:33:25 +00:00
|
|
|
});
|
2013-11-22 17:40:18 +00:00
|
|
|
}
|
|
|
|
|
2013-11-24 00:27:36 +00:00
|
|
|
/**
|
|
|
|
\brief Auxiliary template for \c map_append.
|
|
|
|
*/
|
2013-11-22 17:40:18 +00:00
|
|
|
template<typename T, typename F>
|
2013-12-07 22:59:21 +00:00
|
|
|
lazy_list<T> map_append_aux(lazy_list<T> const & h, lazy_list<T> const & l, F && f, char const * cname) {
|
2013-11-24 01:45:01 +00:00
|
|
|
return mk_lazy_list<T>([=]() {
|
2013-11-23 23:33:25 +00:00
|
|
|
auto p1 = h.pull();
|
|
|
|
if (p1) {
|
2013-12-07 22:59:21 +00:00
|
|
|
return some(mk_pair(p1->first, map_append_aux(p1->second, l, f, cname)));
|
2013-11-23 23:33:25 +00:00
|
|
|
} else {
|
2013-11-23 23:54:26 +00:00
|
|
|
check_interrupted();
|
2013-11-23 23:33:25 +00:00
|
|
|
auto p2 = l.pull();
|
|
|
|
if (p2) {
|
2013-12-07 22:59:21 +00:00
|
|
|
check_system(cname);
|
|
|
|
return map_append_aux(f(p2->first), p2->second, f, cname).pull();
|
2013-11-23 23:33:25 +00:00
|
|
|
} else {
|
|
|
|
return typename lazy_list<T>::maybe_pair();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2013-11-22 17:40:18 +00:00
|
|
|
}
|
|
|
|
|
2013-11-24 00:27:36 +00:00
|
|
|
/**
|
|
|
|
\brief Applies \c f to each element of \c l. The function \c must return a lazy_list.
|
|
|
|
All lazy_lists are appended together.
|
|
|
|
*/
|
2013-11-22 17:40:18 +00:00
|
|
|
template<typename T, typename F>
|
2013-12-07 22:59:21 +00:00
|
|
|
lazy_list<T> map_append(lazy_list<T> const & l, F && f, char const * cname = "lazy list") {
|
|
|
|
return map_append_aux(lazy_list<T>(), l, f, cname);
|
2013-11-22 17:40:18 +00:00
|
|
|
}
|
2013-11-24 19:11:10 +00:00
|
|
|
|
|
|
|
template<typename T, typename F>
|
2013-12-07 22:59:21 +00:00
|
|
|
lazy_list<T> repeat(T const & v, F && f, char const * cname = "lazy list") {
|
2013-11-24 19:11:10 +00:00
|
|
|
return mk_lazy_list<T>([=]() {
|
|
|
|
auto p = f(v).pull();
|
|
|
|
if (!p) {
|
|
|
|
return some(mk_pair(v, lazy_list<T>()));
|
|
|
|
} else {
|
2013-12-07 22:59:21 +00:00
|
|
|
check_system(cname);
|
|
|
|
return append(repeat(p->first, f, cname),
|
|
|
|
map_append(p->second, [=](T const & v2) { return repeat(v2, f, cname); }, cname), cname).pull();
|
2013-11-24 19:11:10 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, typename F>
|
2013-12-07 22:59:21 +00:00
|
|
|
lazy_list<T> repeat_at_most(T const & v, F && f, unsigned k, char const * cname = "lazy list") {
|
2013-11-24 19:11:10 +00:00
|
|
|
return mk_lazy_list<T>([=]() {
|
|
|
|
if (k == 0) {
|
|
|
|
return some(mk_pair(v, lazy_list<T>()));
|
|
|
|
} else {
|
|
|
|
auto p = f(v).pull();
|
|
|
|
if (!p) {
|
|
|
|
return some(mk_pair(v, lazy_list<T>()));
|
|
|
|
} else {
|
2013-12-07 22:59:21 +00:00
|
|
|
check_system(cname);
|
|
|
|
return append(repeat_at_most(p->first, f, k - 1, cname),
|
|
|
|
map_append(p->second, [=](T const & v2) { return repeat_at_most(v2, f, k - 1, cname); }, cname),
|
|
|
|
cname).pull();
|
2013-11-24 19:11:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2013-11-24 00:27:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
\brief Return a lazy list such that only the elements that can be computed in
|
|
|
|
less than \c ms milliseconds are kept. That is, it uses a timeout for the \c pull
|
|
|
|
method in the class lazy_list. If the \c pull method timeouts, the lazy list
|
|
|
|
is truncated.
|
|
|
|
|
|
|
|
\remark the \c method is executed in a separate execution thread.
|
|
|
|
|
|
|
|
\remark \c check_ms is how often the main thread checks whether the child
|
|
|
|
thread finished.
|
|
|
|
*/
|
2013-12-09 22:56:48 +00:00
|
|
|
#if !defined(LEAN_MULTI_THREAD)
|
|
|
|
template<typename T>
|
|
|
|
lazy_list<T> timeout(lazy_list<T> const & l, unsigned, unsigned) {
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
#else
|
2013-11-24 00:27:36 +00:00
|
|
|
template<typename T>
|
|
|
|
lazy_list<T> timeout(lazy_list<T> const & l, unsigned ms, unsigned check_ms = g_small_sleep) {
|
|
|
|
if (check_ms == 0)
|
|
|
|
check_ms = 1;
|
2013-11-24 01:45:01 +00:00
|
|
|
return mk_lazy_list<T>([=]() {
|
2013-11-24 00:27:36 +00:00
|
|
|
typename lazy_list<T>::maybe_pair r;
|
2013-12-09 22:56:48 +00:00
|
|
|
atomic<bool> done(false);
|
2013-11-24 00:27:36 +00:00
|
|
|
interruptible_thread th([&]() {
|
|
|
|
try {
|
|
|
|
r = l.pull();
|
|
|
|
} catch (...) {
|
|
|
|
r = typename lazy_list<T>::maybe_pair();
|
|
|
|
}
|
|
|
|
done = true;
|
|
|
|
});
|
|
|
|
try {
|
2013-12-10 00:55:13 +00:00
|
|
|
auto start = chrono::steady_clock::now();
|
|
|
|
chrono::milliseconds d(ms);
|
|
|
|
chrono::milliseconds small(check_ms);
|
2013-11-24 00:27:36 +00:00
|
|
|
while (!done) {
|
2013-12-10 00:55:13 +00:00
|
|
|
auto curr = chrono::steady_clock::now();
|
|
|
|
if (chrono::duration_cast<chrono::milliseconds>(curr - start) > d)
|
2013-11-24 00:27:36 +00:00
|
|
|
break;
|
|
|
|
check_interrupted();
|
2013-12-09 22:56:48 +00:00
|
|
|
this_thread::sleep_for(small);
|
2013-11-24 00:27:36 +00:00
|
|
|
}
|
|
|
|
th.request_interrupt();
|
|
|
|
th.join();
|
|
|
|
if (r)
|
|
|
|
return some(mk_pair(r->first, timeout(r->second, ms, check_ms)));
|
|
|
|
else
|
|
|
|
return r;
|
|
|
|
} catch (...) {
|
|
|
|
th.request_interrupt();
|
|
|
|
th.join();
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2013-12-09 22:56:48 +00:00
|
|
|
#endif
|
2013-11-24 00:51:17 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
\brief Similar to interleave, but the heads are computed in parallel.
|
|
|
|
Moreover, when pulling results from the lists, if one finishes before the other,
|
|
|
|
then the other one is interrupted.
|
|
|
|
*/
|
2013-12-09 22:56:48 +00:00
|
|
|
#if !defined(LEAN_MULTI_THREAD)
|
2013-12-04 18:27:18 +00:00
|
|
|
template<typename T>
|
|
|
|
lazy_list<T> par(lazy_list<T> const & l1, lazy_list<T> const & l2, unsigned = g_small_sleep) {
|
|
|
|
return interleave(l1, l2);
|
|
|
|
}
|
|
|
|
#else
|
2013-11-24 00:51:17 +00:00
|
|
|
template<typename T>
|
|
|
|
lazy_list<T> par(lazy_list<T> const & l1, lazy_list<T> const & l2, unsigned check_ms = g_small_sleep) {
|
2013-11-24 01:45:01 +00:00
|
|
|
return mk_lazy_list<T>([=]() {
|
2013-11-24 00:51:17 +00:00
|
|
|
typename lazy_list<T>::maybe_pair r1;
|
|
|
|
typename lazy_list<T>::maybe_pair r2;
|
2013-12-09 22:56:48 +00:00
|
|
|
atomic<bool> done1(false);
|
|
|
|
atomic<bool> done2(false);
|
2013-11-24 00:51:17 +00:00
|
|
|
interruptible_thread th1([&]() {
|
|
|
|
try {
|
|
|
|
r1 = l1.pull();
|
|
|
|
} catch (...) {
|
|
|
|
r1 = typename lazy_list<T>::maybe_pair();
|
|
|
|
}
|
|
|
|
done1 = true;
|
|
|
|
});
|
|
|
|
interruptible_thread th2([&]() {
|
|
|
|
try {
|
|
|
|
r2 = l2.pull();
|
|
|
|
} catch (...) {
|
|
|
|
r2 = typename lazy_list<T>::maybe_pair();
|
|
|
|
}
|
|
|
|
done2 = true;
|
|
|
|
});
|
|
|
|
try {
|
2013-12-10 00:55:13 +00:00
|
|
|
chrono::milliseconds small(check_ms);
|
2013-11-24 00:51:17 +00:00
|
|
|
while (!done1 && !done2) {
|
|
|
|
check_interrupted();
|
2013-12-09 22:56:48 +00:00
|
|
|
this_thread::sleep_for(small);
|
2013-11-24 00:51:17 +00:00
|
|
|
}
|
|
|
|
th1.request_interrupt();
|
|
|
|
th2.request_interrupt();
|
|
|
|
th1.join();
|
|
|
|
th2.join();
|
2013-11-24 08:38:31 +00:00
|
|
|
if (r1 && r2) {
|
|
|
|
lazy_list<T> tail(r2->first, par(r1->second, r2->second));
|
|
|
|
return some(mk_pair(r1->first, tail));
|
|
|
|
} else if (r1) {
|
2013-11-24 00:51:17 +00:00
|
|
|
return some(mk_pair(r1->first, par(r1->second, l2)));
|
|
|
|
} else if (r2) {
|
|
|
|
return some(mk_pair(r2->first, par(l1, r2->second)));
|
|
|
|
} else {
|
|
|
|
return r2;
|
|
|
|
}
|
|
|
|
} catch (...) {
|
|
|
|
th1.request_interrupt();
|
|
|
|
th2.request_interrupt();
|
|
|
|
th1.join();
|
|
|
|
th2.join();
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2013-12-04 18:27:18 +00:00
|
|
|
#endif
|
2013-11-22 17:40:18 +00:00
|
|
|
}
|