<commit_before>module Language.LaTeX.Builder.Math

  (charToMath, mstring, protect, verb,
   _Delta, _Gamma, _Lambda, _Leftarrow, _Leftrightarrow, _Omega, _Phi, _Pi, _Pr,
   _Rightarrow, _Sigma, _Theta, _Xi, acute, aleph, alpha, approx, array, at,
   backslash, bar, beta, between, bigcap, bigcup, bigvee, bigwedge, bmod, bot,
   braces, brackets, breve, cal, cap, cdots, cdotp, check, chi, circ, cong, cos, cosh,
   cot, csc, cup, ddot, ddots, delta, det, diamond, dim, displaystyle, divide,
   dot, downarrow, emptyset, epsilon, eq, neq, equiv, eta, exists, forall_, frac,
   gamma, gcd, ge, geq, grave, group, hat, iff, imath, implies, in_, inf,
   infty, int, iota, jmath, kappa, lambda, langle, lbrace, lceiling, lcm,
   ldots, ldotp, le, leftarrow, leftrightarrow, leq, lfloor, lim, liminf, limsup, ln,
   log, mathBinOp, mathBinOps, mathCmd, mathCmdArg, mathCmdArgs, mathCmdsArg,
   mathDecl, mathGroup, allMathItems, allMathDecls, rawDecls, decl, decls,
   mathToLR, mathbb, mathbf,
   mathcal, mathfrak, mathtt, max, min, mit, mleft, mediumspace,
   negthinspace, mod, models, mrat, mright, msup, thickspace,
   thinspace, mu, nabla, ne, neg, notin, nu, oint, omega, oplus, otimes,
   overbrace, overline, parenChar, parens, partial, phi, pi, pm, pmod, prec,
   prod, propto, psi, quad, rangle, rawMath, rawMathChar, rbrace, rceiling,
   rfloor, rho, rightarrow, scriptscriptstyle, scriptstyle, sec, sigma, sin, sinh,
   space, sqrt, sqrt', square, stackrel, sub, subset, subseteq, succ, sum, sup,
   supset, supseteq, tan, tanh, tau, text, textstyle, theta, tilde, times, to, top,
   underbrace, underline, uparrow, upsilon, varepsilon, varphi, vartheta, vdash,
   vdots, vec, vee, wedge, widehat, widetilde, xi, zeta,
   a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,
   _A, _B, _C, _D, _E, _F, _G, _H, _I, _J, _K, _L, _M, _N, _O, _P,
   _Q, _R, _S, _T, _U, _V, _W, _X, _Y, _Z,
   _Downarrow, _Uparrow,
   vartriangleright,phantom,

   -- reexports
   cells, cell, vline, hline, cline
  ) where

import Prelude hiding (sqrt, min, max, lcm, gcd, log, mod, tanh, cosh, tan, sinh,
                       sin, cos, succ, sum, pi, mapM)
import Data.List hiding (sum, and, group)
import Data.Ratio
import Data.Char
import Data.Foldable (foldMap)
import Data.Traversable (sequenceA, mapM)
import Data.Maybe
import Data.Monoid
import Data.String
import qualified Data.IntMap as IntMap
import Control.Arrow
import Control.Applicative
import Control.Monad hiding (mapM)
import Control.Monad.Error (throwError)

import Language.LaTeX.Types
import Language.LaTeX.Builder.MonoidUtils
import Language.LaTeX.Builder (cell, cells, vline, hline, cline)
import qualified Language.LaTeX.Builder as B
import qualified Language.LaTeX.Builder.Internal as B

liftMath :: (MathItm -> MathItm) -> MathItem -> MathItem
liftMath f = MathItem . liftM f . mathItmM

liftMath2 :: (MathItm -> MathItm -> MathItm) -> MathItem -> MathItem -> MathItem
liftMath2 f (MathItem a) (MathItem b) = MathItem (liftM2 f a b)

group :: MathItem -> MathItem
group = liftMath MathGroup

-- | Same as 'group'
mathGroup :: MathItem -> MathItem
mathGroup = liftMath MathGroup

