Building.NET GUIs for Haskell applications Beatriz Alarcón Jiménez [email protected].

22
Building .NET Building .NET GUIs for GUIs for Haskell Haskell applications applications Beatriz Alarcón Jiménez [email protected]

Transcript of Building.NET GUIs for Haskell applications Beatriz Alarcón Jiménez [email protected].

Building .NET Building .NET GUIs for GUIs for Haskell Haskell

applicationsapplicationsBeatriz Alarcón Jiménez

[email protected]

.NET Technologies 2006.NET Technologies 2006

OutlineOutline

IntroductionIntroduction Overview of .NET graphic controlsOverview of .NET graphic controls A simple case studyA simple case study Interoperability by means of COM in Interoperability by means of COM in

HaskellHaskell Integration of COM into .NETIntegration of COM into .NET A .NET version of Mu-termA .NET version of Mu-term Conclusions and future workConclusions and future work

.NET Technologies 2006.NET Technologies 2006

IntroductionIntroduction

.NET: a new framework for Software .NET: a new framework for Software DevelopmentDevelopment

Functional languages: HaskellFunctional languages: Haskell Advantages: powerful features for Advantages: powerful features for

developing softwaredeveloping software Disadvantages: lack of an Integrated Disadvantages: lack of an Integrated

Development Environment (IDE) to build Development Environment (IDE) to build GUIsGUIs

COM (Component Object Model)COM (Component Object Model)

.NET Technologies 2006.NET Technologies 2006

IntroductionIntroduction

From Haskell to .NET through COMFrom Haskell to .NET through COM Starting point: Haskell DirectStarting point: Haskell Direct

A framework for Haskell FFI (Foreign Fuction A framework for Haskell FFI (Foreign Fuction Interface) based on the standard IDL (Interface Interface) based on the standard IDL (Interface Definition Language)Definition Language)

COMcomponent

DLL DLL COMCOM

Haskell

component

.NET Technologies 2006.NET Technologies 2006

Overview of .NET graphic Overview of .NET graphic controlscontrols

System.Windows.Forms namespaceSystem.Windows.Forms namespace .NET controls and data.NET controls and data

Button, GroupBox, Panel, Label, Button, GroupBox, Panel, Label, SplitterSplitter

--

CheckBox, RadioButtonCheckBox, RadioButton BoolBool

ListBoxListBox ([Int],[String])([Int],[String])

ComboBoxComboBox (Int,[String])(Int,[String])

ListViewListView [[String]][[String]]

TrackBar, ProgressBar, TrackBar, ProgressBar, NumericUpDownNumericUpDown

IntInt

TextBox, Rich TextBoxTextBox, Rich TextBox StringString

MainMenu, OpenFileDialog, MainMenu, OpenFileDialog, SaveFileDialog, FolderBrowserDialogSaveFileDialog, FolderBrowserDialog

__

.NET Technologies 2006.NET Technologies 2006

A simple case studyA simple case study

Simple graphic interface to introduce Simple graphic interface to introduce and manipulate strings by means of and manipulate strings by means of simple transformations:simple transformations:

• Converting the characters of the string into Converting the characters of the string into capital or small letterscapital or small letters• Removing spare blank spacesRemoving spare blank spaces• Simple encryption (Caesar’s method)Simple encryption (Caesar’s method)

.NET Technologies 2006.NET Technologies 2006

A simple case studyA simple case study In the Haskell part (Hlist.hs):In the Haskell part (Hlist.hs):

type Focus = Int type Length = Int data HL = H_L [(String, Length)] Focus deriving Show

