如何使用 Data.SBV 帮助推导出正确的堆栈机实现?



Graham Hutton在《Programming in Haskell》的第2版中,用了最后两章来讨论基于堆栈机的AST实现。 最后,他展示了如何从AST的语义模型中推导出该机器的正确实现。

我试图在这种推导中寻求Data.SBV的帮助,但失败了。

我希望有人能帮助我了解我是否:

  1. 要求Data.SBV做不到的事情,或者
  2. Data.SBV询问它可以做的事情,但要求错误。
-- test/sbv-stack.lhs - Data.SBV assisted stack machine implementation derivation.
{-# LANGUAGE OverloadedLists #-}
{-# LANGUAGE ScopedTypeVariables #-}
import Data.SBV
import qualified Data.SBV.List as L
import           Data.SBV.List ((.:), (.++))  -- Since they don't collide w/ any existing list functions.
-- AST Definition
data Exp = Val SWord8
| Sum Exp Exp
-- Our "Meaning" Function
eval :: Exp -> SWord8
eval (Val x)   = x
eval (Sum x y) = eval x + eval y
type Stack  = SList Word8
-- Our "Operational" Definition.
--
-- This function attempts to implement the *specification* provided by our
-- "meaning" function, above, in a way that is more conducive to
-- implementation in our available (and, perhaps, quite primitive)
-- computational machinery.
--
-- Note that we've (temporarily) assumed that this machinery will consist
-- of some form of *stack-based computation engine* (because we're
-- following Hutton's example).
--
-- Note that we give the *specification* of the function in the first
-- (commented out) line of the definition. The derivation of the actual
-- correct definition from this specification is detailed in Ch. 17 of
-- Hutton's book.
eval' :: Exp -> Stack -> Stack
-- eval' e s = eval e : s         -- our "specification"
eval' (Val n) s = push n s        -- We're defining this one manually.
where
push :: SWord8 -> Stack -> Stack
push n s = n .: s
eval' (Sum x y) s = add (eval' y (eval' x s))
where
add :: Stack -> Stack
add = uninterpret "add" s       -- This is the function we're asking to be derived.
-- Now, let's just ask SBV to "solve" our specification of `eval'`:
spec :: Goal
spec = do x :: SWord8 <- forall "x"
y :: SWord8 <- forall "y"
-- Our spec., from above, specialized to the `Sum` case:
constrain $ eval' (Sum (Val x) (Val y)) L.nil .== eval (Sum (Val x) (Val y)) .: L.nil

我们得到:

λ> :l test/sbv-stack.lhs
[1 of 1] Compiling Main             ( test/sbv-stack.lhs, interpreted )
Ok, one module loaded.
Collecting type info for 1 module(s) ... 
λ> sat spec
Unknown.
Reason: smt tactic failed to show goal to be sat/unsat (incomplete quantifiers)

发生了什么事?!
好吧,也许,要求 SBV 解决谓词(即 -a -> Bool(以外的任何东西都不起作用?

这里的根本问题是你混合了 SMTLib 的序列逻辑和量词。事实证明,这个问题对于 SMT 求解器来说太难处理了。如果你把自己限制在基本的逻辑上,这种函数的综合确实是可能的。(位向量、整数、实数。但是将序列添加到混音中会将其放入不可判定的片段中。

这并不意味着 z3 不能合成你的add函数。也许未来的版本可能能够处理它。但在这一点上,你受到启发式的摆布。要了解原因,请注意,您要求求解器合成以下定义:

add :: Stack -> Stack
add s = v .: s''
where (a, s')  = L.uncons s
(b, s'') = L.uncons s'
v        = a + b

虽然这看起来相当无辜和简单,但它需要超出Z3当前能力的能力。一般来说,z3 目前可以合成只对具体元素做出有限数量选择的函数。但是,如果输出依赖于每个输入选择的输入,则无法做到这一点。(把它想象成一个案例分析生成引擎:它可以变出一个函数,将某些输入映射到其他输入,但无法确定是否应该增加某些内容或必须添加两件事。这源于有限模型查找理论的工作,远远超出了这个答案的范围!详情请看这里:https://arxiv.org/abs/1706.00096(

SBV和SMT解决这类问题的更好用例是实际告诉它add函数是什么,然后证明一些给定的程序是使用Hutton的策略正确"编译"的。请注意,我明确地说一个"给定"程序:对于任意程序进行建模和证明也非常困难,但是对于给定的固定程序,您可以相当轻松地做到这一点。如果你有兴趣证明任意程序的对应关系,你真的应该看看定理证明器,如Isabelle,Coq,ACL2等;它可以处理归纳,毫无疑问,这种问题需要一种证明技术。请注意,SMT 求解器通常不能执行归纳。(您可以使用电子匹配来模拟一些类似归纳的证明,但这充其量是一个笨拙,通常无法维护。

下面是您的示例,编码以证明x -> y -> x + y程序在引用语义方面"正确"编译和执行:

{-# LANGUAGE ScopedTypeVariables #-}
import Data.SBV
import qualified Data.SBV.List as L
import           Data.SBV.List ((.:))
-- AST Definition
data Exp = Val SWord8
| Sum Exp Exp
-- Our "Meaning" Function
eval :: Exp -> SWord8
eval (Val x)   = x
eval (Sum x y) = eval x + eval y
-- Evaluation by "execution"
type Stack  = SList Word8
run :: Exp -> SWord8
run e = L.head (eval' e L.nil)
where eval' :: Exp -> Stack -> Stack
eval' (Val n)   s = n .: s
eval' (Sum x y) s = add (eval' y (eval' x s))
add :: Stack -> Stack
add s = v .: s''
where (a, s')  = L.uncons s
(b, s'') = L.uncons s'
v        = a + b
correct :: IO ThmResult
correct = prove $ do x :: SWord8 <- forall "x"
y :: SWord8 <- forall "y"

let pgm = Sum (Val x) (Val y)
spec    = eval pgm
machine = run  pgm
return $ spec .== machine

当我运行这个时,我得到:

*Main> correct
Q.E.D.

而且证明几乎不需要时间。您可以通过添加其他运算符、绑定表单、函数调用来轻松扩展它,如果您愿意,整个工作。只要你坚持使用固定的"程序"进行验证,它应该可以正常工作。

如果你犯了一个错误,假设通过减法来定义add(修改它的最后一行以准备好v = a - b(,你会得到:

*Main> correct
Falsifiable. Counter-example:
x = 32 :: Word8
y =  0 :: Word8

我希望这能让我们了解 SMT 求解器的当前功能是什么,以及如何通过 SBV 将它们用于 Haskell。

程序合成是一个活跃的研究领域,具有许多自定义技术和工具。开箱即用的 SMT 求解器不会让您实现目标。但是,如果您确实在 Haskell 中构建了这样的自定义系统,则可以使用 SBV 访问底层 SMT 求解器,以解决在此过程中必须处理的许多约束。

(旁白:SBV 软件包附带了一个扩展示例,其精神相似但目标不同:https://hackage.haskell.org/package/sbv-8.5/docs/Documentation-SBV-Examples-Strings-SQLInjection.html。该程序展示了如何使用 SBV 和 SMT 求解器在理想化的 SQL 实现中查找 SQL 注入漏洞。这可能在这里引起一些兴趣,并且与 SMT 求解器在实践中的典型使用方式更一致。

最新更新