-
-
Notifications
You must be signed in to change notification settings - Fork 7
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 #1055 from lean-ja/Seasawher/issue1054
HAdd 型クラスを紹介する
- Loading branch information
Showing
2 changed files
with
28 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,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 |
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