{-# LANGUAGE GADTs #-}

-- | Defines functions for printing expressions.
module Language.Drasil.Printing.Import.Expr (expr) where

import Language.Drasil hiding (neg, sec, symbol, isIn)
import Language.Drasil.Display (Symbol(..))
import Language.Drasil.Expr.Development (ArithBinOp(..), AssocArithOper(..),
  AssocBoolOper(..), BoolBinOp(..), EqBinOp(..), Expr(..),
  LABinOp(..), OrdBinOp(..), UFunc(..), UFuncB(..), UFuncVN(..), UFuncVV(..),
  VVNBinOp(..), VVVBinOp(..), eprec, precA, precB)
import Language.Drasil.Literal.Development (Literal(..))

import qualified Language.Drasil.Printing.AST as P
import Language.Drasil.Printing.PrintingInformation (PrintingInformation, ckdb, stg)

import Control.Lens ((^.))
import Data.List (intersperse)

import Language.Drasil.Printing.Import.Literal (literal)
import Language.Drasil.Printing.Import.Symbol (symbol)
import Language.Drasil.Printing.Import.Helpers (lookupC, parens)


-- | Helper that creates an expression row given printing information, an operator, and an expression.
mkCall :: PrintingInformation -> P.Ops -> Expr -> P.Expr
mkCall :: PrintingInformation -> Ops -> Expr -> Expr
mkCall s :: PrintingInformation
s o :: Ops
o e :: Expr
e = [Expr] -> Expr
P.Row [Ops -> Expr
P.MO Ops
o, Expr -> Expr
parens (Expr -> Expr) -> Expr -> Expr
forall a b. (a -> b) -> a -> b
$ Expr -> PrintingInformation -> Expr
expr Expr
e PrintingInformation
s]

-- | Helper that creates a binary expression row given printing information, an operator, and two expressions.
mkBOp :: PrintingInformation -> P.Ops -> Expr -> Expr -> P.Expr
mkBOp :: PrintingInformation -> Ops -> Expr -> Expr -> Expr
mkBOp sm :: PrintingInformation
sm o :: Ops
o a :: Expr
a b :: Expr
b = [Expr] -> Expr
P.Row [Expr -> PrintingInformation -> Expr
expr Expr
a PrintingInformation
sm, Ops -> Expr
P.MO Ops
o, Expr -> PrintingInformation -> Expr
expr Expr
b PrintingInformation
sm]

-- | Helper that adds parenthesis to an expression where appropriate.
expr' :: PrintingInformation -> Int -> Expr -> P.Expr
expr' :: PrintingInformation -> Int -> Expr -> Expr
expr' s :: PrintingInformation
s p :: Int
p e :: Expr
e = Expr -> Expr
fence (Expr -> Expr) -> Expr -> Expr
forall a b. (a -> b) -> a -> b
$ Expr -> PrintingInformation -> Expr
expr Expr
e PrintingInformation
s
  where fence :: Expr -> Expr
fence = if Expr -> Int
eprec Expr
e Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
p then Expr -> Expr
parens else Expr -> Expr
forall a. a -> a
id

-- | Helper for properly rendering negation of expressions.
neg' :: Expr -> Bool
neg' :: Expr -> Bool
neg' (Lit (Dbl _))          = Bool
True
neg' (Lit (Int _))          = Bool
True
neg' (Lit (ExactDbl _))     = Bool
True
neg' Operator{}             = Bool
True
neg' (AssocA MulI _)        = Bool
True
neg' (AssocA MulRe _)       = Bool
True
neg' (LABinaryOp Index _ _) = Bool
True
neg' (UnaryOp _ _)          = Bool
True
neg' (UnaryOpB _ _)         = Bool
True
neg' (UnaryOpVV _ _)        = Bool
True
neg' (C _)                  = Bool
True
neg' _                      = Bool
False

-- | Render negated expressions.
neg :: PrintingInformation -> Expr -> P.Expr
neg :: PrintingInformation -> Expr -> Expr
neg sm :: PrintingInformation
sm a :: Expr
a = [Expr] -> Expr
P.Row [Ops -> Expr
P.MO Ops
P.Neg, (if Expr -> Bool
neg' Expr
a then Expr -> Expr
forall a. a -> a
id else Expr -> Expr
parens) (Expr -> Expr) -> Expr -> Expr
forall a b. (a -> b) -> a -> b
$ Expr -> PrintingInformation -> Expr
expr Expr
a PrintingInformation
sm]

-- | For printing indexes.
indx :: PrintingInformation -> Expr -> Expr -> P.Expr
indx :: PrintingInformation -> Expr -> Expr -> Expr
indx sm :: PrintingInformation
sm (C c :: UID
c) i :: Expr
i = Symbol -> Expr
f Symbol
s
  where
    i' :: Expr
i' = Expr -> PrintingInformation -> Expr
expr Expr
i PrintingInformation
sm
    s :: Symbol
s = Stage -> ChunkDB -> UID -> Symbol
lookupC (PrintingInformation
sm PrintingInformation
-> Getting Stage PrintingInformation Stage -> Stage
forall s a. s -> Getting a s a -> a
^. Getting Stage PrintingInformation Stage
Lens' PrintingInformation Stage
stg) (PrintingInformation
sm PrintingInformation
-> Getting ChunkDB PrintingInformation ChunkDB -> ChunkDB
forall s a. s -> Getting a s a -> a
^. Getting ChunkDB PrintingInformation ChunkDB
Lens' PrintingInformation ChunkDB
ckdb) UID
c
    f :: Symbol -> Expr
f (Corners [] [] [] [b :: Symbol
b] e :: Symbol
e) =
      let e' :: Expr
e' = Symbol -> Expr
symbol Symbol
e
          b' :: Expr
b' = Symbol -> Expr
symbol Symbol
b in
      [Expr] -> Expr
P.Row [[Expr] -> Expr
P.Row [Expr
e', Expr -> Expr
P.Sub ([Expr] -> Expr
P.Row [Expr
b', Ops -> Expr
P.MO Ops
P.Comma, Expr
i'])]] -- FIXME, extra Row
    f a :: Symbol
a@(Variable _) = [Expr] -> Expr
P.Row [Symbol -> Expr
symbol Symbol
a, Expr -> Expr
P.Sub Expr
i']
    f a :: Symbol
a@(Label _)    = [Expr] -> Expr
P.Row [Symbol -> Expr
symbol Symbol
a, Expr -> Expr
P.Sub Expr
i']
--    f a@(Greek _)  = P.Row [symbol a, P.Sub i']
    f   e :: Symbol
e          = let e' :: Expr
e' = Symbol -> Expr
symbol Symbol
e in [Expr] -> Expr
P.Row [[Expr] -> Expr
P.Row [Expr
e'], Expr -> Expr
P.Sub Expr
i']
indx sm :: PrintingInformation
sm a :: Expr
a i :: Expr
i = [Expr] -> Expr
P.Row [[Expr] -> Expr
P.Row [Expr -> PrintingInformation -> Expr
expr Expr
a PrintingInformation
sm], Expr -> Expr
P.Sub (Expr -> Expr) -> Expr -> Expr
forall a b. (a -> b) -> a -> b
$ Expr -> PrintingInformation -> Expr
expr Expr
i PrintingInformation
sm]

-- | For printing expressions that call something.
call :: PrintingInformation -> UID -> [Expr] -> [(UID,Expr)] -> P.Expr
call :: PrintingInformation -> UID -> [Expr] -> [(UID, Expr)] -> Expr
call sm :: PrintingInformation
sm f :: UID
f ps :: [Expr]
ps ns :: [(UID, Expr)]
ns = [Expr] -> Expr
P.Row [Symbol -> Expr
symbol (Symbol -> Expr) -> Symbol -> Expr
forall a b. (a -> b) -> a -> b
$ Stage -> ChunkDB -> UID -> Symbol
lookupC (PrintingInformation
sm PrintingInformation
-> Getting Stage PrintingInformation Stage -> Stage
forall s a. s -> Getting a s a -> a
^. Getting Stage PrintingInformation Stage
Lens' PrintingInformation Stage
stg) (PrintingInformation
sm PrintingInformation
-> Getting ChunkDB PrintingInformation ChunkDB -> ChunkDB
forall s a. s -> Getting a s a -> a
^. Getting ChunkDB PrintingInformation ChunkDB
Lens' PrintingInformation ChunkDB
ckdb) UID
f,
  Expr -> Expr
parens (Expr -> Expr) -> Expr -> Expr
forall a b. (a -> b) -> a -> b
$ [Expr] -> Expr
P.Row ([Expr] -> Expr) -> [Expr] -> Expr
forall a b. (a -> b) -> a -> b
$ Expr -> [Expr] -> [Expr]
forall a. a -> [a] -> [a]
intersperse (Ops -> Expr
P.MO Ops
P.Comma) ([Expr] -> [Expr]) -> [Expr] -> [Expr]
forall a b. (a -> b) -> a -> b
$ (Expr -> Expr) -> [Expr] -> [Expr]
forall a b. (a -> b) -> [a] -> [b]
map (Expr -> PrintingInformation -> Expr
`expr` PrintingInformation
sm) [Expr]
ps [Expr] -> [Expr] -> [Expr]
forall a. [a] -> [a] -> [a]
++
  (UID -> Expr -> Expr) -> [UID] -> [Expr] -> [Expr]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (\n :: UID
n a :: Expr
a -> [Expr] -> Expr
P.Row [Symbol -> Expr
symbol (Symbol -> Expr) -> Symbol -> Expr
forall a b. (a -> b) -> a -> b
$ Stage -> ChunkDB -> UID -> Symbol
lookupC (PrintingInformation
sm PrintingInformation
-> Getting Stage PrintingInformation Stage -> Stage
forall s a. s -> Getting a s a -> a
^. Getting Stage PrintingInformation Stage
Lens' PrintingInformation Stage
stg) (PrintingInformation
sm PrintingInformation
-> Getting ChunkDB PrintingInformation ChunkDB -> ChunkDB
forall s a. s -> Getting a s a -> a
^. Getting ChunkDB PrintingInformation ChunkDB
Lens' PrintingInformation ChunkDB
ckdb) UID
n,
  Ops -> Expr
P.MO Ops
P.Eq, Expr -> PrintingInformation -> Expr
expr Expr
a PrintingInformation
sm]) (((UID, Expr) -> UID) -> [(UID, Expr)] -> [UID]
forall a b. (a -> b) -> [a] -> [b]
map (UID, Expr) -> UID
forall a b. (a, b) -> a
fst [(UID, Expr)]
ns) (((UID, Expr) -> Expr) -> [(UID, Expr)] -> [Expr]
forall a b. (a -> b) -> [a] -> [b]
map (UID, Expr) -> Expr
forall a b. (a, b) -> b
snd [(UID, Expr)]
ns)]

-- | Helper function for addition 'EOperator's.
eopAdds :: PrintingInformation -> DomainDesc t Expr Expr -> Expr -> P.Expr
eopAdds :: PrintingInformation -> DomainDesc t Expr Expr -> Expr -> Expr
eopAdds sm :: PrintingInformation
sm (BoundedDD v :: Symbol
v Continuous l :: Expr
l h :: Expr
h) e :: Expr
e =
  [Expr] -> Expr
P.Row [Ops -> Expr
P.MO Ops
P.Inte, Expr -> Expr
P.Sub (Expr -> PrintingInformation -> Expr
expr Expr
l PrintingInformation
sm), Expr -> Expr
P.Sup (Expr -> PrintingInformation -> Expr
expr Expr
h PrintingInformation
sm),
         [Expr] -> Expr
P.Row [Expr -> PrintingInformation -> Expr
expr Expr
e PrintingInformation
sm], Spacing -> Expr
P.Spc Spacing
P.Thin, String -> Expr
P.Ident "d", Symbol -> Expr
symbol Symbol
v]
eopAdds sm :: PrintingInformation
sm (AllDD v :: Symbol
v Continuous) e :: Expr
e =
  [Expr] -> Expr
P.Row [Ops -> Expr
P.MO Ops
P.Inte, Expr -> Expr
P.Sub (Symbol -> Expr
symbol Symbol
v), [Expr] -> Expr
P.Row [Expr -> PrintingInformation -> Expr
expr Expr
e PrintingInformation
sm], Spacing -> Expr
P.Spc Spacing
P.Thin,
         String -> Expr
P.Ident "d", Symbol -> Expr
symbol Symbol
v]
eopAdds sm :: PrintingInformation
sm (BoundedDD v :: Symbol
v Discrete l :: Expr
l h :: Expr
h) e :: Expr
e =
  [Expr] -> Expr
P.Row [Ops -> Expr
P.MO Ops
P.Summ, Expr -> Expr
P.Sub ([Expr] -> Expr
P.Row [Symbol -> Expr
symbol Symbol
v, Ops -> Expr
P.MO Ops
P.Eq, Expr -> PrintingInformation -> Expr
expr Expr
l PrintingInformation
sm]), Expr -> Expr
P.Sup (Expr -> PrintingInformation -> Expr
expr Expr
h PrintingInformation
sm),
         [Expr] -> Expr
P.Row [Expr -> PrintingInformation -> Expr
expr Expr
e PrintingInformation
sm]]
eopAdds sm :: PrintingInformation
sm (AllDD _ Discrete) e :: Expr
e = [Expr] -> Expr
P.Row [Ops -> Expr
P.MO Ops
P.Summ, [Expr] -> Expr
P.Row [Expr -> PrintingInformation -> Expr
expr Expr
e PrintingInformation
sm]]

-- | Helper function for multiplicative 'EOperator's.
eopMuls :: PrintingInformation -> DomainDesc t Expr Expr -> Expr -> P.Expr
eopMuls :: PrintingInformation -> DomainDesc t Expr Expr -> Expr -> Expr
eopMuls sm :: PrintingInformation
sm (BoundedDD v :: Symbol
v Discrete l :: Expr
l h :: Expr
h) e :: Expr
e =
  [Expr] -> Expr
P.Row [Ops -> Expr
P.MO Ops
P.Prod, Expr -> Expr
P.Sub ([Expr] -> Expr
P.Row [Symbol -> Expr
symbol Symbol
v, Ops -> Expr
P.MO Ops
P.Eq, Expr -> PrintingInformation -> Expr
expr Expr
l PrintingInformation
sm]), Expr -> Expr
P.Sup (Expr -> PrintingInformation -> Expr
expr Expr
h PrintingInformation
sm),
         [Expr] -> Expr
P.Row [Expr -> PrintingInformation -> Expr
expr Expr
e PrintingInformation
sm]]
eopMuls sm :: PrintingInformation
sm (AllDD _ Discrete) e :: Expr
e = [Expr] -> Expr
P.Row [Ops -> Expr
P.MO Ops
P.Prod, [Expr] -> Expr
P.Row [Expr -> PrintingInformation -> Expr
expr Expr
e PrintingInformation
sm]]
eopMuls _ (AllDD _ Continuous) _ = String -> Expr
forall a. HasCallStack => String -> a
error "Printing/Import.hs Product-Integral not implemented."
eopMuls _ (BoundedDD _ Continuous _ _) _ = String -> Expr
forall a. HasCallStack => String -> a
error "Printing/Import.hs Product-Integral not implemented."


-- | Helper function for translating 'EOperator's.
eop :: PrintingInformation -> AssocArithOper -> DomainDesc t Expr Expr -> Expr -> P.Expr
eop :: PrintingInformation
-> AssocArithOper -> DomainDesc t Expr Expr -> Expr -> Expr
eop sm :: PrintingInformation
sm AddI = PrintingInformation -> DomainDesc t Expr Expr -> Expr -> Expr
forall (t :: RTopology).
PrintingInformation -> DomainDesc t Expr Expr -> Expr -> Expr
eopAdds PrintingInformation
sm
eop sm :: PrintingInformation
sm AddRe = PrintingInformation -> DomainDesc t Expr Expr -> Expr -> Expr
forall (t :: RTopology).
PrintingInformation -> DomainDesc t Expr Expr -> Expr -> Expr
eopAdds PrintingInformation
sm
eop sm :: PrintingInformation
sm MulI = PrintingInformation -> DomainDesc t Expr Expr -> Expr -> Expr
forall (t :: RTopology).
PrintingInformation -> DomainDesc t Expr Expr -> Expr -> Expr
eopMuls PrintingInformation
sm
eop sm :: PrintingInformation
sm MulRe = PrintingInformation -> DomainDesc t Expr Expr -> Expr -> Expr
forall (t :: RTopology).
PrintingInformation -> DomainDesc t Expr Expr -> Expr -> Expr
eopMuls PrintingInformation
sm


-- | Translate Exprs to printable layout AST.
expr :: Expr -> PrintingInformation -> P.Expr
expr :: Expr -> PrintingInformation -> Expr
expr (Lit l :: Literal
l)                  sm :: PrintingInformation
sm = Literal -> PrintingInformation -> Expr
literal Literal
l PrintingInformation
sm
expr (AssocB And l :: [Expr]
l)           sm :: PrintingInformation
sm = Ops -> Int -> [Expr] -> PrintingInformation -> Expr
assocExpr Ops
P.And (AssocBoolOper -> Int
precB AssocBoolOper
And) [Expr]
l PrintingInformation
sm
expr (AssocB Or l :: [Expr]
l)            sm :: PrintingInformation
sm = Ops -> Int -> [Expr] -> PrintingInformation -> Expr
assocExpr Ops
P.Or (AssocBoolOper -> Int
precB AssocBoolOper
Or) [Expr]
l PrintingInformation
sm
expr (AssocA AddI l :: [Expr]
l)          sm :: PrintingInformation
sm = [Expr] -> Expr
P.Row ([Expr] -> Expr) -> [Expr] -> Expr
forall a b. (a -> b) -> a -> b
$ [Expr] -> AssocArithOper -> PrintingInformation -> [Expr]
addExpr [Expr]
l AssocArithOper
AddI PrintingInformation
sm
expr (AssocA AddRe l :: [Expr]
l)         sm :: PrintingInformation
sm = [Expr] -> Expr
P.Row ([Expr] -> Expr) -> [Expr] -> Expr
forall a b. (a -> b) -> a -> b
$ [Expr] -> AssocArithOper -> PrintingInformation -> [Expr]
addExpr [Expr]
l AssocArithOper
AddRe PrintingInformation
sm
expr (AssocA MulI l :: [Expr]
l)          sm :: PrintingInformation
sm = [Expr] -> Expr
P.Row ([Expr] -> Expr) -> [Expr] -> Expr
forall a b. (a -> b) -> a -> b
$ [Expr] -> AssocArithOper -> PrintingInformation -> [Expr]
mulExpr [Expr]
l AssocArithOper
MulI PrintingInformation
sm
expr (AssocA MulRe l :: [Expr]
l)         sm :: PrintingInformation
sm = [Expr] -> Expr
P.Row ([Expr] -> Expr) -> [Expr] -> Expr
forall a b. (a -> b) -> a -> b
$ [Expr] -> AssocArithOper -> PrintingInformation -> [Expr]
mulExpr [Expr]
l AssocArithOper
MulRe PrintingInformation
sm
expr (C c :: UID
c)                    sm :: PrintingInformation
sm = Symbol -> Expr
symbol (Symbol -> Expr) -> Symbol -> Expr
forall a b. (a -> b) -> a -> b
$ Stage -> ChunkDB -> UID -> Symbol
lookupC (PrintingInformation
sm PrintingInformation
-> Getting Stage PrintingInformation Stage -> Stage
forall s a. s -> Getting a s a -> a
^. Getting Stage PrintingInformation Stage
Lens' PrintingInformation Stage
stg) (PrintingInformation
sm PrintingInformation
-> Getting ChunkDB PrintingInformation ChunkDB -> ChunkDB
forall s a. s -> Getting a s a -> a
^. Getting ChunkDB PrintingInformation ChunkDB
Lens' PrintingInformation ChunkDB
ckdb) UID
c
expr (FCall f :: UID
f [x :: Expr
x] [])         sm :: PrintingInformation
sm =
  [Expr] -> Expr
P.Row [Symbol -> Expr
symbol (Symbol -> Expr) -> Symbol -> Expr
forall a b. (a -> b) -> a -> b
$ Stage -> ChunkDB -> UID -> Symbol
lookupC (PrintingInformation
sm PrintingInformation
-> Getting Stage PrintingInformation Stage -> Stage
forall s a. s -> Getting a s a -> a
^. Getting Stage PrintingInformation Stage
Lens' PrintingInformation Stage
stg) (PrintingInformation
sm PrintingInformation
-> Getting ChunkDB PrintingInformation ChunkDB -> ChunkDB
forall s a. s -> Getting a s a -> a
^. Getting ChunkDB PrintingInformation ChunkDB
Lens' PrintingInformation ChunkDB
ckdb) UID
f, Expr -> Expr
parens (Expr -> Expr) -> Expr -> Expr
forall a b. (a -> b) -> a -> b
$ Expr -> PrintingInformation -> Expr
expr Expr
x PrintingInformation
sm]
expr (FCall f :: UID
f l :: [Expr]
l ns :: [(UID, Expr)]
ns)           sm :: PrintingInformation
sm = PrintingInformation -> UID -> [Expr] -> [(UID, Expr)] -> Expr
call PrintingInformation
sm UID
f [Expr]
l [(UID, Expr)]
ns
expr (Case _ ps :: [(Expr, Expr)]
ps)              sm :: PrintingInformation
sm =
  if [(Expr, Expr)] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [(Expr, Expr)]
ps Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< 2
    then String -> Expr
forall a. HasCallStack => String -> a
error "Attempting to use multi-case expr incorrectly"
    else [(Expr, Expr)] -> Expr
P.Case ([Expr] -> [Expr] -> [(Expr, Expr)]
forall a b. [a] -> [b] -> [(a, b)]
zip (((Expr, Expr) -> Expr) -> [(Expr, Expr)] -> [Expr]
forall a b. (a -> b) -> [a] -> [b]
map ((Expr -> PrintingInformation -> Expr)
-> PrintingInformation -> Expr -> Expr
forall a b c. (a -> b -> c) -> b -> a -> c
flip Expr -> PrintingInformation -> Expr
expr PrintingInformation
sm (Expr -> Expr) -> ((Expr, Expr) -> Expr) -> (Expr, Expr) -> Expr
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Expr, Expr) -> Expr
forall a b. (a, b) -> a
fst) [(Expr, Expr)]
ps) (((Expr, Expr) -> Expr) -> [(Expr, Expr)] -> [Expr]
forall a b. (a -> b) -> [a] -> [b]
map ((Expr -> PrintingInformation -> Expr)
-> PrintingInformation -> Expr -> Expr
forall a b c. (a -> b -> c) -> b -> a -> c
flip Expr -> PrintingInformation -> Expr
expr PrintingInformation
sm (Expr -> Expr) -> ((Expr, Expr) -> Expr) -> (Expr, Expr) -> Expr
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Expr, Expr) -> Expr
forall a b. (a, b) -> b
snd) [(Expr, Expr)]
ps))
expr (Matrix a :: [[Expr]]
a)               sm :: PrintingInformation
sm = [[Expr]] -> Expr
P.Mtx ([[Expr]] -> Expr) -> [[Expr]] -> Expr
forall a b. (a -> b) -> a -> b
$ ([Expr] -> [Expr]) -> [[Expr]] -> [[Expr]]
forall a b. (a -> b) -> [a] -> [b]
map ((Expr -> Expr) -> [Expr] -> [Expr]
forall a b. (a -> b) -> [a] -> [b]
map (Expr -> PrintingInformation -> Expr
`expr` PrintingInformation
sm)) [[Expr]]
a
expr (UnaryOp Log u :: Expr
u)          sm :: PrintingInformation
sm = PrintingInformation -> Ops -> Expr -> Expr
mkCall PrintingInformation
sm Ops
P.Log Expr
u
expr (UnaryOp Ln u :: Expr
u)           sm :: PrintingInformation
sm = PrintingInformation -> Ops -> Expr -> Expr
mkCall PrintingInformation
sm Ops
P.Ln Expr
u
expr (UnaryOp Sin u :: Expr
u)          sm :: PrintingInformation
sm = PrintingInformation -> Ops -> Expr -> Expr
mkCall PrintingInformation
sm Ops
P.Sin Expr
u
expr (UnaryOp Cos u :: Expr
u)          sm :: PrintingInformation
sm = PrintingInformation -> Ops -> Expr -> Expr
mkCall PrintingInformation
sm Ops
P.Cos Expr
u
expr (UnaryOp Tan u :: Expr
u)          sm :: PrintingInformation
sm = PrintingInformation -> Ops -> Expr -> Expr
mkCall PrintingInformation
sm Ops
P.Tan Expr
u
expr (UnaryOp Sec u :: Expr
u)          sm :: PrintingInformation
sm = PrintingInformation -> Ops -> Expr -> Expr
mkCall PrintingInformation
sm Ops
P.Sec Expr
u
expr (UnaryOp Csc u :: Expr
u)          sm :: PrintingInformation
sm = PrintingInformation -> Ops -> Expr -> Expr
mkCall PrintingInformation
sm Ops
P.Csc Expr
u
expr (UnaryOp Cot u :: Expr
u)          sm :: PrintingInformation
sm = PrintingInformation -> Ops -> Expr -> Expr
mkCall PrintingInformation
sm Ops
P.Cot Expr
u
expr (UnaryOp Arcsin u :: Expr
u)       sm :: PrintingInformation
sm = PrintingInformation -> Ops -> Expr -> Expr
mkCall PrintingInformation
sm Ops
P.Arcsin Expr
u
expr (UnaryOp Arccos u :: Expr
u)       sm :: PrintingInformation
sm = PrintingInformation -> Ops -> Expr -> Expr
mkCall PrintingInformation
sm Ops
P.Arccos Expr
u
expr (UnaryOp Arctan u :: Expr
u)       sm :: PrintingInformation
sm = PrintingInformation -> Ops -> Expr -> Expr
mkCall PrintingInformation
sm Ops
P.Arctan Expr
u
expr (UnaryOp Exp u :: Expr
u)          sm :: PrintingInformation
sm = [Expr] -> Expr
P.Row [Ops -> Expr
P.MO Ops
P.Exp, Expr -> Expr
P.Sup (Expr -> Expr) -> Expr -> Expr
forall a b. (a -> b) -> a -> b
$ Expr -> PrintingInformation -> Expr
expr Expr
u PrintingInformation
sm]
expr (UnaryOp Abs u :: Expr
u)          sm :: PrintingInformation
sm = Fence -> Fence -> Expr -> Expr
P.Fenced Fence
P.Abs Fence
P.Abs (Expr -> Expr) -> Expr -> Expr
forall a b. (a -> b) -> a -> b
$ Expr -> PrintingInformation -> Expr
expr Expr
u PrintingInformation
sm
expr (UnaryOpB Not u :: Expr
u)         sm :: PrintingInformation
sm = [Expr] -> Expr
P.Row [Ops -> Expr
P.MO Ops
P.Not, Expr -> PrintingInformation -> Expr
expr Expr
u PrintingInformation
sm]
expr (UnaryOpVN Norm u :: Expr
u)       sm :: PrintingInformation
sm = Fence -> Fence -> Expr -> Expr
P.Fenced Fence
P.Norm Fence
P.Norm (Expr -> Expr) -> Expr -> Expr
forall a b. (a -> b) -> a -> b
$ Expr -> PrintingInformation -> Expr
expr Expr
u PrintingInformation
sm
expr (UnaryOpVN Dim u :: Expr
u)        sm :: PrintingInformation
sm = PrintingInformation -> Ops -> Expr -> Expr
mkCall PrintingInformation
sm Ops
P.Dim Expr
u
expr (UnaryOp Sqrt u :: Expr
u)         sm :: PrintingInformation
sm = Expr -> Expr
P.Sqrt (Expr -> Expr) -> Expr -> Expr
forall a b. (a -> b) -> a -> b
$ Expr -> PrintingInformation -> Expr
expr Expr
u PrintingInformation
sm
expr (UnaryOp Neg u :: Expr
u)          sm :: PrintingInformation
sm = PrintingInformation -> Expr -> Expr
neg PrintingInformation
sm Expr
u
expr (UnaryOpVV NegV u :: Expr
u)       sm :: PrintingInformation
sm = PrintingInformation -> Expr -> Expr
neg PrintingInformation
sm Expr
u
expr (ArithBinaryOp Frac a :: Expr
a b :: Expr
b) sm :: PrintingInformation
sm = Expr -> Expr -> Expr
P.Div (Expr -> PrintingInformation -> Expr
expr Expr
a PrintingInformation
sm) (Expr -> PrintingInformation -> Expr
expr Expr
b PrintingInformation
sm)
expr (ArithBinaryOp Pow a :: Expr
a b :: Expr
b)  sm :: PrintingInformation
sm = PrintingInformation -> Expr -> Expr -> Expr
pow PrintingInformation
sm Expr
a Expr
b
expr (ArithBinaryOp Subt a :: Expr
a b :: Expr
b) sm :: PrintingInformation
sm = [Expr] -> Expr
P.Row [Expr -> PrintingInformation -> Expr
expr Expr
a PrintingInformation
sm, Ops -> Expr
P.MO Ops
P.Subt, Expr -> PrintingInformation -> Expr
expr Expr
b PrintingInformation
sm]
expr (BoolBinaryOp Impl a :: Expr
a b :: Expr
b)  sm :: PrintingInformation
sm = PrintingInformation -> Ops -> Expr -> Expr -> Expr
mkBOp PrintingInformation
sm Ops
P.Impl Expr
a Expr
b
expr (BoolBinaryOp Iff a :: Expr
a b :: Expr
b)   sm :: PrintingInformation
sm = PrintingInformation -> Ops -> Expr -> Expr -> Expr
mkBOp PrintingInformation
sm Ops
P.Iff Expr
a Expr
b
expr (EqBinaryOp Eq a :: Expr
a b :: Expr
b)      sm :: PrintingInformation
sm = PrintingInformation -> Ops -> Expr -> Expr -> Expr
mkBOp PrintingInformation
sm Ops
P.Eq Expr
a Expr
b
expr (EqBinaryOp NEq a :: Expr
a b :: Expr
b)     sm :: PrintingInformation
sm = PrintingInformation -> Ops -> Expr -> Expr -> Expr
mkBOp PrintingInformation
sm Ops
P.NEq Expr
a Expr
b
expr (LABinaryOp Index a :: Expr
a b :: Expr
b)   sm :: PrintingInformation
sm = PrintingInformation -> Expr -> Expr -> Expr
indx PrintingInformation
sm Expr
a Expr
b
expr (OrdBinaryOp Lt a :: Expr
a b :: Expr
b)     sm :: PrintingInformation
sm = PrintingInformation -> Ops -> Expr -> Expr -> Expr
mkBOp PrintingInformation
sm Ops
P.Lt Expr
a Expr
b
expr (OrdBinaryOp Gt a :: Expr
a b :: Expr
b)     sm :: PrintingInformation
sm = PrintingInformation -> Ops -> Expr -> Expr -> Expr
mkBOp PrintingInformation
sm Ops
P.Gt Expr
a Expr
b
expr (OrdBinaryOp LEq a :: Expr
a b :: Expr
b)    sm :: PrintingInformation
sm = PrintingInformation -> Ops -> Expr -> Expr -> Expr
mkBOp PrintingInformation
sm Ops
P.LEq Expr
a Expr
b
expr (OrdBinaryOp GEq a :: Expr
a b :: Expr
b)    sm :: PrintingInformation
sm = PrintingInformation -> Ops -> Expr -> Expr -> Expr
mkBOp PrintingInformation
sm Ops
P.GEq Expr
a Expr
b
expr (VVNBinaryOp Dot a :: Expr
a b :: Expr
b)    sm :: PrintingInformation
sm = PrintingInformation -> Ops -> Expr -> Expr -> Expr
mkBOp PrintingInformation
sm Ops
P.Dot Expr
a Expr
b
expr (VVVBinaryOp Cross a :: Expr
a b :: Expr
b)  sm :: PrintingInformation
sm = PrintingInformation -> Ops -> Expr -> Expr -> Expr
mkBOp PrintingInformation
sm Ops
P.Cross Expr
a Expr
b
expr (Operator o :: AssocArithOper
o d :: DiscreteDomainDesc Expr Expr
d e :: Expr
e)         sm :: PrintingInformation
sm = PrintingInformation
-> AssocArithOper -> DiscreteDomainDesc Expr Expr -> Expr -> Expr
forall (t :: RTopology).
PrintingInformation
-> AssocArithOper -> DomainDesc t Expr Expr -> Expr -> Expr
eop PrintingInformation
sm AssocArithOper
o DiscreteDomainDesc Expr Expr
d Expr
e
expr (RealI c :: UID
c ri :: RealInterval Expr Expr
ri)             sm :: PrintingInformation
sm = PrintingInformation -> Symbol -> RealInterval Expr Expr -> Expr
renderRealInt PrintingInformation
sm (Stage -> ChunkDB -> UID -> Symbol
lookupC (PrintingInformation
sm PrintingInformation
-> Getting Stage PrintingInformation Stage -> Stage
forall s a. s -> Getting a s a -> a
^. Getting Stage PrintingInformation Stage
Lens' PrintingInformation Stage
stg)
  (PrintingInformation
sm PrintingInformation
-> Getting ChunkDB PrintingInformation ChunkDB -> ChunkDB
forall s a. s -> Getting a s a -> a
^. Getting ChunkDB PrintingInformation ChunkDB
Lens' PrintingInformation ChunkDB
ckdb) UID
c) RealInterval Expr Expr
ri

-- | Common method of converting associative operations into printable layout AST.
assocExpr :: P.Ops -> Int -> [Expr] -> PrintingInformation -> P.Expr
assocExpr :: Ops -> Int -> [Expr] -> PrintingInformation -> Expr
assocExpr op :: Ops
op prec :: Int
prec exprs :: [Expr]
exprs sm :: PrintingInformation
sm = [Expr] -> Expr
P.Row ([Expr] -> Expr) -> [Expr] -> Expr
forall a b. (a -> b) -> a -> b
$ Expr -> [Expr] -> [Expr]
forall a. a -> [a] -> [a]
intersperse (Ops -> Expr
P.MO Ops
op) ([Expr] -> [Expr]) -> [Expr] -> [Expr]
forall a b. (a -> b) -> a -> b
$ (Expr -> Expr) -> [Expr] -> [Expr]
forall a b. (a -> b) -> [a] -> [b]
map (PrintingInformation -> Int -> Expr -> Expr
expr' PrintingInformation
sm Int
prec) [Expr]
exprs

-- | Helper for rendering printable expressions.
addExpr :: [Expr] -> AssocArithOper -> PrintingInformation -> [P.Expr]
addExpr :: [Expr] -> AssocArithOper -> PrintingInformation -> [Expr]
addExpr exprs :: [Expr]
exprs o :: AssocArithOper
o sm :: PrintingInformation
sm = [Expr] -> [Expr]
addExprFilter ((Expr -> Expr) -> [Expr] -> [Expr]
forall a b. (a -> b) -> [a] -> [b]
map (PrintingInformation -> Int -> Expr -> Expr
expr' PrintingInformation
sm (AssocArithOper -> Int
precA AssocArithOper
o)) [Expr]
exprs)

-- | Add add symbol only when the second Expr is not negation 
addExprFilter :: [P.Expr] -> [P.Expr]
addExprFilter :: [Expr] -> [Expr]
addExprFilter [] = []
addExprFilter [x :: Expr
x] = [Expr
x]
addExprFilter (x1 :: Expr
x1:P.Row[P.MO P.Neg, x2 :: Expr
x2]:xs :: [Expr]
xs) = Expr
x1 Expr -> [Expr] -> [Expr]
forall a. a -> [a] -> [a]
: [Expr] -> [Expr]
addExprFilter ([Expr] -> Expr
P.Row[Ops -> Expr
P.MO Ops
P.Neg, Expr
x2] Expr -> [Expr] -> [Expr]
forall a. a -> [a] -> [a]
: [Expr]
xs)
addExprFilter (x :: Expr
x:xs :: [Expr]
xs) = Expr
x Expr -> [Expr] -> [Expr]
forall a. a -> [a] -> [a]
: Ops -> Expr
P.MO Ops
P.Add Expr -> [Expr] -> [Expr]
forall a. a -> [a] -> [a]
: [Expr] -> [Expr]
addExprFilter [Expr]
xs

-- | Helper for rendering printable expressions.
mulExpr ::  [Expr] -> AssocArithOper -> PrintingInformation -> [P.Expr]
mulExpr :: [Expr] -> AssocArithOper -> PrintingInformation -> [Expr]
mulExpr (hd1 :: Expr
hd1:hd2 :: Expr
hd2:tl :: [Expr]
tl) o :: AssocArithOper
o sm :: PrintingInformation
sm = case (Expr
hd1, Expr
hd2) of
  (a :: Expr
a, Lit (Int _))      ->  [PrintingInformation -> Int -> Expr -> Expr
expr' PrintingInformation
sm (AssocArithOper -> Int
precA AssocArithOper
o) Expr
a, Ops -> Expr
P.MO Ops
P.Dot] [Expr] -> [Expr] -> [Expr]
forall a. [a] -> [a] -> [a]
++ [Expr] -> AssocArithOper -> PrintingInformation -> [Expr]
mulExpr (Expr
hd2 Expr -> [Expr] -> [Expr]
forall a. a -> [a] -> [a]
: [Expr]
tl) AssocArithOper
o PrintingInformation
sm
  (a :: Expr
a, Lit (ExactDbl _)) ->  [PrintingInformation -> Int -> Expr -> Expr
expr' PrintingInformation
sm (AssocArithOper -> Int
precA AssocArithOper
o) Expr
a, Ops -> Expr
P.MO Ops
P.Dot] [Expr] -> [Expr] -> [Expr]
forall a. [a] -> [a] -> [a]
++ [Expr] -> AssocArithOper -> PrintingInformation -> [Expr]
mulExpr (Expr
hd2 Expr -> [Expr] -> [Expr]
forall a. a -> [a] -> [a]
: [Expr]
tl) AssocArithOper
o PrintingInformation
sm
  (a :: Expr
a, Lit (Dbl _))      ->  [PrintingInformation -> Int -> Expr -> Expr
expr' PrintingInformation
sm (AssocArithOper -> Int
precA AssocArithOper
o) Expr
a, Ops -> Expr
P.MO Ops
P.Dot] [Expr] -> [Expr] -> [Expr]
forall a. [a] -> [a] -> [a]
++ [Expr] -> AssocArithOper -> PrintingInformation -> [Expr]
mulExpr (Expr
hd2 Expr -> [Expr] -> [Expr]
forall a. a -> [a] -> [a]
: [Expr]
tl) AssocArithOper
o PrintingInformation
sm
  (a :: Expr
a, _)                ->  [PrintingInformation -> Int -> Expr -> Expr
expr' PrintingInformation
sm (AssocArithOper -> Int
precA AssocArithOper
o) Expr
a, Ops -> Expr
P.MO Ops
P.Mul] [Expr] -> [Expr] -> [Expr]
forall a. [a] -> [a] -> [a]
++ [Expr] -> AssocArithOper -> PrintingInformation -> [Expr]
mulExpr (Expr
hd2 Expr -> [Expr] -> [Expr]
forall a. a -> [a] -> [a]
: [Expr]
tl) AssocArithOper
o PrintingInformation
sm
mulExpr [hd :: Expr
hd]         o :: AssocArithOper
o sm :: PrintingInformation
sm = [PrintingInformation -> Int -> Expr -> Expr
expr' PrintingInformation
sm (AssocArithOper -> Int
precA AssocArithOper
o) Expr
hd]
mulExpr []           o :: AssocArithOper
o sm :: PrintingInformation
sm = [PrintingInformation -> Int -> Expr -> Expr
expr' PrintingInformation
sm (AssocArithOper -> Int
precA AssocArithOper
o) (Integer -> Expr
forall r. LiteralC r => Integer -> r
int 1)]


-- | Helper that adds parenthesis to the first expression. The second expression
-- is written as a superscript attached to the first.
withParens :: PrintingInformation -> Expr -> Expr -> P.Expr
withParens :: PrintingInformation -> Expr -> Expr -> Expr
withParens prI :: PrintingInformation
prI a :: Expr
a b :: Expr
b = [Expr] -> Expr
P.Row [Expr -> Expr
parens (Expr -> PrintingInformation -> Expr
expr Expr
a PrintingInformation
prI), Expr -> Expr
P.Sup (Expr -> PrintingInformation -> Expr
expr Expr
b PrintingInformation
prI)]

-- | Helper for properly rendering exponents.
pow :: PrintingInformation -> Expr -> Expr -> P.Expr
pow :: PrintingInformation -> Expr -> Expr -> Expr
pow prI :: PrintingInformation
prI a :: Expr
a@(AssocA AddI _)          b :: Expr
b = PrintingInformation -> Expr -> Expr -> Expr
withParens PrintingInformation
prI Expr
a Expr
b
pow prI :: PrintingInformation
prI a :: Expr
a@(AssocA AddRe _)         b :: Expr
b = PrintingInformation -> Expr -> Expr -> Expr
withParens PrintingInformation
prI Expr
a Expr
b
pow prI :: PrintingInformation
prI a :: Expr
a@(AssocA MulI _)          b :: Expr
b = PrintingInformation -> Expr -> Expr -> Expr
withParens PrintingInformation
prI Expr
a Expr
b
pow prI :: PrintingInformation
prI a :: Expr
a@(AssocA MulRe _)         b :: Expr
b = PrintingInformation -> Expr -> Expr -> Expr
withParens PrintingInformation
prI Expr
a Expr
b
pow prI :: PrintingInformation
prI a :: Expr
a@(ArithBinaryOp Subt _ _) b :: Expr
b = PrintingInformation -> Expr -> Expr -> Expr
withParens PrintingInformation
prI Expr
a Expr
b
pow prI :: PrintingInformation
prI a :: Expr
a@(ArithBinaryOp Frac _ _) b :: Expr
b = PrintingInformation -> Expr -> Expr -> Expr
withParens PrintingInformation
prI Expr
a Expr
b
pow prI :: PrintingInformation
prI a :: Expr
a@(ArithBinaryOp Pow _ _)  b :: Expr
b = PrintingInformation -> Expr -> Expr -> Expr
withParens PrintingInformation
prI Expr
a Expr
b
pow prI :: PrintingInformation
prI a :: Expr
a                          b :: Expr
b = [Expr] -> Expr
P.Row [Expr -> PrintingInformation -> Expr
expr Expr
a PrintingInformation
prI, Expr -> Expr
P.Sup (Expr -> PrintingInformation -> Expr
expr Expr
b PrintingInformation
prI)]

-- | Print a 'RealInterval'.
renderRealInt :: PrintingInformation -> Symbol -> RealInterval Expr Expr -> P.Expr
renderRealInt :: PrintingInformation -> Symbol -> RealInterval Expr Expr -> Expr
renderRealInt st :: PrintingInformation
st s :: Symbol
s (Bounded (Inc,a :: Expr
a) (Inc,b :: Expr
b)) =
  [Expr] -> Expr
P.Row [Expr -> PrintingInformation -> Expr
expr Expr
a PrintingInformation
st, Ops -> Expr
P.MO Ops
P.LEq, Symbol -> Expr
symbol Symbol
s, Ops -> Expr
P.MO Ops
P.LEq, Expr -> PrintingInformation -> Expr
expr Expr
b PrintingInformation
st]
renderRealInt st :: PrintingInformation
st s :: Symbol
s (Bounded (Inc,a :: Expr
a) (Exc,b :: Expr
b)) =
  [Expr] -> Expr
P.Row [Expr -> PrintingInformation -> Expr
expr Expr
a PrintingInformation
st, Ops -> Expr
P.MO Ops
P.LEq, Symbol -> Expr
symbol Symbol
s, Ops -> Expr
P.MO Ops
P.Lt, Expr -> PrintingInformation -> Expr
expr Expr
b PrintingInformation
st]
renderRealInt st :: PrintingInformation
st s :: Symbol
s (Bounded (Exc,a :: Expr
a) (Inc,b :: Expr
b)) =
  [Expr] -> Expr
P.Row [Expr -> PrintingInformation -> Expr
expr Expr
a PrintingInformation
st, Ops -> Expr
P.MO Ops
P.Lt, Symbol -> Expr
symbol Symbol
s, Ops -> Expr
P.MO Ops
P.LEq, Expr -> PrintingInformation -> Expr
expr Expr
b PrintingInformation
st]
renderRealInt st :: PrintingInformation
st s :: Symbol
s (Bounded (Exc,a :: Expr
a) (Exc,b :: Expr
b)) =
  [Expr] -> Expr
P.Row [Expr -> PrintingInformation -> Expr
expr Expr
a PrintingInformation
st, Ops -> Expr
P.MO Ops
P.Lt, Symbol -> Expr
symbol Symbol
s, Ops -> Expr
P.MO Ops
P.Lt, Expr -> PrintingInformation -> Expr
expr Expr
b PrintingInformation
st]
renderRealInt st :: PrintingInformation
st s :: Symbol
s (UpTo (Inc,a :: Expr
a))   = [Expr] -> Expr
P.Row [Symbol -> Expr
symbol Symbol
s, Ops -> Expr
P.MO Ops
P.LEq, Expr -> PrintingInformation -> Expr
expr Expr
a PrintingInformation
st]
renderRealInt st :: PrintingInformation
st s :: Symbol
s (UpTo (Exc,a :: Expr
a))   = [Expr] -> Expr
P.Row [Symbol -> Expr
symbol Symbol
s, Ops -> Expr
P.MO Ops
P.Lt,  Expr -> PrintingInformation -> Expr
expr Expr
a PrintingInformation
st]
renderRealInt st :: PrintingInformation
st s :: Symbol
s (UpFrom (Inc,a :: Expr
a)) = [Expr] -> Expr
P.Row [Symbol -> Expr
symbol Symbol
s, Ops -> Expr
P.MO Ops
P.GEq, Expr -> PrintingInformation -> Expr
expr Expr
a PrintingInformation
st]
renderRealInt st :: PrintingInformation
st s :: Symbol
s (UpFrom (Exc,a :: Expr
a)) = [Expr] -> Expr
P.Row [Symbol -> Expr
symbol Symbol
s, Ops -> Expr
P.MO Ops
P.Gt,  Expr -> PrintingInformation -> Expr
expr Expr
a PrintingInformation
st]