lean-ja / lean-by-example

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

`dsimproc` 使用例: Vector の成分を取得する #847

Open Seasawher opened 1 month ago

Seasawher commented 1 month ago
import Mathlib.Data.Fin.VecNotation

open Lean

partial def matchVecLit (e : Expr) : Option (List Expr) :=
  match_expr e with
  | Matrix.vecEmpty _ => some []
  | Matrix.vecCons _ _ x xs => (x :: ·) <$> matchVecLit xs
  | _ => none

dsimproc Matrix.cons_val (Matrix.vecCons _ _ _) := fun e => do
  let_expr Matrix.vecCons _ _ x xs' n := e | return .continue
  let some n := n.int? | return .continue -- The docstring claims only nat or int, but works fine for Fin
  let some xs' := matchVecLit xs' | return .continue
  let xs := x :: xs'
  let n' := (n % xs.length).toNat
  return .continue (xs.get! n')

lemma foo {a b c d : ℕ} : ![a, b, c, d, a, b, c, d, a, b, c, d] (-3) = b := by dsimp
lemma bar {a b c d : ℕ} : ![a, b, c, d, a, b, c, d, a, b, c, d] (-1) = d := by simp
Seasawher commented 1 month ago

Zulip: simp and vector notation