Difference between revisions of "Phooey"

From HaskellWiki
Jump to navigation Jump to search
Line 10: Line 10:
 
== Introduction ==
 
== Introduction ==
   
GUIs are usually programmed in an "unnatural" style, in that implementation dependencies are inverted, relative to logical dependencies. This reversal results directly from the data-driven (push) orientation of most GUI libraries. While outputs depend on inputs from a user and semantic point of view, the ''push'' style imposes an implementation dependence of inputs on outputs.
+
GUIs are usually programmed in an unnatural style, in that implementation dependencies are inverted, relative to logical dependencies. This reversal results directly from the ''push'' (data-driven) orientation of most GUI libraries. While outputs depend on inputs from a user and semantic point of view, the ''push'' style imposes an implementation dependence of inputs on outputs.
   
 
A second drawback of the push style is that it is ''imperative'' rather than declarative. A GUI program describes actions to update a model and and view in reaction to user input. In contrast to the ''how-to-update'' style of an imperative program, a functional GUI program would express ''what-it-is'' of a model in terms of the inputs and of the view in terms of the model.
 
A second drawback of the push style is that it is ''imperative'' rather than declarative. A GUI program describes actions to update a model and and view in reaction to user input. In contrast to the ''how-to-update'' style of an imperative program, a functional GUI program would express ''what-it-is'' of a model in terms of the inputs and of the view in terms of the model.
   
The questions of ''push-vs-pull'' (dependency inversion) and ''imperative-vs-declarative'' are related. While an imperative GUI program could certainly be written to pull (poll) values from input to model and model to view, thus eliminating the dependency inversion, I don't know how a declarative program could be written in the inverted-dependency style. (If you do, please say so on the [[Talk:Phooey talk page]]).
+
The questions of ''push-vs-pull'' and ''imperative-vs-declarative'' are related. While an imperative GUI program could certainly be written to pull (poll) values from input to model and model to view, thus eliminating the dependency inversion, I don't know how a declarative program could be written in the inverted-dependency style. ([[Talk:Phooey|Do you?]]).
   
A good reason for using push rather than pull in a GUI implementation is that it is typically much more efficient. A simple pull implementation would either waste time recomputing an unchanging model and view, or deal with the complexity of avoiding that recomputation. The push style computes only when inputs change. (Animation negates this advantage of push.)
+
A important reason for using push rather than pull in a GUI implementation is that push is typically much more efficient. A simple pull implementation would either waste time recomputing an unchanging model and view (pegging your CPU for no benefit), or deal with the complexity of avoiding that recomputation. The push style computes only when inputs change. (Animation negates this advantage of push.)
   
Phooey ("'''Ph'''unctional '''oo'''s'''e'''r '''y'''nterfaces") adopts the declarative style, in which outputs are expressed in terms of inputs. Under the hood, however, the implementation is push-based (data-driven). The dependency inversion is handled invisibly by Phooey, so that GUIs may be specified simply and declaratively while still having an efficient implementation. I have taken care to structure Phooey's implementation as simply as possible to make clear how this dependency inversion works (subject of paper in progress). In addition, Phooey supports dynamic input bounds, flexible layout, and mutually-referential widgets.
+
Phooey ("'''Ph'''unctional '''oo'''s'''e'''r '''y'''nterfaces") adopts the declarative style, in which outputs are expressed in terms of inputs. Under the hood, however, the implementation is push-based (data-driven). Phooey performs the dependency inversion invisibly, so that programmers may express GUIs simply and declaratively while still getting an efficient implementation. I have taken care to structure Phooey's implementation as simply as possible to make clear how this dependency inversion works (subject of paper in progress). In addition, Phooey supports dynamic input bounds, flexible layout, and mutually-referential widgets. (The last feature is currently broken.)
   
 
As an example, below is a simple shopping list GUI. The <hask>total</hask> displayed at the bottom of the window always shows the sum of the values of the <hask>apples</hask> and <hask>bananas</hask> input sliders. When a user changes the inputs, the output updates accordingly.
 
As an example, below is a simple shopping list GUI. The <hask>total</hask> displayed at the bottom of the window always shows the sum of the values of the <hask>apples</hask> and <hask>bananas</hask> input sliders. When a user changes the inputs, the output updates accordingly.

Revision as of 21:06, 16 January 2007

Abstract

Phooey is an arrow-based functional UI library for Haskell.

Introduction

