2013-09-29 19:33:12 +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
|
2013-11-24 01:05:46 +00:00
|
|
|
#include <utility>
|
2013-09-29 19:33:12 +00:00
|
|
|
#include "util/debug.h"
|
2013-11-23 23:33:25 +00:00
|
|
|
#include "util/rc.h"
|
|
|
|
#include "util/optional.h"
|
2013-09-29 19:33:12 +00:00
|
|
|
|
|
|
|
namespace lean {
|
|
|
|
/**
|
|
|
|
\brief ML-like lazy lists.
|
|
|
|
*/
|
|
|
|
template<typename T>
|
|
|
|
class lazy_list {
|
|
|
|
public:
|
2013-11-23 23:33:25 +00:00
|
|
|
typedef optional<std::pair<T, lazy_list>> maybe_pair; // head and tail pair
|
|
|
|
private:
|
|
|
|
class cell_base {
|
2013-09-29 19:33:12 +00:00
|
|
|
MK_LEAN_RC();
|
|
|
|
void dealloc() { delete this; }
|
|
|
|
public:
|
2013-11-23 23:33:25 +00:00
|
|
|
cell_base():m_rc(0) {}
|
|
|
|
virtual ~cell_base() {}
|
|
|
|
virtual maybe_pair pull() const = 0;
|
2013-09-29 19:33:12 +00:00
|
|
|
};
|
|
|
|
|
2013-11-23 23:33:25 +00:00
|
|
|
template<typename F>
|
|
|
|
class cell : public cell_base {
|
|
|
|
F m_f;
|
2013-09-29 19:33:12 +00:00
|
|
|
public:
|
2013-11-23 23:33:25 +00:00
|
|
|
cell(F && f):cell_base(), m_f(f) {}
|
|
|
|
virtual ~cell() {}
|
|
|
|
virtual maybe_pair pull() const { return m_f(); }
|
2013-09-29 19:33:12 +00:00
|
|
|
};
|
|
|
|
|
2013-11-24 01:45:01 +00:00
|
|
|
class cell_singleton : public cell_base {
|
|
|
|
T m_val;
|
|
|
|
public:
|
|
|
|
cell_singleton(T const & v):cell_base(), m_val(v) {}
|
|
|
|
cell_singleton(T && v):cell_base(), m_val(std::move(v)) {}
|
|
|
|
virtual ~cell_singleton() {}
|
|
|
|
virtual maybe_pair pull() const {
|
|
|
|
lazy_list empty;
|
|
|
|
return maybe_pair(m_val, empty);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class cell_pair : public cell_base {
|
|
|
|
T m_head;
|
|
|
|
lazy_list m_tail;
|
|
|
|
public:
|
|
|
|
cell_pair(T const & h, lazy_list const & t):cell_base(), m_head(h), m_tail(t) {}
|
|
|
|
virtual ~cell_pair() {}
|
|
|
|
virtual maybe_pair pull() const {
|
|
|
|
return maybe_pair(m_head, m_tail);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-11-23 23:33:25 +00:00
|
|
|
cell_base * m_ptr;
|
2013-09-29 19:33:12 +00:00
|
|
|
public:
|
|
|
|
lazy_list():m_ptr(nullptr) {}
|
|
|
|
lazy_list(lazy_list const & s):m_ptr(s.m_ptr) { if (m_ptr) m_ptr->inc_ref(); }
|
2013-11-23 23:33:25 +00:00
|
|
|
lazy_list(lazy_list && s):m_ptr(s.m_ptr) { s.m_ptr = nullptr; }
|
2013-11-24 01:45:01 +00:00
|
|
|
lazy_list(T const & v):m_ptr(new cell_singleton(v)) { m_ptr->inc_ref(); }
|
|
|
|
lazy_list(T && v):m_ptr(new cell_singleton(std::forward<T>(v))) { m_ptr->inc_ref(); }
|
|
|
|
lazy_list(T const & h, lazy_list const & t):m_ptr(new cell_pair(h, t)) { m_ptr->inc_ref(); }
|
2013-09-29 19:33:12 +00:00
|
|
|
~lazy_list() { if (m_ptr) m_ptr->dec_ref(); }
|
|
|
|
|
2013-12-13 23:00:30 +00:00
|
|
|
lazy_list & operator=(lazy_list const & s) { LEAN_COPY_REF(s); }
|
|
|
|
lazy_list & operator=(lazy_list && s) { LEAN_MOVE_REF(s); }
|
2013-09-29 19:33:12 +00:00
|
|
|
|
2014-06-27 01:51:35 +00:00
|
|
|
/**
|
|
|
|
\brief Return true if the lazy_list is definitely empty. This is an approximated test (i.e., it is not an "iff").
|
|
|
|
Here are examples where this method will return true.
|
|
|
|
|
|
|
|
1) lazy_list empty;
|
|
|
|
lean_assert(is_nil(empty));
|
|
|
|
|
|
|
|
2) lazy_list singleton(v);
|
|
|
|
maybe_pair p = singleton.pull();
|
|
|
|
lean_assert(p);
|
|
|
|
lean_assert(is_nil(p->second));
|
|
|
|
|
|
|
|
3) assume is_nil(l), then
|
|
|
|
lazy_list singleton(v, l);
|
|
|
|
maybe_pair p = singleton.pull();
|
|
|
|
lean_assert(p);
|
|
|
|
lean_assert(is_nil(p->second));
|
|
|
|
*/
|
|
|
|
bool is_nil() const { return m_ptr == nullptr; }
|
|
|
|
|
2013-11-23 23:33:25 +00:00
|
|
|
maybe_pair pull() const {
|
|
|
|
if (m_ptr)
|
|
|
|
return m_ptr->pull();
|
|
|
|
else
|
|
|
|
return maybe_pair();
|
|
|
|
}
|
2013-09-29 19:33:12 +00:00
|
|
|
|
2013-11-23 23:33:25 +00:00
|
|
|
friend T head(lazy_list const & l) { return l.pull()->first; }
|
|
|
|
friend lazy_list tail(lazy_list const & l) { return l.pull()->second; }
|
2013-11-24 01:45:01 +00:00
|
|
|
|
|
|
|
template<typename F>
|
|
|
|
static lazy_list mk_lazy_list_core(F && f) {
|
|
|
|
lazy_list r;
|
|
|
|
r.m_ptr = new cell<F>(std::forward<F>(f));
|
|
|
|
r.m_ptr->inc_ref();
|
|
|
|
return r;
|
|
|
|
}
|
2013-09-29 19:33:12 +00:00
|
|
|
};
|
2013-11-24 01:45:01 +00:00
|
|
|
template<typename T, typename F>
|
|
|
|
lazy_list<T> mk_lazy_list(F && f) {
|
|
|
|
return lazy_list<T>::mk_lazy_list_core(std::forward<F>(f));
|
|
|
|
}
|
2013-09-29 19:33:12 +00:00
|
|
|
}
|