Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create pattern.md #661

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions docs/pattern.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# パターンマッチング
origlangはパターンという発展途上の概念が存在します。

`var` 文や `let` 文の左側には反芻不可能なパターンを置くことができます。
反芻不可能なパターンとは、以下の規則によって再帰的に定義されるパターンです。

1. 無条件に束縛する場合、それは反芻不可能なパターンである。
2. パターンがガード節を持つ場合、そのパターンは反芻不可能なパターンではない。
3. パターンが論理和パターンである場合、そのパターンは反芻不可能なパターンではない。
4. タプルパターンの中に直接含まれるすべてのサブパターンが反芻不可能ならば、そのタプルパターンは反芻不可能なパターンである。
5. (未実装) レコードパターンの中に直接含まれるすべてのサブパターンが反芻不可能ならば、そのレコードパターンは反芻不可能なパターンである。

## (未実装) パターンを変数に格納する機能の提案文書

パターンは変数に格納することができる。
ただし、網羅性検査の可能性を損なわないために、いくつかの制限がある。

1. パターンの条件として束縛して良いのはコンパイル時に値が確定する値のみ
2. scrutineeの型は明示的に示す必要がある
3. ガード節は使えない

```origlang
// is_small_even: StaticPattern!<Int32>
let is_small_even = arg matches 0 | 2 | 4

if is_small_even(2) then
print "Exactly!"
else
print "Unexpected"
end
```