Skip to content
This repository was archived by the owner on Jan 5, 2025. It is now read-only.

Commit 89cce7a

Browse files
committed
bump toolchain to v4.8.0
1 parent 125b267 commit 89cce7a

File tree

3 files changed

+139
-139
lines changed

3 files changed

+139
-139
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,30 @@
1-
/- # 1.2 演習問題 -/
2-
3-
/-! ## 1
4-
関数 `joinStringsWith` を `String -> String -> String -> String` 型の関数であって,その第一引数を第二引数と第三引数の間に配置して新しい文字列を作成するようなものとして定義してください.`joinStringsWith ", " "one" "and another"` は `"one, and another"` に等しくなるはずです.-/
5-
6-
def joinStringsWith (s1 : String) (s2 : String) (s3 : String) : String :=
7-
s2 ++ s1 ++ s3
8-
9-
example : joinStringsWith ", " "one" "and another" = "one, and another" := rfl
10-
11-
/- ### 補足: `String.append` と `++` は同じか? -/
12-
13-
example (s t : String) : s ++ t = String.append s t := rfl
14-
15-
#synth Append String
16-
17-
#check String.instAppendString
18-
19-
/-! ## 2
20-
`joinStringsWith ": "` の型は何でしょうか? Lean で答えを確認してください.-/
21-
22-
#check (joinStringsWith ": " : String → String → String)
23-
24-
/-! ## 3
25-
与えられた高さ,幅,奥行きを持つ直方体の体積を計算する関数 `volume` を `Nat → Nat → Nat → Nat` 型の関数として定義してください.
26-
-/
27-
28-
def volume (h : Nat) (w : Nat) (d : Nat) : Nat := h * w * d
29-
30-
example : volume 3 4 5 = 60 := rfl
31-
32-
#check (volume : Nat → Nat → Nat → Nat)
1+
/- # 1.2 演習問題 -/
2+
3+
/-! ## 1
4+
関数 `joinStringsWith` を `String -> String -> String -> String` 型の関数であって,その第一引数を第二引数と第三引数の間に配置して新しい文字列を作成するようなものとして定義してください.`joinStringsWith ", " "one" "and another"` は `"one, and another"` に等しくなるはずです.-/
5+
6+
def joinStringsWith (s1 : String) (s2 : String) (s3 : String) : String :=
7+
s2 ++ s1 ++ s3
8+
9+
example : joinStringsWith ", " "one" "and another" = "one, and another" := rfl
10+
11+
/- ### 補足: `String.append` と `++` は同じか? -/
12+
13+
example (s t : String) : s ++ t = String.append s t := rfl
14+
15+
#synth Append String
16+
17+
/-! ## 2
18+
`joinStringsWith ": "` の型は何でしょうか? Lean で答えを確認してください.-/
19+
20+
#check (joinStringsWith ": " : String → String → String)
21+
22+
/-! ## 3
23+
与えられた高さ,幅,奥行きを持つ直方体の体積を計算する関数 `volume` を `Nat → Nat → Nat → Nat` 型の関数として定義してください.
24+
-/
25+
26+
def volume (h : Nat) (w : Nat) (d : Nat) : Nat := h * w * d
27+
28+
example : volume 3 4 5 = 60 := rfl
29+
30+
#check (volume : Nat → Nat → Nat → Nat)
Original file line numberDiff line numberDiff line change
@@ -1,106 +1,108 @@
1-
/-
2-
### Mapping on a Tree
3-
4-
Define a function `BinTree.mapM`. By analogy to `mapM` for lists, this function should apply a monadic function to each data entry in a tree, as a preorder traversal. The type signature should be:
5-
6-
`def BinTree.mapM [Monad m] (f : α → m β) : BinTree α → m (BinTree β)`
7-
-/
8-
9-
inductive BinTree (α : Type) where
10-
| leaf : BinTree α
11-
| branch : BinTree α → α → BinTree α → BinTree α
12-
deriving Repr
13-
14-
def BinTree.mapM [Monad m] (f : α → m β) : BinTree α → m (BinTree β)
15-
| BinTree.leaf => pure BinTree.leaf
16-
| BinTree.branch l x r => do
17-
let l' ← BinTree.mapM f l
18-
let x' ← f x
19-
let r' ← BinTree.mapM f r
20-
pure (BinTree.branch l' x' r')
21-
22-
def sample := BinTree.branch (BinTree.branch BinTree.leaf 0 BinTree.leaf) 2 (BinTree.branch BinTree.leaf 3 BinTree.leaf)
23-
24-
def test (n : Nat) : Option Nat :=
25-
if n == 0 then none
26-
else some n
27-
28-
example : BinTree.mapM test sample = none := rfl
29-
30-
/-
31-
### The Option Monad Contract
32-
33-
First, write a convincing argument that the `Monad` instance for `Option` satisfies the monad contract. Then, consider the following instance:
34-
35-
```
36-
instance : Monad Option where
37-
pure x := some x
38-
bind opt next := none
39-
```
40-
41-
Both methods have the correct type. Why does this instance violate the monad contract?
42-
-/
43-
44-
instance instLawfulMonadOption : LawfulMonad Option where
45-
map_const := by
46-
intro α β
47-
rfl
48-
id_map := by
49-
intro α x
50-
cases x <;> rfl
51-
seqLeft_eq := by
52-
intro α β x y
53-
cases x <;> try rfl
54-
cases y <;> rfl
55-
seqRight_eq := by
56-
intro α β x y
57-
cases x <;> try rfl
58-
cases y <;> rfl
59-
pure_seq := by
60-
intro α β g x
61-
rfl
62-
pure_bind := by
63-
intro α β x f
64-
dsimp [Bind.bind]
65-
rfl
66-
bind_pure_comp := by
67-
intro α x f y
68-
cases y <;> rfl
69-
bind_assoc := by
70-
intro α β γ x f g
71-
cases x <;> rfl
72-
bind_map := by
73-
intro α β x f
74-
cases x <;> rfl
75-
76-
instance (priority := high) : Monad Option where
77-
pure x := some x
78-
bind opt next := none
79-
80-
instance : LawfulMonad Option where
81-
map_const := by
82-
intro α β
83-
rfl
84-
id_map := by
85-
intro α x
86-
cases x <;> rfl
87-
seqLeft_eq := by
88-
intro α β x y
89-
cases x <;> try rfl
90-
cases y <;> rfl
91-
seqRight_eq := by
92-
intro α β x y
93-
cases x <;> try rfl
94-
cases y <;> rfl
95-
pure_seq := by
96-
intro α β g x
97-
rfl
98-
pure_bind := by
99-
intro α β x f
100-
dsimp [Bind.bind]
101-
sorry
102-
bind_pure_comp := by sorry
103-
bind_assoc := by
104-
intro α β γ x f g
105-
cases x <;> rfl
106-
bind_map := by sorry
1+
/-
2+
### Mapping on a Tree
3+
4+
Define a function `BinTree.mapM`. By analogy to `mapM` for lists, this function should apply a monadic function to each data entry in a tree, as a preorder traversal. The type signature should be:
5+
6+
`def BinTree.mapM [Monad m] (f : α → m β) : BinTree α → m (BinTree β)`
7+
-/
8+
namespace Class
9+
10+
inductive BinTree (α : Type) where
11+
| leaf : BinTree α
12+
| branch : BinTree α → α → BinTree α → BinTree α
13+
deriving Repr
14+
15+
def BinTree.mapM [Monad m] (f : α → m β) : BinTree α → m (BinTree β)
16+
| BinTree.leaf => pure BinTree.leaf
17+
| BinTree.branch l x r => do
18+
let l' ← BinTree.mapM f l
19+
let x' ← f x
20+
let r' ← BinTree.mapM f r
21+
pure (BinTree.branch l' x' r')
22+
23+
def sample := BinTree.branch (BinTree.branch BinTree.leaf 0 BinTree.leaf) 2 (BinTree.branch BinTree.leaf 3 BinTree.leaf)
24+
25+
def test (n : Nat) : Option Nat :=
26+
if n == 0 then none
27+
else some n
28+
29+
example : BinTree.mapM test sample = none := rfl
30+
31+
/-
32+
### The Option Monad Contract
33+
34+
First, write a convincing argument that the `Monad` instance for `Option` satisfies the monad contract. Then, consider the following instance:
35+
36+
```
37+
instance : Monad Option where
38+
pure x := some x
39+
bind opt next := none
40+
```
41+
42+
Both methods have the correct type. Why does this instance violate the monad contract?
43+
-/
44+
45+
instance instLawfulMonadOption : LawfulMonad Option where
46+
map_const := by
47+
intro α β
48+
rfl
49+
id_map := by
50+
intro α x
51+
cases x <;> rfl
52+
seqLeft_eq := by
53+
intro α β x y
54+
cases x <;> try rfl
55+
cases y <;> rfl
56+
seqRight_eq := by
57+
intro α β x y
58+
cases x <;> try rfl
59+
cases y <;> rfl
60+
pure_seq := by
61+
intro α β g x
62+
rfl
63+
pure_bind := by
64+
intro α β x f
65+
rfl
66+
bind_pure_comp := by
67+
intro α x f y
68+
cases y <;> rfl
69+
bind_assoc := by
70+
intro α β γ x f g
71+
cases x <;> rfl
72+
bind_map := by
73+
intro α β x f
74+
cases x <;> rfl
75+
76+
instance (priority := high) : Monad Option where
77+
pure x := some x
78+
bind opt next := none
79+
80+
instance : LawfulMonad Option where
81+
map_const := by
82+
intro α β
83+
rfl
84+
id_map := by
85+
intro α x
86+
cases x <;> rfl
87+
seqLeft_eq := by
88+
intro α β x y
89+
cases x <;> try rfl
90+
cases y <;> rfl
91+
seqRight_eq := by
92+
intro α β x y
93+
cases x <;> try rfl
94+
cases y <;> rfl
95+
pure_seq := by
96+
intro α β g x
97+
rfl
98+
pure_bind := by
99+
intro α β x f
100+
dsimp [Bind.bind]
101+
sorry
102+
bind_pure_comp := by sorry
103+
bind_assoc := by
104+
intro α β γ x f g
105+
cases x <;> rfl
106+
bind_map := by sorry
107+
108+
end Class
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
leanprover/lean4:4.6.0
1+
leanprover/lean4:4.8.0

0 commit comments

Comments
 (0)