There is a way to do this using a bunch of GHC extensions, but I get the feeling that you're misinterpreting parametric polymorphism. The type (Show a) => a -> Bool means that the function isString can be implemented without caring what type a is, only knowing that it is in Show. As an example of this sort of thing, I know that there are only 4 values of type a -> Bool (without the class context). They are the constant functions (\x -> True), (\x -> False), and two kinds of failure (\x -> _|_), and _|_, where _|_ is pronounced "bottom" and represents something along the lines of nontermination (aborting the program also counts). That said, typeclasses, along with undecidable, overlapping instances (two GHC extensions) will allow us to do this, and without the Show context, but with an additional context of our own, which every type happens to be a member of: class IsString t where isString :: t -> Bool instance IsString [Char] where isString _ = True instance IsString a where isString _ = False Note that you need the command line options -fglasgow-exts, -fallow-overlapping-instances, and -fallow-undecidable-instances in order for this to work. Also, the types of things you pass to isString have to be completely determined. Typing "isString 5" on the ghci prompt will fail, because ghc will not be able to exactly tell which type "5" is without context, which it needs to in order to determine which instance of "isString" applies. Note that if you made String an instance of Num, then 5 might really represent a string, and the situation would be completely ambiguous. Typing isString (5 :: Integer) will work, and return False. Now here's another question: why do you want it? I can't imagine a reasonable use of this function which wouldn't be better handled in some other way. In fact, the only place I've seen this desire before is in deciding whether to apply putStrLn or print to a value such that no quotation marks are printed. In that case, you should seriously be sure that you can't tell the difference between these statically. Normally in code, where you are prepared to type putStrLn or print, you know the type that's involved, and it only takes half a second to think about which of the two to apply. Sometimes you genuinely want to print a string rather than using putStrLn, as it converts nonprinting characters into escapes, and clearly shows whether there are spaces at the end of the string, for instance. - Cale On 13/10/05, Huong Nguyen <hiperfume@gmail.com> 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@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe