module Data.Number.Flint.NMod.Poly.Instances (
    NModPoly (..)
  , module GHC.Exts
) where

import Test.QuickCheck

import GHC.Exts

import System.IO.Unsafe
import Control.Monad

import Foreign.Ptr
import Foreign.C.String
import Foreign.Storable
import Foreign.Marshal.Alloc (free)
import Foreign.Marshal.Array (advancePtr)

import Data.Number.Flint.NMod.Poly
import Data.Number.Flint.NMod.Poly.Factor

import Data.Number.Flint.UFD

instance Show NModPoly where
  show :: NModPoly -> String
show NModPoly
p = (NModPoly, String) -> String
forall a b. (a, b) -> b
snd ((NModPoly, String) -> String) -> (NModPoly, String) -> String
forall a b. (a -> b) -> a -> b
$ IO (NModPoly, String) -> (NModPoly, String)
forall a. IO a -> a
unsafePerformIO (IO (NModPoly, String) -> (NModPoly, String))
-> IO (NModPoly, String) -> (NModPoly, String)
forall a b. (a -> b) -> a -> b
$ do
    NModPoly -> (Ptr CNModPoly -> IO String) -> IO (NModPoly, String)
forall {a}. NModPoly -> (Ptr CNModPoly -> IO a) -> IO (NModPoly, a)
withNModPoly NModPoly
p ((Ptr CNModPoly -> IO String) -> IO (NModPoly, String))
-> (Ptr CNModPoly -> IO String) -> IO (NModPoly, String)
forall a b. (a -> b) -> a -> b
$ \Ptr CNModPoly
p -> do
      String -> (CString -> IO String) -> IO String
forall a. String -> (CString -> IO a) -> IO a
withCString String
"x" ((CString -> IO String) -> IO String)
-> (CString -> IO String) -> IO String
forall a b. (a -> b) -> a -> b
$ \CString
x -> do
        CString
cs <- Ptr CNModPoly -> CString -> IO CString
nmod_poly_get_str_pretty Ptr CNModPoly
p CString
x
        String
s <- CString -> IO String
peekCString CString
cs
        CString -> IO ()
forall a. Ptr a -> IO ()
free CString
cs
        String -> IO String
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return String
s

instance Num NModPoly where
  * :: NModPoly -> NModPoly -> NModPoly
(*) = (Ptr CNModPoly -> Ptr CNModPoly -> Ptr CNModPoly -> IO ())
-> NModPoly -> NModPoly -> NModPoly
forall {a}.
(Ptr CNModPoly -> Ptr CNModPoly -> Ptr CNModPoly -> IO a)
-> NModPoly -> NModPoly -> NModPoly
lift2 Ptr CNModPoly -> Ptr CNModPoly -> Ptr CNModPoly -> IO ()
nmod_poly_mul
  + :: NModPoly -> NModPoly -> NModPoly
(+) = (Ptr CNModPoly -> Ptr CNModPoly -> Ptr CNModPoly -> IO ())
-> NModPoly -> NModPoly -> NModPoly
forall {a}.
(Ptr CNModPoly -> Ptr CNModPoly -> Ptr CNModPoly -> IO a)
-> NModPoly -> NModPoly -> NModPoly
lift2 Ptr CNModPoly -> Ptr CNModPoly -> Ptr CNModPoly -> IO ()
nmod_poly_add
  (-) = (Ptr CNModPoly -> Ptr CNModPoly -> Ptr CNModPoly -> IO ())
-> NModPoly -> NModPoly -> NModPoly
forall {a}.
(Ptr CNModPoly -> Ptr CNModPoly -> Ptr CNModPoly -> IO a)
-> NModPoly -> NModPoly -> NModPoly
lift2 Ptr CNModPoly -> Ptr CNModPoly -> Ptr CNModPoly -> IO ()
nmod_poly_sub
  abs :: NModPoly -> NModPoly
abs = NModPoly -> NModPoly
forall a. HasCallStack => a
undefined
  signum :: NModPoly -> NModPoly
signum = NModPoly -> NModPoly
forall a. HasCallStack => a
undefined
  fromInteger :: Integer -> NModPoly
