Why does adding a data type as a constraint to a type declaration result in an error match instead of a more correct error?

Or, in other words, is there a case to add a constraint, as described below?

Working code , with a minimal type declaration for function f:

data ZeroPositive = Zero | Positive Int deriving Show
f :: [Int] -> [ZeroPositive]
f [] = []
f (0:xs) = Zero:(f xs)
f (x:xs) = (Positive x):(f xs)
main = putStrLn . show $ f [0,2]

result:

[Zero,Positive 2]

Broken code with useless restriction. ZeroPositive:

data ZeroPositive = Zero | Positive Int deriving Show
f :: ZeroPositive => [Int] -> [ZeroPositive]
f [] = []
f (0:xs) = Zero:(f xs)
f (x:xs) = (Positive x):(f xs)
main = putStrLn . show $ f [0,2]

result:

  99.hs:3:7:
  Couldn't match expected type `ZeroPositive' with actual type `[t0]'
  In the pattern: []
  In an equation for `f': f [] = []

  99.hs:3:12:
  Couldn't match expected type `[Int] -> [ZeroPositive]'
          with actual type `[a0]'
  In the expression: []
  In an equation for `f': f [] = []

  ...

  99.hs:6:32:
  Couldn't match expected type `ZeroPositive' with actual type `[t0]'
  In the first argument of `f', namely `[0, 2]'
  In the second argument of `($)', namely `f [0, 2]'
  In the expression: putStrLn . show $ f [0, 2]
+4
source share
2 answers

This should be a mistake, because it ZeroPositiveis a data type, not a class, therefore it is not allowed as a restriction.

, , , GHC, ( GHC 7.4 GHC 7.6, , ConstraintKinds, , , => -> ), => ->, .

+5

, ; . : , a => b, , a . Haskell , a C t, C - , t - . . ( ), .

+4

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


All Articles