module Language.Drasil.CodeExpr.Extract (
    eDep, eDep',
    eNamesRI, eNamesRI'
) where

import Language.Drasil.Space (RealInterval(..))
import Language.Drasil.UID (UID)

import Language.Drasil.CodeExpr.Lang (CodeExpr(..))

import Data.List (nub)

-- | Generic traverse of all expressions that could lead to names.
eNames :: CodeExpr -> [UID]
eNames :: CodeExpr -> [UID]
eNames (AssocA AssocArithOper
_ [CodeExpr]
l)          = forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap CodeExpr -> [UID]
eNames [CodeExpr]
l
eNames (AssocB AssocBoolOper
_ [CodeExpr]
l)          = forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap CodeExpr -> [UID]
eNames [CodeExpr]
l
eNames (C UID
c)                 = [UID
c]
eNames Lit{}                 = []
eNames (FCall UID
f [CodeExpr]
x [(UID, CodeExpr)]
ns)        = UID
f forall a. a -> [a] -> [a]
: forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap CodeExpr -> [UID]
eNames [CodeExpr]
x forall a. [a] -> [a] -> [a]
++ forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst [(UID, CodeExpr)]
ns forall a. [a] -> [a] -> [a]
++ 
                              forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (CodeExpr -> [UID]
eNames forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> b
snd) [(UID, CodeExpr)]
ns
eNames (New UID
c [CodeExpr]
x [(UID, CodeExpr)]
ns)          = UID
c forall a. a -> [a] -> [a]
: forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap CodeExpr -> [UID]
eNames [CodeExpr]
x forall a. [a] -> [a] -> [a]
++ forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst [(UID, CodeExpr)]
ns forall a. [a] -> [a] -> [a]
++ 
                              forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (CodeExpr -> [UID]
eNames forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> b
snd) [(UID, CodeExpr)]
ns
eNames (Message UID
a UID
m [CodeExpr]
x [(UID, CodeExpr)]
ns)    = UID
a forall a. a -> [a] -> [a]
: UID
m forall a. a -> [a] -> [a]
: forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap CodeExpr -> [UID]
eNames [CodeExpr]
x forall a. [a] -> [a] -> [a]
++ forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst [(UID, CodeExpr)]
ns forall a. [a] -> [a] -> [a]
++ 
                              forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (CodeExpr -> [UID]
eNames forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> b
snd) [(UID, CodeExpr)]
ns
eNames (Field UID
o UID
f)           = [UID
o, UID
f]
eNames (Case Completeness
_ [(CodeExpr, CodeExpr)]
ls)           = forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (CodeExpr -> [UID]
eNames forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> a
fst) [(CodeExpr, CodeExpr)]
ls forall a. [a] -> [a] -> [a]
++ forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (CodeExpr -> [UID]
eNames forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> b
snd) [(CodeExpr, CodeExpr)]
ls
eNames (UnaryOp UFunc
_ CodeExpr
u)         = CodeExpr -> [UID]
eNames CodeExpr
u
eNames (UnaryOpB UFuncB
_ CodeExpr
u)        = CodeExpr -> [UID]
eNames CodeExpr
u
eNames (UnaryOpVV UFuncVV
_ CodeExpr
u)       = CodeExpr -> [UID]
eNames CodeExpr
u
eNames (UnaryOpVN UFuncVN
_ CodeExpr
u)       = CodeExpr -> [UID]
eNames CodeExpr
u
eNames (ArithBinaryOp ArithBinOp
_ CodeExpr
a CodeExpr
b) = CodeExpr -> [UID]
eNames CodeExpr
a forall a. [a] -> [a] -> [a]
++ CodeExpr -> [UID]
eNames CodeExpr
b
eNames (BoolBinaryOp BoolBinOp
_ CodeExpr
a CodeExpr
b)  = CodeExpr -> [UID]
eNames CodeExpr
a forall a. [a] -> [a] -> [a]
++ CodeExpr -> [UID]
eNames CodeExpr
b
eNames (EqBinaryOp EqBinOp
_ CodeExpr
a CodeExpr
b)    = CodeExpr -> [UID]
eNames CodeExpr
a forall a. [a] -> [a] -> [a]
++ CodeExpr -> [UID]
eNames CodeExpr
b
eNames (LABinaryOp LABinOp
_ CodeExpr
a CodeExpr
b)    = CodeExpr -> [UID]
eNames CodeExpr
a forall a. [a] -> [a] -> [a]
++ CodeExpr -> [UID]
eNames CodeExpr
b
eNames (OrdBinaryOp OrdBinOp
_ CodeExpr
a CodeExpr
b)   = CodeExpr -> [UID]
eNames CodeExpr
a forall a. [a] -> [a] -> [a]
++ CodeExpr -> [UID]
eNames CodeExpr
b
eNames (VVVBinaryOp VVVBinOp
_ CodeExpr
a CodeExpr
b)   = CodeExpr -> [UID]
eNames CodeExpr
a forall a. [a] -> [a] -> [a]
++ CodeExpr -> [UID]
eNames CodeExpr
b
eNames (VVNBinaryOp VVNBinOp
_ CodeExpr
a CodeExpr
b)   = CodeExpr -> [UID]
eNames CodeExpr
a forall a. [a] -> [a] -> [a]
++ CodeExpr -> [UID]
eNames CodeExpr
b
eNames (NVVBinaryOp NVVBinOp
_ CodeExpr
a CodeExpr
b)   = CodeExpr -> [UID]
eNames CodeExpr
a forall a. [a] -> [a] -> [a]
++ CodeExpr -> [UID]
eNames CodeExpr
b
eNames (Operator AssocArithOper
_ DiscreteDomainDesc CodeExpr CodeExpr
_ CodeExpr
e)      = CodeExpr -> [UID]
eNames CodeExpr
e
eNames (Matrix [[CodeExpr]]
a)            = forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap CodeExpr -> [UID]
eNames) [[CodeExpr]]
a
eNames (RealI UID
c RealInterval CodeExpr CodeExpr
b)           = UID
c forall a. a -> [a] -> [a]
: RealInterval CodeExpr CodeExpr -> [UID]
eNamesRI RealInterval CodeExpr CodeExpr
b

