mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-23 19:42:50 -06:00
partial evaluator work
* Evaluate operators once, not every time they are looked up * Remember the list of parameter values instead of recomputing it from the pattern type every time a table selection is made. * Quick fix for partial application of some predefined functions.
This commit is contained in:
@@ -218,7 +218,7 @@ checkInfo opts sgr (m,mo) c info = do
|
|||||||
(Just (L loct ty), Just (L locd de)) -> do
|
(Just (L loct ty), Just (L locd de)) -> do
|
||||||
ty' <- chIn loct "operation" $
|
ty' <- chIn loct "operation" $
|
||||||
(if False --flag optNewComp opts
|
(if False --flag optNewComp opts
|
||||||
then CN.checkLType gr ty typeType >>= return . CN.normalForm gr . fst
|
then CN.checkLType gr ty typeType >>= return . CN.normalForm (CN.resourceValues gr) . fst -- !!
|
||||||
else checkLType gr [] ty typeType >>= computeLType gr [] . fst)
|
else checkLType gr [] ty typeType >>= computeLType gr [] . fst)
|
||||||
(de',_) <- chIn locd "operation" $
|
(de',_) <- chIn locd "operation" $
|
||||||
(if False -- flag optNewComp opts
|
(if False -- flag optNewComp opts
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
-- | preparation for PMCFG generation.
|
-- | preparation for PMCFG generation.
|
||||||
module GF.Compile.Compute.ConcreteNew
|
module GF.Compile.Compute.ConcreteNew
|
||||||
( normalForm
|
( normalForm
|
||||||
|
, GlobalEnv, resourceValues
|
||||||
, Value(..), Env, eval, apply, value2term
|
, Value(..), Env, eval, apply, value2term
|
||||||
) where
|
) where
|
||||||
|
|
||||||
@@ -9,7 +10,7 @@ import GF.Grammar hiding (Env, VGen, VApp, VRecType)
|
|||||||
import GF.Grammar.Lookup(lookupResDef,allParamValues)
|
import GF.Grammar.Lookup(lookupResDef,allParamValues)
|
||||||
import GF.Grammar.Predef(cPredef,cErrorType,cTok,cStr)
|
import GF.Grammar.Predef(cPredef,cErrorType,cTok,cStr)
|
||||||
import GF.Grammar.PatternMatch(matchPattern)
|
import GF.Grammar.PatternMatch(matchPattern)
|
||||||
import GF.Grammar.Lockfield(unlockRecord,lockLabel,isLockLabel)
|
import GF.Grammar.Lockfield(unlockRecord,lockLabel,isLockLabel,lockRecType)
|
||||||
import GF.Compile.Compute.Value
|
import GF.Compile.Compute.Value
|
||||||
import GF.Compile.Compute.Predef(predefs)
|
import GF.Compile.Compute.Predef(predefs)
|
||||||
import GF.Data.Str(Str,glueStr,str2strings,str,sstr,plusStr,strTok)
|
import GF.Data.Str(Str,glueStr,str2strings,str,sstr,plusStr,strTok)
|
||||||
@@ -25,30 +26,52 @@ import Debug.Trace(trace)
|
|||||||
|
|
||||||
-- * Main entry points
|
-- * Main entry points
|
||||||
|
|
||||||
normalForm :: SourceGrammar -> Term -> Term
|
normalForm :: GlobalEnv -> Term -> Term
|
||||||
normalForm gr = nfx gr []
|
normalForm = nfx . toplevel
|
||||||
nfx gr env = value2term gr [] . eval gr env
|
nfx env = value2term (srcgr env) [] . value env
|
||||||
|
|
||||||
eval :: SourceGrammar -> Env -> Term -> Value
|
eval :: GlobalEnv -> Term -> Value
|
||||||
eval gr env t = value (gr,env) t
|
eval = value . toplevel
|
||||||
|
|
||||||
apply gr env = apply' (gr,env)
|
apply env = apply' env
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
-- * Environments
|
-- * Environments
|
||||||
|
|
||||||
type CompleteEnv = (SourceGrammar,Env)
|
type ResourceValues = Map.Map Ident (Map.Map Ident (Err Value))
|
||||||
|
|
||||||
ext b (gr,env) = (gr,b:env)
|
data GlobalEnv = GE SourceGrammar ResourceValues
|
||||||
|
data CompleteEnv = CE {srcgr::SourceGrammar,rvs::ResourceValues,local::Env}
|
||||||
|
|
||||||
var env x = maybe unbound id (lookup x (snd env))
|
ext b env = env{local=b:local env}
|
||||||
|
extend bs env = env{local=bs++local env}
|
||||||
|
global env = GE (srcgr env) (rvs env)
|
||||||
|
toplevel (GE gr rvs) = CE gr rvs []
|
||||||
|
|
||||||
|
var env x = maybe unbound id (lookup x (local env))
|
||||||
where unbound = bug ("Unknown variable: "++showIdent x)
|
where unbound = bug ("Unknown variable: "++showIdent x)
|
||||||
|
|
||||||
|
resource env (m,c) =
|
||||||
|
err bug id $
|
||||||
|
if isPredefCat c
|
||||||
|
then fmap (value0 env) (lockRecType c defLinType) -- hmm
|
||||||
|
else maybe e id $ Map.lookup c =<< Map.lookup m (rvs env)
|
||||||
|
where e = fail $ "Not found: "++showIdent m++"."++showIdent c
|
||||||
|
|
||||||
|
-- | Convert operators once, not every time they are looked up
|
||||||
|
resourceValues :: SourceGrammar -> GlobalEnv
|
||||||
|
resourceValues gr = env
|
||||||
|
where
|
||||||
|
env = GE gr rvs
|
||||||
|
rvs = Map.mapWithKey moduleResources (moduleMap gr)
|
||||||
|
moduleResources m = Map.mapWithKey (moduleResource m) . jments
|
||||||
|
moduleResource m c _info = fmap (eval env) (lookupResDef gr (m,c))
|
||||||
|
|
||||||
-- * Computing values
|
-- * Computing values
|
||||||
|
|
||||||
-- | Computing the value of a top-level term
|
-- | Computing the value of a top-level term
|
||||||
value0 gr t = eval gr [] t
|
value0 = eval . global
|
||||||
|
|
||||||
-- | Computing the value of a term
|
-- | Computing the value of a term
|
||||||
value :: CompleteEnv -> Term -> Value
|
value :: CompleteEnv -> Term -> Value
|
||||||
@@ -58,13 +81,13 @@ value env t0 =
|
|||||||
Q x@(m,f)
|
Q x@(m,f)
|
||||||
| m == cPredef -> if f==cErrorType -- to be removed
|
| m == cPredef -> if f==cErrorType -- to be removed
|
||||||
then let p = identC (BS.pack "P")
|
then let p = identC (BS.pack "P")
|
||||||
in value0 (fst env) (mkProd [(Implicit,p,typeType)] (Vr p) [])
|
in value0 env (mkProd [(Implicit,p,typeType)] (Vr p) [])
|
||||||
else VApp x []
|
else VApp x []
|
||||||
| otherwise -> valueResDef (fst env) x
|
| otherwise -> resource env x --valueResDef (fst env) x
|
||||||
QC x -> VCApp x []
|
QC x -> VCApp x []
|
||||||
App e1 e2 -> apply' env e1 [value env e2]
|
App e1 e2 -> apply' env e1 [value env e2]
|
||||||
Let (x,(oty,t)) body -> value (ext (x,value env t) env) body
|
Let (x,(oty,t)) body -> value (ext (x,value env t) env) body
|
||||||
Meta i -> VMeta i (snd env) []
|
Meta i -> VMeta i (local env) []
|
||||||
Prod bt x t1 t2 -> VProd bt (value env t1) x (Bind $ \ vx -> value (ext (x,vx) env) t2)
|
Prod bt x t1 t2 -> VProd bt (value env t1) x (Bind $ \ vx -> value (ext (x,vx) env) t2)
|
||||||
Abs bt x t -> VAbs bt x (Bind $ \ vx -> value (ext (x,vx) env) t)
|
Abs bt x t -> VAbs bt x (Bind $ \ vx -> value (ext (x,vx) env) t)
|
||||||
EInt n -> VInt n
|
EInt n -> VInt n
|
||||||
@@ -80,9 +103,9 @@ value env t0 =
|
|||||||
FV ts -> vfv (map (value env) ts)
|
FV ts -> vfv (map (value env) ts)
|
||||||
R as -> VRec [(lbl,value env t)|(lbl,(oty,t))<-as]
|
R as -> VRec [(lbl,value env t)|(lbl,(oty,t))<-as]
|
||||||
T i cs -> valueTable env i cs
|
T i cs -> valueTable env i cs
|
||||||
V ty ts -> VV ty (map (value env) ts)
|
V ty ts -> VV ty (paramValues env ty) (map (value env) ts)
|
||||||
C t1 t2 -> vconcat (both (value env) (t1,t2))
|
C t1 t2 -> vconcat (both (value env) (t1,t2))
|
||||||
S t1 t2 -> select (fst env) (both (value env) (t1,t2))
|
S t1 t2 -> select env (both (value env) (t1,t2))
|
||||||
P t l -> --maybe (bug $ "project "++show l++" from "++show v) id $
|
P t l -> --maybe (bug $ "project "++show l++" from "++show v) id $
|
||||||
maybe (VP v l) id $
|
maybe (VP v l) id $
|
||||||
proj l v where v = (value env t)
|
proj l v where v = (value env t)
|
||||||
@@ -93,7 +116,11 @@ value env t0 =
|
|||||||
EPatt p -> VPatt p -- hmm
|
EPatt p -> VPatt p -- hmm
|
||||||
t -> ppbug (text "value"<+>ppTerm Unqualified 10 t $$ text (show t))
|
t -> ppbug (text "value"<+>ppTerm Unqualified 10 t $$ text (show t))
|
||||||
|
|
||||||
valueResDef gr = err bug (value0 gr) . lookupResDef gr
|
--valueResDef gr = err bug (value0 gr) . lookupResDef gr
|
||||||
|
|
||||||
|
paramValues env ty = let pty = nfx env ty
|
||||||
|
ats = err bug id $ allParamValues (srcgr env) pty
|
||||||
|
in map (value0 env) ats
|
||||||
|
|
||||||
vconcat vv@(v1,v2) =
|
vconcat vv@(v1,v2) =
|
||||||
case vv of
|
case vv of
|
||||||
@@ -145,7 +172,7 @@ extR t vv =
|
|||||||
ls -> error $ text "clash"<+>text (show ls)
|
ls -> error $ text "clash"<+>text (show ls)
|
||||||
(VRec rs1, VRec rs2) -> plusVRec rs1 rs2
|
(VRec rs1, VRec rs2) -> plusVRec rs1 rs2
|
||||||
(v1 , VRec [(l,_)]) | isLockLabel l -> v1 -- hmm
|
(v1 , VRec [(l,_)]) | isLockLabel l -> v1 -- hmm
|
||||||
(VS (VV t vs) s,v2) -> VS (VV t [extR t (v1,v2)|v1<-vs]) s
|
(VS (VV t pvs vs) s,v2) -> VS (VV t pvs [extR t (v1,v2)|v1<-vs]) s
|
||||||
(v1,v2) -> ok2 VExtR v1 v2 -- hmm
|
(v1,v2) -> ok2 VExtR v1 v2 -- hmm
|
||||||
-- (v1,v2) -> error $ text "not records" $$ text (show v1) $$ text (show v2)
|
-- (v1,v2) -> error $ text "not records" $$ text (show v1) $$ text (show v2)
|
||||||
where
|
where
|
||||||
@@ -166,8 +193,8 @@ glue vv = case vv of
|
|||||||
return $ vfv [foldr1 VC (map VString (str2strings (glueStr v u))) | v <- x', u <- y']
|
return $ vfv [foldr1 VC (map VString (str2strings (glueStr v u))) | v <- x', u <- y']
|
||||||
(VC va vb,v2) -> VC va (glue (vb,v2))
|
(VC va vb,v2) -> VC va (glue (vb,v2))
|
||||||
(v1,VC va vb) -> VC (glue (va,va)) vb
|
(v1,VC va vb) -> VC (glue (va,va)) vb
|
||||||
(VS (VV ty vs) vb,v2) -> VS (VV ty [glue (v,v2)|v<-vs]) vb
|
(VS (VV ty pvs vs) vb,v2) -> VS (VV ty pvs [glue (v,v2)|v<-vs]) vb
|
||||||
(v1,VS (VV ty vs) vb) -> VS (VV ty [glue (v1,v)|v<-vs]) vb
|
(v1,VS (VV ty pvs vs) vb) -> VS (VV ty pvs [glue (v1,v)|v<-vs]) vb
|
||||||
-- (v1,v2) -> ok2 VGlue v1 v2
|
-- (v1,v2) -> ok2 VGlue v1 v2
|
||||||
(v1,v2) -> bug vv
|
(v1,v2) -> bug vv
|
||||||
where
|
where
|
||||||
@@ -205,43 +232,44 @@ vfv vs = case nub vs of
|
|||||||
[v] -> v
|
[v] -> v
|
||||||
vs -> VFV vs
|
vs -> VFV vs
|
||||||
|
|
||||||
select gr vv =
|
select env vv =
|
||||||
case vv of
|
case vv of
|
||||||
(v1,VFV vs) -> vfv [select gr (v1,v2)|v2<-vs]
|
(v1,VFV vs) -> vfv [select env (v1,v2)|v2<-vs]
|
||||||
(VFV vs,v2) -> vfv [select gr (v1,v2)|v1<-vs]
|
(VFV vs,v2) -> vfv [select env (v1,v2)|v1<-vs]
|
||||||
(v1@(VV pty rs),v2) ->
|
(v1@(VV pty vs rs),v2) ->
|
||||||
err (const (VS v1 v2)) id $
|
err (const (VS v1 v2)) id $
|
||||||
do ats <- allParamValues gr pty
|
do --ats <- allParamValues (srcgr env) pty
|
||||||
let vs = map (value0 gr) ats
|
--let vs = map (value0 env) ats
|
||||||
i <- maybeErr "no match" $ findIndex (==v2) vs
|
i <- maybeErr "no match" $ findIndex (==v2) vs
|
||||||
return (rs!!i)
|
return (rs!!i)
|
||||||
(v1@(VT i cs),v2) ->
|
(v1@(VT i cs),v2) ->
|
||||||
err bug (valueMatch gr) $ matchPattern cs (value2term gr [] v2)
|
err bug (valueMatch env) $ matchPattern cs (value2term (srcgr env) [] v2)
|
||||||
(VS (VV pty rs) v12,v2) -> VS (VV pty [select gr (v11,v2)|v11<-rs]) v12
|
(VS (VV pty pvs rs) v12,v2) -> VS (VV pty pvs [select env (v11,v2)|v11<-rs]) v12
|
||||||
(v1,v2) -> ok2 VS v1 v2
|
(v1,v2) -> ok2 VS v1 v2
|
||||||
|
|
||||||
valueMatch gr (Bind f,env') = f (mapSnd (value0 gr) env')
|
valueMatch env (Bind f,env') = f (mapSnd (value0 env) env')
|
||||||
|
|
||||||
valueTable env@(gr,bs) i cs =
|
valueTable env i cs =
|
||||||
case i of
|
case i of
|
||||||
TComp ty -> VV ty (map (value env.snd) cs)
|
TComp ty -> VV ty (paramValues env ty) (map (value env.snd) cs)
|
||||||
_ -> err keep id convert
|
_ -> err keep id convert
|
||||||
where
|
where
|
||||||
keep _ = VT i (err bug id $ mapM valueCase cs)
|
keep _ = VT i (err bug id $ mapM valueCase cs)
|
||||||
|
|
||||||
valueCase (p,t) = do p' <- inlinePattMacro p
|
valueCase (p,t) = do p' <- inlinePattMacro p
|
||||||
return (p',Bind $ \ bs' -> value (gr,bs'++bs) t)
|
return (p',Bind $ \ bs' -> value (extend bs' env) t)
|
||||||
|
--{-
|
||||||
convert = do ty <- getTableType i
|
convert = do ty <- getTableType i
|
||||||
let pty = nfx gr bs ty
|
let pty = nfx env ty
|
||||||
vs <- allParamValues gr pty
|
vs <- allParamValues (srcgr env) pty
|
||||||
|
let pvs = map (value0 env) vs
|
||||||
cs' <- mapM valueCase cs
|
cs' <- mapM valueCase cs
|
||||||
sts <- mapM (matchPattern cs') vs
|
sts <- mapM (matchPattern cs') vs
|
||||||
return $ VV pty (map (valueMatch gr) sts)
|
return $ VV pty pvs (map (valueMatch env) sts)
|
||||||
|
--}
|
||||||
inlinePattMacro p =
|
inlinePattMacro p =
|
||||||
case p of
|
case p of
|
||||||
PM qc -> case valueResDef gr qc of
|
PM qc -> case resource env qc of
|
||||||
VPatt p' -> inlinePattMacro p'
|
VPatt p' -> inlinePattMacro p'
|
||||||
r -> ppbug $ hang (text "Expected pattern macro:") 4
|
r -> ppbug $ hang (text "Expected pattern macro:") 4
|
||||||
(text (show r))
|
(text (show r))
|
||||||
@@ -254,8 +282,7 @@ apply' env t vs =
|
|||||||
Q x@(m,f) | m==cPredef -> let constr = --trace ("predef "++show x) .
|
Q x@(m,f) | m==cPredef -> let constr = --trace ("predef "++show x) .
|
||||||
VApp x
|
VApp x
|
||||||
in maybe constr id (Map.lookup f predefs) vs
|
in maybe constr id (Map.lookup f predefs) vs
|
||||||
| otherwise -> err bug (\t->apply' (fst env,[]) t vs)
|
| otherwise -> vapply (resource env x) vs
|
||||||
(lookupResDef (fst env) x)
|
|
||||||
App t1 t2 -> apply' env t1 (value env t2 : vs)
|
App t1 t2 -> apply' env t1 (value env t2 : vs)
|
||||||
-- Abs b x t -> beta env b x t vs
|
-- Abs b x t -> beta env b x t vs
|
||||||
_ -> vapply (value env t) vs
|
_ -> vapply (value env t) vs
|
||||||
@@ -266,7 +293,7 @@ vapply v vs =
|
|||||||
VError {} -> v
|
VError {} -> v
|
||||||
-- VClosure env (Abs b x t) -> beta gr env b x t vs
|
-- VClosure env (Abs b x t) -> beta gr env b x t vs
|
||||||
VAbs bt _ (Bind f) -> vbeta bt f vs
|
VAbs bt _ (Bind f) -> vbeta bt f vs
|
||||||
VS (VV t fs) s -> VS (VV t [vapply f vs|f<-fs]) s
|
VS (VV t pvs fs) s -> VS (VV t pvs [vapply f vs|f<-fs]) s
|
||||||
VFV fs -> vfv [vapply f vs|f<-fs]
|
VFV fs -> vfv [vapply f vs|f<-fs]
|
||||||
v -> bug $ "vapply "++show v++" "++show vs
|
v -> bug $ "vapply "++show v++" "++show vs
|
||||||
|
|
||||||
@@ -308,7 +335,7 @@ value2term gr xs v0 =
|
|||||||
VTblType p res -> Table (v2t p) (v2t res)
|
VTblType p res -> Table (v2t p) (v2t res)
|
||||||
VRecType rs -> RecType [(l,v2t v) | (l,v) <- rs]
|
VRecType rs -> RecType [(l,v2t v) | (l,v) <- rs]
|
||||||
VRec as -> R [(l,(Nothing,v2t v))|(l,v) <- as]
|
VRec as -> R [(l,(Nothing,v2t v))|(l,v) <- as]
|
||||||
VV t vs -> V t (map v2t vs)
|
VV t _ vs -> V t (map v2t vs)
|
||||||
VT i cs -> T i (map nfcase cs)
|
VT i cs -> T i (map nfcase cs)
|
||||||
VFV vs -> FV (map v2t vs)
|
VFV vs -> FV (map v2t vs)
|
||||||
VC v1 v2 -> C (v2t v1) (v2t v2)
|
VC v1 v2 -> C (v2t v1) (v2t v2)
|
||||||
|
|||||||
@@ -9,8 +9,9 @@ import Data.Char (isUpper,toLower,toUpper)
|
|||||||
import GF.Data.Utilities (mapSnd,apBoth)
|
import GF.Data.Utilities (mapSnd,apBoth)
|
||||||
|
|
||||||
import GF.Compile.Compute.Value
|
import GF.Compile.Compute.Value
|
||||||
import GF.Infra.Ident (Ident)
|
import GF.Infra.Ident (Ident,varX)
|
||||||
import GF.Grammar.Predef
|
import GF.Grammar.Predef
|
||||||
|
import PGF.Data(BindType(..))
|
||||||
|
|
||||||
predefs :: Map.Map Ident ([Value]->Value)
|
predefs :: Map.Map Ident ([Value]->Value)
|
||||||
predefs = Map.fromList $ mapSnd strictf
|
predefs = Map.fromList $ mapSnd strictf
|
||||||
@@ -40,6 +41,10 @@ predefs = Map.fromList $ mapSnd strictf
|
|||||||
|
|
||||||
apISS f vs = case vs of
|
apISS f vs = case vs of
|
||||||
[VInt i, VString s] -> string (f i s)
|
[VInt i, VString s] -> string (f i s)
|
||||||
|
[VInt i] -> VAbs Explicit (varX 0) $ Bind $ \ v ->
|
||||||
|
case norm v of
|
||||||
|
VString s -> string (f i s)
|
||||||
|
_ -> bug $ "f::Int->Str->Str got "++show (vs++[v])
|
||||||
_ -> bug $ "f::Int->Str->Str got "++show vs
|
_ -> bug $ "f::Int->Str->Str got "++show vs
|
||||||
|
|
||||||
apSSB f vs = case vs of
|
apSSB f vs = case vs of
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ data Value
|
|||||||
| VTblType Value Value
|
| VTblType Value Value
|
||||||
| VRecType [(Label,Value)]
|
| VRecType [(Label,Value)]
|
||||||
| VRec [(Label,Value)]
|
| VRec [(Label,Value)]
|
||||||
| VV Type [Value]
|
| VV Type [Value] [Value] -- preserve type for conversion back to Term
|
||||||
| VT TInfo [(Patt,Bind Env)]
|
| VT TInfo [(Patt,Bind Env)]
|
||||||
| VC Value Value
|
| VC Value Value
|
||||||
| VS Value Value
|
| VS Value Value
|
||||||
|
|||||||
@@ -10,7 +10,7 @@
|
|||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
|
|
||||||
module GF.Compile.GeneratePMCFG
|
module GF.Compile.GeneratePMCFG
|
||||||
(generatePMCFG, pgfCncCat, addPMCFG
|
(generatePMCFG, pgfCncCat, addPMCFG, resourceValues
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import PGF.CId
|
import PGF.CId
|
||||||
@@ -23,7 +23,7 @@ import GF.Grammar.Predef
|
|||||||
import GF.Data.BacktrackM
|
import GF.Data.BacktrackM
|
||||||
import GF.Data.Operations
|
import GF.Data.Operations
|
||||||
import GF.Data.Utilities (updateNthM, updateNth)
|
import GF.Data.Utilities (updateNthM, updateNth)
|
||||||
import GF.Compile.Compute.ConcreteNew(normalForm)
|
import GF.Compile.Compute.ConcreteNew(GlobalEnv,normalForm,resourceValues)
|
||||||
import System.IO(hPutStr,hPutStrLn,stderr)
|
import System.IO(hPutStr,hPutStrLn,stderr)
|
||||||
import qualified Data.Map as Map
|
import qualified Data.Map as Map
|
||||||
import qualified Data.Set as Set
|
import qualified Data.Set as Set
|
||||||
@@ -45,10 +45,11 @@ import Control.Exception
|
|||||||
|
|
||||||
generatePMCFG :: Options -> SourceGrammar -> SourceModule -> IO SourceModule
|
generatePMCFG :: Options -> SourceGrammar -> SourceModule -> IO SourceModule
|
||||||
generatePMCFG opts sgr cmo@(cm,cmi) = do
|
generatePMCFG opts sgr cmo@(cm,cmi) = do
|
||||||
(seqs,js) <- mapAccumWithKeyM (addPMCFG opts gr am cm) Map.empty (jments cmi)
|
(seqs,js) <- mapAccumWithKeyM (addPMCFG opts gr cenv am cm) Map.empty (jments cmi)
|
||||||
when (verbAtLeast opts Verbose) $ hPutStrLn stderr ""
|
when (verbAtLeast opts Verbose) $ hPutStrLn stderr ""
|
||||||
return (cm,cmi{mseqs = Just (mkSetArray seqs), jments = js})
|
return (cm,cmi{mseqs = Just (mkSetArray seqs), jments = js})
|
||||||
where
|
where
|
||||||
|
cenv = resourceValues gr
|
||||||
gr = prependModule sgr cmo
|
gr = prependModule sgr cmo
|
||||||
MTConcrete am = mtype cmi
|
MTConcrete am = mtype cmi
|
||||||
|
|
||||||
@@ -64,14 +65,14 @@ mapAccumWithKeyM f a m = do let xs = Map.toAscList m
|
|||||||
return (a,(k,y):kys)
|
return (a,(k,y):kys)
|
||||||
|
|
||||||
|
|
||||||
addPMCFG :: Options -> SourceGrammar -> Ident -> Ident -> SeqSet -> Ident -> Info -> IO (SeqSet, Info)
|
addPMCFG :: Options -> SourceGrammar -> GlobalEnv -> Ident -> Ident -> SeqSet -> Ident -> Info -> IO (SeqSet, Info)
|
||||||
addPMCFG opts gr am cm seqs id (GF.Grammar.CncFun mty@(Just (cat,cont,val)) mlin@(Just (L _ term)) mprn Nothing) = do
|
addPMCFG opts gr cenv am cm seqs id (GF.Grammar.CncFun mty@(Just (cat,cont,val)) mlin@(Just (L _ term)) mprn Nothing) = do
|
||||||
let pres = protoFCat gr res val
|
let pres = protoFCat gr res val
|
||||||
pargs = [protoFCat gr (snd $ catSkeleton ty) lincat | ((_,_,ty),(_,_,lincat)) <- zip ctxt cont]
|
pargs = [protoFCat gr (snd $ catSkeleton ty) lincat | ((_,_,ty),(_,_,lincat)) <- zip ctxt cont]
|
||||||
|
|
||||||
pmcfgEnv0 = emptyPMCFGEnv
|
pmcfgEnv0 = emptyPMCFGEnv
|
||||||
|
|
||||||
b = convert opts gr term val pargs
|
b = convert opts gr cenv term val pargs
|
||||||
(seqs1,b1) = addSequencesB seqs b
|
(seqs1,b1) = addSequencesB seqs b
|
||||||
pmcfgEnv1 = foldBM addRule
|
pmcfgEnv1 = foldBM addRule
|
||||||
pmcfgEnv0
|
pmcfgEnv0
|
||||||
@@ -98,13 +99,13 @@ addPMCFG opts gr am cm seqs id (GF.Grammar.CncFun mty@(Just (cat,cont,val)) mlin
|
|||||||
newArgs = map getFIds newArgs'
|
newArgs = map getFIds newArgs'
|
||||||
in addFunction env0 newCat fun newArgs
|
in addFunction env0 newCat fun newArgs
|
||||||
|
|
||||||
addPMCFG opts gr am cm seqs id (GF.Grammar.CncCat mty@(Just (L _ lincat)) mdef@(Just (L _ term)) mprn Nothing) = do
|
addPMCFG opts gr cenv am cm seqs id (GF.Grammar.CncCat mty@(Just (L _ lincat)) mdef@(Just (L _ term)) mprn Nothing) = do
|
||||||
let pres = protoFCat gr (am,id) lincat
|
let pres = protoFCat gr (am,id) lincat
|
||||||
parg = protoFCat gr (identW,cVar) typeStr
|
parg = protoFCat gr (identW,cVar) typeStr
|
||||||
|
|
||||||
pmcfgEnv0 = emptyPMCFGEnv
|
pmcfgEnv0 = emptyPMCFGEnv
|
||||||
|
|
||||||
b = convert opts gr term lincat [parg]
|
b = convert opts gr cenv term lincat [parg]
|
||||||
(seqs1,b1) = addSequencesB seqs b
|
(seqs1,b1) = addSequencesB seqs b
|
||||||
pmcfgEnv1 = foldBM addRule
|
pmcfgEnv1 = foldBM addRule
|
||||||
pmcfgEnv0
|
pmcfgEnv0
|
||||||
@@ -119,14 +120,14 @@ addPMCFG opts gr am cm seqs id (GF.Grammar.CncCat mty@(Just (L _ lincat)) mdef@(
|
|||||||
!fun = mkArray lins
|
!fun = mkArray lins
|
||||||
in addFunction env0 newCat fun [[fidVar]]
|
in addFunction env0 newCat fun [[fidVar]]
|
||||||
|
|
||||||
addPMCFG opts gr am cm seqs id info = return (seqs, info)
|
addPMCFG opts gr cenv am cm seqs id info = return (seqs, info)
|
||||||
|
|
||||||
convert opts gr term val pargs =
|
convert opts gr cenv term val pargs =
|
||||||
runCnvMonad gr conv (pargs,[])
|
runCnvMonad gr conv (pargs,[])
|
||||||
where
|
where
|
||||||
conv = convertTerm opts CNil val =<< unfactor term'
|
conv = convertTerm opts CNil val =<< unfactor cenv term'
|
||||||
term' = if flag optNewComp opts
|
term' = if flag optNewComp opts
|
||||||
then normalForm gr (recordExpand val term) -- new evaluator
|
then normalForm cenv (recordExpand val term) -- new evaluator
|
||||||
else term -- old evaluator is invoked from GF.Compile.Optimize
|
else term -- old evaluator is invoked from GF.Compile.Optimize
|
||||||
|
|
||||||
recordExpand :: Type -> Term -> Term
|
recordExpand :: Type -> Term -> Term
|
||||||
@@ -142,8 +143,8 @@ recordExpand typ trm =
|
|||||||
_ -> R [assign lab (P trm lab) | (lab,_) <- tys]
|
_ -> R [assign lab (P trm lab) | (lab,_) <- tys]
|
||||||
_ -> trm
|
_ -> trm
|
||||||
|
|
||||||
unfactor :: Term -> CnvMonad Term
|
unfactor :: GlobalEnv -> Term -> CnvMonad Term
|
||||||
unfactor t = CM (\gr c -> c (unfac gr t))
|
unfactor cenv t = CM (\gr c -> c (unfac gr t))
|
||||||
where
|
where
|
||||||
unfac gr t =
|
unfac gr t =
|
||||||
case t of
|
case t of
|
||||||
|
|||||||
@@ -38,11 +38,13 @@ import Control.Monad.Identity
|
|||||||
|
|
||||||
mkCanon2pgf :: Options -> SourceGrammar -> Ident -> IO D.PGF
|
mkCanon2pgf :: Options -> SourceGrammar -> Ident -> IO D.PGF
|
||||||
mkCanon2pgf opts gr am = do
|
mkCanon2pgf opts gr am = do
|
||||||
(an,abs) <- mkAbstr gr am
|
(an,abs) <- mkAbstr am
|
||||||
cncs <- mapM (mkConcr gr) (allConcretes gr am)
|
cncs <- mapM mkConcr (allConcretes gr am)
|
||||||
return $ updateProductionIndices (D.PGF Map.empty an abs (Map.fromList cncs))
|
return $ updateProductionIndices (D.PGF Map.empty an abs (Map.fromList cncs))
|
||||||
where
|
where
|
||||||
mkAbstr gr am = return (i2i am, D.Abstr flags funs cats bcode)
|
cenv = resourceValues gr
|
||||||
|
|
||||||
|
mkAbstr am = return (i2i am, D.Abstr flags funs cats bcode)
|
||||||
where
|
where
|
||||||
aflags =
|
aflags =
|
||||||
concatOptions (reverse [mflags mo | (_,mo) <- modules gr, isModAbs mo])
|
concatOptions (reverse [mflags mo | (_,mo) <- modules gr, isModAbs mo])
|
||||||
@@ -64,7 +66,7 @@ mkCanon2pgf opts gr am = do
|
|||||||
(map (\x -> (0,snd x)) . sortBy (compare `on` fst))
|
(map (\x -> (0,snd x)) . sortBy (compare `on` fst))
|
||||||
[(loc,i2i f) | ((m,f),AbsFun (Just (L loc ty)) _ _ (Just True),_) <- adefs, snd (GM.valCat ty) == cat]
|
[(loc,i2i f) | ((m,f),AbsFun (Just (L loc ty)) _ _ (Just True),_) <- adefs, snd (GM.valCat ty) == cat]
|
||||||
|
|
||||||
mkConcr gr cm = do
|
mkConcr cm = do
|
||||||
let cflags = concatOptions [mflags mo | (i,mo) <- modules gr, isModCnc mo,
|
let cflags = concatOptions [mflags mo | (i,mo) <- modules gr, isModCnc mo,
|
||||||
Just r <- [lookup i (allExtendSpecs gr cm)]]
|
Just r <- [lookup i (allExtendSpecs gr cm)]]
|
||||||
|
|
||||||
@@ -96,7 +98,7 @@ mkCanon2pgf opts gr am = do
|
|||||||
-- we have to create the PMCFG code just before linking
|
-- we have to create the PMCFG code just before linking
|
||||||
addMissingPMCFGs seqs [] = return (seqs,[])
|
addMissingPMCFGs seqs [] = return (seqs,[])
|
||||||
addMissingPMCFGs seqs (((m,id), info):is) = do
|
addMissingPMCFGs seqs (((m,id), info):is) = do
|
||||||
(seqs,info) <- addPMCFG opts gr am cm seqs id info
|
(seqs,info) <- addPMCFG opts gr cenv am cm seqs id info
|
||||||
(seqs,is ) <- addMissingPMCFGs seqs is
|
(seqs,is ) <- addMissingPMCFGs seqs is
|
||||||
return (seqs, ((m,id), info) : is)
|
return (seqs, ((m,id), info) : is)
|
||||||
|
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
|
|
||||||
module GF.Grammar.Grammar (
|
module GF.Grammar.Grammar (
|
||||||
SourceGrammar, SourceModInfo(..), SourceModule, ModuleType(..),
|
SourceGrammar, SourceModInfo(..), SourceModule, ModuleType(..),
|
||||||
emptySourceGrammar, mGrammar, modules, prependModule,
|
emptySourceGrammar, mGrammar, modules, prependModule, moduleMap,
|
||||||
|
|
||||||
MInclude (..), OpenSpec(..),
|
MInclude (..), OpenSpec(..),
|
||||||
extends, isInherited, inheritAll,
|
extends, isInherited, inheritAll,
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ import GF.Grammar.ShowTerm
|
|||||||
import GF.Grammar.Lookup (allOpers,allOpersTo)
|
import GF.Grammar.Lookup (allOpers,allOpersTo)
|
||||||
import GF.Compile.Rename(renameSourceTerm)
|
import GF.Compile.Rename(renameSourceTerm)
|
||||||
import GF.Compile.Compute.Concrete (computeConcrete,checkPredefError)
|
import GF.Compile.Compute.Concrete (computeConcrete,checkPredefError)
|
||||||
import qualified GF.Compile.Compute.ConcreteNew as CN(normalForm)
|
import qualified GF.Compile.Compute.ConcreteNew as CN(normalForm,resourceValues)
|
||||||
import GF.Compile.TypeCheck.Concrete (inferLType,ppType)
|
import GF.Compile.TypeCheck.Concrete (inferLType,ppType)
|
||||||
import GF.Infra.Dependencies(depGraph)
|
import GF.Infra.Dependencies(depGraph)
|
||||||
import GF.Infra.CheckM
|
import GF.Infra.CheckM
|
||||||
@@ -333,7 +333,7 @@ checkComputeTerm' new sgr t = do
|
|||||||
((t,_),_) <- runCheck $ do t <- renameSourceTerm sgr mo t
|
((t,_),_) <- runCheck $ do t <- renameSourceTerm sgr mo t
|
||||||
inferLType sgr [] t
|
inferLType sgr [] t
|
||||||
t1 <- if new
|
t1 <- if new
|
||||||
then return (CN.normalForm sgr t)
|
then return (CN.normalForm (CN.resourceValues sgr) t)
|
||||||
else computeConcrete sgr t
|
else computeConcrete sgr t
|
||||||
checkPredefError sgr t1
|
checkPredefError sgr t1
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user