addPair :: HL -> String -> HL -- Adds a new string and its lengthgetString :: HL -> String -- Obtains the `current' stringwriteString :: HL -> String -> HL -- Updates the `current' stringgetLength :: HL -> Int -- Length of the `current' stringsetFocus :: HL -> Int -> HL -- Sets the (index of) `current' stringtoUpperCase :: String -> String

toLowerCase :: String -> StringdeleteB :: String -> Stringencrypt :: String -> String

.NET Technologies 2006.NET Technologies 2006

Interoperability by Interoperability by means of COM in Haskellmeans of COM in Haskell

A Haskell program that implements a COM A Haskell program that implements a COM component consists of four parts:component consists of four parts:

The application code, written in Haskell by the The application code, written in Haskell by the programmerprogrammer

An IDL specification describing the Haskell functions which An IDL specification describing the Haskell functions which we want to make accesible through the COM interfacewe want to make accesible through the COM interface

A set of Haskell modules which are automatically A set of Haskell modules which are automatically generated from the IDL by the HDirect toolgenerated from the IDL by the HDirect tool

A Haskell library module, Com, that exports all the A Haskell library module, Com, that exports all the functions needed to support COM objects in Haskell and a functions needed to support COM objects in Haskell and a C library module that provides some Run-Time Support C library module that provides some Run-Time Support (RTS)(RTS)

.NET Technologies 2006.NET Technologies 2006

A Haskell COM A Haskell COM componentcomponent

Example.idl

EXAMPLE.hsEXAMPLE.hs

ExampleProxy.hs

ExampleProxy.hs

Com.lhs(library)

Com.lhs(library)

HDirect

HDirect

RTS RTS Hlist.hs

.NET Technologies 2006.NET Technologies 2006

The IDL of the Haskell The IDL of the Haskell ComponentComponent

IDL is a declarative language to describe the IDL is a declarative language to describe the interface of a componentinterface of a component

In the IDL specification we declare (the In the IDL specification we declare (the profiles of the Haskell) functions we wish to profiles of the Haskell) functions we wish to have accesible from C# code have accesible from C# code

The interface for the function deleteB The interface for the function deleteB extracted from example.idl:extracted from example.idl:

HRESULT deleteB();HRESULT deleteB();

.NET Technologies 2006.NET Technologies 2006

Encapsulating a Haskell Encapsulating a Haskell component as a COM component as a COM

componentcomponent Once the IDL has been specified, the next step is Once the IDL has been specified, the next step is

to generate the proxy (ExampleProxy.hs, to generate the proxy (ExampleProxy.hs, automatically) and the automatically) and the skeletonskeleton of the of the component component

Regarding the definition of the Regarding the definition of the skeletonskeleton, HDirect , HDirect accomplishes the following tasks:accomplishes the following tasks: To import the necessary Haskell modulesTo import the necessary Haskell modules To introduce a To introduce a State State type to implement the persistence type to implement the persistence

of the funtional data by means of a mutable variableof the funtional data by means of a mutable variable To include Haskell declarations corresponding to the To include Haskell declarations corresponding to the

funtions defined in the IDLfuntions defined in the IDL

.NET Technologies 2006.NET Technologies 2006

Encapsulating a Haskell Encapsulating a Haskell component as a COM component as a COM

componentcomponentmodule module EXAMPLEEXAMPLE where (...) where (...)

import IOExtsimport IOExtsimport qualified Hlist --Pure Haskell Componentimport qualified Hlist --Pure Haskell Component

data State = State(IORef HList.HL)data State = State(IORef HList.HL)

deleteB :: StatedeleteB :: State -> Prelude.IO ()-> Prelude.IO ()deleteB (State st) = do {deleteB (State st) = do { hl <- readIORef sthl <- readIORef st ; str' <- Prelude.return (HList.deleteB (HList.getString hl)); str' <- Prelude.return (HList.deleteB (HList.getString hl)) ; writeIORef st (HList.writeString str' hl); writeIORef st (HList.writeString str' hl)}}

(…)(…)

.NET Technologies 2006.NET Technologies 2006

Creating a COM DLL from Creating a COM DLL from Haskell modulesHaskell modules

After compiling these modules, it is After compiling these modules, it is necessary to provide a Main module.necessary to provide a Main module.

Once the module Main has been compiled, Once the module Main has been compiled, we use HDirect to build the type library we use HDirect to build the type library (.tlb) from the IDL and the proxy (.tlb) from the IDL and the proxy (example.tlb)(example.tlb)

We must bind the type library to our DLLWe must bind the type library to our DLL Now, we can build the DLL including all Now, we can build the DLL including all

compiled modules of the applicationcompiled modules of the application

.NET Technologies 2006.NET Technologies 2006

Integration of COM Integration of COM into .NETinto .NET

Register the DLL (regsvr32.exe)Register the DLL (regsvr32.exe) The data types, error management,…are The data types, error management,…are

different for different for managedmanaged and and unmanagedunmanaged objectsobjects

TlbimpTlbimp is a console application that converts is a console application that converts the type definitions in a COM type library into the type definitions in a COM type library into equivalent .NET assembly definitions equivalent .NET assembly definitions

Now, in VS .NET, we can add the generated Now, in VS .NET, we can add the generated assembly as reference in our Windows assembly as reference in our Windows applicationapplication

.NET Technologies 2006.NET Technologies 2006

Integration of COM Integration of COM into .NETinto .NET

In the C# component, we create an In the C# component, we create an instance of the classinstance of the class

ExampleClass h=new ExampleClass();ExampleClass h=new ExampleClass(); Haskell functions can be accessed as Haskell functions can be accessed as

if they were C# functionsif they were C# functions

.NET Technologies 2006.NET Technologies 2006

private void update()private void update(){{

this.index=this.cbTextList.SelectedIndex;this.index=this.cbTextList.SelectedIndex;string str=h.string str=h.getStringgetString();();

this.tbEntryText.Text=str;this.tbEntryText.Text=str;this.tbLength.Text=h.this.tbLength.Text=h.getLengthgetLength().ToString();().ToString();

this.cbTextList.Items.RemoveAt(index);this.cbTextList.Items.RemoveAt(index);this.cbTextList.Items.Insert(index,str);this.cbTextList.Items.Insert(index,str);

this.cbTextList.Text=str;this.cbTextList.Text=str;}}

private void bdeleteBln_Click(object sender, private void bdeleteBln_Click(object sender, System.EventArgs e)System.EventArgs e)

{{h.h.deleteBdeleteB();();this.update();this.update();

}}

Example.csExample.cs

.NET Technologies 2006.NET Technologies 2006

A .NET version of Mu-A .NET version of Mu-termterm

Mu-term is a termination proof tool for Mu-term is a termination proof tool for (Context-Sensitive) Rewriting Systems(Context-Sensitive) Rewriting Systems

Mu-term is written in Haskell and Mu-term is written in Haskell and wxHaskell was used to develop the wxHaskell was used to develop the graphical user interfacegraphical user interface

The system consists of around 30 The system consists of around 30 Haskell modules containing more than Haskell modules containing more than 5000 lines of code5000 lines of code

.NET Technologies 2006.NET Technologies 2006

Main window of Mu-termMain window of Mu-term

Buttons TextBox

MainMenu,OpenFileDialog,SaveFileDialog

GroupBoxTrackBar

ComboBoxCheckBox

.NET Technologies 2006.NET Technologies 2006

ListView

TextBox

ListBox

Other windows of Mu-termOther windows of Mu-term

.NET Technologies 2006.NET Technologies 2006

A .NET version of Mu-A .NET version of Mu-termterm

We have developed a new (hybrid) We have developed a new (hybrid) version of Mu-term, which has the same version of Mu-term, which has the same functionality but a new GUI written in functionality but a new GUI written in C#C#

Mu-term is available at: Mu-term is available at: http://www.dsic.upv.es/~slucas/csr/termination/mutehttp://www.dsic.upv.es/~slucas/csr/termination/muterm/rm/

.NET Technologies 2006.NET Technologies 2006

ConclusionsConclusions

We have shown how to integrate We have shown how to integrate software components developed in software components developed in Haskell together with (graphic) Haskell together with (graphic) components developed in a .NET components developed in a .NET language such as C#language such as C#

We have demonstrated the We have demonstrated the practicality of this approach by giving practicality of this approach by giving a new .NET GUI to Mu-terma new .NET GUI to Mu-term

.NET Technologies 2006.NET Technologies 2006

Thanks for your attention!Thanks for your attention!