Guest User

Untitled

a guest
Jan 24th, 2016
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. data Expr a = Simple a | Seq (Expr a) (Expr a) | Or (Expr a) (Expr a) | And (Expr a) (Expr a) | Cond (Expr a) (Expr a) (Expr a)
  2.  
  3. npathsT :: Expr a -> Integer
  4. npathsT (Simple _) = 1
  5. npathsT (Seq x y) = npaths x * npathsT y
  6. npathsT (Or x y) = npathsT x + npathsF x * npathsT y
  7. npathsT (And x y) = npathsT x * npathsT y
  8. npathsT (Cond b x y) = npathsT b * npathsT x + npathsF b * npathsT y
  9.  
  10. npathsF :: Expr a -> Integer
  11. npathsF (Simple _) = 1
  12. npathsF (Seq x y) = npaths x * npathsF y
  13. npathsF (Or x y) = npathsF x * npathsF y
  14. npathsF (And x y) = npathsF x + npathsT x * npathsF y
  15. npathsF (Cond b x y) = npathsT b * npathsF x + npathsF b * npathsF y
  16.  
  17. npaths :: Expr a -> Integer
  18. npaths (Simple _) = 1
  19. npaths (Seq x y) = npaths x * npaths y
  20. npaths (Or x y) = npathsT x + npathsF x * npaths y
  21. npaths (And x y) = npathsF x + npathsT x * npaths y
  22. npaths (Cond b x y) = npathsT b * npaths x + npathsF b * npaths y
  23.  
  24. pathsT :: Expr a -> [[a]]
  25. pathsT (Simple x) = return [x]
  26. pathsT (Seq x y) = do
  27.   xp <- paths x
  28.   yp <- pathsT y
  29.   return (xp ++ yp)
  30. pathsT (Or x y) = xPaths ++ xyPaths
  31.   where
  32.     xPaths = pathsT x
  33.     xyPaths = do
  34.       xp <- pathsF x
  35.       yp <- pathsT y
  36.       return (xp ++ yp)
  37. pathsT (And x y) = do
  38.   xp <- pathsT x
  39.   yp <- pathsT y
  40.   return (xp ++ yp)
  41. pathsT (Cond b x y) = truePaths ++ falsePaths
  42.   where
  43.     truePaths = do
  44.       tp <- pathsT b
  45.       xp <- pathsT x
  46.       return (tp ++ xp)
  47.     falsePaths = do
  48.       fp <- pathsF b
  49.       yp <- pathsT y
  50.       return (fp ++ yp)
  51.  
  52. pathsF :: Expr a -> [[a]]
  53. pathsF (Simple x) = return [x]
  54. pathsF (Seq x y) = do
  55.   xp <- paths x
  56.   yp <- pathsF y
  57.   return (xp ++ yp)
  58. pathsF (Or x y) = do
  59.   xp <- pathsF x
  60.   yp <- pathsF y
  61.   return (xp ++ yp)
  62. pathsF (And x y) = xPaths ++ xyPaths
  63.   where
  64.     xPaths = pathsF x
  65.     xyPaths = do
  66.       xp <- pathsT x
  67.       yp <- pathsF y
  68.       return (xp ++ yp)
  69. pathsF (Cond b x y) = truePaths ++ falsePaths
  70.   where
  71.     truePaths = do
  72.       tp <- pathsT b
  73.       xp <- pathsF x
  74.       return (tp ++ xp)
  75.     falsePaths = do
  76.       fp <- pathsF b
  77.       yp <- pathsF y
  78.       return (fp ++ yp)
  79.  
  80. paths :: Expr a -> [[a]]
  81. paths (Simple x) = return [x]
  82. paths (Seq x y) = do
  83.   xp <- paths x
  84.   yp <- paths y
  85.   return (xp ++ yp)
  86. paths (Or x y) = xPaths ++ xyPaths
  87.   where
  88.     xPaths = pathsT x
  89.     xyPaths = do
  90.       xp <- pathsF x
  91.       yp <- paths y
  92.       return (xp ++ yp)
  93. paths (And x y) = xPaths ++ xyPaths
  94.   where
  95.     xPaths = pathsF x
  96.     xyPaths = do
  97.       xp <- pathsT x
  98.       yp <- paths y
  99.       return (xp ++ yp)
  100. paths (Cond b x y) = truePaths ++ falsePaths
  101.   where
  102.     truePaths = do
  103.       tp <- pathsT b
  104.       xp <- paths x
  105.       return (tp ++ xp)
  106.     falsePaths = do
  107.       fp <- pathsF b
  108.       yp <- paths y
  109.       return (fp ++ yp)
  110.  
  111. s = Simple
  112.  
  113. -- A?B:C
  114. e1 = Cond (s 'A') (s 'B') (s 'C')
  115. -- (A||B)?C:D
  116. e2 = Cond (s 'A' `Or` s 'B') (s 'C') (s 'D')
  117. -- (A?B:C)?D:E
  118. e3 = Cond e1 (s 'D') (s 'E')
  119. -- (A?B:C)?(D?E:F):G
  120. e4 = Cond e1 (Cond (s 'D') (s 'E') (s 'F')) (s 'G')
  121. -- ((A?(B&&C):(D||E))||F)?G:H
  122. e5 = Cond ((Cond (s 'A') (s 'B' `And` s 'C') (s 'D' `Or` s 'E')) `Or` (s 'F')) (s 'G') (s 'H')
Advertisement
Add Comment
Please, Sign In to add comment