尽管已明确注释,Haskell仍无法推断类型(或类型级别的Nat)相等性?

Agn*_*yay 5 haskell type-level-computation

我试图用Haskell实现Braun树,定义如下:

{-# LANGUAGE GADTs #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE ScopedTypeVariables #-}

data BraunTree (n :: Nat) a where
    Empty :: BraunTree 0 a
    Fork :: a -> BraunTree n a -> 
            BraunTree m a ->
            Either (n :~: m) (n :~: (m + 1)) ->
            BraunTree (n + m + 1) a
Run Code Online (Sandbox Code Playgroud)

现在,我试图尝试如何“类型安全”地将内容插入此树。

insert :: a -> BraunTree (n :: Nat) a -> BraunTree (n + 1 :: Nat) a
insert x Empty = Fork x Empty Empty (Left Refl)
insert x (Fork y (t1 :: BraunTree p a) (t2 :: BraunTree q a) (Left (Refl :: p :~: q))) = Fork x (t1' :: BraunTree (p + 1) a) (t2 :: BraunTree q a) (Right (sucCong Refl :: (p + 1) :~: (q + 1)))
    where
        t1' :: BraunTree (p + 1) a
        t1' = insert x t1
Run Code Online (Sandbox Code Playgroud)

与sucCong作为

sucCong :: ((p :: Nat) :~: (q :: Nat)) -> (p + 1 :: Nat) :~: (q + 1 :: Nat)
sucCong Refl = Refl
Run Code Online (Sandbox Code Playgroud)

现在,虽然insert编译的第一个子句很好,但第二行引发了一个令人困惑的错误。

/home/agnishom/test/typeExp/braun.hs:31:90: error:
    • Could not deduce: (((n1 + 1) + n1) + 1) ~ (n + 1)
      from the context: n ~ ((n1 + m) + 1)
        bound by a pattern with constructor:
                   Fork :: forall a (n :: Nat) (m :: Nat).
                           a
                           -> BraunTree n a
                           -> BraunTree m a
                           -> Either (n :~: m) (n :~: (m + 1))
                           -> BraunTree ((n + m) + 1) a,
                 in an equation for ‘insert’
        at /home/agnishom/test/typeExp/braun.hs:31:11-85
      or from: m ~ n1
        bound by a pattern with constructor:
                   Refl :: forall k (a :: k). a :~: a,
                 in an equation for ‘insert’
        at /home/agnishom/test/typeExp/braun.hs:31:69-72
      Expected type: BraunTree (n + 1) a
        Actual type: BraunTree (((n1 + 1) + m) + 1) a
      NB: ‘+’ is a type function, and may not be injective
    • In the expression:
        Fork
          x
          (t1' :: BraunTree (p + 1) a)
          (t2 :: BraunTree q a)
          (Right (sucCong Refl :: (p + 1) :~: (q + 1)))
      In an equation for ‘insert’:
          insert
            x
            (Fork y
                  (t1 :: BraunTree p a)
                  (t2 :: BraunTree q a)
                  (Left (Refl :: p :~: q)))
            = Fork
                x
                (t1' :: BraunTree (p + 1) a)
                (t2 :: BraunTree q a)
                (Right (sucCong Refl :: (p + 1) :~: (q + 1)))
            where
                t1' :: BraunTree (p + 1) a
                t1' = insert x (t1 :: BraunTree p a)
    • Relevant bindings include
        t1' :: BraunTree (n1 + 1) a
          (bound at /home/agnishom/test/typeExp/braun.hs:34:9)
        t1 :: BraunTree n1 a
          (bound at /home/agnishom/test/typeExp/braun.hs:31:19)
        insert :: a -> BraunTree n a -> BraunTree (n + 1) a
          (bound at /home/agnishom/test/typeExp/braun.hs:29:1)
Run Code Online (Sandbox Code Playgroud)

我不确定我在做什么错。另外,t1 :: BraunTree n1 a即使我已经注释了,haskell为什么仍认为(在错误消息中)t1 :: BraunTree p a?

帮助解释此错误消息将非常有帮助

HTN*_*TNW 1

您的类型签名太多。读完它们非常困难。另外,sucCong也没有必要。让我们先清理一下:

\n\n
insert :: a -> BraunTree n a -> BraunTree (n + 1) a\ninsert x Empty = Fork x Empty Empty (Left Refl)\ninsert x (Fork y t1 t2 (Left Refl)) = Fork x (insert x t1) t2 (Right Refl)\n-- by matching on Refl       ^^^^ you already prove that p ~ q\n-- and (p + 1) ~ (q + 1) just follows naturally (i.e. is Refl)       ^^^^\n-- if you just bound the equality to a variable, then sucCong would be necessary\n-- as it would match the variable to Refl "for" you.\n
Run Code Online (Sandbox Code Playgroud)\n\n

错误是一样的

\n\n
insert :: a -> BraunTree n a -> BraunTree (n + 1) a\ninsert x Empty = Fork x Empty Empty (Left Refl)\ninsert x (Fork y t1 t2 (Left Refl)) = Fork x (insert x t1) t2 (Right Refl)\n-- by matching on Refl       ^^^^ you already prove that p ~ q\n-- and (p + 1) ~ (q + 1) just follows naturally (i.e. is Refl)       ^^^^\n-- if you just bound the equality to a variable, then sucCong would be necessary\n-- as it would match the variable to Refl "for" you.\n
Run Code Online (Sandbox Code Playgroud)\n\n

消息底部n1是 的索引t1,您将其称为p。我们还知道m(t2\ 的索引)等于p,并且n(函数参数)等于(p + m) + 1。让我们对失败的约束应用所有可以的替换:

\n\n
Braun.hs:#:39: error:\n    \xe2\x80\xa2 Could not deduce: (((n1 + 1) + n1) + 1) ~ (n + 1)\n      from the context: n ~ ((n1 + m) + 1)\n        bound by a pattern with constructor:\n                   Fork :: forall a (n :: Nat) (m :: Nat).\n                           a\n                           -> BraunTree n a\n                           -> BraunTree m a\n                           -> Either (n :~: m) (n :~: (m + 1))\n                           -> BraunTree ((n + m) + 1) a,\n                 in an equation for \xe2\x80\x98insert\xe2\x80\x99\n        at Braun.hs:#:11-34\n      or from: m ~ n1\n        bound by a pattern with constructor:\n                   Refl :: forall k (a :: k). a :~: a,\n                 in an equation for \xe2\x80\x98insert\xe2\x80\x99\n        at Braun.hs:#:30-33\n      Expected type: BraunTree (n + 1) a\n        Actual type: BraunTree (((n1 + 1) + m) + 1) a\n      NB: \xe2\x80\x98+\xe2\x80\x99 is a non-injective type family\n    \xe2\x80\xa2 In the expression: Fork x (insert x t1) t2 (Right Refl)\n      In an equation for \xe2\x80\x98insert\xe2\x80\x99:\n          insert x (Fork y t1 t2 (Left Refl))\n            = Fork x (insert x t1) t2 (Right Refl)\n    \xe2\x80\xa2 Relevant bindings include\n        t1 :: BraunTree n1 a (bound at Braun.hs:#:18)\n        insert :: a -> BraunTree n a -> BraunTree (n + 1) a\n          (bound at Braun.hs:#:1)\n  |\n# | insert x (Fork y t1 t2 (Left Refl)) = Fork x (insert x t1) t2 (Right Refl)\n  |                                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n
Run Code Online (Sandbox Code Playgroud)\n\n

问题是 GHC 无法证明这一点((p + 1) + p) ~ ((p + p) + 1)。如果您使用了一个更好的Nat、未内置于编译器中的编译器,那么您自己就可以证明这是正确的。事实上,最明智的想法可能是:

\n\n
{-# LANGUAGE AllowAmbiguousTypes #-}\nimport Unsafe.Coerce\n-- using TypeApplications usually means using AllowAmbiguousTypes\n\n-- it is also possible to use a compiler plugin to "teach" GHC the laws\n-- of arithmetic\n-- by keeping the unsafeCoerce in these wrappers, you decrease the chance of\n-- "proving" something that isn\'t actually true.\nplusAssoc :: forall l m r. ((l + m) + r) :~: (l + (m + r))\nplusAssoc = unsafeCoerce Refl\nplusComm :: forall l r. (l + r) :~: (r + l)\nplusComm = unsafeCoerce Refl\n\ninsert :: a -> BraunTree n a -> BraunTree (n + 1) a\ninsert x Empty = Fork x Empty Empty (Left Refl)\ninsert x (Fork y (t1 :: BraunTree p a) t2 (Left Refl)) =\n  case plusAssoc @p @1 @p of Refl -> -- (p + 1) + p => p + (1 + p)\n    case plusComm @1 @p of Refl -> -- p + (1 + p) => p + (p + 1)\n      case plusAssoc @p @p @1 of Refl -> -- p + (p + 1) => (p + p) + 1\n        Fork x (insert x t1) t2 (Right Refl)\n
Run Code Online (Sandbox Code Playgroud)\n\n

注意:真的应该BraunTree有两个构造函数吗?本质上有两种Fork:平衡的一种和不平衡的一种。分成两个构造函数会更有意义(并消除大量间接)Fork。它也会更好,因为您可以消除某些部分定义的值。

\n