Skip to content

Commit 36d5db9

Browse files
make deriveEnter and deriveLeave generic for regex and not specific to Hedge
1 parent 3c7e14a commit 36d5db9

24 files changed

+96
-99
lines changed

Validator.lean

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import Validator.Std
22

3-
import Validator.Derive
43
import Validator.Hedge
54
import Validator.Learning
65
import Validator.Memoize

Validator/Derive.lean

Lines changed: 0 additions & 2 deletions
This file was deleted.

Validator/Derive/Enter.lean

Lines changed: 0 additions & 15 deletions
This file was deleted.

Validator/Derive/Leave.lean

Lines changed: 0 additions & 27 deletions
This file was deleted.

Validator/Hedge/Grammar.lean

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ import Validator.Hedge.Language
1818
-- n = the number of non-terminals
1919
abbrev Ref (n: Nat) := Fin n -- non-terminal
2020

21-
abbrev Rule (n: Nat) (φ: Type) :=
22-
Regex (φ × Ref n)
21+
abbrev Symbol (n: Nat) (φ: Type) := (φ × Ref n)
22+
23+
abbrev Rule (n: Nat) (φ: Type) := Regex (Symbol n φ)
2324

2425
abbrev Rules (n: Nat) (φ: Type) (l: Nat) :=
2526
Vec (Rule n φ) l

Validator/Learning/Basic.lean

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import Validator.Hedge.IfExpr
1313
import Validator.Regex.Language
1414
import Validator.Pred.AnyEq
1515

16-
import Validator.Derive.Enter
17-
import Validator.Derive.Leave
16+
import Validator.Regex.Enter
17+
import Validator.Regex.LeaveSmart
1818

1919
namespace Basic
2020

@@ -34,7 +34,7 @@ def derive {α: Type}
3434
let dchildxs: Rules n φ (Symbol.nums xs) := List.foldl (derive G Φ) childxs children
3535
let ns: Vec Bool (Symbol.nums xs) := Vec.map dchildxs Rule.null
3636
-- leaves is the other one of our two new memoizable functions.
37-
let lchildxs: Rules n φ l := Leave.deriveLeaves xs ns
37+
let lchildxs: Rules n φ l := LeaveSmart.deriveLeaves xs ns
3838
lchildxs
3939

4040
def derivs {α: Type}

Validator/Learning/Compress.lean

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import Validator.Hedge.Grammar
1414
import Validator.Hedge.IfExpr
1515
import Validator.Regex.Regex
1616

17-
import Validator.Derive.Enter
18-
import Validator.Derive.Leave
17+
import Validator.Regex.Enter
18+
import Validator.Regex.LeaveSmart
1919

2020
namespace Compress
2121

@@ -36,7 +36,7 @@ def derive [DecidableEq φ]
3636
let cdchildxs := List.foldl (derive G Φ) cchildxs children
3737
-- dchildxs = uncompressed derivatives of children.
3838
let dchildxs := Compress.expand indices cdchildxs
39-
Leave.deriveLeaves xs (Vec.map dchildxs Rule.null)
39+
LeaveSmart.deriveLeaves xs (Vec.map dchildxs Rule.null)
4040

4141
def derivs [DecidableEq φ]
4242
(G: Grammar n φ) (Φ : φ → α → Bool)

Validator/Learning/Memoize.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Validator.Learning.Parser
1010

1111
namespace Memoize
1212

13-
def validate {m} [DecidableEq φ] [ValidateM m n φ α]
13+
def validate {m} [DecidableEq φ] [ValidateM m (Symbol n φ) α]
1414
(G: Grammar n φ) (Φ : φ → α → Bool)
1515
(x: Rule n φ): m Bool :=
1616
Parser.validate G Φ x

Validator/Learning/Parser.lean

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,33 @@ import Validator.Parser.Hint
1414
import Validator.Parser.Parser
1515
import Validator.Parser.TokenTree
1616

17-
import Validator.Derive.Enter
18-
import Validator.Derive.Leave
17+
import Validator.Regex.Enter
18+
import Validator.Regex.LeaveSmart
1919

