WxHaskell Daan Leijen. Ok…. Dat lijkt me een goed plan. Denk je dat je zo iets dinsdag af kunt hebben en presenteren? Doaitse On donderdag, september.

Slides:



Advertisements
Similar presentations
Chapter 16 Graphical User Interfaces
Advertisements

Introduction to Java 2 Programming
Computer Science 112 Fundamentals of Programming II User Interfaces
CE 311 K Introduction to Computer Methods VB Controls and Events Daene C. McKinney.
Mobile Development Introduction to Visual Studio Development Rob Miles Department of Computer Science.
Tk Widgets This material is best on several sources –Slides by Dr. Ernest J. Friedman-Hill –various Tcl/Tk books.
Chapter 16 Graphical User Interfaces John Keyser’s Modifications of Slides by Bjarne Stroustrup
Unit 3 Graphical User Interface (GUI) Dr. Magdi AMER.
Chapter 3 DATA: TYPES, CLASSES, AND OBJECTS. Chapter 3 Data Abstraction Abstract data types allow you to work with data without concern for how the data.
C++ Programming Languages
Lecture 4: Embedded Application Framework Qt Tutorial Cheng-Liang (Paul) Hsieh ECE 424 Embedded Systems Design.
WxErlang Mats-Ola Persson.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
Event Driven Programming and GUIs Part 3 CS221 – 4/15/09.
OpenGL and Projections
Libraries Programs that other people write that help you. #include // enables C++ #include // enables human-readable text #include // enables math functions.
CS510AP Quick Check Monads. QuickCheck Quick check is a Haskell library for doing random testing. You can read more about quickcheck at –
C++ fundamentals.
Graphical User Interfaces in Haskell Koen Lindström Claessen.
Computer Science 111 Fundamentals of Programming I User Interfaces Introduction to GUI programming.
Building.NET GUIs for Haskell applications Beatriz Alarcón Jiménez
Chapter 13 Graphics classes Bjarne Stroustrup
1 Introduction to C# Programming Console applications No visual components Only text output Two types MS-DOS prompt - Used in Windows 95/98/ME Command.
SIMPLE PROBLEM SOLVING in Java: a Problem Set Framework Viera K. Proulx Richard Rasala Jason Jay Rodrigues CCSCNE 2002 Conference.
LAYOUT CONTROLS. XAML Overview XAML : eXtensible Application Markup Language pronounced (ZAMEL) is a markup language used to design user interfaces XML-based.
Object Oriented Software Development 9. Creating Graphical User Interfaces.
Introduction to Building Windows 8.1 & Windows Phone Applications.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Synopsys Custom Designer Tutorial for a chip integration using the University of Utah Standard Cell Libraries In ON Semiconductor 0.5u C5 CMOS Version.
GTK Binding for Alice Robert Grabowski Eine GTK-Schnittstelle für Alice.
COP4020 Programming Languages Names, Scopes, and Bindings Prof. Xin Yuan.
CNS 1410 Graphical User Interfaces. Obectives Students should understand the difference between a procedural program and an Event Driven Program. Students.
C# GUI - Basics. Objectives.NET supports two types: WinForms, traditional, desktop GUI apps. WebForms – newer, for Web apps. Visual Studio.NET supports.
ITEC 109 Lecture 27 GUI. GUIs Review Sounds –Arrays hold sample values –Creating a keyboard –Sound effects Homework 3 –The big two –Due after break –Lab.
Applications Development
1 Introduction to C# Programming Console applications No visual components Only text output Two types MS-DOS prompt - Used in Windows 95/98/ME Command.
Introduction to C#. Why C#? Develop on the Following Platforms ASP.NET Native Windows Windows 8 / 8.1 Windows Phone WPF Android (Xamarin) iOS (Xamarin)
Return to Outline Copyright © 2011 by Maribeth H. Price 3-1 Labeling and annotation.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Graphics Programming with Python. Many choices Python offers us several library choices:  Tkinter  WxPython  PyQt  PyGTK  Jython  And others...
Lab 4 - Variables. Information Hiding General Principle: – Restrict the access to variables and methods as much as possible Can label instance variables.
C++ (intro) Created by Hwansoo Han Edited by Ikjun Yeom.
Creating visual interfaces in python
Side effects A side effect is anything that happens in a method other than computing and/or returning a value. Example: public class hello { public int.
Graphical User Interfaces Tonga Institute of Higher Education.
By Mr. Muhammad Pervez Akhtar
Programmeren 1 6 september 2010 HOORCOLLEGE 2: INTERACTIE EN CONDITIES PROGRAMMEREN 1 6 SEPTEMBER 2009 Software Systems - Programming - Week.
BBT 10 Visual Basic 03 Enrichment. Tip Before creating any files for your project, first create a new folder Save all of your files in the folder Pieces.
FUNCTIONAL PROGRAMING AT WORK - HASKELL AND DOMAIN SPECIFIC LANGUAGES Dr. John Peterson Western State Colorado University.
Graphical User Interface (GUI) Two-Dimensional Graphical Shapes.
CINT & Reflex – The Future CINT’s Future Layout Reflex API Work In Progress: Use Reflex to store dictionary data Smaller memory footprint First step to.
CE EN 547 – BRIGHAM YOUNG UNIVERSITY. 1. Build conceptual model object 2. Build coverages 3. Define grid frame 4. Build grid 5. Initialize MODFLOW 6.
C++ Functions A bit of review (things we’ve covered so far)
C# for C++ Programmers 1.
Topics Graphical User Interfaces Using the tkinter Module
Present Simple VS. Continuous
Human Computer Interaction
Mobile Application Development BSCS-7 Lecture # 8
Programming Languages and Paradigms
AFP - Lecture 2 Domain Specific Embedded Languages
Programming Paradigms
Lecture 2 Domain Specific Embedded Languages
Fundamentals of Python: From First Programs Through Data Structures
An overview of Java, Data types and variables
Computer Science 111 Fundamentals of Programming I User Interfaces
12.4 p 471 a) double[] number = {1.05, 2.05, 3.05, 4.05, 5.05};
6. WinForms 2003 C# GUI - Basics.
Programming Languages and Paradigms
Graphical User Interface
Advanced Functional Programming
Presentation transcript:

