replace GFCC with PGF in (almost) all places

This commit is contained in:
krasimir
2008-05-30 11:15:33 +00:00
parent 1172539a95
commit 8bb0c32a9c
20 changed files with 246 additions and 267 deletions

View File

@@ -1,4 +1,4 @@
module PGF.Check (checkGFCC, checkGFCCio, checkGFCCmaybe) where
module PGF.Check (checkPGF) where
import PGF.CId
import PGF.Data
@@ -9,26 +9,11 @@ import qualified Data.Map as Map
import Control.Monad
import Debug.Trace
checkGFCCio :: GFCC -> IO GFCC
checkGFCCio gfcc = case checkGFCC gfcc of
Ok (gc,b) -> do
putStrLn $ if b then "OK" else "Corrupted GFCC"
return gc
Bad s -> do
putStrLn s
error "building GFCC failed"
---- needed in old Custom
checkGFCCmaybe :: GFCC -> Maybe GFCC
checkGFCCmaybe gfcc = case checkGFCC gfcc of
Ok (gc,b) -> return gc
Bad s -> Nothing
checkGFCC :: GFCC -> Err (GFCC,Bool)
checkGFCC gfcc = do
(cs,bs) <- mapM (checkConcrete gfcc)
(Map.assocs (concretes gfcc)) >>= return . unzip
return (gfcc {concretes = Map.fromAscList cs}, and bs)
checkPGF :: PGF -> Err (PGF,Bool)
checkPGF pgf = do
(cs,bs) <- mapM (checkConcrete pgf)
(Map.assocs (concretes pgf)) >>= return . unzip
return (pgf {concretes = Map.fromAscList cs}, and bs)
-- errors are non-fatal; replace with 'fail' to change this
@@ -43,18 +28,18 @@ labelBoolErr ms iob = do
if b then return (x,b) else (msg ms >> return (x,b))
checkConcrete :: GFCC -> (CId,Concr) -> Err ((CId,Concr),Bool)
checkConcrete gfcc (lang,cnc) =
checkConcrete :: PGF -> (CId,Concr) -> Err ((CId,Concr),Bool)
checkConcrete pgf (lang,cnc) =
labelBoolErr ("happened in language " ++ prCId lang) $ do
(rs,bs) <- mapM checkl (Map.assocs (lins cnc)) >>= return . unzip
return ((lang,cnc{lins = Map.fromAscList rs}),and bs)
where
checkl = checkLin gfcc lang
checkl = checkLin pgf lang
checkLin :: GFCC -> CId -> (CId,Term) -> Err ((CId,Term),Bool)
checkLin gfcc lang (f,t) =
checkLin :: PGF -> CId -> (CId,Term) -> Err ((CId,Term),Bool)
checkLin pgf lang (f,t) =
labelBoolErr ("happened in function " ++ prCId f) $ do
(t',b) <- checkTerm (lintype gfcc lang f) t --- $ inline gfcc lang t
(t',b) <- checkTerm (lintype pgf lang f) t --- $ inline pgf lang t
return ((f,t'),b)
inferTerm :: [CType] -> Term -> Err (Term,CType)
@@ -137,22 +122,22 @@ ints = C
str :: CType
str = S []
lintype :: GFCC -> CId -> CId -> LinType
lintype gfcc lang fun = case typeSkeleton (lookType gfcc fun) of
lintype :: PGF -> CId -> CId -> LinType
lintype pgf lang fun = case typeSkeleton (lookType pgf fun) of
(cs,c) -> (map vlinc cs, linc c) ---- HOAS
where
linc = lookLincat gfcc lang
linc = lookLincat pgf lang
vlinc (0,c) = linc c
vlinc (i,c) = case linc c of
R ts -> R (ts ++ replicate i str)
inline :: GFCC -> CId -> Term -> Term
inline gfcc lang t = case t of
inline :: PGF -> CId -> Term -> Term
inline pgf lang t = case t of
F c -> inl $ look c
_ -> composSafeOp inl t
where
inl = inline gfcc lang
look = lookLin gfcc lang
inl = inline pgf lang
look = lookLin pgf lang
composOp :: Monad m => (Term -> m Term) -> Term -> m Term
composOp f trm = case trm of

View File

@@ -8,9 +8,9 @@ import qualified Data.Map as Map
import Data.List
import Data.Array
-- internal datatypes for GFCC
-- internal datatypes for PGF
data GFCC = GFCC {
data PGF = PGF {
absname :: CId ,
cncnames :: [CId] ,
gflags :: Map.Map CId String, -- value of a global flag
@@ -120,17 +120,17 @@ fcatVar = (-4)
-- print statistics
statGFCC :: GFCC -> String
statGFCC gfcc = unlines [
"Abstract\t" ++ prCId (absname gfcc),
"Concretes\t" ++ unwords (map prCId (cncnames gfcc)),
"Categories\t" ++ unwords (map prCId (Map.keys (cats (abstract gfcc))))
statGFCC :: PGF -> String
statGFCC pgf = unlines [
"Abstract\t" ++ prCId (absname pgf),
"Concretes\t" ++ unwords (map prCId (cncnames pgf)),
"Categories\t" ++ unwords (map prCId (Map.keys (cats (abstract pgf))))
]
-- merge two GFCCs; fails is differens absnames; priority to second arg
unionGFCC :: GFCC -> GFCC -> GFCC
unionGFCC one two = case absname one of
unionPGF :: PGF -> PGF -> PGF
unionPGF one two = case absname one of
n | n == wildCId -> two -- extending empty grammar
| n == absname two -> one { -- extending grammar with same abstract
concretes = Map.union (concretes two) (concretes one),
@@ -138,8 +138,8 @@ unionGFCC one two = case absname one of
}
_ -> one -- abstracts don't match ---- print error msg
emptyGFCC :: GFCC
emptyGFCC = GFCC {
emptyPGF :: PGF
emptyPGF = PGF {
absname = wildCId,
cncnames = [] ,
gflags = Map.empty,
@@ -149,9 +149,9 @@ emptyGFCC = GFCC {
-- encode idenfifiers and strings in UTF8
utf8GFCC :: GFCC -> GFCC
utf8GFCC gfcc = gfcc {
concretes = Map.map u8concr (concretes gfcc)
utf8GFCC :: PGF -> PGF
utf8GFCC pgf = pgf {
concretes = Map.map u8concr (concretes pgf)
}
where
u8concr cnc = cnc {

View File

@@ -8,8 +8,8 @@ import qualified Data.Map as M
import System.Random
-- generate an infinite list of trees exhaustively
generate :: GFCC -> CId -> Maybe Int -> [Exp]
generate gfcc cat dp = concatMap (\i -> gener i cat) depths
generate :: PGF -> CId -> Maybe Int -> [Exp]
generate pgf cat dp = concatMap (\i -> gener i cat) depths
where
gener 0 c = [EApp f [] | (f, ([],_)) <- fns c]
gener i c = [
@@ -20,12 +20,12 @@ generate gfcc cat dp = concatMap (\i -> gener i cat) depths
let tr = EApp f ts,
depth tr >= i
]
fns c = [(f,catSkeleton ty) | (f,ty) <- functionsToCat gfcc c]
fns c = [(f,catSkeleton ty) | (f,ty) <- functionsToCat pgf c]
depths = maybe [0 ..] (\d -> [0..d]) dp
-- generate an infinite list of trees randomly
genRandom :: StdGen -> GFCC -> CId -> [Exp]
genRandom gen gfcc cat = genTrees (randomRs (0.0, 1.0 :: Double) gen) cat where
genRandom :: StdGen -> PGF -> CId -> [Exp]
genRandom gen pgf cat = genTrees (randomRs (0.0, 1.0 :: Double) gen) cat where
timeout = 47 -- give up
@@ -55,7 +55,7 @@ genRandom gen gfcc cat = genTrees (randomRs (0.0, 1.0 :: Double) gen) cat where
in (t:ts, k + ks)
_ -> ([],0)
fns cat = [(f,(fst (catSkeleton ty))) | (f,ty) <- functionsToCat gfcc cat]
fns cat = [(f,(fst (catSkeleton ty))) | (f,ty) <- functionsToCat pgf cat]
{-
@@ -63,8 +63,8 @@ genRandom gen gfcc cat = genTrees (randomRs (0.0, 1.0 :: Double) gen) cat where
-- note: you cannot throw away rules with unknown words from the grammar
-- because it is not known which field in each rule may match the input
searchParse :: Int -> GFCC -> CId -> [String] -> [Exp]
searchParse i gfcc cat ws = [t | t <- gen, s <- lins t, words s == ws] where
gen = take i $ generate gfcc cat
lins t = [linearize gfcc lang t | lang <- cncnames gfcc]
searchParse :: Int -> PGF -> CId -> [String] -> [Exp]
searchParse i pgf cat ws = [t | t <- gen, s <- lins t, words s == ws] where
gen = take i $ generate pgf cat
lins t = [linearize pgf lang t | lang <- cncnames pgf]
-}

View File

@@ -8,10 +8,10 @@ import Data.List
import Debug.Trace
-- linearization and computation of concrete GFCC Terms
-- linearization and computation of concrete PGF Terms
linearize :: GFCC -> CId -> Exp -> String
linearize mcfg lang = realize . linExp mcfg lang
linearize :: PGF -> CId -> Exp -> String
linearize pgf lang = realize . linExp pgf lang
realize :: Term -> String
realize trm = case trm of
@@ -25,8 +25,8 @@ realize trm = case trm of
TM s -> s
_ -> "ERROR " ++ show trm ---- debug
linExp :: GFCC -> CId -> Exp -> Term
linExp gfcc lang = lin
linExp :: PGF -> CId -> Exp -> Term
linExp pgf lang = lin
where
lin (EAbs xs e ) = case lin e of
R ts -> R $ ts ++ (Data.List.map (kks . prCId) xs)
@@ -38,12 +38,12 @@ linExp gfcc lang = lin
lin (EVar x ) = TM (prCId x)
lin (EMeta i ) = TM (show i)
comp = compute gfcc lang
look = lookLin gfcc lang
comp = compute pgf lang
look = lookLin pgf lang
compute :: GFCC -> CId -> [Term] -> Term -> Term
compute mcfg lang args = comp where
compute :: PGF -> CId -> [Term] -> Term -> Term
compute pgf lang args = comp where
comp trm = case trm of
P r p -> proj (comp r) (comp p)
W s t -> W s (comp t)
@@ -54,7 +54,7 @@ compute mcfg lang args = comp where
S ts -> S $ filter (/= S []) $ map comp ts
_ -> trm
look = lookOper mcfg lang
look = lookOper pgf lang
idx xs i = if i > length xs - 1
then trace

View File

@@ -8,58 +8,58 @@ import qualified Data.Array as Array
import Data.Maybe
import Data.List
-- operations for manipulating GFCC grammars and objects
-- operations for manipulating PGF grammars and objects
lookLin :: GFCC -> CId -> CId -> Term
lookLin gfcc lang fun =
lookMap tm0 fun $ lins $ lookMap (error "no lang") lang $ concretes gfcc
lookLin :: PGF -> CId -> CId -> Term
lookLin pgf lang fun =
lookMap tm0 fun $ lins $ lookMap (error "no lang") lang $ concretes pgf
lookOper :: GFCC -> CId -> CId -> Term
lookOper gfcc lang fun =
lookMap tm0 fun $ opers $ lookMap (error "no lang") lang $ concretes gfcc
lookOper :: PGF -> CId -> CId -> Term
lookOper pgf lang fun =
lookMap tm0 fun $ opers $ lookMap (error "no lang") lang $ concretes pgf
lookLincat :: GFCC -> CId -> CId -> Term
lookLincat gfcc lang fun =
lookMap tm0 fun $ lincats $ lookMap (error "no lang") lang $ concretes gfcc
lookLincat :: PGF -> CId -> CId -> Term
lookLincat pgf lang fun =
lookMap tm0 fun $ lincats $ lookMap (error "no lang") lang $ concretes pgf
lookParamLincat :: GFCC -> CId -> CId -> Term
lookParamLincat gfcc lang fun =
lookMap tm0 fun $ paramlincats $ lookMap (error "no lang") lang $ concretes gfcc
lookParamLincat :: PGF -> CId -> CId -> Term
lookParamLincat pgf lang fun =
lookMap tm0 fun $ paramlincats $ lookMap (error "no lang") lang $ concretes pgf
lookType :: GFCC -> CId -> Type
lookType gfcc f =
fst $ lookMap (error $ "lookType " ++ show f) f (funs (abstract gfcc))
lookType :: PGF -> CId -> Type
lookType pgf f =
fst $ lookMap (error $ "lookType " ++ show f) f (funs (abstract pgf))
lookParser :: GFCC -> CId -> Maybe ParserInfo
lookParser gfcc lang = parser $ lookMap (error "no lang") lang $ concretes gfcc
lookParser :: PGF -> CId -> Maybe ParserInfo
lookParser pgf lang = parser $ lookMap (error "no lang") lang $ concretes pgf
lookFCFG :: GFCC -> CId -> Maybe FGrammar
lookFCFG gfcc lang = fmap toFGrammar $ lookParser gfcc lang
lookFCFG :: PGF -> CId -> Maybe FGrammar
lookFCFG pgf lang = fmap toFGrammar $ lookParser pgf lang
where
toFGrammar :: ParserInfo -> FGrammar
toFGrammar pinfo = (Array.elems (allRules pinfo), startupCats pinfo)
lookStartCat :: GFCC -> String
lookStartCat gfcc = fromMaybe "S" $ msum $ Data.List.map (Map.lookup (mkCId "startcat"))
[gflags gfcc, aflags (abstract gfcc)]
lookStartCat :: PGF -> String
lookStartCat pgf = fromMaybe "S" $ msum $ Data.List.map (Map.lookup (mkCId "startcat"))
[gflags pgf, aflags (abstract pgf)]
lookGlobalFlag :: GFCC -> CId -> String
lookGlobalFlag gfcc f =
lookMap "?" f (gflags gfcc)
lookGlobalFlag :: PGF -> CId -> String
lookGlobalFlag pgf f =
lookMap "?" f (gflags pgf)
lookAbsFlag :: GFCC -> CId -> String
lookAbsFlag gfcc f =
lookMap "?" f (aflags (abstract gfcc))
lookAbsFlag :: PGF -> CId -> String
lookAbsFlag pgf f =
lookMap "?" f (aflags (abstract pgf))
lookCncFlag :: GFCC -> CId -> CId -> String
lookCncFlag gfcc lang f =
lookMap "?" f $ cflags $ lookMap (error "no lang") lang $ concretes gfcc
lookCncFlag :: PGF -> CId -> CId -> String
lookCncFlag pgf lang f =
lookMap "?" f $ cflags $ lookMap (error "no lang") lang $ concretes pgf
functionsToCat :: GFCC -> CId -> [(CId,Type)]
functionsToCat gfcc cat =
[(f,ty) | f <- fs, Just (ty,_) <- [Map.lookup f $ funs $ abstract gfcc]]
functionsToCat :: PGF -> CId -> [(CId,Type)]
functionsToCat pgf cat =
[(f,ty) | f <- fs, Just (ty,_) <- [Map.lookup f $ funs $ abstract pgf]]
where
fs = lookMap [] cat $ catfuns $ abstract gfcc
fs = lookMap [] cat $ catfuns $ abstract pgf
depth :: Exp -> Int
depth (EAbs _ t) = depth t

View File

@@ -1,4 +1,4 @@
module PGF.Raw.Convert (toGFCC,fromGFCC) where
module PGF.Raw.Convert (toPGF,fromPGF) where
import PGF.CId
import PGF.Data
@@ -12,10 +12,10 @@ import qualified Data.Map as Map
pgfMajorVersion, pgfMinorVersion :: Integer
(pgfMajorVersion, pgfMinorVersion) = (1,0)
-- convert parsed grammar to internal GFCC
-- convert parsed grammar to internal PGF
toGFCC :: Grammar -> GFCC
toGFCC (Grm [
toPGF :: Grammar -> PGF
toPGF (Grm [
App "pgf" (AInt v1 : AInt v2 : App a []:cs),
App "flags" gfs,
ab@(
@@ -24,7 +24,7 @@ toGFCC (Grm [
App "cat" cts
]),
App "concrete" ccs
]) = GFCC {
]) = PGF {
absname = mkCId a,
cncnames = [mkCId c | App c [] <- cs],
gflags = Map.fromAscList [(mkCId f,v) | App f [AStr v] <- gfs],
@@ -135,20 +135,20 @@ toTerm e = case e of
--- from internal to parser --
------------------------------
fromGFCC :: GFCC -> Grammar
fromGFCC gfcc0 = Grm [
fromPGF :: PGF -> Grammar
fromPGF pgf0 = Grm [
App "pgf" (AInt pgfMajorVersion:AInt pgfMinorVersion
: App (prCId (absname gfcc)) [] : map (flip App [] . prCId) (cncnames gfcc)),
App "flags" [App (prCId f) [AStr v] | (f,v) <- Map.toList (gflags gfcc `Map.union` aflags agfcc)],
: App (prCId (absname pgf)) [] : map (flip App [] . prCId) (cncnames pgf)),
App "flags" [App (prCId f) [AStr v] | (f,v) <- Map.toList (gflags pgf `Map.union` aflags apgf)],
App "abstract" [
App "fun" [App (prCId f) [fromType t,fromExp d] | (f,(t,d)) <- Map.toList (funs agfcc)],
App "cat" [App (prCId f) (map fromHypo hs) | (f,hs) <- Map.toList (cats agfcc)]
App "fun" [App (prCId f) [fromType t,fromExp d] | (f,(t,d)) <- Map.toList (funs apgf)],
App "cat" [App (prCId f) (map fromHypo hs) | (f,hs) <- Map.toList (cats apgf)]
],
App "concrete" [App (prCId lang) (fromConcrete c) | (lang,c) <- Map.toList (concretes gfcc)]
App "concrete" [App (prCId lang) (fromConcrete c) | (lang,c) <- Map.toList (concretes pgf)]
]
where
gfcc = utf8GFCC gfcc0
agfcc = abstract gfcc
pgf = utf8GFCC pgf0
apgf = abstract pgf
fromConcrete cnc = [
App "flags" [App (prCId f) [AStr v] | (f,v) <- Map.toList (cflags cnc)],
App "lin" [App (prCId f) [fromTerm v] | (f,v) <- Map.toList (lins cnc)],

View File

@@ -37,7 +37,7 @@ prRecord = prr where
RS s -> prQuotedString s
RCon s -> s
-- uses the encoding of record types in GFCC.paramlincat
-- uses the encoding of record types in PGF.paramlincat
mkRecord :: Term -> Term -> Record
mkRecord typ trm = case (typ,trm) of
(R rs, R ts) -> RR [(str lab, mkRecord ty t) | (P lab ty, t) <- zip rs ts]
@@ -50,18 +50,18 @@ mkRecord typ trm = case (typ,trm) of
str = realize
-- show all branches, without labels and params
allLinearize :: GFCC -> CId -> Exp -> String
allLinearize gfcc lang = concat . map pr . tabularLinearize gfcc lang where
allLinearize :: PGF -> CId -> Exp -> String
allLinearize pgf lang = concat . map pr . tabularLinearize pgf lang where
pr (p,vs) = unlines vs
-- show all branches, with labels and params
tableLinearize :: GFCC -> CId -> Exp -> String
tableLinearize gfcc lang = unlines . map pr . tabularLinearize gfcc lang where
tableLinearize :: PGF -> CId -> Exp -> String
tableLinearize pgf lang = unlines . map pr . tabularLinearize pgf lang where
pr (p,vs) = p +++ ":" +++ unwords (intersperse "|" vs)
-- create a table from labels+params to variants
tabularLinearize :: GFCC -> CId -> Exp -> [(String,[String])]
tabularLinearize gfcc lang = branches . recLinearize gfcc lang where
tabularLinearize :: PGF -> CId -> Exp -> [(String,[String])]
tabularLinearize pgf lang = branches . recLinearize pgf lang where
branches r = case r of
RR fs -> [(lab +++ b,s) | (lab,t) <- fs, (b,s) <- branches t]
RT fs -> [(lab +++ b,s) | (lab,t) <- fs, (b,s) <- branches t]
@@ -70,17 +70,17 @@ tabularLinearize gfcc lang = branches . recLinearize gfcc lang where
RCon _ -> []
-- show record in GF-source-like syntax
recordLinearize :: GFCC -> CId -> Exp -> String
recordLinearize gfcc lang = prRecord . recLinearize gfcc lang
recordLinearize :: PGF -> CId -> Exp -> String
recordLinearize pgf lang = prRecord . recLinearize pgf lang
-- create a GF-like record, forming the basis of all functions above
recLinearize :: GFCC -> CId -> Exp -> Record
recLinearize gfcc lang exp = mkRecord typ $ linExp gfcc lang exp where
recLinearize :: PGF -> CId -> Exp -> Record
recLinearize pgf lang exp = mkRecord typ $ linExp pgf lang exp where
typ = case exp of
EApp f _ -> lookParamLincat gfcc lang $ valCat $ lookType gfcc f
EApp f _ -> lookParamLincat pgf lang $ valCat $ lookType pgf f
-- show GFCC term
termLinearize :: GFCC -> CId -> Exp -> String
termLinearize gfcc lang = show . linExp gfcc lang
-- show PGF term
termLinearize :: PGF -> CId -> Exp -> String
termLinearize pgf lang = show . linExp pgf lang