From 818a2c1927b7299387c8eb71f58a2415cb6e5354 Mon Sep 17 00:00:00 2001 From: "simonpj@microsoft.com" Date: Wed, 10 Dec 2008 05:44:32 +0000 Subject: [PATCH] Improve documentation for data family instances (cf Trac #1968) The HEAD allows GADT syntax for data/newtype family instances. (GHC 6.10 does not seem to.) --- docs/users_guide/glasgow_exts.xml | 60 +++++++++++++++++++++++++++++-------- 1 file changed, 47 insertions(+), 13 deletions(-) diff --git a/docs/users_guide/glasgow_exts.xml b/docs/users_guide/glasgow_exts.xml index ec08210..3e96098 100644 --- a/docs/users_guide/glasgow_exts.xml +++ b/docs/users_guide/glasgow_exts.xml @@ -3860,37 +3860,71 @@ data instance GMap (Either a b) v = GMapEither (GMap a v) (GMap b v) can be any number. - Data and newtype instance declarations are only legit when an - appropriate family declaration is in scope - just like class instances - require the class declaration to be visible. Moreover, each instance + Data and newtype instance declarations are only permitted when an + appropriate family declaration is in scope - just as a class instance declaratoin + requires the class declaration to be visible. Moreover, each instance declaration has to conform to the kind determined by its family declaration. This implies that the number of parameters of an instance declaration matches the arity determined by the kind of the family. - Although, all data families are declared with - the data keyword, instances can be - either data or newtypes, or a mix - of both. + A data family instance declaration can use the full exprssiveness of + ordinary data or newtype declarations: + + Although, a data family is introduced with + the keyword "data", a data family instance can + use either data or newtype. For example: + +data family T a +data instance T Int = T1 Int | T2 Bool +newtype instance T Char = TC Bool + + + A can use GADT syntax for the data constructors, + and indeed can define a GADT. For example: + +data family G a b +data instance G [a] b where + G1 :: c -> G [Int] b + G2 :: G [a] Bool + + + You can use a deriving clause on a + data instance or newtype instance + declaration. + + + + + Even if type families are defined as toplevel declarations, functions - that perform different computations for different family instances still + that perform different computations for different family instances may still need to be defined as methods of type classes. In particular, the following is not possible: data family T a data instance T Int = A data instance T Char = B -nonsence :: T a -> Int -nonsence A = 1 -- WRONG: These two equations together... -nonsence B = 2 -- ...will produce a type error. +foo :: T a -> Int +foo A = 1 -- WRONG: These two equations together... +foo B = 2 -- ...will produce a type error. + +Instead, you would have to write foo as a class operation, thus: + +class C a where + foo :: T a -> Int +instance Foo Int where + foo A = 1 +instance Foo Char where + foo B = 2 - Given the functionality provided by GADTs (Generalised Algebraic Data + (Given the functionality provided by GADTs (Generalised Algebraic Data Types), it might seem as if a definition, such as the above, should be feasible. However, type families are - in contrast to GADTs - are open; i.e., new instances can always be added, possibly in other modules. Supporting pattern matching across different data instances - would require a form of extensible case construct. + would require a form of extensible case construct.) -- 1.7.10.4