{-# LANGUAGE ForeignFunctionInterface #-}

module SimpleFFI( main ) where

{- |
Module      :   SimpleFFI
Description :   Introduction to FFI
Copyright   :   (c) Thomas Lang, "Real World Haskell"
License     :   BSD3

Portability :   portable
Stability   :   stable

This module gives a short introduction of using the
Foreign Function Interface in Haskell, which is used
for using things defined in other programming languages
like C.
-}


{-
 - to use all that stuff, we have to import
 - the Foreign module and additional ones:
 -
 - Foreign.C.Types
 - Foreign.C.Ptr
 - Foreign.C.String
 - Foreign.Marshal.Array
 -}
import Foreign
import Foreign.C.Types


{-
 - calling now the Sinus function
 - defined in "math.h"
 -}
foreign import ccall "math.h sin"
    c_sin :: CDouble -> CDouble


-- using the above bound c_sin and
-- wrapping it to Haskell's native types
fastsin :: Double -> Double
fastsin x = realToFrac (c_sin (realToFrac x))


-- main function: calculating sinus of every 10 steps
main = mapM_ (print . fastsin) [0/10, 1/10 .. 10/10 ]
