From d7fe6a8dda6860241ac11662ddf7a60f836b0e89 Mon Sep 17 00:00:00 2001 From: breandan Date: Mon, 16 Oct 2023 11:23:19 -0400 Subject: [PATCH] implement more efficient pruner (TODO) --- .../ai/hypergraph/kaliningraph/parsing/BarHillel.kt | 2 +- .../ai/hypergraph/kaliningraph/parsing/Normalization.kt | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/commonMain/kotlin/ai/hypergraph/kaliningraph/parsing/BarHillel.kt b/src/commonMain/kotlin/ai/hypergraph/kaliningraph/parsing/BarHillel.kt index fb0a0310..c0003c86 100644 --- a/src/commonMain/kotlin/ai/hypergraph/kaliningraph/parsing/BarHillel.kt +++ b/src/commonMain/kotlin/ai/hypergraph/kaliningraph/parsing/BarHillel.kt @@ -112,7 +112,7 @@ fun CFG.dropVestigialProductions( // val reachable = reachableSymbols() val rw = toMutableSet() .apply { removeAll { prod -> prod.RHS.any { criteria(it) && it !in nts } } } - .freeze().removeUselessSymbols() + .freeze().removeUselessSymbols()//.removeUnreachable().freeze().removeNonGenerating() println("Removed ${size - rw.size} vestigial productions.") diff --git a/src/commonMain/kotlin/ai/hypergraph/kaliningraph/parsing/Normalization.kt b/src/commonMain/kotlin/ai/hypergraph/kaliningraph/parsing/Normalization.kt index 23247856..2412b554 100644 --- a/src/commonMain/kotlin/ai/hypergraph/kaliningraph/parsing/Normalization.kt +++ b/src/commonMain/kotlin/ai/hypergraph/kaliningraph/parsing/Normalization.kt @@ -161,6 +161,7 @@ fun CFG.refactorEpsilonProds(nlbls: Set<Σᐩ> = nullableNonterminals()): CFG = * A useful symbol is both generating and reachable. */ +// TODO: https://zerobone.net/blog/cs/non-productive-cfg-rules/ fun CFG.removeUselessSymbols( generating: Set<Σᐩ> = genSym(), reachable: Set<Σᐩ> = reachSym() @@ -180,6 +181,13 @@ fun CFG.removeUselessSymbols( // } // .first.toSet() +//fun CFG.removeNonGenerating(generating: Set<Σᐩ> = genSym()) = +// toMutableSet().apply { removeAll { (s, _) -> s !in generating } } +// +//fun CFG.removeUnreachable() = +// toMutableSet().apply { removeAll { (s, _) -> !reachableNTs[bindex[s]] } } +// .also { println("Removed ${size - it.size} unreachable productions.") } + fun CFG.equivalenceClass(from: Σᐩ): Set<Σᐩ> = unitReachability[from] ?: setOf(from) fun LabeledGraph.transitiveClosure(from: Set<Σᐩ>) =