{-# Language CPP #-}
-- | Dependency tracking
module Csound.Dynamic.Types.Dep(
    DepT(..), LocalHistory(..), runDepT, execDepT, evalDepT,
    -- * Dependencies
    depT, depT_, mdepT, stripDepT, stmtOnlyT,

    -- * Variables
    newLocalVar, newLocalVars,
    writeVar, readVar, readOnlyVar, initVar, appendVarBy,

    -- * Arrays
    newLocalArrVar, newTmpArrVar,
    readArr, readOnlyArr, writeArr, writeInitArr, initArr, appendArrBy,

    -- * Read macros
    readMacrosDouble, readMacrosInt, readMacrosString,
    initMacrosDouble, initMacrosString, initMacrosInt
) where

#if __GLASGOW_HASKELL__ < 710
import Control.Applicative
#endif

import Control.Monad.Trans.Class
import Control.Monad.Trans.State.Strict
import Control.Monad(ap, liftM, zipWithM_)
import Data.Default

import Data.Fix(Fix(..))

import Csound.Dynamic.Types.Exp

-- | Csound's synonym for 'IO'-monad. 'Dep' means Side Effect.
-- You will bump into 'Dep' trying to read and write to delay lines,
-- making random signals or trying to save your audio to file.
-- Instrument is expected to return a value of @Dep [Sig]@.
-- So it's okay to do some side effects when playing a note.
newtype DepT m a = DepT { DepT m a -> StateT LocalHistory m a
unDepT :: StateT LocalHistory m a }

data LocalHistory = LocalHistory
    { LocalHistory -> E
expDependency :: E
    , LocalHistory -> Int
newLineNum    :: Int
    , LocalHistory -> Int
newLocalVarId :: Int }

instance Default LocalHistory where
    def :: LocalHistory
def = E -> Int -> Int -> LocalHistory
LocalHistory E
start Int
0 Int
0

instance Monad m => Functor (DepT m) where
    fmap :: (a -> b) -> DepT m a -> DepT m b
fmap = (a -> b) -> DepT m a -> DepT m b
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM

instance Monad m => Applicative (DepT m) where
    pure :: a -> DepT m a
pure = a -> DepT m a
forall (m :: * -> *) a. Monad m => a -> m a
return
    <*> :: DepT m (a -> b) -> DepT m a -> DepT m b
(<*>) = DepT m (a -> b) -> DepT m a -> DepT m b
forall (m :: * -> *) a b. Monad m => m (a -> b) -> m a -> m b
ap

instance Monad m => Monad (DepT m) where
    return :: a -> DepT m a
return = StateT LocalHistory m a -> DepT m a
forall (m :: * -> *) a. StateT LocalHistory m a -> DepT m a
DepT (StateT LocalHistory m a -> DepT m a)
-> (a -> StateT LocalHistory m a) -> a -> DepT m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> StateT LocalHistory m a
forall (m :: * -> *) a. Monad m => a -> m a
return
    DepT m a
ma >>= :: DepT m a -> (a -> DepT m b) -> DepT m b
>>= a -> DepT m b
mf = StateT LocalHistory m b -> DepT m b
forall (m :: * -> *) a. StateT LocalHistory m a -> DepT m a
DepT (StateT LocalHistory m b -> DepT m b)
-> StateT LocalHistory m b -> DepT m b
forall a b. (a -> b) -> a -> b
$ DepT m a -> StateT LocalHistory m a
forall (m :: * -> *) a. DepT m a -> StateT LocalHistory m a
unDepT DepT m a
ma StateT LocalHistory m a
-> (a -> StateT LocalHistory m b) -> StateT LocalHistory m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= DepT m b -> StateT LocalHistory m b
forall (m :: * -> *) a. DepT m a -> StateT LocalHistory m a
unDepT (DepT m b -> StateT LocalHistory m b)
-> (a -> DepT m b) -> a -> StateT LocalHistory m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> DepT m b
mf

instance MonadTrans DepT where
    lift :: m a -> DepT m a
lift m a
ma = StateT LocalHistory m a -> DepT m a
forall (m :: * -> *) a. StateT LocalHistory m a -> DepT m a
DepT (StateT LocalHistory m a -> DepT m a)
-> StateT LocalHistory m a -> DepT m a
forall a b. (a -> b) -> a -> b
$ m a -> StateT LocalHistory m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m a
ma

runDepT :: (Functor m, Monad m) => DepT m a -> m (a, LocalHistory)
runDepT :: DepT m a -> m (a, LocalHistory)
runDepT DepT m a
a = StateT LocalHistory m a -> LocalHistory -> m (a, LocalHistory)
forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
runStateT (DepT m a -> StateT LocalHistory m a
forall (m :: * -> *) a. DepT m a -> StateT LocalHistory m a
unDepT (DepT m a -> StateT LocalHistory m a)
-> DepT m a -> StateT LocalHistory m a
forall a b. (a -> b) -> a -> b
$ DepT m a
a) LocalHistory
forall a. Default a => a
def

evalDepT :: (Functor m, Monad m) => DepT m a -> m a
evalDepT :: DepT m a -> m a
evalDepT DepT m a
a = StateT LocalHistory m a -> LocalHistory -> m a
forall (m :: * -> *) s a. Monad m => StateT s m a -> s -> m a
evalStateT (DepT m a -> StateT LocalHistory m a
forall (m :: * -> *) a. DepT m a -> StateT LocalHistory m a
unDepT (DepT m a -> StateT LocalHistory m a)
-> DepT m a -> StateT LocalHistory m a
forall a b. (a -> b) -> a -> b
$ DepT m a
a) LocalHistory
forall a. Default a => a
def

execDepT :: (Functor m, Monad m) => DepT m () -> m E
execDepT :: DepT m () -> m E
execDepT DepT m ()
a = (LocalHistory -> E) -> m LocalHistory -> m E
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap LocalHistory -> E
expDependency (m LocalHistory -> m E) -> m LocalHistory -> m E
forall a b. (a -> b) -> a -> b
$ StateT LocalHistory m () -> LocalHistory -> m LocalHistory
forall (m :: * -> *) s a. Monad m => StateT s m a -> s -> m s
execStateT (DepT m () -> StateT LocalHistory m ()
forall (m :: * -> *) a. DepT m a -> StateT LocalHistory m a
unDepT (DepT m () -> StateT LocalHistory m ())
-> DepT m () -> StateT LocalHistory m ()
forall a b. (a -> b) -> a -> b
$ DepT m ()
a) LocalHistory
forall a. Default a => a
def

-- dependency tracking

start :: E
start :: E
start = Exp E -> E
noRate Exp E
forall a. MainExp a
Starts

depends :: E -> E -> E
depends :: E -> E -> E
depends E
a1 E
a2 = Exp E -> E
noRate (Exp E -> E) -> Exp E -> E
forall a b. (a -> b) -> a -> b
$ PrimOr E -> PrimOr E -> Exp E
forall a. a -> a -> MainExp a
Seq (E -> PrimOr E
toPrimOr E
a1) (E -> PrimOr E
toPrimOr E
a2)

depT :: Monad m => E -> DepT m E
depT :: E -> DepT m E
depT E
a = StateT LocalHistory m E -> DepT m E
forall (m :: * -> *) a. StateT LocalHistory m a -> DepT m a
DepT (StateT LocalHistory m E -> DepT m E)
-> StateT LocalHistory m E -> DepT m E
forall a b. (a -> b) -> a -> b
$ do
    LocalHistory
s <- StateT LocalHistory m LocalHistory
forall (m :: * -> *) s. Monad m => StateT s m s
get
    let a1 :: E
a1 = RatedExp E -> E
forall (f :: * -> *). f (Fix f) -> Fix f
Fix (RatedExp E -> E) -> RatedExp E -> E
forall a b. (a -> b) -> a -> b
$ (E -> RatedExp E
forall (f :: * -> *). Fix f -> f (Fix f)
unFix E
a) { ratedExpDepends :: Maybe Int
ratedExpDepends = Int -> Maybe Int
forall a. a -> Maybe a
Just (LocalHistory -> Int
newLineNum LocalHistory
s) }
    LocalHistory -> StateT LocalHistory m ()
forall (m :: * -> *) s. Monad m => s -> StateT s m ()
put (LocalHistory -> StateT LocalHistory m ())
-> LocalHistory -> StateT LocalHistory m ()
forall a b. (a -> b) -> a -> b
$ LocalHistory
s {
        newLineNum :: Int
newLineNum = Int -> Int
forall a. Enum a => a -> a
succ (Int -> Int) -> Int -> Int
forall a b. (a -> b) -> a -> b
$ LocalHistory -> Int
newLineNum LocalHistory
s,
        expDependency :: E
expDependency = E -> E -> E
depends (LocalHistory -> E
expDependency LocalHistory
s) E
a1 }
    E -> StateT LocalHistory m E
forall (m :: * -> *) a. Monad m => a -> m a
return E
a1

depT_ :: (Monad m) => E -> DepT m ()
depT_ :: E -> DepT m ()
depT_ = (E -> ()) -> DepT m E -> DepT m ()
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (() -> E -> ()
forall a b. a -> b -> a
const ()) (DepT m E -> DepT m ()) -> (E -> DepT m E) -> E -> DepT m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. E -> DepT m E
forall (m :: * -> *). Monad m => E -> DepT m E
depT

mdepT :: (Monad m) => MultiOut [E] -> MultiOut (DepT m [E])
mdepT :: MultiOut [E] -> MultiOut (DepT m [E])
mdepT MultiOut [E]
mas = \Int
n -> (E -> DepT m E) -> [E] -> DepT m [E]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM E -> DepT m E
forall (m :: * -> *). Monad m => E -> DepT m E
depT ([E] -> DepT m [E]) -> [E] -> DepT m [E]
forall a b. (a -> b) -> a -> b
$ ( MultiOut [E] -> MultiOut [E]
forall a b. (a -> b) -> a -> b
$ Int
n) MultiOut [E]
mas

stripDepT :: Monad m => DepT m a -> m a
stripDepT :: DepT m a -> m a
stripDepT (DepT StateT LocalHistory m a
a) = StateT LocalHistory m a -> LocalHistory -> m a
forall (m :: * -> *) s a. Monad m => StateT s m a -> s -> m a
evalStateT StateT LocalHistory m a
a LocalHistory
forall a. Default a => a
def

stmtOnlyT :: Monad m => Exp E -> DepT m ()
stmtOnlyT :: Exp E -> DepT m ()
stmtOnlyT Exp E
stmt = E -> DepT m ()
forall (m :: * -> *). Monad m => E -> DepT m ()
depT_ (E -> DepT m ()) -> E -> DepT m ()
forall a b. (a -> b) -> a -> b
$ Exp E -> E
noRate Exp E
stmt

-- local variables

newLocalVars :: Monad m => [Rate] -> m [E] -> DepT m [Var]
newLocalVars :: [Rate] -> m [E] -> DepT m [Var]
newLocalVars [Rate]
rs m [E]
vs = do
    [Var]
vars <- (Rate -> DepT m Var) -> [Rate] -> DepT m [Var]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Rate -> DepT m Var
forall (m :: * -> *). Monad m => Rate -> DepT m Var
newVar [Rate]
rs
    (Var -> E -> DepT m ()) -> [Var] -> [E] -> DepT m ()
forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> m c) -> [a] -> [b] -> m ()
zipWithM_ Var -> E -> DepT m ()
forall (m :: * -> *). Monad m => Var -> E -> DepT m ()
initVar [Var]
vars ([E] -> DepT m ()) -> DepT m [E] -> DepT m ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< m [E] -> DepT m [E]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m [E]
vs
    [Var] -> DepT m [Var]
