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

HAdd 型クラスを紹介する #1055

Merged
merged 1 commit into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
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
27 changes: 27 additions & 0 deletions LeanByExample/Reference/TypeClass/HAdd.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/- # HAdd
`HAdd` は、`+` という二項演算子のための型クラスです。
`+` 記法が何を意味するかについては制約はありません。
-/

structure Colour where
red : Nat
blue : Nat
green : Nat
deriving Repr

def sample : Colour := { red := 2, blue := 4, green := 8 }

-- 最初は `+` 記号が定義されていないのでエラーになります。
/--
error: failed to synthesize
HAdd Colour Colour ?m.1653
Additional diagnostic information may be available using the `set_option diagnostics true` command.
-/
#guard_msgs in #eval sample + sample

/-- HAdd 型クラスのインスタンスを実装する -/
instance : HAdd Colour Colour Colour where
hAdd c1 c2 := ⟨c1.red + c2.red, c1.blue + c2.blue, c1.green + c2.green⟩

-- 足し算記号が使えるようになった
#eval sample + sample
1 change: 1 addition & 0 deletions booksrc/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
- [Decidable: 決定可能](./Reference/TypeClass/Decidable.md)
- [Functor: ファンクタ](./Reference/TypeClass/Functor.md)
- [GetElem: n番目の要素を取得](./Reference/TypeClass/GetElem.md)
- [HAdd: + のための記法クラス](./Reference/TypeClass/HAdd.md)
- [Inhabited: 有項にする](./Reference/TypeClass/Inhabited.md)
- [OfNat: 数値リテラルを使用](./Reference/TypeClass/OfNat.md)
- [Repr: 表示方法を指定](./Reference/TypeClass/Repr.md)
Expand Down