Zulip Chat Archive

Stream: lean4

Topic: Define Macro


Joseph O (May 28 2022 at 20:02):

in this code

syntax binder' := "(" ident " : " term ")"
syntax "def" ident binder'* " -> " term " : " term : command

macro_rules
  | `(def $f:ident ($a:ident : $ty:term) -> $ty':term: $v:term) =>
    `(def $f:ident -> ($ty:term -> $ty:term): fun $a:ident => $v:term)
  | `(def $f:ident ($a:ident : $ty:term) $bs:binder'* -> $ty':term: $v:term) =>
    `(def $f:ident $bs:binder'* -> ($ty:term -> $ty':term): fun $a => $v:term)

def add (a : Nat) (b : Nat) -> Nat:
  x + y

Im getting the error elaboration function for '«commandDef___->_:_»' has not been implemented for the add function. Any ideas why?

Notification Bot (May 28 2022 at 21:33):

Joseph O has marked this topic as unresolved.

Joseph O (May 28 2022 at 21:33):

Hm i wonder why this has errors

syntax binder' := ident term
syntax "sub" ident "(" sepBy(binder', ",") ")" " returns " term "{" term "}" : command

macro_rules
  | `(sub $f:ident ($a:ident $ty:term) returns ty':term { $v:term }) =>
    `(def $f:ident : ($ty:term -> $ty':term) := fun $a:ident => $v:term)
  | `(sub $f:ident ($bs:binder',* $a:ident $ty:term) returns $ty':term { $v:term }) =>
    `(sub $f:ident ($bs:binder',*) returns ($ty:term -> $ty':term) { fun $a:ident => $v:term })

Joseph O (May 28 2022 at 21:35):

here are the errors:

expected '{'

elaboration function for 'antiquot' has not been implemented
  $ty:term

expected command

expected ')'

expected '}' or identifier

Joseph O (May 28 2022 at 22:00):

Shouldnt this work the same as the first one?

Joseph O (May 28 2022 at 22:00):

Im a bit confused

Joseph O (May 28 2022 at 22:00):

Unless I translated incorrectly

Sebastian Ullrich (May 29 2022 at 07:39):

At around the location of the first error there is a clear mistake, a typo basically. Try double-checking that part.

Joseph O (May 29 2022 at 13:23):

Sebastian Ullrich said:

At around the location of the first error there is a clear mistake, a typo basically. Try double-checking that part.

Ok I fixed it and now I have these errors

expected 'ident', '}' or identifier

elaboration function for 'antiquot' has not been implemented
  $ty:term

expected command

expected ')'

expected '}' or identifier

Siddharth Bhat (May 29 2022 at 13:32):

I believe you're having trouble because { .. } is consumed by the term parser, since we can build records using {..} syntax.

For example, this minimal program produces a similar error:

syntax "returns" term "{" term "}" : command

macro_rules
| `(returns $x:term { $y:term  }) => `("foo")
-- expected 'ident', '}' or identifier

On the other hand, something like this where we add a clear delineation between term and { succeeds:

syntax "returns" term ";;" "{" term "}" : command
-- New: `;;` in the rule.
macro_rules
| `(returns $x:term ;; { $y:term  }) => `("foo")

Siddharth Bhat (May 29 2022 at 13:33):

(DELETED: editing snafu)

Joseph O (May 31 2022 at 14:09):

Siddharth Bhat said:

I believe you're having trouble because { .. } is consumed by the term parser, since we can build records using {..} syntax.

For example, this minimal program produces a similar error:

syntax "returns" term "{" term "}" : command

macro_rules
| `(returns $x:term { $y:term  }) => `("foo")
-- expected 'ident', '}' or identifier

On the other hand, something like this where we add a clear delineation between term and { succeeds:

syntax "returns" term ";;" "{" term "}" : command
-- New: `;;` in the rule.
macro_rules
| `(returns $x:term ;; { $y:term  }) => `("foo")

So there is no way we can avoid that?


Last updated: Dec 20 2023 at 11:08 UTC