module Language.Egison.PrettyMath.Mathematica
( showMathExpr
) where
import Data.List (intercalate)
import Language.Egison.PrettyMath.AST
showMathExpr :: MathExpr -> String
showMathExpr :: MathExpr -> String
showMathExpr (Atom String
a []) = String
a
showMathExpr (Atom String
a [MathIndex]
xs) = String
a String -> String -> String
forall a. [a] -> [a] -> [a]
++ [MathIndex] -> String
showMathExprIndices [MathIndex]
xs
showMathExpr (Partial MathExpr
f [MathExpr]
xs) = MathExpr -> String
showMathExpr MathExpr
f String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"_" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String -> [MathExpr] -> String
showMathExprs String
"_" [MathExpr]
xs
showMathExpr (NegativeAtom String
a) = String
"-" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
a
showMathExpr (Plus []) = String
""
showMathExpr (Plus (MathExpr
x:[MathExpr]
xs)) = MathExpr -> String
showMathExpr MathExpr
x String -> String -> String
forall a. [a] -> [a] -> [a]
++ [MathExpr] -> String
showMathExprForPlus [MathExpr]
xs
where
showMathExprForPlus :: [MathExpr] -> String
showMathExprForPlus :: [MathExpr] -> String
showMathExprForPlus [] = String
""
showMathExprForPlus (NegativeAtom String
a:[MathExpr]
xs) = String
" - " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
a String -> String -> String
forall a. [a] -> [a] -> [a]
++ [MathExpr] -> String
showMathExprForPlus [MathExpr]
xs
showMathExprForPlus (Multiply (NegativeAtom String
"1":[MathExpr]
ys):[MathExpr]
xs) = String
" - " String -> String -> String
forall a. [a] -> [a] -> [a]
++ MathExpr -> String
showMathExpr ([MathExpr] -> MathExpr
Multiply [MathExpr]
ys) String -> String -> String
forall a. [a] -> [a] -> [a]
++ [MathExpr] -> String
showMathExprForPlus [MathExpr]
xs
showMathExprForPlus (Multiply (NegativeAtom String
a:[MathExpr]
ys):[MathExpr]
xs) = String
" - " String -> String -> String
forall a. [a] -> [a] -> [a]
++ MathExpr -> String
showMathExpr ([MathExpr] -> MathExpr
Multiply (String -> [MathIndex] -> MathExpr
Atom String
a []MathExpr -> [MathExpr] -> [MathExpr]
forall a. a -> [a] -> [a]
:[MathExpr]
ys)) String -> String -> String
forall a. [a] -> [a] -> [a]
++ [MathExpr] -> String
showMathExprForPlus [MathExpr]
xs
showMathExprForPlus (MathExpr
x:[MathExpr]
xs) = String
" + " String -> String -> String
forall a. [a] -> [a] -> [a]
++ MathExpr -> String
showMathExpr MathExpr
x String -> String -> String
forall a. [a] -> [a] -> [a]
++ [MathExpr] -> String
showMathExprForPlus [MathExpr]
xs
showMathExpr (Multiply []) = String
""
showMathExpr (Multiply [MathExpr
x]) = MathExpr -> String
showMathExpr MathExpr
x
showMathExpr (Multiply (Atom String
"1" []:[MathExpr]
xs)) = MathExpr -> String
showMathExpr ([MathExpr] -> MathExpr
Multiply [MathExpr]
xs)
showMathExpr (Multiply (NegativeAtom String
"1":[MathExpr]
xs)) = String
"-" String -> String -> String
forall a. [a] -> [a] -> [a]
++ MathExpr -> String
showMathExpr ([MathExpr] -> MathExpr
Multiply [MathExpr]
xs)
showMathExpr (Multiply (MathExpr
x:[MathExpr]
xs)) = MathExpr -> String
showMathExpr' MathExpr
x String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" " String -> String -> String
forall a. [a] -> [a] -> [a]
++ MathExpr -> String
showMathExpr ([MathExpr] -> MathExpr
Multiply [MathExpr]
xs)
showMathExpr (Div MathExpr
x MathExpr
y) = MathExpr -> String
addBracket MathExpr
x String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"/" String -> String -> String
forall a. [a] -> [a] -> [a]
++ MathExpr -> String
addBracket MathExpr
y
where
addBracket :: MathExpr -> String
addBracket x :: MathExpr
x@(Atom String
_ []) = MathExpr -> String
showMathExpr MathExpr
x
addBracket MathExpr
x = String
"(" String -> String -> String
forall a. [a] -> [a] -> [a]
++ MathExpr -> String
showMathExpr MathExpr
x String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
")"
showMathExpr (Power MathExpr
lv1 MathExpr
lv2) = MathExpr -> String
showMathExpr MathExpr
lv1 String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"^" String -> String -> String
forall a. [a] -> [a] -> [a]
++ MathExpr -> String
showMathExpr MathExpr
lv2
showMathExpr (Func (Atom String
"sqrt" []) [MathExpr
x]) = String
"Sqrt[" String -> String -> String
forall a. [a] -> [a] -> [a]
++ MathExpr -> String
showMathExpr MathExpr
x String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"]"
showMathExpr (Func (Atom String
"rt" []) [MathExpr
x, MathExpr
y]) = String
"Surd[" String -> String -> String
forall a. [a] -> [a] -> [a]
++ MathExpr -> String
showMathExpr MathExpr
x String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"," String -> String -> String
forall a. [a] -> [a] -> [a]
++ MathExpr -> String
showMathExpr MathExpr
y String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"]"
showMathExpr (Func (Atom String
"exp" []) [MathExpr
x])= String
"e^(" String -> String -> String
forall a. [a] -> [a] -> [a]
++ MathExpr -> String
showMathExpr MathExpr
x String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
")"
showMathExpr (Func MathExpr
f [MathExpr]
xs) = MathExpr -> String
showMathExpr MathExpr
f String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"(" String -> String -> String
forall a. [a] -> [a] -> [a]
++ [MathExpr] -> String
showMathExprArg [MathExpr]
xs String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
")"
showMathExpr (Tensor [MathExpr]
lvs [MathIndex]
mis)
| [MathIndex] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [MathIndex]
mis = String
"{" String -> String -> String
forall a. [a] -> [a] -> [a]
++ [MathExpr] -> String
showMathExprArg [MathExpr]
lvs String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"}"
| Bool -> Bool
not ((MathIndex -> Bool) -> [MathIndex] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any MathIndex -> Bool
isSub [MathIndex]
mis) = String
"{" String -> String -> String
forall a. [a] -> [a] -> [a]
++ [MathExpr] -> String
showMathExprArg [MathExpr]
lvs String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"}^(" String -> String -> String
forall a. [a] -> [a] -> [a]
++ [MathIndex] -> String
showMathExprIndices [MathIndex]
mis String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
")"
| (MathIndex -> Bool) -> [MathIndex] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all MathIndex -> Bool
isSub [MathIndex]
mis = String
"{" String -> String -> String
forall a. [a] -> [a] -> [a]
++ [MathExpr] -> String
showMathExprArg [MathExpr]
lvs String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"}_(" String -> String -> String
forall a. [a] -> [a] -> [a]
++ [MathIndex] -> String
showMathExprIndices [MathIndex]
mis String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
")"
| Bool
otherwise = String
"{" String -> String -> String
forall a. [a] -> [a] -> [a]
++ [MathExpr] -> String
showMathExprArg [MathExpr]
lvs String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"}_(" String -> String -> String
forall a. [a] -> [a] -> [a]
++ [MathIndex] -> String
showMathExprIndices ((MathIndex -> Bool) -> [MathIndex] -> [MathIndex]
forall a. (a -> Bool) -> [a] -> [a]
filter MathIndex -> Bool
isSub [MathIndex]
mis) String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
")^(" String -> String -> String
forall a. [a] -> [a] -> [a]
++ [MathIndex] -> String
showMathExprIndices ((MathIndex -> Bool) -> [MathIndex] -> [MathIndex]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool) -> (MathIndex -> Bool) -> MathIndex -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MathIndex -> Bool
isSub) [MathIndex]
mis) String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
")"
showMathExpr (Tuple [MathExpr]
xs) = String
"(" String -> String -> String
forall a. [a] -> [a] -> [a]
++ [MathExpr] -> String
showMathExprArg [MathExpr]
xs String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
")"
showMathExpr (Collection [MathExpr]
xs) = String
"{" String -> String -> String
forall a. [a] -> [a] -> [a]
++ [MathExpr] -> String
showMathExprArg [MathExpr]
xs String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"}"
showMathExpr (Quote MathExpr
x) = String
"(" String -> String -> String
forall a. [a] -> [a] -> [a]
++ MathExpr -> String
showMathExpr MathExpr
x String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
")"
showMathExpr' :: MathExpr -> String
showMathExpr' :: MathExpr -> String
showMathExpr' (Plus [MathExpr]
xs) = String
"(" String -> String -> String
forall a. [a] -> [a] -> [a]
++ MathExpr -> String
showMathExpr ([MathExpr] -> MathExpr
Plus [MathExpr]
xs) String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
")"
showMathExpr' MathExpr
x = MathExpr -> String
showMathExpr MathExpr
x
showMathExprs :: String -> [MathExpr] -> String
showMathExprs :: String -> [MathExpr] -> String
showMathExprs String
sep [MathExpr]
exprs = String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
sep ([String] -> String) -> [String] -> String
forall a b. (a -> b) -> a -> b
$ (MathExpr -> String) -> [MathExpr] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map MathExpr -> String
showMathExpr [MathExpr]
exprs
showMathExprArg :: [MathExpr] -> String
showMathExprArg :: [MathExpr] -> String
showMathExprArg = String -> [MathExpr] -> String
showMathExprs String
", "
showMathExprIndices :: [MathIndex] -> String
showMathExprIndices :: [MathIndex] -> String
showMathExprIndices [] = String -> String
forall a. HasCallStack => String -> a
error String
"unreachable"
showMathExprIndices [MathIndex]
lvs = (MathIndex -> String) -> [MathIndex] -> String
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap MathIndex -> String
showMathIndex [MathIndex]
lvs
showMathIndex :: MathIndex -> String
showMathIndex :: MathIndex -> String
showMathIndex (Super MathExpr
a) = MathExpr -> String
showMathExpr MathExpr
a
showMathIndex (Sub MathExpr
a) = MathExpr -> String
showMathExpr MathExpr
a