module UHC.Light.Compiler.Pred.CommonCHR
( module UHC.Light.Compiler.CHR, module UHC.Light.Compiler.CHR.Constraint
, RedHowAnnotation (..)
, CHRConstraint, CHRIntermediateUntilAssume
, ByScopeRedHow (..)
, patchToAssumeConstraint
, CHRPredOccCnstrTraceMp, CHRPredOccCnstrMp
, gathPredLToProveCnstrMp, gathPredLToAssumeCnstrMp
, predOccCnstrMpLiftScope
, rhaMbId
, mkProveConstraint, mkAssumeConstraint, mkAssumeConstraint' )
where
import UHC.Light.Compiler.CHR
import UHC.Light.Compiler.CHR.Constraint
import qualified Data.Map as Map
import qualified Data.Set as Set
import UHC.Util.Pretty
import UHC.Light.Compiler.Base.Common
import UHC.Light.Compiler.Ty
import UHC.Light.Compiler.VarMp
import Control.Monad
import UHC.Util.Binary
import UHC.Util.Serialize
data RedHowAnnotation
= RedHow_ByInstance !HsName !Pred !PredScope
| RedHow_BySuperClass !HsName !Int !CTag
| RedHow_ProveObl !UID !PredScope
| RedHow_Assumption !VarUIDHsName !PredScope
| RedHow_ByScope !ByScopeRedHow
| RedHow_ByLabel !Label !LabelOffset !PredScope
| RedHow_Lambda !UID !PredScope
deriving
( Eq, Ord
, Typeable, Data
)
rhaMbId :: RedHowAnnotation -> Maybe UID
rhaMbId (RedHow_ProveObl i _) = Just i
rhaMbId _ = Nothing
instance Show RedHowAnnotation where
show = showPP . pp
instance PP RedHowAnnotation where
pp (RedHow_ByInstance s p sc) = "inst" >#< ppParensCommas [pp p, pp s, pp sc]
pp (RedHow_BySuperClass s _ _ ) = "super" >#< s
pp (RedHow_ProveObl i sc) = "prove" >#< i >#< sc
pp (RedHow_Assumption vun sc) = "assume" >#< ppParensCommas [pp vun, pp sc]
pp (RedHow_ByScope v ) = "scope" >|< ppParens v
pp (RedHow_ByLabel l o sc) = "label" >#< l >|< "@" >|< o >|< sc
pp (RedHow_Lambda i sc) = "lambda" >#< i >#< sc
type CHRConstraint = Constraint CHRPredOcc RedHowAnnotation
type CHRIntermediateUntilAssume = (CHRPredOcc,(PredScope,CHRPredOccCnstrTraceMp))
data ByScopeRedHow
= ByScopeRedHow_Prove
| ByScopeRedHow_Assume
| ByScopeRedHow_Other (AlwaysEq String)
deriving
( Eq, Ord
, Typeable, Data
)
instance Show ByScopeRedHow where
show ByScopeRedHow_Prove = "prv"
show ByScopeRedHow_Assume = "ass"
show (ByScopeRedHow_Other s) = show s
instance PP ByScopeRedHow where
pp = pp . show
mkProveConstraint :: Range -> Pred -> UID -> PredScope -> (CHRConstraint,RedHowAnnotation)
mkProveConstraint r pr i sc = (Prove (mkCHRPredOccRng r pr sc),RedHow_ProveObl i sc)
mkAssumeConstraint'' :: Range -> Pred -> VarUIDHsName -> PredScope -> (CHRConstraint,RedHowAnnotation)
mkAssumeConstraint'' r pr vun sc = (Assume (mkCHRPredOccRng r pr sc),RedHow_Assumption vun sc)
mkAssumeConstraint' :: Range -> Pred -> UID -> HsName -> PredScope -> (CHRConstraint,RedHowAnnotation)
mkAssumeConstraint' r pr i n sc = mkAssumeConstraint'' r pr (VarUIDHs_Name i n) sc
mkAssumeConstraint :: Range -> Pred -> UID -> PredScope -> (CHRConstraint,RedHowAnnotation)
mkAssumeConstraint r pr i sc = mkAssumeConstraint'' r pr (VarUIDHs_UID i) sc
patchToAssumeConstraint :: UID -> PredScope -> (PredScope -> RedHowAnnotation -> x -> x) -> (CHRConstraint,x) -> (CHRConstraint,x)
patchToAssumeConstraint i sc set (c,x)
= (Assume (pr {cpoCxt = cx {cpocxScope = sc}}), set sc (RedHow_Assumption (VarUIDHs_UID i) sc) x)
where pr = cnstrPred c
cx = cpoCxt pr
type CHRPredOccCnstrTraceMp = ConstraintToInfoTraceMp CHRPredOcc RedHowAnnotation
type CHRPredOccCnstrMp = ConstraintToInfoMap CHRPredOcc RedHowAnnotation
gathPredLToProveCnstrMp :: [PredOcc] -> CHRPredOccCnstrMp
gathPredLToProveCnstrMp l = cnstrMpFromList [ rngLift (poRange po) mkProveConstraint (poPr po) (poId po) (poScope po) | po <- l ]
gathPredLToAssumeCnstrMp :: [PredOcc] -> CHRPredOccCnstrMp
gathPredLToAssumeCnstrMp l = cnstrMpFromList [ rngLift (poRange po) mkAssumeConstraint (poPr po) (poId po) (poScope po) | po <- l ]
predOccCnstrMpLiftScope :: PredScope -> CHRPredOccCnstrMp -> CHRPredOccCnstrMp
predOccCnstrMpLiftScope sc
= Map.mapKeysWith (++) c . Map.map (map i)
where c (Prove o@(CHRPredOcc {cpoCxt=cx}))
= Prove (o {cpoCxt = cx {cpocxScope = sc}})
c x = x
i (RedHow_ProveObl id _)
= RedHow_ProveObl id sc
i x = x
instance Serialize ByScopeRedHow where
sput (ByScopeRedHow_Prove ) = sputWord8 0
sput (ByScopeRedHow_Assume ) = sputWord8 1
sput (ByScopeRedHow_Other a ) = sputWord8 2 >> sput a
sget = do
t <- sgetWord8
case t of
0 -> return ByScopeRedHow_Prove
1 -> return ByScopeRedHow_Assume
2 -> liftM ByScopeRedHow_Other sget
instance Serialize RedHowAnnotation where
sput (RedHow_ByInstance a b c ) = sputWord8 0 >> sput a >> sput b >> sput c
sput (RedHow_BySuperClass a b c ) = sputWord8 1 >> sput a >> sput b >> sput c
sput (RedHow_ProveObl a b ) = sputWord8 2 >> sput a >> sput b
sput (RedHow_Assumption a b ) = sputWord8 3 >> sput a >> sput b
sput (RedHow_ByScope a ) = sputWord8 4 >> sput a
sput (RedHow_ByLabel a b c ) = sputWord8 5 >> sput a >> sput b >> sput c
sput (RedHow_Lambda a b ) = sputWord8 6 >> sput a >> sput b
sget = do t <- sgetWord8
case t of
0 -> liftM3 RedHow_ByInstance sget sget sget
1 -> liftM3 RedHow_BySuperClass sget sget sget
2 -> liftM2 RedHow_ProveObl sget sget
3 -> liftM2 RedHow_Assumption sget sget
4 -> liftM RedHow_ByScope sget
5 -> liftM3 RedHow_ByLabel sget sget sget
6 -> liftM2 RedHow_Lambda sget sget