Graphical User Interfaces in Haskell Koen Lindström Claessen

Download Graphical User Interfaces in Haskell Koen Lindström Claessen

Post on 24-Dec-2015

217 views

Category:

Documents

4 download

Embed Size (px)

TRANSCRIPT

<ul><li> Slide 1 </li> <li> Graphical User Interfaces in Haskell Koen Lindstrm Claessen </li> <li> Slide 2 </li> <li> First a Demo An editor for simple straight line drawings Line currently being drawn by dragging the mouse Remove last line drawn Load and save drawings to files A graphical user interface </li> <li> Slide 3 </li> <li> Saving a Drawing File selection dialogue </li> <li> Slide 4 </li> <li> GUI Libraries GUI programming is complex Much graphics programming needed Many different elements needed (buttons, scroll bars, menus, entry fields) It is not practical to write a GUI from scratch We must use an existing GUI library </li> <li> Slide 5 </li> <li> Portability Every platform provides a GUI library but they are all different! The differences are more than just skin deep How do we write portable programs with GUIs? </li> <li> Slide 6 </li> <li> Two Roads to Portability WindowsMacLinux New portable GUI library Applications The Java approach Interfaces look funny WindowsMacLinux Portable interface Applications Windows GUI lib Mac GUI library Linux GUI lib The wxWindows approach Native look and feel </li> <li> Slide 7 </li> <li> wxHaskell WindowsMacLinux wxWindows Haskell applications Windows GUI lib Mac GUI library Linux GUI lib wxHaskell Industrial strength open source library 500 C++ classes with 4000 methods! There are many native Haskell GUI libraries: More Haskellish Generally not portable Generally less complete wxHaskell is a good compromise. </li> <li> Slide 8 </li> <li> GHC wxHaskell only works with GHC Glasgow Haskell compiler Haskell main program ghc package wx make Prog -o prog Prog.hs Compiled code prog A compiled program like any other Cannot call individual functions in an interpreter </li> <li> Slide 9 </li> <li> Haskell Main Programs Definition main :: IO () In module Main (the default) Main programs produce no result they just do input/output main :: IO () main = do putStrLn What is your name? name </li> <li> Overloading The same attribute name can be used with many different widget types Attribute names are overloaded Type: text :: Textual w =&gt; Attr w String Same idea used for many, many attributes f </li> <li> Slide 15 </li> <li> Layout The layout attribute specifies the appearance on the screen f </li> <li> Slide 16 </li> <li> Instructions Controls have attributes which are instructions to follow if the user activates them A GUIs purpose is to let a user give instructions to the computer but </li> <li> Slide 17 </li> <li> Modifying Attributes Attributes can be read and modified Modifying attributes changes what appears on the screen do s </li> <li> Slide 18 </li> <li> Invisible Widgets Some widgets are invisible They still have attributes that can be set and modified Examples: Timer widget Interval On command Variable widget Value </li> <li> Slide 19 </li> <li> Functions vs. Instructions f :: String -&gt; Int g :: String -&gt; IO Int vs Compare: Only the knowledge about the string is needed to understand the result Anything can be used to compute the result: Reading from files, randomness, user input! Moreover, anything can be modified or changed! </li> <li> Slide 20 </li> <li> Design Principles Separate the GUI from the functionality as much as possible So that the GUI can later be replaced without changing the functionality Avoid duplicating code as far as possible So that errors need be fixed only once (c.f. cut and paste programming) </li> <li> Slide 21 </li> <li> Modelling the State A GUI is often used to edit or modify something What is the state? </li> <li> Slide 22 </li> <li> Examples Hello.hs Simple hello example BouncingBalls.hs A simple animation containing bouncing balls Draw.hs A simple drawing program Mine.hs A version of the game minesweeper </li> <li> Slide 23 </li> <li> How do I find the right function? Haskell has many hierarchical libraries, with documentation on the web Every wxHaskell function is documented on the web Know how to find the documentation! </li> <li> Slide 24 </li> <li> haskell.org Libraries! </li> <li> Slide 25 </li> <li> haskell.org/libraries/ Hierarchical libraries! You have these already. </li> <li> Slide 26 </li> <li> Hierarchical Libraries Lots of modules </li> <li> Slide 27 </li> <li> Googling wxHaskell Here it is! </li> <li> Slide 28 </li> <li> wxHaskell Home Page Documentation </li> <li> Slide 29 </li> <li> wxHaskell Documentation Haddock documentation for wxHaskell General information on wxWidgets sometimes you have to read this </li> <li> Slide 30 </li> <li> wxHaskell Haddock Attributes: defines set, get, (:=), Classes: one for each attribute Controls: Buttons, entry fields etc Dialogs: Built-in popups, e.g. for choosing a file Draw: Functions for drawing on a panel Layout: Operators to build layouts And much, much, more! Ignore WXCore! Its a level below wxHaskell </li> <li> Slide 31 </li> <li> Reading http://wxhaskell.sourceforge.net/ wxHaskell: A Portable and Concise GUI Library for Haskell, Daan Leijen Haskell Workshop, Salt Lake City, 2004 http://www.cs.uu.nl/~daan/download/papers/w xhaskell.pdf </li> </ul>