leanfran 0.0.0-stage → 0.6.0-rc.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,78 @@
1
+ namespace Ts
2
+
3
+ inductive Exc where
4
+ | error (name : String) (message : String)
5
+ | undefinedValue
6
+ | zeroDivision
7
+ deriving Repr, DecidableEq
8
+
9
+ abbrev M := Except Exc
10
+
11
+ def index (xs : List α) (i : Int) : Option α :=
12
+ if i < 0 then none else xs[i.toNat]?
13
+
14
+ def get (xs : List α) (i : Int) : M α :=
15
+ match index xs i with
16
+ | some v => pure v
17
+ | none => throw .undefinedValue
18
+
19
+ def unwrap (o : Option α) : M α :=
20
+ match o with
21
+ | some v => pure v
22
+ | none => throw .undefinedValue
23
+
24
+ def floorDiv (a b : Int) : M Int := if b = 0 then throw .zeroDivision else pure (Int.fdiv a b)
25
+
26
+ def truncDiv (a b : Int) : M Int := if b = 0 then throw .zeroDivision else pure (Int.tdiv a b)
27
+
28
+ def rem (a b : Int) : M Int := if b = 0 then throw .zeroDivision else pure (Int.tmod a b)
29
+
30
+ def range (a b : Int) : List Int := (List.range (b - a).toNat).map fun (k : Nat) => a + (k : Int)
31
+
32
+ def abs (a : Int) : Int := if a < 0 then -a else a
33
+
34
+ def length (xs : List α) : Int := xs.length
35
+
36
+ def strLength (s : String) : Int :=
37
+ (s.toList.map fun c => if c.toNat ≥ 0x10000 then (2 : Int) else 1).sum
38
+
39
+ def startsWith (s t : String) : Bool := t.toList.isPrefixOf s.toList
40
+
41
+ def endsWith (s t : String) : Bool := t.toList.isSuffixOf s.toList
42
+
43
+ def includes (s t : String) : Bool :=
44
+ (List.range (s.length + 1)).any fun i => t.toList.isPrefixOf (s.toList.drop i)
45
+
46
+ class Str (α : Type) where
47
+ str : α → String
48
+
49
+ instance : Str Int := ⟨fun n => toString n⟩
50
+ instance : Str String := ⟨id⟩
51
+ instance : Str Bool := ⟨fun b => if b then "true" else "false"⟩
52
+
53
+ def str [Str α] (a : α) : String := Str.str a
54
+
55
+ @[simp] theorem str_string (s : String) : str s = s := rfl
56
+
57
+ @[simp] theorem length_eq (xs : List α) : length xs = (xs.length : Int) := rfl
58
+
59
+ @[simp] theorem range_mem (a b i : Int) : i ∈ range a b ↔ a ≤ i ∧ i < b := by
60
+ simp only [range, List.mem_map, List.mem_range]
61
+ constructor
62
+ · rintro ⟨k, hk, rfl⟩; omega
63
+ · intro h; exact ⟨(i - a).toNat, by omega, by omega⟩
64
+
65
+ theorem range_succ (a b : Int) (h : a ≤ b) : range a (b + 1) = range a b ++ [b] := by
66
+ simp only [range]
67
+ have h1 : (b + 1 - a).toNat = (b - a).toNat + 1 := by omega
68
+ rw [h1, List.range_succ, List.map_append]
69
+ simp only [List.map_cons, List.map_nil, List.append_cancel_left_eq, List.cons.injEq, and_true]
70
+ omega
71
+
72
+ @[simp] theorem index_nil (i : Int) : index ([] : List α) i = none := by
73
+ unfold index; split <;> simp
74
+
75
+ theorem abs_nonneg (a : Int) : 0 ≤ abs a := by
76
+ unfold abs; split <;> omega
77
+
78
+ end Ts