forall (m :: * -> *) a. Monad m => a -> m a
return [Var]
vars

newLocalVar :: Monad m => Rate -> m E -> DepT m Var
newLocalVar :: Rate -> m E -> DepT m Var
newLocalVar Rate
rate m E
val = do
    Var
var <- Rate -> DepT m Var
forall (m :: * -> *). Monad m => Rate -> DepT m Var
newVar Rate
rate
    Var -> E -> DepT m ()
forall (m :: * -> *). Monad m => Var -> E -> DepT m ()
initVar Var
var (E -> DepT m ()) -> DepT m E -> DepT m ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< m E -> DepT m E
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m E
val
    Var -> DepT m Var
forall (m :: * -> *) a. Monad m => a -> m a
return Var
var

newVar :: Monad m => Rate -> DepT m Var
newVar :: Rate -> DepT m Var
newVar Rate
rate = StateT LocalHistory m Var -> DepT m Var
forall (m :: * -> *) a. StateT LocalHistory m a -> DepT m a
DepT (StateT LocalHistory m Var -> DepT m Var)
-> StateT LocalHistory m Var -> DepT m Var
forall a b. (a -> b) -> a -> b
$ do
    LocalHistory
s <- StateT LocalHistory m LocalHistory
forall (m :: * -> *) s. Monad m => StateT s m s
get
    let v :: Var
