refactor(library/aliases): aliases are from name to names

Signed-off-by: Leonardo de Moura <leonardo@microsoft.com>
This commit is contained in:
Leonardo de Moura 2014-07-27 21:01:59 -07:00
parent 5555d620cf
commit 864fdd37da
12 changed files with 108 additions and 109 deletions

View file

@ -12,9 +12,6 @@ section
abbreviation id (a : A) : A
:= a
abbreviation const (a : A) : B → A
:= λx, a
abbreviation on_fun (f : B → B → C) (g : A → B) : A → A → C
:= λx y, f (g x) (g y)
@ -22,6 +19,9 @@ section
:= λx y, op (f x y) (g x y)
end
abbreviation const {A : Type} (B : Type) (a : A) : B → A
:= λx, a
abbreviation dcompose {A : Type} {B : A → Type} {C : Π {x : A}, B x → Type} (f : Π {x : A} (y : B x), C y) (g : Πx, B x) : Πx, C (g x)
:= λx, f (g x)

View file

@ -1,7 +1,24 @@
-- Copyright (c) 2014 Microsoft Corporation. All rights reserved.
-- Released under Apache 2.0 license as described in the file LICENSE.
-- Author: Leonardo de Moura
import logic
import logic function
using function
-- Function extensionality
axiom funext : ∀ {A : Type} {B : A → Type} {f g : Π x, B x} (H : ∀ x, f x = g x), f = g
namespace function
section
parameters {A : Type} {B : Type} {C : Type} {D : Type}
theorem compose_assoc (f : C → D) (g : B → C) (h : A → B) : (f ∘ g) ∘ h = f ∘ (g ∘ h)
:= funext (take x, refl _)
theorem compose_id_left (f : A → B) : id ∘ f = f
:= funext (take x, refl _)
theorem compose_id_right (f : A → B) : f ∘ id = f
:= funext (take x, refl _)
theorem compose_const_right (f : B → C) (b : B) : f ∘ (const A b) = const A (f b)
:= funext (take x, refl _)
end end

View file

@ -201,7 +201,7 @@ environment using_cmd(parser & p) {
name to_id = p.check_id_next("invalid 'using' command renaming, identifier expected");
check_identifier(p, env, ns, from_id);
exceptions.push_back(from_id);
env = add_alias(env, to_id, mk_constant(ns+from_id));
env = add_expr_alias(env, to_id, ns+from_id);
}
} else if (p.curr_is_token_or_id(g_hiding)) {
p.next();
@ -217,7 +217,7 @@ environment using_cmd(parser & p) {
name id = p.get_name_val();
p.next();
check_identifier(p, env, ns, id);
env = add_alias(env, id, mk_constant(ns+id));
env = add_expr_alias(env, id, ns+id);
}
} else {
throw parser_error("invalid 'using' command option, identifier, 'hiding' or 'renaming' expected", p.pos());

View file

@ -40,7 +40,7 @@ environment universe_cmd(parser & p) {
name full_n = ns + n;
env = module::add_universe(env, full_n);
if (!ns.is_anonymous())
env = add_alias(env, n, mk_global_univ(full_n));
env = add_level_alias(env, n, full_n);
}
return env;
}
@ -87,7 +87,7 @@ static environment declare_var(parser & p, environment env,
else
env = module::add(env, check(env, mk_var_decl(full_n, ls, type)));
if (!ns.is_anonymous())
env = add_alias(env, n, mk_constant(full_n));
env = add_expr_alias(env, n, full_n);
return env;
}
}
@ -279,7 +279,7 @@ environment definition_cmd_core(parser & p, bool is_theorem, bool _is_opaque) {
env = module::add(env, check(env, mk_definition(env, real_n, append(ls, new_ls), type, value, modifiers.m_is_opaque)));
}
if (real_n != n)
env = add_decl_alias(env, n, mk_constant(real_n));
env = add_expr_alias_rec(env, n, real_n);
if (modifiers.m_is_instance)
env = add_instance(env, real_n);
if (modifiers.m_is_coercion)

View file

@ -514,7 +514,7 @@ struct inductive_cmd_fn {
m_p.add_local_expr(id, r);
}
if (full_id != id)
env = add_decl_alias(env, id, mk_constant(full_id));
env = add_expr_alias_rec(env, id, full_id);
return env;
}

