Funny type.
Jay Cox
sqrtofone@yahoo.com
Sun, 27 May 2001 22:46:37 -0500
One day I was playing around with types and I came across this type:
>data S m a = Nil | Cons a (m (S m a))
The idea being one could use generic functions with whatever monad m (of
course
m doesn't need to be a monad but my original idea was to be able to make
mutable lists with some sort of monad m.)
Anyway in attempting to define a generic show instance for the above
datatype I finally came upon:
>instance (Show a, Show (m (S m a))) => Show (S m a) where
> show Nil = "Nil"
> show (Cons x y) = "Cons " ++ show x ++ " " ++ show y
which boggles my mind. But hugs +98 and ghci
-fallow-undecidable-instances both allow it to compile but when i try
>show s
on
>s = Cons 3 [Cons 4 [], Cons 5 [Cons 2 [],Cons 3 []]]
(btw yes we are "nesting" an arbitrary lists here! however structurally
it really isnt much different from any tree datatype)
we get
ERROR -
*** The type checker has reached the cutoff limit while trying to
*** determine whether:
*** Show (S [] Integer)
*** can be deduced from:
*** ()
*** This may indicate that the problem is undecidable. However,
*** you may still try to increase the cutoff limit using the -c
*** option and then try again. (The current setting is -c998)
funny thing is apparently if you set -c to an odd number (on hugs)
it gives
*** The type checker has reached the cutoff limit while trying to
*** determine whether:
*** Show Integer
*** can be deduced from:
*** ()
why would it try to deduce Show integer?
Anyway, is my instance declaration still a bit mucked up?
Also, could there be a way to give a definition of show for S [] a?
Heres my sample definition just in case anybody is curious.
>myshow Nil = "Nil"
>myshow (Cons x y) = "Cons "++ show x ++
> " [" ++ blowup y ++ "]"
> where blowup (x:y:ls) = myshow x ++ "," ++ blowup (y:ls)
> blowup (x:[]) = myshow x
> blowup [] = ""
Thanks,
Jay Cox
yet another non-academic person on this list.