Skip to content

Commit

Permalink
Merge pull request #1055 from lean-ja/Seasawher/issue1054
Browse files Browse the repository at this point in the history
HAdd 型クラスを紹介する
  • Loading branch information
Seasawher authored Nov 4, 2024
2 parents 62bf630 + 50de317 commit 709646a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
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

0 comments on commit 709646a

Please sign in to comment.