-- | Generic traversal of everything that could come from an interval to names (similar to 'eNames').
eNamesRI :: RealInterval CodeExpr CodeExpr -> [UID]
eNamesRI :: RealInterval CodeExpr CodeExpr -> [UID]
eNamesRI (Bounded (Inclusive
_, CodeExpr
il) (Inclusive
_, CodeExpr
iu)) = CodeExpr -> [UID]
eNames CodeExpr
il forall a. [a] -> [a] -> [a]
++ CodeExpr -> [UID]
eNames CodeExpr
iu
eNamesRI (UpTo (Inclusive
_, CodeExpr
iu))           = CodeExpr -> [UID]
eNames CodeExpr
iu
eNamesRI (UpFrom (Inclusive
_, CodeExpr
il))         = CodeExpr -> [UID]
eNames CodeExpr
il

-- | Generic traverse of all positions that could lead to 'eNames' without
-- functions.  FIXME : this should really be done via post-facto filtering, but
-- right now the information needed to do this is not available!
eNames' :: CodeExpr -> [UID]
eNames' :: CodeExpr -> [UID]
eNames' (AssocA AssocArithOper
_ [CodeExpr]
l)          = forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap CodeExpr -> [UID]
eNames' [CodeExpr]
l
eNames' (AssocB AssocBoolOper
_ [CodeExpr]
l)          = forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap CodeExpr -> [UID]
eNames' [CodeExpr]
l
eNames' (C UID
c)                 = [UID
c]
eNames' Lit{}                 = []
eNames' (FCall UID
_ [CodeExpr]
x [(UID, CodeExpr)]
ns)        = forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap CodeExpr -> [UID]
eNames' [CodeExpr]
x forall a. [a] -> [a] -> [a]
++ forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst [(UID, CodeExpr)]
ns forall a. [a] -> [a] -> [a]
++ 
                               forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (CodeExpr -> [UID]
eNames forall b c a. (b -> c) -> (a -> b) -> a -> c
.forall a b. (a, b) -> b
snd) [(UID, CodeExpr)]
ns
eNames' (New UID
_ [CodeExpr]
x [(UID, CodeExpr)]
ns)          = forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap CodeExpr -> [UID]
eNames' [CodeExpr]
x forall a. [a] -> [a] -> [a]
++ forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst [(UID, CodeExpr)]
ns forall a. [a] -> [a] -> [a]
++ 
                               forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (CodeExpr -> [UID]
eNames forall b c a. (b -> c) -> (a -> b) -> a -> c
.forall a b. (a, b) -> b
snd) [(UID, CodeExpr)]
ns
eNames' (Message UID
a UID
_ [CodeExpr]
x [(UID, CodeExpr)]
ns)    = UID
a forall a. a -> [a] -> [a]
: forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap CodeExpr -> [UID]
eNames' [CodeExpr]
x forall a. [a] -> [a] -> [a]
++ forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst [(UID, CodeExpr)]
ns forall a. [a] -> [a] -> [a]
++ 
                               forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (CodeExpr -> [UID]
eNames forall b c a. (b -> c) -> (a -> b) -> a -> c
.forall a b. (a, b) -> b
snd) [(UID, CodeExpr)]
ns
eNames' (Field UID
o UID
f)           = [UID
o, UID
f]
eNames' (Case Completeness
_ [(CodeExpr, CodeExpr)]
ls)           = forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (CodeExpr -> [UID]
eNames' forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> a
fst) [(CodeExpr, CodeExpr)]
ls forall a. [a] -> [a] -> [a]
++ 
                               forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (CodeExpr -> [UID]
eNames' forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> b
snd) [(CodeExpr, CodeExpr)]
ls
eNames' (UnaryOp UFunc
_ CodeExpr
u)         = CodeExpr -> [UID]
eNames' CodeExpr
u
eNames' (UnaryOpB UFuncB
_ CodeExpr
u)        = CodeExpr -> [UID]
eNames' CodeExpr
u
eNames' (UnaryOpVV UFuncVV
_ CodeExpr
u)       = CodeExpr -> [UID]
eNames' CodeExpr
u
eNames' (UnaryOpVN UFuncVN
_ CodeExpr
u)       = CodeExpr -> [UID]
eNames' CodeExpr
u
eNames' (ArithBinaryOp ArithBinOp
_ CodeExpr
a CodeExpr
b) = CodeExpr -> [UID]
eNames' CodeExpr
a forall a. [a] -> [a] -> [a]
++ CodeExpr -> [UID]
eNames' CodeExpr
b
eNames' (BoolBinaryOp BoolBinOp
_ CodeExpr
a CodeExpr
b)  = CodeExpr -> [UID]
eNames' CodeExpr
a forall a. [a] -> [a] -> [a]
++ CodeExpr -> [UID]
eNames' CodeExpr
b
eNames' (EqBinaryOp EqBinOp
_ CodeExpr
a CodeExpr
b)    = CodeExpr -> [UID]
eNames' CodeExpr
a forall a. [a] -> [a] -> [a]
++ CodeExpr -> [UID]
eNames' CodeExpr
b
eNames' (LABinaryOp LABinOp
_ CodeExpr
a CodeExpr
b)    = CodeExpr -> [UID]
eNames' CodeExpr
a forall a. [a] -> [a] -> [a]
++ CodeExpr -> [UID]
eNames' CodeExpr
b
eNames' (OrdBinaryOp OrdBinOp
_ CodeExpr
a CodeExpr
b)   = CodeExpr -> [UID]
eNames' CodeExpr
a forall a. [a] -> [a] -> [a]
++ CodeExpr -> [UID]
eNames' CodeExpr
b
eNames' (VVVBinaryOp VVVBinOp
_ CodeExpr
a CodeExpr
b)   = CodeExpr -> [UID]
eNames' CodeExpr
a forall a. [a] -> [a] -> [a]
++ CodeExpr -> [UID]
eNames' CodeExpr
b
eNames' (VVNBinaryOp VVNBinOp
_ CodeExpr
a CodeExpr
b)   = CodeExpr -> [UID]
eNames' CodeExpr
a forall a. [a] -> [a] -> [a]
++ CodeExpr -> [UID]
eNames' CodeExpr
b
eNames' (NVVBinaryOp NVVBinOp
_ CodeExpr
a CodeExpr
b)   = CodeExpr -> [UID]
eNames' CodeExpr
a forall a. [a] -> [a] -> [a]
++ CodeExpr -> [UID]
eNames' CodeExpr
b
eNames' (Operator AssocArithOper
_ DiscreteDomainDesc CodeExpr CodeExpr
_ CodeExpr
e)      = CodeExpr -> [UID]
eNames' CodeExpr
e
eNames' (Matrix [[CodeExpr]]
a)            = forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap CodeExpr -> [UID]
eNames') [[CodeExpr]]
a
eNames' (RealI UID
c RealInterval CodeExpr CodeExpr
b)           = UID
c forall a. a -> [a] -> [a]
: RealInterval CodeExpr CodeExpr -> [UID]
eNamesRI' RealInterval CodeExpr CodeExpr
b

