lean4-htt/src/library/quote.cpp
2018-09-06 18:09:22 -07:00

120 lines
3.6 KiB
C++

/*
Copyright (c) 2016 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Leonardo de Moura
*/
#include <string>
#include "util/fresh_name.h"
#include "kernel/abstract.h"
#include "kernel/replace_fn.h"
#include "library/placeholder.h"
#include "library/constants.h"
#include "library/annotation.h"
#include "library/exception.h"
#include "library/util.h"
#include "library/quote.h"
#include "library/type_context.h"
namespace lean {
static std::string * g_expr_quote_opcode = nullptr;
static expr * g_expr = nullptr;
static expr * g_pexpr = nullptr;
static name * g_expr_quote_pre = nullptr;
expr mk_elaborated_expr_quote(expr const & e) {
return mk_quote(true, e);
}
expr mk_unelaborated_expr_quote(expr const & e) {
// We use a transparent annotation instead of the opaque macro above so that the quoted term is accessible to
// collect_locals etc.
return mk_annotation(*g_expr_quote_pre, e);
}
expr mk_pexpr_quote(expr const & e) {
return mk_quote(false, e);
}
bool is_expr_quote(expr const & e) {
return
(is_annotation(e, *g_expr_quote_pre)) ||
(is_quote(e) && quote_is_reflected(e));
}
bool is_pexpr_quote(expr const & e) {
return is_quote(e) && !quote_is_reflected(e);
}
expr const & get_expr_quote_value(expr const & e) {
lean_assert(is_expr_quote(e));
if (is_quote(e)) {
return quote_value(e);
} else {
return get_annotation_arg(e);
}
}
expr const & get_pexpr_quote_value(expr const & e) {
lean_assert(is_pexpr_quote(e));
return quote_value(e);
}
static name * g_antiquote = nullptr;
expr mk_antiquote(expr const & e) { return mk_annotation(*g_antiquote, e); }
bool is_antiquote(expr const & e) { return is_annotation(e, *g_antiquote); }
expr const & get_antiquote_expr(expr const & e) {
lean_assert(is_antiquote(e));
return get_annotation_arg(e);
}
static name * g_quote_fresh = nullptr;
expr mk_pexpr_quote_and_substs(expr const & e, bool is_strict) {
name x("_x");
name_generator ngen(*g_quote_fresh);
buffer<expr> locals;
buffer<expr> aqs;
expr s = replace(e, [&](expr const & t, unsigned) {
if (is_antiquote(t)) {
expr local = mk_local(ngen.next(), x.append_after(locals.size() + 1),
mk_expr_placeholder(), mk_binder_info());
locals.push_back(local);
aqs.push_back(get_antiquote_expr(t));
return some_expr(local);
}
if (is_local(t) && is_strict) {
throw generic_exception(t, "unexpected local in quotation expression");
}
return none_expr();
});
expr r = mk_pexpr_quote(Fun(locals, s));
expr subst = mk_constant(get_pexpr_subst_name());
expr to_pexpr = mk_constant(get_to_pexpr_name());
for (expr const & aq : aqs) {
r = mk_app(subst, r, mk_app(to_pexpr, aq));
}
return r;
}
void initialize_quote() {
g_quote_fresh = new name("_quote_fresh");
register_name_generator_prefix(*g_quote_fresh);
g_expr = new expr(mk_const(get_lean_expr_name()));
g_pexpr = new expr(mk_constant(get_pexpr_name()));
g_antiquote = new name("antiquote");
g_expr_quote_pre = new name("expr_quote_pre");
register_annotation(*g_antiquote);
register_annotation(*g_expr_quote_pre);
}
void finalize_quote() {
delete g_quote_fresh;
delete g_expr_quote_pre;
delete g_expr_quote_opcode;
delete g_expr;
delete g_pexpr;
delete g_antiquote;
}
}