In haskell, what exactly is an ADT constructor type that has no parameters?

Suppose I set the type

data A a = A a | B deriving Show

I know that (B :: A Int) == (B :: A Double)does not check typecheck, because A Intand A Doubleare different, unequal types, so I can not use(==) :: Eq a => a -> a -> Bool

But then I can ask ghci what it is show B, and ghci says that it is the string "B". What is a type B? Is it A Int? What for? And why ghci does not complain that the type is Bambiguous, given that it can be A afor absolutely anyone a.

I can also ask ghci what is show (B==B)it and it says "True" what is type B?

When I specify ghci type B with :t B, it prints B :: A a, but if I'm not confused in the two examples above, it should be a specific type without any type parameters. So, how do I know what is Bin B==B?

I'm a little confused. Is it being described somewhere?

+4
source share
1 answer

The reason for the behavior is the ghc extension, which is called ExtendedDefaultRules .

Quote from the link:

However, it is tedious for the user to specify the type, so GHCi extends Haskell's default rules (Section 4.3.4 of the Haskell 2010 Report) as follows. The standard rules take each group (C1 a, C2 a, ..., Cn a) for each variable of type a and by default sets a variable of type if

  • a
  • Ci .
  • , Ci .

GHCi GHC, -XExtendedDefaultRules :

  • 2 : Ci .
  • 3 : , Ci numeric Show, Eq Ord. () default.

, , B == B show B, () a.

, GHCi:

:set -XNoExtendedDefaultRules
data Foo a = A a | B deriving Eq
B == B

ambiguous type variable a.

+9

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


All Articles