[Haskell-cafe] Newbie question on Haskell type

robert dockins robdockins at fastmail.fm
Fri Oct 14 09:29:01 EDT 2005


So this is essentially a parsing problem.  You want a user to be able 
input a string and have it interpreted as an appropriate data value.  I 
think you may want to look at the Parsec library 
(http://www.cs.uu.nl/~daan/parsec.html).  I don't think the direction 
you are heading will get the results you want.


As to typeable, the basic types are mostly all members of Typeable.  You 
can find a pretty good list here:

http://www.haskell.org/ghc/docs/latest/html/libraries/base/Data.Typeable.html#t%3ATypeable

Additionally, GHC can create Typeable instances automaticly for user 
defined datatypes; just add a deriving Typeable clause.

data SomeType = C1 | C2 deriving (Show,Eq,Typeable)

The restriction is that all types which appear in constructors must also 
be in Typeable.

I believe the DrIFT preprocessor can also create Typeable instances if 
you are not using GHC.

Huong Nguyen wrote:
> Hello,
> 
> Thanks for your solution.
> 
> My main purpose is that I want to input a value and check whether this 
> value is belong to some specific types or not. These types can be some 
> popular types (such as: String, Char, Int, etc) or some more complex 
> data structures defined by user. Thus, at first, I try with type String 
> (even with that simple type, I still face difficulty ;-))
> 
> I want to ask you which types can be used with Data.Typeable. I read for 
> over 15 minutes but it is still not clear with me.
> For some other complex data types defined by user, what I should do to 
> use Data.Typeable ?
> 
> Thank you very much.
> 
> On 10/13/05, *robert dockins* <robdockins at fastmail.fm 
> <mailto:robdockins at fastmail.fm>> wrote:
> 
>     In GHC you can do this:
> 
>      > import Data.Typeable
> 
>      > isString :: (Typeable a) => a -> Bool
>      > isString x = typeOf x == typeOf (undefined::String)
> 
>     Why do you want this?  It's not the kind of operation one does very
>     often in Haskell.
> 
> 
>     Huong Nguyen wrote:
> 
>      > Hi all,
>      >
>      > I want to write a small functionto test whether an input is a
>     String or
>      > not. For example,
>      >
>      > isString::(Show a) =>a ->Bool
>      > This function will return True if the input is a string and
>     return False
>      > if not
>      >
>      > Any of you have idea about that? Thanks in advance
>      >
>      >
>      >
>     ------------------------------------------------------------------------
> 
>      >
>      > _______________________________________________
>      > Haskell-Cafe mailing list
>      > Haskell-Cafe at haskell.org <mailto:Haskell-Cafe at haskell.org>
>      > http://www.haskell.org/mailman/listinfo/haskell-cafe
> 
> 



More information about the Haskell-Cafe mailing list