2020
import Validator.Validator.ValidateM
2121
import Validator.Validator.Inst.TreeParserM
2222

2323
namespace Parser
2424

25-
def deriveEnter [DecidableEq φ] [ValidateM m n φ α]
25+
def deriveEnter [DecidableEq φ] [ValidateM m (Symbol n φ) α]
2626
(G: Grammar n φ) (Φ: φ -> α -> Bool)
2727
(xs: Rules n φ l): m (Rules n φ (Symbol.nums xs)) := do
2828
let enters <- Enter.DeriveEnter.deriveEnter xs
2929
let token <- Parser.token
3030
return IfExpr.evals G Φ enters token
3131

32-
def deriveLeaveM [DecidableEq φ] [ValidateM m n φ α] (xs: Rules n φ l) (cs: Rules n φ (Symbol.nums xs)): m (Rules n φ l) :=
33-
Leave.DeriveLeaveM.deriveLeaveM xs (Vec.map cs Rule.null)
32+
def deriveLeaveM [DecidableEq φ] [ValidateM m (Symbol n φ) α] (xs: Rules n φ l) (cs: Rules n φ (Symbol.nums xs)): m (Rules n φ l) :=
33+
LeaveSmart.DeriveLeaveM.deriveLeaveM xs (Vec.map cs Rule.null)
3434

35-
def deriveValue [DecidableEq φ] [ValidateM m n φ α]
35+
def deriveValue [DecidableEq φ] [ValidateM m (Symbol n φ) α]
3636
(G: Grammar n φ) (Φ: φ -> α -> Bool)
3737
(xs: Rules n φ l): m (Rules n φ l) := do
3838
deriveEnter G Φ xs >>= deriveLeaveM (α := α) xs
3939

4040
-- TODO: Is it possible to have a Parser type that can be proved to be of the correct shape, and have not expection throwing
4141
-- for example: can you prove that your Parser will never return an Hint.leave after returning a Hint.field.
4242
-- This class can be called the LawfulParser.
43-
partial def derive [DecidableEq φ] [ValidateM m n φ α]
43+
partial def derive [DecidableEq φ] [ValidateM m (Symbol n φ) α]
4444
(G: Grammar n φ) (Φ: φ -> α -> Bool)
4545
(xs: Rules n φ l): m (Rules n φ l) := do
4646
if List.all xs.toList Regex.unescapable then
@@ -60,7 +60,7 @@ partial def derive [DecidableEq φ] [ValidateM m n φ α]
6060
| Hint.leave => return xs -- never happens at the top
6161
| Hint.eof => return xs -- only happens at the top
6262

63-
def validate {m} [DecidableEq φ] [ValidateM m n φ α]
63+
def validate {m} [DecidableEq φ] [ValidateM m (Symbol n φ) α]
6464
(G: Grammar n φ) (Φ: φ -> α -> Bool)
6565
(x: Rule n φ): m Bool := do
6666
let dxs <- derive G Φ (Vec.cons x Vec.nil)

Validator/Memoize/MemEnter.lean

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import Validator.Hedge.IfExpr
77
import Validator.Regex.Regex
88
import Validator.Regex.Symbol
99

10-
import Validator.Derive.Enter
10+
import Validator.Regex.Enter
1111

1212
abbrev MemEnter.EnterMap (n: Nat) φ [DecidableEq φ] [Hashable φ] :=
1313
Std.ExtDHashMap
@@ -62,5 +62,5 @@ def deriveEnter
6262
Debug.debug "cache hit"
6363
return value
6464

65-
instance [DecidableEq φ] [Hashable φ] [Monad m] [Debug m] [MemEnter m n φ] : Enter.DeriveEnter m n φ where
65+
instance [DecidableEq φ] [Hashable φ] [Monad m] [Debug m] [MemEnter m n φ] : Enter.DeriveEnter m (φ × Ref n) where
6666
deriveEnter {l: Nat} (xs: Rules n φ l): m (IfExprs n φ (Symbol.nums xs)) := deriveEnter xs

0 commit comments

Comments
 (0)