143 lines
5.7 KiB
C++
143 lines
5.7 KiB
C++
|
/*
|
||
|
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/builtin.h"
|
||
|
#include "library/placeholder.h"
|
||
|
#include "frontends/lean/parser_calc.h"
|
||
|
#include "frontends/lean/parser_imp.h"
|
||
|
#include "frontends/lean/operator_info.h"
|
||
|
#include "frontends/lean/frontend.h"
|
||
|
|
||
|
namespace lean {
|
||
|
bool calc_proof_parser::supports(expr const & op1) const {
|
||
|
return
|
||
|
std::find_if(m_supported_operators.begin(), m_supported_operators.end(), [&](op_data const & op2) { return op1 == op2.m_fn; })
|
||
|
!= m_supported_operators.end();
|
||
|
}
|
||
|
|
||
|
void calc_proof_parser::add_supported_operator(op_data const & op1) {
|
||
|
if (supports(op1.m_fn))
|
||
|
throw exception("operator already supported in the calculational proof manager");
|
||
|
m_supported_operators = cons(op1, m_supported_operators);
|
||
|
}
|
||
|
|
||
|
optional<trans_data> calc_proof_parser::find_trans_data(expr const & op1, expr const & op2) const {
|
||
|
auto it = std::find_if(m_trans_ops.begin(), m_trans_ops.end(),
|
||
|
[&](std::tuple<expr, expr, trans_data> const & entry) { return std::get<0>(entry) == op1 && std::get<1>(entry) == op2; });
|
||
|
if (it == m_trans_ops.end())
|
||
|
return optional<trans_data>();
|
||
|
else
|
||
|
return optional<trans_data>(std::get<2>(*it));
|
||
|
}
|
||
|
|
||
|
void calc_proof_parser::add_trans_step(expr const & op1, expr const & op2, trans_data const & d) {
|
||
|
if (!supports(op1) || !supports(op2) || !supports(d.m_rop))
|
||
|
throw exception("invalid transitivity step in calculational proof manager, operator not supported");
|
||
|
if (find_trans_data(op1, op2))
|
||
|
throw exception("transitivity step is already supported in the calculational proof manager");
|
||
|
if (d.m_num_args < 5)
|
||
|
throw exception("transitivity step must have at least 5 arguments");
|
||
|
m_trans_ops.emplace_front(op1, op2, d);
|
||
|
}
|
||
|
|
||
|
calc_proof_parser::calc_proof_parser() {
|
||
|
expr imp = mk_implies_fn();
|
||
|
expr eq = mk_homo_eq_fn();
|
||
|
expr iff = mk_iff_fn();
|
||
|
add_supported_operator(op_data(imp, 2));
|
||
|
add_supported_operator(op_data(eq, 3));
|
||
|
add_supported_operator(op_data(iff, 2));
|
||
|
add_trans_step(eq, eq, trans_data(mk_trans_fn(), 6, eq));
|
||
|
add_trans_step(eq, imp, trans_data(mk_constant("EqImpTrans"), 5, imp));
|
||
|
add_trans_step(imp, eq, trans_data(mk_constant("ImpEqTrans"), 5, imp));
|
||
|
add_trans_step(imp, imp, trans_data(mk_constant("ImpTrans"), 5, imp));
|
||
|
add_trans_step(iff, iff, trans_data(mk_trans_fn(), 6, iff));
|
||
|
add_trans_step(iff, imp, trans_data(mk_constant("EqImpTrans"), 5, imp));
|
||
|
add_trans_step(imp, iff, trans_data(mk_constant("ImpEqTrans"), 5, imp));
|
||
|
add_trans_step(eq, iff, trans_data(mk_trans_fn(), 6, iff));
|
||
|
add_trans_step(iff, eq, trans_data(mk_trans_fn(), 6, iff));
|
||
|
}
|
||
|
|
||
|
optional<expr> calc_proof_parser::find_op(operator_info const & op, pos_info const & p) const {
|
||
|
if (!op)
|
||
|
return none_expr();
|
||
|
for (auto d : op.get_denotations()) {
|
||
|
// TODO(Leo): I'm ignoring overloading.
|
||
|
if (supports(d))
|
||
|
return some_expr(d);
|
||
|
}
|
||
|
throw parser_error("invalid calculational proof, operator is not supported", p);
|
||
|
return none_expr();
|
||
|
}
|
||
|
|
||
|
expr calc_proof_parser::parse_op(parser_imp & imp) const {
|
||
|
environment const & env = imp.get_environment();
|
||
|
auto p = imp.pos();
|
||
|
name id = imp.check_identifier_next("invalid calculational proof, identifier expected");
|
||
|
if (auto r = find_op(find_led(env, id), p))
|
||
|
return *r;
|
||
|
if (auto r = find_op(find_nud(env, id), p))
|
||
|
return *r;
|
||
|
expr e = imp.get_name_ref(id, p, false /* do not process implicit args */);
|
||
|
if (!supports(e))
|
||
|
throw parser_error("invalid calculational proof, operator is not supported", p);
|
||
|
return e;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
\brief Parse
|
||
|
|
||
|
calc expr op expr : proof1
|
||
|
... op expr : proof2
|
||
|
|
||
|
... op expr : proofn
|
||
|
*/
|
||
|
expr calc_proof_parser::parse(parser_imp & imp) const {
|
||
|
auto p = imp.pos();
|
||
|
expr first = imp.parse_expr();
|
||
|
if (!is_app(first))
|
||
|
throw parser_error("invalid calculational proof, first expression must be an application", p);
|
||
|
expr op = arg(first, 0);
|
||
|
if (!supports(op))
|
||
|
throw parser_error("invalid calculational proof, first expression is not an application of a supported operator", p);
|
||
|
if (num_args(first) < 3)
|
||
|
throw parser_error("invalid calculational proof, first expression must be an application of a binary operator (modulo implicit arguments)", p);
|
||
|
imp.check_colon_next("invalid calculational proof, ':' expected");
|
||
|
unsigned num = num_args(first);
|
||
|
expr result = imp.parse_expr();
|
||
|
expr rhs = arg(first, num - 2);
|
||
|
expr lhs = arg(first, num - 1);
|
||
|
while (imp.curr() == scanner::token::Ellipsis) {
|
||
|
imp.next();
|
||
|
p = imp.pos();
|
||
|
expr new_op = parse_op(imp);
|
||
|
auto tdata = find_trans_data(op, new_op);
|
||
|
if (!tdata)
|
||
|
throw parser_error("invalid calculational proof, operators cannot be combined", p);
|
||
|
expr new_lhs = imp.parse_expr();
|
||
|
p = imp.pos();
|
||
|
imp.check_colon_next("invalid calculational proof, ':' expected");
|
||
|
expr step_pr = imp.parse_expr();
|
||
|
buffer<expr> args;
|
||
|
args.push_back(tdata->m_fn);
|
||
|
for (unsigned i = 0; i < tdata->m_num_args - 5; i++) {
|
||
|
// transitivity step has implicit arguments
|
||
|
args.push_back(imp.save(mk_placeholder(), p));
|
||
|
}
|
||
|
args.push_back(rhs);
|
||
|
args.push_back(lhs);
|
||
|
args.push_back(new_lhs);
|
||
|
args.push_back(result);
|
||
|
args.push_back(step_pr);
|
||
|
result = mk_app(args);
|
||
|
op = tdata->m_rop;
|
||
|
lhs = new_lhs;
|
||
|
}
|
||
|
std::cout << result << "\n";
|
||
|
return result;
|
||
|
}
|
||
|
}
|