[Haskell-cafe] Dynamics/Read and Existential types (was: Is it possible to read existential types?)
So let me ask this question a different way. Is it possible to use Read/Show or Typeable with Existential types. Given MyClass and MyType as follows: class (Read a,Show a,Typeable)=>MyClass a where foo::a type MyType = forall a. MyClass a=> [a] Is there a way to persist MyType? My suspicion is that there isn't because Haskell knows how to say that a string can't be parsed into a particular type but it does not know how to say that a string can't be parsed into any type that is an instance of a particular class. So, assuming you can't I amn curious to know know if this is an in-principle problem or just an in-fact problem. -Alex- _ S. Alexander Jacobson mailto:[EMAIL PROTECTED] tel:917-770-6565 http://alexjacobson.com ___ Haskell-Cafe mailing list [EMAIL PROTECTED] http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Re: Is it possible to read existential types?
On 2004-04-29T11:50:48-0400, S. Alexander Jacobson wrote: > But isn't the point of this code that you don't > need that type signature? If I knew in advance > that it was an Integer then I wouldn't need to > passs "Integer" in the list. In the context in which the code was originally written, the point was not that you don't need that type signature. Your comment above helped me understand what you want, I think. -- 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 pgp0.pgp Description: PGP signature ___ Haskell-Cafe mailing list [EMAIL PROTECTED] http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Re: Is it possible to read existential types?
But isn't the point of this code that you don't need that type signature? If I knew in advance that it was an Integer then I wouldn't need to passs "Integer" in the list. -Alex- On Thu, 29 Apr 2004, Chung-chieh Shan wrote: > On 2004-04-28T23:33:31-0400, S. Alexander Jacobson wrote: > > I don't think this works. I just tried it with: > > main = print $ lookupRead 1 [(1,("Integer","100"))] > > This fails for the same reason > > print $ read "100" > > fails. You need to give a type signature to avoid type-class instance > ambiguity: > > main = print $ (lookupRead 1 [(1,("Integer","100"))] :: Maybe Integer) > > On GHCi 6.2.1, the above yields "Just 100" for me. > > -- > Edit this signature at http://www.digitas.harvard.edu/cgi-bin/ken/sig > Be it declared and enacted by this present Parliament / That the People > of England / are / a Commonwealth and free State / without any King or > House of Lords.-- An Act declaring England to be a Commonwealth > 1649-05-19 | 355 years | 2004-05-19http://tinyurl.com/2dqnh > _ S. Alexander Jacobson mailto:[EMAIL PROTECTED] tel:917-770-6565 http://alexjacobson.com ___ Haskell-Cafe mailing list [EMAIL PROTECTED] http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Re: Is it possible to read existential types?
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:[EMAIL PROTECTED] 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 > ___ Haskell-Cafe mailing list [EMAIL PROTECTED] http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Re: Is it possible to read existential types?
On 2004-04-28T23:33:31-0400, S. Alexander Jacobson wrote: > I don't think this works. I just tried it with: > main = print $ lookupRead 1 [(1,("Integer","100"))] This fails for the same reason print $ read "100" fails. You need to give a type signature to avoid type-class instance ambiguity: main = print $ (lookupRead 1 [(1,("Integer","100"))] :: Maybe Integer) On GHCi 6.2.1, the above yields "Just 100" for me. -- Edit this signature at http://www.digitas.harvard.edu/cgi-bin/ken/sig Be it declared and enacted by this present Parliament / That the People of England / are / a Commonwealth and free State / without any King or House of Lords.-- An Act declaring England to be a Commonwealth 1649-05-19 | 355 years | 2004-05-19http://tinyurl.com/2dqnh signature.asc Description: Digital signature ___ Haskell-Cafe mailing list [EMAIL PROTECTED] http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Re: Is it possible to read existential types?
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 pgp0.pgp Description: PGP signature ___ Haskell-Cafe mailing list [EMAIL PROTECTED] http://www.haskell.org/mailman/listinfo/haskell-cafe