2014-02-23 21:19:39 +00:00
|
|
|
/*
|
|
|
|
Copyright (c) 2014 Microsoft Corporation. All rights reserved.
|
|
|
|
Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
|
|
|
|
Author: Leonardo de Moura
|
|
|
|
*/
|
|
|
|
#include "util/rc.h"
|
|
|
|
#include "kernel/constraint.h"
|
|
|
|
namespace lean {
|
|
|
|
struct constraint_cell {
|
|
|
|
void dealloc();
|
|
|
|
MK_LEAN_RC()
|
|
|
|
constraint_kind m_kind;
|
|
|
|
justification m_jst;
|
2014-06-21 15:15:47 +00:00
|
|
|
constraint_cell(constraint_kind k, justification const & j):m_rc(1), m_kind(k), m_jst(j) {}
|
2014-02-23 21:19:39 +00:00
|
|
|
};
|
2014-05-10 03:25:27 +00:00
|
|
|
struct eq_constraint_cell : public constraint_cell {
|
2014-02-23 21:19:39 +00:00
|
|
|
expr m_lhs;
|
|
|
|
expr m_rhs;
|
2014-05-10 03:25:27 +00:00
|
|
|
eq_constraint_cell(expr const & lhs, expr const & rhs, justification const & j):
|
2014-06-21 15:15:47 +00:00
|
|
|
constraint_cell(constraint_kind::Eq, j),
|
2014-02-23 21:19:39 +00:00
|
|
|
m_lhs(lhs), m_rhs(rhs) {}
|
|
|
|
};
|
|
|
|
struct level_constraint_cell : public constraint_cell {
|
|
|
|
level m_lhs;
|
|
|
|
level m_rhs;
|
|
|
|
level_constraint_cell(level const & lhs, level const & rhs, justification const & j):
|
2014-06-22 17:50:47 +00:00
|
|
|
constraint_cell(constraint_kind::LevelEq, j),
|
2014-02-23 21:19:39 +00:00
|
|
|
m_lhs(lhs), m_rhs(rhs) {}
|
|
|
|
};
|
2014-06-21 15:15:47 +00:00
|
|
|
struct choice_constraint_cell : public constraint_cell {
|
|
|
|
expr m_mvar;
|
|
|
|
choice_fn m_fn;
|
|
|
|
choice_constraint_cell(expr const & mvar, choice_fn const & fn, justification const & j):
|
|
|
|
constraint_cell(constraint_kind::Choice, j),
|
|
|
|
m_mvar(mvar), m_fn(fn) {}
|
|
|
|
};
|
2014-02-23 21:19:39 +00:00
|
|
|
|
2014-04-28 18:13:51 +00:00
|
|
|
void constraint_cell::dealloc() {
|
|
|
|
switch (m_kind) {
|
2014-05-10 03:25:27 +00:00
|
|
|
case constraint_kind::Eq:
|
|
|
|
delete static_cast<eq_constraint_cell*>(this); break;
|
2014-06-22 17:50:47 +00:00
|
|
|
case constraint_kind::LevelEq:
|
2014-04-28 18:13:51 +00:00
|
|
|
delete static_cast<level_constraint_cell*>(this); break;
|
2014-06-21 15:15:47 +00:00
|
|
|
case constraint_kind::Choice:
|
|
|
|
delete static_cast<choice_constraint_cell*>(this); break;
|
2014-04-28 18:13:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-23 21:19:39 +00:00
|
|
|
constraint::constraint(constraint_cell * ptr):m_ptr(ptr) { lean_assert(m_ptr->get_rc() == 1); }
|
|
|
|
constraint::constraint(constraint const & s):m_ptr(s.m_ptr) { if (m_ptr) m_ptr->inc_ref(); }
|
|
|
|
constraint::constraint(constraint && s):m_ptr(s.m_ptr) { s.m_ptr = nullptr; }
|
|
|
|
constraint::~constraint() { if (m_ptr) m_ptr->dec_ref(); }
|
|
|
|
constraint & constraint::operator=(constraint const & c) { LEAN_COPY_REF(c); }
|
|
|
|
constraint & constraint::operator=(constraint && c) { LEAN_MOVE_REF(c); }
|
|
|
|
constraint_kind constraint::kind() const { lean_assert(m_ptr); return m_ptr->m_kind; }
|
|
|
|
justification const & constraint::get_justification() const { lean_assert(m_ptr); return m_ptr->m_jst; }
|
|
|
|
|
|
|
|
constraint mk_eq_cnstr(expr const & lhs, expr const & rhs, justification const & j) {
|
2014-05-10 03:25:27 +00:00
|
|
|
return constraint(new eq_constraint_cell(lhs, rhs, j));
|
2014-02-23 21:19:39 +00:00
|
|
|
}
|
2014-06-22 17:50:47 +00:00
|
|
|
constraint mk_level_eq_cnstr(level const & lhs, level const & rhs, justification const & j) {
|
2014-02-23 21:19:39 +00:00
|
|
|
return constraint(new level_constraint_cell(lhs, rhs, j));
|
|
|
|
}
|
2014-06-21 15:15:47 +00:00
|
|
|
constraint mk_choice_cnstr(expr const & m, choice_fn const & fn, justification const & j) {
|
|
|
|
lean_assert(is_meta(m));
|
|
|
|
return constraint(new choice_constraint_cell(m, fn, j));
|
2014-02-23 21:19:39 +00:00
|
|
|
}
|
|
|
|
|
2014-05-10 03:25:27 +00:00
|
|
|
expr const & cnstr_lhs_expr(constraint const & c) { lean_assert(is_eq_cnstr(c)); return static_cast<eq_constraint_cell*>(c.raw())->m_lhs; }
|
|
|
|
expr const & cnstr_rhs_expr(constraint const & c) { lean_assert(is_eq_cnstr(c)); return static_cast<eq_constraint_cell*>(c.raw())->m_rhs; }
|
2014-06-22 17:50:47 +00:00
|
|
|
level const & cnstr_lhs_level(constraint const & c) {
|
|
|
|
lean_assert(is_level_eq_cnstr(c));
|
|
|
|
return static_cast<level_constraint_cell*>(c.raw())->m_lhs;
|
|
|
|
}
|
|
|
|
level const & cnstr_rhs_level(constraint const & c) {
|
|
|
|
lean_assert(is_level_eq_cnstr(c));
|
|
|
|
return static_cast<level_constraint_cell*>(c.raw())->m_rhs;
|
|
|
|
}
|
2014-06-21 15:15:47 +00:00
|
|
|
expr const & cnstr_mvar(constraint const & c) { lean_assert(is_choice_cnstr(c)); return static_cast<choice_constraint_cell*>(c.raw())->m_mvar; }
|
|
|
|
choice_fn const & cnstr_choice_fn(constraint const & c) {
|
|
|
|
lean_assert(is_choice_cnstr(c)); return static_cast<choice_constraint_cell*>(c.raw())->m_fn;
|
|
|
|
}
|
2014-02-23 21:19:39 +00:00
|
|
|
|
2014-06-22 23:27:04 +00:00
|
|
|
constraint update_justification(constraint const & c, justification const & j) {
|
|
|
|
switch (c.kind()) {
|
|
|
|
case constraint_kind::Eq:
|
|
|
|
return mk_eq_cnstr(cnstr_lhs_expr(c), cnstr_rhs_expr(c), j);
|
|
|
|
case constraint_kind::LevelEq:
|
|
|
|
return mk_level_eq_cnstr(cnstr_lhs_level(c), cnstr_rhs_level(c), j);
|
|
|
|
case constraint_kind::Choice:
|
|
|
|
return mk_choice_cnstr(cnstr_mvar(c), cnstr_choice_fn(c), j);
|
|
|
|
}
|
|
|
|
lean_unreachable(); // LCOV_EXCL_LINE
|
|
|
|
}
|
|
|
|
|
2014-02-23 21:19:39 +00:00
|
|
|
std::ostream & operator<<(std::ostream & out, constraint const & c) {
|
|
|
|
switch (c.kind()) {
|
|
|
|
case constraint_kind::Eq:
|
|
|
|
out << cnstr_lhs_expr(c) << " ≈ " << cnstr_rhs_expr(c);
|
2014-04-28 19:02:59 +00:00
|
|
|
break;
|
2014-06-22 17:50:47 +00:00
|
|
|
case constraint_kind::LevelEq:
|
2014-05-10 03:25:27 +00:00
|
|
|
out << cnstr_lhs_level(c) << " = " << cnstr_rhs_level(c);
|
2014-04-28 19:02:59 +00:00
|
|
|
break;
|
2014-06-21 15:15:47 +00:00
|
|
|
case constraint_kind::Choice:
|
|
|
|
out << "choice " << cnstr_mvar(c);
|
|
|
|
break;
|
2014-02-23 21:19:39 +00:00
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
}
|