Skip to content

Commit a974f13

Browse files
add list splitat theorem
Also move extra Nat theorems into their own file
1 parent 52b919e commit a974f13

File tree

5 files changed

+111
-90
lines changed

5 files changed

+111
-90
lines changed

Katydid.lean

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Katydid.Std.Ordering
22
import Katydid.Std.Decidable
33
import Katydid.Std.OrderingThunk
4+
import Katydid.Std.Nat
45
import Katydid.Std.Lists
56
import Katydid.Std.Ltac
67
import Katydid.Std.Balistic

Katydid/Expr/Desc.lean

+10-10
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ We want to represent some nested function calls for a very restricted language,
66
We represent the description (including AST) of the expr, or as we call it the Descriptor here:
77
-/
88

9-
namespace Desc
10-
119
inductive Desc where
1210
| intro
1311
(name: String)
@@ -16,11 +14,13 @@ inductive Desc where
1614
(reader: Bool)
1715
deriving Repr
1816

19-
def Desc.name (desc: Desc): String :=
17+
namespace Desc
18+
19+
def name (desc: Desc): String :=
2020
match desc with
2121
| ⟨ name, _, _, _ ⟩ => name
2222

23-
def Desc.params (desc: Desc): List Desc :=
23+
def params (desc: Desc): List Desc :=
2424
match desc with
2525
| ⟨ _, params, _, _⟩ => params
2626

@@ -30,24 +30,24 @@ The `hash` field is important, because it is used to efficiently compare functio
3030
* and(lt(3, 5), lt(3, 5)) => lt(3, 5)
3131
* or(and(lt(3, 5), contains("abcd", "bc")), and(contains("abcd", "bc"), lt(3, 5))) => and(contains("abcd", "bc"), lt(3, 5))
3232
-/
33-
def hash_list (innit: UInt64) (list: List UInt64): UInt64 :=
33+
private def hash_list (innit: UInt64) (list: List UInt64): UInt64 :=
3434
List.foldl (fun acc h => 31 * acc + h) innit list
3535

36-
def hash_string (s: String): UInt64 :=
36+
private def hash_string (s: String): UInt64 :=
3737
hash_list 0 (List.map (Nat.toUInt64 ∘ Char.toNat) (String.toList s))
3838

39-
def hash_desc (desc: Desc): UInt64 :=
39+
private def hash_desc (desc: Desc): UInt64 :=
4040
match desc with
4141
| ⟨ name, params, _, _⟩ => hash_list (31 * 17 + hash_string name) (hash_params params)
4242
where hash_params (params: List Desc): List UInt64 :=
4343
match params with
4444
| [] => []
4545
| param::params => hash_desc param :: hash_params params
4646

47-
def Desc.hash (desc: Desc): UInt64 :=
47+
def hash (desc: Desc): UInt64 :=
4848
hash_desc desc
4949

50-
def any_reader (d: Desc): Bool :=
50+
private def any_reader (d: Desc): Bool :=
5151
match d with
5252
| ⟨ _, params, _, _⟩ => any_params params
5353
where any_params (params: List Desc): Bool :=
@@ -56,7 +56,7 @@ where any_params (params: List Desc): Bool :=
5656
| param::params => any_reader param || any_params params
5757

5858
/- The reader field tells us whether the function has any variables or can be evaluated at compile time. -/
59-
def Desc.reader (desc: Desc): Bool :=
59+
def reader (desc: Desc): Bool :=
6060
any_reader desc
6161

6262
inductive IsSmart : Desc → Prop

Katydid/Regex/Language.lean

+1-1
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ def derive_char {α: Type} [DecidableEq α] {a: α} {c: α}:
256256
funext
257257
rw [derive_iff_char]
258258

259-
def derive_iff_pred {α: Type} {p: α -> Prop} [dp: DecidablePred p] {x: α} {xs: List α}:
259+
def derive_iff_pred {α: Type} {p: α -> Prop} {x: α} {xs: List α}:
260260
(derive (pred p) x) xs <-> (onlyif (p x) emptystr) xs := by
261261
simp only [derive, derives, singleton_append]
262262
simp only [onlyif, emptystr]

