2013-10-01 00:58:46 +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 <algorithm>
|
|
|
|
#include "util/debug.h"
|
|
|
|
#include "util/rc.h"
|
|
|
|
#include "util/buffer.h"
|
|
|
|
#include "util/sexpr/format.h"
|
2013-10-01 17:25:18 +00:00
|
|
|
#include "kernel/expr.h"
|
|
|
|
#include "kernel/formatter.h"
|
2013-10-14 21:37:35 +00:00
|
|
|
#include "kernel/pos_info_provider.h"
|
2013-10-01 00:58:46 +00:00
|
|
|
|
|
|
|
namespace lean {
|
2013-10-23 20:42:14 +00:00
|
|
|
class justification;
|
2013-10-01 00:58:46 +00:00
|
|
|
/**
|
2013-10-23 20:42:14 +00:00
|
|
|
\brief Base class used to represent justification objects.
|
|
|
|
These objects are used to justification the execution of different engines in Lean.
|
|
|
|
The justifications may help users understanding why something did not work.
|
|
|
|
The justifications are particularly important for the elaborator.
|
2013-10-01 00:58:46 +00:00
|
|
|
*/
|
2013-10-23 20:42:14 +00:00
|
|
|
class justification_cell {
|
2013-10-01 00:58:46 +00:00
|
|
|
MK_LEAN_RC();
|
|
|
|
void dealloc() { delete this; }
|
2013-10-16 00:32:02 +00:00
|
|
|
protected:
|
|
|
|
static void add_pos_info(format & r, expr const & e, pos_info_provider const * p);
|
2013-10-01 00:58:46 +00:00
|
|
|
public:
|
2013-10-23 20:42:14 +00:00
|
|
|
justification_cell():m_rc(0) {}
|
|
|
|
virtual ~justification_cell() {}
|
2013-10-16 00:32:02 +00:00
|
|
|
virtual format pp_header(formatter const & fmt, options const & opts) const = 0;
|
|
|
|
virtual format pp(formatter const & fmt, options const & opts, pos_info_provider const * p, bool display_children) const;
|
2013-10-23 20:42:14 +00:00
|
|
|
virtual void get_children(buffer<justification_cell*> & r) const = 0;
|
2013-10-01 17:25:18 +00:00
|
|
|
virtual expr const & get_main_expr() const { return expr::null(); }
|
2013-10-14 18:50:51 +00:00
|
|
|
bool is_shared() const { return get_rc() > 1; }
|
2013-10-01 00:58:46 +00:00
|
|
|
};
|
|
|
|
|
2013-10-26 21:21:29 +00:00
|
|
|
/**
|
|
|
|
\brief Base class for justification objects used to justify case-splits.
|
|
|
|
*/
|
|
|
|
class assumption_justification : public justification_cell {
|
|
|
|
unsigned m_idx;
|
|
|
|
public:
|
|
|
|
assumption_justification(unsigned idx);
|
|
|
|
virtual void get_children(buffer<justification_cell*> &) const;
|
|
|
|
virtual expr const & get_main_expr() const;
|
|
|
|
virtual format pp_header(formatter const &, options const &) const;
|
|
|
|
};
|
|
|
|
|
2013-10-01 00:58:46 +00:00
|
|
|
/**
|
2013-10-23 20:42:14 +00:00
|
|
|
\brief Smart pointer for justification_cell's
|
2013-10-01 00:58:46 +00:00
|
|
|
*/
|
2013-10-23 20:42:14 +00:00
|
|
|
class justification {
|
|
|
|
justification_cell * m_ptr;
|
2013-10-01 00:58:46 +00:00
|
|
|
public:
|
2013-10-23 20:42:14 +00:00
|
|
|
justification():m_ptr(nullptr) {}
|
|
|
|
justification(justification_cell * ptr):m_ptr(ptr) { if (m_ptr) m_ptr->inc_ref(); }
|
|
|
|
justification(justification const & s):m_ptr(s.m_ptr) { if (m_ptr) m_ptr->inc_ref(); }
|
|
|
|
justification(justification && s):m_ptr(s.m_ptr) { s.m_ptr = nullptr; }
|
|
|
|
~justification() { if (m_ptr) m_ptr->dec_ref(); }
|
2013-10-01 00:58:46 +00:00
|
|
|
|
|
|
|
operator bool() const { return m_ptr != nullptr; }
|
|
|
|
|
2013-10-23 20:42:14 +00:00
|
|
|
justification_cell * raw() const { return m_ptr; }
|
2013-10-01 00:58:46 +00:00
|
|
|
|
2013-10-23 20:42:14 +00:00
|
|
|
friend void swap(justification & a, justification & b) { std::swap(a.m_ptr, b.m_ptr); }
|
2013-10-01 00:58:46 +00:00
|
|
|
|
2013-10-23 20:42:14 +00:00
|
|
|
justification & operator=(justification const & s) { LEAN_COPY_REF(justification, s); }
|
|
|
|
justification & operator=(justification && s) { LEAN_MOVE_REF(justification, s); }
|
2013-10-14 21:37:35 +00:00
|
|
|
format pp(formatter const & fmt, options const & opts, pos_info_provider const * p = nullptr, bool display_children = true) const {
|
|
|
|
lean_assert(m_ptr);
|
|
|
|
return m_ptr->pp(fmt, opts, p, display_children);
|
|
|
|
}
|
2013-10-01 17:25:18 +00:00
|
|
|
expr const & get_main_expr() const { return m_ptr ? m_ptr->get_main_expr() : expr::null(); }
|
2013-10-23 20:42:14 +00:00
|
|
|
void get_children(buffer<justification_cell*> & r) const { if (m_ptr) m_ptr->get_children(r); }
|
2013-10-01 00:58:46 +00:00
|
|
|
bool has_children() const;
|
|
|
|
};
|
2013-10-14 18:50:51 +00:00
|
|
|
|
2013-10-26 21:21:29 +00:00
|
|
|
inline justification mk_assumption_justification(unsigned idx) { return justification(new assumption_justification(idx)); }
|
|
|
|
|
2013-10-14 18:50:51 +00:00
|
|
|
/**
|
|
|
|
\brief Return true iff \c t depends on \c d.
|
|
|
|
That is \c t is a descendant of \c d.
|
|
|
|
*/
|
2013-10-23 20:42:14 +00:00
|
|
|
bool depends_on(justification const & t, justification const & d);
|
2013-10-27 18:02:29 +00:00
|
|
|
|
|
|
|
/** \brief Add \c t to \c r */
|
|
|
|
inline void push_back(buffer<justification_cell*> & r, justification const & t) {
|
|
|
|
if (t) r.push_back(t.raw());
|
|
|
|
}
|
|
|
|
|
|
|
|
/** \brief Add justifications in the collection \c s into r. */
|
|
|
|
template<typename T>
|
|
|
|
void append(buffer<justification_cell*> & r, T const & s) {
|
|
|
|
for (auto t : s)
|
|
|
|
push_back(r, t);
|
|
|
|
}
|
2013-10-01 00:58:46 +00:00
|
|
|
}
|