Skip to content

Commit

Permalink
Merge pull request #817 from lean-ja/Seasawher/issue349
Browse files Browse the repository at this point in the history
`@[match_pattern]` 属性を紹介する
  • Loading branch information
Seasawher authored Sep 17, 2024
2 parents 5c1db48 + 4a58599 commit 67dc818
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
30 changes: 30 additions & 0 deletions Examples/Command/Attribute/MatchPattern.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/- # match_pattern
`match_pattern` 属性を付与すると、元々コンストラクタしか使用できない `match` 式でのパターンマッチの枝に、指定した関数を使うことができるようになります。
-/

/-- 自前で定義したリスト -/
inductive MyList (α : Type) where
| nil : MyList α
| cons : α → MyList α → MyList α

variable {α : Type}

/-- MyList の `cons` コンストラクタに対するラッパー。中身は同じ -/
def MyList.myCons (a : α) (as : MyList α) : MyList α :=
MyList.cons a as

-- 最初は `match` の中で `MyList.myCons` を使うことはできない
/-- error: invalid pattern, constructor or constant marked with '[match_pattern]' expected -/
#guard_msgs in
def badLength : MyList α → Nat
| MyList.nil => 0
| MyList.myCons _ as => 1 + badLength as

-- `match_pattern` 属性を付与する
attribute [match_pattern] MyList.myCons

-- これで `match` の中で `MyList.myCons` を使うことができる
def goodLength : MyList α → Nat
| MyList.nil => 0
| MyList.myCons _ as => 1 + goodLength as
1 change: 1 addition & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
- [csimp: 効率的な実装に置換](./Command/Attribute/Csimp.md)
- [induction_eliminator: 独自帰納法](./Command/Attribute/InductionEliminator.md)
- [inherit_doc: ドキュメントの継承](./Command/Attribute/InheritDoc.md)
- [match_pattern: 独自パタンマッチ](./Command/Attribute/MatchPattern.md)
- [simps: simp補題の自動生成](./Command/Attribute/Simps.md)

- [タクティク](./Tactic/README.md)
Expand Down

0 comments on commit 67dc818

Please sign in to comment.