Skip to content

Commit bd6d054

Browse files
authored
Merge pull request #15 from anka-213/advanced-macros
Expand the capabilities of macros
2 parents 2da7b9b + 01ed583 commit bd6d054

File tree

1 file changed

+41
-15
lines changed

1 file changed

+41
-15
lines changed

UDAnnotations.hs

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,20 @@ data CncLabels = CncLabels {
7979
multiLabels :: M.Map Cat (Bool, Label), -- cat -> (if-head-first, other-labels) e.g. #multiword Prep head first fixed
8080
auxCategories :: M.Map CId String, -- auxcat -> cat, in both gf2ud and ud2gf, e.g. #auxcat Cop AUX
8181
changeLabels :: M.Map Label [(Label,Condition)], -- change to another label afterwards e.g. #change obj>obl above case
82-
macroFunctions :: M.Map CId (AbsType,(([CId],AbsTree),[(Label,[UDData])])), -- ud2gf only, e.g. #auxfun MkVPS_Fut will vp : Will -> VP -> VPS = MkVPS (TTAnt TFut ASimul) PPos vp ; aux head
82+
macroFunctions :: M.Map CId MacroFunction, -- ud2gf only, e.g. #auxfun MkVPS_Fut will vp : Will -> VP -> VPS = MkVPS (TTAnt TFut ASimul) PPos vp ; aux head
8383
altFunLabels :: M.Map CId [[Label]], -- ud2gf only, e.g. #altfun ComplSlash head obl
8484
disabledFunctions :: M.Map Fun () -- not to be used in ud2gf, e.g. #disable the_Det thePl_Det
8585

8686
}
8787

88+
data MacroFunction = MacroFunction
89+
{ mfType :: AbsType
90+
, mfArgNames :: [CId]
91+
, mfExpansion :: AbsTree
92+
, mfLabels :: [(Label, [UDData])]
93+
}
94+
deriving (Show)
95+
8896
data Condition =
8997
CAbove Label -- to change a label if it dominates this label
9098
| CFeatures [UDData] -- if it has these features
@@ -187,10 +195,15 @@ addMissing env = env {
187195

188196
-- #macro PredCop np cop comp : NP -> Cop -> Comp -> Cl = PredVP np (UseComp comp) ; subj cop head
189197
-- CId (AbsType,(([CId],AbsTree),[Label]))
190-
pMacroFunction (f:ws) = case break (==":") ws of
198+
pMacroFunction (f,ws) = case break (==":") ws of
191199
(xs,_:ww) -> case break (=="=") ww of
192200
(ty,_:tl) -> case break (==";") tl of
193-
(df,_:ls) -> (pAbsType (unwords ty), ((map mkCId xs, pAbsTree (unwords df)),map labelAndMorpho ls))
201+
(df,_:ls) -> MacroFunction
202+
{ mfType = pAbsType (unwords ty)
203+
, mfArgNames = map mkCId xs
204+
, mfExpansion = pAbsTree (unwords df)
205+
, mfLabels = map labelAndMorpho ls
206+
}
194207
_ -> error $ "missing labels in #macro " ++ unwords (f:ws)
195208
_ -> error $ "missing definition in #macro " ++ unwords (f:ws)
196209
_ -> error $ "missing type in #macro " ++ unwords (f:ws)
@@ -214,7 +227,7 @@ pCncLabels = dispatch . map words . uncomment . lines
214227
"#multiword":c:hp:lab:_ -> labs{multiLabels = M.insert (mkCId c) (hp/="head-last",lab) (multiLabels labs)}
215228
"#auxcat":c:p:[] -> labs{auxCategories = M.insert (mkCId c) p (auxCategories labs)}
216229
"#change":c1:">":c2:ws -> labs{changeLabels = M.insert c1 [(c2, pCondition ws)] (changeLabels labs)}
217-
"#auxfun":f:typdef -> labs{macroFunctions = M.insert (mkCId f) (pMacroFunction (f:typdef)) (macroFunctions labs)}
230+
"#auxfun":f:typdef -> labs{macroFunctions = M.insert (mkCId f) (pMacroFunction (f,typdef)) (macroFunctions labs)}
218231
"#disable":fs -> labs{disabledFunctions = inserts [(mkCId f,()) | f <- fs] (disabledFunctions labs)}
219232
"#altfun":f:xs -> labs{altFunLabels = M.insertWith (++) (mkCId f) [xs] (altFunLabels labs)}
220233

@@ -274,34 +287,47 @@ catsForPOS env = M.fromListWith (++) $
274287
-- CId (AbsType,(([CId],AbsTree),[Label]))
275288
expandMacro :: UDEnv -> AbsTree -> AbsTree
276289
expandMacro env tr@(RTree f ts) = case M.lookup f (macroFunctions (cncLabels env)) of
277-
Just (_,((xx,df),_)) -> subst (zip xx (map (expandMacro env) ts)) df
278-
_ -> RTree f (map (expandMacro env) ts)
290+
Just (MacroFunction _ xx df _) | length ts' == length xx ->
291+
expandMacro env $
292+
subst (zip xx ts') df
293+
_ -> RTree f ts'
279294
where
280-
subst xts t@(RTree h us) = case us of
295+
ts' = map (expandMacro env) ts
296+
subst xts t@(RTree h us) =
297+
case us of
281298
[] -> maybe t id (lookup h xts)
299+
-- Expand head: #auxfun Ex a b : A -> B -> C = a b ; cn head
300+
_ | Just (RTree h' hus) <- lookup h xts -> RTree h' (hus ++ map (subst xts) us)
282301
_ -> RTree h (map (subst xts) us)
283302

284303
----------------------------------------------------------------------------
285304
-- used in ud2gf: macros + real abstract functions, except the disabled ones
286305

287306
allFunsEnv :: UDEnv -> [(Fun,LabelledType)]
288-
allFunsEnv env =
289-
[(f,(val,zip args ls)) |
290-
(f,((val,args),((xx,df),ls))) <- M.assocs (macroFunctions (cncLabels env))]
307+
allFunsEnv env =
308+
macroFuns
309+
++
310+
labeledFuns
291311
++
292-
[(f, mkLabelledType typ labels) |
312+
altFuns
313+
where
314+
macroFuns =
315+
[(f,(val,zip args ls)) |
316+
(f,MacroFunction (val,args) xx df ls) <- M.assocs (macroFunctions (cncLabels env))]
317+
labeledFuns =
318+
[(f, mkLabelledType typ labels) |
293319
(f,labelss) <- M.assocs (funLabels (absLabels env)),
294320
M.notMember f (disabledFunctions (cncLabels env)),
295321
not (isBackupFunction f), ---- apply backups only later
296322
Just typ <- [functionType (pgfGrammar env) f],
297323
(_,labels) <- labelss ---- TODO precise handling of generalized labels
298-
]
299-
++
300-
[(f, mkLabelledType typ labels) |
324+
]
325+
altFuns =
326+
[(f, mkLabelledType typ labels) |
301327
(f,labelss) <- M.assocs (altFunLabels (cncLabels env)),
302328
labels <- labelss,
303329
Just typ <- [functionType (pgfGrammar env) f]
304-
]
330+
]
305331

306332
mkBackup ast cat = RTree (mkCId (showCId cat ++ "Backup")) [ast]
307333
isBackupFunction f = isSuffixOf "Backup" (showCId f)

0 commit comments

Comments
 (0)