@@ -9,25 +9,18 @@ import io.ksmt.decl.KDecl
99import io.ksmt.decl.KFuncDecl
1010import io.ksmt.expr.KExpr
1111import io.ksmt.expr.KUninterpretedSortValue
12+ import io.ksmt.solver.KModel
1213import io.ksmt.solver.model.KFuncInterp
1314import io.ksmt.solver.model.KFuncInterpEntryVarsFree
1415import io.ksmt.solver.model.KFuncInterpVarsFree
15- import io.ksmt.solver.KModel
1616import io.ksmt.solver.model.KModelEvaluator
1717import io.ksmt.solver.model.KModelImpl
18- import io.ksmt.sort.KArray2Sort
19- import io.ksmt.sort.KArray3Sort
20- import io.ksmt.sort.KArrayNSort
21- import io.ksmt.sort.KArraySort
2218import io.ksmt.sort.KArraySortBase
2319import io.ksmt.sort.KBoolSort
2420import io.ksmt.sort.KBvSort
25- import io.ksmt.sort.KFpRoundingModeSort
26- import io.ksmt.sort.KFpSort
2721import io.ksmt.sort.KIntSort
2822import io.ksmt.sort.KRealSort
2923import io.ksmt.sort.KSort
30- import io.ksmt.sort.KSortVisitor
3124import io.ksmt.sort.KUninterpretedSort
3225import io.ksmt.utils.uncheckedCast
3326
@@ -46,35 +39,37 @@ class KYicesModel(
4639 model.collectDefinedTerms().mapTo(hashSetOf()) { converter.convertDecl(it) }
4740 }
4841
49- override val uninterpretedSorts: Set <KUninterpretedSort > by lazy {
50- uninterpretedSortDependencies.keys
51- }
42+ private val uninterpretedSortUniverse: Map <KUninterpretedSort , Set <KUninterpretedSortValue >> by lazy {
43+ val values = model.uninterpretedSortValues()
5244
53- private val uninterpretedSortDependencies: Map <KUninterpretedSort , Set <KDecl <* >>> by lazy {
54- val sortsWithDependencies = hashMapOf<KUninterpretedSort , MutableSet <KDecl <* >>>()
55- val sortCollector = UninterpretedSortCollector (sortsWithDependencies)
56- declarations.forEach { sortCollector.collect(it) }
57- sortsWithDependencies
58- }
45+ val sortValues = hashMapOf<YicesSort , MutableSet <Int >>()
46+ for (value in values) {
47+ val (valueId, internalizedSort) = model.scalarValue(value)
48+ val valueIdx = yicesCtx.convertUninterpretedSortValueIndex(valueId)
49+ sortValues.getOrPut(internalizedSort, ::hashSetOf).add(valueIdx)
50+ }
51+
52+ val result = hashMapOf<KUninterpretedSort , Set <KUninterpretedSortValue >>()
53+ for ((internalizedSort, valueIndices) in sortValues) {
54+ val sort = converter.convertSort(internalizedSort)
55+ check(sort is KUninterpretedSort ) { " Unexpected sort with uninterpreted value: $sort " }
56+ val sortUniverse = valueIndices.mapTo(hashSetOf()) {
57+ ctx.mkUninterpretedSortValue(sort, it)
58+ }
59+ result[sort] = sortUniverse
60+ }
5961
60- private val uninterpretedSortUniverse =
61- hashMapOf< KUninterpretedSort , Set < KUninterpretedSortValue >>()
62+ result
63+ }
6264
63- private val knownUninterpretedSortValues =
64- hashMapOf< KUninterpretedSort , MutableMap < Int , KUninterpretedSortValue >>()
65+ override val uninterpretedSorts : Set < KUninterpretedSort >
66+ get() = uninterpretedSortUniverse.keys
6567
6668 private val interpretations = hashMapOf<KDecl <* >, KFuncInterp <* >>()
6769 private val funcInterpretationsToDo = arrayListOf<Pair <YVal , KFuncDecl <* >>>()
6870
69- override fun uninterpretedSortUniverse (
70- sort : KUninterpretedSort
71- ): Set <KUninterpretedSortValue >? = uninterpretedSortUniverse.getOrPut(sort) {
72- val sortDependencies = uninterpretedSortDependencies[sort] ? : return null
73-
74- sortDependencies.forEach { interpretation(it) }
75-
76- knownUninterpretedSortValues[sort]?.values?.toHashSet() ? : hashSetOf()
77- }
71+ override fun uninterpretedSortUniverse (sort : KUninterpretedSort ): Set <KUninterpretedSortValue >? =
72+ uninterpretedSortUniverse[sort]
7873
7974 private val evaluatorWithModelCompletion by lazy { KModelEvaluator (ctx, this , isComplete = true ) }
8075 private val evaluatorWithoutModelCompletion by lazy { KModelEvaluator (ctx, this , isComplete = false ) }
@@ -94,11 +89,8 @@ class KYicesModel(
9489 is KIntSort -> mkIntNum(model.bigRationalValue(yval))
9590 is KUninterpretedSort -> {
9691 val uninterpretedSortValueId = model.scalarValue(yval)[0 ]
97- val sortValues = knownUninterpretedSortValues.getOrPut(sort) { hashMapOf() }
98- sortValues.getOrPut(uninterpretedSortValueId) {
99- val valueIndex = yicesCtx.convertUninterpretedSortValueIndex(uninterpretedSortValueId)
100- mkUninterpretedSortValue(sort, valueIndex)
101- }
92+ val valueIndex = yicesCtx.convertUninterpretedSortValueIndex(uninterpretedSortValueId)
93+ mkUninterpretedSortValue(sort, valueIndex)
10294 }
10395 is KArraySortBase <* > -> {
10496 val funcDecl = ctx.mkFreshFuncDecl(" array" , sort.range, sort.domainSorts)
@@ -184,52 +176,4 @@ class KYicesModel(
184176 nativeModel?.close()
185177 nativeModel = null
186178 }
187-
188- private class UninterpretedSortCollector (
189- private val sorts : MutableMap <KUninterpretedSort , MutableSet <KDecl <* >>>
190- ) : KSortVisitor<Unit> {
191- private lateinit var currentDecl: KDecl <* >
192-
193- fun collect (decl : KDecl <* >) {
194- currentDecl = decl
195-
196- decl.sort.accept(this )
197- decl.argSorts.forEach { it.accept(this ) }
198- }
199-
200- override fun <D : KSort , R : KSort > visit (sort : KArraySort <D , R >) {
201- sort.range.accept(this )
202- sort.domain.accept(this )
203- }
204-
205- override fun <D0 : KSort , D1 : KSort , R : KSort > visit (sort : KArray2Sort <D0 , D1 , R >) {
206- sort.range.accept(this )
207- sort.domain0.accept(this )
208- sort.domain1.accept(this )
209- }
210-
211- override fun <D0 : KSort , D1 : KSort , D2 : KSort , R : KSort > visit (sort : KArray3Sort <D0 , D1 , D2 , R >) {
212- sort.range.accept(this )
213- sort.domain0.accept(this )
214- sort.domain1.accept(this )
215- sort.domain2.accept(this )
216- }
217-
218- override fun <R : KSort > visit (sort : KArrayNSort <R >) {
219- sort.range.accept(this )
220- sort.domainSorts.forEach { it.accept(this ) }
221- }
222-
223- override fun visit (sort : KUninterpretedSort ) {
224- val sortDependencies = sorts.getOrPut(sort) { hashSetOf() }
225- sortDependencies.add(currentDecl)
226- }
227-
228- override fun visit (sort : KBoolSort ) = Unit
229- override fun visit (sort : KIntSort ) = Unit
230- override fun visit (sort : KRealSort ) = Unit
231- override fun <S : KBvSort > visit (sort : S ) = Unit
232- override fun <S : KFpSort > visit (sort : S ) = Unit
233- override fun visit (sort : KFpRoundingModeSort ) = Unit
234- }
235179}
0 commit comments