-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #817 from lean-ja/Seasawher/issue349
`@[match_pattern]` 属性を紹介する
- Loading branch information
Showing
2 changed files
with
31 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters