feat(frontends/lean): max precedence used by Lean is not max_uint anymore
The motivation is to allow users to define notation with higher precedence than function application.
This commit is contained in:
parent
85d0521d48
commit
b5e0ded163
7 changed files with 30 additions and 17 deletions
|
@ -35,7 +35,7 @@ static optional<unsigned> parse_optional_precedence(parser & p) {
|
|||
return optional<unsigned>(p.parse_small_nat());
|
||||
} else if (p.curr_is_token_or_id(get_max_tk())) {
|
||||
p.next();
|
||||
return optional<unsigned>(std::numeric_limits<unsigned>::max());
|
||||
return optional<unsigned>(get_max_prec());
|
||||
} else {
|
||||
return optional<unsigned>();
|
||||
}
|
||||
|
|
|
@ -1022,10 +1022,11 @@ expr parser::parse_nud_notation() {
|
|||
}
|
||||
|
||||
expr parser::parse_led_notation(expr left) {
|
||||
if (led().find(get_token_info().value()))
|
||||
if (led().find(get_token_info().value())) {
|
||||
return parse_notation(led(), &left);
|
||||
else
|
||||
return mk_app(left, parse_nud_notation(), pos_of(left));
|
||||
} else {
|
||||
return mk_app(left, parse_expr(get_max_prec()), pos_of(left));
|
||||
}
|
||||
}
|
||||
|
||||
expr parser::id_to_expr(name const & id, pos_info const & p) {
|
||||
|
@ -1188,12 +1189,8 @@ expr parser::parse_nud() {
|
|||
|
||||
expr parser::parse_led(expr left) {
|
||||
switch (curr()) {
|
||||
case scanner::token_kind::Keyword: return parse_led_notation(left);
|
||||
case scanner::token_kind::Identifier: return mk_app(left, parse_id(), pos_of(left));
|
||||
case scanner::token_kind::Numeral: return mk_app(left, parse_numeral_expr(), pos_of(left));
|
||||
case scanner::token_kind::Decimal: return mk_app(left, parse_decimal_expr(), pos_of(left));
|
||||
case scanner::token_kind::String: return mk_app(left, parse_string_expr(), pos_of(left));
|
||||
default: return left;
|
||||
case scanner::token_kind::Keyword: return parse_led_notation(left);
|
||||
default: return mk_app(left, parse_expr(get_max_prec()), pos_of(left));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1206,7 +1203,7 @@ unsigned parser::curr_lbp() const {
|
|||
return 0;
|
||||
case scanner::token_kind::Identifier: case scanner::token_kind::Numeral:
|
||||
case scanner::token_kind::Decimal: case scanner::token_kind::String:
|
||||
return std::numeric_limits<unsigned>::max();
|
||||
return get_max_prec();
|
||||
}
|
||||
lean_unreachable(); // LCOV_EXCL_LINE
|
||||
}
|
||||
|
|
|
@ -766,7 +766,7 @@ auto pretty_fn::pp_notation(notation_entry const & entry, buffer<optional<expr>>
|
|||
to_buffer(entry.get_transitions(), ts);
|
||||
format fmt;
|
||||
unsigned i = ts.size();
|
||||
unsigned last_rbp = max_bp()-1;
|
||||
unsigned last_rbp = inf_bp()-1;
|
||||
unsigned token_lbp = 0;
|
||||
bool extra_space = false;
|
||||
bool last = true;
|
||||
|
@ -779,7 +779,7 @@ auto pretty_fn::pp_notation(notation_entry const & entry, buffer<optional<expr>>
|
|||
case notation::action_kind::Skip:
|
||||
curr = format(tk);
|
||||
if (last)
|
||||
last_rbp = max_bp();
|
||||
last_rbp = inf_bp();
|
||||
break;
|
||||
case notation::action_kind::Expr:
|
||||
if (args.empty() || !args.back()) {
|
||||
|
@ -915,7 +915,7 @@ auto pretty_fn::pp_notation(notation_entry const & entry, buffer<optional<expr>>
|
|||
}
|
||||
extra_space = add_extra_space(tk);
|
||||
}
|
||||
unsigned first_lbp = max_bp();
|
||||
unsigned first_lbp = inf_bp();
|
||||
if (!entry.is_nud()) {
|
||||
first_lbp = token_lbp;
|
||||
lean_assert(!last);
|
||||
|
|
|
@ -21,14 +21,15 @@ class notation_entry;
|
|||
|
||||
class pretty_fn {
|
||||
public:
|
||||
static unsigned max_bp() { return std::numeric_limits<unsigned>::max(); }
|
||||
static unsigned max_bp() { return get_max_prec(); }
|
||||
static unsigned inf_bp() { return std::numeric_limits<unsigned>::max(); }
|
||||
class result {
|
||||
unsigned m_lbp;
|
||||
unsigned m_rbp;
|
||||
format m_fmt;
|
||||
public:
|
||||
result():m_lbp(max_bp()), m_rbp(max_bp()) {}
|
||||
result(format const & fmt):m_lbp(max_bp()), m_rbp(max_bp()), m_fmt(fmt) {}
|
||||
result(format const & fmt):m_lbp(inf_bp()), m_rbp(inf_bp()), m_fmt(fmt) {}
|
||||
result(unsigned rbp, format const & fmt):m_lbp(max_bp()), m_rbp(rbp), m_fmt(fmt) {}
|
||||
result(unsigned lbp, unsigned rbp, format const & fmt):m_lbp(lbp), m_rbp(rbp), m_fmt(fmt) {}
|
||||
unsigned lbp() const { return m_lbp; }
|
||||
|
|
|
@ -11,7 +11,7 @@ Author: Leonardo de Moura
|
|||
|
||||
namespace lean {
|
||||
static unsigned g_arrow_prec = 25;
|
||||
static unsigned g_max_prec = std::numeric_limits<unsigned>::max();
|
||||
static unsigned g_max_prec = 1024;
|
||||
static unsigned g_plus_prec = 65;
|
||||
static unsigned g_cup_prec = 60;
|
||||
unsigned get_max_prec() { return g_max_prec; }
|
||||
|
|
13
tests/lean/notation7.lean
Normal file
13
tests/lean/notation7.lean
Normal file
|
@ -0,0 +1,13 @@
|
|||
import logic
|
||||
open num
|
||||
|
||||
constant f : num → num
|
||||
constant g : num → num → num
|
||||
|
||||
notation A `:+1`:100000 := f A
|
||||
|
||||
check g 0:+1:+1 (1:+1 + 2:+1):+1
|
||||
|
||||
set_option pp.notation false
|
||||
|
||||
check g 0:+1:+1 (1:+1 + 2:+1):+1
|
2
tests/lean/notation7.lean.expected.out
Normal file
2
tests/lean/notation7.lean.expected.out
Normal file
|
@ -0,0 +1,2 @@
|
|||
g 0 :+1 :+1 (1 :+1 + 2 :+1) :+1 : num
|
||||
g (f (f 0)) (f (add (f 1) (f 2))) : num
|
Loading…
Reference in a new issue