Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.1k views
in Technique[技术] by (71.8m points)

haskell - Could not deduce KnownNat in two existentials with respect to the singletons library

I was experimenting with the singletons library and I found a case that I don't understand.

{-# 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 

I could use this to get the minimum of two Things

minthing :: Thing -> Thing -> Integer
minthing (Thing foo1) (Thing foo2) = min (foolen foo1) (foolen foo2)

But why can't I just do this?

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))))
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to do some theorem proving to check that given KnownNat a and KnownNat b you can get KnownNat (Min a b). A possible solution:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...