Advertisement
Guest User

Untitled

a guest
May 27th, 2015
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. import Data.Char
  2. import Data.List
  3. import Debug.Trace
  4.  
  5. -- The real program
  6.  
  7. type Program = [Clause]
  8.  
  9. data Clause = Fact String Bool
  10. | Rule String Bool
  11. deriving (Eq, Show)
  12.  
  13. parse :: [String] -> Program
  14. parse [] = []
  15. parse (x:xs)
  16. = parseHelp (x:xs) []
  17.  
  18. parseHelp :: [String] -> [Clause] -> Program
  19. parseHelp [] clauses = clauses
  20. parseHelp (x:xs) clauses
  21. | isFact x = parseHelp xs ((Fact (getFactName x) True):clauses)
  22. | otherwise = trace ("RHS: " ++ show(clauses)) (parseHelp xs ((Rule x rhs):clauses))
  23. where
  24. facts = getFacts $ splitWords x
  25. rhs = getRHS facts clauses
  26.  
  27. isFact :: String -> Bool
  28. isFact [] = False
  29. isFact (x:xs)
  30. | x == '.' = True
  31. | x == ':' = False
  32. | otherwise = isFact xs
  33.  
  34. getRHS :: [Clause] -> Program -> Bool
  35. getRHS [] others = True
  36. getRHS (x:xs) others
  37. = elem x others && getRHS xs others
  38.  
  39. splitWords :: String -> [String]
  40. splitWords [] = []
  41. splitWords (x:xs)
  42. | x == '-' = split xs ""
  43. | otherwise = splitWords xs
  44.  
  45. split :: String -> String -> [String]
  46. split [] ys | ys == "" = []
  47. | otherwise = [ys]
  48. split (x:xs) ys
  49. | isLetter x = split xs (ys ++ [x])
  50. | ys == "" = split xs ""
  51. | otherwise = ys : (split xs "")
  52.  
  53. getFacts :: [String] -> [Clause]
  54. getFacts [] = []
  55. getFacts (x:xs)
  56. = (Fact x True) : getFacts xs
  57.  
  58. getFactName :: String -> String
  59. getFactName [] = ""
  60. getFactName (x:xs)
  61. | isLetter x = x : getFactName xs
  62. | otherwise = ""
  63.  
  64. t1 = "a."
  65. t2 = "b."
  66. t3 = "c :- a,b."
  67. t4 = "d :- c,b."
  68. t5 = "e :- a,f."
  69.  
  70. pTest = parse [t1, t2, t3, t4, t5]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement