Understanding when to use type classes or GADT?

I'm trying to figure out the differences between class types and GADTS, especially when using the extension -XMultiParamTypeClasses.

Both have similar features:

class MyClass a b where
  f :: a -> b -> Bool

instance MyClass String String where
  f s1 s2 = ...

instance MyClass Int Int where
  f i1 i2 = ...

data Gadt a where
  F :: String -> String -> Bool
  F2 :: Int -> Int -> Bool

So far, the only difference that I really see is that GADT allows an interface like functions to have flexible number arguments:

data Gadt a where
  PassTwoArgs :: String -> String -> Gadt Bool
  PassOneArgs :: String -> Gadt Bool

myFunction :: Gadt a -> a
myFunction (PassTwoArgs s1 s2) = ...
myFunction (PassOneArgs s1) = ...

This is not easy to do with type classes so far.

Are there any other differences or uses of one another?

+4
source share
2 answers

If you have a class, you can add new instances at any time.

GADT, , . , . , , .

, . , - , . (, (==) Int String, .) GADT - , , - . - , GADT - , Haskell .

"", - .;-) , .

+6

, (G) ADT. , .

, ; , (G) ADT. .

+2

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


All Articles