commonMain.it.unibo.tuprolog.solve.SolutionIterator.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of solve-classic-metadata Show documentation
Show all versions of solve-classic-metadata Show documentation
Stable, state-machine-based implementation of Prolog's SLDNF resolution principle
package it.unibo.tuprolog.solve
import it.unibo.tuprolog.core.Substitution
import it.unibo.tuprolog.core.Var
import it.unibo.tuprolog.solve.fsm.EndState
import it.unibo.tuprolog.solve.fsm.State
internal class SolutionIterator(
state: State,
private val onStateTransition: (State, Long) -> Unit = { _, _ -> Unit }
) : Iterator {
var state: State = state
private set
var step: Long = 0
private set
override fun hasNext(): Boolean {
return state.let { it !is EndState || it.hasOpenAlternatives }
}
override fun next(): Solution {
do {
state = state.next()
step += 1
onStateTransition(state, step)
} while (state !is EndState)
return (state as EndState).solution.cleanUp()
}
private fun Solution.cleanUp(): Solution =
when (this) {
is Solution.Yes -> cleanUp()
else -> this
}
private fun Solution.Yes.cleanUp(): Solution.Yes {
return copy(substitution = substitution.cleanUp(query.variables.toSet()))
}
private fun Substitution.Unifier.cleanUp(toRetain: Set): Substitution.Unifier {
return filter { _, term -> (term !is Var) || (term in toRetain) }
}
}