haskell - 规范化 TypeNats 不等式
问题描述
我有两个具有 Nat(来自 TypeNats)类型的函数,它们都有一个比较类型 operator <=
。2 <= n
function的约束b
包含 function 的1 <= n
约束a
。有没有办法让 ghc 解决2 <= n
满足约束1 <= n
的问题,这样我就不必为 指定两个(1 <= n, 2 <= n)
约束b
?
下面的代码演示了错误。
{-# 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
推荐阅读
- excel - 基于前 X 个单元格的条件格式
- ios - Xcode 11 - 命令 CodeSign 因非零退出代码而失败。无法为签名者构建自签名根的链
- scala - 为 scala,Mavan 构建失败
- python - 使用 Json 格式化和缩进网站请求 Python
- sas - SAS PassThru 到 Snowflake 使用 EXECUTE(%include
此过程适用于直通到 Teradata。我们正在迁移到 Snowflake。我使用数据步骤创建程序并将语句 INSERT INTO TEMPTABLE VALUES('VAR', 'VAR', 'VAR, VAR', 'VAR, 'VAR', 'VAR'); 雪的execute();当我将每条记录复制
- mongodb - 如果另一个字段匹配条件 MongoDB,则从字段中获取值
- expo - 如何将 Android 证书设置为 Expo?
- r - 如何根据循环中的迭代次数(在 R 中)命名列?
- reactjs - 更改视图时 React Three.js 后处理闪烁的黑屏(Redux 的三纤)
- php - WPMU 从特定博客 id 的 db 表中获取值并将其放入 html