Skip to content

Commit

Permalink
try to understand why lazy extraction takes so much longer than eager
Browse files Browse the repository at this point in the history
  • Loading branch information
breandan committed Oct 2, 2023
1 parent 5207082 commit 8b14737
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ fun PSingleton(v: String): List<Π2A<PTree>> = listOf(PTree(v) to PTree())

// Algebraic data type / polynomial functor for parse forests
class PTree(val root: String = "ε", val branches: List<Π2A<PTree>> = listOf()) {
val repr = sequenceOf(if ("ε" in root) "" else root)
val branchSeq = branches.asSequence()
// Returns the set of all strings derivable from the given PTree
fun choose(): Sequence<String> =
if (branches.isEmpty()) sequenceOf(if("ε" in root) "" else root)
else branches.asSequence().flatMap { (l, r) ->
if (branches.isEmpty()) repr
else branchSeq.flatMap { (l, r) ->
// TODO: Use weighted choice mechanism
(l.choose() * r.choose()).map { (a, b) ->
if (a == "") b else if (b == "") a else "$a $b"
if (a.isEmpty()) b else if (b.isEmpty()) a else "$a $b"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,7 @@ Yield_Arg -> From_Keyword Test | Testlist_Endcomma
START -> [1,START,4]
START -> [3,START,4]
[1,+,3] -> +
[1,+,3] -> *
[1,L,1] -> [1,O,1] [1,N,1]
[1,L,1] -> [1,O,3] [3,N,1]
[1,L,1] -> [1,O,4] [4,N,1]
Expand Down Expand Up @@ -793,6 +794,7 @@ Yield_Arg -> From_Keyword Test | Testlist_Endcomma
[3,START,4] -> [3,N,4] [4,L,4]
[3,b,4] -> b
[4,+,1] -> +
[4,+,1] -> *
[4,L,1] -> [4,O,1] [1,N,1]
[4,L,1] -> [4,O,3] [3,N,1]
[4,L,1] -> [4,O,4] [4,N,1]
Expand Down Expand Up @@ -841,7 +843,7 @@ Yield_Arg -> From_Keyword Test | Testlist_Endcomma
val cfg = """
START -> N L
N -> N N | a | b
O -> x
O -> *
O -> +
L -> O N
""".parseCFG()
Expand All @@ -858,7 +860,8 @@ Yield_Arg -> From_Keyword Test | Testlist_Endcomma
val fsaCfg = """
START -> START b | 3 b
3 -> 1 +
1 -> a | 1 a | START +
3 -> 1 *
1 -> a | 1 a | START + | START *
""".parseCFG()

println("Grammar size: ${bhcfg.size}")
Expand All @@ -874,11 +877,13 @@ Yield_Arg -> From_Keyword Test | Testlist_Endcomma
// .toList().also { println("Found ${it.size} solutions.") }
// }.also { println("Brute force solver took: ${it.inWholeMilliseconds}ms") }

val clock = TimeSource.Monotonic.markNow()

measureTime {
// bhcfg.solveSeq(template)
bhcfg.solve(template) { it.weight }
.onEach {
println(it)
println("${clock.elapsedNow().inWholeMilliseconds}ms: " + it)
assertTrue { it in bhcfg.language }
assertTrue { it in fsaCfg.language }
assertTrue { it in cfg.language }
Expand Down

0 comments on commit 8b14737

Please sign in to comment.