1
0
forked from GitHub/gf-core

example-based: use random generation for the examples

This commit is contained in:
hallgren
2011-10-17 13:51:25 +00:00
parent c0becec2bd
commit d4ee4a6133
2 changed files with 8 additions and 7 deletions

View File

@@ -10,7 +10,7 @@ import qualified Data.IntMap as IntMap
import qualified Data.Set as Set import qualified Data.Set as Set
import Data.Maybe import Data.Maybe
import System.Environment (getArgs) import System.Environment (getArgs)
import System.Random (newStdGen) import System.Random (RandomGen) --newStdGen
type MyType = CId -- name of the categories from the program type MyType = CId -- name of the categories from the program
@@ -39,8 +39,7 @@ data Environ = Env {getTypeMap :: TypeMap, -- mapping between a
getConcMap :: ConcMap, -- concrete expression after parsing getConcMap :: ConcMap, -- concrete expression after parsing
getSigs :: Map.Map MyType [FuncWithArg], -- functions for which we have the concrete syntax already with args getSigs :: Map.Map MyType [FuncWithArg], -- functions for which we have the concrete syntax already with args
getAll :: [FuncWithArg] -- all the functions with arguments getAll :: [FuncWithArg] -- all the functions with arguments
} }
getNext :: Environ -> ([MyFunc],[MyFunc]) getNext :: Environ -> ([MyFunc],[MyFunc])
@@ -60,12 +59,12 @@ getNext env =
in (exampleable,testeable) in (exampleable,testeable)
provideExample :: Environ -> MyFunc -> PGF -> PGF -> Language -> Maybe (Expr,String) provideExample :: RandomGen gen => gen -> Environ -> MyFunc -> PGF -> PGF -> Language -> Maybe (Expr,String)
provideExample env myfunc parsePGF pgfFile lang = provideExample gen env myfunc parsePGF pgfFile lang =
fmap giveExample $ getNameExpr myfunc env fmap giveExample $ getNameExpr myfunc env
where where
giveExample e_ = giveExample e_ =
let newexpr = head $ generateFromDepth pgfFile e_ (Just 5) -- change here with the new random generator let newexpr = head $ generateRandomFromDepth gen pgfFile e_ (Just 5) -- change here with the new random generator
ty = getType $ head $ filter (\x -> getName x == myfunc) $ getAll env ty = getType $ head $ filter (\x -> getName x == myfunc) $ getAll env
embeddedExpr = maybe "" (\x -> ", as in: " ++ q (linearize pgfFile lang x)) (embedInStart (getAll env) (Map.fromList [(ty,e_)])) embeddedExpr = maybe "" (\x -> ", as in: " ++ q (linearize pgfFile lang x)) (embedInStart (getAll env) (Map.fromList [(ty,e_)]))
lexpr = linearize pgfFile lang newexpr lexpr = linearize pgfFile lang newexpr

View File

@@ -1,4 +1,5 @@
module ExampleService(cgiMain,cgiMain',newPGFCache) where module ExampleService(cgiMain,cgiMain',newPGFCache) where
import System.Random(newStdGen)
import System.FilePath((</>),makeRelative) import System.FilePath((</>),makeRelative)
import Data.Map(fromList) import Data.Map(fromList)
import Data.Char(isDigit) import Data.Char(isDigit)
@@ -33,7 +34,8 @@ doProvideExample root cwd cache environ =
parsePGF <- readParsePGF cwd cache parsePGF <- readParsePGF cwd cache
let adjpath path = root</>makeRelative "/" (makeRelative root cwd</>path) let adjpath path = root</>makeRelative "/" (makeRelative root cwd</>path)
pgf <- liftIO . readCache cache . adjpath =<< getInp "grammar" pgf <- liftIO . readCache cache . adjpath =<< getInp "grammar"
let Just (e,s) = E.provideExample environ fun parsePGF pgf lang gen <- liftIO newStdGen
let Just (e,s) = E.provideExample gen environ fun parsePGF pgf lang
res = (showExpr [] e,s) res = (showExpr [] e,s)
liftIO $ logError $ "proveExample ... = "++show res liftIO $ logError $ "proveExample ... = "++show res
outputJSONP res outputJSONP res