2

How can I use any user data type which derives from Show with Debug.Observe.observe?

data MyData = Foo | Bar deriving (Show)

myFunc = consumMyData . observe "Debug: " . produceMyData
consumMyData :: MyData -> ...
produceMyData :: ... -> MyData

main = runO $ myFunc

So I can avoid writing:

instance Observable MyData where
    observer my = send (show my) (return my)

all the time, when all I need is the data name.

Observable instance are only defined for all standard data types but Show.

Or if I had something like:

unshow :: Unshow a => String -> a

I could do:

myFunc consumMyData . unshow . oberserve "Debug: " . show . produceMyData

3
  • How about something like an "observeShow :: Show a => String -> a -> a" which forces "(show my)" but returns only "my" and only when "my" is actually used Sep 29, 2010 at 19:52
  • I misunderstood earlier, prior comment deleted. What about this function observeShow a = send (show a) (return a)? Untested, but it should work.
    – John L
    Sep 30, 2010 at 14:12
  • Of course, it will only work if you can call the function directly, not if you need an Observable instance.
    – John L
    Sep 30, 2010 at 14:14

1 Answer 1

2

First of all, I would strongly recommend that you just provide all the necessary Observable instances. You can use DrIFT, which supports Observable directly, or Derive to automatically derive the instances so you don't need to write them yourself.

The alternatives are less savory.

unshow :: Unshow a => String -> a

This is provided by the Read class. The single operation is read, which has exactly the type you give. The functions read and show should be inverses such that read . show is equivalent to id. Most standard data types provide Read, although third-party libraries may not.

This is going to be much less efficient than an Observable instance because you'll be deserializing all your data every time you observe it. If you have data of any significant size this will be a big hit.

The other alternative is to create an Observable instance for every type.

instance Show a => Observable a where
    observer my = send (show my) (return my)

Again, this does not mean "When 'a' is a member of Show, use this Observable instance." It means "Use this Observable instance for everything, and it's an error if there isn't a Show instance in scope". This requires a lot of somewhat sketchy extensions and leads to problems with overlapping instances.

If you care enough to use Hood, I don't see why you would want to have this instance.

Edit:

It appears that type conversion has a higher precedence than function application: putStrLn . show $ read "Foo"::MyData -- error putStrLn . show $ (read "Foo"::MyData)

This is a fairly common problem. Consider the function:

f1 :: String -> String
f1 = show . read

GHC won't allow this. read :: Read a => String -> a and show :: Show a => a -> String, but when you combine the two, GHC can't figure out what type to instantiate the a type variable at. In other words, you have a string, you're deserializing it then re-serializing it, but what type are you deserializing to? You need to tell GHC by using an explicit type signature for either read or show, which is what your second example does. This isn't type conversion, it's that you've written code the type inferencer can't figure out without help.

4
  • I appears that type conversion has a higher precedence than function application: putStrLn . show $ read "Foo"::MyData -- error putStrLn . show $ (read "Foo"::MyData) Sep 29, 2010 at 19:27
  • It's easier sometimes to use "read . observe . show" than to instance classes, especially when performance isn't an issue. Sep 29, 2010 at 19:35
  • But wouldn't "instance Show a => Observable a" only limit the use of observe for Show instances within the module scope? Sep 29, 2010 at 19:38
  • This instance means you will only be able to use observe for types with Show instances in scope, but the type checker will still consider it a valid instance for all types. Class contexts don't have an effect on type checking or inferencing until the very end of the process.
    – John L
    Sep 29, 2010 at 20:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.