v = VarType -> Rate -> Name -> Var
Var VarType
LocalVar Rate
rate (Int -> Name
forall a. Show a => a -> Name
show (Int -> Name) -> Int -> Name
forall a b. (a -> b) -> a -> b
$ LocalHistory -> Int
newLocalVarId LocalHistory
s)
    LocalHistory -> StateT LocalHistory m ()
forall (m :: * -> *) s. Monad m => s -> StateT s m ()
put (LocalHistory -> StateT LocalHistory m ())
-> LocalHistory -> StateT LocalHistory m ()
forall a b. (a -> b) -> a -> b
$ LocalHistory
s { newLocalVarId :: Int
newLocalVarId = Int -> Int
forall a. Enum a => a -> a
succ (Int -> Int) -> Int -> Int
forall a b. (a -> b) -> a -> b
$ LocalHistory -> Int
newLocalVarId LocalHistory
s }
    Var -> StateT LocalHistory m Var
forall (m :: * -> *) a. Monad m => a -> m a
return Var
v

--------------------------------------------------
-- variables

-- generic funs

writeVar :: Monad m => Var -> E -> DepT m ()
writeVar :: Var -> E -> DepT m ()
writeVar Var
v E
x = E -> DepT m ()
forall (m :: * -> *). Monad m => E -> DepT m ()
depT_ (E -> DepT m ()) -> E -> DepT m ()
forall a b. (a -> b) -> a -> b
$ Exp E -> E
noRate (Exp E -> E) -> Exp E -> E
forall a b. (a -> b) -> a -> b
$ Var -> PrimOr E -> Exp E
forall a. Var -> a -> MainExp a
WriteVar Var
v (PrimOr E -> Exp E) -> PrimOr E -> Exp E
forall a b. (a -> b) -> a -> b
$ E -> PrimOr E
toPrimOr E
x

