/02-development/Hets/GMP/GMP-CoLoSS/GMP/Logics/P.hs

https://bitbucket.org/jmelo_lyncode/thesis · Haskell · 151 lines · 106 code · 20 blank · 25 comment · 8 complexity · e23586605a6bc2e10cf14a77471b91ce MD5 · raw file

  1. {- | Module : $Header$
  2. - Description : Implementation of logic instance Probabilistic modal logic
  3. - Copyright : (c) Daniel Hausmann & Georgel Calin & Lutz Schroeder, DFKI Lab Bremen,
  4. - Rob Myers & Dirk Pattinson, Department of Computing, ICL
  5. - License : GPLv2 or higher, see LICENSE.txt
  6. - Maintainer : hausmann@dfki.de
  7. - Stability : provisional
  8. - Portability : portable
  9. -
  10. - Provides the implementation of the matching functions of probabilistic modal logic.
  11. -}
  12. module GMP.Logics.P where
  13. import List
  14. import Ratio
  15. import Maybe
  16. import Debug.Trace
  17. import Text.ParserCombinators.Parsec
  18. import GMP.Logics.Generic
  19. import GMP.Parser
  20. --------------------------------------------------------------------------------
  21. -- instance of Probabilistic Modal Logic and needed functions
  22. --------------------------------------------------------------------------------
  23. data P a = P Rational [Formula a] deriving (Eq,Show)
  24. instance (SigFeature b c d, Eq (b (c d)), Eq (c d)) => NonEmptyFeature P b c d where
  25. nefMatch flags seq = let poslits = keep_poslits seq
  26. neglits = keep_neglits seq
  27. -- take all combinations of positive and negative modal operators
  28. all_combinations = [ (pos, neg) | pos <- map nub (powerList poslits),
  29. neg <- map nub (powerList neglits)] \\ [([],[])]
  30. probabilities xs = map (\(Mod (P k _)) -> k) xs
  31. strip_neg (Neg phi) = phi
  32. bound (p,n) = pml_bound ((probabilities p),(probabilities (map strip_neg n)))
  33. tuples (p,n) = nub [(pts,nts,k)| pts <- (tuprange (bound (p,n)) (length p)),
  34. nts <- (tuprange (bound (p,n)) (length n)),
  35. k <- [-(bound (p,n))..(bound (p,n))]]
  36. side_condition_tuples (p,n) = filter (pml_side_condition (p,n)) (tuples (p,n))
  37. pml_match (p,n) = -- trace ("\n filtered tuples:"
  38. -- ++ show((pml_filter_tuples (side_condition_tuples (p,n)) []))) $
  39. map (pml_build_matches (p,n))
  40. (pml_filter_tuples (side_condition_tuples (p,n)) [])
  41. in if (flags!!1)
  42. then
  43. trace ("\n allc: tracing defunct" )
  44. map pml_match all_combinations
  45. else map pml_match all_combinations
  46. nefPretty d = case d of
  47. P r [] -> "[P]" ++ show r ++ "nothing contained"
  48. P r e -> "[P]" ++ show r ++ (pretty (head e))
  49. nefFeatureFromSignature sig = P 1
  50. nefFeatureFromFormula phi = P 1
  51. nefStripFeature (P i phis) = phis
  52. nefDisj2Conj (Mod (P r phi)) = Mod (P r ([disj2conj (head phi)]))
  53. nefNegNorm (Mod (P r phi)) = Mod (P r ([negNorm (head phi)]))
  54. nefParser sig =
  55. do x <- natural
  56. let auxP n = do char '/'
  57. m<-natural
  58. return $ toRational (fromInteger n/fromInteger m)
  59. <|> do char '.'
  60. m<-natural
  61. let noDig n
  62. | n<10 = 1
  63. | n>=10 = 1 + noDig (div n 10)
  64. let rat n = toRational(fromInteger n /
  65. fromInteger (10^(noDig n)))
  66. let res = toRational n + rat m
  67. return res
  68. <|> do return $ toRational n
  69. <?> "Parser.parsePindex.auxP"
  70. aux <- auxP x
  71. return $ P aux
  72. --------------------------------------------------------------------------------
  73. -- additional functions for the matching function of this logic
  74. --------------------------------------------------------------------------------
  75. pml_build_matches :: (SigFeature a b c, Eq (a (b c))) => ([Formula (P (a (b c)))],[Formula (P (a (b c)))]) -> ([Int],[Int],Int) -> [Sequent]
  76. pml_build_matches (poslits,neglits) (prs,nrs,k) =
  77. let (pos_inds,neg_inds) = (to_inds prs,to_inds nrs)
  78. all_inds = [(pos,neg) | pos <- (powerList pos_inds), neg <- (powerList neg_inds)]
  79. (sposlits,sneglits) = ([phi | Mod (P k [phi]) <- poslits],[phi | Neg (Mod (P k [phi])) <- neglits])
  80. relevant_inds = filter (\(pos,neg) -> (sum $ imgInt pos prs) - (sum $ imgInt neg nrs) < k)
  81. all_inds
  82. getJ (ps,ns) = (img ps sposlits) ++
  83. (img ns sneglits)
  84. getnJ (ps,ns) = (img (pos_inds \\ ps) sposlits) ++
  85. (img (neg_inds \\ ns) sneglits)
  86. in [Sequent (map (\rs -> Neg (andify ((map nneg (getnJ rs)) ++ (getJ rs))) )
  87. relevant_inds)]
  88. pml_side_condition :: ([Formula (P (a (b c)))],[Formula (P (a (b c)))]) -> ([Int],[Int],Int) -> Bool
  89. pml_side_condition (pls,nls) (pints,nints,k) =
  90. let (rpints,rnints) = (map fromIntegral pints,map fromIntegral nints)
  91. psum = sum $ zipbin (*) rpints (map (\(Mod(P x _))->fromRational(x)) pls)
  92. nsum = sum $ zipbin (*) rnints (map (\(Neg(Mod(P x _)))-> fromRational(-x)) nls)
  93. in if null(pints) then (psum + nsum < fromIntegral(k))
  94. else (psum + nsum <= fromIntegral(k))
  95. pml_bound :: ([Rational],[Rational]) -> Int
  96. pml_bound (rps,rns) =
  97. let n = (length rps) + (length rns)
  98. toints rs = concatMap (\r -> [numerator r,denominator r]) rs
  99. allints = (toints rps) ++ (toints rns)
  100. logint x = ceiling(logBase 2 (1 + x))
  101. logsum = sum $ map (\y -> logint (fromIntegral(y))) allints
  102. in 20*n*n*(1+n) + 10*n*n*logsum
  103. -- find maximal elts of those tuples satisfying the side condition
  104. pml_filter_tuples :: [([Int], [Int], Int)] -> [([Int], [Int], Int)] -> [([Int], [Int], Int)]
  105. pml_filter_tuples [] bs = bs
  106. pml_filter_tuples (a:as) bs
  107. | any (\x -> pml_geq x a) bs = pml_filter_tuples as bs
  108. | otherwise = a:(filter (\x -> not (pml_leq x a)) bs)
  109. pml_leq :: ([Int],[Int], Int) -> ([Int],[Int], Int) -> Bool
  110. pml_leq (p1, n1, k1) (p2, n2, k2) = (k1 == k2) &&
  111. (and (( map (\(x, y) -> x <= y) ((zip p1 p2) ++
  112. (zip n1 n2)))))
  113. pml_geq :: ([Int],[Int], Int) -> ([Int],[Int], Int) -> Bool
  114. pml_geq (p1, n1, k1) (p2, n2, k2) = (k1 == k2) &&
  115. (and (( map (\(x, y) -> x >= y) ((zip p1 p2) ++
  116. (zip n1 n2)))))
  117. -- Construct all integer n-tuples with elements from 1,..,r
  118. tuprange :: Int -> Int -> [[Int]]
  119. tuprange _ 0 = [[]]
  120. tuprange r n =
  121. let go xs ys = map (\z -> z:ys) xs
  122. in concatMap (go [1..r]) (tuprange r (n-1))
  123. -- zip two lists together using a binary operator
  124. zipbin :: (a -> a -> a) -> [a] -> [a] -> [a]
  125. zipbin _ [] _ = []; zipbin _ _ [] = [];
  126. zipbin f (x:xs) (y:ys) = (f x y):(zipbin f xs ys)
  127. --------------------------------------------------------------------------------
  128. -- instance of sigFeature for probabilistic modal logic
  129. --------------------------------------------------------------------------------
  130. instance (SigFeature b c d, Eq (c d), Eq (b (c d))) => NonEmptySigFeature P b c d where
  131. neGoOn sig flag = genericPGoOn sig flag