[Haskell-cafe] Re: Is it possible to read existential types?

Chung-chieh Shan ccshan at post.harvard.edu
Wed Apr 28 20:12:35 EDT 2004


On 2004-04-28T15:12:03-0400, S. Alexander Jacobson wrote:
> Ok, but it sounds like you need to know the list
> of possible types in advance.  Is it possible
> to have a lib take a filepath and type name as an
> arbitrary string, and read the instance in?

I don't think you need to know the list of possible types in advance.
Here is some (only slightly tested) code:

    import Data.Dynamic
    import Maybe
    import Monad

    lookupRead :: (Eq key, Read val, Typeable val) =>
        key -> [(key, (String, String))] -> Maybe val
    lookupRead key list = ret
      where ret = case lookup key list of
                    Just (typ, val) ->
                      if typ == show (typeOf (fromJust ret))
                        then case reads val of
                               [(v,"")] -> Just v
                               _        -> Nothing
                        else Nothing
                    _ -> Nothing

The question is how to get the result of lookupRead memoized, so that
the call to reads happens at most once for each entry in the list.

-- 
Edit this signature at http://www.digitas.harvard.edu/cgi-bin/ken/sig
BBC News: Universities face week of protest
http://news.bbc.co.uk/1/hi/education/3508209.stm
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 187 bytes
Desc: not available
Url : http://www.haskell.org//pipermail/haskell-cafe/attachments/20040428/86263c3c/attachment.bin


More information about the Haskell-Cafe mailing list