mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-23 11:42:49 -06:00
gfcc from GF now works for LangEng (except literals)
This commit is contained in:
@@ -5,7 +5,7 @@ cat S ; NP ; VP ;
|
|||||||
fun
|
fun
|
||||||
Pred : NP -> VP -> S ;
|
Pred : NP -> VP -> S ;
|
||||||
|
|
||||||
He, She : NP ;
|
Je, Tu, Il, Elle, Nous, Vous, Ils, Elles : NP ;
|
||||||
|
|
||||||
Strong : VP ;
|
Strong : VP ;
|
||||||
|
|
||||||
|
|||||||
@@ -3,16 +3,21 @@ concrete KoeFre of Koe = {
|
|||||||
param
|
param
|
||||||
Gen = Masc | Fem ;
|
Gen = Masc | Fem ;
|
||||||
Num = Sg | Pl ;
|
Num = Sg | Pl ;
|
||||||
|
Per = P1 | P2 | P3 ;
|
||||||
|
|
||||||
oper
|
oper
|
||||||
Agr : Type = {g : Gen ; n : Num} ;
|
Agr : Type = {g : Gen ; n : Num ; p : Per} ;
|
||||||
|
|
||||||
predA : Str -> {s : Agr => Str} = \adj ->
|
predA : Str -> {s : Agr => Str} = \adj ->
|
||||||
{s = \\a => copula a.n ++ regA adj a.g a.n} ;
|
{s = \\a => copula a.n a.p ++ regA adj a.g a.n} ;
|
||||||
|
|
||||||
copula : Num -> Str = \n -> case n of {
|
copula : Num -> Per -> Str = \n,p -> case <n,p> of {
|
||||||
Sg => "est" ;
|
<Sg,P1> => "suis" ;
|
||||||
Pl => "sont"
|
<Sg,P2> => "es" ;
|
||||||
|
<Sg,P3> => "est" ;
|
||||||
|
<Pl,P1> => "sommes" ;
|
||||||
|
<Pl,P2> => "êtes" ;
|
||||||
|
<Pl,P3> => "sont"
|
||||||
} ;
|
} ;
|
||||||
|
|
||||||
regA : Str -> Gen -> Num -> Str = \s,g,n -> case <g,n> of {
|
regA : Str -> Gen -> Num -> Str = \s,g,n -> case <g,n> of {
|
||||||
@@ -29,8 +34,14 @@ lincat
|
|||||||
lin
|
lin
|
||||||
Pred np vp = {s = np.s ++ vp.s ! np.a} ;
|
Pred np vp = {s = np.s ++ vp.s ! np.a} ;
|
||||||
|
|
||||||
He = {s = "il" ; a = {g = Masc ; n = Sg}} ;
|
Je = {s = "je" ; a = {g = Masc ; n = Sg ; p = P1}} ;
|
||||||
She = {s = "elle" ; a = {g = Fem ; n = Sg}} ;
|
Tu = {s = "tu" ; a = {g = Masc ; n = Sg ; p = P2}} ;
|
||||||
|
Il = {s = "il" ; a = {g = Masc ; n = Sg ; p = P3}} ;
|
||||||
|
Elle = {s = "elle" ; a = {g = Fem ; n = Sg ; p = P3}} ;
|
||||||
|
Nous = {s = "nous" ; a = {g = Masc ; n = Pl ; p = P1}} ;
|
||||||
|
Vous = {s = "vous" ; a = {g = Masc ; n = Pl ; p = P2}} ;
|
||||||
|
Ils = {s = "ils" ; a = {g = Masc ; n = Pl ; p = P3}} ;
|
||||||
|
Elles = {s = "elles" ; a = {g = Fem ; n = Pl ; p = P3}} ;
|
||||||
|
|
||||||
Strong = predA "fort" ;
|
Strong = predA "fort" ;
|
||||||
|
|
||||||
|
|||||||
@@ -29,50 +29,62 @@ checkLin gfcc lang (f,t) =
|
|||||||
labelBoolIO ("happened in function " ++ printTree f) $
|
labelBoolIO ("happened in function " ++ printTree f) $
|
||||||
checkTerm (lintype gfcc lang f) $ inline gfcc lang t
|
checkTerm (lintype gfcc lang f) $ inline gfcc lang t
|
||||||
|
|
||||||
inferTerm :: [Tpe] -> Term -> Maybe Tpe
|
inferTerm :: [Tpe] -> Term -> Err Tpe
|
||||||
inferTerm args trm = case trm of
|
inferTerm args trm = case trm of
|
||||||
K _ -> return str
|
K _ -> return str
|
||||||
C i -> return $ ints i
|
C i -> return $ ints i
|
||||||
V i -> if i < length args
|
V i -> do
|
||||||
then (return $ args !! i)
|
testErr (i < length args) ("too large index " ++ show i)
|
||||||
else error ("index " ++ show i)
|
return $ args !! i
|
||||||
S ts -> do
|
S ts -> do
|
||||||
tys <- mapM infer ts
|
tys <- mapM infer ts
|
||||||
if all (==str) tys
|
let tys' = filter (/=str) tys
|
||||||
then return str
|
testErr (null tys')
|
||||||
else error ("only strings expected in: " ++ printTree trm
|
("expected Str in " ++ prt trm ++ " not " ++ unwords (map prt tys'))
|
||||||
++ " instead of " ++ unwords (map printTree tys)
|
return str
|
||||||
)
|
|
||||||
R ts -> do
|
R ts -> do
|
||||||
tys <- mapM infer ts
|
tys <- mapM infer ts
|
||||||
return $ tuple tys
|
return $ tuple tys
|
||||||
P t u -> do
|
P t u -> do
|
||||||
R tys <- infer t
|
tt <- infer t
|
||||||
case u of
|
tu <- infer u
|
||||||
|
case tt of
|
||||||
|
R tys -> case tu of
|
||||||
R [v] -> infer $ P t v
|
R [v] -> infer $ P t v
|
||||||
R (v:vs) -> infer $ P (head tys) (R vs) -----
|
R (v:vs) -> infer $ P (head tys) (R vs) -----
|
||||||
|
|
||||||
C i -> if (i < length tys)
|
C i -> do
|
||||||
then (return $ tys !! i) -- record: index must be known
|
testErr (i < length tys)
|
||||||
else error ("too few fields in " ++ printTree (R tys))
|
("required more than " ++ show i ++ " fields in " ++ prt (R tys))
|
||||||
_ -> if all (==head tys) tys -- table: must be same
|
(return $ tys !! i) -- record: index must be known
|
||||||
then return (head tys)
|
_ -> do
|
||||||
else error ("projection " ++ printTree trm)
|
let typ = head tys
|
||||||
FV ts -> return $ head ts ---- empty variants; check equality
|
testErr (all (==typ) tys) ("different types in table " ++ prt trm)
|
||||||
|
return typ -- table: must be same
|
||||||
|
_ -> Bad $ "projection from " ++ prt t ++ " : " ++ prt tt
|
||||||
|
FV [] -> return str ----
|
||||||
|
FV (t:ts) -> do
|
||||||
|
ty <- infer t
|
||||||
|
tys <- mapM infer ts
|
||||||
|
testErr (all (==ty) tys) ("different types in variants " ++ prt trm)
|
||||||
|
return ty
|
||||||
W s r -> infer r
|
W s r -> infer r
|
||||||
_ -> error ("no type inference for " ++ printTree trm)
|
_ -> Bad ("no type inference for " ++ prt trm)
|
||||||
where
|
where
|
||||||
infer = inferTerm args
|
infer = inferTerm args
|
||||||
|
prt = printTree
|
||||||
|
|
||||||
checkTerm :: LinType -> Term -> IO Bool
|
checkTerm :: LinType -> Term -> IO Bool
|
||||||
checkTerm (args,val) trm = case inferTerm args trm of
|
checkTerm (args,val) trm = case inferTerm args trm of
|
||||||
Just ty -> if eqType ty val then return True else do
|
Ok ty -> if eqType ty val
|
||||||
|
then return True
|
||||||
|
else do
|
||||||
putStrLn $ "term: " ++ printTree trm ++
|
putStrLn $ "term: " ++ printTree trm ++
|
||||||
"\nexpected type: " ++ printTree val ++
|
"\nexpected type: " ++ printTree val ++
|
||||||
"\ninferred type: " ++ printTree ty
|
"\ninferred type: " ++ printTree ty
|
||||||
return False
|
return False
|
||||||
_ -> do
|
Bad s -> do
|
||||||
putStrLn $ "cannot infer type of " ++ printTree trm
|
putStrLn s
|
||||||
return False
|
return False
|
||||||
|
|
||||||
eqType :: Tpe -> Tpe -> Bool
|
eqType :: Tpe -> Tpe -> Bool
|
||||||
@@ -117,14 +129,31 @@ inline gfcc lang t = case t of
|
|||||||
|
|
||||||
composOp :: Monad m => (Term -> m Term) -> Term -> m Term
|
composOp :: Monad m => (Term -> m Term) -> Term -> m Term
|
||||||
composOp f trm = case trm of
|
composOp f trm = case trm of
|
||||||
R ts -> liftM R $ mapM comp ts
|
R ts -> liftM R $ mapM f ts
|
||||||
S ts -> liftM S $ mapM comp ts
|
S ts -> liftM S $ mapM f ts
|
||||||
FV ts -> liftM FV $ mapM comp ts
|
FV ts -> liftM FV $ mapM f ts
|
||||||
P t u -> liftM2 P (comp t) (comp u)
|
P t u -> liftM2 P (f t) (f u)
|
||||||
W s t -> liftM (W s) $ comp t
|
W s t -> liftM (W s) $ f t
|
||||||
_ -> return trm
|
_ -> return trm
|
||||||
where
|
|
||||||
comp = composOp f
|
|
||||||
|
|
||||||
composSafeOp :: (Term -> Term) -> Term -> Term
|
composSafeOp :: (Term -> Term) -> Term -> Term
|
||||||
composSafeOp f = maybe undefined id . composOp (return . f)
|
composSafeOp f = maybe undefined id . composOp (return . f)
|
||||||
|
|
||||||
|
-- from GF.Data.Oper
|
||||||
|
|
||||||
|
maybeErr :: String -> Maybe a -> Err a
|
||||||
|
maybeErr s = maybe (Bad s) Ok
|
||||||
|
|
||||||
|
testErr :: Bool -> String -> Err ()
|
||||||
|
testErr cond msg = if cond then return () else Bad msg
|
||||||
|
|
||||||
|
errVal :: a -> Err a -> a
|
||||||
|
errVal a = err (const a) id
|
||||||
|
|
||||||
|
errIn :: String -> Err a -> Err a
|
||||||
|
errIn msg = err (\s -> Bad (s ++ "\nOCCURRED IN\n" ++ msg)) return
|
||||||
|
|
||||||
|
err :: (String -> b) -> (a -> b) -> Err a -> b
|
||||||
|
err d f e = case e of
|
||||||
|
Ok a -> f a
|
||||||
|
Bad s -> d s
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
module GF.Canon.GFCC.DataGFCC where
|
module GF.Canon.GFCC.DataGFCC where
|
||||||
|
|
||||||
import GF.Canon.GFCC.AbsGFCC
|
import GF.Canon.GFCC.AbsGFCC
|
||||||
|
import GF.Canon.GFCC.PrintGFCC
|
||||||
import Data.Map
|
import Data.Map
|
||||||
import Data.List
|
import Data.List
|
||||||
import Debug.Trace ----
|
import Debug.Trace ----
|
||||||
@@ -92,10 +93,14 @@ compute mcfg lang args = comp where
|
|||||||
|
|
||||||
look = lookLin mcfg lang
|
look = lookLin mcfg lang
|
||||||
|
|
||||||
idx xs i = if i > length xs - 1 then trace "overrun !!\n" (last xs) else xs !! i
|
idx xs i = if i > length xs - 1
|
||||||
|
then trace
|
||||||
|
("too large " ++ show i ++ " for\n" ++ unlines (Prelude.map prt xs) ++ "\n") TM
|
||||||
|
else xs !! i
|
||||||
|
|
||||||
proj r p = case (r,p) of
|
proj r p = case (r,p) of
|
||||||
(_, FV ts) -> FV $ Prelude.map (proj r) ts
|
(_, FV ts) -> FV $ Prelude.map (proj r) ts
|
||||||
|
(FV ts, _ ) -> FV $ Prelude.map (\t -> proj t r) ts
|
||||||
(W s t, _) -> kks (s ++ getString (proj t p))
|
(W s t, _) -> kks (s ++ getString (proj t p))
|
||||||
(_,R is) -> comp $ foldl P r is
|
(_,R is) -> comp $ foldl P r is
|
||||||
_ -> comp $ getField r (getIndex p)
|
_ -> comp $ getField r (getIndex p)
|
||||||
@@ -116,6 +121,8 @@ compute mcfg lang args = comp where
|
|||||||
TM -> TM
|
TM -> TM
|
||||||
_ -> trace ("ERROR in grammar compiler: field from " ++ show t) t
|
_ -> trace ("ERROR in grammar compiler: field from " ++ show t) t
|
||||||
|
|
||||||
|
prt = printTree
|
||||||
|
|
||||||
mkGFCC :: Grammar -> GFCC
|
mkGFCC :: Grammar -> GFCC
|
||||||
mkGFCC (Grm (Hdr a cs) ab@(Abs funs) ccs) = GFCC {
|
mkGFCC (Grm (Hdr a cs) ab@(Abs funs) ccs) = GFCC {
|
||||||
absname = a,
|
absname = a,
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import qualified GF.Grammar.Macros as GM
|
|||||||
import qualified GF.Infra.Modules as M
|
import qualified GF.Infra.Modules as M
|
||||||
import qualified GF.Infra.Option as O
|
import qualified GF.Infra.Option as O
|
||||||
|
|
||||||
|
import GF.Devel.PrGrammar
|
||||||
import GF.Devel.ModDeps
|
import GF.Devel.ModDeps
|
||||||
import GF.Infra.Ident
|
import GF.Infra.Ident
|
||||||
import GF.Infra.Option
|
import GF.Infra.Option
|
||||||
@@ -38,7 +39,9 @@ mkCanon2gfcc opts cnc gr =
|
|||||||
|
|
||||||
canon2gfcc :: Options -> SourceGrammar -> C.Grammar
|
canon2gfcc :: Options -> SourceGrammar -> C.Grammar
|
||||||
canon2gfcc opts cgr@(M.MGrammar ((a,M.ModMod abm):cms)) =
|
canon2gfcc opts cgr@(M.MGrammar ((a,M.ModMod abm):cms)) =
|
||||||
C.Grm (C.Hdr (i2i a) cs) (C.Abs adefs) cncs where
|
(if (oElem (iOpt "show_canon") opts) then trace (prGrammar cgr) else id) $
|
||||||
|
C.Grm (C.Hdr (i2i a) cs) (C.Abs adefs) cncs
|
||||||
|
where
|
||||||
cs = map (i2i . fst) cms
|
cs = map (i2i . fst) cms
|
||||||
adefs = [C.Fun f' (mkType ty) (C.Tr (C.AC f') []) |
|
adefs = [C.Fun f' (mkType ty) (C.Tr (C.AC f') []) |
|
||||||
(f,AbsFun (Yes ty) _) <- tree2list (M.jments abm), let f' = i2i f]
|
(f,AbsFun (Yes ty) _) <- tree2list (M.jments abm), let f' = i2i f]
|
||||||
@@ -66,7 +69,7 @@ mkCType t = case t of
|
|||||||
EInt i -> C.C $ fromInteger i
|
EInt i -> C.C $ fromInteger i
|
||||||
RecType rs -> C.R [mkCType t | (_, t) <- rs]
|
RecType rs -> C.R [mkCType t | (_, t) <- rs]
|
||||||
Table pt vt -> case pt of
|
Table pt vt -> case pt of
|
||||||
EInt i -> C.R $ replicate (fromInteger i) $ mkCType vt
|
EInt i -> C.R $ replicate (1 + fromInteger i) $ mkCType vt
|
||||||
RecType rs -> mkCType $ foldr Table vt (map snd rs)
|
RecType rs -> mkCType $ foldr Table vt (map snd rs)
|
||||||
Sort "Str" -> C.S [] --- Str only
|
Sort "Str" -> C.S [] --- Str only
|
||||||
_ -> error $ "mkCType " ++ show t
|
_ -> error $ "mkCType " ++ show t
|
||||||
@@ -150,7 +153,7 @@ canon2canon abs = recollect . map cl2cl . repartition abs . purgeGrammar abs
|
|||||||
where
|
where
|
||||||
recollect =
|
recollect =
|
||||||
M.MGrammar . nubBy (\ (i,_) (j,_) -> i==j) . concatMap M.modules
|
M.MGrammar . nubBy (\ (i,_) (j,_) -> i==j) . concatMap M.modules
|
||||||
cl2cl cg = tr $ M.MGrammar $ map c2c $ M.modules cg where
|
cl2cl cg = {- tr $ -} M.MGrammar $ map c2c $ M.modules cg where
|
||||||
c2c (c,m) = case m of
|
c2c (c,m) = case m of
|
||||||
M.ModMod mo@(M.Module _ _ _ _ _ js) ->
|
M.ModMod mo@(M.Module _ _ _ _ _ js) ->
|
||||||
(c, M.ModMod $ M.replaceJudgements mo $ mapTree j2j js)
|
(c, M.ModMod $ M.replaceJudgements mo $ mapTree j2j js)
|
||||||
@@ -202,7 +205,7 @@ paramValues cgr = (labels,untyps,typs) where
|
|||||||
]
|
]
|
||||||
typsFrom ty = case ty of
|
typsFrom ty = case ty of
|
||||||
Table p t -> typsFrom p ++ typsFrom t
|
Table p t -> typsFrom p ++ typsFrom t
|
||||||
RecType ls -> RecType (unlockTyp ls) : concat [typsFrom t | (_, t) <- ls]
|
RecType ls -> RecType (sort (unlockTyp ls)) : concat [typsFrom t | (_, t) <- ls]
|
||||||
_ -> [ty]
|
_ -> [ty]
|
||||||
|
|
||||||
typsFromTrm :: Term -> STM [Type] Term
|
typsFromTrm :: Term -> STM [Type] Term
|
||||||
@@ -210,6 +213,8 @@ paramValues cgr = (labels,untyps,typs) where
|
|||||||
V ty ts -> updateSTM (ty:) >> mapM_ typsFromTrm ts >> return tr
|
V ty ts -> updateSTM (ty:) >> mapM_ typsFromTrm ts >> return tr
|
||||||
T (TTyped ty) cs ->
|
T (TTyped ty) cs ->
|
||||||
updateSTM (ty:) >> mapM_ typsFromTrm [t | (_, t) <- cs] >> return tr
|
updateSTM (ty:) >> mapM_ typsFromTrm [t | (_, t) <- cs] >> return tr
|
||||||
|
T (TComp ty) cs ->
|
||||||
|
updateSTM (ty:) >> mapM_ typsFromTrm [t | (_, t) <- cs] >> return tr
|
||||||
_ -> GM.composOp typsFromTrm tr
|
_ -> GM.composOp typsFromTrm tr
|
||||||
|
|
||||||
jments =
|
jments =
|
||||||
@@ -244,7 +249,7 @@ type2type cgr env@(labels,untyps,typs) ty = case ty of
|
|||||||
_ -> ty
|
_ -> ty
|
||||||
where
|
where
|
||||||
t2t = type2type cgr env
|
t2t = type2type cgr env
|
||||||
look ty = EInt $ toInteger $ case Map.lookup ty typs of
|
look ty = EInt $ (+ (-1)) $ toInteger $ case Map.lookup ty typs of
|
||||||
Just vs -> length $ Map.assocs vs
|
Just vs -> length $ Map.assocs vs
|
||||||
_ -> trace ("unknown partype " ++ show ty) 66669
|
_ -> trace ("unknown partype " ++ show ty) 66669
|
||||||
|
|
||||||
@@ -253,12 +258,13 @@ term2term cgr env@(labels,untyps,typs) tr = case tr of
|
|||||||
App _ _ -> mkValCase tr
|
App _ _ -> mkValCase tr
|
||||||
QC _ _ -> mkValCase tr
|
QC _ _ -> mkValCase tr
|
||||||
R rs -> R [(mkLab i, (Nothing, t2t t)) |
|
R rs -> R [(mkLab i, (Nothing, t2t t)) |
|
||||||
(i,(l,(_,t))) <- zip [0..] (unlock rs)]
|
(i,(l,(_,t))) <- zip [0..] (sort (unlock rs))]
|
||||||
P t l -> r2r tr
|
P t l -> r2r tr
|
||||||
PI t l i -> EInt $ toInteger i
|
PI t l i -> EInt $ toInteger i
|
||||||
T (TTyped ty) cs -> mkCurry $ V ty [t2t t | (_, t) <- cs]
|
T (TComp ty) cs -> t2t $ V ty $ map snd cs ---- should be elim'ed in tc
|
||||||
|
T (TTyped ty) cs -> t2t $ V ty $ map snd cs ---- should be elim'ed in tc
|
||||||
V ty ts -> mkCurry $ V ty [t2t t | t <- ts]
|
V ty ts -> mkCurry $ V ty [t2t t | t <- ts]
|
||||||
S t p -> S (t2t t) (t2t p)
|
S t p -> mkCurrySel (t2t t) (t2t p)
|
||||||
_ -> GM.composSafeOp t2t tr
|
_ -> GM.composSafeOp t2t tr
|
||||||
where
|
where
|
||||||
t2t = term2term cgr env
|
t2t = term2term cgr env
|
||||||
@@ -321,9 +327,9 @@ term2term cgr env@(labels,untyps,typs) tr = case tr of
|
|||||||
Just v -> EInt v
|
Just v -> EInt v
|
||||||
_ -> valNumFV $ tryVar tr
|
_ -> valNumFV $ tryVar tr
|
||||||
_ -> valNumFV $ tryVar tr
|
_ -> valNumFV $ tryVar tr
|
||||||
tryVar tr = case tr of
|
tryVar tr = case GM.appForm tr of
|
||||||
----- Par c ts -> [Par c ts' | ts' <- combinations (map tryVar ts)]
|
---(c, ts) -> [ts' | ts' <- combinations (map tryVar ts)]
|
||||||
FV ts -> ts
|
(FV ts,_) -> ts
|
||||||
_ -> [tr]
|
_ -> [tr]
|
||||||
valNumFV ts = case ts of
|
valNumFV ts = case ts of
|
||||||
[tr] -> EInt 66667 ----K (KS (A.prt tr +++ prtTrace tr "66667"))
|
[tr] -> EInt 66667 ----K (KS (A.prt tr +++ prtTrace tr "66667"))
|
||||||
@@ -332,7 +338,8 @@ term2term cgr env@(labels,untyps,typs) tr = case tr of
|
|||||||
mkCurry trm = case trm of
|
mkCurry trm = case trm of
|
||||||
V (RecType [(_,ty)]) ts -> V ty ts
|
V (RecType [(_,ty)]) ts -> V ty ts
|
||||||
V (RecType ((_,ty):ltys)) ts ->
|
V (RecType ((_,ty):ltys)) ts ->
|
||||||
V ty [mkCurry (V (RecType ltys) cs) | cs <- chop (lengthtyp ty) ts]
|
V ty [mkCurry (V (RecType ltys) cs) |
|
||||||
|
cs <- chop (product (map (lengthtyp . snd) ltys)) ts]
|
||||||
_ -> trm
|
_ -> trm
|
||||||
lengthtyp ty = case Map.lookup ty typs of
|
lengthtyp ty = case Map.lookup ty typs of
|
||||||
Just m -> length (Map.assocs m)
|
Just m -> length (Map.assocs m)
|
||||||
@@ -342,6 +349,9 @@ term2term cgr env@(labels,untyps,typs) tr = case tr of
|
|||||||
(xs1,xs2) -> xs1:chop i xs2
|
(xs1,xs2) -> xs1:chop i xs2
|
||||||
|
|
||||||
|
|
||||||
|
mkCurrySel t p = S t p ----
|
||||||
|
|
||||||
|
|
||||||
mkLab k = LIdent (("_" ++ show k))
|
mkLab k = LIdent (("_" ++ show k))
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user