From f44b51a83906faf00f68d077f2ec85ad655d7a64 Mon Sep 17 00:00:00 2001 From: Danila Danko Date: Fri, 9 Feb 2024 12:24:37 +0300 Subject: [PATCH 1/2] feat: add `single` option that prints a single normalized program --- eo-phi-normalizer/app/Main.hs | 36 ++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/eo-phi-normalizer/app/Main.hs b/eo-phi-normalizer/app/Main.hs index 8e179ce62..6f8090935 100644 --- a/eo-phi-normalizer/app/Main.hs +++ b/eo-phi-normalizer/app/Main.hs @@ -8,13 +8,13 @@ module Main where -import Control.Monad (when) +import Control.Monad (unless, when) import Data.Foldable (forM_) import Data.List (nub) import Language.EO.Phi (Object (Formation), Program (Program), defaultMain, parseProgram, printTree) import Language.EO.Phi.Rules.Common (Context (..), applyRules, applyRulesChain) -import Language.EO.Phi.Rules.Yaml +import Language.EO.Phi.Rules.Yaml (RuleSet (rules, title), convertRule, parseRuleSetFromFile) import Options.Generic import System.IO (IOMode (WriteMode), hClose, hPutStr, hPutStrLn, openFile, stdout) @@ -22,6 +22,7 @@ data CLINamedParams = CLINamedParams { chain :: Bool , rulesYaml :: Maybe String , outPath :: Maybe String + , single :: Bool } deriving (Generic, Show, ParseRecord, Read, ParseField) @@ -31,6 +32,7 @@ instance ParseFields CLINamedParams where <$> parseFields (Just "Print out steps of reduction") (Just "chain") (Just 'c') Nothing <*> parseFields (Just "Path to the Yaml file with custom rules") (Just "rules-yaml") Nothing Nothing <*> parseFields (Just "Output file path (defaults to stdout)") (Just "output") (Just 'o') Nothing + <*> parseFields (Just "Print a single normlized expression") (Just "single") (Just 's') Nothing data CLIOptions = CLIOptions CLINamedParams (Maybe FilePath) deriving (Generic, Show, ParseRecord) @@ -46,7 +48,7 @@ main = do let logStr = hPutStr handle let logStrLn = hPutStrLn handle ruleSet <- parseRuleSetFromFile path - logStrLn ruleSet.title + unless single $ logStrLn ruleSet.title src <- maybe getContents readFile inPath let progOrError = parseProgram src case progOrError of @@ -57,18 +59,22 @@ main = do | otherwise = pure <$> applyRules (Context (convertRule <$> ruleSet.rules) [Formation bindings]) (Formation bindings) uniqueResults = nub results totalResults = length uniqueResults - logStrLn "Input:" - logStrLn (printTree input) - logStrLn "====================================================" - forM_ (zip [1 ..] uniqueResults) $ \(i, steps) -> do - logStrLn $ - "Result " <> show i <> " out of " <> show totalResults <> ":" - let n = length steps - forM_ (zip [1 ..] steps) $ \(k, step) -> do - Control.Monad.when chain $ - logStr ("[ " <> show k <> " / " <> show n <> " ]") - logStrLn (printTree step) - logStrLn "----------------------------------------------------" + when (totalResults == 0) $ error "Could not normalize the program" + if single + then logStrLn (printTree (head uniqueResults)) + else do + logStrLn "Input:" + logStrLn (printTree input) + logStrLn "====================================================" + forM_ (zip [1 ..] uniqueResults) $ \(i, steps) -> do + logStrLn $ + "Result " <> show i <> " out of " <> show totalResults <> ":" + let n = length steps + forM_ (zip [1 ..] steps) $ \(k, step) -> do + when chain $ + logStr ("[ " <> show k <> " / " <> show n <> " ]") + logStrLn (printTree step) + logStrLn "----------------------------------------------------" hClose handle -- TODO #48:15m still need to consider `chain` (should rewrite/change defaultMain to mainWithOptions) Nothing -> defaultMain From 3ad0df331befa745d8c7e3f79228226b87e1298e Mon Sep 17 00:00:00 2001 From: Danila Danko Date: Fri, 9 Feb 2024 12:30:17 +0300 Subject: [PATCH 2/2] feat: document the `single` option in readme --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index 4c95cca6b..68ad4d6ab 100644 --- a/README.md +++ b/README.md @@ -139,6 +139,18 @@ Result 1 out of 1: ---------------------------------------------------- ``` +#### `--single` + +Use `--single` to print a single normalized program. + +```sh +# Command +stack run -- --single --rules-yaml ./eo-phi-normalizer/test/eo/phi/rules/yegor.yaml test.phi + +# Output +⟦ a ↦ ξ.b (c ↦ ⟦ ⟧).d (ρ ↦ ⟦ b ↦ ⟦ c ↦ ∅, d ↦ ⟦ φ ↦ ξ.ρ.c ⟧ ⟧ ⟧) ⟧ +``` + ## Rulesets A ruleset describes a set of user-defined rewriting rules.