module MagicHaskeller.Analytical(
get1, getMany, getManyM, getManyTyped, noBK, c, SplicedPrims,
getOne, synth, synthM, synthTyped
) where
import Data.Char(ord,chr)
import Data.Array
import qualified Data.Map as Map
import Data.Generics
import Language.Haskell.TH
import Control.Monad.Search.Combinatorial
import Control.Monad.Search.Best
import MagicHaskeller.TyConLib
import MagicHaskeller.CoreLang hiding (C)
import MagicHaskeller.PriorSubsts
import MagicHaskeller.ReadTHType(typeToTHType)
import MagicHaskeller.MHTH(decsToExpDecs)
import MagicHaskeller(p1)
import MagicHaskeller.Analytical.Synthesize
#ifdef DEBUG
import MagicHaskeller.Analytical.Debug
#endif
type Strategy = Matrix
type SplicedPrims = ([Dec],[Primitive])
get1 :: SplicedPrims
-> SplicedPrims
-> Exp
get1 target bk = head $ concat $ getMany target bk
getMany :: SplicedPrims
-> SplicedPrims
-> [[Exp]]
getMany tgt bk = unMx $ toMx (getManyM tgt bk :: Strategy Exp)
getManyM :: (Search m) =>
SplicedPrims
-> SplicedPrims
-> m Exp
getManyM (tgt,pt) (bk,pb) = let ps = pt++pb
tcl = primitivesToTCL ps
vl = primitivesToVL tcl ps
in fmap (exprToTHExp vl) (analyticSynth tcl vl tgt bk)
getManyTyped :: SplicedPrims
-> SplicedPrims
-> [[Exp]]
getManyTyped (tgt,pt) (bk,pb)
= let ps = pt++pb
tcl = primitivesToTCL ps
vl = primitivesToVL tcl ps
(unit, ty) = analyticSynthAndInfType tcl vl tgt bk
addSignature thexp = SigE thexp $ typeToTHType tcl ty
in map (map (addSignature . exprToTHExpLite vl)) $ unMx $ toMx (unit :: Strategy CoreExpr)
noBK :: SplicedPrims
noBK = ([],[])
c :: Q [Dec] -> ExpQ
c decq = do decs <- decq
expdecs <- decsToExpDecs decs
expPrims <- fmap ListE $ mapM p1 $ cons decs
return $ TupE [expdecs, expPrims]
cons, conEs, conPs :: (Data a, Typeable a) => a -> [Exp]
cons a = conEs a ++ conPs a
conEs = everything (++) (mkQ [] (\x -> [ e | e@(ConE _) <- [x]]))
conPs = everything (++) (mkQ [] (\x -> [ ConE name | (ConP name _) <- [x]]))
getOne :: [Dec] -> [Dec] -> Exp
getOne iops bk = head $ concat $ synth iops bk
synth :: [Dec] -> [Dec] -> [[Exp]]
synth iops bk = unMx $ toMx (synthM iops bk :: Strategy Exp)
synthM :: Search m => [Dec] -> [Dec] -> m Exp
synthM iops bk = fmap (exprToTHExp defaultVarLib) (analyticSynth defaultTCL defaultVarLib iops bk)
synthTyped :: [Dec] -> [Dec] -> [[Exp]]
synthTyped iops bk
= let (unit, ty) = analyticSynthAndInfType defaultTCL defaultVarLib iops bk
addSignature thexp = SigE thexp $ typeToTHType defaultTCL ty
in map (map (addSignature . exprToTHExpLite defaultVarLib)) $ unMx $ toMx (unit :: Strategy CoreExpr)
synthesize :: [Dec] -> [Dec] -> [[String]]
synthesize iops bk
= map (map pprint) $ synth iops bk