From 06ef44817de918e6507b3e9c448b4dbdc09d6390 Mon Sep 17 00:00:00 2001 From: eyihluyc Date: Wed, 4 Dec 2024 15:05:28 +0300 Subject: [PATCH 1/4] Make latex output complete (parseable) --- eo-phi-normalizer/app/Main.hs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/eo-phi-normalizer/app/Main.hs b/eo-phi-normalizer/app/Main.hs index cd883fde..26185bb5 100644 --- a/eo-phi-normalizer/app/Main.hs +++ b/eo-phi-normalizer/app/Main.hs @@ -597,7 +597,7 @@ main = withUtf8 do Nothing -> do ruleSet :: RuleSet <- decodeThrow $(embedFileRelative "test/eo/phi/rules/new.yaml") return (False, ruleSet.title, convertRuleNamed <$> ruleSet.rules) - unless (single || json || (chain && latex)) $ logStrLn ruleSetTitle + unless (single || json || latex) $ logStrLn ruleSetTitle bindingsWithDeps <- case deepMergePrograms (program' : deps) of Left err -> throw (CouldNotMergeDependencies err) Right (Program bindingsWithDeps) -> return bindingsWithDeps @@ -647,8 +647,19 @@ main = withUtf8 do logStrLn [fmtTrim|{linesCombined}|] logStrLn "\\end{phiquation*}" logStrLn "\n\\end{document}" - | latex -> + | latex -> do + logStrLn + [fmtTrim| + % {ruleSetTitle} + + \\documentclass{{article}} + \\usepackage{{eolang}} + \\begin{{document}} + |] + logStrLn "\\begin{phiquation*}" logStrLn . toLatexString $ logEntryLog (head (head uniqueResults)) + logStrLn "\\end{phiquation*}" + logStrLn "\n\\end{document}" | otherwise -> do logStrLn "Input:" logStrLn (printTree program') From 83f725acc07a0be6e4618ed0bf5d1f46581691ad Mon Sep 17 00:00:00 2001 From: eyihluyc Date: Wed, 4 Dec 2024 18:43:49 +0300 Subject: [PATCH 2/4] Explain multiple chains and add a trailing trans --- eo-phi-normalizer/app/Main.hs | 67 +++++++++++++---------- eo-phi-normalizer/eo-phi-normalizer.cabal | 4 ++ eo-phi-normalizer/package.yaml | 1 + 3 files changed, 44 insertions(+), 28 deletions(-) diff --git a/eo-phi-normalizer/app/Main.hs b/eo-phi-normalizer/app/Main.hs index 26185bb5..284e2148 100644 --- a/eo-phi-normalizer/app/Main.hs +++ b/eo-phi-normalizer/app/Main.hs @@ -63,6 +63,7 @@ import Data.List (intercalate, isPrefixOf) import Data.Maybe (fromMaybe) import Data.Text.Internal.Builder (toLazyText) import Data.Text.Lazy as TL (unpack) +import Data.Text.Lazy.Manipulate import Data.Version (showVersion) import Data.Yaml (decodeFileThrow, decodeThrow) import GHC.Generics (Generic) @@ -611,6 +612,23 @@ main = withUtf8 do limits = ApplicationLimits maxDepth (maxGrowthFactor * objectSize (Formation bindings)) ctx = (defaultContext rules (Formation bindingsWithDeps)){builtinRules = builtin} -- IMPORTANT: context contains dependencies! totalResults = length uniqueResults + inLatexDocument :: IO () -> IO () + inLatexDocument logContent = do + logStrLn + [fmtTrim| + % {ruleSetTitle} + + \\documentclass{{article}} + \\usepackage{{eolang}} + \\begin{{document}} + |] + logContent + logStrLn "\n\\end{document}" + inPhiEquation :: String -> IO () + inPhiEquation phiExpr = do + logStrLn "\\begin{phiquation*}" + logStrLn [fmtTrim|{phiExpr}|] + logStrLn "\\end{phiquation*}" when (null uniqueResults || null (head uniqueResults)) (throw CouldNotNormalize) if | single && json -> @@ -630,36 +648,29 @@ main = withUtf8 do { input = printTree program' , output = (logEntryToPair . fmap printAsProgramOrAsObject <$>) <$> uniqueResults } - | chain && latex -> do - logStrLn - [fmtTrim| - % {ruleSetTitle} - - \\documentclass{{article}} - \\usepackage{{eolang}} - \\begin{{document}} - |] - forM_ uniqueResults $ \steps -> do + | chain && latex -> inLatexDocument $ do + forM_ (zip [1 ..] uniqueResults) $ \(index, steps) -> do let latexLines = toLatexString (Formation bindings) : (toLatexString . (.logEntryLog) <$> steps) transitions :: [String] = ((\x -> [fmt| \\trans_{{\\rulename{{{logEntryMessage x}}}}} \n|]) <$> steps) <> ["."] - linesCombined :: String = fold $ zipWith (\latexLine transition -> [fmt|{latexLine}{transition}|]) latexLines transitions - logStrLn "\\begin{phiquation*}" - logStrLn [fmtTrim|{linesCombined}|] - logStrLn "\\end{phiquation*}" - logStrLn "\n\\end{document}" - | latex -> do - logStrLn - [fmtTrim| - % {ruleSetTitle} - - \\documentclass{{article}} - \\usepackage{{eolang}} - \\begin{{document}} - |] - logStrLn "\\begin{phiquation*}" - logStrLn . toLatexString $ logEntryLog (head (head uniqueResults)) - logStrLn "\\end{phiquation*}" - logStrLn "\n\\end{document}" + trailingTransitions :: [String] = "" : repeat [fmt| \\trans |] + linesCombined :: String = + fold $ + zipWith3 + ( \trailingTrans latexLine transition -> + [fmt|{trailingTrans}{latexLine}{transition}|] + ) + trailingTransitions + latexLines + transitions + unless (length uniqueResults == 1) $ + logStrLn $ + "\nThis is the " <> unpack (toOrdinal index) <> " possible chain of normalizing rewritings:\n" + inPhiEquation linesCombined + | latex -> + inLatexDocument $ + inPhiEquation $ + toLatexString $ + logEntryLog (head (head uniqueResults)) | otherwise -> do logStrLn "Input:" logStrLn (printTree program') diff --git a/eo-phi-normalizer/eo-phi-normalizer.cabal b/eo-phi-normalizer/eo-phi-normalizer.cabal index 5c9df3b0..e358bf15 100644 --- a/eo-phi-normalizer/eo-phi-normalizer.cabal +++ b/eo-phi-normalizer/eo-phi-normalizer.cabal @@ -269,6 +269,7 @@ library , scientific , template-haskell , text + , text-manipulate , unordered-containers , yaml default-language: Haskell2010 @@ -313,6 +314,7 @@ executable eo-phi-normalizer , scientific , template-haskell , text + , text-manipulate , unordered-containers , with-utf8 , yaml @@ -387,6 +389,7 @@ test-suite doctests , scientific , template-haskell , text + , text-manipulate , unordered-containers , yaml default-language: Haskell2010 @@ -439,6 +442,7 @@ test-suite spec , scientific , template-haskell , text + , text-manipulate , unordered-containers , with-utf8 , yaml diff --git a/eo-phi-normalizer/package.yaml b/eo-phi-normalizer/package.yaml index cf6209ac..6debe96e 100644 --- a/eo-phi-normalizer/package.yaml +++ b/eo-phi-normalizer/package.yaml @@ -76,6 +76,7 @@ dependencies: - hspec-core - text - template-haskell + - text-manipulate - blaze-html - blaze-markup - scientific From d93b09cfc5cc26437c5f30bea6c5efc3bef5626d Mon Sep 17 00:00:00 2001 From: eyihluyc Date: Wed, 4 Dec 2024 19:29:37 +0300 Subject: [PATCH 3/4] Use fmt --- eo-phi-normalizer/app/Main.hs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/eo-phi-normalizer/app/Main.hs b/eo-phi-normalizer/app/Main.hs index 284e2148..b68466e8 100644 --- a/eo-phi-normalizer/app/Main.hs +++ b/eo-phi-normalizer/app/Main.hs @@ -663,8 +663,8 @@ main = withUtf8 do latexLines transitions unless (length uniqueResults == 1) $ - logStrLn $ - "\nThis is the " <> unpack (toOrdinal index) <> " possible chain of normalizing rewritings:\n" + logStrLn + [fmt|\nThis is the {unpack (toOrdinal index)} possible chain of normalizing rewritings:\n|] inPhiEquation linesCombined | latex -> inLatexDocument $ From 13c0fc31daf397915524763cd06f5c40ca069bb4 Mon Sep 17 00:00:00 2001 From: eyihluyc Date: Wed, 4 Dec 2024 19:31:32 +0300 Subject: [PATCH 4/4] Specify import --- eo-phi-normalizer/app/Main.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eo-phi-normalizer/app/Main.hs b/eo-phi-normalizer/app/Main.hs index b68466e8..aaeadce8 100644 --- a/eo-phi-normalizer/app/Main.hs +++ b/eo-phi-normalizer/app/Main.hs @@ -63,7 +63,7 @@ import Data.List (intercalate, isPrefixOf) import Data.Maybe (fromMaybe) import Data.Text.Internal.Builder (toLazyText) import Data.Text.Lazy as TL (unpack) -import Data.Text.Lazy.Manipulate +import Data.Text.Lazy.Manipulate (toOrdinal) import Data.Version (showVersion) import Data.Yaml (decodeFileThrow, decodeThrow) import GHC.Generics (Generic)