module Feldspar.Core.Constructs.MutableToPure
( MutableToPure (..)
) where
import qualified Control.Exception as C
import Data.Array.IArray
import Data.Array.MArray (freeze)
import Data.Array.Unsafe (unsafeFreeze)
import System.IO.Unsafe
import Language.Syntactic
import Language.Syntactic.Constructs.Binding
import Feldspar.Lattice
import Feldspar.Core.Types
import Feldspar.Core.Interpretation
data MutableToPure a where
RunMutableArray :: Type a => MutableToPure (Mut (MArr a) :-> Full [a])
WithArray :: Type b => MutableToPure (MArr a :-> ([a] -> Mut b) :-> Full (Mut b))
instance Semantic MutableToPure
where
semantics RunMutableArray = Sem "runMutableArray" runMutableArrayEval
semantics WithArray = Sem "withArray" withArrayEval
runMutableArrayEval :: forall a . Mut (MArr a) -> [a]
runMutableArrayEval m = unsafePerformIO $
do marr <- m
iarr <- unsafeFreeze marr
return (elems (iarr :: Array WordN a))
withArrayEval :: forall a b. MArr a -> ([a] -> Mut b) -> Mut b
withArrayEval ma f
= do a <- f (elems (unsafePerformIO $ freeze ma :: Array WordN a))
C.evaluate a
instance Typed MutableToPure
where
typeDictSym RunMutableArray = Just Dict
typeDictSym _ = Nothing
semanticInstances ''MutableToPure
instance EvalBind MutableToPure where evalBindSym = evalBindSymDefault
instance AlphaEq dom dom dom env => AlphaEq MutableToPure MutableToPure dom env
where
alphaEqSym = alphaEqSymDefault
instance Sharable MutableToPure
instance Monotonic MutableToPure
instance SizeProp MutableToPure
where
sizeProp RunMutableArray (WrapFull arr :* Nil) = infoSize arr
sizeProp WithArray _ = universal
instance (MutableToPure :<: dom, Optimize dom dom) => Optimize MutableToPure dom
where
constructFeatUnOpt opts RunMutableArray args = constructFeatUnOptDefaultTyp opts typeRep RunMutableArray args
constructFeatUnOpt opts WithArray args = constructFeatUnOptDefaultTyp opts (MutType typeRep) WithArray args