Skip to content

Commit

Permalink
Clean up (and fix) the context matching function
Browse files Browse the repository at this point in the history
Co-authored-by: Nikolai Kudasov <[email protected]>
  • Loading branch information
aabounegm and fizruk committed Feb 7, 2024
1 parent 0b78d54 commit 4d68a2f
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions eo-phi-normalizer/src/Language/EO/Phi/Rules/Yaml.hs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ parseRuleSetFromFile = Yaml.decodeFileThrow
convertRule :: Rule -> Common.Rule
convertRule Rule{..} ctx obj =
[ obj'
| contextSubsts <- matchContext ctx obj context
| contextSubsts <- matchContext ctx context
, let pattern' = applySubst contextSubsts pattern
, let result' = applySubst contextSubsts result
, subst <- matchObject pattern' obj
Expand All @@ -90,13 +90,12 @@ convertRule Rule{..} ctx obj =
, not (objectHasMetavars obj')
]

matchContext :: Common.Context -> Object -> Maybe RuleContext -> [Subst]
matchContext Common.Context{..} obj = \case
Nothing -> [emptySubst]
Just (RuleContext Nothing Nothing) -> [emptySubst]
Just (RuleContext (Just pattern) Nothing) -> matchObject pattern globalObject
Just (RuleContext Nothing (Just pattern)) -> matchObject pattern thisObject
Just (RuleContext (Just globalPattern) (Just thisPattern)) -> matchObject globalPattern globalObject ++ matchObject thisPattern thisObject
matchContext :: Common.Context -> Maybe RuleContext -> [Subst]
matchContext Common.Context{} Nothing = [emptySubst]
matchContext Common.Context{..} (Just (RuleContext p1 p2)) = do
subst1 <- maybe [emptySubst] (`matchObject` globalObject) p1
subst2 <- maybe [emptySubst] ((`matchObject` thisObject) . applySubst subst1) p2
return (subst1 <> subst2)
where
globalObject = NonEmpty.last outerFormations
thisObject = NonEmpty.head outerFormations
Expand Down

0 comments on commit 4d68a2f

Please sign in to comment.