220 likes | 412 Views
Building .NET GUIs for Haskell applications. Beatriz Alarcón Jiménez balarcon@dsic.upv.es. Outline. Introduction Overview of .NET graphic controls A simple case study Interoperability by means of COM in Haskell Integration of COM into .NET A .NET version of Mu-term
E N D
Building .NET GUIs for Haskell applications Beatriz Alarcón Jiménez balarcon@dsic.upv.es
Outline • Introduction • Overview of .NET graphic controls • A simple case study • Interoperability by means of COM in Haskell • Integration of COM into .NET • A .NET version of Mu-term • Conclusions and future work .NET Technologies 2006
Introduction • .NET: a new framework for Software Development • Functional languages: Haskell • Advantages: powerful features for developing software • Disadvantages: lack of an Integrated Development Environment (IDE) to build GUIs • COM (Component Object Model) .NET Technologies 2006
Introduction • From Haskell to .NET through COM • Starting point: Haskell Direct A framework for Haskell FFI (Foreign Fuction Interface) based on the standard IDL (Interface Definition Language) DLL COM COM component Haskell component .NET Technologies 2006
Overview of .NET graphic controls • System.Windows.Forms namespace • .NET controls and data .NET Technologies 2006
A simple case study • Simple graphic interface to introduce and manipulate strings by means of simple transformations: • Converting the characters of the string into capital or small letters • Removing spare blank spaces • Simple encryption (Caesar’s method) .NET Technologies 2006
A simple case study • 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 length getString :: HL -> String -- Obtains the `current' string writeString :: HL -> String -> HL -- Updates the `current' string getLength :: HL -> Int -- Length of the `current' string setFocus :: HL -> Int -> HL -- Sets the (index of) `current' string toUpperCase :: String -> String toLowerCase :: String -> String deleteB :: String -> String encrypt :: String -> String .NET Technologies 2006
Interoperability by means of COM in Haskell • A Haskell program that implements a COM component consists of four parts: • The application code, written in Haskell by the programmer • An IDL specification describing the Haskell functions which we want to make accesible through the COM interface • A set of Haskell modules which are automatically generated from the IDL by the HDirect tool • A Haskell library module, Com, that exports all the functions needed to support COM objects in Haskell and a C library module that provides some Run-Time Support (RTS) .NET Technologies 2006
Example.idl HDirect ExampleProxy.hs EXAMPLE.hs Com.lhs (library) RTS A Haskell COM component Hlist.hs .NET Technologies 2006
The IDL of the Haskell Component • IDL is a declarative language to describe the interface of a component • In the IDL specification we declare (the profiles of the Haskell) functions we wish to have accesible from C# code • The interface for the function deleteB extracted from example.idl: HRESULT deleteB(); .NET Technologies 2006
Encapsulating a Haskell component as a COM component • Once the IDL has been specified, the next step is to generate the proxy (ExampleProxy.hs, automatically) and the skeleton of the component • Regarding the definition of the skeleton, HDirect accomplishes the following tasks: • To import the necessary Haskell modules • To introduce a State type to implement the persistence of the funtional data by means of a mutable variable • To include Haskell declarations corresponding to the funtions defined in the IDL .NET Technologies 2006
Encapsulating a Haskell component as a COM component module EXAMPLE where (...) import IOExts import qualified Hlist --Pure Haskell Component data State = State(IORef HList.HL) deleteB :: State -> Prelude.IO () deleteB (State st) = do { hl <- readIORef st ; str' <- Prelude.return (HList.deleteB (HList.getString hl)) ; writeIORef st (HList.writeString str' hl) } (…) .NET Technologies 2006
Creating a COM DLL from Haskell modules • After compiling these modules, it is necessary to provide a Main module. • Once the module Main has been compiled, we use HDirect to build the type library (.tlb) from the IDL and the proxy (example.tlb) • We must bind the type library to our DLL • Now, we can build the DLL including all compiled modules of the application .NET Technologies 2006
Integration of COM into .NET • Register the DLL (regsvr32.exe) • The data types, error management,…are different for managed and unmanaged objects • Tlbimp is a console application that converts the type definitions in a COM type library into equivalent .NET assembly definitions • Now, in VS .NET, we can add the generated assembly as reference in our Windows application .NET Technologies 2006
Integration of COM into .NET • In the C# component, we create an instance of the class • ExampleClass h=new ExampleClass(); • Haskell functions can be accessed as if they were C# functions .NET Technologies 2006
Example.cs private void update() { this.index=this.cbTextList.SelectedIndex; string str=h.getString(); this.tbEntryText.Text=str; this.tbLength.Text=h.getLength().ToString(); this.cbTextList.Items.RemoveAt(index); this.cbTextList.Items.Insert(index,str); this.cbTextList.Text=str; } private void bdeleteBln_Click(object sender, System.EventArgs e) { h.deleteB(); this.update(); } .NET Technologies 2006
A .NET version of Mu-term • Mu-term is a termination proof tool for (Context-Sensitive) Rewriting Systems • Mu-term is written in Haskell and wxHaskell was used to develop the graphical user interface • The system consists of around 30 Haskell modules containing more than 5000 lines of code .NET Technologies 2006
MainMenu, OpenFileDialog, SaveFileDialog CheckBox ComboBox TrackBar GroupBox Buttons TextBox • Main window of Mu-term .NET Technologies 2006
ListView ListBox TextBox • Other windows of Mu-term .NET Technologies 2006
A .NET version of Mu-term • We have developed a new (hybrid) version of Mu-term, which has the same functionality but a new GUI written in C# • Mu-term is available at: http://www.dsic.upv.es/~slucas/csr/termination/muterm/ .NET Technologies 2006
Conclusions • We have shown how to integrate software components developed in Haskell together with (graphic) components developed in a .NET language such as C# • We have demonstrated the practicality of this approach by giving a new .NET GUI to Mu-term .NET Technologies 2006
Thanks for your attention! .NET Technologies 2006