{-# Language FlexibleInstances #-}
module Language.Drasil.TeX.Monad where
import Prelude hiding (print)
import qualified Text.PrettyPrint as TP
import Language.Drasil
import Control.Applicative hiding (empty)
import qualified Language.Drasil.Printing.Helpers as H
data MathContext = Text | Math | Curr deriving MathContext -> MathContext -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: MathContext -> MathContext -> Bool
$c/= :: MathContext -> MathContext -> Bool
== :: MathContext -> MathContext -> Bool
$c== :: MathContext -> MathContext -> Bool
Eq
newtype PrintLaTeX a = PL { forall a. PrintLaTeX a -> MathContext -> a
runPrint :: MathContext -> a }
instance Functor PrintLaTeX where
fmap :: forall a b. (a -> b) -> PrintLaTeX a -> PrintLaTeX b
fmap a -> b
f (PL MathContext -> a
ca) = forall a. (MathContext -> a) -> PrintLaTeX a
PL forall a b. (a -> b) -> a -> b
$ \MathContext
ctx -> a -> b
f (MathContext -> a
ca MathContext
ctx)
instance Applicative PrintLaTeX where
pure :: forall a. a -> PrintLaTeX a
pure = forall a. (MathContext -> a) -> PrintLaTeX a
PL forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. a -> b -> a
const
PL MathContext -> a -> b
f <*> :: forall a b. PrintLaTeX (a -> b) -> PrintLaTeX a -> PrintLaTeX b
<*> PL MathContext -> a
v = forall a. (MathContext -> a) -> PrintLaTeX a
PL forall a b. (a -> b) -> a -> b
$ \MathContext
ctx -> MathContext -> a -> b
f MathContext
ctx (MathContext -> a
v MathContext
ctx)
instance Monad PrintLaTeX where
return :: forall a. a -> PrintLaTeX a
return = forall (f :: * -> *) a. Applicative f => a -> f a
pure
PrintLaTeX a
m >>= :: forall a b. PrintLaTeX a -> (a -> PrintLaTeX b) -> PrintLaTeX b
>>= a -> PrintLaTeX b
k = forall a. (MathContext -> a) -> PrintLaTeX a
PL forall a b. (a -> b) -> a -> b
$ \MathContext
ctx ->
let a :: a
a = forall a. PrintLaTeX a -> MathContext -> a
runPrint PrintLaTeX a
m MathContext
ctx in
forall a. PrintLaTeX a -> MathContext -> a
runPrint (a -> PrintLaTeX b
k a
a) MathContext
ctx
type D = PrintLaTeX TP.Doc
switch :: (MathContext -> MathContext) -> D -> D
switch :: (MathContext -> MathContext) -> PrintLaTeX Doc -> PrintLaTeX Doc
switch MathContext -> MathContext
f (PL MathContext -> Doc
g) = forall a. (MathContext -> a) -> PrintLaTeX a
PL forall a b. (a -> b) -> a -> b
$ \MathContext
c -> MathContext -> MathContext -> (MathContext -> Doc) -> Doc
adjust MathContext
c (MathContext -> MathContext
f MathContext
c) MathContext -> Doc
g
where
bstext :: Doc
bstext = String -> Doc
TP.text String
"\\text"
br :: Doc -> Doc
br Doc
doc = String -> Doc
TP.text String
"{" Doc -> Doc -> Doc
TP.<> Doc
doc Doc -> Doc -> Doc
TP.<> String -> Doc
TP.text String
"}"
adjust :: MathContext -> MathContext -> (MathContext -> TP.Doc) -> TP.Doc
adjust :: MathContext -> MathContext -> (MathContext -> Doc) -> Doc
adjust MathContext
Math MathContext
Math MathContext -> Doc
gen = MathContext -> Doc
gen MathContext
Math
adjust MathContext
Text MathContext
Text MathContext -> Doc
gen = MathContext -> Doc
gen MathContext
Text
adjust MathContext
Math MathContext
Text MathContext -> Doc
gen = Doc
bstext Doc -> Doc -> Doc
TP.<> Doc -> Doc
br (MathContext -> Doc
gen MathContext
Text)
adjust MathContext
Text MathContext
Math MathContext -> Doc
gen = Doc -> Doc
H.dollarDoc forall a b. (a -> b) -> a -> b
$ MathContext -> Doc
gen MathContext
Math
adjust MathContext
Curr MathContext
Curr MathContext -> Doc
gen = MathContext -> Doc
gen MathContext
Text
adjust MathContext
Curr MathContext
x MathContext -> Doc
gen = MathContext -> Doc
gen MathContext
x
adjust MathContext
x MathContext
Curr MathContext -> Doc
gen = MathContext -> Doc
gen MathContext
x
toMath, toText :: D -> D
toMath :: PrintLaTeX Doc -> PrintLaTeX Doc
toMath = (MathContext -> MathContext) -> PrintLaTeX Doc -> PrintLaTeX Doc
switch (forall a b. a -> b -> a
const MathContext
Math)
toText :: PrintLaTeX Doc -> PrintLaTeX Doc
toText = (MathContext -> MathContext) -> PrintLaTeX Doc -> PrintLaTeX Doc
switch (forall a b. a -> b -> a
const MathContext
Text)
getCtx :: PrintLaTeX MathContext
getCtx :: PrintLaTeX MathContext
getCtx = forall a. (MathContext -> a) -> PrintLaTeX a
PL forall a. a -> a
id
instance Semigroup (PrintLaTeX TP.Doc) where
(PL MathContext -> Doc
s1) <> :: PrintLaTeX Doc -> PrintLaTeX Doc -> PrintLaTeX Doc
<> (PL MathContext -> Doc
s2) = forall a. (MathContext -> a) -> PrintLaTeX a
PL forall a b. (a -> b) -> a -> b
$ \MathContext
ctx -> MathContext -> Doc
s1 MathContext
ctx Doc -> Doc -> Doc
TP.<> MathContext -> Doc
s2 MathContext
ctx
instance Monoid (PrintLaTeX TP.Doc) where
mempty :: PrintLaTeX Doc
mempty = forall (f :: * -> *) a. Applicative f => a -> f a
pure Doc
TP.empty
infixl 5 %%
(%%) :: D -> D -> D
%% :: PrintLaTeX Doc -> PrintLaTeX Doc -> PrintLaTeX Doc
(%%) = forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 Doc -> Doc -> Doc
(TP.$$)
infixr 6 $+$
($+$) :: D -> D -> D
$+$ :: PrintLaTeX Doc -> PrintLaTeX Doc -> PrintLaTeX Doc
($+$) = forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 Doc -> Doc -> Doc
(TP.$+$)
tpRunPrint :: ([TP.Doc] -> TP.Doc) -> [D] -> D
tpRunPrint :: ([Doc] -> Doc) -> [PrintLaTeX Doc] -> PrintLaTeX Doc
tpRunPrint [Doc] -> Doc
f [PrintLaTeX Doc]
l = forall a. (MathContext -> a) -> PrintLaTeX a
PL forall a b. (a -> b) -> a -> b
$ \MathContext
ctx -> [Doc] -> Doc
f forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map (forall a. PrintLaTeX a -> MathContext -> a
`runPrint` MathContext
ctx) [PrintLaTeX Doc]
l
vcat :: [D] -> D
vcat :: [PrintLaTeX Doc] -> PrintLaTeX Doc
vcat = ([Doc] -> Doc) -> [PrintLaTeX Doc] -> PrintLaTeX Doc
tpRunPrint [Doc] -> Doc
TP.vcat
vpunctuate :: TP.Doc -> [D] -> D
vpunctuate :: Doc -> [PrintLaTeX Doc] -> PrintLaTeX Doc
vpunctuate Doc
x = ([Doc] -> Doc) -> [PrintLaTeX Doc] -> PrintLaTeX Doc
tpRunPrint ([Doc] -> Doc
TP.vcat forall b c a. (b -> c) -> (a -> b) -> a -> c
. Doc -> [Doc] -> [Doc]
TP.punctuate Doc
x)
hpunctuate :: TP.Doc -> [D] -> D
hpunctuate :: Doc -> [PrintLaTeX Doc] -> PrintLaTeX Doc
hpunctuate Doc
x = ([Doc] -> Doc) -> [PrintLaTeX Doc] -> PrintLaTeX Doc
tpRunPrint ([Doc] -> Doc
TP.hcat forall b c a. (b -> c) -> (a -> b) -> a -> c
. Doc -> [Doc] -> [Doc]
TP.punctuate Doc
x)
lub :: MathContext -> MathContext -> MathContext
lub :: MathContext -> MathContext -> MathContext
lub MathContext
Math MathContext
Math = MathContext
Math
lub MathContext
Text MathContext
Text = MathContext
Text
lub MathContext
Curr MathContext
Curr = MathContext
Curr
lub MathContext
Curr MathContext
x = MathContext
x
lub MathContext
x MathContext
Curr = MathContext
x
lub MathContext
_ MathContext
_ = MathContext
Text
newtype Latex = L { Latex -> String
unPL :: String }
instance RenderSpecial Latex where
special :: Special -> Latex
special Special
Circle = String -> Latex
L String
"{}^{\\circ}"