create lin/lincat can now fetch the definitions from the source grammar

This commit is contained in:
Krasimir Angelov
2024-02-08 15:14:05 +01:00
parent ab30f1f9e5
commit 3b4f12e621
3 changed files with 46 additions and 28 deletions

View File

@@ -17,8 +17,8 @@ data TransactionCommand
= CreateFun [Option] Fun Type = CreateFun [Option] Fun Type
| CreateCat [Option] Cat [Hypo] | CreateCat [Option] Cat [Hypo]
| CreateConcrete [Option] ConcName | CreateConcrete [Option] ConcName
| CreateLin [Option] Fun Term Bool | CreateLin [Option] Fun (Maybe Term) Bool
| CreateLincat [Option] Cat Term | CreateLincat [Option] Cat (Maybe Term)
| DropFun [Option] Fun | DropFun [Option] Fun
| DropCat [Option] Cat | DropCat [Option] Cat
| DropConcrete [Option] ConcName | DropConcrete [Option] ConcName

View File

@@ -84,6 +84,7 @@ pTransactionCommand = do
return (DropConcrete opts name) return (DropConcrete opts name)
"lin" | elem (take 1 cmd) ["c","a"] -> do "lin" | elem (take 1 cmd) ["c","a"] -> do
f <- pIdent f <- pIdent
body <- option Nothing $ do
skipSpaces skipSpaces
args <- sepBy pIdent skipSpaces args <- sepBy pIdent skipSpaces
skipSpaces skipSpaces
@@ -92,20 +93,23 @@ pTransactionCommand = do
t <- readS_to_P (\s -> case runPartial pTerm s of t <- readS_to_P (\s -> case runPartial pTerm s of
Right (s,t) -> [(t,s)] Right (s,t) -> [(t,s)]
_ -> []) _ -> [])
return (CreateLin opts f (foldr (Abs Explicit . identS) t args) (take 1 cmd == "a")) return (Just (foldr (Abs Explicit . identS) t args))
return (CreateLin opts f body (take 1 cmd == "a"))
| take 1 cmd == "d" -> do | take 1 cmd == "d" -> do
f <- pIdent f <- pIdent
return (DropLin opts f) return (DropLin opts f)
"lincat" "lincat"
| take 1 cmd == "c" -> do | take 1 cmd == "c" -> do
f <- pIdent f <- pIdent
body <- option Nothing $ do
skipSpaces skipSpaces
char '=' char '='
skipSpaces skipSpaces
t <- readS_to_P (\s -> case runPartial pTerm s of t <- readS_to_P (\s -> case runPartial pTerm s of
Right (s,t) -> [(t,s)] Right (s,t) -> [(t,s)]
_ -> []) _ -> [])
return (CreateLincat opts f t) return (Just t)
return (CreateLincat opts f body)
| take 1 cmd == "d" -> do | take 1 cmd == "d" -> do
f <- pIdent f <- pIdent
return (DropLincat opts f) return (DropLincat opts f)

View File

