From b1402e8bd6a68a891b00a214d6cf184d66defe19 Mon Sep 17 00:00:00 2001 From: aarne Date: Mon, 22 Sep 2003 13:16:55 +0000 Subject: Founding the newly structured GF2.0 cvs archive. --- src/GF/UseGrammar/Randomized.hs | 47 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/GF/UseGrammar/Randomized.hs (limited to 'src/GF/UseGrammar/Randomized.hs') diff --git a/src/GF/UseGrammar/Randomized.hs b/src/GF/UseGrammar/Randomized.hs new file mode 100644 index 000000000..dceb6acc6 --- /dev/null +++ b/src/GF/UseGrammar/Randomized.hs @@ -0,0 +1,47 @@ +module Randomized where + +import Abstract +import Editing + +import Operations +import Zipper + +--- import Arch (myStdGen) --- circular for hbc +import Random --- (mkStdGen, StdGen, randoms) --- bad import for hbc + +-- random generation and refinement. AR 22/8/2001 +-- implemented as sequence of refinement menu selecsions, encoded as integers + +myStdGen = mkStdGen --- + +-- build one random tree; use mx to prevent infinite search +mkRandomTree :: StdGen -> Int -> CGrammar -> QIdent -> Err Tree +mkRandomTree gen mx gr cat = mkTreeFromInts (take mx (randoms gen)) gr cat + +refineRandom :: StdGen -> Int -> CGrammar -> Action +refineRandom gen mx = mkStateFromInts $ take mx $ map abs (randoms gen) + +-- build a tree from a list of integers +mkTreeFromInts :: [Int] -> CGrammar -> QIdent -> Err Tree +mkTreeFromInts ints gr cat = do + st0 <- newCat gr cat initState + state <- mkStateFromInts ints gr st0 + return $ loc2tree state + +mkStateFromInts :: [Int] -> CGrammar -> Action +mkStateFromInts ints gr = mkRandomState ints where + mkRandomState [] state = do + testErr (isCompleteState state) "not completed" + return state + mkRandomState (n:ns) state = do + let refs = refinementsState gr state + testErr (not (null refs)) $ "no refinements available for" +++ + prt (actVal state) + (ref,_) <- (refs !? (n `mod` (length refs))) + state1 <- refineWithAtom False gr ref state + if isCompleteState state1 + then return state1 + else do + state2 <- goNextMeta state1 + mkRandomState ns state2 + -- cgit v1.2.3