Zulip Chat Archive

Stream: lean4

Topic: syntax definition with only the head part works


Locria Cyber (Jul 21 2025 at 23:43):

structure Tagged (tag : String) (a : Type u) where
  inner: a

def ABC := Tagged "a" String  Tagged "b" String  Tagged "c" String

open Lean
syntax "tagged": term
-- syntax "tagged" str ident: term

macro "tagged " tag:str val:ident : term => do
  let rec build_pattern : Nat  Term  MacroM Term
    | 0, acc => `(Sum.inl $acc)
    | n+1, acc => do `(Sum.inr $( build_pattern n acc))
  -- This is a simplified example assuming tags "a", "b", "c" correspond to positions 0, 1, 2
  let position := match tag.getString with
    | "a" => 0
    | "b" => 1
    | "c" => 2
    | _ => panic! "Unsupported tag"
  build_pattern position ( `(⟨$val))

def process (abc : ABC) :=
  match abc with
  -- | .inl ⟨x⟩ => x
  | tagged "a" x => x
  | .inr (.inl x) => x
  | .inr (.inr x) => x

I'm curious why syntax "tagged" str ident: term doesn't work?

Sebastian Ullrich (Jul 22 2025 at 06:17):

macro already creates syntax


Last updated: Dec 20 2025 at 21:32 UTC