View file

@ -431,8 +431,8 @@ level parser::parse_level_id() {
return *it;
if (m_env.is_universe(id))
return mk_global_univ(id);
if (auto it = get_alias_level(m_env, id))
return *it;
if (auto it = get_level_alias(m_env, id))
return mk_global_univ(*it);
throw parser_error(sstream() << "unknown universe '" << id << "'", p);
}
@ -841,13 +841,13 @@ expr parser::id_to_expr(name const & id, pos_info const & p) {
if (m_env.find(id))
r = save_pos(mk_constant(id, ls), p);
// aliases
auto as = get_alias_exprs(m_env, id);
auto as = get_expr_aliases(m_env, id);
if (!is_nil(as)) {
buffer<expr> new_as;
if (r)
new_as.push_back(*r);
for (auto const & e : as) {
new_as.push_back(copy_with_new_pos(propagate_levels(e, ls), p));
new_as.push_back(copy_with_new_pos(mk_constant(e, ls), p));
}
r = save_pos(mk_choice(new_as.size(), new_as.data()), p);
}

View file

@ -175,7 +175,7 @@ auto pretty_fn::pp_sort(expr const & e) -> result {
auto pretty_fn::pp_const(expr const & e) -> result {
name n = const_name(e);
if (!m_full_names) {
if (auto it = is_aliased(m_env, mk_constant(n))) { // TODO(Leo): fix is_aliased should get a name as argument
if (auto it = is_expr_aliased(m_env, n)) {
n = *it;
} else {
for (name const & ns : get_namespaces(m_env)) {

View file

@ -25,18 +25,18 @@ static environment update(environment const & env, aliases_ext const & ext);
struct aliases_ext : public environment_extension {
struct state {
bool m_in_section;
rb_map<name, list<expr>, name_quick_cmp> m_aliases;
rb_map<expr, name, expr_quick_cmp> m_inv_aliases;
rb_map<name, level, name_quick_cmp> m_level_aliases;
rb_map<level, name, level_quick_cmp> m_inv_level_aliases;
rb_map<name, list<name>, name_quick_cmp> m_aliases;
rb_map<name, name, name_quick_cmp> m_inv_aliases;
rb_map<name, name, name_quick_cmp> m_level_aliases;
rb_map<name, name, name_quick_cmp> m_inv_level_aliases;
state():m_in_section(false) {}
void add_alias(name const & a, expr const & e) {
void add_expr_alias(name const & a, name const & e) {
auto it = m_aliases.find(a);
if (it)
m_aliases.insert(a, list<expr>(e, filter(*it, [&](expr const & t) { return t != e; })));
m_aliases.insert(a, list<name>(e, filter(*it, [&](name const & t) { return t != e; })));
else
m_aliases.insert(a, list<expr>(e));
m_aliases.insert(a, list<name>(e));
m_inv_aliases.insert(e, a);
}
};
@ -44,11 +44,11 @@ struct aliases_ext : public environment_extension {
state m_state;
list<state> m_scopes;
void add_alias(name const & a, expr const & e) {
m_state.add_alias(a, e);
void add_expr_alias(name const & a, name const & e) {
m_state.add_expr_alias(a, e);
}
void add_alias(name const & a, level const & l) {
void add_level_alias(name const & a, name const & l) {
auto it = m_state.m_level_aliases.find(a);
if (it)
throw exception(sstream() << "universe level alias '" << a << "' shadows existing alias");
@ -56,25 +56,25 @@ struct aliases_ext : public environment_extension {
m_state.m_inv_level_aliases.insert(l, a);
}
list<state> add_decl_alias_core(list<state> const & scopes, name const & a, expr const & e) {
list<state> add_expr_alias_rec_core(list<state> const & scopes, name const & a, name const & e) {
if (empty(scopes)) {
return scopes;
} else {
state s = head(scopes);
s.add_alias(a, e);
s.add_expr_alias(a, e);
if (s.m_in_section) {
return cons(s, add_decl_alias_core(tail(scopes), a, e));
return cons(s, add_expr_alias_rec_core(tail(scopes), a, e));
} else {
return cons(s, tail(scopes));
}
}
}
void add_decl_alias(name const & a, expr const & e) {
void add_expr_alias_rec(name const & a, name const & e) {
if (m_state.m_in_section) {
m_scopes = add_decl_alias_core(m_scopes, a, e);
m_scopes = add_expr_alias_rec_core(m_scopes, a, e);
} else {
add_alias(a, e);
add_expr_alias(a, e);
}
}
@ -131,28 +131,28 @@ static void check_atomic(name const & a) {
throw exception(sstream() << "invalid alias '" << a << "', aliases must be atomic names");
}
environment add_alias(environment const & env, name const & a, expr const & e) {
environment add_expr_alias(environment const & env, name const & a, name const & e) {
check_atomic(a);
aliases_ext ext = get_extension(env);
ext.add_alias(a, e);
ext.add_expr_alias(a, e);
return update(env, ext);
}
environment add_decl_alias(environment const & env, name const & a, expr const & e) {
environment add_expr_alias_rec(environment const & env, name const & a, name const & e) {
check_atomic(a);
aliases_ext ext = get_extension(env);
ext.add_decl_alias(a, e);
ext.add_expr_alias_rec(a, e);
return update(env, ext);
}
optional<name> is_aliased(environment const & env, expr const & t) {
optional<name> is_expr_aliased(environment const & env, name const & t) {
auto it = get_extension(env).m_state.m_inv_aliases.find(t);
return it ? optional<name>(*it) : optional<name>();
}
list<expr> get_alias_exprs(environment const & env, name const & n) {
list<name> get_expr_aliases(environment const & env, name const & n) {
auto it = get_extension(env).m_state.m_aliases.find(n);
return it ? *it : list<expr>();
return it ? *it : list<name>();
}
static void check_no_shadow(environment const & env, name const & a) {
@ -160,22 +160,22 @@ static void check_no_shadow(environment const & env, name const & a) {
throw exception(sstream() << "universe level alias '" << a << "' shadows existing global universe level");
}
environment add_alias(environment const & env, name const & a, level const & l) {
environment add_level_alias(environment const & env, name const & a, name const & l) {
check_atomic(a);
check_no_shadow(env, a);
aliases_ext ext = get_extension(env);
ext.add_alias(a, l);
ext.add_level_alias(a, l);
return update(env, ext);
}
optional<name> is_aliased(environment const & env, level const & l) {
optional<name> is_level_aliased(environment const & env, name const & l) {
auto it = get_extension(env).m_state.m_inv_level_aliases.find(l);
return it ? optional<name>(*it) : optional<name>();
}
optional<level> get_alias_level(environment const & env, name const & n) {
optional<name> get_level_alias(environment const & env, name const & n) {
auto it = get_extension(env).m_state.m_level_aliases.find(n);
return it ? some_level(*it) : optional<level>();
return it ? optional<name>(*it) : optional<name>();
}
// Return true iff \c n is (prefix + ex) for some ex in exceptions
@ -189,9 +189,7 @@ environment add_aliases(environment const & env, name const & prefix, name const
env.for_each_declaration([&](declaration const & d) {
if (is_prefix_of(prefix, d.get_name()) && !is_exception(d.get_name(), prefix, num_exceptions, exceptions)) {
name a = d.get_name().replace_prefix(prefix, new_prefix);
levels ls = map2<level>(d.get_univ_params(), [](name const &) { return mk_level_placeholder(); });
expr c = mk_constant(d.get_name(), ls);
ext.add_alias(a, c);
ext.add_expr_alias(a, d.get_name());
}
});
env.for_each_universe([&](name const & u) {
@ -199,32 +197,31 @@ environment add_aliases(environment const & env, name const & prefix, name const
name a = u.replace_prefix(prefix, new_prefix);
if (env.is_universe(a))
throw exception(sstream() << "universe level alias '" << a << "' shadows existing global universe level");
ext.add_alias(a, mk_global_univ(u));
ext.add_level_alias(a, u);
}
});
return update(env, ext);
}
static int add_alias(lua_State * L) {
if (is_expr(L, 3))
return push_environment(L, add_alias(to_environment(L, 1), to_name_ext(L, 2), to_expr(L, 3)));
else
return push_environment(L, add_alias(to_environment(L, 1), to_name_ext(L, 2), to_level(L, 3)));
static int add_expr_alias(lua_State * L) {
return push_environment(L, add_expr_alias(to_environment(L, 1), to_name_ext(L, 2), to_name_ext(L, 3)));
}
static int add_level_alias(lua_State * L) {
return push_environment(L, add_level_alias(to_environment(L, 1), to_name_ext(L, 2), to_name_ext(L, 3)));
}
static int is_aliased(lua_State * L) {
if (is_expr(L, 2))
return push_optional_name(L, is_aliased(to_environment(L, 1), to_expr(L, 2)));
else
return push_optional_name(L, is_aliased(to_environment(L, 1), to_level(L, 2)));
static int is_expr_aliased(lua_State * L) {
return push_optional_name(L, is_expr_aliased(to_environment(L, 1), to_name_ext(L, 2)));
}
static int is_level_aliased(lua_State * L) {
return push_optional_name(L, is_level_aliased(to_environment(L, 1), to_name_ext(L, 2)));
}
static int get_alias_exprs(lua_State * L) {
return push_list_expr(L, get_alias_exprs(to_environment(L, 1), to_name_ext(L, 2)));
static int get_expr_aliases(lua_State * L) {
return push_list_name(L, get_expr_aliases(to_environment(L, 1), to_name_ext(L, 2)));
}
static int get_alias_level(lua_State * L) {
return push_optional_level(L, get_alias_level(to_environment(L, 1), to_name_ext(L, 2)));
static int get_level_alias(lua_State * L) {
return push_optional_name(L, get_level_alias(to_environment(L, 1), to_name_ext(L, 2)));
}
static int add_aliases(lua_State * L) {
@ -248,10 +245,12 @@ static int add_aliases(lua_State * L) {
}
void open_aliases(lua_State * L) {
SET_GLOBAL_FUN(add_alias, "add_alias");
SET_GLOBAL_FUN(add_expr_alias, "add_expr_alias");
SET_GLOBAL_FUN(add_level_alias, "add_level_alias");
SET_GLOBAL_FUN(is_expr_aliased, "is_expr_aliased");
SET_GLOBAL_FUN(is_level_aliased, "is_level_aliased");
SET_GLOBAL_FUN(get_expr_aliases, "get_expr_aliases");
SET_GLOBAL_FUN(get_level_alias, "get_level_alias");
SET_GLOBAL_FUN(add_aliases, "add_aliases");
SET_GLOBAL_FUN(is_aliased, "is_aliased");
SET_GLOBAL_FUN(get_alias_exprs, "get_alias_exprs");
SET_GLOBAL_FUN(get_alias_level, "get_alias_level");
}
}

View file

@ -10,32 +10,32 @@ Author: Leonardo de Moura
#include "kernel/environment.h"
namespace lean {
/** \brief Add the alias \c a for expression \c e. \c e must not have free variables. */
environment add_alias(environment const & env, name const & a, expr const & e);
/** \brief Add the alias \c a for \c e. */
environment add_expr_alias(environment const & env, name const & a, name const & e);
/**
\brief Add alias \c a for expression \c e, and also add it to all parent scopes
\brief Add alias \c a for \c e, and also add it to all parent scopes
until in a namespace scope.
*/
environment add_decl_alias(environment const & env, name const & a, expr const & e);
environment add_expr_alias_rec(environment const & env, name const & a, name const & e);
/** \brief If \c t is aliased in \c env, then return its name. Otherwise, return none. */
optional<name> is_aliased(environment const & env, expr const & t);
optional<name> is_expr_aliased(environment const & env, name const & t);
/** \brief Return expressions associated with the given alias. */
list<expr> get_alias_exprs(environment const & env, name const & n);
list<name> get_expr_aliases(environment const & env, name const & n);
/**
\brief Add the alias \c a for the level expression \c l. An error is generated if the new alias shadows
\brief Add the alias \c a for level \c l. An error is generated if the new alias shadows
existing aliases and/or declarations. We don't have "choice" construct for universe
levels.
*/
environment add_alias(environment const & env, name const & a, level const & l);
environment add_level_alias(environment const & env, name const & a, name const & l);
/** \brief If \c l is aliased in \c env, then return its name. Otherwise, return none. */
optional<name> is_aliased(environment const & env, level const & l);
optional<name> is_level_aliased(environment const & env, name const & l);
/** \brief Return the level associated with the given alias. */
optional<level> get_alias_level(environment const & env, name const & n);
optional<name> get_level_alias(environment const & env, name const & n);
/**
\brief Create an alias for each declaration named <tt>prefix.rest</tt>.

View file

@ -18,9 +18,9 @@ env = add_inductive(env,
env:for_each_decl(function(d) print(d:name()) end)
env = add_aliases(env, "nat", "natural")
assert(get_alias_exprs(env, {"natural", "zero"}):head() == zero)
assert(get_alias_exprs(env, {"natural", "nat"}):head() == nat)
assert(is_aliased(env, nat) == name("natural", "nat"))
assert(get_expr_aliases(env, {"natural", "zero"}):head() == name("nat", "zero"))
assert(get_expr_aliases(env, {"natural", "nat"}):head() == name("nat", "nat"))
assert(is_expr_aliased(env, {"nat", "nat"}) == name("natural", "nat"))
local A = Local("A", U_l)
@ -30,21 +30,21 @@ env = add_inductive(env,
name("list", "cons"), Pi(A, mk_arrow(A, list_l(A), list_l(A))))
env = add_aliases(env, "list", "lst")
print(not get_alias_exprs(env, {"lst", "list_rec"}):is_nil())
print(not get_expr_aliases(env, {"lst", "list_rec"}):is_nil())
env = add_aliases(env, "list")
print(get_alias_exprs(env, "list_rec"):head())
assert(not get_alias_exprs(env, "list_rec"):is_nil())
assert(not get_alias_exprs(env, {"lst", "list_rec"}):is_nil())
print(get_expr_aliases(env, "list_rec"):head())
assert(not get_expr_aliases(env, "list_rec"):is_nil())
assert(not get_expr_aliases(env, {"lst", "list_rec"}):is_nil())
env = add_aliases(env, "list", "l")
print(get_alias_exprs(env, {"l", "list"}):head())
print(get_alias_exprs(env, {"l", "cons"}):head())
print(get_expr_aliases(env, {"l", "list"}):head())
print(get_expr_aliases(env, {"l", "cons"}):head())
local A = Local("A", mk_sort(1))
local R = Local("R", mk_arrow(A, A, Prop))
local a = Local("a", A)
local b = Local("b", A)
env = add_alias(env, "z", zero)
assert(get_alias_exprs(env, "z"):head() == zero)
assert(get_alias_exprs(env, "zz"):is_nil())
env = add_expr_alias(env, "z", name("nat", "zero"))
assert(get_expr_aliases(env, "z"):head() == name("nat", "zero"))
assert(get_expr_aliases(env, "zz"):is_nil())

View file

@ -3,10 +3,6 @@ local env = environment()
local env = environment()
local l = mk_param_univ("l")
local u = mk_global_univ("u")
env = add_alias(env, "m", max_univ(l, u))
env = add_alias(env, "l1", max_univ(l, 1))
assert(is_aliased(env, max_univ(l, u)) == name("m"))
assert(not is_aliased(env, max_univ(u, l)))
assert(get_alias_level(env, "m") == max_univ(l, u))
assert(get_alias_level(env, "l1") == max_univ(l, 1))
assert(not get_alias_level(env, "l2"))
env = add_level_alias(env, "m", "l")
env = add_level_alias(env, "l1", "l")
assert(is_level_aliased(env, "l") == name("l1"))

View file

@ -1,13 +0,0 @@
local env = environment()
env = add_decl(env, mk_var_decl(name("foo", "x"), Prop))
env = add_decl(env, mk_var_decl(name("foo", "y"), Prop))
env = add_decl(env, mk_var_decl(name("foo", "z"), Prop))
env = env:add_universe(name("foo", "u"))
env = env:add_universe(name("foo", "v"))
env = add_aliases(env, "foo", "bla", {"z", "v"})
assert(not get_alias_exprs(env, {"bla", "x"}):is_nil())
assert(not get_alias_exprs(env, {"bla", "y"}):is_nil())
assert(get_alias_exprs(env, {"bla", "z"}):is_nil())
assert(get_alias_level(env, {"bla", "u"}))
assert(not get_alias_level(env, {"bla", "v"}))