happy-arbitrary-0.0.3: src/Language/Happy/Sized.hs
{-# LANGUAGE StrictData #-}
-- | Trees of a given size in tokens. Every symbol knows the fewest and the
-- most tokens it can make. A node picks one of the productions that can make
-- its budget, and shares the budget among its children so that each of them
-- can make its share. Both favour symbols that reach more of the grammar.
module Language.Happy.Sized
( Range (..)
, Generator
, generator
, genSized
, ranges
) where
import Control.Monad (zipWithM)
import Data.List (minimumBy, sortOn)
import Data.Map (Map)
import qualified Data.Map as Map
import Data.Maybe (mapMaybe)
import Data.Ord (comparing)
import Data.Set (Set)
import qualified Data.Set as Set
import Data.Text (Text)
import Language.Happy.Ast (Sym (..))
import Language.Happy.GenTree (GenTree (..))
import Language.Happy.Grammar (Rules)
import Test.QuickCheck (Gen, choose, shuffle)
-- | The fewest and the most tokens a symbol makes. 'Nothing' as the most
-- means there's no limit.
data Range = Range Int (Maybe Int)
deriving (Show, Eq)
-- | Returns the size range of every nonterminal that makes a finite tree.
ranges :: Rules -> Set Text -> Map Sym Range
ranges grammar terminals = Map.intersectionWith Range mins maxes
where
size m s@(Sym name _)
| Set.member name terminals = Just 1
| otherwise = Map.lookup s m
best pick m = Map.mapMaybe (\prods -> case mapMaybe (fmap sum . mapM (size m)) prods of
[] -> Nothing
ns -> Just (pick ns)) grammar
-- The smallest tree has no path with a nonterminal on it twice, so as
-- many rounds as there are nonterminals are enough.
mins = iterate (best minimum) Map.empty !! (Map.size grammar + 1)
-- With n nonterminals, a most with a limit settles within n rounds. One
-- without a limit keeps growing, at least once every n rounds, because no
-- cycle is longer than n.
rounds = iterate (best maximum) (0 <$ mins)
settled = rounds !! (Map.size grammar + 1)
later = rounds !! (2 * Map.size grammar + 2)
maxes = Map.mapWithKey (\s n -> if Map.lookup s later == Just n then Just n else Nothing) settled
-- | A grammar prepared for generating trees. It's expensive to make, so make
-- one and generate all trees from it.
data Generator token = Generator
{ genTokenMap :: Map Text token
, genRanges :: Map Sym Range
, genReach :: Map Sym (Set Sym)
, genOptions :: Map Sym [([Sym], Range, Double)]
, genStart :: Sym
}
generator :: Rules -> Map Text token -> Sym -> Generator token
generator grammar tokens start = Generator
{ genTokenMap = tokens
, genRanges = rs
, genReach = reached
-- Weighted by the square root of how many symbols a production reaches,
-- so big productions come up more often without crowding out small ones.
, genOptions = Map.map (\prods -> [ (prod, r, sqrt (fromIntegral (Set.size (Set.unions (map (reachOf reached) prod))))) | prod <- prods, Just r <- [prodRange tokens rs prod] ]) grammar
, genStart = start
}
where
rs = ranges grammar (Map.keysSet tokens)
reached = reach grammar
-- | Generate a tree of the start symbol with @n@ tokens (or as close as the
-- grammar allows).
genSized :: Generator token -> Int -> Gen (GenTree token)
genSized g n = build g (clamp n (rangeOf g (genStart g))) (genStart g)
build :: Generator token -> Int -> Sym -> Gen (GenTree token)
build g b sym@(Sym name _) = case Map.lookup name (genTokenMap g) of
Just token -> return $ Leaf token
Nothing -> do
let candidates = Map.findWithDefault [] sym (genOptions g)
fitting = [ (w, (prod, r)) | (prod, r, w) <- candidates, fits b r ]
-- Not every size between minimum and maximum is possible (e.g.
-- @Gap : x | x x x@ can't make 2), so we take the closest.
(prod, r) <- if null fitting
then return $ minimumBy (comparing (distance b . snd)) [ (prod, r) | (prod, r, _) <- candidates ]
else pickWeighted fitting
shares <- split (clamp b r) [ (rangeOf g s, Set.size (reachOf (genReach g) s)) | s <- prod ]
Node sym <$> zipWithM (build g) shares prod
reachOf :: Map Sym (Set Sym) -> Sym -> Set Sym
reachOf reached s = Map.findWithDefault (Set.singleton s) s reached
rangeOf :: Generator token -> Sym -> Range
rangeOf g s@(Sym name _)
| Map.member name (genTokenMap g) = Range 1 (Just 1)
| otherwise = Map.findWithDefault (error $ "genSized: " ++ show s ++ " makes no finite tree") s (genRanges g)
-- | Token range of a production, or 'Nothing' if one of its symbols can't
-- produce a finite tree.
prodRange :: Map Text token -> Map Sym Range -> [Sym] -> Maybe Range
prodRange tokens rs prod = foldr add (Range 0 (Just 0)) <$> mapM symRange prod
where
symRange s@(Sym name _)
| Map.member name tokens = Just (Range 1 (Just 1))
| otherwise = Map.lookup s rs
add (Range a b) (Range c d) = Range (a + c) ((+) <$> b <*> d)
fits :: Int -> Range -> Bool
fits b (Range lo hi) = lo <= b && maybe True (>= b) hi
distance :: Int -> Range -> Int
distance b (Range lo hi) = max (lo - b) (maybe 0 (b -) hi)
clamp :: Int -> Range -> Int
clamp b (Range lo hi) = max lo (maybe b (min b) hi)
-- | Picks one of the values with a probability proportional to its weight.
pickWeighted :: [(Double, a)] -> Gen a
pickWeighted options = do
r <- choose (0, sum (map fst options))
return $ pick r options
where
pick _ [(_, a)] = a
pick r ((w, a) : rest)
| r <= w = a
| otherwise = pick (r - w) rest
pick _ [] = error "pickWeighted: no options"
-- | Returns the symbols each nonterminal can reach, itself included.
reach :: Rules -> Map Sym (Set Sym)
reach grammar = iterate step (Map.mapWithKey (\s _ -> Set.singleton s) grammar) !! (Map.size grammar + 1)
where
step sets = Map.mapWithKey (\s prods -> Set.insert s (Set.unions [ Map.findWithDefault (Set.singleton c) c sets | c <- concat prods ])) grammar
-- | Shares a budget among children with these ranges and weights, in random
-- order, so that the children after each one can still make what's left. A
-- child gets its fewest tokens plus a part of the rest that grows with its
-- weight (times a random factor): a list of one token can take any number of
-- tokens, but a tree full of it tests less than one with more in it.
split :: Int -> [(Range, Int)] -> Gen [Int]
split budget children = do
order <- shuffle (zip [0 :: Int ..] children)
weighted <- mapM (\(i, (r, n)) -> (\u -> (i, r, fromIntegral n * u)) <$> choose (0, 1 :: Double)) order
return $ map snd . sortOn fst $ go budget weighted
where
go _ [] = []
go left ((i, Range lo hi, w) : rest) = (i, share) : go (left - share) rest
where
restLo = sum [ l | (_, Range l _, _) <- rest ]
restHi = sum <$> sequence [ h | (_, Range _ h, _) <- rest ]
from = max lo (maybe lo (left -) restHi)
to = maybe id min hi (left - restLo)
spare = left - lo - restLo
total = w + sum [ v | (_, _, v) <- rest ]
wanted = lo + if total > 0 then round (fromIntegral spare * w / total) else 0
share = max from (min to wanted)