module Language.Egison.PrettyMath.Maxima
( showMathExpr
) where
import Language.Egison.PrettyMath.AST
showMathExpr :: MathExpr -> String
showMathExpr :: MathExpr -> String
showMathExpr (Atom String
a []) = String
a
showMathExpr (Partial MathExpr
_ [MathExpr]
_) = String
"undefined"
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]) = MathExpr -> String
showMathExpr MathExpr
y String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"^(1/" 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
"exp" []) [MathExpr
x]) = String
"exp(" 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]
_ [MathIndex]
_) = String
"undefined"
showMathExpr (Tuple [MathExpr]
_) = String
"undefined"
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' x :: MathExpr
x@(Plus [MathExpr]
_) = 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
x = MathExpr -> String
showMathExpr MathExpr
x
showMathExprArg :: [MathExpr] -> String
showMathExprArg :: [MathExpr] -> String
showMathExprArg [] = String
""
showMathExprArg [Tensor [MathExpr]
_ []] = String
"undefined"
showMathExprArg [MathExpr
a] = MathExpr -> String
showMathExpr MathExpr
a
showMathExprArg [MathExpr]
lvs = MathExpr -> String
showMathExpr ([MathExpr] -> MathExpr
forall a. [a] -> a
head [MathExpr]
lvs) String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
", " String -> String -> String
forall a. [a] -> [a] -> [a]
++ [MathExpr] -> String
showMathExprArg ([MathExpr] -> [MathExpr]
forall a. [a] -> [a]
tail [MathExpr]
lvs)