- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用的是由 singletons 包生成的非常简单的类型级自然数。我现在正在尝试向其中添加一个 Ord 实例。
{-# LANGUAGE MultiParamTypeClasses, TemplateHaskell, KindSignatures, DataKinds, ScopedTypeVariables, GADTs, TypeFamilies, FlexibleInstances, TypeOperators, UndecidableInstances, InstanceSigs #-}
module Functions where
import Data.Singletons
import Data.Singletons.TH
import Data.Singletons.Prelude
import Data.Promotion.Prelude
singletons [d|
data Nat = Z | S Nat
deriving Eq
instance Ord Nat where
(<=) Z _ = True
(<=) (S _) Z = False
(<=) (S n) (S m) = n <= m
|]
我遇到了一个又一个错误。最新的是:
src/Functions.hs:10:1:
Couldn't match kind ‘Nat’ with ‘*’
When matching types
n0 :: Nat
t1 :: *
Expected type: Sing t1
Actual type: Sing n0
Relevant bindings include
n_a9na :: Sing n0 (bound at src/Functions.hs:10:1)
lambda :: Sing n0 -> Sing m0 -> Sing (Apply (Apply (:<=$) t00) t10)
(bound at src/Functions.hs:10:1)
In the second argument of ‘applySing’, namely ‘n_a9na’
In the first argument of ‘applySing’, namely
‘applySing (singFun2 (Proxy :: Proxy (:<=$)) (%:<=)) n_a9na’
src/Functions.hs:10:1:
Could not deduce (SOrd 'KProxy) arising from a use of ‘%:<=’
from the context (t00 ~ 'S n)
bound by a pattern with constructor
SS :: forall (z_a9mg :: Nat) (n_a9mh :: Nat).
(z_a9mg ~ 'S n_a9mh) =>
Sing n_a9mh -> Sing z_a9mg,
in an equation for ‘%:<=’
at src/Functions.hs:(10,1)-(18,15)
or from (t10 ~ 'S n1)
bound by a pattern with constructor
SS :: forall (z_a9mg :: Nat) (n_a9mh :: Nat).
(z_a9mg ~ 'S n_a9mh) =>
Sing n_a9mh -> Sing z_a9mg,
in an equation for ‘%:<=’
at src/Functions.hs:(10,1)-(18,15)
or from (t00 ~ Apply SSym0 n0, t10 ~ Apply SSym0 m0)
bound by the type signature for
lambda_a9n9 :: (t00 ~ Apply SSym0 n0, t10 ~ Apply SSym0 m0) =>
Sing n0 -> Sing m0 -> Sing (Apply (Apply (:<=$) t00) t10)
at src/Functions.hs:(10,1)-(18,15)
In the second argument of ‘singFun2’, namely ‘(%:<=)’
In the first argument of ‘applySing’, namely
‘singFun2 (Proxy :: Proxy (:<=$)) (%:<=)’
In the first argument of ‘applySing’, namely
‘applySing (singFun2 (Proxy :: Proxy (:<=$)) (%:<=)) n_a9na’
有人知道执行此操作的正确方法是什么吗?
最佳答案
我不确定为什么会失败。我同样对实现 compare
时遇到的类似失败感到困惑,更对尝试(看似简单)时遇到的失败感到困惑
singletons [d| data Nat = Z | S Nat deriving (Eq,Ord) |]
我的猜测是 Ord
中的某些内容已关闭...但是,这是有效的。稍后我将尝试了解 singleton
的内部结构。
singletons [d|
data Nat = Z | S Nat
deriving (Eq)
instance Ord Nat where
compare = compare'
compare' :: Nat -> Nat -> Ordering
compare' Z Z = EQ
compare' (S _) Z = GT
compare' Z (S _) = LT
compare' (S n) (S m) = compare' n m
|]
顺便说一下,我这里使用的是 GHC 8.0。
在研究了单例
之后,我发现了问题的真正根源(并且对类型级黑客攻击的可能性感到震惊)。使用 GHC 中的 -ddump-splices
我能够获得生成的实际 Haskell 代码(对于您问题中的初始代码)。有问题的部分是
instance PEq (Proxy :: Proxy Nat_a7Vb) where
type (:==) (a_a8Rs :: Nat_a7Vb) (b_a8Rt :: Nat_a7Vb) = Equals_1627424016_a8Rr a_a8Rs b_a8Rt
和
instance POrd (Proxy :: Proxy Nat_a7Vb) where
type (:<=) (a_aa9e :: Nat_a7Vb) (a_aa9f :: Nat_a7Vb) = Apply (Apply TFHelper_1627428966Sym0 a_aa9e) a_aa9f
编译生成的代码,我收到了这两个稍微有用的错误消息
Expecting one more argument to ‘Proxy’
Expected kind ‘Proxy Nat_a7Vb’, but ‘Proxy’ has kind ‘k0 -> *’
与 PEq
和 POrd
类中的 (Proxy::Proxy Nat_a7Vb)
有关。如果没有 -XPolyKinds
则无法编译。检查了 singletons
的存储库,确实它告诉您需要启用 -XTypeInType
,从而启用 -XPolyKinds
。
所以,没有错误,您只需要添加 PolyKinds
或 TypeInType
(我推荐后者,因为这是软件包推荐的......)到您的 LANGUAGE
编译指示,让一切正常工作。
关于haskell - 将 Ord 实例添加到 'singleton' 包生成的自然值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39002342/
通过量化约束,我可以推导出 Eq (A f)正好?但是,当我尝试导出 Ord (A f) 时,它失败了。当约束类具有父类(super class)时,我不明白如何使用量化约束。我如何得出 Ord (A
我最近开始学习 Python。我正在阅读 Mark Lutz 第 4 版。在一本书中,我看到了该代码(如下)。这段代码中发生了什么?我不理解第 4 行。你能解释一下吗?抱歉我的英语不好。 B = '1
我正在遵循本指南:https://docs.oracle.com/cd/E56351_01/doc.30/e87809/getting-started-with-RESTful-services.ht
当我尝试从 tomcat 管理器启动 ORDS 时,它说:“上下文路径 [/ords] 上的应用程序无法启动” catalina.log 说: ... SEVERE [http-nio-8081-ex
我正在 Haskell 中创建一个递归的合并排序函数。我被告知作为评估的一部分,我必须将类型定义为: isort :: Ord a => [a] -> [a] 我假设该函数需要一个数组作为输入并输出一
假设您的评分是这样的 Rating = OneStar | TwoStars | ThreeStars | FourStars | FiveStars 在 Haskell 中实例化/实现此类代数数据类
我正在尝试创建一些数据结构来解决图形难题。我正在尝试定义边缘的比较标准,但我不确定如何定义。到目前为止: data Edge = Edge (Set String) Bool 如何“通知”编译器,如果
我见过一个与此类似的问题,但没有人确切地告诉我如何为结构实现 Ord。例如,以下内容: struct SomeNum { name: String, value: u32, } imp
我正在尝试配置 Oracle Apex 5 和 ORDS 以与 Tomcat 一起工作。我设法按照 Oracle 文档说明完成了所有步骤,但是当我尝试使用 http://localhost:8080/
Python官方文档解释了ord(c) ord(c): Given a string representing one Unicode character, return an integer rep
由于 Ord 是 Eq 的子类,我发现很难理解创建该类的新类型实例的样子。 我已经设法做到了: newtype NT1 = NT1 Integer instance Eq NT1 wh
根据ISO 8859-1 €符号的十进制值为128 我的默认PHP脚本编码是 echo mb_internal_encoding(); //ISO-8859-1 所以现在作为PHP echo chr(
Data.Ord 包括这些方法: compare :: a -> a -> Ordering ( a -> Bool ( a -> Bool (>) :: a -> a -> Bool (>=) ::
假设我有一个树的实现: data Tree a children = EmptyTree | Tree a (children (Tree a children)) 是否可以限制children返回
我正在尝试编程某种 Caesar Cipher在科博尔。但不知何故,我收到以下编译错误: Numeric function "INTEGER FUNCTION ORD" was not allowed
鉴于以下类型: import Data.Set as Set -- http://json.org/ type Key = String data Json = JObject Key (Set JV
我对 Haskell 比较陌生,所以希望这不是一个愚蠢的问题。我正在寻找一种常见/众所周知的类型,它本质上是一个元组,但定义了 Ord 以便它只比较第一个元素。 我自己可以这样定义: data Ran
我试图找到元素总和最小的列表。: shortest :: (Num a) => [[a]] -> [a] shortest [] = [] shortest (x:xs) = if sum x [[
我在 Haskell 中创建了一个数据类型: type Name = String data ModelNode = NodeAttribute Name | Nod
我是 Haskell 新手。谁能解释一下 Ord a 的区别和用法吗? 现在我熟悉了 [a] -> [a] -> [a] 的事情。但这, Ord a => [a] -> [a] -> [a] 请详细解
我是一名优秀的程序员,十分优秀!