Zulip Chat Archive

Stream: Is there code for X?

Topic: macro for applying list of arguments


Asei Inoue (Jun 16 2024 at 04:18):

set_option linter.unusedVariables false

def foo (a1 a2 a3 a4 a5 a6 a7 : String) : IO Unit :=
  IO.println "many arguments!"

def args := ["one", "two", "three", "four", "five", "six", "seven"]

-- some macro definition...
-- Here it is temporarily defined as infix.
infix:70 " apply " => sorry

#check (foo apply args)

Yaël Dillies (Jun 16 2024 at 08:55):

Are you looking for docs#Lean.mkAppN ?

Asei Inoue (Jun 16 2024 at 10:23):

@Yaël Dillies Thank you! but argument type of mkAppN is Expr...

Yaël Dillies (Jun 16 2024 at 10:24):

You can turn your args into an Array Expr if you want to

Patrick Massot (Jun 16 2024 at 10:24):

This would be a very inefficient way

Patrick Massot (Jun 16 2024 at 10:25):

Asei is right that they need something purely syntactic.

Yaël Dillies (Jun 16 2024 at 10:25):

Maybe it would be good to un #xy

Patrick Massot (Jun 16 2024 at 10:26):

It is easy to do if you know that kind of things. I’ll wait a bit to see if someone who can write the solution without firing up Lean shows up. Other I’ll take a look.

Eric Wieser (Jun 16 2024 at 10:37):

No metaprogramming needed:

import Mathlib
set_option linter.unusedVariables false

def foo (a1 a2 a3 a4 a5 a6 a7 : String) : IO Unit :=
  IO.println "many arguments!"

def args := ["one", "two", "three", "four", "five", "six", "seven"]

def apply (l : List α) (f : Function.OfArity α β l.length) : β :=
  match l with
  | [] => f
  | x :: xs => apply xs (f x)

#eval (apply args foo : IO Unit)

Asei Inoue (Jun 16 2024 at 10:40):

@Eric Wieser Thank you! I also want to generalise to cases where the argument types are not necessarily the same.

Eric Wieser (Jun 16 2024 at 10:45):

Then you can't use a list

Asei Inoue (Jun 16 2024 at 10:47):

@Eric Wieser indeed, you're right...!!

Eric Wieser (Jun 16 2024 at 10:48):

You might find some of the declarations near docs#Function.OfArity interesting

Eric Wieser (Jun 16 2024 at 10:49):

docs#Function.FromTypes.uncurry especially


Last updated: May 02 2025 at 03:31 UTC