summaryrefslogtreecommitdiff
path: root/src/GF/Parsing/ConvertGFCtoMCFG/Coercions.hs
diff options
context:
space:
mode:
authorpeb <unknown>2005-03-21 21:31:43 +0000
committerpeb <unknown>2005-03-21 21:31:43 +0000
commit531be3a72e938f2360d312c7fd9cd173e5442594 (patch)
tree7857722b476b4091e600301525f394b98e886095 /src/GF/Parsing/ConvertGFCtoMCFG/Coercions.hs
parent75d228629a267da1be6c26a6fb13a14f3da0f7c2 (diff)
"Committed_by_peb"
Diffstat (limited to 'src/GF/Parsing/ConvertGFCtoMCFG/Coercions.hs')
-rw-r--r--src/GF/Parsing/ConvertGFCtoMCFG/Coercions.hs70
1 files changed, 70 insertions, 0 deletions
diff --git a/src/GF/Parsing/ConvertGFCtoMCFG/Coercions.hs b/src/GF/Parsing/ConvertGFCtoMCFG/Coercions.hs
new file mode 100644
index 000000000..a0bac995c
--- /dev/null
+++ b/src/GF/Parsing/ConvertGFCtoMCFG/Coercions.hs
@@ -0,0 +1,70 @@
+----------------------------------------------------------------------
+-- |
+-- Module : AddCoercions
+-- Maintainer : PL
+-- Stability : (stable)
+-- Portability : (portable)
+--
+-- > CVS $Date: 2005/03/21 22:31:53 $
+-- > CVS $Author: peb $
+-- > CVS $Revision: 1.1 $
+--
+-----------------------------------------------------------------------------
+
+
+module GF.Parsing.ConvertGFCtoMCFG.Coercions (addCoercions) where
+
+import Tracing
+import GF.Printing.PrintParser
+import GF.Printing.PrintSimplifiedTerm
+-- import PrintGFC
+-- import qualified PrGrammar as PG
+
+import qualified Ident
+import GF.Parsing.Utilities
+import GF.Parsing.GrammarTypes
+import GF.Parsing.MCFGrammar (Rule(..), Lin(..))
+import GF.Data.SortedList
+import List (groupBy) -- , transpose)
+
+----------------------------------------------------------------------
+
+addCoercions :: MCFGrammar -> MCFGrammar
+addCoercions rules = coercions ++ rules
+ where (allHeads, allArgs) = unzip [ ((head, lbls), nubsort args) |
+ Rule head args lins _ <- rules,
+ let lbls = [ lbl | Lin lbl _ <- lins ] ]
+ allHeadSet = nubsort allHeads
+ allArgSet = union allArgs <\\> map fst allHeadSet
+ coercions = tracePrt "#coercions total" (prt . length) $
+ concat $
+ tracePrt "#coercions per cat" (prtList . map length) $
+ combineCoercions
+ (groupBy sameCatFst allHeadSet)
+ (groupBy sameCat allArgSet)
+ sameCatFst a b = sameCat (fst a) (fst b)
+
+
+combineCoercions [] _ = []
+combineCoercions _ [] = []
+combineCoercions allHeads'@(heads:allHeads) allArgs'@(args:allArgs)
+ = case compare (mainCat $ fst $ head heads) (mainCat $ head args) of
+ LT -> combineCoercions allHeads allArgs'
+ GT -> combineCoercions allHeads' allArgs
+ EQ -> makeCoercion heads args : combineCoercions allHeads allArgs
+
+
+makeCoercion heads args = [ Rule arg [head] lins coercionName |
+ (head@(MCFCat _ headCns), lbls) <- heads,
+ let lins = [ Lin lbl [Cat (head, lbl, 0)] | lbl <- lbls ],
+ arg@(MCFCat _ argCns) <- args,
+ argCns `subset` headCns ]
+
+
+coercionName = Ident.IW
+
+mainCat (MCFCat c _) = c
+
+sameCat mc1 mc2 = mainCat mc1 == mainCat mc2
+
+