text:
{-# LANGUAGE Haskell2010 #-}
{-# LINE 1 "Data/Vector/Unboxed/Base.hs" #-}

{-# LANGUAGE BangPatterns, CPP, MultiParamTypeClasses, TypeFamilies, FlexibleContexts #-}
{-# LANGUAGE DeriveDataTypeable, StandaloneDeriving #-}
{-# OPTIONS_HADDOCK hide #-}

-- |
-- Module      : Data.Vector.Unboxed.Base
-- Copyright   : (c) Roman Leshchinskiy 2009-2010
-- License     : BSD-style
--
-- Maintainer  : Roman Leshchinskiy <rl@cse.unsw.edu.au>
-- Stability   : experimental
-- Portability : non-portable
--
-- Adaptive unboxed vectors: basic implementation
--

module Data.Vector.Unboxed.Base (
  MVector(..), IOVector, STVector, Vector(..), Unbox
) where

import qualified Data.Vector.Generic         as G
import qualified Data.Vector.Generic.Mutable as M

import qualified Data.Vector.Primitive as P

import Control.DeepSeq ( NFData(rnf) )

import Control.Monad.Primitive
import Control.Monad ( liftM )

import Data.Word ( Word8, Word16, Word32, Word64 )
import Data.Int  ( Int8, Int16, Int32, Int64 )
import Data.Complex

import Data.Typeable ( Typeable )

import Data.Data     ( Data(..) )

-- Data.Vector.Internal.Check is unused

data family MVector s a
data family Vector    a

type IOVector = MVector RealWorld
type STVector s = MVector s

type instance G.Mutable Vector = MVector

class (G.Vector Vector a, M.MVector MVector a) => Unbox a

instance NFData (Vector a) where rnf !_ = ()
instance NFData (MVector s a) where rnf !_ = ()

-- -----------------
-- Data and Typeable
-- -----------------
deriving instance Typeable Vector
deriving instance Typeable MVector

instance (Data a, Unbox a) => Data (Vector a) where
  gfoldl       = G.gfoldl
  toConstr _   = error "toConstr"
  gunfold _ _  = error "gunfold"
  dataTypeOf _ = G.mkType "Data.Vector.Unboxed.Vector"
  dataCast1    = G.dataCast

-- ----
-- Unit
-- ----

newtype instance MVector s () = MV_Unit Int
newtype instance Vector    () = V_Unit Int

instance Unbox ()

instance M.MVector MVector () where
  {-# INLINE basicLength #-}
  {-# INLINE basicUnsafeSlice #-}
  {-# INLINE basicOverlaps #-}
  {-# INLINE basicUnsafeNew #-}
  {-# INLINE basicInitialize #-}
  {-# INLINE basicUnsafeRead #-}
  {-# INLINE basicUnsafeWrite #-}
  {-# INLINE basicClear #-}
  {-# INLINE basicSet #-}
  {-# INLINE basicUnsafeCopy #-}
  {-# INLINE basicUnsafeGrow #-}

  basicLength (MV_Unit n) = n

  basicUnsafeSlice _ m (MV_Unit _) = MV_Unit m

  basicOverlaps _ _ = False

  basicUnsafeNew n = return (MV_Unit n)

  -- Nothing to initialize
  basicInitialize _ = return ()

  basicUnsafeRead (MV_Unit _) _ = return ()

  basicUnsafeWrite (MV_Unit _) _ () = return ()

  basicClear _ = return ()

  basicSet (MV_Unit _) () = return ()

  basicUnsafeCopy (MV_Unit _) (MV_Unit _) = return ()

  basicUnsafeGrow (MV_Unit n) m = return $ MV_Unit (n+m)

instance G.Vector Vector () where
  {-# INLINE basicUnsafeFreeze #-}
  basicUnsafeFreeze (MV_Unit n) = return $ V_Unit n

  {-# INLINE basicUnsafeThaw #-}
  basicUnsafeThaw (V_Unit n) = return $ MV_Unit n

  {-# INLINE basicLength #-}
  basicLength (V_Unit n) = n

  {-# INLINE basicUnsafeSlice #-}
  basicUnsafeSlice _ m (V_Unit _) = V_Unit m

  {-# INLINE basicUnsafeIndexM #-}
  basicUnsafeIndexM (V_Unit _) _ = return ()

  {-# INLINE basicUnsafeCopy #-}
  basicUnsafeCopy (MV_Unit _) (V_Unit _) = return ()

  {-# INLINE elemseq #-}
  elemseq _ = seq

-- ---------------
-- Primitive types
-- ---------------

newtype instance MVector s Int = MV_Int (P.MVector s Int)
newtype instance Vector    Int = V_Int  (P.Vector    Int)
instance Unbox Int
instance M.MVector MVector Int where {                                     {-# INLINE basicLength #-}                                            ; {-# INLINE basicUnsafeSlice #-}                                       ; {-# INLINE basicOverlaps #-}                                          ; {-# INLINE basicUnsafeNew #-}                                         ; {-# INLINE basicInitialize #-}                                        ; {-# INLINE basicUnsafeReplicate #-}                                   ; {-# INLINE basicUnsafeRead #-}                                        ; {-# INLINE basicUnsafeWrite #-}                                       ; {-# INLINE basicClear #-}                                             ; {-# INLINE basicSet #-}                                               ; {-# INLINE basicUnsafeCopy #-}                                        ; {-# INLINE basicUnsafeGrow #-}                                        ; basicLength ( MV_Int v) = M.basicLength v                                 ; basicUnsafeSlice i n ( MV_Int v) =  MV_Int $ M.basicUnsafeSlice i n v         ; basicOverlaps ( MV_Int v1) ( MV_Int v2) = M.basicOverlaps v1 v2               ; basicUnsafeNew n =  MV_Int `liftM` M.basicUnsafeNew n                     ; basicInitialize ( MV_Int v) = M.basicInitialize v                         ; basicUnsafeReplicate n x =  MV_Int `liftM` M.basicUnsafeReplicate n x     ; basicUnsafeRead ( MV_Int v) i = M.basicUnsafeRead v i                     ; basicUnsafeWrite ( MV_Int v) i x = M.basicUnsafeWrite v i x               ; basicClear ( MV_Int v) = M.basicClear v                                   ; basicSet ( MV_Int v) x = M.basicSet v x                                   ; basicUnsafeCopy ( MV_Int v1) ( MV_Int v2) = M.basicUnsafeCopy v1 v2           ; basicUnsafeMove ( MV_Int v1) ( MV_Int v2) = M.basicUnsafeMove v1 v2           ; basicUnsafeGrow ( MV_Int v) n =  MV_Int `liftM` M.basicUnsafeGrow v n }
instance G.Vector Vector Int where {                                       {-# INLINE basicUnsafeFreeze #-}                                      ; {-# INLINE basicUnsafeThaw #-}                                        ; {-# INLINE basicLength #-}                                            ; {-# INLINE basicUnsafeSlice #-}                                       ; {-# INLINE basicUnsafeIndexM #-}                                      ; {-# INLINE elemseq #-}                                                ; basicUnsafeFreeze ( MV_Int v) =  V_Int `liftM` G.basicUnsafeFreeze v        ; basicUnsafeThaw ( V_Int v) =  MV_Int `liftM` G.basicUnsafeThaw v            ; basicLength ( V_Int v) = G.basicLength v                                 ; basicUnsafeSlice i n ( V_Int v) =  V_Int $ G.basicUnsafeSlice i n v         ; basicUnsafeIndexM ( V_Int v) i = G.basicUnsafeIndexM v i                 ; basicUnsafeCopy ( MV_Int mv) ( V_Int v) = G.basicUnsafeCopy mv v            ; elemseq _ = seq }

newtype instance MVector s Int8 = MV_Int8 (P.MVector s Int8)
newtype instance Vector    Int8 = V_Int8  (P.Vector    Int8)
instance Unbox Int8
instance M.MVector MVector Int8 where {                                     {-# INLINE basicLength #-}                                            ; {-# INLINE basicUnsafeSlice #-}                                       ; {-# INLINE basicOverlaps #-}                                          ; {-# INLINE basicUnsafeNew #-}                                         ; {-# INLINE basicInitialize #-}                                        ; {-# INLINE basicUnsafeReplicate #-}                                   ; {-# INLINE basicUnsafeRead #-}                                        ; {-# INLINE basicUnsafeWrite #-}                                       ; {-# INLINE basicClear #-}                                             ; {-# INLINE basicSet #-}                                               ; {-# INLINE basicUnsafeCopy #-}                                        ; {-# INLINE basicUnsafeGrow #-}                                        ; basicLength ( MV_Int8 v) = M.basicLength v                                 ; basicUnsafeSlice i n ( MV_Int8 v) =  MV_Int8 $ M.basicUnsafeSlice i n v         ; basicOverlaps ( MV_Int8 v1) ( MV_Int8 v2) = M.basicOverlaps v1 v2               ; basicUnsafeNew n =  MV_Int8 `liftM` M.basicUnsafeNew n                     ; basicInitialize ( MV_Int8 v) = M.basicInitialize v                         ; basicUnsafeReplicate n x =  MV_Int8 `liftM` M.basicUnsafeReplicate n x     ; basicUnsafeRead ( MV_Int8 v) i = M.basicUnsafeRead v i                     ; basicUnsafeWrite ( MV_Int8 v) i x = M.basicUnsafeWrite v i x               ; basicClear ( MV_Int8 v) = M.basicClear v                                   ; basicSet ( MV_Int8 v) x = M.basicSet v x                                   ; basicUnsafeCopy ( MV_Int8 v1) ( MV_Int8 v2) = M.basicUnsafeCopy v1 v2           ; basicUnsafeMove ( MV_Int8 v1) ( MV_Int8 v2) = M.basicUnsafeMove v1 v2           ; basicUnsafeGrow ( MV_Int8 v) n =  MV_Int8 `liftM` M.basicUnsafeGrow v n }
instance G.Vector Vector Int8 where {                                       {-# INLINE basicUnsafeFreeze #-}                                      ; {-# INLINE basicUnsafeThaw #-}                                        ; {-# INLINE basicLength #-}                                            ; {-# INLINE basicUnsafeSlice #-}                                       ; {-# INLINE basicUnsafeIndexM #-}                                      ; {-# INLINE elemseq #-}                                                ; basicUnsafeFreeze ( MV_Int8 v) =  V_Int8 `liftM` G.basicUnsafeFreeze v        ; basicUnsafeThaw ( V_Int8 v) =  MV_Int8 `liftM` G.basicUnsafeThaw v            ; basicLength ( V_Int8 v) = G.basicLength v                                 ; basicUnsafeSlice i n ( V_Int8 v) =  V_Int8 $ G.basicUnsafeSlice i n v         ; basicUnsafeIndexM ( V_Int8 v) i = G.basicUnsafeIndexM v i                 ; basicUnsafeCopy ( MV_Int8 mv) ( V_Int8 v) = G.basicUnsafeCopy mv v            ; elemseq _ = seq }

newtype instance MVector s Int16 = MV_Int16 (P.MVector s Int16)
newtype instance Vector    Int16 = V_Int16  (P.Vector    Int16)
instance Unbox Int16
instance M.MVector MVector Int16 where {                                     {-# INLINE basicLength #-}                                            ; {-# INLINE basicUnsafeSlice #-}                                       ; {-# INLINE basicOverlaps #-}                                          ; {-# INLINE basicUnsafeNew #-}                                         ; {-# INLINE basicInitialize #-}                                        ; {-# INLINE basicUnsafeReplicate #-}                                   ; {-# INLINE basicUnsafeRead #-}                                        ; {-# INLINE basicUnsafeWrite #-}                                       ; {-# INLINE basicClear #-}                                             ; {-# INLINE basicSet #-}                                               ; {-# INLINE basicUnsafeCopy #-}                                        ; {-# INLINE basicUnsafeGrow #-}                                        ; basicLength ( MV_Int16 v) = M.basicLength v                                 ; basicUnsafeSlice i n ( MV_Int16 v) =  MV_Int16 $ M.basicUnsafeSlice i n v         ; basicOverlaps ( MV_Int16 v1) ( MV_Int16 v2) = M.basicOverlaps v1 v2               ; basicUnsafeNew n =  MV_Int16 `liftM` M.basicUnsafeNew n                     ; basicInitialize ( MV_Int16 v) = M.basicInitialize v                         ; basicUnsafeReplicate n x =  MV_Int16 `liftM` M.basicUnsafeReplicate n x     ; basicUnsafeRead ( MV_Int16 v) i = M.basicUnsafeRead v i                     ; basicUnsafeWrite ( MV_Int16 v) i x = M.basicUnsafeWrite v i x               ; basicClear ( MV_Int16 v) = M.basicClear v                                   ; basicSet ( MV_Int16 v) x = M.basicSet v x                                   ; basicUnsafeCopy ( MV_Int16 v1) ( MV_Int16 v2) = M.basicUnsafeCopy v1 v2           ; basicUnsafeMove ( MV_Int16 v1) ( MV_Int16 v2) = M.basicUnsafeMove v1 v2           ; basicUnsafeGrow ( MV_Int16 v) n =  MV_Int16 `liftM` M.basicUnsafeGrow v n }
instance G.Vector Vector Int16 where {                                       {-# INLINE basicUnsafeFreeze #-}                                      ; {-# INLINE basicUnsafeThaw #-}                                        ; {-# INLINE basicLength #-}                                            ; {-# INLINE basicUnsafeSlice #-}                                       ; {-# INLINE basicUnsafeIndexM #-}                                      ; {-# INLINE elemseq #-}                                                ; basicUnsafeFreeze ( MV_Int16 v) =  V_Int16 `liftM` G.basicUnsafeFreeze v        ; basicUnsafeThaw ( V_Int16 v) =  MV_Int16 `liftM` G.basicUnsafeThaw v            ; basicLength ( V_Int16 v) = G.basicLength v                                 ; basicUnsafeSlice i n ( V_Int16 v) =  V_Int16 $ G.basicUnsafeSlice i n v         ; basicUnsafeIndexM ( V_Int16 v) i = G.basicUnsafeIndexM v i                 ; basicUnsafeCopy ( MV_Int16 mv) ( V_Int16 v) = G.basicUnsafeCopy mv v            ; elemseq _ = seq }

newtype instance MVector s Int32 = MV_Int32 (P.MVector s Int32)
newtype instance Vector    Int32 = V_Int32  (P.Vector    Int32)
instance Unbox Int32
instance M.MVector MVector Int32 where {                                     {-# INLINE basicLength #-}                                            ; {-# INLINE basicUnsafeSlice #-}                                       ; {-# INLINE basicOverlaps #-}                                          ; {-# INLINE basicUnsafeNew #-}                                         ; {-# INLINE basicInitialize #-}                                        ; {-# INLINE basicUnsafeReplicate #-}                                   ; {-# INLINE basicUnsafeRead #-}                                        ; {-# INLINE basicUnsafeWrite #-}                                       ; {-# INLINE basicClear #-}                                             ; {-# INLINE basicSet #-}                                               ; {-# INLINE basicUnsafeCopy #-}                                        ; {-# INLINE basicUnsafeGrow #-}                                        ; basicLength ( MV_Int32 v) = M.basicLength v                                 ; basicUnsafeSlice i n ( MV_Int32 v) =  MV_Int32 $ M.basicUnsafeSlice i n v         ; basicOverlaps ( MV_Int32 v1) ( MV_Int32 v2) = M.basicOverlaps v1 v2               ; basicUnsafeNew n =  MV_Int32 `liftM` M.basicUnsafeNew n                     ; basicInitialize ( MV_Int32 v) = M.basicInitialize v                         ; basicUnsafeReplicate n x =  MV_Int32 `liftM` M.basicUnsafeReplicate n x     ; basicUnsafeRead ( MV_Int32 v) i = M.basicUnsafeRead v i                     ; basicUnsafeWrite ( MV_Int32 v) i x = M.basicUnsafeWrite v i x               ; basicClear ( MV_Int32 v) = M.basicClear v                                   ; basicSet ( MV_Int32 v) x = M.basicSet v x                                   ; basicUnsafeCopy ( MV_Int32 v1) ( MV_Int32 v2) = M.basicUnsafeCopy v1 v2           ; basicUnsafeMove ( MV_Int32 v1) ( MV_Int32 v2) = M.basicUnsafeMove v1 v2           ; basicUnsafeGrow ( MV_Int32 v) n =  MV_Int32 `liftM` M.basicUnsafeGrow v n }
instance G.Vector Vector Int32 where {                                       {-# INLINE basicUnsafeFreeze #-}                                      ; {-# INLINE basicUnsafeThaw #-}                                        ; {-# INLINE basicLength #-}                                            ; {-# INLINE basicUnsafeSlice #-}                                       ; {-# INLINE basicUnsafeIndexM #-}                                      ; {-# INLINE elemseq #-}                                                ; basicUnsafeFreeze ( MV_Int32 v) =  V_Int32 `liftM` G.basicUnsafeFreeze v        ; basicUnsafeThaw ( V_Int32 v) =  MV_Int32 `liftM` G.basicUnsafeThaw v            ; basicLength ( V_Int32 v) = G.basicLength v                                 ; basicUnsafeSlice i n ( V_Int32 v) =  V_Int32 $ G.basicUnsafeSlice i n v         ; basicUnsafeIndexM ( V_Int32 v) i = G.basicUnsafeIndexM v i                 ; basicUnsafeCopy ( MV_Int32 mv) ( V_Int32 v) = G.basicUnsafeCopy mv v            ; elemseq _ = seq }

newtype instance MVector s Int64 = MV_Int64 (P.MVector s Int64)
newtype instance Vector    Int64 = V_Int64  (P.Vector    Int64)
instance Unbox Int64
instance M.MVector MVector Int64 where {                                     {-# INLINE basicLength #-}                                            ; {-# INLINE basicUnsafeSlice #-}                                       ; {-# INLINE basicOverlaps #-}                                          ; {-# INLINE basicUnsafeNew #-}                                         ; {-# INLINE basicInitialize #-}                                        ; {-# INLINE basicUnsafeReplicate #-}                                   ; {-# INLINE basicUnsafeRead #-}                                        ; {-# INLINE basicUnsafeWrite #-}                                       ; {-# INLINE basicClear #-}                                             ; {-# INLINE basicSet #-}                                               ; {-# INLINE basicUnsafeCopy #-}                                        ; {-# INLINE basicUnsafeGrow #-}                                        ; basicLength ( MV_Int64 v) = M.basicLength v                                 ; basicUnsafeSlice i n ( MV_Int64 v) =  MV_Int64 $ M.basicUnsafeSlice i n v         ; basicOverlaps ( MV_Int64 v1) ( MV_Int64 v2) = M.basicOverlaps v1 v2               ; basicUnsafeNew n =  MV_Int64 `liftM` M.basicUnsafeNew n                     ; basicInitialize ( MV_Int64 v) = M.basicInitialize v                         ; basicUnsafeReplicate n x =  MV_Int64 `liftM` M.basicUnsafeReplicate n x     ; basicUnsafeRead ( MV_Int64 v) i = M.basicUnsafeRead v i                     ; basicUnsafeWrite ( MV_Int64 v) i x = M.basicUnsafeWrite v i x               ; basicClear ( MV_Int64 v) = M.basicClear v                                   ; basicSet ( MV_Int64 v) x = M.basicSet v x                                   ; basicUnsafeCopy ( MV_Int64 v1) ( MV_Int64 v2) = M.basicUnsafeCopy v1 v2           ; basicUnsafeMove ( MV_Int64 v1) ( MV_Int64 v2) = M.basicUnsafeMove v1 v2           ; basicUnsafeGrow ( MV_Int64 v) n =  MV_Int64 `liftM` M.basicUnsafeGrow v n }
instance G.Vector Vector Int64 where {                                       {-# INLINE basicUnsafeFreeze #-}                                      ; {-# INLINE basicUnsafeThaw #-}                                        ; {-# INLINE basicLength #-}                                            ; {-# INLINE basicUnsafeSlice #-}                                       ; {-# INLINE basicUnsafeIndexM #-}                                      ; {-# INLINE elemseq #-}                                                ; basicUnsafeFreeze ( MV_Int64 v) =  V_Int64 `liftM` G.basicUnsafeFreeze v        ; basicUnsafeThaw ( V_Int64 v) =  MV_Int64 `liftM` G.basicUnsafeThaw v            ; basicLength ( V_Int64 v) = G.basicLength v                                 ; basicUnsafeSlice i n ( V_Int64 v) =  V_Int64 $ G.basicUnsafeSlice i n v         ; basicUnsafeIndexM ( V_Int64 v) i = G.basicUnsafeIndexM v i                 ; basicUnsafeCopy ( MV_Int64 mv) ( V_Int64 v) = G.basicUnsafeCopy mv v            ; elemseq _ = seq }

newtype instance MVector s Word = MV_Word (P.MVector s Word)
newtype instance Vector    Word = V_Word  (P.Vector    Word)
instance Unbox Word
instance M.MVector MVector Word where {                                     {-# INLINE basicLength #-}                                            ; {-# INLINE basicUnsafeSlice #-}                                       ; {-# INLINE basicOverlaps #-}                                          ; {-# INLINE basicUnsafeNew #-}                                         ; {-# INLINE basicInitialize #-}                                        ; {-# INLINE basicUnsafeReplicate #-}                                   ; {-# INLINE basicUnsafeRead #-}                                        ; {-# INLINE basicUnsafeWrite #-}                                       ; {-# INLINE basicClear #-}                                             ; {-# INLINE basicSet #-}                                               ; {-# INLINE basicUnsafeCopy #-}                                        ; {-# INLINE basicUnsafeGrow #-}                                        ; basicLength ( MV_Word v) = M.basicLength v                                 ; basicUnsafeSlice i n ( MV_Word v) =  MV_Word $ M.basicUnsafeSlice i n v         ; basicOverlaps ( MV_Word v1) ( MV_Word v2) = M.basicOverlaps v1 v2               ; basicUnsafeNew n =  MV_Word `liftM` M.basicUnsafeNew n                     ; basicInitialize ( MV_Word v) = M.basicInitialize v                         ; basicUnsafeReplicate n x =  MV_Word `liftM` M.basicUnsafeReplicate n x     ; basicUnsafeRead ( MV_Word v) i = M.basicUnsafeRead v i                     ; basicUnsafeWrite ( MV_Word v) i x = M.basicUnsafeWrite v i x               ; basicClear ( MV_Word v) = M.basicClear v                                   ; basicSet ( MV_Word v) x = M.basicSet v x                                   ; basicUnsafeCopy ( MV_Word v1) ( MV_Word v2) = M.basicUnsafeCopy v1 v2           ; basicUnsafeMove ( MV_Word v1) ( MV_Word v2) = M.basicUnsafeMove v1 v2           ; basicUnsafeGrow ( MV_Word v) n =  MV_Word `liftM` M.basicUnsafeGrow v n }
instance G.Vector Vector Word where {                                       {-# INLINE basicUnsafeFreeze #-}                                      ; {-# INLINE basicUnsafeThaw #-}                                        ; {-# INLINE basicLength #-}                                            ; {-# INLINE basicUnsafeSlice #-}                                       ; {-# INLINE basicUnsafeIndexM #-}                                      ; {-# INLINE elemseq #-}                                                ; basicUnsafeFreeze ( MV_Word v) =  V_Word `liftM` G.basicUnsafeFreeze v        ; basicUnsafeThaw ( V_Word v) =  MV_Word `liftM` G.basicUnsafeThaw v            ; basicLength ( V_Word v) = G.basicLength v                                 ; basicUnsafeSlice i n ( V_Word v) =  V_Word $ G.basicUnsafeSlice i n v         ; basicUnsafeIndexM ( V_Word v) i = G.basicUnsafeIndexM v i                 ; basicUnsafeCopy ( MV_Word mv) ( V_Word v) = G.basicUnsafeCopy mv v            ; elemseq _ = seq }

newtype instance MVector s Word8 = MV_Word8 (P.MVector s Word8)
newtype instance Vector    Word8 = V_Word8  (P.Vector    Word8)
instance Unbox Word8
instance M.MVector MVector Word8 where {                                     {-# INLINE basicLength #-}                                            ; {-# INLINE basicUnsafeSlice #-}                                       ; {-# INLINE basicOverlaps #-}                                          ; {-# INLINE basicUnsafeNew #-}                                         ; {-# INLINE basicInitialize #-}                                        ; {-# INLINE basicUnsafeReplicate #-}                                   ; {-# INLINE basicUnsafeRead #-}                                        ; {-# INLINE basicUnsafeWrite #-}                                       ; {-# INLINE basicClear #-}                                             ; {-# INLINE basicSet #-}                                               ; {-# INLINE basicUnsafeCopy #-}                                        ; {-# INLINE basicUnsafeGrow #-}                                        ; basicLength ( MV_Word8 v) = M.basicLength v                                 ; basicUnsafeSlice i n ( MV_Word8 v) =  MV_Word8 $ M.basicUnsafeSlice i n v         ; basicOverlaps ( MV_Word8 v1) ( MV_Word8 v2) = M.basicOverlaps v1 v2               ; basicUnsafeNew n =  MV_Word8 `liftM` M.basicUnsafeNew n                     ; basicInitialize ( MV_Word8 v) = M.basicInitialize v                         ; basicUnsafeReplicate n x =  MV_Word8 `liftM` M.basicUnsafeReplicate n x     ; basicUnsafeRead ( MV_Word8 v) i = M.basicUnsafeRead v i                     ; basicUnsafeWrite ( MV_Word8 v) i x = M.basicUnsafeWrite v i x               ; basicClear ( MV_Word8 v) = M.basicClear v                                   ; basicSet ( MV_Word8 v) x = M.basicSet v x                                   ; basicUnsafeCopy ( MV_Word8 v1) ( MV_Word8 v2) = M.basicUnsafeCopy v1 v2           ; basicUnsafeMove ( MV_Word8 v1) ( MV_Word8 v2) = M.basicUnsafeMove v1 v2           ; basicUnsafeGrow ( MV_Word8 v) n =  MV_Word8 `liftM` M.basicUnsafeGrow v n }
instance G.Vector Vector Word8 where {                                       {-# INLINE basicUnsafeFreeze #-}                                      ; {-# INLINE basicUnsafeThaw #-}                                        ; {-# INLINE basicLength #-}                                            ; {-# INLINE basicUnsafeSlice #-}                                       ; {-# INLINE basicUnsafeIndexM #-}                                      ; {-# INLINE elemseq #-}                                                ; basicUnsafeFreeze ( MV_Word8 v) =  V_Word8 `liftM` G.basicUnsafeFreeze v        ; basicUnsafeThaw ( V_Word8 v) =  MV_Word8 `liftM` G.basicUnsafeThaw v            ; basicLength ( V_Word8 v) = G.basicLength v                                 ; basicUnsafeSlice i n ( V_Word8 v) =  V_Word8 $ G.basicUnsafeSlice i n v         ; basicUnsafeIndexM ( V_Word8 v) i = G.basicUnsafeIndexM v i                 ; basicUnsafeCopy ( MV_Word8 mv) ( V_Word8 v) = G.basicUnsafeCopy mv v            ; elemseq _ = seq }

newtype instance MVector s Word16 = MV_Word16 (P.MVector s Word16)
newtype instance Vector    Word16 = V_Word16  (P.Vector    Word16)
instance Unbox Word16
instance M.MVector MVector Word16 where {                                     {-# INLINE basicLength #-}                                            ; {-# INLINE basicUnsafeSlice #-}                                       ; {-# INLINE basicOverlaps #-}                                          ; {-# INLINE basicUnsafeNew #-}                                         ; {-# INLINE basicInitialize #-}                                        ; {-# INLINE basicUnsafeReplicate #-}                                   ; {-# INLINE basicUnsafeRead #-}                                        ; {-# INLINE basicUnsafeWrite #-}                                       ; {-# INLINE basicClear #-}                                             ; {-# INLINE basicSet #-}                                               ; {-# INLINE basicUnsafeCopy #-}                                        ; {-# INLINE basicUnsafeGrow #-}                                        ; basicLength ( MV_Word16 v) = M.basicLength v                                 ; basicUnsafeSlice i n ( MV_Word16 v) =  MV_Word16 $ M.basicUnsafeSlice i n v         ; basicOverlaps ( MV_Word16 v1) ( MV_Word16 v2) = M.basicOverlaps v1 v2               ; basicUnsafeNew n =  MV_Word16 `liftM` M.basicUnsafeNew n                     ; basicInitialize ( MV_Word16 v) = M.basicInitialize v                         ; basicUnsafeReplicate n x =  MV_Word16 `liftM` M.basicUnsafeReplicate n x     ; basicUnsafeRead ( MV_Word16 v) i = M.basicUnsafeRead v i                     ; basicUnsafeWrite ( MV_Word16 v) i x = M.basicUnsafeWrite v i x               ; basicClear ( MV_Word16 v) = M.basicClear v                                   ; basicSet ( MV_Word16 v) x = M.basicSet v x                                   ; basicUnsafeCopy ( MV_Word16 v1) ( MV_Word16 v2) = M.basicUnsafeCopy v1 v2           ; basicUnsafeMove ( MV_Word16 v1) ( MV_Word16 v2) = M.basicUnsafeMove v1 v2           ; basicUnsafeGrow ( MV_Word16 v) n =  MV_Word16 `liftM` M.basicUnsafeGrow v n }
instance G.Vector Vector Word16 where {                                       {-# INLINE basicUnsafeFreeze #-}                                      ; {-# INLINE basicUnsafeThaw #-}                                        ; {-# INLINE basicLength #-}                                            ; {-# INLINE basicUnsafeSlice #-}                                       ; {-# INLINE basicUnsafeIndexM #-}                                      ; {-# INLINE elemseq #-}                                                ; basicUnsafeFreeze ( MV_Word16 v) =  V_Word16 `liftM` G.basicUnsafeFreeze v        ; basicUnsafeThaw ( V_Word16 v) =  MV_Word16 `liftM` G.basicUnsafeThaw v            ; basicLength ( V_Word16 v) = G.basicLength v                                 ; basicUnsafeSlice i n ( V_Word16 v) =  V_Word16 $ G.basicUnsafeSlice i n v         ; basicUnsafeIndexM ( V_Word16 v) i = G.basicUnsafeIndexM v i                 ; basicUnsafeCopy ( MV_Word16 mv) ( V_Word16 v) = G.basicUnsafeCopy mv v            ; elemseq _ = seq }

newtype instance MVector s Word32 = MV_Word32 (P.MVector s Word32)
newtype instance Vector    Word32 = V_Word32  (P.Vector    Word32)
instance Unbox Word32
instance M.MVector MVector Word32 where {                                     {-# INLINE basicLength #-}                                            ; {-# INLINE basicUnsafeSlice #-}                                       ; {-# INLINE basicOverlaps #-}                                          ; {-# INLINE basicUnsafeNew #-}                                         ; {-# INLINE basicInitialize #-}                                        ; {-# INLINE basicUnsafeReplicate #-}                                   ; {-# INLINE basicUnsafeRead #-}                                        ; {-# INLINE basicUnsafeWrite #-}                                       ; {-# INLINE basicClear #-}                                             ; {-# INLINE basicSet #-}                                               ; {-# INLINE basicUnsafeCopy #-}                                        ; {-# INLINE basicUnsafeGrow #-}                                        ; basicLength ( MV_Word32 v) = M.basicLength v                                 ; basicUnsafeSlice i n ( MV_Word32 v) =  MV_Word32 $ M.basicUnsafeSlice i n v         ; basicOverlaps ( MV_Word32 v1) ( MV_Word32 v2) = M.basicOverlaps v1 v2               ; basicUnsafeNew n =  MV_Word32 `liftM` M.basicUnsafeNew n                     ; basicInitialize ( MV_Word32 v) = M.basicInitialize v                         ; basicUnsafeReplicate n x =  MV_Word32 `liftM` M.basicUnsafeReplicate n x     ; basicUnsafeRead ( MV_Word32 v) i = M.basicUnsafeRead v i                     ; basicUnsafeWrite ( MV_Word32 v) i x = M.basicUnsafeWrite v i x               ; basicClear ( MV_Word32 v) = M.basicClear v                                   ; basicSet ( MV_Word32 v) x = M.basicSet v x                                   ; basicUnsafeCopy ( MV_Word32 v1) ( MV_Word32 v2) = M.basicUnsafeCopy v1 v2           ; basicUnsafeMove ( MV_Word32 v1) ( MV_Word32 v2) = M.basicUnsafeMove v1 v2           ; basicUnsafeGrow ( MV_Word32 v) n =  MV_Word32 `liftM` M.basicUnsafeGrow v n }
instance G.Vector Vector Word32 where {                                       {-# INLINE basicUnsafeFreeze #-}                                      ; {-# INLINE basicUnsafeThaw #-}                                        ; {-# INLINE basicLength #-}                                            ; {-# INLINE basicUnsafeSlice #-}                                       ; {-# INLINE basicUnsafeIndexM #-}                                      ; {-# INLINE elemseq #-}                                                ; basicUnsafeFreeze ( MV_Word32 v) =  V_Word32 `liftM` G.basicUnsafeFreeze v        ; basicUnsafeThaw ( V_Word32 v) =  MV_Word32 `liftM` G.basicUnsafeThaw v            ; basicLength ( V_Word32 v) = G.basicLength v                                 ; basicUnsafeSlice i n ( V_Word32 v) =  V_Word32 $ G.basicUnsafeSlice i n v         ; basicUnsafeIndexM ( V_Word32 v) i = G.basicUnsafeIndexM v i                 ; basicUnsafeCopy ( MV_Word32 mv) ( V_Word32 v) = G.basicUnsafeCopy mv v            ; elemseq _ = seq }

newtype instance MVector s Word64 = MV_Word64 (P.MVector s Word64)
newtype instance Vector    Word64 = V_Word64  (P.Vector    Word64)
instance Unbox Word64
instance M.MVector MVector Word64 where {                                     {-# INLINE basicLength #-}                                            ; {-# INLINE basicUnsafeSlice #-}                                       ; {-# INLINE basicOverlaps #-}                                          ; {-# INLINE basicUnsafeNew #-}                                         ; {-# INLINE basicInitialize #-}                                        ; {-# INLINE basicUnsafeReplicate #-}                                   ; {-# INLINE basicUnsafeRead #-}                                        ; {-# INLINE basicUnsafeWrite #-}                                       ; {-# INLINE basicClear #-}                                             ; {-# INLINE basicSet #-}                                               ; {-# INLINE basicUnsafeCopy #-}                                        ; {-# INLINE basicUnsafeGrow #-}                                        ; basicLength ( MV_Word64 v) = M.basicLength v                                 ; basicUnsafeSlice i n ( MV_Word64 v) =  MV_Word64 $ M.basicUnsafeSlice i n v         ; basicOverlaps ( MV_Word64 v1) ( MV_Word64 v2) = M.basicOverlaps v1 v2               ; basicUnsafeNew n =  MV_Word64 `liftM` M.basicUnsafeNew n                     ; basicInitialize ( MV_Word64 v) = M.basicInitialize v                         ; basicUnsafeReplicate n x =  MV_Word64 `liftM` M.basicUnsafeReplicate n x     ; basicUnsafeRead ( MV_Word64 v) i = M.basicUnsafeRead v i                     ; basicUnsafeWrite ( MV_Word64 v) i x = M.basicUnsafeWrite v i x               ; basicClear ( MV_Word64 v) = M.basicClear v                                   ; basicSet ( MV_Word64 v) x = M.basicSet v x                                   ; basicUnsafeCopy ( MV_Word64 v1) ( MV_Word64 v2) = M.basicUnsafeCopy v1 v2           ; basicUnsafeMove ( MV_Word64 v1) ( MV_Word64 v2) = M.basicUnsafeMove v1 v2           ; basicUnsafeGrow ( MV_Word64 v) n =  MV_Word64 `liftM` M.basicUnsafeGrow v n }
instance G.Vector Vector Word64 where {                                       {-# INLINE basicUnsafeFreeze #-}                                      ; {-# INLINE basicUnsafeThaw #-}                                        ; {-# INLINE basicLength #-}                                            ; {-# INLINE basicUnsafeSlice #-}                                       ; {-# INLINE basicUnsafeIndexM #-}                                      ; {-# INLINE elemseq #-}                                                ; basicUnsafeFreeze ( MV_Word64 v) =  V_Word64 `liftM` G.basicUnsafeFreeze v        ; basicUnsafeThaw ( V_Word64 v) =  MV_Word64 `liftM` G.basicUnsafeThaw v            ; basicLength ( V_Word64 v) = G.basicLength v                                 ; basicUnsafeSlice i n ( V_Word64 v) =  V_Word64 $ G.basicUnsafeSlice i n v         ; basicUnsafeIndexM ( V_Word64 v) i = G.basicUnsafeIndexM v i                 ; basicUnsafeCopy ( MV_Word64 mv) ( V_Word64 v) = G.basicUnsafeCopy mv v            ; elemseq _ = seq }

newtype instance MVector s Float = MV_Float (P.MVector s Float)
newtype instance Vector    Float = V_Float  (P.Vector    Float)
instance Unbox Float
instance M.MVector MVector Float where {                                     {-# INLINE basicLength #-}                                            ; {-# INLINE basicUnsafeSlice #-}                                       ; {-# INLINE basicOverlaps #-}                                          ; {-# INLINE basicUnsafeNew #-}                                         ; {-# INLINE basicInitialize #-}                                        ; {-# INLINE basicUnsafeReplicate #-}                                   ; {-# INLINE basicUnsafeRead #-}                                        ; {-# INLINE basicUnsafeWrite #-}                                       ; {-# INLINE basicClear #-}                                             ; {-# INLINE basicSet #-}                                               ; {-# INLINE basicUnsafeCopy #-}                                        ; {-# INLINE basicUnsafeGrow #-}                                        ; basicLength ( MV_Float v) = M.basicLength v                                 ; basicUnsafeSlice i n ( MV_Float v) =  MV_Float $ M.basicUnsafeSlice i n v         ; basicOverlaps ( MV_Float v1) ( MV_Float v2) = M.basicOverlaps v1 v2               ; basicUnsafeNew n =  MV_Float `liftM` M.basicUnsafeNew n                     ; basicInitialize ( MV_Float v) = M.basicInitialize v                         ; basicUnsafeReplicate n x =  MV_Float `liftM` M.basicUnsafeReplicate n x     ; basicUnsafeRead ( MV_Float v) i = M.basicUnsafeRead v i                     ; basicUnsafeWrite ( MV_Float v) i x = M.basicUnsafeWrite v i x               ; basicClear ( MV_Float v) = M.basicClear v                                   ; basicSet ( MV_Float v) x = M.basicSet v x                                   ; basicUnsafeCopy ( MV_Float v1) ( MV_Float v2) = M.basicUnsafeCopy v1 v2           ; basicUnsafeMove ( MV_Float v1) ( MV_Float v2) = M.basicUnsafeMove v1 v2           ; basicUnsafeGrow ( MV_Float v) n =  MV_Float `liftM` M.basicUnsafeGrow v n }
instance G.Vector Vector Float where {                                       {-# INLINE basicUnsafeFreeze #-}                                      ; {-# INLINE basicUnsafeThaw #-}                                        ; {-# INLINE basicLength #-}                                            ; {-# INLINE basicUnsafeSlice #-}                                       ; {-# INLINE basicUnsafeIndexM #-}                                      ; {-# INLINE elemseq #-}                                                ; basicUnsafeFreeze ( MV_Float v) =  V_Float `liftM` G.basicUnsafeFreeze v        ; basicUnsafeThaw ( V_Float v) =  MV_Float `liftM` G.basicUnsafeThaw v            ; basicLength ( V_Float v) = G.basicLength v                                 ; basicUnsafeSlice i n ( V_Float v) =  V_Float $ G.basicUnsafeSlice i n v         ; basicUnsafeIndexM ( V_Float v) i = G.basicUnsafeIndexM v i                 ; basicUnsafeCopy ( MV_Float mv) ( V_Float v) = G.basicUnsafeCopy mv v            ; elemseq _ = seq }

newtype instance MVector s Double = MV_Double (P.MVector s Double)
newtype instance Vector    Double = V_Double  (P.Vector    Double)
instance Unbox Double
instance M.MVector MVector Double where {                                     {-# INLINE basicLength #-}                                            ; {-# INLINE basicUnsafeSlice #-}                                       ; {-# INLINE basicOverlaps #-}                                          ; {-# INLINE basicUnsafeNew #-}                                         ; {-# INLINE basicInitialize #-}                                        ; {-# INLINE basicUnsafeReplicate #-}                                   ; {-# INLINE basicUnsafeRead #-}                                        ; {-# INLINE basicUnsafeWrite #-}                                       ; {-# INLINE basicClear #-}                                             ; {-# INLINE basicSet #-}                                               ; {-# INLINE basicUnsafeCopy #-}                                        ; {-# INLINE basicUnsafeGrow #-}                                        ; basicLength ( MV_Double v) = M.basicLength v                                 ; basicUnsafeSlice i n ( MV_Double v) =  MV_Double $ M.basicUnsafeSlice i n v         ; basicOverlaps ( MV_Double v1) ( MV_Double v2) = M.basicOverlaps v1 v2               ; basicUnsafeNew n =  MV_Double `liftM` M.basicUnsafeNew n                     ; basicInitialize ( MV_Double v) = M.basicInitialize v                         ; basicUnsafeReplicate n x =  MV_Double `liftM` M.basicUnsafeReplicate n x     ; basicUnsafeRead ( MV_Double v) i = M.basicUnsafeRead v i                     ; basicUnsafeWrite ( MV_Double v) i x = M.basicUnsafeWrite v i x               ; basicClear ( MV_Double v) = M.basicClear v                                   ; basicSet ( MV_Double v) x = M.basicSet v x                                   ; basicUnsafeCopy ( MV_Double v1) ( MV_Double v2) = M.basicUnsafeCopy v1 v2           ; basicUnsafeMove ( MV_Double v1) ( MV_Double v2) = M.basicUnsafeMove v1 v2           ; basicUnsafeGrow ( MV_Double v) n =  MV_Double `liftM` M.basicUnsafeGrow v n }
instance G.Vector Vector Double where {                                       {-# INLINE basicUnsafeFreeze #-}                                      ; {-# INLINE basicUnsafeThaw #-}                                        ; {-# INLINE basicLength #-}                                            ; {-# INLINE basicUnsafeSlice #-}                                       ; {-# INLINE basicUnsafeIndexM #-}                                      ; {-# INLINE elemseq #-}                                                ; basicUnsafeFreeze ( MV_Double v) =  V_Double `liftM` G.basicUnsafeFreeze v        ; basicUnsafeThaw ( V_Double v) =  MV_Double `liftM` G.basicUnsafeThaw v            ; basicLength ( V_Double v) = G.basicLength v                                 ; basicUnsafeSlice i n ( V_Double v) =  V_Double $ G.basicUnsafeSlice i n v         ; basicUnsafeIndexM ( V_Double v) i = G.basicUnsafeIndexM v i                 ; basicUnsafeCopy ( MV_Double mv) ( V_Double v) = G.basicUnsafeCopy mv v            ; elemseq _ = seq }

newtype instance MVector s Char = MV_Char (P.MVector s Char)
newtype instance Vector    Char = V_Char  (P.Vector    Char)
instance Unbox Char
instance M.MVector MVector Char where {                                     {-# INLINE basicLength #-}                                            ; {-# INLINE basicUnsafeSlice #-}                                       ; {-# INLINE basicOverlaps #-}                                          ; {-# INLINE basicUnsafeNew #-}                                         ; {-# INLINE basicInitialize #-}                                        ; {-# INLINE basicUnsafeReplicate #-}                                   ; {-# INLINE basicUnsafeRead #-}                                        ; {-# INLINE basicUnsafeWrite #-}                                       ; {-# INLINE basicClear #-}                                             ; {-# INLINE basicSet #-}                                               ; {-# INLINE basicUnsafeCopy #-}                                        ; {-# INLINE basicUnsafeGrow #-}                                        ; basicLength ( MV_Char v) = M.basicLength v                                 ; basicUnsafeSlice i n ( MV_Char v) =  MV_Char $ M.basicUnsafeSlice i n v         ; basicOverlaps ( MV_Char v1) ( MV_Char v2) = M.basicOverlaps v1 v2               ; basicUnsafeNew n =  MV_Char `liftM` M.basicUnsafeNew n                     ; basicInitialize ( MV_Char v) = M.basicInitialize v                         ; basicUnsafeReplicate n x =  MV_Char `liftM` M.basicUnsafeReplicate n x     ; basicUnsafeRead ( MV_Char v) i = M.basicUnsafeRead v i                     ; basicUnsafeWrite ( MV_Char v) i x = M.basicUnsafeWrite v i x               ; basicClear ( MV_Char v) = M.basicClear v                                   ; basicSet ( MV_Char v) x = M.basicSet v x                                   ; basicUnsafeCopy ( MV_Char v1) ( MV_Char v2) = M.basicUnsafeCopy v1 v2           ; basicUnsafeMove ( MV_Char v1) ( MV_Char v2) = M.basicUnsafeMove v1 v2           ; basicUnsafeGrow ( MV_Char v) n =  MV_Char `liftM` M.basicUnsafeGrow v n }
instance G.Vector Vector Char where {                                       {-# INLINE basicUnsafeFreeze #-}                                      ; {-# INLINE basicUnsafeThaw #-}                                        ; {-# INLINE basicLength #-}                                            ; {-# INLINE basicUnsafeSlice #-}                                       ; {-# INLINE basicUnsafeIndexM #-}                                      ; {-# INLINE elemseq #-}                                                ; basicUnsafeFreeze ( MV_Char v) =  V_Char `liftM` G.basicUnsafeFreeze v        ; basicUnsafeThaw ( V_Char v) =  MV_Char `liftM` G.basicUnsafeThaw v            ; basicLength ( V_Char v) = G.basicLength v                                 ; basicUnsafeSlice i n ( V_Char v) =  V_Char $ G.basicUnsafeSlice i n v         ; basicUnsafeIndexM ( V_Char v) i = G.basicUnsafeIndexM v i                 ; basicUnsafeCopy ( MV_Char mv) ( V_Char v) = G.basicUnsafeCopy mv v            ; elemseq _ = seq }

-- ----
-- Bool
-- ----

fromBool :: Bool -> Word8
{-# INLINE fromBool #-}
fromBool True = 1
fromBool False = 0

toBool :: Word8 -> Bool
{-# INLINE toBool #-}
toBool 0 = False
toBool _ = True

newtype instance MVector s Bool = MV_Bool (P.MVector s Word8)
newtype instance Vector    Bool = V_Bool  (P.Vector    Word8)

instance Unbox Bool

instance M.MVector MVector Bool where
  {-# INLINE basicLength #-}
  {-# INLINE basicUnsafeSlice #-}
  {-# INLINE basicOverlaps #-}
  {-# INLINE basicUnsafeNew #-}
  {-# INLINE basicInitialize #-}
  {-# INLINE basicUnsafeReplicate #-}
  {-# INLINE basicUnsafeRead #-}
  {-# INLINE basicUnsafeWrite #-}
  {-# INLINE basicClear #-}
  {-# INLINE basicSet #-}
  {-# INLINE basicUnsafeCopy #-}
  {-# INLINE basicUnsafeGrow #-}
  basicLength (MV_Bool v) = M.basicLength v
  basicUnsafeSlice i n (MV_Bool v) = MV_Bool $ M.basicUnsafeSlice i n v
  basicOverlaps (MV_Bool v1) (MV_Bool v2) = M.basicOverlaps v1 v2
  basicUnsafeNew n = MV_Bool `liftM` M.basicUnsafeNew n
  basicInitialize (MV_Bool v) = M.basicInitialize v
  basicUnsafeReplicate n x = MV_Bool `liftM` M.basicUnsafeReplicate n (fromBool x)
  basicUnsafeRead (MV_Bool v) i = toBool `liftM` M.basicUnsafeRead v i
  basicUnsafeWrite (MV_Bool v) i x = M.basicUnsafeWrite v i (fromBool x)
  basicClear (MV_Bool v) = M.basicClear v
  basicSet (MV_Bool v) x = M.basicSet v (fromBool x)
  basicUnsafeCopy (MV_Bool v1) (MV_Bool v2) = M.basicUnsafeCopy v1 v2
  basicUnsafeMove (MV_Bool v1) (MV_Bool v2) = M.basicUnsafeMove v1 v2
  basicUnsafeGrow (MV_Bool v) n = MV_Bool `liftM` M.basicUnsafeGrow v n

instance G.Vector Vector Bool where
  {-# INLINE basicUnsafeFreeze #-}
  {-# INLINE basicUnsafeThaw #-}
  {-# INLINE basicLength #-}
  {-# INLINE basicUnsafeSlice #-}
  {-# INLINE basicUnsafeIndexM #-}
  {-# INLINE elemseq #-}
  basicUnsafeFreeze (MV_Bool v) = V_Bool `liftM` G.basicUnsafeFreeze v
  basicUnsafeThaw (V_Bool v) = MV_Bool `liftM` G.basicUnsafeThaw v
  basicLength (V_Bool v) = G.basicLength v
  basicUnsafeSlice i n (V_Bool v) = V_Bool $ G.basicUnsafeSlice i n v
  basicUnsafeIndexM (V_Bool v) i = toBool `liftM` G.basicUnsafeIndexM v i
  basicUnsafeCopy (MV_Bool mv) (V_Bool v) = G.basicUnsafeCopy mv v
  elemseq _ = seq

-- -------
-- Complex
-- -------

newtype instance MVector s (Complex a) = MV_Complex (MVector s (a,a))
newtype instance Vector    (Complex a) = V_Complex  (Vector    (a,a))

instance (Unbox a) => Unbox (Complex a)

instance (Unbox a) => M.MVector MVector (Complex a) where
  {-# INLINE basicLength #-}
  {-# INLINE basicUnsafeSlice #-}
  {-# INLINE basicOverlaps #-}
  {-# INLINE basicUnsafeNew #-}
  {-# INLINE basicInitialize #-}
  {-# INLINE basicUnsafeReplicate #-}
  {-# INLINE basicUnsafeRead #-}
  {-# INLINE basicUnsafeWrite #-}
  {-# INLINE basicClear #-}
  {-# INLINE basicSet #-}
  {-# INLINE basicUnsafeCopy #-}
  {-# INLINE basicUnsafeGrow #-}
  basicLength (MV_Complex v) = M.basicLength v
  basicUnsafeSlice i n (MV_Complex v) = MV_Complex $ M.basicUnsafeSlice i n v
  basicOverlaps (MV_Complex v1) (MV_Complex v2) = M.basicOverlaps v1 v2
  basicUnsafeNew n = MV_Complex `liftM` M.basicUnsafeNew n
  basicInitialize (MV_Complex v) = M.basicInitialize v
  basicUnsafeReplicate n (x :+ y) = MV_Complex `liftM` M.basicUnsafeReplicate n (x,y)
  basicUnsafeRead (MV_Complex v) i = uncurry (:+) `liftM` M.basicUnsafeRead v i
  basicUnsafeWrite (MV_Complex v) i (x :+ y) = M.basicUnsafeWrite v i (x,y)
  basicClear (MV_Complex v) = M.basicClear v
  basicSet (MV_Complex v) (x :+ y) = M.basicSet v (x,y)
  basicUnsafeCopy (MV_Complex v1) (MV_Complex v2) = M.basicUnsafeCopy v1 v2
  basicUnsafeMove (MV_Complex v1) (MV_Complex v2) = M.basicUnsafeMove v1 v2
  basicUnsafeGrow (MV_Complex v) n = MV_Complex `liftM` M.basicUnsafeGrow v n

instance (Unbox a) => G.Vector Vector (Complex a) where
  {-# INLINE basicUnsafeFreeze #-}
  {-# INLINE basicUnsafeThaw #-}
  {-# INLINE basicLength #-}
  {-# INLINE basicUnsafeSlice #-}
  {-# INLINE basicUnsafeIndexM #-}
  {-# INLINE elemseq #-}
  basicUnsafeFreeze (MV_Complex v) = V_Complex `liftM` G.basicUnsafeFreeze v
  basicUnsafeThaw (V_Complex v) = MV_Complex `liftM` G.basicUnsafeThaw v
  basicLength (V_Complex v) = G.basicLength v
  basicUnsafeSlice i n (V_Complex v) = V_Complex $ G.basicUnsafeSlice i n v
  basicUnsafeIndexM (V_Complex v) i
                = uncurry (:+) `liftM` G.basicUnsafeIndexM v i
  basicUnsafeCopy (MV_Complex mv) (V_Complex v)
                = G.basicUnsafeCopy mv v
  elemseq _ (x :+ y) z = G.elemseq (undefined :: Vector a) x
                       $ G.elemseq (undefined :: Vector a) y z

-- ------
-- Tuples
-- ------

data instance MVector s (a, b)
    = MV_2 {-# UNPACK #-} !Int !(MVector s a)
                               !(MVector s b)
data instance Vector (a, b)
    = V_2 {-# UNPACK #-} !Int !(Vector a)
                              !(Vector b)
instance (Unbox a, Unbox b) => Unbox (a, b)
instance (Unbox a, Unbox b) => M.MVector MVector (a, b) where
  {-# INLINE basicLength  #-}
  basicLength (MV_2 n_ _ _) = n_
  {-# INLINE basicUnsafeSlice  #-}
  basicUnsafeSlice i_ m_ (MV_2 _ as bs)
      = MV_2 m_ (M.basicUnsafeSlice i_ m_ as)
                (M.basicUnsafeSlice i_ m_ bs)
  {-# INLINE basicOverlaps  #-}
  basicOverlaps (MV_2 _ as1 bs1) (MV_2 _ as2 bs2)
      = M.basicOverlaps as1 as2
        || M.basicOverlaps bs1 bs2
  {-# INLINE basicUnsafeNew  #-}
  basicUnsafeNew n_
      = do
          as <- M.basicUnsafeNew n_
          bs <- M.basicUnsafeNew n_
          return $ MV_2 n_ as bs
  {-# INLINE basicInitialize  #-}
  basicInitialize (MV_2 _ as bs)
      = do
          M.basicInitialize as
          M.basicInitialize bs
  {-# INLINE basicUnsafeReplicate  #-}
  basicUnsafeReplicate n_ (a, b)
      = do
          as <- M.basicUnsafeReplicate n_ a
          bs <- M.basicUnsafeReplicate n_ b
          return $ MV_2 n_ as bs
  {-# INLINE basicUnsafeRead  #-}
  basicUnsafeRead (MV_2 _ as bs) i_
      = do
          a <- M.basicUnsafeRead as i_
          b <- M.basicUnsafeRead bs i_
          return (a, b)
  {-# INLINE basicUnsafeWrite  #-}
  basicUnsafeWrite (MV_2 _ as bs) i_ (a, b)
      = do
          M.basicUnsafeWrite as i_ a
          M.basicUnsafeWrite bs i_ b
  {-# INLINE basicClear  #-}
  basicClear (MV_2 _ as bs)
      = do
          M.basicClear as
          M.basicClear bs
  {-# INLINE basicSet  #-}
  basicSet (MV_2 _ as bs) (a, b)
      = do
          M.basicSet as a
          M.basicSet bs b
  {-# INLINE basicUnsafeCopy  #-}
  basicUnsafeCopy (MV_2 _ as1 bs1) (MV_2 _ as2 bs2)
      = do
          M.basicUnsafeCopy as1 as2
          M.basicUnsafeCopy bs1 bs2
  {-# INLINE basicUnsafeMove  #-}
  basicUnsafeMove (MV_2 _ as1 bs1) (MV_2 _ as2 bs2)
      = do
          M.basicUnsafeMove as1 as2
          M.basicUnsafeMove bs1 bs2
  {-# INLINE basicUnsafeGrow  #-}
  basicUnsafeGrow (MV_2 n_ as bs) m_
      = do
          as' <- M.basicUnsafeGrow as m_
          bs' <- M.basicUnsafeGrow bs m_
          return $ MV_2 (m_+n_) as' bs'
instance (Unbox a, Unbox b) => G.Vector Vector (a, b) where
  {-# INLINE basicUnsafeFreeze  #-}
  basicUnsafeFreeze (MV_2 n_ as bs)
      = do
          as' <- G.basicUnsafeFreeze as
          bs' <- G.basicUnsafeFreeze bs
          return $ V_2 n_ as' bs'
  {-# INLINE basicUnsafeThaw  #-}
  basicUnsafeThaw (V_2 n_ as bs)
      = do
          as' <- G.basicUnsafeThaw as
          bs' <- G.basicUnsafeThaw bs
          return $ MV_2 n_ as' bs'
  {-# INLINE basicLength  #-}
  basicLength (V_2 n_ _ _) = n_
  {-# INLINE basicUnsafeSlice  #-}
  basicUnsafeSlice i_ m_ (V_2 _ as bs)
      = V_2 m_ (G.basicUnsafeSlice i_ m_ as)
               (G.basicUnsafeSlice i_ m_ bs)
  {-# INLINE basicUnsafeIndexM  #-}
  basicUnsafeIndexM (V_2 _ as bs) i_
      = do
          a <- G.basicUnsafeIndexM as i_
          b <- G.basicUnsafeIndexM bs i_
          return (a, b)
  {-# INLINE basicUnsafeCopy  #-}
  basicUnsafeCopy (MV_2 _ as1 bs1) (V_2 _ as2 bs2)
      = do
          G.basicUnsafeCopy as1 as2
          G.basicUnsafeCopy bs1 bs2
  {-# INLINE elemseq  #-}
  elemseq _ (a, b)
      = G.elemseq (undefined :: Vector a) a
        . G.elemseq (undefined :: Vector b) b
data instance MVector s (a, b, c)
    = MV_3 {-# UNPACK #-} !Int !(MVector s a)
                               !(MVector s b)
                               !(MVector s c)
data instance Vector (a, b, c)
    = V_3 {-# UNPACK #-} !Int !(Vector a)
                              !(Vector b)
                              !(Vector c)
instance (Unbox a, Unbox b, Unbox c) => Unbox (a, b, c)
instance (Unbox a,
          Unbox b,
          Unbox c) => M.MVector MVector (a, b, c) where
  {-# INLINE basicLength  #-}
  basicLength (MV_3 n_ _ _ _) = n_
  {-# INLINE basicUnsafeSlice  #-}
  basicUnsafeSlice i_ m_ (MV_3 _ as bs cs)
      = MV_3 m_ (M.basicUnsafeSlice i_ m_ as)
                (M.basicUnsafeSlice i_ m_ bs)
                (M.basicUnsafeSlice i_ m_ cs)
  {-# INLINE basicOverlaps  #-}
  basicOverlaps (MV_3 _ as1 bs1 cs1) (MV_3 _ as2 bs2 cs2)
      = M.basicOverlaps as1 as2
        || M.basicOverlaps bs1 bs2
        || M.basicOverlaps cs1 cs2
  {-# INLINE basicUnsafeNew  #-}
  basicUnsafeNew n_
      = do
          as <- M.basicUnsafeNew n_
          bs <- M.basicUnsafeNew n_
          cs <- M.basicUnsafeNew n_
          return $ MV_3 n_ as bs cs
  {-# INLINE basicInitialize #-}
  basicInitialize (MV_3 _ as bs cs)
      = do
          M.basicInitialize as
          M.basicInitialize bs
          M.basicInitialize cs
  {-# INLINE basicUnsafeReplicate  #-}
  basicUnsafeReplicate n_ (a, b, c)
      = do
          as <- M.basicUnsafeReplicate n_ a
          bs <- M.basicUnsafeReplicate n_ b
          cs <- M.basicUnsafeReplicate n_ c
          return $ MV_3 n_ as bs cs
  {-# INLINE basicUnsafeRead  #-}
  basicUnsafeRead (MV_3 _ as bs cs) i_
      = do
          a <- M.basicUnsafeRead as i_
          b <- M.basicUnsafeRead bs i_
          c <- M.basicUnsafeRead cs i_
          return (a, b, c)
  {-# INLINE basicUnsafeWrite  #-}
  basicUnsafeWrite (MV_3 _ as bs cs) i_ (a, b, c)
      = do
          M.basicUnsafeWrite as i_ a
          M.basicUnsafeWrite bs i_ b
          M.basicUnsafeWrite cs i_ c
  {-# INLINE basicClear  #-}
  basicClear (MV_3 _ as bs cs)
      = do
          M.basicClear as
          M.basicClear bs
          M.basicClear cs
  {-# INLINE basicSet  #-}
  basicSet (MV_3 _ as bs cs) (a, b, c)
      = do
          M.basicSet as a
          M.basicSet bs b
          M.basicSet cs c
  {-# INLINE basicUnsafeCopy  #-}
  basicUnsafeCopy (MV_3 _ as1 bs1 cs1) (MV_3 _ as2 bs2 cs2)
      = do
          M.basicUnsafeCopy as1 as2
          M.basicUnsafeCopy bs1 bs2
          M.basicUnsafeCopy cs1 cs2
  {-# INLINE basicUnsafeMove  #-}
  basicUnsafeMove (MV_3 _ as1 bs1 cs1) (MV_3 _ as2 bs2 cs2)
      = do
          M.basicUnsafeMove as1 as2
          M.basicUnsafeMove bs1 bs2
          M.basicUnsafeMove cs1 cs2
  {-# INLINE basicUnsafeGrow  #-}
  basicUnsafeGrow (MV_3 n_ as bs cs) m_
      = do
          as' <- M.basicUnsafeGrow as m_
          bs' <- M.basicUnsafeGrow bs m_
          cs' <- M.basicUnsafeGrow cs m_
          return $ MV_3 (m_+n_) as' bs' cs'
instance (Unbox a,
          Unbox b,
          Unbox c) => G.Vector Vector (a, b, c) where
  {-# INLINE basicUnsafeFreeze  #-}
  basicUnsafeFreeze (MV_3 n_ as bs cs)
      = do
          as' <- G.basicUnsafeFreeze as
          bs' <- G.basicUnsafeFreeze bs
          cs' <- G.basicUnsafeFreeze cs
          return $ V_3 n_ as' bs' cs'
  {-# INLINE basicUnsafeThaw  #-}
  basicUnsafeThaw (V_3 n_ as bs cs)
      = do
          as' <- G.basicUnsafeThaw as
          bs' <- G.basicUnsafeThaw bs
          cs' <- G.basicUnsafeThaw cs
          return $ MV_3 n_ as' bs' cs'
  {-# INLINE basicLength  #-}
  basicLength (V_3 n_ _ _ _) = n_
  {-# INLINE basicUnsafeSlice  #-}
  basicUnsafeSlice i_ m_ (V_3 _ as bs cs)
      = V_3 m_ (G.basicUnsafeSlice i_ m_ as)
               (G.basicUnsafeSlice i_ m_ bs)
               (G.basicUnsafeSlice i_ m_ cs)
  {-# INLINE basicUnsafeIndexM  #-}
  basicUnsafeIndexM (V_3 _ as bs cs) i_
      = do
          a <- G.basicUnsafeIndexM as i_
          b <- G.basicUnsafeIndexM bs i_
          c <- G.basicUnsafeIndexM cs i_
          return (a, b, c)
  {-# INLINE basicUnsafeCopy  #-}
  basicUnsafeCopy (MV_3 _ as1 bs1 cs1) (V_3 _ as2 bs2 cs2)
      = do
          G.basicUnsafeCopy as1 as2
          G.basicUnsafeCopy bs1 bs2
          G.basicUnsafeCopy cs1 cs2
  {-# INLINE elemseq  #-}
  elemseq _ (a, b, c)
      = G.elemseq (undefined :: Vector a) a
        . G.elemseq (undefined :: Vector b) b
        . G.elemseq (undefined :: Vector c) c
data instance MVector s (a, b, c, d)
    = MV_4 {-# UNPACK #-} !Int !(MVector s a)
                               !(MVector s b)
                               !(MVector s c)
                               !(MVector s d)
data instance Vector (a, b, c, d)
    = V_4 {-# UNPACK #-} !Int !(Vector a)
                              !(Vector b)
                              !(Vector c)
                              !(Vector d)
instance (Unbox a, Unbox b, Unbox c, Unbox d) => Unbox (a, b, c, d)
instance (Unbox a,
          Unbox b,
          Unbox c,
          Unbox d) => M.MVector MVector (a, b, c, d) where
  {-# INLINE basicLength  #-}
  basicLength (MV_4 n_ _ _ _ _) = n_
  {-# INLINE basicUnsafeSlice  #-}
  basicUnsafeSlice i_ m_ (MV_4 _ as bs cs ds)
      = MV_4 m_ (M.basicUnsafeSlice i_ m_ as)
                (M.basicUnsafeSlice i_ m_ bs)
                (M.basicUnsafeSlice i_ m_ cs)
                (M.basicUnsafeSlice i_ m_ ds)
  {-# INLINE basicOverlaps  #-}
  basicOverlaps (MV_4 _ as1 bs1 cs1 ds1) (MV_4 _ as2 bs2 cs2 ds2)
      = M.basicOverlaps as1 as2
        || M.basicOverlaps bs1 bs2
        || M.basicOverlaps cs1 cs2
        || M.basicOverlaps ds1 ds2
  {-# INLINE basicUnsafeNew  #-}
  basicUnsafeNew n_
      = do
          as <- M.basicUnsafeNew n_
          bs <- M.basicUnsafeNew n_
          cs <- M.basicUnsafeNew n_
          ds <- M.basicUnsafeNew n_
          return $ MV_4 n_ as bs cs ds
  {-# INLINE basicInitialize #-}
  basicInitialize (MV_4 _ as bs cs ds)
      = do
          M.basicInitialize as
          M.basicInitialize bs
          M.basicInitialize cs
          M.basicInitialize ds
  {-# INLINE basicUnsafeReplicate  #-}
  basicUnsafeReplicate n_ (a, b, c, d)
      = do
          as <- M.basicUnsafeReplicate n_ a
          bs <- M.basicUnsafeReplicate n_ b
          cs <- M.basicUnsafeReplicate n_ c
          ds <- M.basicUnsafeReplicate n_ d
          return $ MV_4 n_ as bs cs ds
  {-# INLINE basicUnsafeRead  #-}
  basicUnsafeRead (MV_4 _ as bs cs ds) i_
      = do
          a <- M.basicUnsafeRead as i_
          b <- M.basicUnsafeRead bs i_
          c <- M.basicUnsafeRead cs i_
          d <- M.basicUnsafeRead ds i_
          return (a, b, c, d)
  {-# INLINE basicUnsafeWrite  #-}
  basicUnsafeWrite (MV_4 _ as bs cs ds) i_ (a, b, c, d)
      = do
          M.basicUnsafeWrite as i_ a
          M.basicUnsafeWrite bs i_ b
          M.basicUnsafeWrite cs i_ c
          M.basicUnsafeWrite ds i_ d
  {-# INLINE basicClear  #-}
  basicClear (MV_4 _ as bs cs ds)
      = do
          M.basicClear as
          M.basicClear bs
          M.basicClear cs
          M.basicClear ds
  {-# INLINE basicSet  #-}
  basicSet (MV_4 _ as bs cs ds) (a, b, c, d)
      = do
          M.basicSet as a
          M.basicSet bs b
          M.basicSet cs c
          M.basicSet ds d
  {-# INLINE basicUnsafeCopy  #-}
  basicUnsafeCopy (MV_4 _ as1 bs1 cs1 ds1) (MV_4 _ as2
                                                   bs2
                                                   cs2
                                                   ds2)
      = do
          M.basicUnsafeCopy as1 as2
          M.basicUnsafeCopy bs1 bs2
          M.basicUnsafeCopy cs1 cs2
          M.basicUnsafeCopy ds1 ds2
  {-# INLINE basicUnsafeMove  #-}
  basicUnsafeMove (MV_4 _ as1 bs1 cs1 ds1) (MV_4 _ as2
                                                   bs2
                                                   cs2
                                                   ds2)
      = do
          M.basicUnsafeMove as1 as2
          M.basicUnsafeMove bs1 bs2
          M.basicUnsafeMove cs1 cs2
          M.basicUnsafeMove ds1 ds2
  {-# INLINE basicUnsafeGrow  #-}
  basicUnsafeGrow (MV_4 n_ as bs cs ds) m_
      = do
          as' <- M.basicUnsafeGrow as m_
          bs' <- M.basicUnsafeGrow bs m_
          cs' <- M.basicUnsafeGrow cs m_
          ds' <- M.basicUnsafeGrow ds m_
          return $ MV_4 (m_+n_) as' bs' cs' ds'
instance (Unbox a,
          Unbox b,
          Unbox c,
          Unbox d) => G.Vector Vector (a, b, c, d) where
  {-# INLINE basicUnsafeFreeze  #-}
  basicUnsafeFreeze (MV_4 n_ as bs cs ds)
      = do
          as' <- G.basicUnsafeFreeze as
          bs' <- G.basicUnsafeFreeze bs
          cs' <- G.basicUnsafeFreeze cs
          ds' <- G.basicUnsafeFreeze ds
          return $ V_4 n_ as' bs' cs' ds'
  {-# INLINE basicUnsafeThaw  #-}
  basicUnsafeThaw (V_4 n_ as bs cs ds)
      = do
          as' <- G.basicUnsafeThaw as
          bs' <- G.basicUnsafeThaw bs
          cs' <- G.basicUnsafeThaw cs
          ds' <- G.basicUnsafeThaw ds
          return $ MV_4 n_ as' bs' cs' ds'
  {-# INLINE basicLength  #-}
  basicLength (V_4 n_ _ _ _ _) = n_
  {-# INLINE basicUnsafeSlice  #-}
  basicUnsafeSlice i_ m_ (V_4 _ as bs cs ds)
      = V_4 m_ (G.basicUnsafeSlice i_ m_ as)
               (G.basicUnsafeSlice i_ m_ bs)
               (G.basicUnsafeSlice i_ m_ cs)
               (G.basicUnsafeSlice i_ m_ ds)
  {-# INLINE basicUnsafeIndexM  #-}
  basicUnsafeIndexM (V_4 _ as bs cs ds) i_
      = do
          a <- G.basicUnsafeIndexM as i_
          b <- G.basicUnsafeIndexM bs i_
          c <- G.basicUnsafeIndexM cs i_
          d <- G.basicUnsafeIndexM ds i_
          return (a, b, c, d)
  {-# INLINE basicUnsafeCopy  #-}
  basicUnsafeCopy (MV_4 _ as1 bs1 cs1 ds1) (V_4 _ as2
                                                  bs2
                                                  cs2
                                                  ds2)
      = do
          G.basicUnsafeCopy as1 as2
          G.basicUnsafeCopy bs1 bs2
          G.basicUnsafeCopy cs1 cs2
          G.basicUnsafeCopy ds1 ds2
  {-# INLINE elemseq  #-}
  elemseq _ (a, b, c, d)
      = G.elemseq (undefined :: Vector a) a
        . G.elemseq (undefined :: Vector b) b
        . G.elemseq (undefined :: Vector c) c
        . G.elemseq (undefined :: Vector d) d
data instance MVector s (a, b, c, d, e)
    = MV_5 {-# UNPACK #-} !Int !(MVector s a)
                               !(MVector s b)
                               !(MVector s c)
                               !(MVector s d)
                               !(MVector s e)
data instance Vector (a, b, c, d, e)
    = V_5 {-# UNPACK #-} !Int !(Vector a)
                              !(Vector b)
                              !(Vector c)
                              !(Vector d)
                              !(Vector e)
instance (Unbox a,
          Unbox b,
          Unbox c,
          Unbox d,
          Unbox e) => Unbox (a, b, c, d, e)
instance (Unbox a,
          Unbox b,
          Unbox c,
          Unbox d,
          Unbox e) => M.MVector MVector (a, b, c, d, e) where
  {-# INLINE basicLength  #-}
  basicLength (MV_5 n_ _ _ _ _ _) = n_
  {-# INLINE basicUnsafeSlice  #-}
  basicUnsafeSlice i_ m_ (MV_5 _ as bs cs ds es)
      = MV_5 m_ (M.basicUnsafeSlice i_ m_ as)
                (M.basicUnsafeSlice i_ m_ bs)
                (M.basicUnsafeSlice i_ m_ cs)
                (M.basicUnsafeSlice i_ m_ ds)
                (M.basicUnsafeSlice i_ m_ es)
  {-# INLINE basicOverlaps  #-}
  basicOverlaps (MV_5 _ as1 bs1 cs1 ds1 es1) (MV_5 _ as2
                                                     bs2
                                                     cs2
                                                     ds2
                                                     es2)
      = M.basicOverlaps as1 as2
        || M.basicOverlaps bs1 bs2
        || M.basicOverlaps cs1 cs2
        || M.basicOverlaps ds1 ds2
        || M.basicOverlaps es1 es2
  {-# INLINE basicUnsafeNew  #-}
  basicUnsafeNew n_
      = do
          as <- M.basicUnsafeNew n_
          bs <- M.basicUnsafeNew n_
          cs <- M.basicUnsafeNew n_
          ds <- M.basicUnsafeNew n_
          es <- M.basicUnsafeNew n_
          return $ MV_5 n_ as bs cs ds es
  {-# INLINE basicInitialize #-}
  basicInitialize (MV_5 _ as bs cs ds es)
      = do
          M.basicInitialize as
          M.basicInitialize bs
          M.basicInitialize cs
          M.basicInitialize ds
          M.basicInitialize es
  {-# INLINE basicUnsafeReplicate  #-}
  basicUnsafeReplicate n_ (a, b, c, d, e)
      = do
          as <- M.basicUnsafeReplicate n_ a
          bs <- M.basicUnsafeReplicate n_ b
          cs <- M.basicUnsafeReplicate n_ c
          ds <- M.basicUnsafeReplicate n_ d
          es <- M.basicUnsafeReplicate n_ e
          return $ MV_5 n_ as bs cs ds es
  {-# INLINE basicUnsafeRead  #-}
  basicUnsafeRead (MV_5 _ as bs cs ds es) i_
      = do
          a <- M.basicUnsafeRead as i_
          b <- M.basicUnsafeRead bs i_
          c <- M.basicUnsafeRead cs i_
          d <- M.basicUnsafeRead ds i_
          e <- M.basicUnsafeRead es i_
          return (a, b, c, d, e)
  {-# INLINE basicUnsafeWrite  #-}
  basicUnsafeWrite (MV_5 _ as bs cs ds es) i_ (a, b, c, d, e)
      = do
          M.basicUnsafeWrite as i_ a
          M.basicUnsafeWrite bs i_ b
          M.basicUnsafeWrite cs i_ c
          M.basicUnsafeWrite ds i_ d
          M.basicUnsafeWrite es i_ e
  {-# INLINE basicClear  #-}
  basicClear (MV_5 _ as bs cs ds es)
      = do
          M.basicClear as
          M.basicClear bs
          M.basicClear cs
          M.basicClear ds
          M.basicClear es
  {-# INLINE basicSet  #-}
  basicSet (MV_5 _ as bs cs ds es) (a, b, c, d, e)
      = do
          M.basicSet as a
          M.basicSet bs b
          M.basicSet cs c
          M.basicSet ds d
          M.basicSet es e
  {-# INLINE basicUnsafeCopy  #-}
  basicUnsafeCopy (MV_5 _ as1 bs1 cs1 ds1 es1) (MV_5 _ as2
                                                       bs2
                                                       cs2
                                                       ds2
                                                       es2)
      = do
          M.basicUnsafeCopy as1 as2
          M.basicUnsafeCopy bs1 bs2
          M.basicUnsafeCopy cs1 cs2
          M.basicUnsafeCopy ds1 ds2
          M.basicUnsafeCopy es1 es2
  {-# INLINE basicUnsafeMove  #-}
  basicUnsafeMove (MV_5 _ as1 bs1 cs1 ds1 es1) (MV_5 _ as2
                                                       bs2
                                                       cs2
                                                       ds2
                                                       es2)
      = do
          M.basicUnsafeMove as1 as2
          M.basicUnsafeMove bs1 bs2
          M.basicUnsafeMove cs1 cs2
          M.basicUnsafeMove ds1 ds2
          M.basicUnsafeMove es1 es2
  {-# INLINE basicUnsafeGrow  #-}
  basicUnsafeGrow (MV_5 n_ as bs cs ds es) m_
      = do
          as' <- M.basicUnsafeGrow as m_
          bs' <- M.basicUnsafeGrow bs m_
          cs' <- M.basicUnsafeGrow cs m_
          ds' <- M.basicUnsafeGrow ds m_
          es' <- M.basicUnsafeGrow es m_
          return $ MV_5 (m_+n_) as' bs' cs' ds' es'
instance (Unbox a,
          Unbox b,
          Unbox c,
          Unbox d,
          Unbox e) => G.Vector Vector (a, b, c, d, e) where
  {-# INLINE basicUnsafeFreeze  #-}
  basicUnsafeFreeze (MV_5 n_ as bs cs ds es)
      = do
          as' <- G.basicUnsafeFreeze as
          bs' <- G.basicUnsafeFreeze bs
          cs' <- G.basicUnsafeFreeze cs
          ds' <- G.basicUnsafeFreeze ds
          es' <- G.basicUnsafeFreeze es
          return $ V_5 n_ as' bs' cs' ds' es'
  {-# INLINE basicUnsafeThaw  #-}
  basicUnsafeThaw (V_5 n_ as bs cs ds es)
      = do
          as' <- G.basicUnsafeThaw as
          bs' <- G.basicUnsafeThaw bs
          cs' <- G.basicUnsafeThaw cs
          ds' <- G.basicUnsafeThaw ds
          es' <- G.basicUnsafeThaw es
          return $ MV_5 n_ as' bs' cs' ds' es'
  {-# INLINE basicLength  #-}
  basicLength (V_5 n_ _ _ _ _ _) = n_
  {-# INLINE basicUnsafeSlice  #-}
  basicUnsafeSlice i_ m_ (V_5 _ as bs cs ds es)
      = V_5 m_ (G.basicUnsafeSlice i_ m_ as)
               (G.basicUnsafeSlice i_ m_ bs)
               (G.basicUnsafeSlice i_ m_ cs)
               (G.basicUnsafeSlice i_ m_ ds)
               (G.basicUnsafeSlice i_ m_ es)
  {-# INLINE basicUnsafeIndexM  #-}
  basicUnsafeIndexM (V_5 _ as bs cs ds es) i_
      = do
          a <- G.basicUnsafeIndexM as i_
          b <- G.basicUnsafeIndexM bs i_
          c <- G.basicUnsafeIndexM cs i_
          d <- G.basicUnsafeIndexM ds i_
          e <- G.basicUnsafeIndexM es i_
          return (a, b, c, d, e)
  {-# INLINE basicUnsafeCopy  #-}
  basicUnsafeCopy (MV_5 _ as1 bs1 cs1 ds1 es1) (V_5 _ as2
                                                      bs2
                                                      cs2
                                                      ds2
                                                      es2)
      = do
          G.basicUnsafeCopy as1 as2
          G.basicUnsafeCopy bs1 bs2
          G.basicUnsafeCopy cs1 cs2
          G.basicUnsafeCopy ds1 ds2
          G.basicUnsafeCopy es1 es2
  {-# INLINE elemseq  #-}
  elemseq _ (a, b, c, d, e)
      = G.elemseq (undefined :: Vector a) a
        . G.elemseq (undefined :: Vector b) b
        . G.elemseq (undefined :: Vector c) c
        . G.elemseq (undefined :: Vector d) d
        . G.elemseq (undefined :: Vector e) e
data instance MVector s (a, b, c, d, e, f)
    = MV_6 {-# UNPACK #-} !Int !(MVector s a)
                               !(MVector s b)
                               !(MVector s c)
                               !(MVector s d)
                               !(MVector s e)
                               !(MVector s f)
data instance Vector (a, b, c, d, e, f)
    = V_6 {-# UNPACK #-} !Int !(Vector a)
                              !(Vector b)
                              !(Vector c)
                              !(Vector d)
                              !(Vector e)
                              !(Vector f)
instance (Unbox a,
          Unbox b,
          Unbox c,
          Unbox d,
          Unbox e,
          Unbox f) => Unbox (a, b, c, d, e, f)
instance (Unbox a,
          Unbox b,
          Unbox c,
          Unbox d,
          Unbox e,
          Unbox f) => M.MVector MVector (a, b, c, d, e, f) where
  {-# INLINE basicLength  #-}
  basicLength (MV_6 n_ _ _ _ _ _ _) = n_
  {-# INLINE basicUnsafeSlice  #-}
  basicUnsafeSlice i_ m_ (MV_6 _ as bs cs ds es fs)
      = MV_6 m_ (M.basicUnsafeSlice i_ m_ as)
                (M.basicUnsafeSlice i_ m_ bs)
                (M.basicUnsafeSlice i_ m_ cs)
                (M.basicUnsafeSlice i_ m_ ds)
                (M.basicUnsafeSlice i_ m_ es)
                (M.basicUnsafeSlice i_ m_ fs)
  {-# INLINE basicOverlaps  #-}
  basicOverlaps (MV_6 _ as1 bs1 cs1 ds1 es1 fs1) (MV_6 _ as2
                                                         bs2
                                                         cs2
                                                         ds2
                                                         es2
                                                         fs2)
      = M.basicOverlaps as1 as2
        || M.basicOverlaps bs1 bs2
        || M.basicOverlaps cs1 cs2
        || M.basicOverlaps ds1 ds2
        || M.basicOverlaps es1 es2
        || M.basicOverlaps fs1 fs2
  {-# INLINE basicUnsafeNew  #-}
  basicUnsafeNew n_
      = do
          as <- M.basicUnsafeNew n_
          bs <- M.basicUnsafeNew n_
          cs <- M.basicUnsafeNew n_
          ds <- M.basicUnsafeNew n_
          es <- M.basicUnsafeNew n_
          fs <- M.basicUnsafeNew n_
          return $ MV_6 n_ as bs cs ds es fs
  {-# INLINE basicInitialize #-}
  basicInitialize (MV_6 _ as bs cs ds es fs)
      = do
          M.basicInitialize as
          M.basicInitialize bs
          M.basicInitialize cs
          M.basicInitialize ds
          M.basicInitialize es
          M.basicInitialize fs
  {-# INLINE basicUnsafeReplicate  #-}
  basicUnsafeReplicate n_ (a, b, c, d, e, f)
      = do
          as <- M.basicUnsafeReplicate n_ a
          bs <- M.basicUnsafeReplicate n_ b
          cs <- M.basicUnsafeReplicate n_ c
          ds <- M.basicUnsafeReplicate n_ d
          es <- M.basicUnsafeReplicate n_ e
          fs <- M.basicUnsafeReplicate n_ f
          return $ MV_6 n_ as bs cs ds es fs
  {-# INLINE basicUnsafeRead  #-}
  basicUnsafeRead (MV_6 _ as bs cs ds es fs) i_
      = do
          a <- M.basicUnsafeRead as i_
          b <- M.basicUnsafeRead bs i_
          c <- M.basicUnsafeRead cs i_
          d <- M.basicUnsafeRead ds i_
          e <- M.basicUnsafeRead es i_
          f <- M.basicUnsafeRead fs i_
          return (a, b, c, d, e, f)
  {-# INLINE basicUnsafeWrite  #-}
  basicUnsafeWrite (MV_6 _ as bs cs ds es fs) i_ (a, b, c, d, e, f)
      = do
          M.basicUnsafeWrite as i_ a
          M.basicUnsafeWrite bs i_ b
          M.basicUnsafeWrite cs i_ c
          M.basicUnsafeWrite ds i_ d
          M.basicUnsafeWrite es i_ e
          M.basicUnsafeWrite fs i_ f
  {-# INLINE basicClear  #-}
  basicClear (MV_6 _ as bs cs ds es fs)
      = do
          M.basicClear as
          M.basicClear bs
          M.basicClear cs
          M.basicClear ds
          M.basicClear es
          M.basicClear fs
  {-# INLINE basicSet  #-}
  basicSet (MV_6 _ as bs cs ds es fs) (a, b, c, d, e, f)
      = do
          M.basicSet as a
          M.basicSet bs b
          M.basicSet cs c
          M.basicSet ds d
          M.basicSet es e
          M.basicSet fs f
  {-# INLINE basicUnsafeCopy  #-}
  basicUnsafeCopy (MV_6 _ as1 bs1 cs1 ds1 es1 fs1) (MV_6 _ as2
                                                           bs2
                                                           cs2
                                                           ds2
                                                           es2
                                                           fs2)
      = do
          M.basicUnsafeCopy as1 as2
          M.basicUnsafeCopy bs1 bs2
          M.basicUnsafeCopy cs1 cs2
          M.basicUnsafeCopy ds1 ds2
          M.basicUnsafeCopy es1 es2
          M.basicUnsafeCopy fs1 fs2
  {-# INLINE basicUnsafeMove  #-}
  basicUnsafeMove (MV_6 _ as1 bs1 cs1 ds1 es1 fs1) (MV_6 _ as2
                                                           bs2
                                                           cs2
                                                           ds2
                                                           es2
                                                           fs2)
      = do
          M.basicUnsafeMove as1 as2
          M.basicUnsafeMove bs1 bs2
          M.basicUnsafeMove cs1 cs2
          M.basicUnsafeMove ds1 ds2
          M.basicUnsafeMove es1 es2
          M.basicUnsafeMove fs1 fs2
  {-# INLINE basicUnsafeGrow  #-}
  basicUnsafeGrow (MV_6 n_ as bs cs ds es fs) m_
      = do
          as' <- M.basicUnsafeGrow as m_
          bs' <- M.basicUnsafeGrow bs m_
          cs' <- M.basicUnsafeGrow cs m_
          ds' <- M.basicUnsafeGrow ds m_
          es' <- M.basicUnsafeGrow es m_
          fs' <- M.basicUnsafeGrow fs m_
          return $ MV_6 (m_+n_) as' bs' cs' ds' es' fs'
instance (Unbox a,
          Unbox b,
          Unbox c,
          Unbox d,
          Unbox e,
          Unbox f) => G.Vector Vector (a, b, c, d, e, f) where
  {-# INLINE basicUnsafeFreeze  #-}
  basicUnsafeFreeze (MV_6 n_ as bs cs ds es fs)
      = do
          as' <- G.basicUnsafeFreeze as
          bs' <- G.basicUnsafeFreeze bs
          cs' <- G.basicUnsafeFreeze cs
          ds' <- G.basicUnsafeFreeze ds
          es' <- G.basicUnsafeFreeze es
          fs' <- G.basicUnsafeFreeze fs
          return $ V_6 n_ as' bs' cs' ds' es' fs'
  {-# INLINE basicUnsafeThaw  #-}
  basicUnsafeThaw (V_6 n_ as bs cs ds es fs)
      = do
          as' <- G.basicUnsafeThaw as
          bs' <- G.basicUnsafeThaw bs
          cs' <- G.basicUnsafeThaw cs
          ds' <- G.basicUnsafeThaw ds
          es' <- G.basicUnsafeThaw es
          fs' <- G.basicUnsafeThaw fs
          return $ MV_6 n_ as' bs' cs' ds' es' fs'
  {-# INLINE basicLength  #-}
  basicLength (V_6 n_ _ _ _ _ _ _) = n_
  {-# INLINE basicUnsafeSlice  #-}
  basicUnsafeSlice i_ m_ (V_6 _ as bs cs ds es fs)
      = V_6 m_ (G.basicUnsafeSlice i_ m_ as)
               (G.basicUnsafeSlice i_ m_ bs)
               (G.basicUnsafeSlice i_ m_ cs)
               (G.basicUnsafeSlice i_ m_ ds)
               (G.basicUnsafeSlice i_ m_ es)
               (G.basicUnsafeSlice i_ m_ fs)
  {-# INLINE basicUnsafeIndexM  #-}
  basicUnsafeIndexM (V_6 _ as bs cs ds es fs) i_
      = do
          a <- G.basicUnsafeIndexM as i_
          b <- G.basicUnsafeIndexM bs i_
          c <- G.basicUnsafeIndexM cs i_
          d <- G.basicUnsafeIndexM ds i_
          e <- G.basicUnsafeIndexM es i_
          f <- G.basicUnsafeIndexM fs i_
          return (a, b, c, d, e, f)
  {-# INLINE basicUnsafeCopy  #-}
  basicUnsafeCopy (MV_6 _ as1 bs1 cs1 ds1 es1 fs1) (V_6 _ as2
                                                          bs2
                                                          cs2
                                                          ds2
                                                          es2
                                                          fs2)
      = do
          G.basicUnsafeCopy as1 as2
          G.basicUnsafeCopy bs1 bs2
          G.basicUnsafeCopy cs1 cs2
          G.basicUnsafeCopy ds1 ds2
          G.basicUnsafeCopy es1 es2
          G.basicUnsafeCopy fs1 fs2
  {-# INLINE elemseq  #-}
  elemseq _ (a, b, c, d, e, f)
      = G.elemseq (undefined :: Vector a) a
        . G.elemseq (undefined :: Vector b) b
        . G.elemseq (undefined :: Vector c) c
        . G.elemseq (undefined :: Vector d) d
        . G.elemseq (undefined :: Vector e) e
        . G.elemseq (undefined :: Vector f) f

ext: hs

max_stars_repo_name: BinderDavid/fragnix

max_issues_repo_name: BinderDavid/fragnix

max_forks_repo_name: BinderDavid/fragnix


text:
module Sound.Plot where

import qualified Data.Stream as S

import           Numeric.LinearAlgebra

import           Sound.Types

import           Graphics.Rendering.Plot (Series)
import qualified Graphics.Rendering.Plot as P
import           Graphics.Rendering.Plot.Gtk (PlotHandle)
import qualified Graphics.Rendering.Plot.Gtk as P

plot :: Int -> [Audio] -> IO PlotHandle
plot n audio = P.display $ do
  P.plot (P.Line, map (fromList . S.take n) audio :: [Series])
  P.withTextDefaults $ P.setFontFamily "Ubuntu"
  P.xlabel "samples"
  P.ylabel "amplitude"
  P.yrange P.Linear (-1) 1

max_stars_repo_name: svenkeidel/hsynth

max_issues_repo_name: svenkeidel/hsynth

max_forks_repo_name: svenkeidel/hsynth


text:
module Section_2_5_2 where

import Numeric.LinearAlgebra

import Section_2_3_3 (and', nand, or')

xor :: Double -> Double -> Double
xor x1 x2 =
  let s1 = nand x1 x2
      s2 = or' x1 x2
      y = and' s1 s2
   in y


text:
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DeriveDataTypeable, DeriveGeneric #-}
-- |
-- Module    : Statistics.Distribution.Binomial
-- Copyright : (c) 2009 Bryan O'Sullivan
-- License   : BSD3
--
-- Maintainer  : bos@serpentine.com
-- Stability   : experimental
-- Portability : portable
--
-- The binomial distribution.  This is the discrete probability
-- distribution of the number of successes in a sequence of /n/
-- independent yes\/no experiments, each of which yields success with
-- probability /p/.

module Statistics.Distribution.Binomial
    (
      BinomialDistribution
    -- * Constructors
    , binomial
    , binomialE
    -- * Accessors
    , bdTrials
    , bdProbability
    ) where

import Control.Applicative
import Data.Aeson            (FromJSON(..), ToJSON, Value(..), (.:))
import Data.Binary           (Binary(..))
import Data.Data             (Data, Typeable)
import GHC.Generics          (Generic)
import Numeric.SpecFunctions           (choose,logChoose,incompleteBeta,log1p)
import Numeric.MathFunctions.Constants (m_epsilon)

import qualified Statistics.Distribution as D
import qualified Statistics.Distribution.Poisson.Internal as I
import Statistics.Internal

-- | The binomial distribution.
data BinomialDistribution = BD {
      bdTrials      :: {-# UNPACK #-} !Int
    -- ^ Number of trials.
    , bdProbability :: {-# UNPACK #-} !Double
    -- ^ Probability.
    } deriving (Eq, Typeable, Data, Generic)

instance Show BinomialDistribution where
  showsPrec i (BD n p) = defaultShow2 "binomial" n p i
instance Read BinomialDistribution where
  readPrec = defaultReadPrecM2 "binomial" binomialE

instance ToJSON BinomialDistribution
instance FromJSON BinomialDistribution where
  parseJSON (Object v) = do
    n <- v .: "bdTrials"
    p <- v .: "bdProbability"
    maybe (fail $ errMsg n p) return $ binomialE n p
  parseJSON _ = empty

instance Binary BinomialDistribution where
  put (BD x y) = put x >> put y
  get = do
    n <- get
    p <- get
    maybe (fail $ errMsg n p) return $ binomialE n p

instance D.Distribution BinomialDistribution where
    cumulative = cumulative

instance D.DiscreteDistr BinomialDistribution where
    probability    = probability
    logProbability = logProbability

instance D.Mean BinomialDistribution where
    mean = mean

instance D.Variance BinomialDistribution where
    variance = variance

instance D.MaybeMean BinomialDistribution where
    maybeMean = Just . D.mean

instance D.MaybeVariance BinomialDistribution where
    maybeStdDev   = Just . D.stdDev
    maybeVariance = Just . D.variance

instance D.Entropy BinomialDistribution where
  entropy (BD n p)
    | n == 0 = 0
    | n <= 100 = directEntropy (BD n p)
    | otherwise = I.poissonEntropy (fromIntegral n * p)

instance D.MaybeEntropy BinomialDistribution where
  maybeEntropy = Just . D.entropy

-- This could be slow for big n
probability :: BinomialDistribution -> Int -> Double
probability (BD n p) k
  | k < 0 || k > n = 0
  | n == 0         = 1
    -- choose could overflow Double for n >= 1030 so we switch to
    -- log-domain to calculate probability
  | n < 1000       = choose n k * p^k * (1-p)^(n-k)
  | otherwise      = exp $ logChoose n k + log p * k' + log1p (-p) * nk'
  where
    k'  = fromIntegral k
    nk' = fromIntegral $ n - k

logProbability :: BinomialDistribution -> Int -> Double
logProbability (BD n p) k
  | k < 0 || k > n          = (-1)/0
  | n == 0                  = 0
  | otherwise               = logChoose n k + log p * k' + log1p (-p) * nk'
  where
    k'  = fromIntegral   k
    nk' = fromIntegral $ n - k

-- Summation from different sides required to reduce roundoff errors
cumulative :: BinomialDistribution -> Double -> Double
cumulative (BD n p) x
  | isNaN x      = error "Statistics.Distribution.Binomial.cumulative: NaN input"
  | isInfinite x = if x > 0 then 1 else 0
  | k <  0       = 0
  | k >= n       = 1
  | otherwise    = incompleteBeta (fromIntegral (n-k)) (fromIntegral (k+1)) (1 - p)
  where
    k = floor x

mean :: BinomialDistribution -> Double
mean (BD n p) = fromIntegral n * p

variance :: BinomialDistribution -> Double
variance (BD n p) = fromIntegral n * p * (1 - p)

directEntropy :: BinomialDistribution -> Double
directEntropy d@(BD n _) =
  negate . sum $
  takeWhile (< negate m_epsilon) $
  dropWhile (not . (< negate m_epsilon)) $
  [ let x = probability d k in x * log x | k <- [0..n]]

-- | Construct binomial distribution. Number of trials must be
--   non-negative and probability must be in [0,1] range
binomial :: Int                 -- ^ Number of trials.
         -> Double              -- ^ Probability.
         -> BinomialDistribution
binomial n p = maybe (error $ errMsg n p) id $ binomialE n p

-- | Construct binomial distribution. Number of trials must be
--   non-negative and probability must be in [0,1] range
binomialE :: Int                 -- ^ Number of trials.
          -> Double              -- ^ Probability.
          -> Maybe BinomialDistribution
binomialE n p
  | n < 0            = Nothing
  | p >= 0 || p <= 1 = Just (BD n p)
  | otherwise        = Nothing

errMsg :: Int -> Double -> String
errMsg n p
  = "Statistics.Distribution.Binomial.binomial: n=" ++ show n
  ++ " p=" ++ show p ++ "but n>=0 and p in [0,1]"


text:
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverloadedStrings #-}
module Workflow
    ( WorkflowStage, prev, quantiles, waves, task, meanT, varT
    , Task(..)
    , Indices, Nodes
    , WFType(Sequential, Parallel, Complex)
    , wrap, calculateCDF, calculateCDF', distributionBounds
    , stageOrder,applyDE,applyRS,reverseEdges
    ) where
import Data.Array.Unboxed (listArray)
import Data.Complex (Complex)
import qualified Data.Map as Map
import qualified Data.List as List
import Data.Aeson
import Data.Aeson.Types
import qualified Data.Vector as Vector
import Data.Maybe (fromMaybe)

import Workflow.Toolset

-- | List of indices of the stages in Workflow Nodes Map
type Indices = [Int]

-- | Map structure, stores all stages of the workflow
type Nodes = Map.Map Int WorkflowStage

-- | A stage of the workflow - can be either an atomic task or a workflow
data WorkflowStage = Stage
    { prev      :: Indices          -- ^ previous stages
    , quantiles :: [Double]         -- ^ values of CDF at certain points
    , waves     :: [Complex Double] -- ^ values of characteristic functions at certain points
    , task      :: Task             -- ^ task (or sub-workflow) for execution at this stage
    , meanT     :: Double           -- ^ mean execution time estimation
    , varT      :: Double           -- ^ estimation of execution time variance
    } deriving (Eq)

-- | Representation of the task - it can be a single task or a package
data Task = AtomicTask
    { sample :: Array1D Double -- ^ adjusted data (T' = mu' + sigma' (T(X) - mu(X)) / sigma(X) )
    } | Workflow
    { nodes  :: Nodes   -- ^ list of stages and their predecessors
    , leafs  :: Indices -- ^ last tasks in the workflow
                        --   (workflow is fully executed if these tasks are finished)
    , wftype :: WFType  -- ^ whether this is primitive workflow or complex one
    } deriving (Eq)

-- | Type of the workflow.
--   Depending on the type of the workflow, distribution is calculated in different ways
data WFType = Sequential -- ^ can apply RS reduction
            | Parallel   -- ^ can apply RF reduction
            | Complex    -- ^ cannot reduce workflow
            deriving (Eq,Show)

-- | Wrap the task into workflow stage
--   Arguments: current task, and an array of previous (required) stages
wrap :: Task -> Indices -> WorkflowStage
wrap t@(AtomicTask {sample=arr}) preds = Stage
    { prev      = preds
    , task      = t
    , meanT     = mean arr
    , varT      = var arr
    , quantiles = undefined
    , waves     = undefined
    }
wrap t@(Workflow {nodes=ns,leafs=xs}) preds = Stage
    { prev      = preds
    , task      = t
    , meanT     = foldr1 max (xs >>= f meanT)
    , varT      = foldr1 max (xs >>= f varT)
    , quantiles = undefined
    , waves     = undefined
    }
  where
    f r i = g r ((Map.!) ns i)
    g r x@(Stage {prev=[]}) = [r x]
    g r x = map (+ r x) . concat . map (f r) . prev $ x

-- |
-- Calculate probability distribution of the workflow execution time
-- Arguments: workflow, bounds size (in sigmas), grid density k : n = 2^k
-- Return: ((a,b),wf); (a,b) - integration bounds, wf - resulting workflow
calculateCDF :: WorkflowStage -> Double -> Int -> ((Double,Double),WorkflowStage)
calculateCDF s q k = (bnds, ccdf (map (\x' -> x'-m*c) x, t) s) where
    grid@(x,t) = linSpace k bnds
    bnds@(a,b) = distributionBounds s q
    c = (b-a) / (fromIntegral . length . fst $ grid)
    m = fromIntegral . (round :: Double -> Int) $ meanT s / c

-- |
-- Calculate probability distribution of the workflow execution time
-- Arguments: workflow, grid
-- Return: ((a,b),wf); (a,b) - integration bounds, wf - resulting workflow
calculateCDF' :: WorkflowStage -> ([Double],[Double]) -> ((Double,Double),WorkflowStage)
calculateCDF' s grd = (bnds, ccdf (map (\x' -> x'-m*c) x, t) s) where
    grid@(x,t) = grd
    bnds@(a,b) = (head x, last x)
    c = (b-a) / (fromIntegral . length . fst $ grid)
    m = fromIntegral . (round :: Double -> Int) $ meanT s / c

-- | calculate cdf with grid as input
ccdf :: ([Double],[Double]) -> WorkflowStage -> WorkflowStage
ccdf g@(xGrid,tGrid) s = s{
    quantiles = qu,
    waves = wa,
    task  = newt
    } where
        n = length xGrid
        n2 = length tGrid
        c = (last xGrid - head xGrid) / fromIntegral (n-1)
        m = fromIntegral . (round :: Double -> Int) $ meanT s / c
        xg = map (+m*c) xGrid
        qu = df newt
        wa = cf newt
        newt = parseInner . task $ s
        parseInner t@(AtomicTask {}) = t
        parseInner t@(Workflow {nodes=ns}) = t{nodes = Map.map (ccdf g) ns}
        df (AtomicTask {sample=arr}) = distributionF arr xg
        df (Workflow {nodes=_ns,wftype=Sequential})
            = charToDist (map (+m*c) xGrid, tGrid) wa
        df (Workflow {nodes=ns,wftype=Parallel})
            = Map.fold (zipWith (*) . quantiles) (replicate n 1) ns
        df (Workflow {nodes=ns,leafs=lfs,wftype=Complex})
            = foldr (\(phi1,eX1) (f2) ->
                let m1 = min m $ fromIntegral . (round :: Double -> Int) $ eX1 / c
                    f1 = charToDist (map (+m1*c) xGrid, tGrid) phi1
                    md = round $ m - m1
                    f1' = drop md f1 ++ replicate md 1
                in zipWith (*) f2 f1'
                ) (replicate n 1) (lfs >>= f) where
                f i = gx ((Map.!) ns i)
                gx x@(Stage {prev=[]}) = [(waves x, meanT x)]
                gx x = map ((\(a,b) (e,d) -> (zipWith (*) a e,b+d)) (waves x, meanT x))
                    . concat . map f . prev $ x
        cf (AtomicTask {sample=arr}) = characteristicF arr tGrid
        cf (Workflow {nodes=ns,wftype=Sequential})
            = Map.fold (zipWith (*) . waves) (replicate n2 1) ns
        cf (Workflow {})
            = distToChar (xg, tGrid) qu

-- | Approximate bounds, such that total distribution almost fully lies in these bounds.
--   "k" stands for k-sigma rule (i.e. interval width is 2k*sigma).
--   (a,b) : 0 <= a <= b
distributionBounds :: WorkflowStage -> Double -> (Double,Double)
distributionBounds x q = (max 0 $ m - s, m + s) where
    m = meanT x
    s = q * (sqrt . varT $ x)

-- | Apply DE-reduction (delete redundant edges)
applyDE :: Task -> Task
applyDE t@(AtomicTask {}) = t
applyDE wf@(Workflow {nodes = ns}) = wf{nodes = Map.mapWithKey (\i s -> modify ((Map.!) orders i) s ) ns} where
    orders = stageOrder wf
    modify _ s@(Stage {prev=[]}) = s
    modify _ s@(Stage {prev=[_]}) = s
    modify [_] s = s
    modify ((o,_):ords) s = s{prev = fst $ foldr f ([],[]) ords} where
        f (_,is) (os,iss) = ( mergeOrderedLists os fis
                            ,foldr mergeOrderedLists iss . map (prevTillOrder (o-1)) $ fis
                            ) where fis = filter (not . flip isInOrderedList iss) is
    modify [] s = s
    prevTillOrder o i = if so <= o then [] else foldr mergeOrderedLists xs ys where
        s = (Map.!) ns i
        xs = prev s
        ys = map (prevTillOrder o) xs
        so = fst . last $ (Map.!) orders i

-- | Apply RS-reduction (Reduce Sequential stages to single stage)
applyRS :: Task -> Task
applyRS t@(AtomicTask {}) = t
applyRS wf@(Workflow {nodes = ns}) = wf{nodes = Map.union changed unchanged} where
    re = reverseEdges wf
    candidates =
        Map.filterWithKey (\i _ -> ((1/=) . length . (Map.!) re $ i) ||
        ((1>) . length . prev . (Map.!) ns . head . (Map.!) re $ i))
        . Map.filter ((1==) . length . (Map.!) re . head . prev )
        . Map.filter ((1==) . length . prev) $ ns
    appendPath Stage {prev=[]} = []
    appendPath Stage {prev=[i]} = if (1==) . length . (Map.!) re $ i then i:(appendPath $ (Map.!) ns i) else []
    appendPath Stage {} = []
    allSeqs = Map.mapWithKey (\i s -> i:(appendPath s)) candidates
    unchanged = Map.filterWithKey (\i _ -> isInOrderedList i unchKeys) ns where
        unchKeys = Map.keys ns List.\\ (concat . Map.elems $ allSeqs)
    changed = Map.map createSubWF allSeqs
    createSubWF xs = wrap Workflow {
        nodes = nnns,
        leafs = [head xs],
        wftype = Sequential
        } preds where
            nns = Map.filterWithKey (\i _ -> any (i==) xs) ns
            nnns = Map.adjust (\s -> s{prev=[]}) (last xs) nns
            preds = prev . (Map.!) ns . last $ xs

-- | helper function: return a map with tasks' execution orders
-- required to proceed with Delete Edge (DE) reduction
-- Arguments: Workflow
-- return map (Index of node -> value), where value: (order of this stage o, indices of previous stages with order o-1)
stageOrder :: Task -> Map.Map Int [(Int,Indices)]
stageOrder AtomicTask {} = Map.empty
stageOrder Workflow {leafs = lfs, nodes = ns} = List.foldl' ind' Map.empty lfs where
    ind' m i = m `seq` ind m ((Map.!) ns i) i
    ind m (Stage {prev=[]}) i = if Map.member i m then m else Map.insert i [(1, [])] m
    ind m (Stage {prev=xs}) i = if Map.member i m then m else
                Map.insert i (List.foldr insertNode [] . map createNode $ xs) cm where
                    cm = List.foldl' ind' m xs
                    createNode j = ((1+) . fst . last . (Map.!) cm $ j,[j])
    insertNode x [] = [x]
    insertNode x@(o,i) xxs@((o1,i1):xs)
        | o == o1   = (o1, mergeOrderedLists i i1):xs
        | o <  o1   = x:xxs
        | otherwise = (o1,i1):(insertNode x xs)

-- test it: giveWorkflow >>= mapM_ print . Map.toList . stageOrder . task

-- | helper function: return a map with "next" edges created from "prev" field of (next) stages
reverseEdges :: Task -> Map.Map Int Indices
reverseEdges AtomicTask {} = Map.empty
reverseEdges Workflow{nodes = ns} =
    Map.foldWithKey f (Map.fromAscList . zip (Map.keys ns) . repeat $ []) ns where
        f i Stage{prev = xs} m = foldr (Map.adjust (i:)) m xs

-- | helper function: merge two ordered lists into one ordered list - O(n+m)
mergeOrderedLists :: (Ord a) => [a] -> [a] -> [a]
mergeOrderedLists [] xs = xs
mergeOrderedLists xs [] = xs
mergeOrderedLists (x1:xs1) (x2:xs2) | x1 == x2  = x1 :(mergeOrderedLists xs1 xs2)
                                    | x1 > x2   = x2 :(mergeOrderedLists (x1:xs1) xs2)
                                    | otherwise = x1 :(mergeOrderedLists xs1 (x2:xs2))

-- | helper function: if x is a member of an ordered list - O(n)
isInOrderedList :: (Ord a) => a -> [a] -> Bool
isInOrderedList _ [] = False
isInOrderedList x [y] = x == y
isInOrderedList x (y:ys) | x == y    = True
                         | x >  y    = isInOrderedList x ys
                         | otherwise = False

instance Show Task where
    show AtomicTask {} = "AtomicTask"
    show Workflow {wftype = tp, nodes = ns, leafs = lfs} =
        show tp ++ " Workflow: leafs = " ++ show lfs ++
        "; nodes = " ++ show (Map.toList ns) ++ "."

instance Show WorkflowStage where
    show Stage {prev = [], task = (AtomicTask {})} = "AtomicTask"
    show Stage {prev = pr, task = (AtomicTask {})} = "AtomicTask (prev = " ++ show pr ++ ")"
    show Stage {prev = [], task = wf@Workflow {}} = show wf
    show Stage {prev = pr, task = (Workflow {wftype = tp, nodes = ns})} =
        show tp ++ " Workflow (size = " ++ show (Map.size ns) ++ ", prev = " ++ show pr ++ ")"

instance FromJSON Task where
    parseJSON (Object v) = do
        mleafs <- v .:? "leafs"
        mtasks <- v .:? "tasks"
        case (,) <$> mleafs <*> mtasks of
          Nothing -> AtomicTask . (\a -> listArray (1,length a) a) <$> v .: "sample"
          Just (lfs, tasks) -> return $ if Map.size tasks == 1
                               then task . head $ Map.elems tasks
                               else Workflow tasks lfs (inferWFType tasks lfs)
      where
       -- Safely escape (hopefully) impossible workflows
       inferWFType stages _ | Map.null stages = Complex
       -- check if fully sequential workflow
       inferWFType stages [oneleaf] = goChain stages oneleaf 1
       -- check if fully parallel workflow
       inferWFType stages lfs = if (length lfs == length stages)
                                    && (all (\s -> prev s == prev st) stgs)
                                then Parallel else Complex
                              where
                                st:stgs = Map.elems stages
       -- go through sequence of nodes to check if it is a sequential wf
       goChain stages lf n |   n == Map.size stages
                            && all (flip Map.notMember stages) (prev (stages Map.! lf)) = Sequential
                           |  p <- (prev (stages Map.! lf))
                              ,  n < Map.size stages
                              && length p == 1
                              && Map.member (head p) stages = goChain stages (head p) (n+1)
                           | otherwise = Complex
    parseJSON invalid = typeMismatch "Task" invalid

data IdTask = IdTask Int WorkflowStage

instance FromJSON IdTask where
    parseJSON ov@(Object v) = IdTask
        <$> v .: "id"
        <*> parseJSON ov
    parseJSON invalid = typeMismatch "(Int, Task)" invalid

instance FromJSON Nodes where
    parseJSON (Array v) = Map.fromList . map (\(IdTask i t) -> (i, t)) . Vector.toList <$> mapM parseJSON v
    parseJSON invalid = typeMismatch "AtomicTask" invalid

instance FromJSON WorkflowStage where
    parseJSON ov@(Object v) = do
      t  <- parseJSON ov
      mdeps <- v .:? "deps"
      return $ wrap t (fromMaybe [] mdeps)
    parseJSON invalid = typeMismatch "WorkflowStage" invalid

max_stars_repo_name: achirkin/haskell-estimatewf

max_issues_repo_name: achirkin/haskell-estimatewf

max_forks_repo_name: achirkin/haskell-estimatewf