mathCmdArgs :: String -> [Arg MathItem] -> MathItem
mathCmdArgs m1 ys = MathItem $ MathCmdArgs m1 <$> mapM (mapM mathItmM) ys

mathCmdArg :: String -> MathItem -> MathItem
mathCmdArg m1 m2 = mathCmdArgs m1 [B.mandatory m2]

mathToLR :: String -> [Arg LatexItem] -> MathItem
mathToLR cmdName args = MathItem $ MathToLR cmdName <$> mapM sequenceA args

mathDecl :: String -> MathDecl
mathDecl = pure . MathDcl

rawDecls :: [MathDecl] -> MathItem
rawDecls = MathItem . fmap MathDecls . sequenceA

decls :: [MathDecl] -> MathItem -> MathItem
decls ds itm = group (rawDecls ds <> itm)

decl :: MathDecl -> MathItem -> MathItem
decl dcl = decls [dcl]

mathCmd :: String -> MathItem
mathCmd = MathItem . pure . (`MathCmdArgs` [])

mathBinOp :: String -> MathItem -> MathItem -> MathItem
mathBinOp = liftMath2 . MathBinOp

rawMath :: String -> MathItem
rawMath = MathItem . pure . RawMath

rawMathChar :: Char -> MathItem
rawMathChar = rawMath . ('{':) . (:"}")

mrat :: Rational -> MathItem
mrat = fromRational

sub, sup :: MathItem -> MathItem
sub = (rawMath "_" <>) . mathGroup
sup = (rawMath "^" <>) . mathGroup

frac, stackrel :: MathItem -> MathItem -> MathItem
frac m1 m2 = mathCmdArgs "frac" [B.mandatory m1,B.mandatory m2]
stackrel m1 m2 = mathCmdArgs "stackrel" [B.mandatory m1,B.mandatory m2]

sqrt :: MathItem -> MathItem
sqrt = mathCmdArgs "sqrt" . (:[]) . B.mandatory

sqrt' :: MathItem -> MathItem -> MathItem
sqrt' n1 m1 = mathCmdArgs "sqrt" [B.optional n1, B.mandatory m1]

phantom :: MathItem -> MathItem
phantom = mathCmdArgs "phantom" . (:[]) . B.mandatory

mleft, mright :: Char -> MathItem
mleft m1  = rawMath "\\left"  <> (MathItem $ RawMath <$> parenChar m1)
mright m1 = rawMath "\\right" <> (MathItem $ RawMath <$> parenChar m1)

between :: Char -> Char -> MathItem -> MathItem
between opening closing m1 = mleft opening <> m1 <> mright closing

parens, braces, brackets :: MathItem -> MathItem
parens   = between '(' ')'
braces   = between '{' '}'
brackets = between '[' ']'

parenChar :: Char -> LatexM String
parenChar m1 | m1 `elem` "([.])" = return [m1]
             | m1 == '{'         = return "\\{"
             | m1 == '}'         = return "\\}"
             | otherwise         = throwError $ "invalid parenthesis-like: " ++ show m1

text :: LatexItem -> MathItem
text arg = mathToLR "text" [B.packageDependency amsmath, B.mandatory arg]

array :: [RowSpec MathItem] -> [Row MathItem] -> MathItem
array spec items = MathItem $ B.tabularLike MathArray (map (fmap mathItmM) spec)
                                                      (map (fmap mathItmM) items)

{- This chunk was extracted from Language.LaTeX.Builder -}
lbrace :: MathItem
lbrace = mathCmd "{"
rbrace :: MathItem
rbrace = mathCmd "}"
space :: MathItem
space = mathCmd " "
at :: MathItem
at = mathCmd "@"
in_ :: MathItem
in_ = mathCmd "in"
forall_ :: MathItem
forall_ = mathCmd "forall"
thinspace :: MathItem
thinspace = mathCmd ","
negthinspace :: MathItem
negthinspace = mathCmd "!"

-- \: or \> in LaTeX
mediumspace :: MathItem
mediumspace = mathCmd ":"
thickspace :: MathItem
thickspace = mathCmd ";"
msup :: MathItem
msup = mathCmd "sup"
alpha :: MathItem
alpha = mathCmd "alpha"
beta :: MathItem
beta = mathCmd "beta"
chi :: MathItem
chi = mathCmd "chi"
-- Chi? (don't forget the charToMath table)
delta :: MathItem
delta = mathCmd "delta"
_Delta :: MathItem
_Delta = mathCmd "Delta"
epsilon :: MathItem
epsilon = mathCmd "epsilon"
varepsilon :: MathItem
varepsilon = mathCmd "varepsilon"
eta :: MathItem
eta = mathCmd "eta"
gamma :: MathItem
gamma = mathCmd "gamma"
_Gamma :: MathItem
_Gamma = mathCmd "Gamma"
iota :: MathItem
iota = mathCmd "iota"
kappa :: MathItem
kappa = mathCmd "kappa"
lambda :: MathItem
lambda = mathCmd "lambda"
_Lambda :: MathItem
_Lambda = mathCmd "Lambda"
mu :: MathItem
mu = mathCmd "mu"
nu :: MathItem
nu = mathCmd "nu"
omega :: MathItem
omega = mathCmd "omega"
_Omega :: MathItem
_Omega = mathCmd "Omega"
{-
omicron :: MathItem
omicron = mathCmd "omicron"
-}
phi :: MathItem
phi = mathCmd "phi"
varphi :: MathItem
varphi = mathCmd "varphi"
_Phi :: MathItem
_Phi = mathCmd "Phi"
pi :: MathItem
pi = mathCmd "pi"
_Pi :: MathItem
_Pi = mathCmd "Pi"<commit_msg>L.L.B.Math: omicron<commit_after>module Language.LaTeX.Builder.Math

  (charToMath, mstring, protect, verb,
   _Delta, _Gamma, _Lambda, _Leftarrow, _Leftrightarrow, _Omega, _Phi, _Pi, _Pr,
   _Rightarrow, _Sigma, _Theta, _Xi, acute, aleph, alpha, approx, array, at,
   backslash, bar, beta, between, bigcap, bigcup, bigvee, bigwedge, bmod, bot,
   braces, brackets, breve, cal, cap, cdots, cdotp, check, chi, circ, cong, cos, cosh,
   cot, csc, cup, ddot, ddots, delta, det, diamond, dim, displaystyle, divide,
   dot, downarrow, emptyset, epsilon, eq, neq, equiv, eta, exists, forall_, frac,
   gamma, gcd, ge, geq, grave, group, hat, iff, imath, implies, in_, inf,
   infty, int, iota, jmath, kappa, lambda, langle, lbrace, lceiling, lcm,
   ldots, ldotp, le, leftarrow, leftrightarrow, leq, lfloor, lim, liminf, limsup, ln,
   log, mathBinOp, mathBinOps, mathCmd, mathCmdArg, mathCmdArgs, mathCmdsArg,
   mathDecl, mathGroup, allMathItems, allMathDecls, rawDecls, decl, decls,
   mathToLR, mathbb, mathbf,
   mathcal, mathfrak, mathtt, max, min, mit, mleft, mediumspace,
   negthinspace, mod, models, mrat, mright, msup, thickspace,
   thinspace, mu, nabla, ne, neg, notin, nu, oint, omega, oplus, otimes,
   overbrace, overline, parenChar, parens, partial, phi, pi, pm, pmod, prec,
   prod, propto, psi, quad, rangle, rawMath, rawMathChar, rbrace, rceiling,
   rfloor, rho, rightarrow, scriptscriptstyle, scriptstyle, sec, sigma, sin, sinh,
   space, sqrt, sqrt', square, stackrel, sub, subset, subseteq, succ, sum, sup,
   supset, supseteq, tan, tanh, tau, text, textstyle, theta, tilde, times, to, top,
   underbrace, underline, uparrow, upsilon, varepsilon, varphi, vartheta, vdash,
   vdots, vec, vee, wedge, widehat, widetilde, xi, zeta,
   a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,
   _A, _B, _C, _D, _E, _F, _G, _H, _I, _J, _K, _L, _M, _N, _O, _P,
   _Q, _R, _S, _T, _U, _V, _W, _X, _Y, _Z,
   _Downarrow, _Uparrow,
   vartriangleright,phantom,

   -- reexports
   cells, cell, vline, hline, cline
  ) where

import Prelude hiding (sqrt, min, max, lcm, gcd, log, mod, tanh, cosh, tan, sinh,
                       sin, cos, succ, sum, pi, mapM)
import Data.List hiding (sum, and, group)
import Data.Ratio
import Data.Char
import Data.Foldable (foldMap)
import Data.Traversable (sequenceA, mapM)
import Data.Maybe
import Data.Monoid
import Data.String
import qualified Data.IntMap as IntMap
import Control.Arrow
import Control.Applicative
import Control.Monad hiding (mapM)
import Control.Monad.Error (throwError)

import Language.LaTeX.Types
import Language.LaTeX.Builder.MonoidUtils
import Language.LaTeX.Builder (cell, cells, vline, hline, cline)
import qualified Language.LaTeX.Builder as B
import qualified Language.LaTeX.Builder.Internal as B

liftMath :: (MathItm -> MathItm) -> MathItem -> MathItem
liftMath f = MathItem . liftM f . mathItmM

liftMath2 :: (MathItm -> MathItm -> MathItm) -> MathItem -> MathItem -> MathItem
liftMath2 f (MathItem a) (MathItem b) = MathItem (liftM2 f a b)

group :: MathItem -> MathItem
group = liftMath MathGroup

-- | Same as 'group'
mathGroup :: MathItem -> MathItem
mathGroup = liftMath MathGroup

mathCmdArgs :: String -> [Arg MathItem] -> MathItem
mathCmdArgs m1 ys = MathItem $ MathCmdArgs m1 <$> mapM (mapM mathItmM) ys

mathCmdArg :: String -> MathItem -> MathItem
mathCmdArg m1 m2 = mathCmdArgs m1 [B.mandatory m2]

mathToLR :: String -> [Arg LatexItem] -> MathItem
mathToLR cmdName args = MathItem $ MathToLR cmdName <$> mapM sequenceA args

mathDecl :: String -> MathDecl
mathDecl = pure . MathDcl

rawDecls :: [MathDecl] -> MathItem
rawDecls = MathItem . fmap MathDecls . sequenceA

decls :: [MathDecl] -> MathItem -> MathItem
decls ds itm = group (rawDecls ds <> itm)

decl :: MathDecl -> MathItem -> MathItem
decl dcl = decls [dcl]

mathCmd :: String -> MathItem
mathCmd = MathItem . pure . (`MathCmdArgs` [])

mathBinOp :: String -> MathItem -> MathItem -> MathItem
mathBinOp = liftMath2 . MathBinOp

rawMath :: String -> MathItem
rawMath = MathItem . pure . RawMath

rawMathChar :: Char -> MathItem
rawMathChar = rawMath . ('{':) . (:"}")

mrat :: Rational -> MathItem
mrat = fromRational

sub, sup :: MathItem -> MathItem
sub = (rawMath "_" <>) . mathGroup
sup = (rawMath "^" <>) . mathGroup

frac, stackrel :: MathItem -> MathItem -> MathItem
frac m1 m2 = mathCmdArgs "frac" [B.mandatory m1,B.mandatory m2]
stackrel m1 m2 = mathCmdArgs "stackrel" [B.mandatory m1,B.mandatory m2]

sqrt :: MathItem -> MathItem
sqrt = mathCmdArgs "sqrt" . (:[]) . B.mandatory

sqrt' :: MathItem -> MathItem -> MathItem
sqrt' n1 m1 = mathCmdArgs "sqrt" [B.optional n1, B.mandatory m1]

phantom :: MathItem -> MathItem
phantom = mathCmdArgs "phantom" . (:[]) . B.mandatory

mleft, mright :: Char -> MathItem
mleft m1  = rawMath "\\left"  <> (MathItem $ RawMath <$> parenChar m1)
mright m1 = rawMath "\\right" <> (MathItem $ RawMath <$> parenChar m1)

between :: Char -> Char -> MathItem -> MathItem
between opening closing m1 = mleft opening <> m1 <> mright closing

parens, braces, brackets :: MathItem -> MathItem
parens   = between '(' ')'
braces   = between '{' '}'
brackets = between '[' ']'

parenChar :: Char -> LatexM String
parenChar m1 | m1 `elem` "([.])" = return [m1]
             | m1 == '{'         = return "\\{"
             | m1 == '}'         = return "\\}"
             | otherwise         = throwError $ "invalid parenthesis-like: " ++ show m1

text :: LatexItem -> MathItem
text arg = mathToLR "text" [B.packageDependency amsmath, B.mandatory arg]

array :: [RowSpec MathItem] -> [Row MathItem] -> MathItem
array spec items = MathItem $ B.tabularLike MathArray (map (fmap mathItmM) spec)
                                                      (map (fmap mathItmM) items)

{- This chunk was extracted from Language.LaTeX.Builder -}
lbrace :: MathItem
lbrace = mathCmd "{"
rbrace :: MathItem
rbrace = mathCmd "}"
space :: MathItem
space = mathCmd " "
at :: MathItem
at = mathCmd "@"
in_ :: MathItem
in_ = mathCmd "in"
forall_ :: MathItem
forall_ = mathCmd "forall"
thinspace :: MathItem
thinspace = mathCmd ","
negthinspace :: MathItem
negthinspace = mathCmd "!"

-- \: or \> in LaTeX
mediumspace :: MathItem
mediumspace = mathCmd ":"
thickspace :: MathItem
thickspace = mathCmd ";"
msup :: MathItem
msup = mathCmd "sup"
alpha :: MathItem
alpha = mathCmd "alpha"
beta :: MathItem
beta = mathCmd "beta"
chi :: MathItem
chi = mathCmd "chi"
-- Chi? (don't forget the charToMath table)
delta :: MathItem
delta = mathCmd "delta"
_Delta :: MathItem
_Delta = mathCmd "Delta"
epsilon :: MathItem
epsilon = mathCmd "epsilon"
varepsilon :: MathItem
varepsilon = mathCmd "varepsilon"
eta :: MathItem
eta = mathCmd "eta"
gamma :: MathItem
gamma = mathCmd "gamma"
_Gamma :: MathItem
_Gamma = mathCmd "Gamma"
iota :: MathItem
iota = mathCmd "iota"
kappa :: MathItem
kappa = mathCmd "kappa"
lambda :: MathItem
lambda = mathCmd "lambda"
_Lambda :: MathItem
_Lambda = mathCmd "Lambda"
mu :: MathItem
mu = mathCmd "mu"
nu :: MathItem
nu = mathCmd "nu"
omega :: MathItem
omega = mathCmd "omega"
_Omega :: MathItem
_Omega = mathCmd "Omega"
-- | In LaTeX math mode omicron is just 'o',
--   it is exported as omicron here as well just
--   for convinience.
omicron :: MathItem
omicron = mathCmd "o"
phi :: MathItem
phi = mathCmd "phi"
varphi :: MathItem
varphi = mathCmd "varphi"
_Phi :: MathItem
_Phi = mathCmd "Phi"
pi :: MathItem
pi = mathCmd "pi"
_Pi :: MathItem
_Pi = mathCmd "Pi"
{'fraction_non_alphanumeric': 0.13234497698348227, 'fraction_numerical': 0.004603303547251557, 'mean_word_length': 3.955719557195572, 'pattern_counts': {'":': 2, '<': 25, '<?xml version=': 0, '>': 117, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6789802', 'n_tokens_mistral': 5332, 'n_tokens_neox': 5165, 'n_words': 1649}