From dc71f63b9398b5a1fddb069e1732bd11661bc230 Mon Sep 17 00:00:00 2001 From: Danila Danko Date: Thu, 28 Nov 2024 20:24:38 +0300 Subject: [PATCH 1/2] fix(eo-phi-normalizer): allow optional "when" and "tests" in the rules file --- eo-phi-normalizer/src/Language/EO/Phi/Rules/Yaml.hs | 6 +++--- eo-phi-normalizer/src/Language/EO/Phi/ToLaTeX.hs | 6 +++--- eo-phi-normalizer/src/Language/EO/Test/YamlSpec.hs | 4 +++- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/eo-phi-normalizer/src/Language/EO/Phi/Rules/Yaml.hs b/eo-phi-normalizer/src/Language/EO/Phi/Rules/Yaml.hs index abd8d17fa..7559ee3c2 100644 --- a/eo-phi-normalizer/src/Language/EO/Phi/Rules/Yaml.hs +++ b/eo-phi-normalizer/src/Language/EO/Phi/Rules/Yaml.hs @@ -101,8 +101,8 @@ data Rule = Rule , pattern :: Object , result :: Object , fresh :: Maybe [FreshMetaId] - , when :: [Condition] - , tests :: [RuleTest] + , when :: Maybe [Condition] + , tests :: Maybe [RuleTest] } deriving (Generic, FromJSON, Show) @@ -183,7 +183,7 @@ convertRule Rule{..} ctx obj = do let pattern' = applySubst contextSubsts pattern result' = applySubst contextSubsts result subst <- matchObject pattern' obj - guard $ all (\cond -> checkCond ctx cond (contextSubsts <> subst)) when + guard $ all (\cond -> checkCond ctx cond (contextSubsts <> subst)) (fromMaybe [] when) let substFresh = mkFreshSubst ctx result' fresh result'' = applySubst (contextSubsts <> subst <> substFresh) result' -- TODO #152:30m what context should we pass to evaluate meta funcs? diff --git a/eo-phi-normalizer/src/Language/EO/Phi/ToLaTeX.hs b/eo-phi-normalizer/src/Language/EO/Phi/ToLaTeX.hs index d12b6aba1..c6f45d557 100644 --- a/eo-phi-normalizer/src/Language/EO/Phi/ToLaTeX.hs +++ b/eo-phi-normalizer/src/Language/EO/Phi/ToLaTeX.hs @@ -119,7 +119,7 @@ instance ToLatex RuleAttribute where instance ToLatex Condition where toLatex (IsNF nf) = inMathMode $ toLatex nf <> "\\in\\mathcal{N}" toLatex (IsNFInsideFormation nf_inside_formation) = - (inMathMode $ toLatex nf_inside_formation) <> " is nf inside formation" + inMathMode (toLatex nf_inside_formation) <> " is nf inside formation" toLatex (PresentAttrs (AttrsInBindings attrs bindings)) = inMathMode $ fold (intersperse ", " (map toLatex attrs)) <> " \\in " <> foldMap toLatex bindings toLatex (AbsentAttrs (AttrsInBindings attrs bindings)) = @@ -147,7 +147,7 @@ instance ToLatex Rule where <> inMathMode (toLatex result) <> (if not (null when) || isNonEmptyContext context then "\\\\\\text {if }" else mempty) <> maybe mempty (\c -> "&" <> toLatex c <> "\\\\") context - <> fold (intersperse ",\\\\" (map (("&" <>) . toLatex) when)) + <> fold (intersperse ",\\\\" (maybe [] (map (("&" <>) . toLatex)) when)) instance ToLatex [Rule] where toLatex rules = @@ -166,7 +166,7 @@ ruleToLatexCompact (Rule name _ context _ pattern result _ when _) = <> inMathMode (toLatex result) <> (if not (null when) || isNonEmptyContext context then "\\quad\\text {if }" else "") <> maybe mempty (\c -> toLatex c <> ", ") context - <> fold (intersperse ", " (map toLatex when)) + <> fold (intersperse ", " (maybe [] (map toLatex) when)) rulesToLatexCompact :: [Rule] -> LaTeX rulesToLatexCompact rules = diff --git a/eo-phi-normalizer/src/Language/EO/Test/YamlSpec.hs b/eo-phi-normalizer/src/Language/EO/Test/YamlSpec.hs index afe72e2ca..13e6722a1 100644 --- a/eo-phi-normalizer/src/Language/EO/Test/YamlSpec.hs +++ b/eo-phi-normalizer/src/Language/EO/Test/YamlSpec.hs @@ -27,6 +27,7 @@ module Language.EO.Test.YamlSpec where import Control.Monad (forM_) +import Data.Maybe (fromMaybe) import Language.EO.Phi.Dataize.Context (defaultContext) import Language.EO.Phi.Rules.Common (applyOneRule) import Language.EO.Phi.Rules.Yaml (Rule (..), RuleSet (..), RuleTest (..), RuleTestOption (..), convertRuleNamed) @@ -40,7 +41,8 @@ spec testPaths = describe "User-defined rules unit tests" do describe ruleset.title do forM_ ruleset.rules $ \rule -> do describe rule.name do - forM_ rule.tests $ \ruleTest -> do + let tests' = fromMaybe [] rule.tests + forM_ tests' $ \ruleTest -> do it ruleTest.name $ let rule' = convertRuleNamed rule resultOneStep = applyOneRule (defaultContext [rule'] ruleTest.input) ruleTest.input From 8bf83625d52379fb50b25d91dcde627bfd26fa90 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Thu, 28 Nov 2024 17:34:44 +0000 Subject: [PATCH 2/2] Update Markdown files --- site/docs/src/eo-phi-normalizer/print-rules.md | 16 ++++++++-------- site/docs/src/eo-phi-normalizer/test.md | 4 ++-- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/site/docs/src/eo-phi-normalizer/print-rules.md b/site/docs/src/eo-phi-normalizer/print-rules.md index a387c1068..c4ee4d1be 100644 --- a/site/docs/src/eo-phi-normalizer/print-rules.md +++ b/site/docs/src/eo-phi-normalizer/print-rules.md @@ -43,12 +43,12 @@ eo-phi-normalizer print-rules --tex \rrule{COPY1}: & $ [[ \tau -> ?, B ]]( 0-> b1 ) $ \(\trans\) $ [[ \tau -> \mathbb{S}(b1, b2), B ]] $ \\\text {if }& $ b2 $ is the scope of the redex\\¬ in subformations,\\& $ b1\in\mathcal{N} $ \\\\ \rrule{COPY2}: & $ [[ \tau1 -> ?, \tau2 -> ?, B ]]( 0-> b1, 1-> b2 ) $ \(\trans\) $ [[ \tau1 -> \mathbb{S}(b1, b3), \tau2 -> \mathbb{S}(b2, b3), B ]] $ \\\text {if }& $ b3 $ is the scope of the redex\\¬ in subformations,\\& $ b1\in\mathcal{N} $ ,\\& $ b2\in\mathcal{N} $ \\\\ \rrule{COPYdelta}: & $ [[ D> ?, B ]]( D> y ) $ \(\trans\) $ [[ D> y, B ]] $ \\\text {if }¬ in subformations\\\\ - \rrule{EMPTY}: & $ [[ B1 ]]( ) $ \(\trans\) $ [[ B1 ]] $ \\\\ - \rrule{OVER}: & $ [[ \tau -> b1, B1 ]]( \tau -> b2, B2 ) $ \(\trans\) $ \dead $ \\\\ + \rrule{EMPTY}: & $ [[ B1 ]]( ) $ \(\trans\) $ [[ B1 ]] $ \\\text {if }\\\\ + \rrule{OVER}: & $ [[ \tau -> b1, B1 ]]( \tau -> b2, B2 ) $ \(\trans\) $ \dead $ \\\text {if }\\\\ \rrule{STOP}: & $ [[ B ]].\tau $ \(\trans\) $ \dead $ \\\text {if }& $ \tau, @, \lambda \notin B $ ,\\& $ ^ \in B $ ,\\& $ [[ B ]]\in\mathcal{N} $ \\\\ \rrule{MISS}: & $ [[ B1 ]]( \tau -> b, B2 ) $ \(\trans\) $ \dead $ \\\text {if }& $ \tau, @, \lambda \notin B1 $ \\\\ - \rrule{DD}: & $ \dead.\tau $ \(\trans\) $ \dead $ \\\\ - \rrule{DC}: & $ \dead( B ) $ \(\trans\) $ \dead $ + \rrule{DD}: & $ \dead.\tau $ \(\trans\) $ \dead $ \\\text {if }\\\\ + \rrule{DC}: & $ \dead( B ) $ \(\trans\) $ \dead $ \\\text {if } \end{tabular} \end{figure*} ``` @@ -74,11 +74,11 @@ eo-phi-normalizer print-rules --tex --compact \rrule{COPY1}: $ [[ \tau -> ?, B ]]( 0-> b1 ) $ \(\trans\) $ [[ \tau -> \mathbb{S}(b1, b2), B ]] $ \quad\text {if } $ b2 $ is the scope of the redex, not in subformations, $ b1\in\mathcal{N} $ \\\vspace*{0.5em} \rrule{COPY2}: $ [[ \tau1 -> ?, \tau2 -> ?, B ]]( 0-> b1, 1-> b2 ) $ \(\trans\) $ [[ \tau1 -> \mathbb{S}(b1, b3), \tau2 -> \mathbb{S}(b2, b3), B ]] $ \quad\text {if } $ b3 $ is the scope of the redex, not in subformations, $ b1\in\mathcal{N} $ , $ b2\in\mathcal{N} $ \\\vspace*{0.5em} \rrule{COPYdelta}: $ [[ D> ?, B ]]( D> y ) $ \(\trans\) $ [[ D> y, B ]] $ \quad\text {if }not in subformations\\\vspace*{0.5em} - \rrule{EMPTY}: $ [[ B1 ]]( ) $ \(\trans\) $ [[ B1 ]] $ \\\vspace*{0.5em} - \rrule{OVER}: $ [[ \tau -> b1, B1 ]]( \tau -> b2, B2 ) $ \(\trans\) $ \dead $ \\\vspace*{0.5em} + \rrule{EMPTY}: $ [[ B1 ]]( ) $ \(\trans\) $ [[ B1 ]] $ \quad\text {if }\\\vspace*{0.5em} + \rrule{OVER}: $ [[ \tau -> b1, B1 ]]( \tau -> b2, B2 ) $ \(\trans\) $ \dead $ \quad\text {if }\\\vspace*{0.5em} \rrule{STOP}: $ [[ B ]].\tau $ \(\trans\) $ \dead $ \quad\text {if } $ \tau, @, \lambda \notin B $ , $ ^ \in B $ , $ [[ B ]]\in\mathcal{N} $ \\\vspace*{0.5em} \rrule{MISS}: $ [[ B1 ]]( \tau -> b, B2 ) $ \(\trans\) $ \dead $ \quad\text {if } $ \tau, @, \lambda \notin B1 $ \\\vspace*{0.5em} - \rrule{DD}: $ \dead.\tau $ \(\trans\) $ \dead $ \\\vspace*{0.5em} - \rrule{DC}: $ \dead( B ) $ \(\trans\) $ \dead $ + \rrule{DD}: $ \dead.\tau $ \(\trans\) $ \dead $ \quad\text {if }\\\vspace*{0.5em} + \rrule{DC}: $ \dead( B ) $ \(\trans\) $ \dead $ \quad\text {if } \end{figure*} ``` diff --git a/site/docs/src/eo-phi-normalizer/test.md b/site/docs/src/eo-phi-normalizer/test.md index f8611c653..161c83a5c 100644 --- a/site/docs/src/eo-phi-normalizer/test.md +++ b/site/docs/src/eo-phi-normalizer/test.md @@ -61,11 +61,11 @@ User-defined rules unit tests Should match in subformation [✔] Should work with empty formation [✔] OVER - Language.EO.Test.YamlSpec[44:13] [✔] + Language.EO.Test.YamlSpec[46:13] [✔] STOP Accessing nonexistent attribute [✔] MISS - Language.EO.Test.YamlSpec[44:13] [✔] + Language.EO.Test.YamlSpec[46:13] [✔] Should not match if attr is present [✔] Should not match if phi is present [✔] Should apply in subformations [✔]