:+: and all that (was Re: Do we need a mouse trap?)

Adrian Hey ahey@iee.org
Thu, 04 Mar 1999 09:44:23 +0000 (GMT)


Ana Maria Abrao wrote:
> My problem is
> the following: I want to glue controls and menu items
> at runtime. My idea was to make a list of strings, each
> one representing a control. Then, I would use a recursive
> program to  sweep through this list, and glue the
> controls together. Since the controls are not of the same
> type, it is not possible to use ListLS

I'm not sure your message really was 'Re: Do we need a mouse trap',so you'll
have to excuse me for replying on a new thread. I'm afraid I'm such a novice
when it comes to ObjectIO I can't answer your question, but I can offer some
sympathy. I also find ObjectIO's use of type constructor classes and the
dreaded :+: operator one of the hardest things to understand. Presumably
this has been done to enable us to do something that we couldn't do with a
simple algebraic data type (but I'm not sure what).

I suppose my biggest problem is that its hard to write type signatures with
type constructor classes. I find that if I don't do this as a matter of
routine I get hopelessly confused later on. For example here's a snippet
from one of my test programs:

//menuItems :: some weird class thing
menuItems =
      MenuItem "New..."  [MenuShortKey 'a', MenuFunction (noLS (appPIO beep))]
  :+: MenuItem "Open..." [MenuShortKey 'b']
  :+: MenuItem "Close"   [MenuShortKey 'c']
  :+: MenuSeparator      []
  :+: MenuItem "Quit"    [MenuShortKey 'd', MenuFunction (noLS (appPIO beep))]

You can probably tell I haven't got very far yet, because all these either
beep or do nothing :-)

If I get the compiler to tell me the type of menuItems I get:

menuItems :: :+: MenuItem (:+: MenuItem (:+: MenuItem (:+: MenuSeparator
              MenuItem))) a *(PSt b c)

Eeeeeek! I wouldn't want to try to figure that out myself, type it in, and
maintain it whenever I changed the menu. I would much rather write something
like:

menuItems = ListLS
  [MenuItem "New..."  [MenuShortKey 'a', MenuFunction (noLS (appPIO beep))]
  ,MenuItem "Open..." [MenuShortKey 'b']
  ,MenuItem "Close"   [MenuShortKey 'c']
  ,MenuSeparator      []
  ,MenuItem "Quit"    [MenuShortKey 'd', MenuFunction (noLS (appPIO beep))]
  ]

This would be fine if it weren't for that darned MenuSeparator. Wouldn't
life be much simpler if an algebraic data type was used (and ListLS
dropped)? I guess the correct answer to that question is _no_, but I
don't really understand why :-) Perhaps it will dawn on me as I become
more expert (less woefully ignorant???) regarding ObjectIO.

Regards
--
Adrian Hey