lean-ja / lean-by-example

コード例で学ぶ Lean 言語
https://lean-ja.github.io/lean-by-example/
MIT License
48 stars 7 forks source link

引数を展開して関数に流し込むのは可能か? #252

Open Seasawher opened 4 months ago

Seasawher commented 4 months ago
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"]

-- 明らかに重複がある
#eval foo args[0] args[1] args[2] args[3] args[4] args[5] args[6]
Seasawher commented 4 months ago

マクロを書けばできそう

Seasawher commented 4 months ago

see: https://leanprover.zulipchat.com/#narrow/stream/217875-Is-there-code-for-X.3F/topic/macro.20for.20applying.20list.20of.20arguments/near/444939240

Seasawher commented 4 months ago
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)