简体   繁体   English

SMTLIB2 / Z3中的多态函数

[英]Polymorphic Functions in SMTLIB2 / Z3

Am I correct in understanding that one cannot create "polymorphic" functions in Z3 or SMTLIB2? 我理解一个人不能在Z3或SMTLIB2中创建“多态”函数的理解是否正确? eg I'd like to write something like: 例如,我想写一些类似的东西:

(declare-fun Prop (A) Bool)
(declare-fun x1   ()  Int)
(declare-fun x2   ()  Bool)
(assert (and (Prop x1) (Prop x2)))

(I guess I can get something like this by declaring a union type for Int + Bool and then making Prop work for the union type, but wanted to double check first that it was not possible to use parametric polymorphism directly?) (我想我可以通过为Int + Bool声明一个联合类型,然后使Prop适用于该联合类型来获得类似的信息,但想首先仔细检查一下是否无法直接使用参数多态性?)

Thanks! 谢谢!

That is indeed correct. 确实是正确的。 SMTLib uses a many-sorted first-order logic; SMTLib使用多种一阶逻辑。 so in your example A can be any sort, but it has to be a known sort; 因此,在您的示例中, A可以是任何种类,但必须是已知种类; not a type-parameter. 不是类型参数。

Having said that, SMTLib does allow uninterpreted sorts; 话虽如此,SMTLib确实允许未解释的排序; that is, you can introduce new sorts with no underlying representation. 也就是说,您可以引入没有基础表示形式的新排序。 (Just like uninterpreted functions.) Then, you can have injection functions to this sort, and sort of simulate what you want. (就像未解释的函数一样。)然后,您可以使用这种注入函数,并模拟所需的函数。 (I know, bad pun.) (我知道,糟糕的双关语。)

Here's an example how you can do that in your favorite language Ranjit: https://gist.github.com/LeventErkok/163362a59060188f5e62 这是一个示例,您可以使用自己喜欢的语言Ranjit来做到这一点: https : //gist.github.com/LeventErkok/163362a59060188f5e62

When run, it generates the following SMT-Lib code, which shows what you'd need to generate yourself, give or take: https://gist.github.com/LeventErkok/920aba57cf8cb1810b4a 运行时,它会生成以下SMT-Lib代码,其中显示了生成或赠送自己所需的内容: https : //gist.github.com/LeventErkok/920aba57cf8cb1810b4a

And here's the Haskell output for that very example: 这是该示例的Haskell输出:

Satisfiable. Model: x1 = 0 :: SInteger x2 = False

Of course, you can get fancy and query the SMT-solver for an interpretation of the uninterpreted-functions used during the construction; 当然,您可以看中并查询SMT求解器以解释在构造过程中使用的未解释函数。 but none of that is natively supported by SMTLib. 但是SMTLib本身不支持这些功能。 Though Z3 will give you a model if you ask it nicely, and if you're willing to parse the somewhat obscure and unfortunately non-standard output. 尽管Z3会给您一个模型,但前提是您会很好地询问它,并且是否愿意解析有些晦涩难懂的非标准输出。 Here it is for this example: https://gist.github.com/LeventErkok/54cee74eb3def22dfb5f 这里是这个例子: https : //gist.github.com/LeventErkok/54cee74eb3def22dfb5f

Also note that you'll in general need to give certain axioms on your injections; 另外请注意,通常需要在注射时提供一定的公理。 like they are one-to-one and disjoint from each other; 就像它们是一对一的并且彼此不相交; which is also possible in SMTLib, though in general these will require quantifiers and thus might cause the solver to respond "unknown" since you go into the semi-decidable territory. 这在SMTLib中也是可能的,尽管通常这些将需要量词,因此自进入半确定区域以来,可能会使求解器做出“未知”响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM