gpt4 book ai didi

haskell - 自动导出 SBV 中记录谓词的可证明

转载 作者:行者123 更新时间:2023-12-02 04:04:53 24 4
gpt4 key购买 nike

我的数据类型如下

data X = X {foo :: SInteger, bar :: SInteger}

我想证明例如

forAll_ $ \x -> foo x + bar x .== bar x + foo x

使用 haskell 的 sbv 。这不会编译,因为 X -> SBool 不是 Provable 的实例。我可以将其作为一个实例,例如

instance (Provable p) => Provable (X -> p) where
forAll_ k = forAll_ $ \foo bar -> forAll_ $ k $ X foo bar
forAll (s : ss) k =
forAll ["foo " ++ s, "bar " ++ s] $ \foo bar -> forAll ss $ k $ X foo bar
forAll [] k = forAll_ k
-- and similarly `forSome_` and `forSome`

但这很乏味且容易出错(例如,当应该使用 forAll 时却使用 forSome)。有没有办法为我的类型自动派生 Provable

最佳答案

至少可以减少出错的可能性:

onX :: (((SInteger, SInteger) -> a) -> b) -> ((X -> a) -> b)
onX f g = f (g . uncurry X)

instance Provable p => Provable (X -> p) where
forAll_ = onX forAll_
forSome_ = onX forSome_
forAll = onX . forAll
forSome = onX . forSome

还有一个可推广的模式,以防 SBV 的现有实例最多 7 元组不够。

data Y = Y {a, b, c, d, e, f, g, h, i, j :: SInteger}
-- don't try to write the types of these, you will wear out your keyboard
fmap10 = fmap . fmap . fmap . fmap . fmap . fmap . fmap . fmap . fmap . fmap
onY f g = f (fmap10 g Y)

instance Provable p => Provable (Y -> p) where
forAll_ = onY forAll_
forSome_ = onY forSome_
forAll = onY . forAll
forSome = onY . forSome

不过还是很乏味。

关于haskell - 自动导出 SBV 中记录谓词的可证明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39976211/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com