wxHaskell Daan Leijen

Ok…. Dat lijkt me een goed plan. Denk je dat je zo iets dinsdag af kunt hebben en presenteren? Doaitse On donderdag, september 18, 2003, at 10:43 AM, Daan Leijen wrote: Nog even over AFP. De turtle-graphics opdracht is volgens mij goed te doen met wxHaskell -- behalve dan dat het geinstalleerd dient te worden. Als je wilt kunnen we misschien wat details afspreken en dat ik dan wat help om de opdracht vorm te geven met 1) een werkend voorbeeld, en 2) concrete vragen.

Overview wxHaskell as a concrete example of FFI, phantom types, existential types, combinator design, inheritance simulation, and IO as first-class values. Homework: implement a "turtle" graphics combinator library.

wxHaskell wxHaskell is a portable GUI library for Haskell build upon the wxWindows (C++) library. Two layers: a "core" binding (WXCore) and a haskellized layer (WX).

Hello world hello :: IO () hello = do f <- frame [text := "Hello!"] quit <- button f [text := "Quit",on command := close f] set f [layout := widget quit]

Layout combinators set f [layout := margin 10 (column 5 [floatCentre (label "Hello"),floatCentre (widget quit) ] ) ]

Demo

What is needed? Foreign Function Interface (FFI) Model inheritance Create abstractions: – Layout combinators – Properties (get/set)

FFI What are the primitives that you need to interface to the imperative world from Haskell?

4 primitive operations are needed. 1. Call the outside world (foreign import) 2. Be called (foreign export) 3. Use foreign data (CInt, Addr) 4. Export haskell data (StablePtr a)

Examples: foreign import sin :: Double -> IO Double foreign import strlen :: Addr -> IO CInt

Phantom types Make "Addr" typesafe: type Ptr a = Addr foreign import strlen :: Ptr Char -> IO Int

Abstraction foreign import strlen :: Ptr Char -> IO Int strLen :: String -> Int strLen s = unsafePerformIO $ withCString s $ \cstr -> strlen cstr

Abstraction withCString :: String -> (Ptr Char -> IO a) -> IO a withCString s f = do p <- malloc (length s+1) mapM_ (poke p) (zip s [0..]) x <- f p free p return x

wxHaskell windowSetLabel :: Window a -> String -> IO () windowSetLabel w txt = withCString txt $ \cstr -> primWindowSetLabel w txt foreign import "windowSetLabel" primWindowSetLabel :: Window a -> Ptr Char -> IO ()

Inheritance How to model inheritance? class Window { void setLabel( const char* txt );.. }; class Frame : public Window { void maximize( void );.. };

Simple model. type Window = Ptr CWindow data CWindow = CWindow type Frame = Ptr CFrame data CFrame = CFrame windowCreate :: IO Window windowSetLabel :: Window -> String -> IO () frameCreate :: IO Frame frameMaximize :: Frame -> IO ()

Conversions? windowFromFrame :: Frame -> Window  do f <- frameCreate windowSetLabel (windowFromFrame f)

Encode inheritance in phantom type type Window a = Ptr (CWindow a) data CWindow a = CWindow type Frame a = Window (CFrame a) data CFrame a = CFrame windowCreate :: IO (Window ()) windowSetLabel :: Window a -> String -> IO () frameCreate :: IO (Frame ()) frameMaximize :: Frame a -> IO ()

It works now do f <- frameCreate windowSetLabel f "Hi" f :: Frame () == Window (CFrame ()) == Ptr (CWindow (CFrame ())) windowSetLabel :: Window a -> String -> IO ()