fromInteger = Integer -> NModPoly
forall a. HasCallStack => a
undefined
  
instance Semigroup NModPoly where
  <> :: NModPoly -> NModPoly -> NModPoly
(<>) = (Ptr CNModPoly -> Ptr CNModPoly -> Ptr CNModPoly -> IO ())
-> NModPoly -> NModPoly -> NModPoly
forall {a}.
(Ptr CNModPoly -> Ptr CNModPoly -> Ptr CNModPoly -> IO a)
-> NModPoly -> NModPoly -> NModPoly
lift2 Ptr CNModPoly -> Ptr CNModPoly -> Ptr CNModPoly -> IO ()
nmod_poly_compose

instance Eq NModPoly where
  == :: NModPoly -> NModPoly -> Bool
(==) NModPoly
x NModPoly
y = (NModPoly, Bool) -> Bool
forall a b. (a, b) -> b
snd ((NModPoly, Bool) -> Bool) -> (NModPoly, Bool) -> Bool
forall a b. (a -> b) -> a -> b
$ (NModPoly, (NModPoly, Bool)) -> (NModPoly, Bool)
forall a b. (a, b) -> b
snd ((NModPoly, (NModPoly, Bool)) -> (NModPoly, Bool))
-> (NModPoly, (NModPoly, Bool)) -> (NModPoly, Bool)
forall a b. (a -> b) -> a -> b
$ IO (NModPoly, (NModPoly, Bool)) -> (NModPoly, (NModPoly, Bool))
forall a. IO a -> a
unsafePerformIO (IO (NModPoly, (NModPoly, Bool)) -> (NModPoly, (NModPoly, Bool)))
-> IO (NModPoly, (NModPoly, Bool)) -> (NModPoly, (NModPoly, Bool))
forall a b. (a -> b) -> a -> b
$ do
    NModPoly
-> (Ptr CNModPoly -> IO (NModPoly, Bool))
-> IO (NModPoly, (NModPoly, Bool))
forall {a}. NModPoly -> (Ptr CNModPoly -> IO a) -> IO (NModPoly, a)
withNModPoly NModPoly
x ((Ptr CNModPoly -> IO (NModPoly, Bool))
 -> IO (NModPoly, (NModPoly, Bool)))
-> (Ptr CNModPoly -> IO (NModPoly, Bool))
-> IO (NModPoly, (NModPoly, Bool))
forall a b. (a -> b) -> a -> b
$ \Ptr CNModPoly
x ->
      NModPoly -> (Ptr CNModPoly -> IO Bool) -> IO (NModPoly, Bool)
forall {a}. NModPoly -> (Ptr CNModPoly -> IO a) -> IO (NModPoly, a)
withNModPoly NModPoly
y ((Ptr CNModPoly -> IO Bool) -> IO (NModPoly, Bool))
-> (Ptr CNModPoly -> IO Bool) -> IO (NModPoly, Bool)
forall a b. (a -> b) -> a -> b
$ \Ptr CNModPoly
y -> do
        CInt
f <- Ptr CNModPoly -> Ptr CNModPoly -> IO CInt
nmod_poly_equal Ptr CNModPoly
x Ptr CNModPoly
y
        Bool -> IO Bool
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool -> IO Bool) -> Bool -> IO Bool
forall a b. (a -> b) -> a -> b
$ CInt
f CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
== CInt
1

instance Ord NModPoly where
  compare :: NModPoly -> NModPoly -> Ordering
compare = NModPoly -> NModPoly -> Ordering
forall a. HasCallStack => a
undefined
  
instance Real NModPoly where
  toRational :: NModPoly -> Rational
toRational = NModPoly -> Rational
forall a. HasCallStack => a
undefined

instance Enum NModPoly where
  toEnum :: Int -> NModPoly
toEnum = Int -> NModPoly
forall a. HasCallStack => a
undefined
  fromEnum :: NModPoly -> Int
fromEnum = NModPoly -> Int
forall a. HasCallStack => a
undefined
  
instance Integral NModPoly where
  toInteger :: NModPoly -> Integer