readVar :: Monad m => Var -> DepT m E
readVar :: Var -> DepT m E
readVar Var
v = E -> DepT m E
forall (m :: * -> *). Monad m => E -> DepT m E
depT (E -> DepT m E) -> E -> DepT m E
forall a b. (a -> b) -> a -> b
$ Exp E -> E
noRate (Exp E -> E) -> Exp E -> E
forall a b. (a -> b) -> a -> b
$ Var -> Exp E
forall a. Var -> MainExp a
ReadVar Var
v

readOnlyVar :: Var -> E
readOnlyVar :: Var -> E
readOnlyVar Var
v = Exp E -> E
noRate (Exp E -> E) -> Exp E -> E
forall a b. (a -> b) -> a -> b
$ Var -> Exp E
forall a. Var -> MainExp a
ReadVar Var
v

initVar :: Monad m => Var -> E -> DepT m ()
initVar :: Var -> E -> DepT m ()
initVar Var
v E
x = E -> DepT m ()
forall (m :: * -> *). Monad m => E -> DepT m ()
depT_ (E -> DepT m ()) -> E -> DepT m ()
forall a b. (a -> b) -> a -> b
$ Rate -> E -> E
setRate Rate
Ir (E -> E) -> E -> E
forall a b. (a -> b) -> a -> b
$ Exp E -> E
noRate (Exp E -> E) -> Exp E -> E
forall a b. (a -> b) -> a -> b
$ Var -> PrimOr E -> Exp E
forall a. Var -> a -> MainExp a
InitVar Var
v (PrimOr E -> Exp E) -> PrimOr E -> Exp E
forall a b. (a -> b) -> a -> b
$ E -> PrimOr E
toPrimOr E
x

appendVarBy :: Monad m => (E -> E -> E) -> Var -> E -> DepT m ()
appendVarBy :: (E -> E -> E) -> Var -> E -> DepT m ()
appendVarBy E -> E -> E
op Var
v E
x = Var -> E -> DepT m ()
forall (m :: * -> *). Monad m => Var -> E -> DepT m ()
writeVar Var
v (E -> DepT m ()) -> (E -> E) -> E -> DepT m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. E -> E -> E
op E
x (E -> DepT m ()) -> DepT m E -> DepT m ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Var -> DepT m E
forall (m :: * -> *). Monad m => Var -> DepT m E
readVar Var
v

--------------------------------------------------
-- arrays

-- init

newLocalArrVar :: Monad m => Rate -> m [E] -> DepT m Var
newLocalArrVar :: Rate -> m [E] -> DepT m Var
newLocalArrVar Rate
rate m [E]
val = do
    Var
var <- Rate -> DepT m Var
forall (m :: * -> *). Monad m => Rate -> DepT m Var
newVar Rate
rate
    Var -> [E] -> DepT m ()
