在Z3中,可以在一个公式中动态重新定义变量的类型吗?

huangapple go评论53阅读模式
英文:

Can the type of a variable in Z3 be dynamically redefined in a formula?

问题

以下是您要翻译的内容:

例如,考虑SymPy的新假设中的公式:

(x>2) & (y>0) & (Q.integer(y) | (y>10))

表达式 (Q.integer(y) | (y>10)) 表示 y 是整数或 y 大于 10(或两者皆是)。如果它不是整数,则它将是实数。有没有办法将这个转换成 SMT-LIB 格式?还是你只需考虑 Q.integer(y) 为真和 Q.integer(y) 为假的情况?

我尝试在网上查找这方面的信息,但找不到提供我所要求的方法。我怀疑这可能是不可能的,一旦声明了变量的类型,就无法更改。下面是我尝试过的显然不起作用的内容:

(declare-const y Real)
(assert (or (> y 10) (declare-const y Int)))
英文:

For example, consider the formula from SymPy's new assumptions:

(x>2) & (y>0) & (Q.integer(y) | (y>10))

The expressions (Q.integer(y) | (y>10)) means that y is an integer or y is greater than 10 (or both). If it's not an integer then it would be a real number. Is there a way to translate this into SMT-LIB format? Or would you simply have to consider the case where Q.integer(y) is true and the case where Q.integer(y) is false?

I tried researching this information online but I couldn't find anything that would provide a way to to do what I asked. I suspect that this isn't possible and after declaring the type of a variable it can not be changed. Something I tried that obviously doesn't work:

(declare-const y Real)
(assert (or (> y 10) (declare-const y Int)))

答案1

得分: 0

SMTLib是一种静态类型语言,意味着每个变量都有一个给定的类型(例如,Real/Integer等),并且该类型是固定的。也就是说,变量不能在运行时动态更改其类型。

我怀疑你实际上想要表达的有点不同。你想要在某些条件下使y不含分数。以下是在SMTLib中如何表达这一点:

(declare-const y Real)

(assert (or (> y 10)
            (exists ((x Int)) (= y (to_real x)))))

上述代码表示要么y > 10,要么存在一个整数,当转换为实数时等于y。在这种情况下,你可以有:

(assert (= y 2.3))
(check-sat)

这将产生:

unsat

或者,你可以有:

(assert (= y 3))
(check-sat)

这将返回sat

不过,我应该补充一点,当你像这样混合使用整数和实数时,当约束变得足够复杂时,求解器可能会返回unknown。换句话说,混合整数和实数算术没有决策过程,否则你可以解决丢番图方程,而这是一个不可判定的问题。

英文:

SMTLib is a statically typed-language, meaning every-variable has a given sort (i.e., Real/Integer/etc.), and that type is fixed. That is, variables cannot change their type "dynamically."

I suspect what you're trying to say is a little different actually. You want y to not have a fraction under certain conditions. This is how you write that in SMTLib:

(declare-const y Real)

(assert (or (> y 10)
            (exists ((x Int)) (= y (to_real x)))))

The above says either y > 10, or there is an integer that when converted equals y. Given this, you can have:

(assert (= y 2.3))
(check-sat)

which produces:

unsat

OR, you can have:

(assert (= y 3))
(check-sat)

which will be sat.

Having said that, I should add when you mix integers and reals like this, you'll find yourself in the semi-decidable fragment of the combined logic: That is, the solver is likely to say unknown when the constraints get sufficiently complicated. (Another way to put this: There's no decision procedure for mixed integer-real arithmetic, as otherwise you could solve Diophantine equations, which is an undecidable problem.)

huangapple
  • 本文由 发表于 2023年6月12日 02:03:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76451818.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定