A common supertype for expressions of type KExpr<T : KSort>
#40
sergeypospelov
started this conversation in
Ideas
Replies: 1 comment
-
My suggestion is to define an extension function that performs a "checked" cast. fun <T : KSort> KExpr<*>.to(sort: T): KExpr<T> = with(ctx) {
check(this@to.sort == sort) { "Sort mismatch" }
@Suppress("UNCHECKED_CAST")
this@to as KExpr<T>
} Usage example: fun sample(expr: KExpr<*>) = with(ctx) {
expr.to(mkBv32Sort())
expr.to(boolSort)
expr.to(mkArraySort(intSort, intSort))
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Problem
Suppose we have such a map:
And we would like to take a local
i0
which is known for sure to be an integer, so we expect aKExpr<KBv32Sort>
.We would write something like this:
The problem here is that the generic part of the cast is unchecked, so if there isn't
KExpr<KBv32Sort>
butKExpr<KFpSort>
, a runtime exception won't be thrown at this line.Actually, a
ClassCastException
will be thrown in the first place, where we useintLocal
asKExpr<KBv32Sort>
, e.g.mkBvAdd(i0, ...)
, so it will be quite unexpected.The same reasonings apply to any types of sorts.
Suggestion
I wonder, if it's possible to introduce a common supertype for all operations which produce
KExpr<KBv32Sort>
, e.g.KBv32Expr
or something like this. As the result, we can write the following:The actual cast happens exactly at this line, so they won't be any unexpected exceptions.
Beta Was this translation helpful? Give feedback.
All reactions