GADT - , GADT. , .
, GADT, , , . , GADT. GADT, :
data Foo a where
StringFoo :: String -> Foo String
IntFoo :: Int -> Foo Int
, . :
deconstructFoo :: Foo a -> a
deconstructFoo (StringFoo s) = "Hello! " ++ s ++ " is a String!"
deconstructFoo (IntFoo i) = i * 3 + 1
, - . deconstructFoo promises a, Foo a. a String, Int.
This is something you cannot do with the regular data type, and the new thing provided by GADT. In the first equation, matching a pattern adds a constraint (a ~ String)to its context. In the second equation, pattern matching adds (a ~ Int).
If you have not created a type in which pattern matching can cause a type refinement, you do not have GADT. You just have a type declared with GADT syntax. What's good is, in many ways, this is better syntax than the syntax of the underlying data type. This is just more detailed for the simplest cases.
source
share