-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShow.v
53 lines (41 loc) · 1.13 KB
/
Show.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
Require Import String.
Open Scope string_scope.
(* 型クラスShowの定義 *)
Class Show (A: Set) := {
show : A -> string
}.
(* string型をShowクラスのインスタンスとする *)
Instance ShowString : Show string := {
show s := s
}.
(* bool型をShowクラスのインスタンスとする *)
Instance ShowBool : Show bool := {
show b := match b with | true => "true" | false => "false" end
}.
Require Import ZArith.
Fixpoint string_of_positive p :=
match p with
| xI p' => "I" ++ string_of_positive p'
| xO p' => "O" ++ string_of_positive p'
| xH => "H"
end.
Instance ShowPositive : Show positive := {
show p := string_of_positive p
}.
Instance ShowZ : Show Z := {
show n :=
match n with
| Z0 => "0"
| Zpos pos => show pos
| Zneg pos => "-" ++ show pos
end
}.
Instance ShowNat : Show nat := {
show n := show (Z_of_nat n) (*TODO*)
}.
(* 文字列はShowクラスのインスタンス *)
Definition foo := show "hello".
(* ブール型はShowクラスのインスタンス *)
Definition bar := show true.
(* 混ぜても使える *)
Definition hoge := show "hello" ++ show false.