PropLang: Difference between revisions

From HaskellWiki
No edit summary
(Categorize)
 
(6 intermediate revisions by one other user not shown)
Line 1: Line 1:
[[Category:User interfaces]] [[Category:Proposals]] [[Category:Libraries]]
A design for a GUI library which is more like Haskell and less like C. To be written over Gtk2Hs.
A design for a GUI library which is more like Haskell and less like C. To be written over Gtk2Hs.


Line 5: Line 6:
== Thoughts by ==
== Thoughts by ==


Neil Mitchell, Duncan Coutts
Neil Mitchell, some patches from Joachim Breitner, discussions and help from Duncan Coutts


== The Var concept ==
For a darcs repo see: http://www.cs.york.ac.uk/fp/darcs/proplang/


This is the low level stuff, on which the library will be built
In particular check out Sample.hs, I am particularly proud of the word count:


  data Var a = ...
  sb!text =< with1 (txt!text) (\x ->
data Notify = ...
    "Word count: " ++ show (length $ words x))


get :: Var a -> IO a
== Overview ==
set :: Var a -> a -> IO ()
addNotify :: Var a -> (a -> IO ()) -> IO Notify
remNotify :: Notify -> IO ()


newVar :: a -> IO (Var a)
There are events, you can register to be notified when an event fires by doing


== A concrete implementation of Var ==
event += handler


One thing to avoid, global state that needs a specific init function
There are variables, which have events which fire, and a value which you can set and get.


  data Var a = Var {value :: IORef a,
  variable <- newVar "value"
                  notifyId :: IORef Integer,
variable += handler
                  notifys :: IORef [(Integer, a -> IO ())],
variable -< "value"
                  source :: IORef (Maybe Notify)}
x <- getVal variable


data Notify = Notify {notifyId :: Integer, notifys :: IORef [(Integer, a -> IO ())]}
There are objects, objects have properties. A property is just a variable.


  newVar :: a -> IO (Var a)
  window!text += handler
  newVar x = do v <- newIORef x
  window!text -< "Titlebar text"
              n <- newIORef []
              c <- newIORef 0
              a <- newIORef Nothing
              return $ Var v c n a


get :: Var a -> IO a
There are bindings which relate variables.
get var = readIORef (value var)


  set :: Var a -> a -> IO ()
  -- the window text is the variable
  set var x = do n <- readIORef (notifys var)
  window!text =<= variable
                writeIORef (value var) x
-- the window text is the variable, but upppercase
                mapM_ (\(a,b) -> b x) n
window!text =< with1 variable (map toUpper)
-- combine the two texts to create the window text
window!text =< with2 variable1 variable2 (++)


addNotify :: Var a -> (a -> IO ()) -> IO Notify
If any of the values on the RHS of a binding change, then the LHS is updated.
addNotify var f = do n <- readIORef (notifys var)
                      c <- readIORef (notifyId var)
                      writeIORef (notifyId var) (c+1)
                      writeIORef (notifys var) ((c, f) : n)
                      return $ Notify c (notifys var)
 
remNotify :: Notify -> IO ()
remNotify notify = do n <- readIORef (notifys notify)
                      writeIORef (notifys notify) (filter (\x -> fst x /= notifyId notify) n)
 
== Object layering ==
 
textBox~text -< "test"
filename <- newVar Nothing
addNotify filename hatCover
lbl~text =< with filename $ \x ->
                  case x of
                      Nothing -> "Select a file"
                      Just x -> "Loaded: " ++ x
 
 
where
 
(~) :: GtkObject -> GtkProp -> Var a -- ignoring lots of details here
(-<) :: Var a -> a -> IO ()
(=<) :: Var a -> Action a b -> IO ()
 
with :: Var a -> (a -> b) -> Action a b
 
 
== Implementation of Object Layering ==
 
data Action a b = Action (Var a) (a -> b)
 
with :: Var a -> (a -> b) -> Action a b
with var f = Action var f
 
(-<) :: Var a -> a -> IO ()
(-<) var val = set var val
 
(=<) :: Var a -> Action a -> IO ()
(=<) dest (Act src f) = do srcOld <- readIORef (source dest)
                            when (isJust srcOld) $ remNotify (fromJust srcOld)
                            note <- addNotify src (\x -> set dest (f x))
                            writeIORef (source dest) (Just note)
 
== Implementation of Gtk Layering ==
 
class TextProvider a where
    text :: a -> Var String
 
instance TextProvider TextView where
    text x = ...
 
txt :: TextView
 
(!) :: a -> (a -> b) -> b
object ! prop = prop object
 
txt!text :: Var String

Latest revision as of 21:21, 23 June 2007

A design for a GUI library which is more like Haskell and less like C. To be written over Gtk2Hs.

Link: http://www.cse.unsw.edu.au/~chak/haskell/ports/

Thoughts by

Neil Mitchell, some patches from Joachim Breitner, discussions and help from Duncan Coutts

For a darcs repo see: http://www.cs.york.ac.uk/fp/darcs/proplang/

In particular check out Sample.hs, I am particularly proud of the word count:

sb!text =< with1 (txt!text) (\x ->
    "Word count: " ++ show (length $ words x))

Overview

There are events, you can register to be notified when an event fires by doing

event += handler

There are variables, which have events which fire, and a value which you can set and get.

variable <- newVar "value"
variable += handler
variable -< "value"
x <- getVal variable

There are objects, objects have properties. A property is just a variable.

window!text += handler
window!text -< "Titlebar text"

There are bindings which relate variables.

-- the window text is the variable
window!text =<= variable
-- the window text is the variable, but upppercase
window!text =< with1 variable (map toUpper)
-- combine the two texts to create the window text
window!text =< with2 variable1 variable2 (++)

If any of the values on the RHS of a binding change, then the LHS is updated.