gt and gr can start from a tree with metavariables, just filling them

This commit is contained in:
aarne
2010-01-30 18:01:18 +00:00
parent 8d03e48e46
commit e71545eab1
3 changed files with 53 additions and 15 deletions

View File

@@ -165,7 +165,7 @@ parseAll :: PGF -> Type -> String -> [[Tree]]
parseAllLang :: PGF -> Type -> String -> [(Language,[Tree])]
-- | The same as 'generateAllDepth' but does not limit
-- the depth in the generation.
-- the depth in the generation, and doesn't give an initial expression.
generateAll :: PGF -> Type -> [Expr]
-- | Generates an infinite list of random abstract syntax expressions.
@@ -176,7 +176,7 @@ generateRandom :: PGF -> Type -> IO [Expr]
-- | Generates an exhaustive possibly infinite list of
-- abstract syntax expressions. A depth can be specified
-- to limit the search space.
generateAllDepth :: PGF -> Type -> Maybe Int -> [Expr]
generateAllDepth :: Maybe Expr -> PGF -> Type -> Maybe Int -> [Expr]
-- | List of all languages available in the given grammar.
languages :: PGF -> [Language]
@@ -248,7 +248,7 @@ generateRandom pgf cat = do
return $ genRandom gen pgf cat
generateAll pgf cat = generate pgf cat Nothing
generateAllDepth pgf cat = generate pgf cat
generateAllDepth mex pgf cat = generateAllFrom mex pgf cat
abstractName pgf = absname pgf

View File

@@ -9,6 +9,29 @@ import PGF.Probabilistic
import qualified Data.Map as M
import System.Random
-- generate all fillings of metavariables in an expr
generateAllFrom :: Maybe Expr -> PGF -> Type -> Maybe Int -> [Expr]
generateAllFrom mex pgf ty mi = maybe (gen ty) (generateForMetas pgf gen) mex where
gen ty = generate pgf ty mi
-- generate random fillings of metavariables in an expr
generateRandomFrom :: Maybe Expr ->
Maybe Probabilities -> StdGen -> PGF -> Type -> [Expr]
generateRandomFrom mex ps rg pgf ty =
maybe (gen ty) (generateForMetas pgf gen) mex where
gen ty = genRandomProb ps rg pgf ty
generateForMetas :: PGF -> (Type -> [Expr]) -> Expr -> [Expr]
generateForMetas pgf gen exp = case exp of
EApp f (EMeta _) -> [EApp g a | g <- gener f, a <- genArg g]
EApp f x -> [EApp g a | g <- gener f, a <- gener x]
_ -> [exp]
where
gener = generateForMetas pgf gen
genArg f = case inferExpr pgf f of
Right (_,DTyp ((_,_,ty):_) _ _) -> gen ty
_ -> []
-- generate an infinite list of trees exhaustively
generate :: PGF -> Type -> Maybe Int -> [Expr]
generate pgf ty@(DTyp _ cat _) dp = filter (\e -> case checkExpr pgf e ty of