Skip to content

Commit 8ad1804

Browse files
committed
solution of assignments
1 parent e6d5efa commit 8ad1804

File tree

2 files changed

+67
-24
lines changed

2 files changed

+67
-24
lines changed

Fad/Assignment02.lean

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,54 @@ the property that the value of `e` after simplification is the same as the
201201
value of `e` before. -/
202202

203203
theorem simplify_correct (env : String → Int) (e : AExp)
204-
: eval env (simplify e) = eval env e := by sorry
205-
204+
: eval env (simplify e) = eval env e := by
205+
induction e with
206+
| num i =>
207+
simp [simplify]
208+
| var x =>
209+
simp [simplify]
210+
| add e₁ e₂ ih₁ ih₂ =>
211+
if h₁: e₁ = AExp.num 0 then
212+
rw [h₁]
213+
simp [simplify, ih₂, eval]
214+
else if h₂ : e₂ = AExp.num 0 then
215+
rw [h₂]
216+
simp [simplify, ih₁, eval]
217+
else
218+
simp [simplify, eval, ih₁, ih₂]
219+
| sub e₁ e₂ ih₁ ih₂ =>
220+
if h₁: e₂ = AExp.num 0 then
221+
rw [h₁]
222+
simp [simplify, ih₁, eval]
223+
else if h₂: e₁ = AExp.num 0 then
224+
rw [h₂]
225+
simp [simplify, ih₂, eval]
226+
else
227+
simp [simplify, eval, ih₁, ih₂]
228+
| mul e₁ e₂ ih₁ ih₂ =>
229+
if h₁: e₁ = AExp.num 1 then
230+
rw [h₁]
231+
simp [simplify, eval, ih₂]
232+
else if h₂ : e₂ = AExp.num 1 then
233+
rw [h₂]
234+
simp [simplify, eval, ih₁]
235+
else if h₃ : e₂ = AExp.num 0 then
236+
rw [h₃]
237+
simp [simplify, eval]
238+
else if h₄ : e₁ = AExp.num 0 then
239+
rw [h₄]
240+
simp [simplify, eval]
241+
else
242+
simp [simplify, eval, ih₁, ih₂]
243+
| div e₁ e₂ ih₁ ih₂ =>
244+
if h₁ : e₂ = AExp.num 1 then
245+
rw [h₁]
246+
simp [simplify, eval, ih₁]
247+
else if h₂ : e₁ = AExp.num 0 then
248+
rw [h₂]
249+
simp [simplify, eval]
250+
else
251+
simp [simplify, eval, ih₁, ih₂]
206252

207253

208254
end Assignment02

Fad/Assignment03.lean

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,22 @@ import Fad.Chapter1
33

44
namespace Assignment03
55

6-
def compress (s : String) : List (Nat × Nat) := sorry
7-
8-
/-
9-
#eval compress "1333332222211" -- returns [(1, 1), (3, 5), (2, 5), (1, 2)]
10-
-/
11-
12-
def uncompress : List (Nat × Nat) → String := sorry
6+
def compress (s : String) : List (Nat × Nat) :=
7+
help (s.toList.map (·.toNat - '0'.toNat)) []
8+
where
9+
help : List Nat → List (Nat × Nat) → List (Nat × Nat)
10+
| [] , acc => acc.reverse
11+
| x :: xs, [] => help xs [(x, 1)]
12+
| x :: xs, p :: acc =>
13+
if x = p.1
14+
then help xs ((p.1, p.2 + 1) :: acc)
15+
else help xs ((x, 1) :: p :: acc)
16+
17+
-- #eval compress "1333332222211" -- returns [(1, 1), (3, 5), (2, 5), (1, 2)]
18+
19+
def uncompress : List (Nat × Nat) → String :=
20+
let h (xs : List Nat) := xs.foldr (λ a r => s!"{a}{r}") ""
21+
List.foldr (λ p r => h (List.replicate p.2 p.1) ++ r) ""
1322

1423
/-
1524
#eval uncompress [(1, 1), (3, 5), (2, 5), (1, 2)] -- returns "1333332222211"
@@ -28,12 +37,6 @@ def SymList.reverse : SymList a → SymList a
2837

2938
#eval List.toSL [1,2,3] |>.reverse |>.fromSL
3039

31-
/- qual a complexidade de SymList.reverse? -/
32-
33-
def answer₁ := "O(???)"
34-
35-
/- tente completar a prova -/
36-
3740
example : ∀ sl : SymList a,
3841
List.reverse sl.fromSL = sl.reverse.fromSL := sorry
3942

@@ -44,25 +47,19 @@ da frente e as funções no elemento de trás, poderíamos definir as
4447
funções no elemento de trás em termos de reverse e das funções
4548
correspondentes no elemento da frente.
4649
47-
Defina `init` como uma expressão usando outras funções sobre SymList
48-
sem recorrer as sublistas?
50+
Defina `init` como uma expressão usando `tailSL` e `SymList.reverse`?
4951
-/
5052

51-
def SymList.init (q : SymList a) : SymList a := sorry
53+
def SymList.init (q : SymList a) : SymList a :=
54+
q.reverse.tailSL.reverse
5255

5356
-- #eval [1,2,3].toSL |>.init |>.fromSL -- returns [1,2]
5457

55-
56-
/- qual a complexidade de SymList.init? -/
57-
58-
def answer₂ := "O(???)"
59-
6058
def List.init {α : Type} : List α → List α
6159
| [] => []
6260
| [_] => []
6361
| x :: xs => x :: init xs
6462

65-
6663
/- agora tente completar a prova seguinte -/
6764

6865
example : ∀ sl : SymList a,

0 commit comments

Comments
 (0)