[Haskell-beginners] data type design question

Tillmann Rendel rendel at daimi.au.dk
Thu Jul 31 02:58:49 EDT 2008


Hi Markus,

Markus Barenhoff wrote:
> mmm lists could also contain list or dict, so on must introduce constructors
> for those too. You will run in problems there because what is with a list of
> lists then you would need a TIntListList and so on... 

I think your problem to be seriously hard :) On the one hand, you want 
to have your T dynamically typed, so that it can returned by Parsec 
parsers and so on, and on the other hand, you want it to be statically 
typed, so that you can express more exact static types e.g. for lists.

That means you have to "embed" your T-typesystem into Haskell's 
typesystem so that your constraints (such as: all elements in this list 
use the same constructor) are statically checked.

This should be possible using advanced techniques, but I'm not sure that 
extra bit of static type safety is worth it.

Maybe you could instead keep your current datatype, and provide some 
convenience functions for accessing T values:

   unknown :: T -> Maybe T
   unknown x = Just x

   string :: T -> Maybe String
   string (TString s) = Just s
   string _ = Nothing

   dict :: T -> Maybe [(String, T)]
   dict (TDict d) = Just d
   dict _ = Nothing

   listOf :: (T -> a) -> T -> Maybe [T]
   listOf f (TList xs) = mapM f xs

Now code which expects, for example, either a list of list of 
dictionaries, or a list of strings, could look like:

   foo t = mconcat
     [ listOf (listOf dict) t >>= \lld -> ...
     , listOf string t >>= \ls -> ...
     ]

This way, you could enable clients to easily check for wellformedness 
while processing the parsed data.

   Tillmann


More information about the Beginners mailing list