2014-01-13 01:45:24 +00:00
|
|
|
/*
|
2014-08-03 23:03:58 +00:00
|
|
|
Copyright (c) 2013-2014 Microsoft Corporation. All rights reserved.
|
2014-01-13 01:45:24 +00:00
|
|
|
Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
|
|
|
|
Author: Leonardo de Moura
|
|
|
|
*/
|
|
|
|
#pragma once
|
2014-08-03 23:03:58 +00:00
|
|
|
#include <functional>
|
2014-01-13 01:45:24 +00:00
|
|
|
#include "util/lua.h"
|
2014-08-03 23:03:58 +00:00
|
|
|
#include "util/optional.h"
|
|
|
|
#include "util/buffer.h"
|
|
|
|
#include "util/name_map.h"
|
2014-01-13 01:45:24 +00:00
|
|
|
#include "kernel/expr.h"
|
2014-01-19 18:32:06 +00:00
|
|
|
#include "kernel/environment.h"
|
2014-01-13 01:45:24 +00:00
|
|
|
|
|
|
|
namespace lean {
|
2014-08-05 01:24:01 +00:00
|
|
|
/** \brief Create a universe level metavariable that can be used as a placeholder in #hop_match.
|
|
|
|
|
|
|
|
\remark The index \c i is encoded in the hierarchical name, and can be quickly accessed.
|
|
|
|
In hop_match the substitution is also efficiently represented as an array (aka buffer).
|
|
|
|
*/
|
|
|
|
level mk_idx_meta_univ(unsigned i);
|
|
|
|
|
|
|
|
/** \brief Context for match_plugins. */
|
|
|
|
class match_context {
|
|
|
|
public:
|
|
|
|
/** \brief Create a fresh name. */
|
|
|
|
virtual name mk_name() = 0;
|
|
|
|
/** \brief Given a variable \c x, return its assignment (if available) */
|
|
|
|
virtual optional<expr> get_subst(expr const & x) const = 0;
|
|
|
|
/** \brief Given a universe level meta-variable \c x (created using #mk_idx_meta_univ), return its assignment (if available) */
|
|
|
|
virtual optional<level> get_subst(level const & x) const = 0;
|
|
|
|
/** \brief Assign the variable \c x to \c e
|
|
|
|
\pre \c x is not assigned
|
|
|
|
*/
|
|
|
|
virtual void assign(expr const & x, expr const & e) = 0;
|
|
|
|
/** \brief Assign the variable \c x to \c l
|
|
|
|
\pre \c x is not assigned, \c x was created using #mk_idx_meta_univ.
|
|
|
|
*/
|
|
|
|
virtual void assign(level const & x, level const & l) = 0;
|
2014-08-05 01:28:14 +00:00
|
|
|
virtual bool match(expr const & p, expr const & t) = 0;
|
2014-08-05 01:24:01 +00:00
|
|
|
};
|
|
|
|
|
2014-08-03 23:03:58 +00:00
|
|
|
/** \brief Callback for extending the higher-order pattern matching procedure.
|
|
|
|
It is invoked before the matcher fails.
|
|
|
|
|
|
|
|
plugin(p, t, s) must return true iff for updated substitution s', s'(p) is definitionally equal to t.
|
|
|
|
*/
|
2014-08-05 01:24:01 +00:00
|
|
|
typedef std::function<bool(expr const &, expr const &, match_context &)> match_plugin; // NOLINT
|
2014-08-03 23:03:58 +00:00
|
|
|
|
2014-08-05 15:37:03 +00:00
|
|
|
/** \brief Create a match_plugin that puts terms in weak-head-normal-form before failing */
|
2014-09-26 17:24:38 +00:00
|
|
|
match_plugin mk_whnf_match_plugin(std::shared_ptr<type_checker> tc);
|
2014-08-05 15:37:03 +00:00
|
|
|
|
2014-01-14 02:06:23 +00:00
|
|
|
/**
|
|
|
|
\brief Matching for higher-order patterns. Return true iff \c t matches the higher-order pattern \c p.
|
2014-08-03 23:03:58 +00:00
|
|
|
The substitution is stored in \c subst. Note that, this procedure treats free-variables as placholders
|
|
|
|
instead of meta-variables.
|
2014-01-14 02:06:23 +00:00
|
|
|
|
|
|
|
\c subst is an assignment for the free variables occurring in \c p.
|
|
|
|
|
2014-08-03 23:03:58 +00:00
|
|
|
\pre \c t must not contain free variables. If it does, they must be replaced with local constants
|
|
|
|
before invoking this functions.
|
2014-01-14 02:06:23 +00:00
|
|
|
|
|
|
|
\c p is a higher-order pattern when in all applications in \c p
|
2014-01-25 18:08:53 +00:00
|
|
|
1- A free variable is not the function OR
|
2014-08-03 23:03:58 +00:00
|
|
|
2- A free variable is the function, but all other arguments are distinct local constants.
|
2014-01-14 02:06:23 +00:00
|
|
|
|
|
|
|
\pre \c subst must be big enough to store all free variables occurring in subst
|
2014-01-19 18:32:06 +00:00
|
|
|
|
2014-08-03 23:03:58 +00:00
|
|
|
If prefix is provided, then it is used for creating unique names.
|
2014-01-29 04:30:47 +00:00
|
|
|
|
2014-01-28 08:50:27 +00:00
|
|
|
If name_subst is different from nullptr, then the procedure stores in name_subst
|
|
|
|
a mapping for binder names. It maps the binder names used in the pattern \c p into
|
|
|
|
the binder names used in \c t.
|
2014-08-03 23:03:58 +00:00
|
|
|
|
|
|
|
If the plugin is provided, then it is invoked before a failure.
|
2014-01-14 02:06:23 +00:00
|
|
|
*/
|
2014-08-05 01:24:01 +00:00
|
|
|
bool match(expr const & p, expr const & t, buffer<optional<expr>> & esubst, buffer<optional<level>> & lsubst,
|
|
|
|
name const * prefix = nullptr, name_map<name> * name_subst = nullptr, match_plugin const * plugin = nullptr);
|
2014-08-05 00:32:07 +00:00
|
|
|
void open_match(lua_State * L);
|
2014-09-23 17:45:14 +00:00
|
|
|
void initialize_match();
|
|
|
|
void finalize_match();
|
2014-01-13 01:45:24 +00:00
|
|
|
}
|