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
|
|
|
|
*/
|
|
|
|
#include "kernel/unification_constraint.h"
|
|
|
|
|
|
|
|
namespace lean {
|
2013-10-23 20:42:14 +00:00
|
|
|
unification_constraint_cell::unification_constraint_cell(unification_constraint_kind k, context const & c, justification const & j):
|
|
|
|
m_kind(k), m_ctx(c), m_justification(j), m_rc(1) {
|
2013-10-01 00:58:46 +00:00
|
|
|
}
|
2013-10-02 00:25:17 +00:00
|
|
|
unification_constraint_cell::~unification_constraint_cell() {
|
|
|
|
}
|
2013-10-01 00:58:46 +00:00
|
|
|
void unification_constraint_cell::dealloc() {
|
2013-10-02 00:25:17 +00:00
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
|
|
|
static format add_context(formatter const & fmt, options const & opts, context const & ctx, format const & body) {
|
|
|
|
bool unicode = get_pp_unicode(opts);
|
|
|
|
format ctx_fmt = fmt(ctx, opts);
|
|
|
|
format turnstile = unicode ? format("\u22A2") /* ⊢ */ : format("|-");
|
|
|
|
return group(format{ctx_fmt, space(), turnstile, line(), body});
|
|
|
|
}
|
|
|
|
|
2013-10-23 20:42:14 +00:00
|
|
|
static format add_justification(formatter const & fmt, options const & opts, format const & body, justification const & jst, pos_info_provider const * p, bool include_justification) {
|
|
|
|
if (jst && include_justification) {
|
2013-10-02 00:25:17 +00:00
|
|
|
unsigned indent = get_pp_indent(opts);
|
2013-10-23 20:42:14 +00:00
|
|
|
return format{body, line(), format("Justification:"), nest(indent, compose(line(), jst.pp(fmt, opts, p)))};
|
2013-10-02 00:25:17 +00:00
|
|
|
} else {
|
|
|
|
return body;
|
2013-10-01 00:58:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-02 00:25:17 +00:00
|
|
|
static format mk_binary(formatter const & fmt, options const & opts, context const & ctx, expr const & lhs, expr const & rhs, format const & op) {
|
|
|
|
format lhs_fmt = fmt(ctx, lhs, false, opts);
|
|
|
|
format rhs_fmt = fmt(ctx, rhs, false, opts);
|
|
|
|
return group(format{lhs_fmt, space(), op, line(), rhs_fmt});
|
|
|
|
}
|
|
|
|
|
|
|
|
static format mk_unification_op(options const & opts) {
|
|
|
|
bool unicode = get_pp_unicode(opts);
|
|
|
|
return unicode ? format("\u2248") /* ≈ */ : format("=?=");
|
|
|
|
}
|
|
|
|
|
2013-10-23 20:42:14 +00:00
|
|
|
unification_constraint_eq::unification_constraint_eq(context const & c, expr const & lhs, expr const & rhs, justification const & j):
|
|
|
|
unification_constraint_cell(unification_constraint_kind::Eq, c, j),
|
2013-10-01 00:58:46 +00:00
|
|
|
m_lhs(lhs),
|
|
|
|
m_rhs(rhs) {
|
|
|
|
}
|
|
|
|
|
2013-10-02 00:25:17 +00:00
|
|
|
unification_constraint_eq::~unification_constraint_eq() {
|
|
|
|
}
|
|
|
|
|
2013-10-23 20:42:14 +00:00
|
|
|
format unification_constraint_eq::pp(formatter const & fmt, options const & opts, pos_info_provider const * p, bool include_justification) const {
|
2013-10-02 00:25:17 +00:00
|
|
|
format op = mk_unification_op(opts);
|
|
|
|
format body = mk_binary(fmt, opts, m_ctx, m_lhs, m_rhs, op);
|
|
|
|
body = add_context(fmt, opts, m_ctx, body);
|
2013-10-23 20:42:14 +00:00
|
|
|
return add_justification(fmt, opts, body, m_justification, p, include_justification);
|
2013-10-02 00:25:17 +00:00
|
|
|
}
|
|
|
|
|
2013-10-23 20:42:14 +00:00
|
|
|
unification_constraint_convertible::unification_constraint_convertible(context const & c, expr const & from, expr const & to, justification const & j):
|
|
|
|
unification_constraint_cell(unification_constraint_kind::Convertible, c, j),
|
2013-10-01 00:58:46 +00:00
|
|
|
m_from(from),
|
|
|
|
m_to(to) {
|
|
|
|
}
|
|
|
|
|
2013-10-02 00:25:17 +00:00
|
|
|
unification_constraint_convertible::~unification_constraint_convertible() {
|
|
|
|
}
|
|
|
|
|
2013-10-23 20:42:14 +00:00
|
|
|
format unification_constraint_convertible::pp(formatter const & fmt, options const & opts, pos_info_provider const * p, bool include_justification) const {
|
2013-10-02 00:25:17 +00:00
|
|
|
bool unicode = get_pp_unicode(opts);
|
|
|
|
format op = unicode ? format("\u227A") /* ≺ */ : format("<<");
|
|
|
|
format body = mk_binary(fmt, opts, m_ctx, m_from, m_to, op);
|
|
|
|
body = add_context(fmt, opts, m_ctx, body);
|
2013-10-23 20:42:14 +00:00
|
|
|
return add_justification(fmt, opts, body, m_justification, p, include_justification);
|
2013-10-02 00:25:17 +00:00
|
|
|
}
|
|
|
|
|
2013-10-23 20:42:14 +00:00
|
|
|
unification_constraint_max::unification_constraint_max(context const & c, expr const & lhs1, expr const & lhs2, expr const & rhs, justification const & j):
|
|
|
|
unification_constraint_cell(unification_constraint_kind::Max, c, j),
|
2013-10-01 00:58:46 +00:00
|
|
|
m_lhs1(lhs1),
|
|
|
|
m_lhs2(lhs2),
|
|
|
|
m_rhs(rhs) {
|
|
|
|
}
|
|
|
|
|
2013-10-02 00:25:17 +00:00
|
|
|
unification_constraint_max::~unification_constraint_max() {
|
|
|
|
}
|
|
|
|
|
2013-10-23 20:42:14 +00:00
|
|
|
format unification_constraint_max::pp(formatter const & fmt, options const & opts, pos_info_provider const * p, bool include_justification) const {
|
2013-10-02 00:25:17 +00:00
|
|
|
format op = mk_unification_op(opts);
|
|
|
|
format lhs1_fmt = fmt(m_ctx, m_lhs1, false, opts);
|
|
|
|
format lhs2_fmt = fmt(m_ctx, m_lhs2, false, opts);
|
|
|
|
format rhs_fmt = fmt(m_ctx, m_rhs, false, opts);
|
2013-10-23 19:00:29 +00:00
|
|
|
format body = group(format{format("max"), lp(), lhs1_fmt, comma(), nest(4, compose(line(), lhs2_fmt)), rp(), space(), op, line(), rhs_fmt});
|
2013-10-02 00:25:17 +00:00
|
|
|
body = add_context(fmt, opts, m_ctx, body);
|
2013-10-23 20:42:14 +00:00
|
|
|
return add_justification(fmt, opts, body, m_justification, p, include_justification);
|
2013-10-02 00:25:17 +00:00
|
|
|
}
|
|
|
|
|
2013-10-25 15:06:21 +00:00
|
|
|
unification_constraint_choice::unification_constraint_choice(context const & c, expr const & mvar, unsigned num, expr const * choices, justification const & j):
|
2013-10-23 20:42:14 +00:00
|
|
|
unification_constraint_cell(unification_constraint_kind::Choice, c, j),
|
2013-10-01 00:58:46 +00:00
|
|
|
m_mvar(mvar),
|
2013-10-25 15:06:21 +00:00
|
|
|
m_choices(choices, choices + num) {
|
2013-10-01 00:58:46 +00:00
|
|
|
}
|
|
|
|
|
2013-10-02 00:25:17 +00:00
|
|
|
unification_constraint_choice::~unification_constraint_choice() {
|
|
|
|
}
|
|
|
|
|
2013-10-23 20:42:14 +00:00
|
|
|
format unification_constraint_choice::pp(formatter const & fmt, options const & opts, pos_info_provider const * p, bool include_justification) const {
|
2013-10-02 00:25:17 +00:00
|
|
|
bool unicode = get_pp_unicode(opts);
|
|
|
|
format m_fmt = fmt(m_ctx, m_mvar, false, opts);
|
|
|
|
format eq_op = mk_unification_op(opts);
|
|
|
|
format or_op = unicode ? format("\u2295") : format("OR");
|
|
|
|
format body;
|
2013-10-25 15:06:21 +00:00
|
|
|
for (unsigned i = 0; i < m_choices.size(); i++) {
|
2013-10-14 15:16:23 +00:00
|
|
|
body += group(paren(format{m_fmt, space(), eq_op, compose(line(), fmt(m_ctx, m_choices[i], false, opts))}));
|
2013-10-25 15:06:21 +00:00
|
|
|
if (i + 1 < m_choices.size())
|
2013-10-02 00:25:17 +00:00
|
|
|
body += format{space(), or_op, line()};
|
|
|
|
}
|
|
|
|
body = group(body);
|
|
|
|
body = add_context(fmt, opts, m_ctx, body);
|
2013-10-23 20:42:14 +00:00
|
|
|
return add_justification(fmt, opts, body, m_justification, p, include_justification);
|
2013-10-02 00:25:17 +00:00
|
|
|
}
|
|
|
|
|
2013-10-23 20:42:14 +00:00
|
|
|
unification_constraint mk_eq_constraint(context const & c, expr const & lhs, expr const & rhs, justification const & j) {
|
|
|
|
return unification_constraint(new unification_constraint_eq(c, lhs, rhs, j));
|
2013-10-01 00:58:46 +00:00
|
|
|
}
|
|
|
|
|
2013-10-23 20:42:14 +00:00
|
|
|
unification_constraint mk_convertible_constraint(context const & c, expr const & from, expr const & to, justification const & j) {
|
|
|
|
return unification_constraint(new unification_constraint_convertible(c, from, to, j));
|
2013-10-01 00:58:46 +00:00
|
|
|
}
|
|
|
|
|
2013-10-23 20:42:14 +00:00
|
|
|
unification_constraint mk_max_constraint(context const & c, expr const & lhs1, expr const & lhs2, expr const & rhs, justification const & j) {
|
|
|
|
return unification_constraint(new unification_constraint_max(c, lhs1, lhs2, rhs, j));
|
2013-10-01 00:58:46 +00:00
|
|
|
}
|
|
|
|
|
2013-10-23 20:42:14 +00:00
|
|
|
unification_constraint mk_choice_constraint(context const & c, expr const & mvar, unsigned num, expr const * choices, justification const & j) {
|
2013-10-25 15:06:21 +00:00
|
|
|
return unification_constraint(new unification_constraint_choice(c, mvar, num, choices, j));
|
2013-10-01 00:58:46 +00:00
|
|
|
}
|
|
|
|
|
2013-10-23 20:42:14 +00:00
|
|
|
unification_constraint mk_choice_constraint(context const & c, expr const & mvar, std::initializer_list<expr> const & choices, justification const & j) {
|
|
|
|
return mk_choice_constraint(c, mvar, choices.size(), choices.begin(), j);
|
2013-10-01 00:58:46 +00:00
|
|
|
}
|
|
|
|
}
|