toInteger = NModPoly -> Integer
forall a. HasCallStack => a
undefined
  div :: NModPoly -> NModPoly -> NModPoly
div NModPoly
x NModPoly
y = IO NModPoly -> NModPoly
forall a. IO a -> a
unsafePerformIO (IO NModPoly -> NModPoly) -> IO NModPoly -> NModPoly
forall a b. (a -> b) -> a -> b
$ do
    (NModPoly
_, CMpLimb
n) <- NModPoly -> (Ptr CNModPoly -> IO CMpLimb) -> IO (NModPoly, CMpLimb)
forall {a}. NModPoly -> (Ptr CNModPoly -> IO a) -> IO (NModPoly, a)
withNModPoly NModPoly
x Ptr CNModPoly -> IO CMpLimb
nmod_poly_modulus
    (NModPoly
_, CMpLimb
m) <- NModPoly -> (Ptr CNModPoly -> IO CMpLimb) -> IO (NModPoly, CMpLimb)
forall {a}. NModPoly -> (Ptr CNModPoly -> IO a) -> IO (NModPoly, a)
withNModPoly NModPoly
y Ptr CNModPoly -> IO CMpLimb
nmod_poly_modulus
    Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (CMpLimb
n CMpLimb -> CMpLimb -> Bool
forall a. Eq a => a -> a -> Bool
/= CMpLimb
m) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ String -> IO ()
forall a. HasCallStack => String -> a
error String
"modulus does not agree."
    NModPoly
p <- CMpLimb -> IO NModPoly
newNModPoly CMpLimb
n
    NModPoly
q <- CMpLimb -> IO NModPoly
newNModPoly CMpLimb
n
    NModPoly
-> (Ptr CNModPoly -> IO (NModPoly, (NModPoly, ())))
-> IO (NModPoly, (NModPoly, (NModPoly, ())))
forall {a}. NModPoly -> (Ptr CNModPoly -> IO a) -> IO (NModPoly, a)
withNModPoly NModPoly
x ((Ptr CNModPoly -> IO (NModPoly, (NModPoly, ())))
 -> IO (NModPoly, (NModPoly, (NModPoly, ()))))
-> (Ptr CNModPoly -> IO (NModPoly, (NModPoly, ())))
-> IO (NModPoly, (NModPoly, (NModPoly, ())))
forall a b. (a -> b) -> a -> b
$ \Ptr CNModPoly
x ->
      NModPoly
-> (Ptr CNModPoly -> IO (NModPoly, ()))
-> IO (NModPoly, (NModPoly, ()))
forall {a}. NModPoly -> (Ptr CNModPoly -> IO a) -> IO (NModPoly, a)
withNModPoly NModPoly
y ((Ptr CNModPoly -> IO (NModPoly, ()))
 -> IO (NModPoly, (NModPoly, ())))
-> (Ptr CNModPoly -> IO (NModPoly, ()))
-> IO (NModPoly, (NModPoly, ()))
forall a b. (a -> b) -> a -> b
$ \Ptr CNModPoly
y ->
        NModPoly -> (Ptr CNModPoly -> IO ()) -> IO (NModPoly, ())
forall {a}. NModPoly -> (Ptr CNModPoly -> IO a) -> IO (NModPoly, a)
withNModPoly NModPoly
q ((Ptr CNModPoly -> IO ()) -> IO (NModPoly, ()))
-> (Ptr CNModPoly -> IO ()) -> IO (NModPoly, ())
forall a b. (a -> b) -> a -> b
$ \Ptr CNModPoly
q ->
          Ptr CNModPoly -> Ptr CNModPoly -> Ptr CNModPoly -> IO ()
nmod_poly_div Ptr CNModPoly
q Ptr CNModPoly
x Ptr CNModPoly
y
    NModPoly -> IO NModPoly
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return NModPoly
q
  quotRem :: NModPoly -> NModPoly -> (NModPoly, NModPoly)
