diff --git a/src/commonMain/kotlin/ai/hypergraph/kaliningraph/parsing/SeqValiant.kt b/src/commonMain/kotlin/ai/hypergraph/kaliningraph/parsing/SeqValiant.kt index e8e950a5..9b4005c6 100644 --- a/src/commonMain/kotlin/ai/hypergraph/kaliningraph/parsing/SeqValiant.kt +++ b/src/commonMain/kotlin/ai/hypergraph/kaliningraph/parsing/SeqValiant.kt @@ -7,13 +7,15 @@ fun PSingleton(v: String): List<Π2A> = listOf(PTree(v) to PTree()) // Algebraic data type / polynomial functor for parse forests class PTree(val root: String = "ε", val branches: List<Π2A> = 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 = - 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" } } } diff --git a/src/commonTest/kotlin/ai/hypergraph/kaliningraph/parsing/SetValiantTest.kt b/src/commonTest/kotlin/ai/hypergraph/kaliningraph/parsing/SetValiantTest.kt index c4903986..fd2f4054 100644 --- a/src/commonTest/kotlin/ai/hypergraph/kaliningraph/parsing/SetValiantTest.kt +++ b/src/commonTest/kotlin/ai/hypergraph/kaliningraph/parsing/SetValiantTest.kt @@ -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] @@ -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] @@ -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() @@ -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}") @@ -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 }