Skip to content

Commit

Permalink
okay, bar-hillel works (just need to prune dangling NTs)
Browse files Browse the repository at this point in the history
  • Loading branch information
breandan committed Oct 2, 2023
1 parent a59c82e commit 0ce0fb8
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 611 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ import ai.hypergraph.kaliningraph.types.*

val rewriteHistory = LRUCache<CFG, List<CFG>>()

// Recursively rmoves all productions containing a dangling nonterminal, i.e.,
// a nonterminal from a synthetic CFG that does not produce any terminals.
fun CFG.removeDanglingNonterminals(
matches: (Σᐩ) -> Boolean = { it.first() == '[' && it.last() == ']' && it.count { it == ',' } == 2 }
): CFG {
val rw =
filter { it.RHS.all { !matches(it) || it in nonterminals } }
.toSet()

return if (rw.size == size) this else rw.removeDanglingNonterminals(matches)
}

/**
* n.b. Normalization may destroy organic nonterminals!
* In order to preserve every organic nonterminal, you
Expand All @@ -32,6 +44,7 @@ fun CFG.normalize(): CFG =
// Must remember to run the unit test if order changes in the future
// ./gradlew jvmTest --tests "ai.hypergraph.kaliningraph.sat.SATValiantTest.testTLArithmetic"
.generateNonterminalStubs()
.removeDanglingNonterminals()
.also { cnf -> rewriteHistory.put(cnf.freeze(), rewrites) }
}

Expand Down
Loading

0 comments on commit 0ce0fb8

Please sign in to comment.