quotRem NModPoly
x NModPoly
y = IO (NModPoly, NModPoly) -> (NModPoly, NModPoly)
forall a. IO a -> a
unsafePerformIO (IO (NModPoly, NModPoly) -> (NModPoly, NModPoly))
-> IO (NModPoly, NModPoly) -> (NModPoly, NModPoly)
forall a b. (a -> b) -> a -> b
$ do
    (NModPoly
_, CMpLimb
n) <- NModPoly -> (Ptr CNModPoly -> IO CMpLimb) -> IO (NModPoly, CMpLimb)
forall {a}. NModPoly -> (Ptr CNModPoly -> IO a) -> IO (NModPoly, a)
withNModPoly NModPoly
x Ptr CNModPoly -> IO CMpLimb
nmod_poly_modulus
    (NModPoly
_, CMpLimb
m) <- NModPoly -> (Ptr CNModPoly -> IO CMpLimb) -> IO (NModPoly, CMpLimb)
forall {a}. NModPoly -> (Ptr CNModPoly -> IO a) -> IO (NModPoly, a)
withNModPoly NModPoly
y Ptr CNModPoly -> IO CMpLimb
nmod_poly_modulus
    Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (CMpLimb
n CMpLimb -> CMpLimb -> Bool
forall a. Eq a => a -> a -> Bool
/= CMpLimb
m) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ String -> IO ()
forall a. HasCallStack => String -> a
error String
"modulus does not agree."
    NModPoly
p <- CMpLimb -> IO NModPoly
newNModPoly CMpLimb
n 
    NModPoly
q <- CMpLimb -> IO NModPoly
newNModPoly CMpLimb
n
    NModPoly
-> (Ptr CNModPoly -> IO (NModPoly, (NModPoly, (NModPoly, ()))))
-> IO (NModPoly, (NModPoly, (NModPoly, (NModPoly, ()))))
forall {a}. NModPoly -> (Ptr CNModPoly -> IO a) -> IO (NModPoly, a)
withNModPoly NModPoly
x ((Ptr CNModPoly -> IO (NModPoly, (NModPoly, (NModPoly, ()))))
 -> IO (NModPoly, (NModPoly, (NModPoly, (NModPoly, ())))))
-> (Ptr CNModPoly -> IO (NModPoly, (NModPoly, (NModPoly, ()))))
-> IO (NModPoly, (NModPoly, (NModPoly, (NModPoly, ()))))
forall a b. (a -> b) -> a -> b
$ \Ptr CNModPoly
x ->
      NModPoly
-> (Ptr CNModPoly -> IO (NModPoly, (NModPoly, ())))
-> IO (NModPoly, (NModPoly, (NModPoly, ())))
forall {a}. NModPoly -> (Ptr CNModPoly -> IO a) -> IO (NModPoly, a)
withNModPoly NModPoly
y ((Ptr CNModPoly -> IO (NModPoly, (NModPoly, ())))
 -> IO (NModPoly, (NModPoly, (NModPoly, ()))))
-> (Ptr CNModPoly -> IO (NModPoly, (NModPoly, ())))
-> IO (NModPoly, (NModPoly, (NModPoly, ())))
forall a b. (a -> b) -> a -> b
$ \Ptr CNModPoly
y ->
        NModPoly
-> (Ptr CNModPoly -> IO (NModPoly, ()))
-> IO (NModPoly, (NModPoly, ()))
forall {a}. NModPoly -> (Ptr CNModPoly -> IO a) -> IO (NModPoly, a)
withNModPoly NModPoly
p ((Ptr CNModPoly -> IO (NModPoly, ()))
 -> IO (NModPoly, (NModPoly, ())))
-> (Ptr CNModPoly -> IO (NModPoly, ()))
-> IO (NModPoly, (NModPoly, ()))
forall a b. (a -> b) -> a -> b
$ \Ptr CNModPoly
p ->
          NModPoly -> (Ptr CNModPoly -> IO ()) -> IO (NModPoly, ())
forall {a}. NModPoly -> (Ptr CNModPoly -> IO a) -> IO (NModPoly, a)
withNModPoly NModPoly
q ((Ptr CNModPoly -> IO ()) -> IO (NModPoly, ()))
-> (Ptr CNModPoly -> IO ()) -> IO (NModPoly, ())
forall a b. (a -> b) -> a -> b
$ \Ptr CNModPoly
q ->
            Ptr CNModPoly
