Why don't DefaultSignatures allow specific types?

{-# LANGUAGE DefaultSignatures #-} class C a where f :: [a] default f :: (Num a) => [a] f = [1,2,3,4,5] 

Above works, below not. It seems that the DefaultSignatures extension only allows you to define restrictions, but not replace a specific type. The Default Method Signature section of the GHC User Guide does not explain this. Why doesn't DefaultSignatures allow me to replace a specific type? What is the rationale? Where can I read more about how and why DefaultSignatures implemented?

 {-# LANGUAGE DefaultSignatures #-} class C a where f :: [a] default f :: [Int] f = [1,2,3,4,5] 
+6
source share
1 answer

If you use GHC 8.0.2, you have to write it differently, because for these types of types there is regression of type checking. See Notes:

https://downloads.haskell.org/~ghc/8.0.2/docs/html/users_guide/8.0.2-notes.html

So you need to write it like this:

 {-# LANGUAGE DefaultSignatures #-} {-# LANGUAGE TypeFamilies #-} class C a where f :: [a] default f :: (a ~ Int) => [a] f = [1,2,3,4,5] 

Instead of saying that f has a list type of Int s, you should say that f has a list type of some type a , where a is Int . The language extension {-# LANGUAGE TypeFamilies #-} necessary to ensure type equality. This is not what it was used for, but it is required. Compilation for GHC 8.0.2

+7
source

Source: https://habr.com/ru/post/1268482/


All Articles