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

S. Alexander Jacobson alex at i2x.com
Thu Apr 29 00:33:31 EDT 2004


I don't think this works.  I just tried it with:

  main = print $ lookupRead 1 [(1,("Integer","100"))]

How would Haskell know that typ actually does
equal typeOf?

-Alex-

_________________________________________________________________
S. Alexander Jacobson                  mailto:me at alexjacobson.com
tel:917-770-6565                       http://alexjacobson.com


On Wed, 28 Apr 2004, Chung-chieh Shan wrote:

> 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
>



More information about the Haskell-Cafe mailing list