/* 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 #include "util/interrupt.h" #include "util/lazy_list.h" #include "util/list.h" namespace lean { template void for_each(lazy_list l, F && f) { while (true) { auto p = l.pull(); if (p) { f(p->first); l = p->second; } else { break; } check_interrupted(); } } /** \brief Create a lazy list that contains the first \c sz elements in \c l. */ template lazy_list take(unsigned sz, lazy_list const & l) { if (sz == 0) { return lazy_list(); } else { return lazy_list([=]() { auto p = l.pull(); if (p) return some(mk_pair(p->first, take(sz - 1, p->second))); else return p; }); } } /** \brief Create a lazy list based on the list \c l. */ template lazy_list to_lazy(list l) { if (l) { return lazy_list([=]() { return some(mk_pair(head(l), to_lazy(tail(l)))); }); } else { return lazy_list(); } } /** \brief Appends the given lazy lists. */ template lazy_list append(lazy_list const & l1, lazy_list const & l2) { return lazy_list([=]() { auto p = l1.pull(); if (!p) { check_interrupted(); return l2.pull(); } else { return some(mk_pair(p->first, append(p->second, l2))); } }); } /** \brief Return \c l1 if l1 is not empty, and \c l2 otherwise. */ template lazy_list orelse(lazy_list const & l1, lazy_list const & l2) { return lazy_list([=]() { auto p = l1.pull(); if (!p) { check_interrupted(); return l2.pull(); } else { return some(mk_pair(p->first, orelse(p->second, lazy_list()))); } }); } /** \brief "Fair" version of \c append. That is, the elements of \c l1 and \c l2 are interleaved. */ template lazy_list interleave(lazy_list const & l1, lazy_list const & l2) { return lazy_list([=]() { auto p = l1.pull(); if (!p) { check_interrupted(); return l2.pull(); } else { return some(mk_pair(p->first, interleave(l2, p->second))); } }); } /** \brief Create a lazy list by applying \c f to the elements of \c l. */ template lazy_list map(lazy_list const & l, F && f) { return lazy_list([=]() { auto p = l.pull(); if (!p) return p; else return some(mk_pair(f(p->first), map(p->second, f))); }); } /** \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. \remark \c check_interrupted() is invoked whenever \c pred returns false. */ template lazy_list filter(lazy_list const & l, P && pred) { return lazy_list([=]() { auto p = l.pull(); if (!p) { return p; } else if (pred(p->first)) { return some(mk_pair(p->first, p->second)); } else { check_interrupted(); return filter(p->second, pred).pull(); } }); } /** \brief Auxiliary template for \c map_append. */ template lazy_list map_append_aux(lazy_list const & h, lazy_list const & l, F && f) { return lazy_list([=]() { auto p1 = h.pull(); if (p1) { return some(mk_pair(p1->first, map_append_aux(p1->second, l, f))); } else { check_interrupted(); auto p2 = l.pull(); if (p2) { check_interrupted(); return map_append_aux(f(p2->first), p2->second, f).pull(); } else { return typename lazy_list::maybe_pair(); } } }); } /** \brief Applies \c f to each element of \c l. The function \c must return a lazy_list. All lazy_lists are appended together. */ template lazy_list map_append(lazy_list const & l, F && f) { return map_append_aux(lazy_list(), l, f); } /** \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. */ template lazy_list timeout(lazy_list const & l, unsigned ms, unsigned check_ms = g_small_sleep) { if (check_ms == 0) check_ms = 1; return lazy_list([=]() { typename lazy_list::maybe_pair r; std::atomic done(false); interruptible_thread th([&]() { try { r = l.pull(); } catch (...) { r = typename lazy_list::maybe_pair(); } done = true; }); try { auto start = std::chrono::steady_clock::now(); std::chrono::milliseconds d(ms); std::chrono::milliseconds small(check_ms); while (!done) { auto curr = std::chrono::steady_clock::now(); if (std::chrono::duration_cast(curr - start) > d) break; check_interrupted(); std::this_thread::sleep_for(small); } 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; } }); } }