Extract latex generation to a typeclass
This commit is contained in:
34
src/Language/Bergamot/Latex.purs
Normal file
34
src/Language/Bergamot/Latex.purs
Normal file
@@ -0,0 +1,34 @@
|
||||
module Language.Bergamot.Latex where
|
||||
|
||||
import Prelude
|
||||
|
||||
import Language.Bergamot.Syntax (Expr(..))
|
||||
import Language.Bergamot.Rules (Rule(..), ProofTree(..))
|
||||
import Data.Foldable (intercalate)
|
||||
import Data.List (List(..), (:))
|
||||
|
||||
class ToLatex a where
|
||||
toLatex :: a -> String
|
||||
|
||||
instance toLatexString :: ToLatex String where
|
||||
toLatex s = s
|
||||
|
||||
instance toLatexExpr :: ToLatex k => ToLatex (Expr k) where
|
||||
toLatex (Atom "type" (t1 : t2 : Nil)) = toLatex t1 <> " : " <> toLatex t2
|
||||
toLatex (Atom "int" Nil) = "\\text{int}"
|
||||
toLatex (Atom "string" Nil) = "\\text{string}"
|
||||
toLatex (Atom "prod" (t1 : t2 : Nil)) = toLatex t1 <> "\\times " <> toLatex t2
|
||||
toLatex (Atom "n" Nil) = "n"
|
||||
toLatex (Atom "s" Nil) = "s"
|
||||
toLatex (Atom "plus" (t1 : t2 : Nil)) = toLatex t1 <> " + " <> toLatex t2
|
||||
toLatex (Atom "pair" (t1 : t2 : Nil)) = "(" <> toLatex t1 <> ", " <> toLatex t2 <> ")"
|
||||
toLatex (Atom "fst" (t : Nil)) = "\\text{fst}\\ " <> toLatex t
|
||||
toLatex (Atom "snd" (t : Nil)) = "\\text{snd}\\ " <> toLatex t
|
||||
toLatex (Atom s xs) = "\\text{" <> s <> "}(" <> intercalate ", " (toLatex <$> xs) <> ")"
|
||||
toLatex (Var x) = toLatex x
|
||||
|
||||
instance toLatexRule :: ToLatex k => ToLatex (Rule k) where
|
||||
toLatex (MkRule {head, tail}) = "\\cfrac{" <> intercalate "\\quad " (toLatex <$> tail) <> "}{" <> toLatex head <> "}"
|
||||
|
||||
instance toLatexTree :: ToLatex k => ToLatex (ProofTree k) where
|
||||
toLatex (MkProofTree {claim, witnesses}) = "\\cfrac{" <> intercalate "\\quad " (toLatex <$> witnesses) <> "}{" <> toLatex claim <> "}"
|
||||
Reference in New Issue
Block a user