作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在试验单例库,发现一个我不明白的案例。
{-# LANGUAGE GADTs, StandaloneDeriving, RankNTypes, ScopedTypeVariables,
FlexibleInstances, KindSignatures, DataKinds, StandaloneDeriving #-}
import Data.Singletons.Prelude
import Data.Singletons.TypeLits
data Foo (a :: Nat) where
Foo :: Foo a
deriving Show
data Thing where
Thing :: KnownNat a => Foo a -> Thing
deriving instance Show Thing
afoo1 :: Foo 1
afoo1 = Foo
afoo2 :: Foo 2
afoo2 = Foo
athing :: Thing
athing = Thing afoo1
foolen :: forall n. KnownNat n => Foo n -> Integer
foolen foo =
case sing of (SNat :: Sing n) -> natVal (Proxy :: Proxy n)
minfoo :: forall a b c. (Min a b ~ c, KnownNat c) => Foo a -> Foo b -> Integer
minfoo _ _ =
let c = case sing of (SNat :: Sing c) -> natVal (Proxy :: Proxy c)
in natVal (Proxy :: Proxy c)
thinglen :: Thing -> Integer
thinglen (Thing foo) = foolen foo
minthing :: Thing -> Thing -> Integer
minthing (Thing foo1) (Thing foo2) = min (foolen foo1) (foolen foo2)
minthing' :: Thing -> Thing -> Integer
minthing' (Thing foo1) (Thing foo2) = minfoo foo1 foo2
• Could not deduce (KnownNat
(Data.Singletons.Prelude.Ord.Case_1627967386
a
a1
(Data.Singletons.Prelude.Ord.Case_1627967254
a a1 (GHC.TypeLits.CmpNat a a1))))
最佳答案
你需要做一些定理证明来检查给定的 KnownNat a
和 KnownNat b
你可以得到KnownNat (Min a b)
.一个可能的解决方案:
import Data.Constraint
theorem :: forall a b. (KnownNat a, KnownNat b) =>
Sing a -> Sing b -> Dict (KnownNat (Min a b))
theorem sa sb = case sCompare sa sb of
SLT -> Dict
SEQ -> Dict
SGT -> Dict
fooSing :: forall a. KnownNat a => Foo a -> Sing a
fooSing _ = sing
minthing' :: Thing -> Thing -> Integer
minthing' (Thing foo1) (Thing foo2) =
case theorem (fooSing foo1) (fooSing foo2) of
Dict -> minfoo foo1 foo2
关于haskell - 无法在关于单例库的两个存在中推断 KnownNat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41492754/
我最近购买了《C 编程语言》并尝试了 Ex 1-8这是代码 #include #include #include /* * */ int main() { int nl,nt,nb;
早上好!我有一个变量“var”,可能为 0。我检查该变量是否为空,如果不是,我将该变量保存在 php session 中,然后调用另一个页面。在这个新页面中,我检查我创建的 session 是否为空,
我正在努力完成 Learn Python the Hard Way ex.25,但我无法理解某些事情。这是脚本: def break_words(stuff): """this functio
我是一名优秀的程序员,十分优秀!