我正在使用单一包生成的非常简单的类型级自然。我现在正在尝试向他们添加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’
有谁知道这样做的正确方法是什么?
答案 0 :(得分:5)
我不确定为什么会失败。我同样对实施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。
在singletons
中探索之后,我发现了问题的真正根源(并且已经被可能存在多少类型级别的hackery所震惊)。使用来自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 -> *’
与(Proxy :: Proxy Nat_a7Vb)
和PEq
类中的POrd
相关。没有-XPolyKinds
,这将无法编译。检查了singletons
的回购,确实它告诉您需要启用-XTypeInType
,然后启用-XPolyKinds
。
所以,没有错误,您只需要添加PolyKinds
或TypeInType
(我推荐后者,因为这是包推荐的内容......)到{{1} pragma让一切运转起来。
答案 1 :(得分:2)
使用提升的布尔关系is never comfortable。布尔人会删除你对学习感兴趣的信息,当你想对测试结果做任何事情时,你就会陷入困境。说不,孩子们。
有更好的方法。 &#34; n
小于或等于m
&#34;是命题,可以证明具有信息丰富的证据。证明一个数字小于另一个数字的一种方法是给出(单例表示)它们的差异:
data LE n m where
LE :: Natty z -> LE n (n :+ z)
我们可以提出一个测试给定数字是否小于另一个的程序。 le
尝试从n
中减去m
,并且失败并返回Nothing
或产生差异,作为Natty
,并证明减法为正确,打包在LE
构造函数中。
le :: Natty n -> Natty m -> Maybe (LE n m)
le Zy m = Just (LE m)
le (Sy n) (Sy m) = fmap (\(LE z) -> LE z) (le n m)
le _ _ = Nothing
这个想法可以推广给我们一个强烈打字的compare
&#34;。比较两个数字时,您要么得知它们相等,要么就知道它们比另一个要小。 (Either (LE n m) (LE m n)
也可以完成这项工作,但这个版本更精确一些。)
data Compare n m where
LT :: Natty z -> Compare n (n :+ S z)
EQ :: Compare n n
GT :: Natty z -> Compare (m :+ S z) m
compare :: Natty n -> Natty m -> Compare n m
compare Zy Zy = EQ
compare Zy (Sy m) = LT m
compare (Sy n) Zy = GT n
compare (Sy n) (Sy m) = case compare n m of
LT z -> LT z
EQ -> EQ
GT z -> GT z
(我从Hasochism解除了这个。)
请注意,与le
不同,compare
为总数。它 总是给你一个结果:每个数字都小于,等于或大于其他每个数字。我们的目标是编写一个程序来测试两个数字中哪一个更小,但我们也发现自己证明数字是totally ordered,并且编写了一个类型安全的减法例程,所有这些都在同一个函数中。
另一种查看compare
的方式是作为视图对自然数字对。当你比较两个数字时,你会知道哪个数字少了多少,并且知道了数字本身的知识。 Agda的点图案对这种细化概念有很好的支持:
compare : (n m : Nat) -> Compare n m
compare zero zero = eq
compare zero (suc m) = lt m
compare (suc n) zero = gt n
compare (suc n) (suc m) with compare n m
-- see how matching on `lt` refines `m` to `n + suc z`
compare (suc n) (suc .(n + suc z)) | lt z = lt z
compare (suc m) (suc .m) | eq = eq
-- likewise matching on `gt` refines `n` to `m + suc z`
compare (suc .(m + suc z)) (suc m) | gt z = gt z
无论如何,我无法直接与您的singletons
错误来源进行对话,但{one} Ord
对于单身人士价值难以处理的一个原因是,它假定您是&#39;重新比较相同类型的值:
class Ord a where
compare :: a -> a -> Ordering
当你比较两个单身时,他们通常不会有相同的类型!这是单身人士的全部观点:他们的类型直接反映了他们的价值。如果您有两个Natty n
值(n
匹配),那么比较它们没什么意义,因为您已经知道它们相等;如果它们不相等,你就无法比较它们!
非常合理的是像Ord
这样的类,它们是在简单类型的世界中设计的,在依赖类型的程序中不一定非常有用。如果您使用的是依赖类型,正确的做法是不要滥用旧工具。以张开双臂迎接这个安全,信息丰富的编程新世界!