feat(library/init/lean/expander): prefix ~> notation macro: construct RHS, move to new module
This commit is contained in:
parent
f0a983fe30
commit
76ea8169bc
3 changed files with 49 additions and 30 deletions
48
library/init/lean/expander.lean
Normal file
48
library/init/lean/expander.lean
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
/-
|
||||
Copyright (c) 2018 Microsoft Corporation. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Author: Sebastian Ullrich
|
||||
|
||||
Macro expander for the Lean language
|
||||
-/
|
||||
prelude
|
||||
import init.lean.parser.module
|
||||
|
||||
namespace lean
|
||||
namespace expander
|
||||
open parser
|
||||
open parser.term
|
||||
open parser.command
|
||||
open parser.command.notation_spec
|
||||
|
||||
def mixfix.transform (stx : tysyntax mixfix.view) : option (tysyntax notation.view) :=
|
||||
do v ← view stx,
|
||||
-- TODO: reserved token case
|
||||
notation_symbol.view.quoted quoted ← view v.symbol,
|
||||
quoted ← view quoted,
|
||||
prec ← view quoted.prec,
|
||||
-- `notation` allows more syntax after `:` than mixfix commands, so we have to do a small conversion
|
||||
let prec_to_action : precedence.view → action.view :=
|
||||
λ prec, {action := review $ action_kind.view.prec prec.prec, ..prec},
|
||||
k ← view v.kind,
|
||||
let (spec, term) := match k : _ → (notation_spec.view × syntax) with
|
||||
| mixfix.kind.view.prefix _ :=
|
||||
let b : tysyntax parser.ident.view := review {part := review $ ident_part.view.default "b", suffix := review none} in
|
||||
(notation_spec.view.rules $ review {
|
||||
id := review none,
|
||||
rules := review [review {
|
||||
symbol := v.symbol,
|
||||
transition := review $ some $ review $ transition.view.arg $ review {
|
||||
id := b,
|
||||
action := review $ do prec ← prec, prec ← view prec, pure $ review $ prec_to_action prec}}]},
|
||||
review {lambda.view . op := review lambda_op.view.«λ», binders := review [review $
|
||||
binder.view.unbracketed $ review {
|
||||
ids := review [review $ binder_id.view.id $ review {id := b, univ := review none}],
|
||||
type := review none, default := review none}
|
||||
],
|
||||
body := review_as app.view {fn := v.term, arg := b}})
|
||||
| _ := sorry,
|
||||
pure $ review {spec := review spec, term := term}
|
||||
|
||||
end expander
|
||||
end lean
|
||||
|
|
@ -66,33 +66,4 @@ any_of [open.parser, section.parser, universe.parser, notation.parser, reserve_n
|
|||
mixfix.parser, reserve_mixfix.parser, check.parser, declaration.parser] <?> "command"
|
||||
|
||||
end parser
|
||||
|
||||
namespace parser
|
||||
namespace «command»
|
||||
open combinators notation_spec
|
||||
|
||||
-- example macro
|
||||
def mixfix.expand (stx : tysyntax mixfix.view) : option (tysyntax notation.view) :=
|
||||
do v ← view stx,
|
||||
-- TODO: reserved token case
|
||||
notation_symbol.view.quoted quoted ← view v.symbol,
|
||||
quoted ← view quoted,
|
||||
prec ← view quoted.prec,
|
||||
-- `notation` allows more syntax after `:` than mixfix commands, so we have to do a small conversion
|
||||
let prec_to_action : precedence.view → action.view :=
|
||||
λ prec, {action := review $ action_kind.view.prec prec.prec, ..prec},
|
||||
k ← view v.kind,
|
||||
let spec := view.rules $ match k with
|
||||
| mixfix.kind.view.prefix _ := review {
|
||||
id := review none,
|
||||
rules := review [review {
|
||||
symbol := v.symbol,
|
||||
transition := review $ some $ review $ transition.view.arg $ review {
|
||||
id := review {part := review $ ident_part.view.default "b", suffix := review none},
|
||||
action := review $ do prec ← prec, prec ← view prec, pure $ review $ prec_to_action prec}}]}
|
||||
| _ := sorry,
|
||||
pure $ review ⟨"notation", review spec, ":=", v.term⟩
|
||||
|
||||
end «command»
|
||||
end parser
|
||||
end lean
|
||||
|
|
|
|||
|
|
@ -121,7 +121,7 @@ end binder
|
|||
@[derive parser.has_tokens parser.has_view]
|
||||
def lambda.parser : term_parser :=
|
||||
node! lambda [
|
||||
op: any_of [symbol "λ", symbol "fun"],
|
||||
op: node_choice! lambda_op {"λ", "fun"},
|
||||
binders: binder.parser+,
|
||||
",",
|
||||
body: recurse 0
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue