Skip to content

Commit c90e554

Browse files
rename some variables to n and l for lengths
1 parent 0021cbf commit c90e554

25 files changed

Lines changed: 360 additions & 358 deletions

Nomenclature.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515
* ih, ih.* (inductive hypothesis)
1616
* iff, Iff (if and only if)
1717
* l, l[0-9]* (length variable name)
18+
* l (length of a Vector)
1819
* le (less than or equal)
1920
* lt (less than)
2021
* m (monad type variable)
2122
* n (natural numbers of finite number variable names)
23+
* n (natural number representing the number of non-terminals or production rules)
2224
* ℕ (natural number type)
23-
* μ (a number used in as a type parameter, for example Fin μ)
2425
* ν (nullable function name)
2526
* Φ, Ψ (generic predicate type parameter, Phi/Psi)
2627
* r (regular expression variable names) (TODO: need two more? s is symbol, p and q are for predicates)

Validator/Derive/Enter.lean

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ import Validator.Expr.Symbol
77

88
namespace Enter
99

10-
def deriveEnter (xs: Rules μ α Pred ν): IfExprs μ α (Symbol.nums xs) :=
10+
def deriveEnter (xs: Rules n α Pred l): IfExprs n α (Symbol.nums xs) :=
1111
List.Vector.map
1212
(fun (pred, ref) => IfExpr.mk pred ref)
1313
(Symbol.Symbols.cast
1414
(Symbol.extracts xs List.Vector.nil).2
1515
(by ac_rfl)
1616
)
1717

18-
class DeriveEnter (m: Type -> Type u) (μ: Nat) (α: outParam Type) where
19-
deriveEnter {ν: Nat} (xs: Rules μ α Pred ν): m (IfExprs μ α (Symbol.nums xs))
18+
class DeriveEnter (m: Type -> Type u) (n: Nat) (α: outParam Type) where
19+
deriveEnter {l: Nat} (xs: Rules n α Pred l): m (IfExprs n α (Symbol.nums xs))
2020

21-
def deriveEnter_list (xs: List (Rule μ α Pred)): List (IfExpr μ α) :=
21+
def deriveEnter_list (xs: List (Rule n α Pred)): List (IfExpr n α) :=
2222
List.map (fun (pred, ref) => IfExpr.mk pred ref) (Symbol.extracts_list xs []).2

Validator/Derive/Leave.lean

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Validator.Derive.Enter
55

66
namespace Leave
77

8-
def leave (x: Regex (Fin ν)) (es: Symbol.Symbols (Pred α × Ref μ) ν) (ns: List.Vector Bool ν): (Rule μ α Pred) :=
8+
def leave (x: Regex (Fin l)) (es: Symbol.Symbols (Pred α × Ref n) l) (ns: List.Vector Bool l): (Rule n α Pred) :=
99
match x with
1010
| Regex.emptyset => Regex.emptyset
1111
| Regex.emptystr => Regex.emptyset
@@ -28,10 +28,10 @@ def leave (x: Regex (Fin ν)) (es: Symbol.Symbols (Pred α × Ref μ) ν) (ns: L
2828
-- The vectors of bools represent the nullability of the derived child expressions.
2929
-- Each bool will then replace each symbol expression with either an emptystr or emptyset.
3030
def deriveLeave
31-
(xs: List.Vector (Regex (Fin ν)) μ1)
32-
(es: Symbol.Symbols (Pred α × Ref μ) ν)
33-
(ns: List.Vector Bool ν)
34-
: (Rules μ α Pred μ1) :=
31+
(xs: List.Vector (Regex (Fin l1)) l2)
32+
(es: Symbol.Symbols (Pred α × Ref n) l1)
33+
(ns: List.Vector Bool l1)
34+
: (Rules n α Pred l2) :=
3535
match xs with
3636
| ⟨[], h⟩ => ⟨[], h⟩
3737
| ⟨x::xs, h⟩ =>
@@ -46,14 +46,14 @@ def deriveLeave
4646
)
4747

4848
def deriveLeaves
49-
(xs: Rules μ α Pred ν)
49+
(xs: Rules n α Pred l)
5050
(ns: List.Vector Bool (Symbol.nums xs))
51-
: (Rules μ α Pred ν) :=
51+
: (Rules n α Pred l) :=
5252
let (regexes, symbols) := Symbol.extracts xs List.Vector.nil
5353
deriveLeave regexes symbols (Symbol.Symbols.cast ns (by simp only [zero_add]))
5454

55-
def deriveLeaveM [Monad m] {μ: Nat} {ν: Nat} (xs: Rules μ α Pred ν) (ns: List.Vector Bool (Symbol.nums xs)): m (Rules μ α Pred ν) := do
55+
def deriveLeaveM [Monad m] {n: Nat} {l: Nat} (xs: Rules n α Pred l) (ns: List.Vector Bool (Symbol.nums xs)): m (Rules n α Pred l) := do
5656
return deriveLeaves xs ns
5757

58-
class DeriveLeaveM (m: Type -> Type u) (μ: Nat) (α: outParam Type) where
59-
deriveLeaveM {ν: Nat} (xs: Rules μ α Pred ν) (ns: List.Vector Bool (Symbol.nums xs)): m (Rules μ α Pred ν)
58+
class DeriveLeaveM (m: Type -> Type u) (n: Nat) (α: outParam Type) where
59+
deriveLeaveM {l: Nat} (xs: Rules n α Pred l) (ns: List.Vector Bool (Symbol.nums xs)): m (Rules n α Pred l)

Validator/Expr/BCFG.lean

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@ import Validator.Expr.Pred
55

66
namespace BalancedContextFreeGrammar
77

8-
abbrev Ref μ := Fin μ
8+
abbrev Ref n := Fin n
99

1010
-- Balanced Context Free Grammars have the restriction that each non-terminal cannot represent any regular expression,
1111
-- but only regular expressions that are surrounded by a start and end terminal.
12-
structure Rule (μ: Nat) (α: Type) (Φ: (α: Type) -> Type) where
12+
structure Rule (n: Nat) (α: Type) (Φ: (α: Type) -> Type) where
1313
enter: Φ α
14-
child: Regex (Ref μ)
14+
child: Regex (Ref n)
1515
leave: Φ α
1616

17-
abbrev RegexRule (μ: Nat) (α: Type) (Φ: (α: Type) -> Type) := Regex ((Φ α) ⊕ (Ref μ))
17+
abbrev RegexRule (n: Nat) (α: Type) (Φ: (α: Type) -> Type) := Regex ((Φ α) ⊕ (Ref n))
1818

19-
def Rule.toRegex {μ: Nat} {α: Type} {Φ: (α: Type) -> Type}
20-
(r: Rule μ α Φ): RegexRule μ α Φ :=
19+
def Rule.toRegex {n: Nat} {α: Type} {Φ: (α: Type) -> Type}
20+
(r: Rule n α Φ): RegexRule n α Φ :=
2121
(Regex.concat
2222
(Regex.symbol (Sum.inl r.enter))
2323
(Regex.concat
@@ -26,14 +26,14 @@ def Rule.toRegex {μ: Nat} {α: Type} {Φ: (α: Type) -> Type}
2626
)
2727
)
2828

29-
abbrev Rules (μ: Nat) (α: Type) (Φ: (α: Type) -> Type) := { xs: List (Rule μ α Φ) // xs.length > 0 }
29+
abbrev Rules (n: Nat) (α: Type) (Φ: (α: Type) -> Type) := { xs: List (Rule n α Φ) // xs.length > 0 }
3030

31-
structure BCFG (μ: Nat) (α: Type) (Φ: (α: Type) -> Type) where
32-
start: { s: List (Ref μ) // s.length > 0 }
33-
prods: Vector (Rules μ α Φ) μ
31+
structure BCFG (n: Nat) (α: Type) (Φ: (α: Type) -> Type) where
32+
start: { s: List (Ref n) // s.length > 0 }
33+
prods: Vector (Rules n α Φ) n
3434

35-
def union_rules {μ: Nat} {α: Type} {Φ: (α: Type) -> Type}
36-
(xs: Rules μ α Φ): RegexRule μ α Φ :=
35+
def union_rules {n: Nat} {α: Type} {Φ: (α: Type) -> Type}
36+
(xs: Rules n α Φ): RegexRule n α Φ :=
3737
match xs with
3838
| Subtype.mk xs hxs =>
3939
match xs with
@@ -42,8 +42,8 @@ def union_rules {μ: Nat} {α: Type} {Φ: (α: Type) -> Type}
4242
| (x::xs) => List.foldl Regex.or x.toRegex (List.map Rule.toRegex xs)
4343

4444
def BCFG.getStart
45-
{μ: Nat} {α: Type} {Φ: (α: Type) -> Type}
46-
(g: BCFG μ α Φ): RegexRule μ α Φ :=
45+
{n: Nat} {α: Type} {Φ: (α: Type) -> Type}
46+
(g: BCFG n α Φ): RegexRule n α Φ :=
4747
match g.start with
4848
| Subtype.mk s hs =>
4949
match s with
@@ -52,15 +52,15 @@ def BCFG.getStart
5252
let xs' := (List.map (fun x' => Regex.symbol (Sum.inr x'))) xs
5353
List.foldl Regex.or (Regex.symbol (Sum.inr x)) xs'
5454

55-
def BCFG.lookup {μ: Nat} {α: Type} {Φ: (α: Type) -> Type}
56-
(g: BCFG μ α Φ) (ref: Fin μ): RegexRule μ α Φ :=
55+
def BCFG.lookup {n: Nat} {α: Type} {Φ: (α: Type) -> Type}
56+
(g: BCFG n α Φ) (ref: Fin n): RegexRule n α Φ :=
5757
union_rules (Vector.get g.prods ref)
5858

59-
def nullable {μ: Nat} {α: Type} {Φ: (α: Type) -> Type} (r: RegexRule μ α Φ): Bool :=
59+
def nullable {n: Nat} {α: Type} {Φ: (α: Type) -> Type} (r: RegexRule n α Φ): Bool :=
6060
Regex.nullable r
6161

62-
partial def derive {μ: Nat} {α: Type} [DecidableEq α] [BEq α]
63-
(g: BCFG μ α Pred) (r: RegexRule μ α Pred) (a: α): RegexRule μ α Pred :=
62+
partial def derive {n: Nat} {α: Type} [DecidableEq α] [BEq α]
63+
(g: BCFG n α Pred) (r: RegexRule n α Pred) (a: α): RegexRule n α Pred :=
6464
match r with
6565
| Regex.emptyset => Regex.emptyset
6666
| Regex.emptystr => Regex.emptyset

Validator/Expr/Compress.lean

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -133,16 +133,16 @@ def vector_cast (h: n = m) (xs: List.Vector α n): List.Vector α m := by
133133
subst h
134134
exact xs
135135

136-
def compress [DecidableEq α] (xs: Rules μ α Pred ν): Σ n, ((Rules μ α Pred n) × (Indices n ν)) :=
137-
let xs_list: List (Rule μ α Pred) := xs.toList
138-
have hn : xs_list.length = ν := by
136+
def compress [DecidableEq α] (xs: Rules n α Pred l1): Σ l2, ((Rules n α Pred l2) × (Indices l2 l1)) :=
137+
let xs_list: List (Rule n α Pred) := xs.toList
138+
have hn : xs_list.length = l1 := by
139139
simp_all only [List.Vector.toList_length, xs_list]
140140

141141
-- sort to increase chance of cache hit
142142
-- TODO: let sxs := List.mergeSort xs
143143

144144
-- remove duplicates
145-
let xs_noreps: { ys: List (Rule μ α Pred) // ∀ x ∈ xs_list, x ∈ ys } := eraseReps_sub xs_list
145+
let xs_noreps: { ys: List (Rule n α Pred) // ∀ x ∈ xs_list, x ∈ ys } := eraseReps_sub xs_list
146146

147147
-- get indices
148148
let xs_idxs: List.Vector (Fin (xs_noreps.val).length) (List.length xs_list) := indices xs_noreps
@@ -152,19 +152,19 @@ def compress [DecidableEq α] (xs: Rules μ α Pred ν): Σ n, ((Rules μ α Pre
152152

153153
-- find all indexes of the original expressions in the compressed expressions
154154
let indices: Indices (xs_noreps.val).length (List.length xs_list) := List.Vector.map (fun x => Index.val x) xs_idxs
155-
let indices': Indices (xs_noreps.val).length ν := vector_cast hn indices
155+
let indices': Indices (xs_noreps.val).length l1 := vector_cast hn indices
156156

157157
Sigma.mk (List.length (xs_noreps.val)) (
158158
Subtype.mk xs_noreps rfl,
159159
indices'
160160
)
161161

162-
def memVector (xs: List.Vector α ν) (y: α): Prop :=
162+
def memVector (xs: List.Vector α l) (y: α): Prop :=
163163
match xs with
164164
| ⟨[], _⟩ => False
165165
| ⟨x::xs, h⟩ => x = y \/ memVector ⟨xs, congrArg Nat.pred h⟩ y
166166

167-
instance [DecidableEq α]: Membership α (List.Vector α ν) where
167+
instance [DecidableEq α]: Membership α (List.Vector α l) where
168168
mem := memVector
169169

170170
theorem memVector_nil {n: Nat} {hxs: [].length = n}
@@ -189,7 +189,7 @@ theorem memVector_cons {n: Nat} {hxs: (x::xs).length = n}
189189
generalize_proofs at h
190190
exact h
191191

192-
def indexOf' [DecidableEq α] (xs: List.Vector α ν) (y: α) (h: y ∈ xs): Fin (xs.length) :=
192+
def indexOf' [DecidableEq α] (xs: List.Vector α l) (y: α) (h: y ∈ xs): Fin (xs.length) :=
193193
match xs with
194194
| ⟨[], hxs⟩ => by
195195
exfalso
@@ -223,27 +223,27 @@ theorem memVector_emptyset [DecidableEq α] {y: Regex α}
223223
simp only [beq_iff_eq] at h2
224224
contradiction
225225

226-
def indexOf [DecidableEq α] (xs: List.Vector (Regex α) ν) (y: Regex α) (h: y ∈ xs \/ y = Regex.emptyset): Index (xs.length) :=
226+
def indexOf [DecidableEq α] (xs: List.Vector (Regex α) l) (y: Regex α) (h: y ∈ xs \/ y = Regex.emptyset): Index (xs.length) :=
227227
if hy: y == Regex.emptyset
228228
then Index.emptyset
229229
else Index.val (indexOf' xs y (memVector_emptyset h hy))
230230

231-
def ofIndex' (xs: Rules μ α Pred ν) (index: Fin ν): Rule μ α Pred :=
231+
def ofIndex' (xs: Rules n α Pred l) (index: Fin l): Rule n α Pred :=
232232
xs.get index
233233

234-
def ofIndex (xs: Rules μ α Pred ν) (index: Index ν): Rule μ α Pred :=
234+
def ofIndex (xs: Rules n α Pred l) (index: Index l): Rule n α Pred :=
235235
match index with
236236
| Index.emptyset => Regex.emptyset
237237
| Index.val n => ofIndex' xs n
238238

239-
def compressed [DecidableEq σ] (xs: List.Vector (Regex σ) ν): Nat :=
239+
def compressed [DecidableEq σ] (xs: List.Vector (Regex σ) l): Nat :=
240240
(List.erase (List.eraseReps xs.toList) Regex.emptyset).length
241241

242242
-- theorem compressed_cons_emptyset [DecidableEq σ] (xs: List (Regex σ)):
243243
-- compressed ⟨Regex.emptyset :: xs, h⟩ = compressed ⟨xs, congrArg Nat.pred h⟩ := by
244244
-- sorry
245245

246-
def numReps [DecidableEq α] (xs: List.Vector α μ) (x: Option α := Option.none): Nat :=
246+
def numReps [DecidableEq α] (xs: List.Vector α l) (x: Option α := Option.none): Nat :=
247247
match x with
248248
| Option.none =>
249249
match xs with
@@ -257,7 +257,7 @@ def numReps [DecidableEq α] (xs: List.Vector α μ) (x: Option α := Option.non
257257
then 1 + numReps ⟨xs, congrArg Nat.pred h⟩ (Option.some x')
258258
else numReps ⟨xs, congrArg Nat.pred h⟩ (Option.some x')
259259

260-
theorem numReps_none_le_length [DecidableEq α] (xs: List.Vector α μ):
260+
theorem numReps_none_le_length [DecidableEq α] (xs: List.Vector α l):
261261
numReps xs Option.none <= xs.length := by
262262
induction xs with
263263
| nil =>
@@ -292,7 +292,7 @@ theorem numReps_none_le_length [DecidableEq α] (xs: List.Vector α μ):
292292
simp [List.Vector.length] at ih
293293
omega
294294

295-
theorem numReps_some_le_length [DecidableEq α] (xs: List.Vector α μ):
295+
theorem numReps_some_le_length [DecidableEq α] (xs: List.Vector α l):
296296
numReps xs (Option.some y) <= xs.length := by
297297
induction xs generalizing y with
298298
| nil =>
@@ -319,15 +319,15 @@ theorem numReps_some_le_length [DecidableEq α] (xs: List.Vector α μ):
319319
have ih' := ih (y := x)
320320
omega
321321

322-
theorem numReps_le_length [DecidableEq α] (xs: List.Vector α μ) (y: Option α):
322+
theorem numReps_le_length [DecidableEq α] (xs: List.Vector α l) (y: Option α):
323323
numReps xs y <= xs.length := by
324324
cases y with
325325
| none =>
326326
apply numReps_none_le_length
327327
| some y =>
328328
apply numReps_some_le_length
329329

330-
-- def eraseReps [DecidableEq α] (xs: List.Vector α μ) (x: Option α := Option.none): List.Vector α (μ - numReps xs x) :=
330+
-- def eraseReps [DecidableEq α] (xs: List.Vector α l) (x: Option α := Option.none): List.Vector α (l - numReps xs x) :=
331331
-- match x with
332332
-- | Option.none =>
333333
-- match xs with
@@ -394,7 +394,7 @@ theorem numReps_le_length [DecidableEq α] (xs: List.Vector α μ) (y: Option α
394394
-- rw [Nat.sub_add_comm hsome]
395395
-- ⟩
396396

397-
def smallest [DecidableEq α] [LT α] [DecidableLT α] (xs: List.Vector α ν) (y: α): Option (Fin ν) :=
397+
def smallest [DecidableEq α] [LT α] [DecidableLT α] (xs: List.Vector α l) (y: α): Option (Fin l) :=
398398
match xs with
399399
| ⟨[], h⟩ => Option.none
400400
| ⟨x::xs, h⟩ =>
@@ -417,7 +417,7 @@ def smallest [DecidableEq α] [LT α] [DecidableLT α] (xs: List.Vector α ν) (
417417
simp
418418
419419

420-
-- def comp [DecidableEq σ] [LT (Regex σ)] [DecidableLT (Regex σ)] (xs: List.Vector (Regex σ) ν) (dup: Option (Regex σ) := Option.none): (List.Vector (Regex σ) (compressed xs)) × (Indices ν (compressed xs)) :=
420+
-- def comp [DecidableEq σ] [LT (Regex σ)] [DecidableLT (Regex σ)] (xs: List.Vector (Regex σ) l) (dup: Option (Regex σ) := Option.none): (List.Vector (Regex σ) (compressed xs)) × (Indices l (compressed xs)) :=
421421
-- match xs with
422422
-- | ⟨[], h⟩ => (⟨[], by simp [compressed, List.eraseReps, List.eraseRepsBy]⟩, ⟨[], h⟩)
423423
-- | ⟨x::xs, h⟩ =>
@@ -436,12 +436,12 @@ def smallest [DecidableEq α] [LT α] [DecidableLT α] (xs: List.Vector α ν) (
436436
-- | Option.some dup => sorry
437437
-- | Option.some i => sorry
438438

439-
def compressM [DecidableEq α] [Monad m] (xs: Rules μ α Pred ν): m (Σ n, (Rules μ α Pred n) × Indices n ν) := do
439+
def compressM [DecidableEq α] [Monad m] (xs: Rules n α Pred l1): m (Σ l2, (Rules n α Pred l2) × Indices l2 l1) := do
440440
return compress xs
441441

442442
-- expand expands a list of expressions.
443-
def expand (indices: Indices ν1 ν2) (xs: Rules μ α Pred ν1): (Rules μ α Pred ν2) :=
443+
def expand (indices: Indices l1 l2) (xs: Rules n α Pred l1): (Rules n α Pred l2) :=
444444
List.Vector.map (ofIndex xs) indices
445445

446-
def expandM [Monad m] (indices: Indices ν1 ν2) (xs: Rules μ α Pred ν1): m (Rules μ α Pred ν2) :=
446+
def expandM [Monad m] (indices: Indices l1 l2) (xs: Rules n α Pred l1): m (Rules n α Pred l2) :=
447447
return (expand indices xs)

0 commit comments

Comments
 (0)