归一化 TypeNats 不等式

Normalizing TypeNats Inequalities

我有两个函数都具有 Nat(来自 TypeNats)类型,并且都具有比较类型运算符 <=。函数 b 的约束 2 <= n 包含函数 a 的约束 1 <= n。有没有办法让 ghc 解决 2 <= n 满足约束 1 <= n 的问题,这样我就不必为 b?

指定两个 (1 <= n, 2 <= n) 约束

下面的代码演示了错误。

{-# LANGUAGE KindSignatures, TypeOperators, ScopedTypeVariables, 
             DataKinds, TypeFamilies #-}

import GHC.TypeNats
import Data.Proxy

a :: forall (a :: Nat). 1 <= a => Proxy a -> Int
a = undefined

b :: forall (a :: Nat). 2 <= a => Proxy a -> Int
b = a

导致编译错误

• Could not deduce: (1 <=? a) ~ 'True arising from a use of ‘a’
  from the context: 2 <= a
    bound by the type signature for:
               b :: forall (a :: Nat). (2 <= a) => Proxy a -> Int
    at Example.hs:9:1-48
• In the expression: a
  In an equation for ‘b’: b = a
• Relevant bindings include
    b :: Proxy a -> Int
      (bound at Example.hs:10:1)

有一个库可以求解等式 ghc-typelits-natnormalise 但不能求解不等式。

尽管包描述,ghc-typelits-natnormalise 也可以解决不平等问题。以下程序使用 GHC 8.6.4 和 ghc-typelits-natnormalise-0.6.2 进行类型检查:

{-# LANGUAGE KindSignatures, TypeOperators, ScopedTypeVariables,
             DataKinds, TypeFamilies #-}
{-# OPTIONS_GHC -fplugin GHC.TypeLits.Normalise -Wall #-}

import GHC.TypeNats
import Data.Proxy

a :: forall (a :: Nat). 1 <= a => Proxy a -> Int
a = undefined

b :: forall (a :: Nat). 2 <= a => Proxy a -> Int
b = a