Personal tools

Salsa

From HaskellWiki

(Difference between revisions)
Jump to: navigation, search
(Removed TODO)
m (Text -> Width)
(One intermediate revision by one user not shown)
Line 37: Line 37:
 
== Using Salsa ==
 
== Using Salsa ==
   
Having a look at the sample programs in the Salsa distribution is probably the best way to learn how to use Salsa at the moment.
+
Having a look at the sample programs in the Salsa distribution is probably the best way to learn how to use Salsa at the moment. There are currently three sample programs available:
  +
  +
* [http://code.haskell.org/Salsa/Samples/Hello Hello]: a basic console 'Hello World' program.
  +
  +
* [http://code.haskell.org/Salsa/Samples/Weather Weather]: a console program that asynchronously downloads the Sydney weather forecast and displays it.
  +
  +
* [http://code.haskell.org/Salsa/Samples/Conway Conway]: a simulator for Conway's Game of Life with a Windows Presentation Foundation GUI. (Requires .NET 3.0 or later.)
  +
  +
=== C# in Haskell ===
   
 
The following table shows some C# code snippets and what they translate to:
 
The following table shows some C# code snippets and what they translate to:
Line 67: Line 67:
 
|-
 
|-
 
| <pre-c>b.Width = 10;</pre-c>
 
| <pre-c>b.Width = 10;</pre-c>
| <hask>set b [_Text :== 10 ]</hask>
+
| <hask>set b [_Width :== 10 ]</hask>
 
| <small>Use <hask>:==</hask> to assign without applying implicit conversions. This is useful for assigning polymorphic numeric literals to properties, since polymorphic-typed values confuse the Salsa coercion machinery.</small>
 
| <small>Use <hask>:==</hask> to assign without applying implicit conversions. This is useful for assigning polymorphic numeric literals to properties, since polymorphic-typed values confuse the Salsa coercion machinery.</small>
 
|-
 
|-
Line 88: Line 88:
 
Also, only a handful of value types are automatically marshaled between Haskell and .NET (these are Int32, String, Bool and Double). All other types are marshaled by reference (which is typically what you want for .NET reference types anyway).
 
Also, only a handful of value types are automatically marshaled between Haskell and .NET (these are Int32, String, Bool and Double). All other types are marshaled by reference (which is typically what you want for .NET reference types anyway).
   
(Patches are very welcome.)
+
(Patches are certainly welcome.)
   
 
== Documentation ==
 
== Documentation ==

Revision as of 13:34, 10 October 2008


Contents

1 About

Salsa is an experimental Haskell library and code generator that allows Haskell programs to host the .NET runtime and interact with .NET libraries. It uses type families extensively to provide a type-safe mapping of the .NET object model in the Haskell type system.

2 Requirements

GHC 6.8
Salsa makes extensive use of type families and thus requires at least version 6.8 of GHC.
Microsoft .NET Framework 3.5
Since the Salsa code generator requires .NET 3.5, any Salsa development also requires this version. Executables produced with Salsa however will run with just .NET 2.0 (provided that only .NET 2.0 assemblies are used by the program).
Salsa will not work with versions 1.0/1.1 of the .NET Framework, or with Mono. (Supporting Mono shouldn't be too much work though.)
Caution!
Salsa is experimental and has not been thoroughly tested. A single typographical error can trigger multiple pages of errors from GHC, or cause it to eat lots of memory.

3 Getting Salsa

You can find Salsa on Hackage. There is also a darcs repository:

darcs get http://code.haskell.org/Salsa

(you’ll need Darcs 2).

4 Using Salsa

Having a look at the sample programs in the Salsa distribution is probably the best way to learn how to use Salsa at the moment. There are currently three sample programs available:

  • Hello: a basic console 'Hello World' program.
  • Weather: a console program that asynchronously downloads the Sydney weather forecast and displays it.
  • Conway: a simulator for Conway's Game of Life with a Windows Presentation Foundation GUI. (Requires .NET 3.0 or later.)

4.1 C# in Haskell

The following table shows some C# code snippets and what they translate to:

C# Salsa Notes
o.ToString()
o # _toString ()
Method names have a leading underscore and lowercase first character.
Console.Write("Hi {0}!", "world");
_Console # _write ("Hi {0}!", "world")
Static methods are called similarly.
Button b = new Button();
b <- new _Button ()
Constructor names have a leading underscore.
s.Length
get s _Length
Use the
get
function to retrieve field and property values. The field/property has a leading underscore.
b.Text = "OK";
set b [_Text := "OK"]
Use the
set
function to set the value of fields and properties. Multiple assignments can be made in the one call.
b.Width = 10;
set b [_Width :== 10 ]
Use
:==
to assign without applying implicit conversions. This is useful for assigning polymorphic numeric literals to properties, since polymorphic-typed values confuse the Salsa coercion machinery.
b.Click += (EventHandler)
  delegate(...) { ... };
set b [_Click :+> delegate _EventHandler (...)]
Use
:+>
to add a handler to an event, and
delegate
to wrap a Haskell function as a .NET delegate.

5 Limitations

There are a number of .NET features that Salsa does not currently support. These include:

  • generic methods and classes
  • multi-dimensional arrays
  • explicit coercions
  • indexer properties
  • exception handling

Also, only a handful of value types are automatically marshaled between Haskell and .NET (these are Int32, String, Bool and Double). All other types are marshaled by reference (which is typically what you want for .NET reference types anyway).

(Patches are certainly welcome.)

6 Documentation

Some of the inner workings of Salsa are described in my undergraduate thesis. In particular, Chapter 8 describes how type families are used to provide a type-safe mapping of the .NET object model in Haskell. If you're considering playing with Salsa, it is probably worth a read:

A .NET Bridge for Haskell: Dancing with the Devil