Skip to content

Commit

Permalink
Speed up the use of builtin version
Browse files Browse the repository at this point in the history
  • Loading branch information
fizruk committed May 31, 2024
1 parent c715538 commit c62d9d0
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 15 deletions.
18 changes: 10 additions & 8 deletions eo-phi-normalizer/app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import Language.EO.Phi.Report.Data (Report'InputConfig (..), Report'OutputConfig
import Language.EO.Phi.Report.Html (ReportFormat (..), reportCSS, reportJS, toStringReport)
import Language.EO.Phi.Report.Html qualified as ReportHtml (ReportConfig (..))
import Language.EO.Phi.Rules.Common
import Language.EO.Phi.Rules.Fast (fastYegorInsideOutAsRule)
import Language.EO.Phi.Rules.Fast (fastYegorInsideOut, fastYegorInsideOutAsRule)
import Language.EO.Phi.Rules.Yaml (RuleSet (rules, title), convertRuleNamed, parseRuleSetFromFile)
import Language.EO.Phi.ToLaTeX
import Main.Utf8
Expand Down Expand Up @@ -387,12 +387,12 @@ main = withUtf8 do
deps <- mapM (getProgram . Just) dependencies
(logStrLn, logStr) <- getLoggers outputFile
-- logStrLn "Running transform"
(ruleSetTitle, rules) <-
(builtin, ruleSetTitle, rules) <-
case rulesPath of
Just path -> do
ruleSet <- parseRuleSetFromFile path
return (ruleSet.title, convertRuleNamed <$> ruleSet.rules)
Nothing -> return ("Yegor's rules (builtin)", [fastYegorInsideOutAsRule])
return (False, ruleSet.title, convertRuleNamed <$> ruleSet.rules)
Nothing -> return (True, "Yegor's rules (builtin)", [fastYegorInsideOutAsRule])
unless (single || json) $ logStrLn ruleSetTitle
bindingsWithDeps <- case deepMergePrograms (program' : deps) of
Left err -> throw (CouldNotMergeDependencies err)
Expand All @@ -401,10 +401,11 @@ main = withUtf8 do
uniqueResults
-- Something here seems incorrect
| chain = map fst $ applyRulesChainWith' limits ctx (Formation bindings)
| builtin = return [LogEntry "" (fastYegorInsideOut ctx (Formation bindings)) 0]
| otherwise = (\x -> [LogEntry "" x 0]) <$> applyRulesWith limits ctx (Formation bindings)
where
limits = ApplicationLimits maxDepth (maxGrowthFactor * objectSize (Formation bindings))
ctx = defaultContext rules (Formation bindingsWithDeps) -- IMPORTANT: context contains dependencies!
ctx = (defaultContext rules (Formation bindingsWithDeps)){builtinRules = builtin} -- IMPORTANT: context contains dependencies!
totalResults = length uniqueResults
when (null uniqueResults || null (head uniqueResults)) (throw CouldNotNormalize)
if
Expand Down Expand Up @@ -451,19 +452,20 @@ main = withUtf8 do
bindingsWithDeps <- case deepMergePrograms (program' : deps) of
Left err -> throw (CouldNotMergeDependencies err)
Right (Program bindingsWithDeps) -> return bindingsWithDeps
(_ruleSetTitle, rules) <-
(builtin, _ruleSetTitle, rules) <-
case rulesPath of
Just path -> do
ruleSet <- parseRuleSetFromFile path
return (ruleSet.title, convertRuleNamed <$> ruleSet.rules)
Nothing -> return ("Yegor's rules (builtin)", [fastYegorInsideOutAsRule])
return (False, ruleSet.title, convertRuleNamed <$> ruleSet.rules)
Nothing -> return (True, "Yegor's rules (builtin)", [fastYegorInsideOutAsRule])
let (Program bindings) = program'
let inputObject
| asPackage = Formation (injectLamdbaPackage bindings)
| otherwise = Formation bindings
let ctx =
(defaultContext rules (Formation bindingsWithDeps)) -- IMPORTANT: context contains dependencies!
{ minimizeTerms = minimizeStuckTerms
, builtinRules = builtin
}
( if chain
then do
Expand Down
8 changes: 7 additions & 1 deletion eo-phi-normalizer/src/Language/EO/Phi/Dataize.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import Data.List.NonEmpty qualified as NonEmpty
-- import Data.List.NonEmpty qualified as NonEmpty
import Data.Maybe (listToMaybe)
import Language.EO.Phi.Rules.Common
import Language.EO.Phi.Rules.Fast (fastYegorInsideOut)
import Language.EO.Phi.Rules.Yaml (substThis)
import Language.EO.Phi.Syntax.Abs
import PyF (fmt)
Expand Down Expand Up @@ -98,7 +99,12 @@ dataizeRecursivelyChain = fmap minimizeObject' . go
ctx <- getContext
let globalObject = NonEmpty.last (outerFormations ctx)
let limits = defaultApplicationLimits (objectSize globalObject)
let normalizedObj = applyRulesChainWith limits obj
let normalizedObj
| builtinRules ctx = do
let obj' = fastYegorInsideOut ctx obj
logStep "Normalized" obj'
return obj'
| otherwise = applyRulesChainWith limits obj
msplit (transformNormLogs normalizedObj) >>= \case
Nothing -> do
logStep "No rules applied" (Left obj)
Expand Down
18 changes: 12 additions & 6 deletions eo-phi-normalizer/src/Language/EO/Phi/Rules/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,16 @@ unsafeParseWith parser input =
type NamedRule = (String, Rule)

data Context = Context
{ allRules :: [NamedRule]
{ builtinRules :: Bool
, allRules :: [NamedRule]
, outerFormations :: NonEmpty Object
, currentAttr :: Attribute
, insideFormation :: Bool
-- ^ Temporary hack for applying Ksi and Phi rules when dataizing
, dataizePackage :: Bool
-- ^ Temporary flag to only dataize Package attributes for the top-level formation.
, minimizeTerms :: Bool
, insideSubObject :: Bool
}

sameContext :: Context -> Context -> Bool
Expand All @@ -76,12 +78,14 @@ sameContext ctx1 ctx2 =
defaultContext :: [NamedRule] -> Object -> Context
defaultContext rules obj =
Context
{ allRules = rules
{ builtinRules = False
, allRules = rules
, outerFormations = NonEmpty.singleton obj
, currentAttr = Phi
, insideFormation = False
, dataizePackage = True
, minimizeTerms = False
, insideSubObject = False
}

-- | A rule tries to apply a transformation to the root object, if possible.
Expand Down Expand Up @@ -112,20 +116,22 @@ withSubObject f ctx root =
f ctx root
<|> case root of
Formation bindings
| not (any isEmptyBinding bindings) -> propagateName1 Formation <$> withSubObjectBindings f ((extendContextWith root ctx){insideFormation = True}) bindings
| not (any isEmptyBinding bindings) -> propagateName1 Formation <$> withSubObjectBindings f ((extendContextWith root subctx){insideFormation = True}) bindings
| otherwise -> []
Application obj bindings ->
asum
[ propagateName2 Application <$> withSubObject f ctx obj <*> pure bindings
, propagateName1 (Application obj) <$> withSubObjectBindings f ctx bindings
[ propagateName2 Application <$> withSubObject f subctx obj <*> pure bindings
, propagateName1 (Application obj) <$> withSubObjectBindings f subctx bindings
]
ObjectDispatch obj a -> propagateName2 ObjectDispatch <$> withSubObject f ctx obj <*> pure a
ObjectDispatch obj a -> propagateName2 ObjectDispatch <$> withSubObject f subctx obj <*> pure a
GlobalObject{} -> []
ThisObject{} -> []
Termination -> []
MetaObject _ -> []
MetaFunction _ _ -> []
MetaSubstThis _ _ -> []
where
subctx = ctx{insideSubObject = True}

-- | Given a unary function that operates only on plain objects,
-- converts it to a function that operates on named objects
Expand Down
1 change: 1 addition & 0 deletions eo-phi-normalizer/src/Language/EO/Phi/Rules/Fast.hs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ fastYegorInsideOutAsRule = ("Yegor's rules (hardcoded)", \ctx obj -> [fastYegorI

fastYegorInsideOut :: Context -> Object -> Object
fastYegorInsideOut ctx = \case
root | insideSubObject ctx -> root -- this rule is only applied at root
root@GlobalObject
| not (insideFormation ctx) ->
NonEmpty.last (outerFormations ctx)
Expand Down

0 comments on commit c62d9d0

Please sign in to comment.