How to type cast

Dario's right, generally in Haskell you want to create a type class to dispatch on something's type. That being said, there is a type safe way to cast in Haskell. This is part of the Scrap your boilerplate generic programming library that allows you to write the "interesting" parts of manipulations of complex nested data types while SYB fills in the blanks. Brain meltingly awesome. Here's a presentation on it in ppt or html.

Here's what the cast looks like:

cast :: (Typeable a, Typeable b) => a -> Maybe b
ghci> (cast 'a') :: Maybe Char
Just 'a'
ghci> (cast 'a') :: Maybe Bool
Nothing
ghci> (cast True) :: Maybe Bool
Just True

In Haskell, all types that allow a conversion to a string instantiate the Show typeclass which provides

show :: Show a => a -> String

So your whole code is nothing but

f x = show x

or

f = show

with the same generic type f :: Show a => a -> String (Forall types a that are conversible to a string, take a value of this type and return a string).

Note that you don't have to do an explicit, run-time type-check like in C#; the generic template is resolved at compile-time. You don't need a polymorphic root type - A cast like in C# would in fact be somewhat complicated and against the language's conception. Instead of allowing arbitrary casts between types, it defined typeclasses for certain meaningful conversions.

Note that compatibility is checked at compile-time:

-- Working
f 1
f "Hallo"
f (1, 2)
f [1, 2, 3]

-- Not working
f (\x -> x + 1) 

In response to your edited question:

As I said before, arbitrary conversions aren't allowed in Haskell (without very very unsafe code). And since Haskell is not object-oriented, there is no inheritance relationship that required any cast. There simply aren't meaningless object values that needed runtime-checking/casting. For expressing alternatives, you'll have to define a union type, a typeclass or use the Either type.

In what case do you encounter an object that is a Customer or an Order? A value of that type is simply nonsensical. Please clarify again.

As to your logger example: You'll need a typeclass:

class Loggable a where
    writeToLog :: a -> IO ()