gpt4 book ai didi

haskell - 在 Haskell 中检测冗余约束?

转载 作者:行者123 更新时间:2023-12-03 16:30:59 26 4
gpt4 key购买 nike

有没有办法在 Haskell 中检测冗余约束?
例如:

class    (A a, B a) => C1 a -- C1 ~ A AND B
instance (A a, B a) => C1 a

class (A a, B a, C a) => C2 a
instance (A a, B a, C a) => C2 a

f :: (C1 a, C2 a) => a
f = ...
这里,C2 暗示 C1,并且在 f 的签名中使用 C1是多余的,即同义反复。
在现实世界的元编程情况下,这将是 哇哦更复杂,并且将极大地帮助整理签名头,并帮助我理解和跟踪正在发生的事情。
考虑到 GHC 的形式主义,这在逻辑上是可能的吗?
GHC 中的基础设施是否可用?

最佳答案

如果我把你的 stub 放在一个文件中:

{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE UndecidableInstances #-}
class A a
class B a
class C a

class (A a, B a) => C1 a -- C1 ~ A AND B
instance (A a, B a) => C1 a

class (A a, B a, C a) => C2 a
instance (A a, B a, C a) => C2 a
然后,您可以通过将变量绑定(bind)到 undefined 来了解冗余约束。以及您有兴趣减少 ghci 的类型:
> x = undefined :: (C1 a, C2 a) => a

<interactive>:1:18: warning: [-Wsimplifiable-class-constraints]
• The constraint ‘C1 a’ matches
instance forall a. (A a, B a) => C1 a -- Defined at test.hs:8:10
This makes type inference for inner bindings fragile;
either use MonoLocalBinds, or simplify it using the instance
• In an expression type signature: (C1 a, C2 a) => a
In the expression: undefined :: (C1 a, C2 a) => a
In an equation for ‘x’: x = undefined :: (C1 a, C2 a) => a

<interactive>:1:18: warning: [-Wsimplifiable-class-constraints]
• The constraint ‘C2 a’ matches
instance forall a. (A a, B a, C a) => C2 a
-- Defined at test.hs:11:10
This makes type inference for inner bindings fragile;
either use MonoLocalBinds, or simplify it using the instance
• In an expression type signature: (C1 a, C2 a) => a
In the expression: undefined :: (C1 a, C2 a) => a
In an equation for ‘x’: x = undefined :: (C1 a, C2 a) => a
*Main
> :t x
x :: forall {a}. (A a, B a, C a) => a
在这种情况下,前两个警告实际上只是一个快乐的意外,与您的 stub 有多简单有关;你不会总是得到它们。真正有趣的是第二个查询, :t x ,这减少了您需要了解的关于 a 的基本事实的限制。 .

关于haskell - 在 Haskell 中检测冗余约束?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67091316/

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