Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made debugVar more flexible #11

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions klogic-core/src/main/kotlin/org/klogic/core/Goal.kt
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ fun delay(f: () -> Goal): Goal = { st: State -> ThunkStream { f()(st) } }
*/
fun <T : Term<T>> debugVar(
term: Term<T>,
reifier: (Term<T>) -> ReifiedTerm<T>,
callBack: (ReifiedTerm<T>) -> Goal
reifier: (Term<T>, Set<Constraint<*>>) -> ReifiedTerm<*> = ::ReifiedTerm,
callBack: (ReifiedTerm<*>) -> Goal
): Goal = { st: State ->
val walkedTerm = term.walk(st.substitution)
val reified = reifier(walkedTerm)
val reified = reifier(walkedTerm, st.constraints)

callBack(reified)(st)
}
Expand Down
58 changes: 58 additions & 0 deletions klogic-core/src/test/kotlin/org/klogic/core/ReifyTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package org.klogic.core

import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.Assertions.assertEquals
import org.klogic.core.Var.Companion.createTypedVar
import org.klogic.utils.terms.PeanoLogicNumber
import org.klogic.utils.terms.PeanoLogicNumber.Companion.succ
import org.klogic.utils.terms.ZeroNaturalNumber.Z
import org.klogic.utils.terms.toPeanoLogicNumber

class ReifyTest {
private val debugValues: MutableMap<String, MutableList<ReifiedTerm<*>>> = mutableMapOf()

@AfterEach
fun clearDebug() {
debugValues.clear()
}

@Test
fun testDebugVar() {
val left = (-1).createTypedVar<PeanoLogicNumber>()
val right = 10.toPeanoLogicNumber()
val result = 13.toPeanoLogicNumber()

val run = run(10, left, plus(left, right, result))

assertEquals(3.toPeanoLogicNumber(), run.single().term)

debugValues["x"]!!.zip(debugValues["z"]!!).forEach {
println("x: ${it.first.term}, z: ${it.second.term}")
}

val expectedDebugValues = (13 downTo 1).map { it.toPeanoLogicNumber() }
assertEquals(expectedDebugValues, debugValues["z"]!!.map { it.term })
}

private fun plus(x: Term<PeanoLogicNumber>, y: Term<PeanoLogicNumber>, z: Term<PeanoLogicNumber>): Goal = conde(
(x `===` Z) and (z `===` y),
freshTypedVars<PeanoLogicNumber, PeanoLogicNumber> { a, b ->
(x `===` succ(a)) and (z `===` succ(b)) and debugVar(
x,
callBack = { reifiedX ->
debugValues.getOrPut("x") { mutableListOf() } += reifiedX

debugVar(
z,
callBack = { reifiedZ ->
debugValues.getOrPut("z") { mutableListOf() } += reifiedZ

plus(a, y, b)
}
)
}
)
}
)
}