lean4-htt/src/library/replace_visitor.h
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

40 lines
1.3 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
*/
#pragma once
#include "kernel/expr_maps.h"
namespace lean {
/**
\brief Base class for implementing operations that apply modifications
on expressions.
The default behavior is a NOOP, users must create subclasses and
redefine the visit_* methods. */
class replace_visitor {
protected:
typedef expr_bi_map<expr> cache;
cache m_cache;
expr save_result(expr const & e, expr && r, bool shared);
virtual expr visit_sort(expr const &);
virtual expr visit_constant(expr const &);
virtual expr visit_var(expr const &);
virtual expr visit_meta(expr const &);
virtual expr visit_fvar(expr const &);
virtual expr visit_local(expr const &);
virtual expr visit_app(expr const &);
virtual expr visit_binding(expr const &);
virtual expr visit_lambda(expr const &);
virtual expr visit_pi(expr const &);
virtual expr visit_let(expr const & e);
virtual expr visit_lit(expr const & e);
virtual expr visit_mdata(expr const & e);
virtual expr visit_proj(expr const & e);
virtual expr visit(expr const &);
public:
expr operator()(expr const & e) { return visit(e); }
void clear() { m_cache.clear(); }
};
}