lean4-htt/src/library/head_map.cpp
Leonardo de Moura 85092412c7 refactor: remove Expr.FVar hack
@Kha @dselsam:
This hack was preventing us from making `Expr` a "real" Lean type.
This was bad for a few reasons:
- It was hard to extend/modify `Expr` in Lean since we would also have
to modify the C++ code that creates the `Expr` objects with the hidden
fields.
- `Expr.lam` and `Expr.forallE` were not following the Lean layout
standard where we sort fields by size. @Kha: recall we used that to
avoid a UB. The issue with `Expr.lam` and `Expr.forallE` is that they
have a "visible" field (`BinderInfo`), which is smaller than
hidden fields such as hash code.
- `Expr.fvar` had only one field at `Expr.lean,` but four behind the
scenes.

I added a new constructor `Local` that is only accessible from C++.
It is only used in legacy code we inherited from Lean2.
We will eventually delete it.

This refactoring was quite painful since many parts of the codebase
were mixing the new `Expr.fvar` with the old `Expr.local`.
I doubt I would be able to do it without the new staging framework
@Kha built.

BTW, some of the patches are horrible. I didn't care much since we
are going to deleted the super ugly files. That being said,
you should expect new weird bevaior due to `Expr.fvar` vs `Expr.local`.

Next step: use the new `ExprCachedData` to make all `Expr` hidden visibles
accessible from Lean.

checkpoint
2019-11-15 14:04:26 -08:00

35 lines
1 KiB
C++

/*
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 "library/head_map.h"
#include "library/explicit.h"
namespace lean {
head_index::head_index(expr const & e) {
expr f = get_app_fn(e);
while (true) {
f = unwrap_pos(f);
if (is_as_atomic(f))
f = get_app_fn(get_as_atomic_arg(f));
else if (is_explicit(f))
f = get_explicit_arg(f);
else
break;
}
m_kind = f.kind();
if (is_constant(f))
m_name = const_name(f);
else if (is_local_or_fvar(f))
m_name = local_or_fvar_name(f);
}
int head_index::cmp::operator()(head_index const & i1, head_index const & i2) const {
if (i1.m_kind != i2.m_kind || (i1.m_kind != expr_kind::Const && i1.m_kind != expr_kind::FVar && i1.m_kind != expr_kind::Local))
return static_cast<int>(i1.m_kind) - static_cast<int>(i2.m_kind);
else
return quick_cmp(i1.m_name, i2.m_name);
}
}