-> Ptr CNModPoly -> Ptr CNModPoly -> Ptr CNModPoly -> IO ()
nmod_poly_divrem Ptr CNModPoly
p Ptr CNModPoly
q Ptr CNModPoly
x Ptr CNModPoly
y
    (NModPoly, NModPoly) -> IO (NModPoly, NModPoly)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (NModPoly
p, NModPoly
q)

instance UFD NModPoly where
  factor :: NModPoly -> [(NModPoly, Int)]
factor NModPoly
x = (NModPolyFactor, [(NModPoly, Int)]) -> [(NModPoly, Int)]
forall a b. (a, b) -> b
snd ((NModPolyFactor, [(NModPoly, Int)]) -> [(NModPoly, Int)])
-> (NModPolyFactor, [(NModPoly, Int)]) -> [(NModPoly, Int)]
forall a b. (a -> b) -> a -> b
$ (NModPoly, (NModPolyFactor, [(NModPoly, Int)]))
-> (NModPolyFactor, [(NModPoly, Int)])
forall a b. (a, b) -> b
snd ((NModPoly, (NModPolyFactor, [(NModPoly, Int)]))
 -> (NModPolyFactor, [(NModPoly, Int)]))
-> (NModPoly, (NModPolyFactor, [(NModPoly, Int)]))
-> (NModPolyFactor, [(NModPoly, Int)])
forall a b. (a -> b) -> a -> b
$ IO (NModPoly, (NModPolyFactor, [(NModPoly, Int)]))
-> (NModPoly, (NModPolyFactor, [(NModPoly, Int)]))
forall a. IO a -> a
unsafePerformIO (IO (NModPoly, (NModPolyFactor, [(NModPoly, Int)]))
 -> (NModPoly, (NModPolyFactor, [(NModPoly, Int)])))
-> IO (NModPoly, (NModPolyFactor, [(NModPoly, Int)]))
-> (NModPoly, (NModPolyFactor, [(NModPoly, Int)]))
forall a b. (a -> b) -> a -> b
$ do
    NModPoly
-> (Ptr CNModPoly -> IO (NModPolyFactor, [(NModPoly, Int)]))
-> IO (NModPoly, (NModPolyFactor, [(NModPoly, Int)]))
forall {a}. NModPoly -> (Ptr CNModPoly -> IO a) -> IO (NModPoly, a)
withNModPoly NModPoly
x ((Ptr CNModPoly -> IO (NModPolyFactor, [(NModPoly, Int)]))
 -> IO (NModPoly, (NModPolyFactor, [(NModPoly, Int)])))
-> (Ptr CNModPoly -> IO (NModPolyFactor, [(NModPoly, Int)]))
-> IO (NModPoly, (NModPolyFactor, [(NModPoly, Int)]))
forall a b. (a -> b) -> a -> b
$ \Ptr CNModPoly
x -> do
      CMpLimb
m <- Ptr CNModPoly -> IO CMpLimb
nmod_poly_modulus Ptr CNModPoly
x
      NModPolyFactor
f <- IO NModPolyFactor
newNModPolyFactor
      NModPolyFactor
-> (Ptr CNModPolyFactor -> IO [(NModPoly, Int)])
-> IO (NModPolyFactor, [(NModPoly, Int)])
forall {a}.
NModPolyFactor
-> (Ptr CNModPolyFactor -> IO a) -> IO (NModPolyFactor, a)
withNModPolyFactor NModPolyFactor
f ((Ptr CNModPolyFactor -> IO [(NModPoly, Int)])
 -> IO (NModPolyFactor, [(NModPoly, Int)]))
-> (Ptr CNModPolyFactor -> IO [(NModPoly, Int)])
-> IO (NModPolyFactor, [(NModPoly, Int)])
forall a b. (a -> b) -> a -> b
$ \Ptr CNModPolyFactor
f -> do
        Ptr CNModPolyFactor -> Ptr CNModPoly -> IO CMpLimb