GUIs are usually programmed in an unnatural style, in that implementation dependencies are inverted, relative to logical dependencies. This reversal results directly from the push (data-driven) orientation of most GUI libraries. While outputs depend on inputs from a user and semantic point of view, the push style imposes an implementation dependence of inputs on outputs.

A second drawback of the push style is that it is imperative rather than declarative. A GUI program describes actions to update a model and and view in reaction to user input. In contrast to the how-to-update style of an imperative program, a functional GUI program would express what-it-is of a model in terms of the inputs and of the view in terms of the model.

The questions of push-vs-pull and imperative-vs-declarative are related. While an imperative GUI program could certainly be written to pull (poll) values from input to model and model to view, thus eliminating the dependency inversion, I don't know how a declarative program could be written in the inverted-dependency style. (Do you?).

A important reason for using push rather than pull in a GUI implementation is that push is typically much more efficient. A simple pull implementation would either waste time recomputing an unchanging model and view (pegging your CPU for no benefit), or deal with the complexity of avoiding that recomputation. The push style computes only when inputs change. (Animation negates this advantage of push.)

Phooey ("Phunctional ooser ynterfaces") adopts the declarative style, in which outputs are expressed in terms of inputs. Under the hood, however, the implementation is push-based (data-driven). Phooey performs the dependency inversion invisibly, so that programmers may express GUIs simply and declaratively while still getting an efficient implementation. I have taken care to structure Phooey's implementation as simply as possible to make clear how this dependency inversion works (subject of paper in progress). In addition, Phooey supports dynamic input bounds, flexible layout, and mutually-referential widgets. (The last feature is currently broken.)

As an example, below is a simple shopping list GUI. The total displayed at the bottom of the window always shows the sum of the values of the apples and bananas input sliders. When a user changes the inputs, the output updates accordingly.

ui1.png

Phooey is structured as an arrow, and this example uses arrow notation. The code:

ui1 :: UI () ()
ui1 = title "Shopping List" $
      proc () -> do
	a <- title "apples"  (islider 3) -< (0,10)
	b <- title "bananas" (islider 7) -< (0,10)
	title "total" showDisplay        -< a+b

There are more examples in the Haddock docs.

Motivation

Phooey came out of Pajama, which is a re-implementation of the Pan language and compiler for function synthesis of interactive, continuous, infinite images. Pan and Pajama use a monadic style for specifying GUIs and are able to do so because they use the implementation trick of Compiling Embedded Languages, in which one manipulates expressions rather than values. (This trick is mostly transparent, but the illusion shows through in places.)

Switching from expressions to values raises a challenge for a declarative monadic approach. For instance, consider this (problematic) monadic UI.

factOopsUI :: UIM ()
factOopsUI =  do n  <- title "n"   (islider 3 (0,20))
                 title "factorial" (showDisplay (fact n))

If n :: Int, then the second half of the GUI depends on the dynamic run-time values flowing out of the first half, and so must be constructed for each new value of n. The problem is a failure to separate a GUI into its static and dynamic parts. A solution with the monadic framework would be to give n a different type, such as Source Int, which captures the static nature of the input GUI while giving access to a source of dynamic values. Phooey provides this monadic solution in its MonadUI module. The awkwardness is that we can no longer use such simple formulations as factOopsUI above, since expressions like product [1..n] won't type-check. (We can play overloading tricks as in Fran, Pan, and Pajama, but they don't always work.) Instead, we could use return, liftM, liftM2, etc.

factUI :: UIM ()
factUI =  do n  <- title "n"   (islider 3 (return (0,20)))
             title "factorial" (showDisplay (liftM fact n))

Rather than introducing the complexity of the Source type and the need for explicit lifting, we can solve the problem by replacing the Monad abstraction with one that separates static and dynamic aspects. Getting that separation is the point of the Arrow abstraction, and thus Phooey's primary interface is formulated as an arrow rather than a monad. Phooey's UI arrow is implemented on top of its UI monad using a simple, reusable pattern. See the ArrowUI module doc and its source code.

Portability

Phooey is built on wxHaskell. Quoting from the wxHaskell home page

wxHaskell is therefore built on top of wxWidgets -- a comprehensive C++ library that is portable across all major GUI platforms; including GTK, Windows, X11, and MacOS X.

So I expect that Phooey runs on all of these platforms. That said, I have only tried Phooey on Windows. Please give it a try and leave a message on the Talk page ("Discuss this page").