summaryrefslogtreecommitdiff
path: root/examples/devel/example/wrappers.hs
blob: f4e0f0b8eff1cda068a71b92fec140323d8a0239 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
{-# LANGUAGE ForeignFunctionInterface #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE GADTs #-}

{-
    $ ghc -O2 wrappers.hs functions.c
    $ ./wrappers
-}

import Numeric.LinearAlgebra
import Numeric.LinearAlgebra.Devel
import System.IO.Unsafe(unsafePerformIO)
import Foreign.C.Types(CInt(..))
import Foreign.Ptr(Ptr)


infixl 1 #
a # b = apply a b
{-# INLINE (#) #-}

infixr 5 :>, ::>
type (:>)  t r = CInt -> Ptr t -> r
type (::>) t r =  CInt -> CInt -> CInt -> CInt -> Ptr t -> r
type Ok = IO CInt

-----------------------------------------------------

x = (3><5) [1..]

main = do
    print   x
    print $ myDiag x
    print $ myDiag (tr x)

-----------------------------------------------------
foreign import ccall unsafe "c_diag" cDiag :: Double ::> Double :> Double ::> Ok

myDiag m = unsafePerformIO $ do
    y <- createVector (min r c)
    z <- createMatrix RowMajor r c
    cDiag # m # y # z #| "cDiag"
    return (y,z)
  where
    (r,c) = size m