Katydid/Std/Lists.lean

+18-79
Original file line numberDiff line numberDiff line change
@@ -9,74 +9,11 @@
99

1010
import Mathlib.Tactic.Linarith
1111

12+
import Katydid.Std.Nat
13+
1214
open Nat
1315
open List
1416

15-
theorem nat_succ_le_succ_iff (x y: Nat):
16-
succ x ≤ succ y <-> x ≤ y := by
17-
apply Iff.intro
18-
case mp =>
19-
apply Nat.le_of_succ_le_succ
20-
case mpr =>
21-
apply Nat.succ_le_succ
22-
23-
theorem nat_succ_eq_plus_one : succ n = n + 1 := by
24-
simp only
25-
26-
theorem nat_pred_le_succ : {n m : Nat} -> Nat.le n (succ m) -> Nat.le (pred n) m
27-
| zero, zero, _ => Nat.le.refl
28-
| _, _, Nat.le.refl => Nat.le.refl
29-
| zero, succ _, Nat.le.step h => h
30-
| succ _, succ _, Nat.le.step h => Nat.le_trans (le_succ _) h
31-
32-
theorem nat_pred_le_succ' : {n m : Nat} -> Nat.le n (succ m) -> Nat.le (pred n) m := by
33-
intro n m h
34-
cases h with
35-
| refl =>
36-
constructor
37-
| step h =>
38-
cases n with
39-
| zero =>
40-
dsimp only [zero_eq, Nat.pred_zero, le_eq]
41-
exact h
42-
| succ n =>
43-
dsimp only [Nat.pred_succ, le_eq]
44-
have h_n_le_succ_n := Nat.le_succ n
45-
exact (Nat.le_trans h_n_le_succ_n h)
46-
47-
48-
theorem nat_min_zero {n: Nat}: min 0 n = 0 :=
49-
Nat.min_eq_left (Nat.zero_le _)
50-
51-
theorem nat_zero_min {n: Nat}: min n 0 = 0 :=
52-
Nat.min_eq_right (Nat.zero_le _)
53-
54-
theorem nat_add_succ_is_succ_add (n m: Nat): succ n + m = succ (n + m) := by
55-
cases n with
56-
| zero =>
57-
rewrite [Nat.add_comm]
58-
simp only [zero_eq, zero_add]
59-
| succ n =>
60-
rewrite [Nat.add_comm]
61-
rewrite [Nat.add_comm (succ n)]
62-
repeat rewrite [Nat.add_succ]
63-
rfl
64-
65-
theorem nat_pred_le_pred : {n m : Nat} → LE.le n m → LE.le (pred n) (pred m) := by
66-
intro n m h
67-
cases h with
68-
| refl => constructor
69-
| step h =>
70-
rename_i m
71-
cases n with
72-
| zero =>
73-
dsimp
74-
exact h
75-
| succ n =>
76-
dsimp
77-
have h_n_le_succ_n := Nat.le_succ n
78-
exact (Nat.le_trans h_n_le_succ_n h)
79-
8017
theorem list_cons_ne_nil (x : α) (xs : List α):
8118
x :: xs ≠ [] := by
8219
intro h'
@@ -648,20 +585,6 @@ theorem list_take_drop (n: Nat) (xs: List α):
648585
apply (congrArg (cons x))
649586
apply ih
650587

651-
theorem nat_succ_gt_succ {n m: Nat}:
652-
succ n > succ m -> n > m := by
653-
intro h
654-
cases h with
655-
| refl =>
656-
constructor
657-
| step s =>
658-
apply Nat.le_of_succ_le_succ
659-
exact (Nat.le_succ_of_le s)
660-
661-
theorem nat_succ_gt_succ' {n m: Nat}:
662-
succ n > succ m -> n > m := by
663-
apply Nat.le_of_succ_le_succ
664-
665588
theorem list_take_large_length {n: Nat} {xs: List α}:
666589
n > length xs -> length (take n xs) = length xs := by
667590
revert xs
@@ -990,3 +913,19 @@ theorem list_split_flatten {α: Type} (zs: List α):
990913
| cons x ys => by
991914
exists [[x], ys]
992915
simp
916+
917+
theorem list_splitAt_eq (n: Nat) (xs: List α):
918+
splitAt n xs = (xs.take n, xs.drop n) :=
919+
splitAt_eq n xs
920+
921+
theorem list_splitAt_length {α: Type} (n: Nat) (xs: List α) (hn: n ≤ length xs):
922+
∃ xs1 xs2, (xs1, xs2) = splitAt n xs
923+
/\ xs1.length = n
924+
/\ xs2.length = xs.length - n := by
925+
have h := splitAt_eq n xs
926+
exists take n xs
927+
exists drop n xs
928+
split_ands
929+
· exact (symm (list_splitAt_eq n xs))
930+
· exact (list_take_length_le n xs hn)
931+
· exact (list_drop_length n xs)