-- | Generic traversal of everything that could come from an interval to names without functions (similar to 'eNames'').
eNamesRI' :: RealInterval CodeExpr CodeExpr -> [UID]
eNamesRI' :: RealInterval CodeExpr CodeExpr -> [UID]
eNamesRI' (Bounded (Inclusive, CodeExpr)
il (Inclusive, CodeExpr)
iu) = CodeExpr -> [UID]
eNames' (forall a b. (a, b) -> b
snd (Inclusive, CodeExpr)
il) forall a. [a] -> [a] -> [a]
++ CodeExpr -> [UID]
eNames' (forall a b. (a, b) -> b
snd (Inclusive, CodeExpr)
iu)
eNamesRI' (UpTo (Inclusive, CodeExpr)
iu)       = CodeExpr -> [UID]
eNames' (forall a b. (a, b) -> b
snd (Inclusive, CodeExpr)
iu)
eNamesRI' (UpFrom (Inclusive, CodeExpr)
il)     = CodeExpr -> [UID]
eNames' (forall a b. (a, b) -> b
snd (Inclusive, CodeExpr)
il)

---------------------------------------------------------------------------
-- And now implement the exported traversals all in terms of the above

-- | Get dependencies from an equation.
eDep :: CodeExpr -> [UID]
eDep :: CodeExpr -> [UID]
eDep = forall a. Eq a => [a] -> [a]
nub forall b c a. (b -> c) -> (a -> b) -> a -> c
. CodeExpr -> [UID]
eNames

-- | Get dependencies from an equation, without functions.
eDep' :: CodeExpr -> [UID]
eDep' :: CodeExpr -> [UID]
eDep' = forall a. Eq a => [a] -> [a]
nub forall b c a. (b -> c) -> (a -> b) -> a -> c
. CodeExpr -> [UID]
eNames'