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"
|
2015-02-04 02:10:38 +00:00
|
|
|
#include "util/lbool.h"
|
2014-08-03 23:03:58 +00:00
|
|
|
#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"
|
2015-06-08 22:52:50 +00:00
|
|
|
#include "library/idx_metavar.h"
|
2014-01-13 01:45:24 +00:00
|
|
|
|
|
|
|
namespace lean {
|
2014-08-05 01:24:01 +00:00
|
|
|
/** \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;
|
2015-06-08 22:52:50 +00:00
|
|
|
/** \brief Given a universe level meta-variable \c x (created using #mk_idx_metauniv),
|
|
|
|
return its assignment (if available) */
|
2014-08-05 01:24:01 +00:00
|
|
|
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
|
2015-06-08 22:52:50 +00:00
|
|
|
\pre \c x is not assigned, \c x was created using #mk_idx_metauniv.
|
2014-08-05 01:24:01 +00:00
|
|
|
*/
|
|
|
|
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.
|
|
|
|
*/
|
2015-02-04 02:10:38 +00:00
|
|
|
class match_plugin {
|
|
|
|
public:
|
|
|
|
virtual ~match_plugin() {}
|
|
|
|
/** \brief The following method is invoked before the matcher tries to process
|
|
|
|
\c p and \c t. The method is only invoked when \c p and \c t have the same kind,
|
2015-06-08 22:52:50 +00:00
|
|
|
\c p is not a special metavariable created using \c mk_idx_metavar, and
|
2015-02-04 02:10:38 +00:00
|
|
|
\c p and \c t are not structurally identical.
|
|
|
|
|
|
|
|
The result should be:
|
|
|
|
- l_false : did not match
|
|
|
|
- l_true : matched
|
|
|
|
- l_undef : did not handled (i.e., default matcher should be used)
|
|
|
|
*/
|
|
|
|
virtual lbool pre(expr const & /*p*/, expr const & /*t*/, match_context & /*ctx*/) const { return l_undef; }
|
|
|
|
|
|
|
|
/** \brief The following method is invoked when matcher doesn't have anything else to do.
|
|
|
|
This method is usually used to invoke expensive procedures such as the normalizer.
|
|
|
|
It should return true it the plugin did manage to match p and t.
|
|
|
|
*/
|
|
|
|
virtual bool on_failure(expr const & p, expr const & t, match_context & ctx) const = 0;
|
|
|
|
};
|
2014-08-03 23:03:58 +00:00
|
|
|
|
2015-02-04 02:10:38 +00:00
|
|
|
/** \brief Simple plugin that just puts terms in whnf and tries again */
|
|
|
|
class whnf_match_plugin : public match_plugin {
|
|
|
|
type_checker & m_tc;
|
|
|
|
public:
|
|
|
|
whnf_match_plugin(type_checker & tc):m_tc(tc) {}
|
|
|
|
virtual bool on_failure(expr const & p, expr const & t, match_context & ctx) const;
|
|
|
|
};
|
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.
|
2015-02-03 23:15:04 +00:00
|
|
|
The substitution is stored in \c subst. Note that, this procedure treats "special" meta-variables
|
2015-06-08 22:52:50 +00:00
|
|
|
(created using #mk_idx_metauniv and #mk_idx_metavar) as placeholders. The substitution of these
|
2015-02-03 23:15:04 +00:00
|
|
|
metavariable can be quickly accessed using an index stored in them. The parameters
|
|
|
|
\c esubst and \c lsubst store the substitutions for them. There are just buffers.
|
2014-01-14 02:06:23 +00:00
|
|
|
|
2015-02-03 23:15:04 +00:00
|
|
|
\pre \p and \c t must not contain free variables. Thus, free-variables must be replaced with local constants
|
|
|
|
before invoking this function.
|
2014-01-14 02:06:23 +00:00
|
|
|
|
2015-02-03 23:15:04 +00:00
|
|
|
Other (non special) meta-variables are treated as opaque constants.
|
2014-01-14 02:06:23 +00:00
|
|
|
|
|
|
|
\c p is a higher-order pattern when in all applications in \c p
|
2015-02-03 23:15:04 +00:00
|
|
|
1- A special meta-variable is not the function OR
|
|
|
|
2- A special meta-variable is the function, but all other arguments are distinct local constants.
|
2014-01-14 02:06:23 +00:00
|
|
|
|
2015-02-03 23:15:04 +00:00
|
|
|
\pre \c esubst and \c lsubst must be big enough to store the substitution.
|
|
|
|
That is, their size should be > than the index of any special metavariable occuring in p.
|
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.
|
2015-01-29 02:39:50 +00:00
|
|
|
|
|
|
|
If \c assigned is provided, then it is set to true if \c esubst or \c lsubst is updated.
|
2014-01-14 02:06:23 +00:00
|
|
|
*/
|
2015-02-03 23:15:04 +00:00
|
|
|
bool match(expr const & p, expr const & t, buffer<optional<level>> & lsubst, buffer<optional<expr>> & esubst,
|
2015-01-29 02:39:50 +00:00
|
|
|
name const * prefix = nullptr, name_map<name> * name_subst = nullptr, match_plugin const * plugin = nullptr,
|
|
|
|
bool * assigned = nullptr);
|
2015-02-03 23:15:04 +00:00
|
|
|
bool match(expr const & p, expr const & t,
|
2015-01-29 22:09:09 +00:00
|
|
|
unsigned lsubst_sz, optional<level> * lsubst,
|
2015-02-03 23:15:04 +00:00
|
|
|
unsigned esubst_sz, optional<expr> * esubst,
|
2015-01-29 22:09:09 +00:00
|
|
|
name const * prefix = nullptr, name_map<name> * name_subst = nullptr,
|
|
|
|
match_plugin const * plugin = nullptr, bool * assigned = 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
|
|
|
}
|