forall (m :: * -> *). Monad m => Var -> [E] -> DepT m ()
initArr Var
var ([E] -> DepT m ()) -> DepT m [E] -> DepT m ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< m [E] -> DepT m [E]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m [E]
val
    Var -> DepT m Var
forall (m :: * -> *) a. Monad m => a -> m a
return Var
var

newTmpArrVar :: Monad m => Rate -> DepT m Var
newTmpArrVar :: Rate -> DepT m Var
newTmpArrVar Rate
rate = Rate -> DepT m Var
forall (m :: * -> *). Monad m => Rate -> DepT m Var
newVar Rate
rate

-- ops

readArr :: Monad m => Var -> [E] -> DepT m E
readArr :: Var -> [E] -> DepT m E
readArr Var
v [E]
ixs = E -> DepT m E
forall (m :: * -> *). Monad m => E -> DepT m E
depT (E -> DepT m E) -> E -> DepT m E
forall a b. (a -> b) -> a -> b
$ Exp E -> E
noRate (Exp E -> E) -> Exp E -> E
forall a b. (a -> b) -> a -> b
$ Var -> ArrIndex (PrimOr E) -> Exp E
forall a. Var -> ArrIndex a -> MainExp a
ReadArr Var
v ((E -> PrimOr E) -> [E] -> ArrIndex (PrimOr E)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap E -> PrimOr E
toPrimOr [E]
ixs)

readOnlyArr :: Var -> [E] -> E
readOnlyArr :: Var -> [E] -> E
readOnlyArr Var
v [E]
ixs = Exp E -> E
noRate (Exp E -> E) -> Exp E -> E
forall a b. (a -> b) -> a -> b
$ Var -> ArrIndex (PrimOr E) -> Exp E
forall a. Var -> ArrIndex a -> MainExp a
ReadArr Var
v ((E -> PrimOr E) -> [E] -> ArrIndex (PrimOr E)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap E -> PrimOr E
toPrimOr [E]
ixs)

writeArr :: Monad m => Var -> [E] -> E -> DepT m ()
writeArr :: Var -> [E] -> E -> DepT m ()
writeArr Var
v [E]
ixs E
a = E -> DepT m ()
forall (m :: * -> *). Monad m => E -> DepT m ()
depT_ (E -> DepT m ()) -> E -> DepT m ()
forall a b. (a -> b) -> a -> b
$ Exp E -> E
noRate (Exp E -> E) -> Exp E -> E
forall a b. (a -> b) -> a -> b
$ Var -> ArrIndex (PrimOr E) -> PrimOr E -> Exp E
forall a. Var -> ArrIndex a -> a -> MainExp a
WriteArr Var
v ((E -> PrimOr E) -> [E] -> ArrIndex (PrimOr E)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap E -> PrimOr E
toPrimOr [E]
ixs) (E -> PrimOr E
toPrimOr E
a)

writeInitArr :: Monad m => Var -> [E] -> E -> DepT m ()
writeInitArr :: Var -> [E] -> E -> DepT m ()
writeInitArr Var
v [E]
ixs E
a = E -> DepT m ()
forall (m :: * -> *). Monad m => E -> DepT m ()
depT_ (E -> DepT m ()) -> E -> DepT m ()
forall a b. (a -> b) -> a -> b
$ Exp E -> E
noRate (Exp E -> E) -> Exp E -> E
forall a b. (a -> b) -> a -> b
$ Var -> ArrIndex (PrimOr E) -> PrimOr E -> Exp E
forall a. Var -> ArrIndex a -> a -> MainExp a
WriteInitArr Var
v ((E -> PrimOr E) -> [E] -> ArrIndex (PrimOr E)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap E -> PrimOr E
toPrimOr [E]
ixs) (E -> PrimOr E
toPrimOr E
a)

initArr :: Monad m => Var -> [E] -> DepT m ()
initArr :: Var -> [E] -> DepT m ()
initArr Var
v [E]
xs = E -> DepT m ()
forall (m :: * -> *). Monad m => E -> DepT m ()
depT_ (E -> DepT m ()) -> E -> DepT m ()
forall a b. (a -> b) -> a -> b
$ Exp E -> E
noRate (Exp E -> E) -> Exp E -> E
forall a b. (a -> b) -> a -> b
$ Var -> ArrIndex (PrimOr E) -> Exp E
forall a. Var -> ArrIndex a -> MainExp a
InitArr Var
v (ArrIndex (PrimOr E) -> Exp E) -> ArrIndex (PrimOr E) -> Exp E
forall a b. (a -> b) -> a -> b
$ (E -> PrimOr E) -> [E] -> ArrIndex (PrimOr E)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap E -> PrimOr E
toPrimOr [E]
xs

