GrammaticalFramework / gf-ud

Functions to analyse and manipulate dependency trees, as well as conversions between GF and dependency trees. The main use case is UD (Universal Dependencies), but the code is designed to be completely generic as for annotation scheme. This repository replaces the old gf-contrib/ud2gf code. It is also meant to be used in the 'vd' command of GF and replace the supporting code in gf-core in the future.
Other
7 stars 15 forks source link

Expand the capabilities of macros #15

Closed anka-213 closed 2 years ago

anka-213 commented 2 years ago

With this, you can write tiny programs in macros, for example to change the shape of a tree between GF and ud.

For example, this code allows ud2gf to handle phrases like "small, fluffy and cute":

#auxcat Comma PUNCT
#auxfun CommaAP_ ap comma : AP -> Comma -> APComma =  ap ; head punct

-- -- #auxfun AndCutePatternMatch_ and cute : Conj -> AP -> AP2AP = MkAP2AP and cute ; cc head
#auxfun AndCuteCont_ and cute : Conj -> AP -> Pair_Conj_AP = MkPair_ and cute ; cc head

-- #auxfun AP2_ small (MkAP2AP and cute) : AP -> AP2AP -> AP = ConjAP and (BaseAP small cute)) ; head conj
#auxfun AP2_ small andCute : AP -> Pair_Conj_AP -> AP = UsePair_ (AP2_helper_ small) andCute ; head conj
#auxfun AP2_helper_ small and cute :  AP -> Conj -> AP -> AP = ConjAP and (BaseAP small cute) ; head dummy nonexsistent

-- #auxfun APBaseComma_ small fluffy (MkAP2AP and cute)  : AP -> APComma -> AP2AP -> ConjListAP = ConjConsAP and small (BaseAP fluffy cute) ; head conj conj
#auxfun APBaseComma_ small fluffy andCute : AP -> APComma -> Pair_Conj_AP -> ConjListAP = UsePair_ (APBaseComma_helper_ small fluffy) andCute ; head conj conj
#auxfun APBaseComma_helper_ small fluffy and cute : AP -> APComma -> Conj -> AP -> ConjListAP = MkTriple_ and small (BaseAP fluffy cute) ; head dummy dummy

-- #auxfun ConjListToAP2_ (ConjConsAP and small furryFluffyCute) : ConjListAP -> AP = ConjAP and (ConsAP small furryFluffyCute) ; head
#auxfun ConjListToAP2_ and_small_furryFluffyCute : ConjListAP -> AP = UseTriple_ ConjListToAP2_helper_ and_small_furryFluffyCute ; head
#auxfun ConjListToAP2_helper_ and small furryFluffyCute : Conj -> AP -> ListAP -> AP = ConjAP and (ConsAP small furryFluffyCute) ; notreal head dummy

-- #auxfun APAddComma_ furry (ConjConsAP and small fluffyAndCute)  : APComma -> ConjListAP -> ConjListAP = ConjConsAP and small (ConsAP furry fluffyAndCute) ; conj head
#auxfun APAddComma_ furry and_small_fluffyCute  : APComma -> ConjListAP -> ConjListAP = UseTriple_ (APAddComma_helper_ furry) and_small_fluffyCute ; conj head
#auxfun APAddComma_helper_ furry and small fluffyCute : APComma -> Conj -> AP -> ListAP -> ConjListAP = MkTriple_ and small (ConsAP furry fluffyCute) ; dummy head

-- Only used inside other macros
-- Pair_a_b = (a -> b -> r) -> r
#auxfun MkPair_ a b handler : a -> b -> ab2r -> r = handler a b ; head dummy nonexsistent
#auxfun UsePair_ handler pair : ab2r -> Pair_a_b -> r = pair handler ; head dummy
-- Triple_a_b_c = (a -> b -> r) -> r
#auxfun MkTriple_ a b c handler : a -> b -> c -> abc2r -> r = handler a b c ; head dummy nonexsistent nope
#auxfun UseTriple_ handler triple : abc2r -> Triple_a_b_c -> r = triple handler ; head dummy

This is pretty hacky though, since some of the macros aren't really real macros, but are only used inside other macros during macro expansion, so it would be nice to have a cleaner solution where you aren't forced to write dummy labels and types.

inariksit commented 2 years ago

Newer version of lists:

-- Handling lists
-- This has to be repeated for every category

-- ** Generic, only used inside other macros **
-- Pair_a_b = (a -> b -> r) -> r
#auxfun MkPair_ a b handler : a -> b -> ab2r -> r = handler a b ; head dummy nonexistent
#auxfun UsePair_ handler pair : ab2r -> Pair_a_b -> r = pair handler ; head dummy

#auxfun Compose_ f g x : b2c -> a2b -> a -> c = f (g x) ; head dummy dummy

-- ** NP **
-- cat, horse, capybara and dog

#auxfun CommaNP_ np comma : NP -> Comma -> NPComma = np ; head punct
#auxfun AndDogPair_ and dog : Conj -> NP -> Pair_Conj_NP = MkPair_ and dog ; cc head
#auxfun NP2_ cat andDog : NP -> Pair_Conj_NP -> NP = UsePair_ (NP2_helper_ cat) andDog ; head conj
#auxfun NP2_helper_ cat and dog :  NP -> Conj -> NP -> NP = ConjNP and (BaseNP cat dog) ; head dummy nonexistent
anka-213 commented 2 years ago

The newer version gives the list in the correct order. The older one does not do that with 4 or more elements.