2014-02-23 21:19:39 +00:00
|
|
|
/*
|
|
|
|
Copyright (c) 2014 Microsoft Corporation. All rights reserved.
|
|
|
|
Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
|
|
|
|
Author: Leonardo de Moura
|
|
|
|
*/
|
|
|
|
#pragma once
|
2014-02-23 22:41:44 +00:00
|
|
|
#include <algorithm>
|
2014-06-21 15:15:47 +00:00
|
|
|
#include "util/lazy_list.h"
|
|
|
|
#include "util/list.h"
|
|
|
|
#include "util/name_generator.h"
|
2014-02-23 21:19:39 +00:00
|
|
|
#include "kernel/expr.h"
|
|
|
|
#include "kernel/justification.h"
|
2014-06-21 15:15:47 +00:00
|
|
|
#include "kernel/metavar.h"
|
|
|
|
|
2014-02-23 21:19:39 +00:00
|
|
|
namespace lean {
|
|
|
|
/**
|
2014-05-10 03:25:27 +00:00
|
|
|
\brief The lean kernel type checker produces two kinds of constraints:
|
2014-02-23 21:19:39 +00:00
|
|
|
|
|
|
|
- Equality constraint: t ≈ s
|
|
|
|
The terms t and s must be definitionally equal.
|
2014-05-10 03:25:27 +00:00
|
|
|
- Universe level constaint: l = m
|
2014-02-23 21:19:39 +00:00
|
|
|
The universe level l must be less than or equal to m.
|
|
|
|
|
2014-05-10 03:25:27 +00:00
|
|
|
\remark The constraints are only generated if the input term contains
|
|
|
|
metavariables or level metavariables.
|
2014-02-23 21:19:39 +00:00
|
|
|
|
|
|
|
Each constraint is associated with a justification object.
|
2014-06-21 15:15:47 +00:00
|
|
|
|
|
|
|
\remark We also have choice constraints that are used by elaborator to specify
|
|
|
|
the possible solutions for a metavariable. The choice constraints are not used by
|
|
|
|
the kernel.
|
|
|
|
*/
|
|
|
|
enum class constraint_kind { Eq, Level, Choice };
|
|
|
|
class constraint;
|
|
|
|
typedef list<constraint> constraints;
|
|
|
|
typedef std::tuple<expr, justification, constraints> choice_fn_result;
|
|
|
|
/**
|
|
|
|
\brief A choice_fn is used to enumerate the possible solutions for a metavariable.
|
|
|
|
The input arguments are:
|
|
|
|
- an inferred type (if available)
|
|
|
|
- substitution map (metavar -> value)
|
|
|
|
- name generator
|
|
|
|
|
|
|
|
The result is a lazy_list of choices, i.e., tuples containing:
|
|
|
|
- an expression representing one of the possible solutions
|
|
|
|
- a justification for it (this is used to accumulate the justification for the substitutions used).
|
|
|
|
- a list of new constraints (that is, the solution is only valid if the additional constraints can be solved)
|
|
|
|
|
|
|
|
One application of choice constraints is overloaded notation.
|
2014-02-23 21:19:39 +00:00
|
|
|
*/
|
2014-06-21 15:15:47 +00:00
|
|
|
typedef std::function<lazy_list<choice_fn_result>(optional<expr> const &, substitution const &, name_generator const &)> choice_fn;
|
|
|
|
|
2014-02-23 21:19:39 +00:00
|
|
|
struct constraint_cell;
|
|
|
|
class constraint {
|
|
|
|
constraint_cell * m_ptr;
|
|
|
|
constraint(constraint_cell * ptr);
|
|
|
|
public:
|
|
|
|
constraint(constraint const & c);
|
|
|
|
constraint(constraint && s);
|
|
|
|
~constraint();
|
|
|
|
|
|
|
|
constraint_kind kind() const;
|
|
|
|
justification const & get_justification() const;
|
|
|
|
|
|
|
|
constraint & operator=(constraint const & c);
|
|
|
|
constraint & operator=(constraint && c);
|
|
|
|
|
|
|
|
friend bool is_eqp(constraint const & c1, constraint const & c2) { return c1.m_ptr == c2.m_ptr; }
|
|
|
|
friend void swap(constraint & l1, constraint & l2) { std::swap(l1, l2); }
|
|
|
|
|
|
|
|
friend constraint mk_eq_cnstr(expr const & lhs, expr const & rhs, justification const & j);
|
|
|
|
friend constraint mk_level_cnstr(level const & lhs, level const & rhs, justification const & j);
|
2014-06-21 15:15:47 +00:00
|
|
|
friend constraint mk_choice_cnstr(expr const & m, choice_fn const & fn, justification const & j);
|
2014-02-23 21:19:39 +00:00
|
|
|
|
|
|
|
constraint_cell * raw() const { return m_ptr; }
|
|
|
|
};
|
|
|
|
|
2014-02-25 00:30:56 +00:00
|
|
|
constraint mk_eq_cnstr(expr const & lhs, expr const & rhs, justification const & j);
|
|
|
|
constraint mk_level_cnstr(level const & lhs, level const & rhs, justification const & j);
|
2014-06-21 15:15:47 +00:00
|
|
|
constraint mk_choice_cnstr(expr const & m, choice_fn const & fn, justification const & j);
|
2014-02-25 00:30:56 +00:00
|
|
|
|
2014-04-28 17:53:53 +00:00
|
|
|
inline bool is_eq_cnstr(constraint const & c) { return c.kind() == constraint_kind::Eq; }
|
|
|
|
inline bool is_level_cnstr(constraint const & c) { return c.kind() == constraint_kind::Level; }
|
2014-06-21 15:15:47 +00:00
|
|
|
inline bool is_choice_cnstr(constraint const & c) { return c.kind() == constraint_kind::Choice; }
|
2014-02-23 21:19:39 +00:00
|
|
|
|
2014-05-10 03:25:27 +00:00
|
|
|
/** \brief Return the lhs of an equality constraint. */
|
2014-02-23 21:19:39 +00:00
|
|
|
expr const & cnstr_lhs_expr(constraint const & c);
|
2014-05-10 03:25:27 +00:00
|
|
|
/** \brief Return the rhs of an equality constraint. */
|
2014-02-23 21:19:39 +00:00
|
|
|
expr const & cnstr_rhs_expr(constraint const & c);
|
|
|
|
/** \brief Return the lhs of an level constraint. */
|
|
|
|
level const & cnstr_lhs_level(constraint const & c);
|
|
|
|
/** \brief Return the rhs of an level constraint. */
|
|
|
|
level const & cnstr_rhs_level(constraint const & c);
|
2014-06-21 15:15:47 +00:00
|
|
|
/** \brief Return the metavariable associated with a choice constraint */
|
|
|
|
expr const & cnstr_mvar(constraint const & c);
|
|
|
|
/** \brief Return the choice_fn associated with a choice constraint. */
|
|
|
|
choice_fn const & cnstr_choice_fn(constraint const & c);
|
2014-02-23 21:19:39 +00:00
|
|
|
|
|
|
|
/** \brief Printer for debugging purposes */
|
|
|
|
std::ostream & operator<<(std::ostream & out, constraint const & c);
|
|
|
|
}
|