Sunday, September 25, 2005

Functional Programming With A Haskell Interpreter: Hugs 98 For The .NET Platform

Some weeks ago I attended two classes of Elias Samra a Linux IPN Community Distinguished Member, and he spoke about the Functional Programing with the Haskell Language which has another point of view about giving solutions to specifics problems.

Since then I realized how can I apply the Functional Programming with Haskell to the near future projects of my consulting services firm, so then I started to do a little research about it.

As you know I'm a fan about the .NET Platform so then I decided to try some Haskell interpreter for this technology so I googled and found the Hugs98 for .NET ( http://galois.com/~sof/hugs98.net ), I installed it and run it, it gave me a nice command console... something like this:

Hugs98 - http://haskell.org/hugs - March 2003
Haskell 98 mode: Restart with command line option -98 to enable extensions
Hugs session for:
Type :? for help
Prelude>

And I started to program with the samples that it had, I was amazed that it can use the classes that reside in the namespace System.Windows.Forms, for example :

module Forms where

{-
This example uses DotNet actions to access .NET
rather than the FFI, as it was written before
FFI support was added to hugs98.net. Feel free
to upgrade it! :)
-}

import Dotnet

type Control a = Object a
type Config a = Control a -> IO ()

build :: IO ()
build = do
frm <- mkCtrl "System.Windows.Forms.Form" [option setSize (200, 200)] btn <- mkCtrl "System.Windows.Forms.Button" [option setText "Click Me", option setSize (50,50), option setLocation (75,75)] frm `addCtrl` btn event btn "Click" (\_ _ -> msgBox "Hello!" "Congratulations, you're running Haskell code!")
invokeStatic "System.Windows.Forms.Application" "Run" frm

option :: (Control a -> b -> IO()) -> b -> Config a
option f val = \ob -> f ob val

mkCtrl :: String -> [Config a] -> IO (Control a)
mkCtrl ctrlType options = do
ctrl <- newObj ctrlType () sequence_ (map (\x-> x ctrl) options)
return ctrl

event :: Control a -> String -> (Object a -> Object b -> IO ()) -> IO()
event ctrl name func = do
delegate <- newDelegator func () <- ctrl # invoke ("add_" ++ name) delegate return () setSize :: Control a -> (Int, Int) -> IO ()
setSize ctrl (width, height) = do
bWidth <- boxValue width bHeight <- boxValue height () <- ctrl # invoke "set_Width" bWidth () <- ctrl # invoke "set_Height" bHeight return () setText :: Control a -> String -> IO ()
setText ctrl text = do
() <- ctrl # invoke "set_Text" text return () setLocation :: Control a -> (Int, Int) -> IO ()
setLocation ctrl (x,y) = do
bX <- boxValue x bY <- boxValue y () <- ctrl # invoke "set_Left" bX () <- ctrl # invoke "set_Top" bY return () add :: Object a -> Object a -> IO ()
add collection thing = do
() <- collection # invoke "Add" thing return () addCtrl :: Control a -> Control a -> IO ()
addCtrl parent child = do
ctrls <- getControls parent () <- add ctrls child return () getControls :: Control a -> IO (Object a)
getControls frm = do
ctrls <- frm # invoke "get_Controls" ()
return ctrls

Again I can prove the interoperability whit the .NET Platform and the Hugs98 interpreter that target it, I will do more complex programs that implements Functional Programming and I will post my results.

Again I invite you to try the Functional Programming and take a look at: http://galois.com/~sof/hugs98.net/
http://haskell.org/hugs/


Victor Romero

Messenger: vic_romero@hotmail.com
Business Web Page: http://www.dsnibble.com.mx
Personal Web Page: http://linux.ipn.mx/cms/space/VictorRomero
Blog Web Page: http://elcarteldetux.blogspotcom/

No comments: