Skip to content

Commit

Permalink
Implement substitution for meta function
Browse files Browse the repository at this point in the history
  • Loading branch information
aabounegm committed Feb 8, 2024
1 parent a3acf39 commit c0e5ecc
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions eo-phi-normalizer/src/Language/EO/Phi/Rules/Yaml.hs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ data Subst = Subst
{ objectMetas :: [(MetaId, Object)]
, bindingsMetas :: [(MetaId, [Binding])]
, attributeMetas :: [(MetaId, Attribute)]
, functionMetas :: [(MetaFunctionName, Object)]
}
deriving (Show)

Expand All @@ -183,7 +184,7 @@ instance Monoid Subst where
mempty = emptySubst

emptySubst :: Subst
emptySubst = Subst [] [] []
emptySubst = Subst [] [] [] []

-- >>> putStrLn $ Language.EO.Phi.printTree (applySubst (Subst [("!n", "⟦ c ↦ ⟦ ⟧ ⟧")] [("!B", ["b ↦ ⟦ ⟧"])] [("!a", "a")]) "!n(ρ ↦ ⟦ !B ⟧)" :: Object)
-- ⟦ c ↦ ⟦ ⟧ ⟧ (ρ ↦ ⟦ b ↦ ⟦ ⟧ ⟧)
Expand All @@ -198,6 +199,7 @@ applySubst subst@Subst{..} = \case
GlobalObject -> GlobalObject
ThisObject -> ThisObject
obj@(MetaObject x) -> fromMaybe obj $ lookup x objectMetas
obj@(MetaFunction name _) -> fromMaybe obj $ lookup name functionMetas
obj -> obj

applySubstAttr :: Subst -> Attribute -> Attribute
Expand All @@ -219,8 +221,8 @@ applySubstBinding subst@Subst{..} = \case
b@(MetaBindings m) -> fromMaybe [b] (lookup m bindingsMetas)

mergeSubst :: Subst -> Subst -> Subst
mergeSubst (Subst xs ys zs) (Subst xs' ys' zs') =
Subst (xs ++ xs') (ys ++ ys') (zs ++ zs')
mergeSubst (Subst xs ys zs fs) (Subst xs' ys' zs' fs') =
Subst (xs ++ xs') (ys ++ ys') (zs ++ zs') (fs ++ fs')

-- 1. need to implement applySubst' :: Subst -> Object -> Object
-- 2. complete the code
Expand All @@ -240,7 +242,9 @@ matchObject (MetaObject m) obj =
{ objectMetas = [(m, obj)]
, bindingsMetas = []
, attributeMetas = []
, functionMetas = []
}
matchObject (MetaFunction name@(MetaFunctionName "@T") obj) _ = [Subst [] [] [] [(name, Common.nuCountAsDataObj obj)]]
matchObject _ _ = [] -- ? emptySubst ?

matchBindings :: [Binding] -> [Binding] -> [Subst]
Expand All @@ -251,6 +255,7 @@ matchBindings [MetaBindings b] bindings =
{ objectMetas = []
, bindingsMetas = [(b, bindings)]
, attributeMetas = []
, functionMetas = []
}
matchBindings (p : ps) bs = do
(bs', subst1) <- matchFindBinding p bs
Expand Down Expand Up @@ -291,6 +296,7 @@ matchAttr (MetaAttr metaId) attr =
{ objectMetas = []
, bindingsMetas = []
, attributeMetas = [(metaId, attr)]
, functionMetas = []
}
]
matchAttr _ _ = []

0 comments on commit c0e5ecc

Please sign in to comment.