{-# LANGUAGE ForeignFunctionInterface #-}

-- |
-- Copyright   : Anders Claesson 2013
-- Maintainer  : Anders Claesson <anders.claesson@gmail.com>
--

module Sym.Perm.Simple
    (
     simple
    ) where

import Sym.Perm
import Foreign
import Foreign.C.Types
import System.IO.Unsafe

foreign import ccall unsafe "simple.h simple" c_simple
    :: Ptr CLong -> CLong -> CInt

-- | Is the permutation simple?
simple :: Perm -> Bool
simple :: Perm -> Bool
simple Perm
w = CInt -> Bool
forall a. (Eq a, Num a) => a -> Bool
toBool (CInt -> Bool) -> (IO CInt -> CInt) -> IO CInt -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IO CInt -> CInt
forall a. IO a -> a
unsafePerformIO (IO CInt -> Bool) -> IO CInt -> Bool
forall a b. (a -> b) -> a -> b
$
    let n :: CLong
n = Int -> CLong
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Perm -> Int
forall a. Size a => a -> Int
size Perm
w)
    in Perm -> (Ptr CLong -> IO CInt) -> IO CInt
forall a. Perm -> (Ptr CLong -> IO a) -> IO a
unsafeWith Perm
w ((Ptr CLong -> IO CInt) -> IO CInt)
-> (Ptr CLong -> IO CInt) -> IO CInt
forall a b. (a -> b) -> a -> b
$ \Ptr CLong
ptr -> CInt -> IO CInt
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (CInt -> IO CInt) -> CInt -> IO CInt
forall a b. (a -> b) -> a -> b
$ Ptr CLong -> CLong -> CInt
c_simple Ptr CLong
ptr CLong
n