Can we ignore class classes?

I wonder if there is a deeper reason why we cannot abstract from class classes (or can we?).

For example, when we have

fzip :: (forall a.[a] -> [a]) -> [b] -> [c] -> [(b,c)] fzip f xs ys = zip (f xs) (f ys) 

then we can say that

 fzip (drop 42) [1..100] ['a'..'z'] fzip reverse [1..100] ['a'..'z'] 

etc. But we can’t

 fzip (map succ) [1..100] ['a'..'z'] 

which we can fix with:

 ezip :: (Enum b, Enum c) => (forall a.Enum a => [a] -> [a]) -> [b] -> [c] -> [(b,c)] ezip f xs ys = zip (f xs) (f ys) 

as well as we can fix

 fzip (map (5*)) [1..100] [1.5, 2.3, 4.7] 

with

 nzip :: (Num b, Num c) => (forall a.Num a => [a] -> [a]) -> [b] -> [c] -> [(b,c)] nzip f xs ys = zip (f xs) (f ys) 

But doesn't it bother us that we can't include ezip and nzip something like:

 gzip :: (gb, gc) => (forall a. ga => [a] -> [a]) -> [b] -> [c] -> [(b,c)] 

although the code is absolutely identical, right down to the class name? Or can we somehow?

Interestingly, when instances were only records containing functions, this would be easily possible.

+5
source share
2 answers

You can almost do it with ConstraintKinds :

 {-# LANGUAGE ConstraintKinds, RankNTypes #-} import Data.Proxy gzip :: (gb, gc) => Proxy g -> (forall a . ga => [a] -> [a]) -> [b] -> [c] -> [(b,c)] gzip _ f xs ys = zip (f xs) (f ys) test1 = gzip (Proxy :: Proxy Enum) (map succ) [1 .. 100] ['a' .. 'z'] test2 = gzip (Proxy :: Proxy Num) (map (5*)) [1 .. 100] [1.5, 2.3, 4.7] 

The main difference is that you need a Proxy argument because GHC cannot make the correct instance for g without help.

+11
source

Add a Proxy argument to limit:

 {-# LANGUAGE PartialTypeSignatures #-} import Data.Proxy gzip :: (gb, gc) => Proxy g -> (forall a. ga => [a] -> [a]) -> [b] -> [c] -> [(b,c)] gzip _ f bs cs = zip (f bs) (f cs) > gzip (Proxy :: _ Enum) [0, 1] "ab" [(1,'b'),(2,'c')] 

The GHC considers the parameters of restrictions, which are found only in the restrictions ambiguously, so we need to explicitly write them in the proxy.

+8
source

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


All Articles