Katydid/Std/Nat.lean

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import Mathlib.Tactic.Linarith
2+
3+
open Nat
4+
5+
theorem nat_succ_le_succ_iff (x y: Nat):
6+
succ x ≤ succ y <-> x ≤ y := by
7+
apply Iff.intro
8+
case mp =>
9+
apply Nat.le_of_succ_le_succ
10+
case mpr =>
11+
apply Nat.succ_le_succ
12+
13+
theorem nat_succ_eq_plus_one : succ n = n + 1 := by
14+
simp only
15+
16+
theorem nat_pred_le_succ : {n m : Nat} -> Nat.le n (succ m) -> Nat.le (pred n) m
17+
| zero, zero, _ => Nat.le.refl
18+
| _, _, Nat.le.refl => Nat.le.refl
19+
| zero, succ _, Nat.le.step h => h
20+
| succ _, succ _, Nat.le.step h => Nat.le_trans (le_succ _) h
21+
22+
theorem nat_pred_le_succ' : {n m : Nat} -> Nat.le n (succ m) -> Nat.le (pred n) m := by
23+
intro n m h
24+
cases h with
25+
| refl =>
26+
constructor
27+
| step h =>
28+
cases n with
29+
| zero =>
30+
dsimp only [zero_eq, Nat.pred_zero, le_eq]
31+
exact h
32+
| succ n =>
33+
dsimp only [Nat.pred_succ, le_eq]
34+
have h_n_le_succ_n := Nat.le_succ n
35+
exact (Nat.le_trans h_n_le_succ_n h)
36+
37+
theorem nat_min_zero {n: Nat}: min 0 n = 0 :=
38+
Nat.min_eq_left (Nat.zero_le _)
39+
40+
theorem nat_zero_min {n: Nat}: min n 0 = 0 :=
41+
Nat.min_eq_right (Nat.zero_le _)
42+
43+
theorem nat_add_succ_is_succ_add (n m: Nat): succ n + m = succ (n + m) := by
44+
cases n with
45+
| zero =>
46+
rewrite [Nat.add_comm]
47+
simp only [zero_eq, zero_add]
48+
| succ n =>
49+
rewrite [Nat.add_comm]
50+
rewrite [Nat.add_comm (succ n)]
51+
repeat rewrite [Nat.add_succ]
52+
rfl
53+
54+
theorem nat_pred_le_pred : {n m : Nat} → LE.le n m → LE.le (pred n) (pred m) := by
55+
intro n m h
56+
cases h with
57+
| refl => constructor
58+
| step h =>
59+
rename_i m
60+
cases n with
61+
| zero =>
62+
dsimp
63+
exact h
64+
| succ n =>
65+
dsimp
66+
have h_n_le_succ_n := Nat.le_succ n
67+
exact (Nat.le_trans h_n_le_succ_n h)
68+
69+
theorem nat_succ_gt_succ {n m: Nat}:
70+
succ n > succ m -> n > m := by
71+
intro h
72+
cases h with
73+
| refl =>
74+
constructor
75+
| step s =>
76+
apply Nat.le_of_succ_le_succ
77+
exact (Nat.le_succ_of_le s)
78+
79+
theorem nat_succ_gt_succ' {n m: Nat}:
80+
succ n > succ m -> n > m := by
81+
apply Nat.le_of_succ_le_succ

0 commit comments

Comments
 (0)