module Language.Synthesis.Synthesis (
Mutation, synthesizeMhList, runningBest, Problem(..)
) where
import Control.Monad.Random (Rand, RandomGen)
import Language.Synthesis.Distribution (Distr)
import qualified Language.Synthesis.Distribution as Distr
import Language.Synthesis.MCMC
import Language.Synthesis.Mutations (Mutation)
data Problem program = Problem { score :: program -> Double
, prior :: Distr program
, jump :: Mutation program
}
synthesizeMhList :: RandomGen gen => Problem program -> Rand gen [(program, Double)]
synthesizeMhList Problem {prior, score, jump} = do
first <- Distr.sample prior
let density prog = (sc, sc + Distr.logProbability prior prog)
where sc = score prog
list <- mhList first density jump
return [(prog, sc) | (prog, sc, _) <- list]
runningBest :: [(a, Double)] -> [(a, Double)]
runningBest [] = []
runningBest (first:rest) = scanl maxScore first rest
where maxScore (p, ps) (q, qs) | qs >= ps = (q, qs)
| otherwise = (p, ps)