Malcolm Wallace <[EMAIL PROTECTED]> writes
>> And for (head []) there's really no useful info to hand. So I'm stumped.
> Although you can't show the value (because there isn't one), you can
> at least show the type of the expression:
Printing the type is very desirable in this situation.
But I did not know it is possible!
> class ShowType a where
> [..]
> instance ShowType a => ShowType [a] where
> showType xs = "[" ++ showType x ++ "]"
> where ~(x:_) = xs
> [..]
!!
Indeed, try
------------------------------------------------------------
class ShowType a where showsType :: a -> String -> String
instance ShowType Char where showsType _ = ("Char"++)
instance ShowType a => ShowType [a]
where
showsType xs = ('[':) . showsType x . (']':) where ~(x:_) = xs
myhead :: ShowType a => [a] -> a
myhead xs@[] = error $ ("myhead [], [] :: "++) $ showsType xs "\n"
myhead (x:_) = x
main = let xss = ["ab","cd"]
in putStr $ shows (myhead $ tail $ tail xss) "\n"
-------------------------------------------
And it says
Fail: myhead [], [] :: [[Char]]
But this is very good! Thank you.
My program exploits certain showsDomOf, similar as your showsType,
and could not sensibly display "empty" things, like Vector [].
Looks like ~(x:_) gives access to a constant instance operation
on the type of an element of xs even when xs is empty.
We need to apply op to any element of the type of the elements of
xs. op depends really on the type, not on the value inside type.
Now, xs occurs empty. Still ~(x:_) finds the needed element to
apply op to.
Looks like it works. But I do not understand the whole thing, so far.
------------------
Sergey Mechveliani
[EMAIL PROTECTED]