[Haskell] Typeable and Data instances for Double, FiniteMap, ...
Georg Martius
mai99dgf at studserv.uni-leipzig.de
Sun Jan 16 13:45:53 EST 2005
Hi,
I was playing around with "Scap you Boilerplate" and realised some missing instances of Typeable and Data. Is there a particular reason why there is no Data Double instance?
Furthermore I was wondering why no instance for the collection types such as FiniteMap, Set and HashTable is provided. At the end of the mail [1] there is my implementation of instances for at least Double and FiniteMap.
Apart from that I looked at the library source-code (GHC) and reallised that there is really much documenting comments in, but which are not Haddock comments. Again I don't understand that. Is the programmer supposed to look at the source-code rather the API documentation?
Regards,
Georg
[1] instances: Data Double, Typeable FiniteMap, Data FiniteMap
> --
> -- Data instance for Double.
> -- IMHO It should be added as a basic type to Data.Constr with DoubleConstr like Float
> doubleConstr :: Double -> Constr
> doubleConstr x = mkConstr 1 (show x) Prefix
> doubleDataType :: DataType
> doubleDataType = mkDataType [doubleConstr 0.0]
>
> instance Data Double where
> toConstr x = doubleConstr x
> fromConstr c = read $ conString c
> dataTypeOf _ = doubleDataType
>
>
>
> --
> -- Typeable instance for FiniteMap
> fmTc :: TyCon
> fmTc = mkTyCon "FM"
>
> instance (Typeable a, Typeable b) => Typeable (FiniteMap a b) where
> typeOf fm = mkAppTy fmTc [typeOf ((undefined :: FiniteMap a b -> a) fm)]
>
> --
> -- Data instance for FiniteMap
> -- treats the FiniteMap as a list.
> -- More performant solutions might be possible,
> -- if internal of FM can be exploited
> fmConstr :: Constr
> fmConstr = mkConstr 1 "FM" Prefix
> fmDataType :: DataType
> fmDataType = mkDataType [fmConstr]
>
> instance (Data a, Data b, Ord a) => Data (FiniteMap a b) where
> gfoldl f z fm = z listToFM `f` (fmToList fm)
> toConstr _ = fmConstr
> fromConstr c = case conIndex c
> of 1 -> emptyFM
> dataTypeOf _ = fmDataType
>
--
---- Georg Martius, Tel: (+49 34297) 89434 ----
------- http://www.flexman.homeip.net ---------
More information about the Haskell
mailing list