@@ -14,10 +14,12 @@ import GF.Command.Abstract
import GF.Command.Parse(readCommandLine,pCommand,readTransactionCommand) import GF.Command.Parse(readCommandLine,pCommand,readTransactionCommand)
import GF.Compile.Rename(renameSourceTerm) import GF.Compile.Rename(renameSourceTerm)
import GF.Compile.TypeCheck.Concrete(inferLType) import GF.Compile.TypeCheck.Concrete(inferLType)
import GF.Compile.Compute.Concrete(normalForm,stdPredef,Globals(..))
import GF.Compile.GeneratePMCFG(pmcfgForm,type2fields) import GF.Compile.GeneratePMCFG(pmcfgForm,type2fields)
import GF.Data.Operations (Err(..)) import GF.Data.Operations (Err(..))
import GF.Data.Utilities(whenM,repeatM) import GF.Data.Utilities(whenM,repeatM)
import GF.Grammar hiding (Ident,isPrefixOf) import GF.Grammar hiding (Ident,isPrefixOf)
import GF.Grammar.Lookup(lookupResDef)
import GF.Infra.UseIO(ioErrorText,putStrLnE) import GF.Infra.UseIO(ioErrorText,putStrLnE)
import GF.Infra.SIO import GF.Infra.SIO
import GF.Infra.Option import GF.Infra.Option
@@ -282,7 +284,7 @@ transactionCommand (CreateCat opts c ctxt) pgf mb_txnid = do
transactionCommand (CreateConcrete opts name) pgf mb_txnid = do transactionCommand (CreateConcrete opts name) pgf mb_txnid = do
lift $ updatePGF pgf mb_txnid (createConcrete name (return ())) lift $ updatePGF pgf mb_txnid (createConcrete name (return ()))
return () return ()
transactionCommand (CreateLin opts f t is_alter) pgf mb_txnid = do transactionCommand (CreateLin opts f mb_t is_alter) pgf mb_txnid = do
sgr <- getGrammar sgr <- getGrammar
mo <- case greatestResource sgr of mo <- case greatestResource sgr of
Nothing -> fail "No source grammar in scope" Nothing -> fail "No source grammar in scope"
@@ -295,7 +297,7 @@ transactionCommand (CreateLin opts f t is_alter) pgf mb_txnid = do
alterConcrete lang $ do alterConcrete lang $ do
mb_fields <- getCategoryFields cat mb_fields <- getCategoryFields cat
case mb_fields of case mb_fields of
Just fields -> case runCheck (compileLinTerm sgr mo t (type2term mo ty)) of Just fields -> case runCheck (compileLinTerm sgr mo f mb_t (type2term mo ty)) of
Ok ((prods,seqtbl,fields'),_) Ok ((prods,seqtbl,fields'),_)
| fields == fields' -> do | fields == fields' -> do
(if is_alter then alterLin else createLin) f prods seqtbl (if is_alter then alterLin else createLin) f prods seqtbl
@@ -310,29 +312,41 @@ transactionCommand (CreateLin opts f t is_alter) pgf mb_txnid = do
(Vr (identS cat)) (Vr (identS cat))
hypos hypos
compileLinTerm sgr mo t ty = do compileLinTerm sgr mo f mb_t ty = do
t <- renameSourceTerm sgr mo (Typed t ty) (t,ty) <- case mb_t of
Just t -> do t <- renameSourceTerm sgr mo (Typed t ty)
(t,ty) <- inferLType sgr [] t (t,ty) <- inferLType sgr [] t
return (t,ty)
Nothing -> case lookupResDef sgr (mo,identS f) of
Ok t -> do ty <- renameSourceTerm sgr mo ty
ty <- normalForm (Gl sgr stdPredef) ty
return (t,ty)
Bad msg -> fail msg
let (ctxt,res_ty) = typeFormCnc ty let (ctxt,res_ty) = typeFormCnc ty
(prods,seqs) <- pmcfgForm sgr t ctxt res_ty Map.empty (prods,seqs) <- pmcfgForm sgr t ctxt res_ty Map.empty
return (prods,mapToSequence seqs,type2fields sgr res_ty) return (prods,mapToSequence seqs,type2fields sgr res_ty)
where where
mapToSequence m = Seq.fromList (map (Left . fst) (sortOn snd (Map.toList m))) mapToSequence m = Seq.fromList (map (Left . fst) (sortOn snd (Map.toList m)))
transactionCommand (CreateLincat opts c t) pgf mb_txnid = do transactionCommand (CreateLincat opts c mb_t) pgf mb_txnid = do
sgr <- getGrammar sgr <- getGrammar
mo <- case greatestResource sgr of mo <- case greatestResource sgr of
Nothing -> fail "No source grammar in scope" Nothing -> fail "No source grammar in scope"
Just mo -> return mo Just mo -> return mo
lang <- optLang pgf opts lang <- optLang pgf opts
case runCheck (compileLincatTerm sgr mo t) of case runCheck (compileLincatTerm sgr mo mb_t) of
Ok (fields,_)-> do lift $ updatePGF pgf mb_txnid (alterConcrete lang (createLincat c fields [] [] Seq.empty >> return ())) Ok (fields,_)-> do lift $ updatePGF pgf mb_txnid (alterConcrete lang (createLincat c fields [] [] Seq.empty >> return ()))
return () return ()
Bad msg -> fail msg Bad msg -> fail msg
where where
compileLincatTerm sgr mo t = do compileLincatTerm sgr mo mb_t = do
t <- renameSourceTerm sgr mo t t <- case mb_t of
Just t -> do t <- renameSourceTerm sgr mo t
(t,_) <- inferLType sgr [] t (t,_) <- inferLType sgr [] t
return t
Nothing -> case lookupResDef sgr (mo,identS c) of
Ok t -> return t
Bad msg -> fail msg
return (type2fields sgr t) return (type2fields sgr t)
transactionCommand (DropFun opts f) pgf mb_txnid = do transactionCommand (DropFun opts f) pgf mb_txnid = do
lift $ updatePGF pgf mb_txnid (dropFunction f) lift $ updatePGF pgf mb_txnid (dropFunction f)