Properties How can we model the "property" methods? windowGetLabel :: Window a -> IO String windowSetLabel :: Window a -> String -> IO () windowGetLayout :: Window a -> IO Layout windowSetLayout :: Window a -> Layout -> IO ()..

Get/Set We would like to have generic "get" and "set" functions: get :: w -> Attr -> IO a set :: w -> Attr -> a -> IO ()

Typed get/set get :: w -> Attr w a -> IO a set :: w -> Attr w a -> a -> IO () text :: Attr (Window a) String

Attributes data Attr w a = Attr (w -> IO a) (w -> a -> IO ()) text :: Attr (Window a) String text = Attr windowGetLabel windowSetLabel

Generic get/set get :: w -> Attr w a -> IO a get w (Attr getter setter) = getter w set :: w -> Attr w a -> a -> IO () set w (Attr getter setter) x = setter w x

Problems I would like to set many "properties" at once: set frame text "hi" set frame size (sz ) set frame color blue == (?) map (set frame) [text "hi", size (sz ), color blue]

Properties Properties save a value/attribute pair. data Prop w = Prop (w -> IO ()) prop :: Attr w a -> a -> Prop w prop (Attr getter setter) x = Prop (\w -> setter w x) set :: w -> [Prop w] -> IO () set w props = mapM_ setprop props where setprop (Prop setter) = setter w

Now we can set many properties set frame [prop text "hi",prop color blue,prop size (sz ) ]

Problem I want a nicer notation: prop text "hi"  text := "hi"

Existentials data Prop w = (:=) (Attr w a) a  data Prop w = forall a. (:=) (Attr w a) a

New definition of "set" set :: w -> [Prop w] -> IO () set w props = mapM_ setprop props where setprop ((Attr getter setter) := x) = setter w x

Definition of "frame" frame :: [Prop (Frame ())] -> IO (Frame ()) frame props = do f <- frameCreate idAny "" rectNull 0 set f props return f

A combinator language for layout set f [layout := margin 10 (column 5 [floatCentre (label "Hello"),floatCentre (widget quit) ] ) ]

Example: C++ vs. Combinators f <- frame [text "Demo"] ok <- button f [text "Ok"] can <- button f [text "Cancel"] txt <- textCtrl f AlignLeft [clientSize := sz ] set f [layout := margin 10 $ column 10 [ fill $ widget txt, hfill $ row 10 [widget ok, widget can] ]

Abstract data type data Layout windowSetLayout :: Window a -> Layout -> IO () do f <- frameCreate idAny "Test" rectNull 0 ok <- buttonCreate f idAny "Bye" rectNull 0 windowSetLayout f (widget ok)

Primitives widget :: Window a -> Layout label :: String -> Layout space :: Size -> Layout

Containers grid :: Int -> Int -> [[Layout]] -> Layout grid 5 5 [[label "x", widget xinput],[label "y", widget yinput]]

Containers 2 row :: Int -> [Layout] -> Layout column :: Int -> [Layout] -> Layout row n xs = grid n 0 [xs] column n xs = grid 0 n (map (\x -> [x]) xs) column 5 [grid 5 5 [[label "x", widget xinput],[label "y", widget yinput]] row 5 [widget ok, widget cancel]]

Attributes margin :: Int -> Layout -> Layout align :: Align -> Layout -> Layout shaped :: Layout -> Layout expand :: Layout -> Layout

Stretch hstretch, vstretch, stretch :: Layout -> Layout stretch = hstretch. vstretch column 5 $ [grid 5 5 [[label "x", hstretch $ expand $ widget xinput],[label "y", hstretch $ expand $ widget yinput]],stretch $ alignBottomRight $ row 5 [widget ok, widget cancel]]

Many helpers fill = stretch. expand hfill = hstretch. expand floatBottomRight = stretch. alignBottomRight empty = space (sz 0 0) glue = fill empty hglue = hfill empty

Implementation data Layout = Widget (Window ()) | Label String | Grid Int Int [[Layout]]

Transformers data Layout = Widget { options :: Options,..} | Label { options :: Options,..} | Grid { options :: Options,..} data Options = Options{ stretchH :: Bool, stretchV :: Bool, marginW :: Int,.. }

Generate Layout widget w = Widget defaultOptions w label s = Label defaultOptions s hstretch layout = updateOptions (\opt -> opt { stretchH = True }) layout updateOptions f layout = layout{ options = f (options layout) }

Translate Layout windowSetLayout :: Window a -> Layout -> IO () windowSetLayout w (Label options s) = do lab <- staticTextCreate s sizer <- windowSizerCreate lab (flags options) windowAddSizer w sizer.. flags options = (if stretchH options then wxHSTRETCH else 0) + (if stretchV options then wxVSTRETCH else 0) +..

Assignment: Turtle graphics Design and implement an embedding of the LOGO language in Haskell. Motivate your design decisions! Deadline: this Sunday.