The mathlink package

[ Tags: bsd3, foreign, library ] [ Propose Tags ]

Makes it easy to write Mathematica packages in Haskell. Just write some functions and provide a package specification in a simple DSL that mimics that of Mathematica's mprep utility.

Data marshaling is accomplished via the MLGet and MLPut classes, which specify types that that can be read from or written to the MathLink connection. Instances of these classes are provided for the obvious standard data types, including tuples, lists, and UArrays.

A Haskell function that is to be exposed to Mathematica has the type signature (MLGet a, MLPut b) => a -> ML b. The ML monad provides single-threaded access to the MathLink connection.

A simple example of a Mathematica package:

import Foreign.MathLink

-- define a function
addTwo :: (Int,Int) -> ML Int
addTwo (x,y) = return $ x+y

-- specify a package
spec :: MLSpec
spec =
    -- start a package definition
  [ Eval $ "BeginPackage":@[St "Test`"]

    -- define a usage message for the public symbol
  , DeclMsg "AddTwo" "usage" "AddTwo[..] adds a pair of numbers"

    -- open a new (private) namespace
  , Eval $ "Begin":@[St "`Private`"]

    -- declare the function
  , DeclFn {
      callPattern = "AddTwo[x_Integer,y_Integer]"
    , argPattern = "{x,y}"
    , func = addTwo

    -- close the namespaces
  , Eval $ "End":@[]
  , Eval $ "EndPackage":@[]

-- runs the MathLink connection
main :: IO ()
main = runMathLink spec


Dependencies array (>=0.2 && <0.4), base (>=4.0 && <4.3), containers (>=0.2 && <0.4), haskell98, ix-shapable, mtl (==1.1.*) [details]
License BSD3
Copyright Copyright (c) Tracy Wadleigh 2009
Author Tracy Wadleigh
Maintainer <>
Category Foreign
Home page
Bug tracker
Source repo head: darcs get
Uploaded Wed Nov 11 15:22:01 UTC 2009 by TracyWadleigh
Distributions NixOS:
Downloads 4164 total (68 in the last 30 days)
Rating (no votes yet) [estimated by rule of succession]
Your Rating
  • λ
  • λ
  • λ
Status Docs not available [build log]
All reported builds failed as of 2016-12-31 [all 7 reports]
Hackage Matrix CI


  • Foreign
    • Foreign.MathLink
      • Foreign.MathLink.Expression
      • Foreign.MathLink.Internal


Maintainer's Corner

For package maintainers and hackage trustees