2023-11-26 14:34:52 -08:00
|
|
|
module Bergamot.Latex exposing (..)
|
|
|
|
|
2023-12-01 23:31:43 -08:00
|
|
|
import Bergamot.Syntax as Syntax exposing (..)
|
|
|
|
import Bergamot.Rules as Rules exposing (..)
|
2023-12-01 16:38:46 -08:00
|
|
|
import Bergamot.Utils exposing (..)
|
2023-12-01 16:25:39 -08:00
|
|
|
|
2023-12-01 23:31:43 -08:00
|
|
|
import Debug
|
|
|
|
|
|
|
|
type Void = Void Void
|
|
|
|
|
|
|
|
absurd : Void -> a
|
|
|
|
absurd (Void v) = absurd v
|
|
|
|
|
|
|
|
renderTermViaRules : RuleEnv -> Term Void -> Maybe String
|
|
|
|
renderTermViaRules env t =
|
|
|
|
Call "latex" [Syntax.map absurd t, Var (MkMetavariable "s")]
|
|
|
|
|> prove
|
|
|
|
|> Rules.andThen reifyProofTree
|
|
|
|
|> single env
|
|
|
|
|> Maybe.andThen (\(MkProofTree node) ->
|
|
|
|
case node.conclusion of
|
|
|
|
Call "latex" [_, StringLit s] -> Just s
|
|
|
|
_ -> Nothing)
|
|
|
|
|
|
|
|
renderRuleViaRules : RuleEnv -> Rule -> Maybe String
|
|
|
|
renderRuleViaRules env r =
|
|
|
|
let
|
|
|
|
quotedPrems = List.map (Syntax.andThen quoteMetavariable) r.premises
|
|
|
|
quotedConc = Syntax.andThen quoteMetavariable r.conclusion
|
|
|
|
buildStr conc prems = String.concat
|
|
|
|
[ "\\cfrac{"
|
|
|
|
, String.join "\\quad " prems
|
|
|
|
, "}{"
|
|
|
|
, conc
|
|
|
|
, "}\\ \\texttt{"
|
|
|
|
, r.name
|
|
|
|
, "}"
|
|
|
|
]
|
|
|
|
in
|
|
|
|
renderTermViaRules env quotedConc
|
|
|
|
|> Maybe.andThen (\conc ->
|
|
|
|
sequenceMaybes (List.map (renderTermViaRules env) quotedPrems)
|
|
|
|
|> Maybe.map (\prems -> buildStr conc prems))
|
|
|
|
|
|
|
|
quoteMetavariable : Metavariable -> Term Void
|
|
|
|
quoteMetavariable (MkMetavariable s) = Call "metavariable" [StringLit <| String.dropLeft 1 s]
|
|
|
|
|
2023-11-26 14:34:52 -08:00
|
|
|
termToLatex : (a -> String) -> Term a -> String
|
|
|
|
termToLatex f t =
|
|
|
|
case t of
|
|
|
|
Call "intlit" [t1] -> termToLatex f t1
|
2023-11-26 16:32:27 -08:00
|
|
|
Call "strlit" [t1] -> termToLatex f t1
|
2023-11-26 20:44:27 -08:00
|
|
|
Call "var" [t1] -> termToLatex f t1
|
2023-11-26 14:34:52 -08:00
|
|
|
Call "plus" [t1, t2] -> termToLatex f t1 ++ " + " ++ termToLatex f t2
|
|
|
|
Call "pair" [t1, t2] -> "(" ++ termToLatex f t1 ++ ", " ++ termToLatex f t2 ++ ")"
|
2023-11-26 20:44:27 -08:00
|
|
|
Call "abs" [t1, t2, t3] -> "\\lambda " ++ termToLatex f t1 ++ " : " ++ termToLatex f t2 ++ ".\\ " ++ termToLatex f t3
|
|
|
|
Call "app" [t1, t2] -> termToLatex f t1 ++ "\\ " ++ termToLatex f t2
|
|
|
|
Call "type" [t1, t2] -> termToLatex f t1 ++ " : " ++ termToLatex f t2
|
2023-11-26 20:48:48 -08:00
|
|
|
Call "type" [t1, t2, t3] -> termToLatex f t1 ++ "\\vdash " ++ termToLatex f t2 ++ " : " ++ termToLatex f t3
|
2023-11-26 20:44:27 -08:00
|
|
|
Call "tpair" [t1, t2] -> termToLatex f t1 ++ "\\times" ++ termToLatex f t2
|
|
|
|
Call "tarr" [t1, t2] -> termToLatex f t1 ++ "\\to" ++ termToLatex f t2
|
|
|
|
Call "extend" [t1, t2, t3] -> termToLatex f t1 ++ ",\\ " ++ termToLatex f t2 ++ " : " ++ termToLatex f t3
|
|
|
|
Call "inenv" [t1, t2, t3] -> termToLatex f t1 ++ " : " ++ termToLatex f t2 ++ " \\in " ++ termToLatex f t3
|
|
|
|
Call "empty" [] -> "\\varnothing"
|
2023-11-26 14:34:52 -08:00
|
|
|
Call s [] -> "\\text{" ++ s ++ "}"
|
|
|
|
Call s ts -> "\\text{" ++ s ++ "}(" ++ String.join "," (List.map (termToLatex f) ts) ++ ")"
|
|
|
|
Var x -> f x
|
|
|
|
IntLit i -> String.fromInt i
|
2023-12-01 16:25:39 -08:00
|
|
|
StringLit s -> "\\texttt{" ++ "\"" ++ encodeLatex (encodeStr s) ++ "\"" ++ "}"
|
2023-11-26 14:34:52 -08:00
|
|
|
|
|
|
|
metavariableToLatex : Metavariable -> String
|
|
|
|
metavariableToLatex (MkMetavariable s) =
|
|
|
|
let
|
|
|
|
noQuestion = String.dropLeft 1 s
|
|
|
|
in
|
2023-11-26 20:44:27 -08:00
|
|
|
if String.startsWith "tau" noQuestion then "\\" ++ noQuestion else
|
|
|
|
if String.startsWith "Gamma" noQuestion then "\\" ++ noQuestion else noQuestion
|
2023-11-26 14:34:52 -08:00
|
|
|
|
|
|
|
ruleToLatex : Rule -> String
|
|
|
|
ruleToLatex r = "\\cfrac{" ++ String.join "\\quad " (List.map (termToLatex metavariableToLatex) r.premises) ++ "}{" ++ termToLatex metavariableToLatex r.conclusion ++ "}\\ \\texttt{" ++ r.name ++ "}"
|
|
|
|
|
|
|
|
unificationVarToLatex : UnificationVar -> String
|
|
|
|
unificationVarToLatex (MkUnificationVar s) = s
|
|
|
|
|
|
|
|
proofTreeToLatex : ProofTree -> String
|
2023-11-26 15:54:01 -08:00
|
|
|
proofTreeToLatex (MkProofTree node) = "\\cfrac{" ++ String.join "\\quad \\quad " (List.map proofTreeToLatex node.premises) ++ "}{" ++ termToLatex unificationVarToLatex node.conclusion ++ "}\\ \\texttt{" ++ node.name ++ "}"
|