appendArrBy :: Monad m => (E -> E -> E) -> Var -> [E] -> E -> DepT m ()
appendArrBy :: (E -> E -> E) -> Var -> [E] -> E -> DepT m ()
appendArrBy E -> E -> E
op Var
v [E]
ixs E
x = Var -> [E] -> E -> DepT m ()
forall (m :: * -> *). Monad m => Var -> [E] -> E -> DepT m ()
writeArr Var
v [E]
ixs (E -> DepT m ()) -> (E -> E) -> E -> DepT m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. E -> E -> E
op E
x (E -> DepT m ()) -> DepT m E -> DepT m ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Var -> [E] -> DepT m E
forall (m :: * -> *). Monad m => Var -> [E] -> DepT m E
readArr Var
v [E]
ixs

--------------------------------------------------
-- read global macros arguments

readMacrosDouble :: String -> E
readMacrosDouble :: Name -> E
readMacrosDouble = (Name -> Exp E) -> Rate -> Name -> E
readMacrosBy Name -> Exp E
forall a. Name -> MainExp a
ReadMacrosDouble Rate
Ir

readMacrosInt :: String -> E
readMacrosInt :: Name -> E
readMacrosInt = (Name -> Exp E) -> Rate -> Name -> E
readMacrosBy Name -> Exp E
forall a. Name -> MainExp a
ReadMacrosInt Rate
Ir

readMacrosString :: String -> E
readMacrosString :: Name -> E
readMacrosString = (Name -> Exp E) -> Rate -> Name -> E
readMacrosBy Name -> Exp E
forall a. Name -> MainExp a
ReadMacrosString Rate
Sr

initMacrosDouble :: Monad m => String -> Double -> DepT m ()
initMacrosDouble :: Name -> Double -> DepT m ()
initMacrosDouble = (Name -> Double -> Exp E) -> Name -> Double -> DepT m ()
forall (m :: * -> *) a.
Monad m =>
(Name -> a -> Exp E) -> Name -> a -> DepT m ()
initMacrosBy Name -> Double -> Exp E
forall a. Name -> Double -> MainExp a
InitMacrosDouble

initMacrosString :: Monad m => String -> String -> DepT m ()
initMacrosString :: Name -> Name -> DepT m ()
initMacrosString = (Name -> Name -> Exp E) -> Name -> Name -> DepT m ()
forall (m :: * -> *) a.
Monad m =>
(Name -> a -> Exp E) -> Name -> a -> DepT m ()
initMacrosBy Name -> Name -> Exp E
forall a. Name -> Name -> MainExp a
InitMacrosString

initMacrosInt :: Monad m => String -> Int -> DepT m ()
initMacrosInt :: Name -> Int -> DepT m ()
initMacrosInt = (Name -> Int -> Exp E) -> Name -> Int -> DepT m ()
forall (m :: * -> *) a.
Monad m =>
(Name -> a -> Exp E) -> Name -> a -> DepT m ()
initMacrosBy Name -> Int -> Exp E
forall a. Name -> Int -> MainExp a
InitMacrosInt

readMacrosBy :: (String -> Exp E) -> Rate -> String -> E
readMacrosBy :: (Name -> Exp E) -> Rate -> Name -> E
readMacrosBy Name -> Exp E
readMacro Rate
rate Name
name = Rate -> Exp E -> E
withRate Rate
rate (Exp E -> E) -> Exp E -> E
forall a b. (a -> b) -> a -> b
$ Name -> Exp E
readMacro Name
name

initMacrosBy :: Monad m => (String -> a -> Exp E) -> String -> a -> DepT m ()
initMacrosBy :: (Name -> a -> Exp E) -> Name -> a -> DepT m ()
initMacrosBy Name -> a -> Exp E
maker Name
name a
value = E -> DepT m ()
forall (m :: * -> *). Monad m => E -> DepT m ()
depT_ (E -> DepT m ()) -> E -> DepT m ()
forall a b. (a -> b) -> a -> b
$ Exp E -> E
noRate (Exp E -> E) -> Exp E -> E
forall a b. (a -> b) -> a -> b
$ Name -> a -> Exp E
maker Name
name a
value