|
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 |
0 commit comments