Changes between Version 11 and Version 12 of TypeFunctions/ClassFamilies


Ignore:
Timestamp:
May 29, 2007 2:45:41 AM (8 years ago)
Author:
sulzmann
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • TypeFunctions/ClassFamilies

    v11 v12  
    247247forall a. Size a => a -> Int 
    248248}}} 
    249 But the second type is not a subtype of the second. 
     249But the second type is not a subtype of the second 
     250because we cannot derive 'Size a' from 'Data a' for any 'a'. 
    250251For this subtype relation to hold, if we can satisfy 
    251 the condition 
    252 that from 'Data a' we can derive 'Size a' for any 'a'. 
    253252This condition is satisfied  
    254253if we make 'Sizes' a superclass of 'Data'. 
     
    256255'Data's class declaration for each new generic function). 
    257256 
    258 '''MS note''': How to fix the (modularity) problem via indexed classes? 
     257We fix the problem by by introducing an indexed class 
     258in gmapQ's context. Thus, we can 'enable' the 'Size' class 
     259when needed. 
     260 
     261{{{ 
     262-- type family  
     263class family Context ctxt :: * -> class 
     264 
     265class Typeable t => Data ctxt t where 
     266   gmapQ :: (forall e . Data e ctxt, Context ctxt e => e -> r) -> t -> [r] 
     267 
     268instance Data Char ctxt where 
     269   gmapQ f c = [] 
     270 
     271instance Data e ctxt => Data [e] ctxt where 
     272   gmapQ f (x:xs) = [f x,f xs] 
     273 
     274 
     275class Size t where 
     276   gsize :: t -> Int 
     277 
     278instance Size t where 
     279   gsize = 42 
     280 
     281... 
     282 
     283instance Data t SizeCtxt => Size t where 
     284   gsize x = 1 + sum (gmapQ gsize x) 
     285 
     286data SizeCtxt -- uninhabited 
     287 
     288class instance Context SizeCtxt e = Size e 
     289}}} 
    259290 
    260291=== Type checking ===