nmod_poly_factor Ptr CNModPolyFactor
f Ptr CNModPoly
x
        (CNModPolyFactor Ptr CNModPoly
d Ptr CLong
e CLong
n CLong
alloc) <- Ptr CNModPolyFactor -> IO CNModPolyFactor
forall a. Storable a => Ptr a -> IO a
peek Ptr CNModPolyFactor
f
        [Int] -> (Int -> IO (NModPoly, Int)) -> IO [(NModPoly, Int)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [Int
0..CLong -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral CLong
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1] ((Int -> IO (NModPoly, Int)) -> IO [(NModPoly, Int)])
-> (Int -> IO (NModPoly, Int)) -> IO [(NModPoly, Int)]
forall a b. (a -> b) -> a -> b
$ \Int
j -> do
          CLong
m <- Ptr CLong -> IO CLong
forall a. Storable a => Ptr a -> IO a
peek (Ptr CLong
e Ptr CLong -> Int -> Ptr CLong
forall a. Storable a => Ptr a -> Int -> Ptr a
`advancePtr` Int
j)
          NModPoly
r <- CMpLimb -> IO NModPoly
newNModPoly (CLong -> CMpLimb
forall a b. (Integral a, Num b) => a -> b
fromIntegral CLong
m)
          NModPoly -> (Ptr CNModPoly -> IO ()) -> IO (NModPoly, ())
forall {a}. NModPoly -> (Ptr CNModPoly -> IO a) -> IO (NModPoly, a)
withNModPoly NModPoly
r ((Ptr CNModPoly -> IO ()) -> IO (NModPoly, ()))
-> (Ptr CNModPoly -> IO ()) -> IO (NModPoly, ())
forall a b. (a -> b) -> a -> b
$ \Ptr CNModPoly
r -> Ptr CNModPoly -> Ptr CNModPoly -> IO ()
nmod_poly_set Ptr CNModPoly
r (Ptr CNModPoly
d Ptr CNModPoly -> Int -> Ptr CNModPoly
forall a. Storable a => Ptr a -> Int -> Ptr a
`advancePtr` Int
j)
          (NModPoly, Int) -> IO (NModPoly, Int)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (NModPoly
r, CLong -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral CLong
m)

lift2 :: (Ptr CNModPoly -> Ptr CNModPoly -> Ptr CNModPoly -> IO a)
-> NModPoly -> NModPoly -> NModPoly
lift2 Ptr CNModPoly -> Ptr CNModPoly -> Ptr CNModPoly -> IO a
f NModPoly
x NModPoly
y = IO NModPoly -> NModPoly
forall a. IO a -> a
unsafePerformIO (IO NModPoly -> NModPoly) -> IO NModPoly -> NModPoly
forall a b. (a -> b) -> a -> b
$ do
  (NModPoly
_, CMpLimb
n) <- NModPoly -> (Ptr CNModPoly -> IO CMpLimb) -> IO (NModPoly, CMpLimb)
forall {a}. NModPoly -> (Ptr CNModPoly -> IO a) -> IO (NModPoly, a)
withNModPoly NModPoly
x Ptr CNModPoly -> IO CMpLimb
nmod_poly_modulus
  (NModPoly
_, CMpLimb
m) <- NModPoly -> (Ptr CNModPoly -> IO CMpLimb) -> IO (NModPoly, CMpLimb)
forall {a}. NModPoly -> (Ptr CNModPoly -> IO a) -> IO (NModPoly, a)
withNModPoly NModPoly
y Ptr CNModPoly -> IO CMpLimb
nmod_poly_modulus
  Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (CMpLimb
n CMpLimb -> CMpLimb -> Bool
forall a. Eq a => a -> a -> Bool
/= CMpLimb
m) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ String -> IO ()
forall a. HasCallStack => String -> a
error String
"modulus does not agree."
  NModPoly
result <- CMpLimb -> IO NModPoly
newNModPoly CMpLimb
n
  NModPoly
-> (Ptr CNModPoly -> IO (NModPoly, (NModPoly, a)))
-> IO (NModPoly, (NModPoly, (NModPoly, a)))
forall {a}. NModPoly -> (Ptr CNModPoly -> IO a) -> IO (NModPoly, a)
withNModPoly NModPoly
result ((Ptr CNModPoly -> IO (NModPoly, (NModPoly, a)))
 -> IO (NModPoly, (NModPoly, (NModPoly, a))))
-> (Ptr CNModPoly -> IO (NModPoly, (NModPoly, a)))
-> IO (NModPoly, (NModPoly, (NModPoly, a)))
forall a b. (a -> b) -> a -> b
$ \Ptr CNModPoly
result -> do
    NModPoly
-> (Ptr CNModPoly -> IO (NModPoly, a))
-> IO (NModPoly, (NModPoly, a))
forall {a}. NModPoly -> (Ptr CNModPoly -> IO a) -> IO (NModPoly, a)
withNModPoly NModPoly
x ((Ptr CNModPoly -> IO (NModPoly, a))
 -> IO (NModPoly, (NModPoly, a)))
-> (Ptr CNModPoly -> IO (NModPoly, a))
-> IO (NModPoly, (NModPoly, a))
forall a b. (a -> b) -> a -> b
$ \Ptr CNModPoly
x -> do
      NModPoly -> (Ptr CNModPoly -> IO a) -> IO (NModPoly, a)
forall {a}. NModPoly -> (Ptr CNModPoly -> IO a) -> IO (NModPoly, a)
withNModPoly NModPoly
y ((Ptr CNModPoly -> IO a) -> IO (NModPoly, a))
-> (Ptr CNModPoly -> IO a) -> IO (NModPoly, a)
forall a b. (a -> b) -> a -> b
$ \Ptr CNModPoly
y -> do
        Ptr CNModPoly -> Ptr CNModPoly -> Ptr CNModPoly -> IO a
f Ptr CNModPoly
result Ptr CNModPoly
x Ptr CNModPoly
y
  NModPoly -> IO NModPoly
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return NModPoly
result

lift1 :: (Ptr CNModPoly -> Ptr CNModPoly -> IO a) -> NModPoly -> NModPoly
lift1 Ptr CNModPoly -> Ptr CNModPoly -> IO a
f NModPoly
x = IO NModPoly -> NModPoly
forall a. IO a -> a
unsafePerformIO (IO NModPoly -> NModPoly) -> IO NModPoly -> NModPoly
forall a b. (a -> b) -> a -> b
$ do
  (NModPoly
_, CMpLimb
n) <- NModPoly -> (Ptr CNModPoly -> IO CMpLimb) -> IO (NModPoly, CMpLimb)
forall {a}. NModPoly -> (Ptr CNModPoly -> IO a) -> IO (NModPoly, a)
withNModPoly NModPoly
x Ptr CNModPoly -> IO CMpLimb
nmod_poly_modulus
  NModPoly
result <- CMpLimb -> IO NModPoly
newNModPoly CMpLimb
n
  NModPoly
-> (Ptr CNModPoly -> IO (NModPoly, a))
-> IO (NModPoly, (NModPoly, a))
forall {a}. NModPoly -> (Ptr CNModPoly -> IO a) -> IO (NModPoly, a)
withNModPoly NModPoly
result ((Ptr CNModPoly -> IO (NModPoly, a))
 -> IO (NModPoly, (NModPoly, a)))
-> (Ptr CNModPoly -> IO (NModPoly, a))
-> IO (NModPoly, (NModPoly, a))
forall a b. (a -> b) -> a -> b
$ \Ptr CNModPoly
result ->
    NModPoly -> (Ptr CNModPoly -> IO a) -> IO (NModPoly, a)
forall {a}. NModPoly -> (Ptr CNModPoly -> IO a) -> IO (NModPoly, a)
withNModPoly NModPoly
x ((Ptr CNModPoly -> IO a) -> IO (NModPoly, a))
-> (Ptr CNModPoly -> IO a) -> IO (NModPoly, a)
forall a b. (a -> b) -> a -> b
$ \Ptr CNModPoly
x ->
    Ptr CNModPoly -> Ptr CNModPoly -> IO a
f Ptr CNModPoly
result Ptr CNModPoly
x
  NModPoly -> IO NModPoly
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return NModPoly
result