此示例要求 m 是一个可以进行报价的 monad。
variable {m : Type → Type} [Monad m] [MonadQuotation m]
默认情况下,反引号 $e 应该是一个术语,因为这是立即预期作为加法的第二个参数的语法类别。
def ex1 (e) := show m _ from `(2 + $e)
ex1 {m : Type → Type} [Monad m] [MonadQuotation m] (e : TSyntax `term) : m (TSyntax `term)#check ex1
ex1 {m : Type → Type} [Monad m] [MonadQuotation m] (e : TSyntax `term) : m (TSyntax `term)
将 $e 注释为数字文字会成功,因为数字文字也是有效术语。
参数 e 的预期类型更改为 TSyntax `num。
def ex2 (e) := show m _ from `(2 + $e:num)
ex2 {m : Type → Type} [Monad m] [MonadQuotation m] (e : TSyntax `num) : m (TSyntax `term)#check ex2
ex2 {m : Type → Type} [Monad m] [MonadQuotation m] (e : TSyntax `num) : m (TSyntax `term)
美元符号和标识符之间不允许有空格。
def ex2 (e) := show m _ from `(2 +unexpected token '$'; expected '`(tactic|' or no space before spliced term $ e:num)
<example>:1:34-1:36: unexpected token '$'; expected '`(tactic|' or no space before spliced term
冒号之前也不允许有空格:
def ex2 (e) := show m _ from `(2 + $eunexpected token ':'; expected ')' :num)
<example>:1:37-1:39: unexpected token ':'; expected ')'