Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Data.Char
- import Data.List
- import Debug.Trace
- -- The real program
- type Program = [Clause]
- data Clause = Fact String Bool
- | Rule String Bool
- deriving (Eq, Show)
- parse :: [String] -> Program
- parse [] = []
- parse (x:xs)
- = parseHelp (x:xs) []
- parseHelp :: [String] -> [Clause] -> Program
- parseHelp [] clauses = clauses
- parseHelp (x:xs) clauses
- | isFact x = parseHelp xs ((Fact (getFactName x) True):clauses)
- | otherwise = trace ("RHS: " ++ show(clauses)) (parseHelp xs ((Rule x rhs):clauses))
- where
- facts = getFacts $ splitWords x
- rhs = getRHS facts clauses
- isFact :: String -> Bool
- isFact [] = False
- isFact (x:xs)
- | x == '.' = True
- | x == ':' = False
- | otherwise = isFact xs
- getRHS :: [Clause] -> Program -> Bool
- getRHS [] others = True
- getRHS (x:xs) others
- = elem x others && getRHS xs others
- splitWords :: String -> [String]
- splitWords [] = []
- splitWords (x:xs)
- | x == '-' = split xs ""
- | otherwise = splitWords xs
- split :: String -> String -> [String]
- split [] ys | ys == "" = []
- | otherwise = [ys]
- split (x:xs) ys
- | isLetter x = split xs (ys ++ [x])
- | ys == "" = split xs ""
- | otherwise = ys : (split xs "")
- getFacts :: [String] -> [Clause]
- getFacts [] = []
- getFacts (x:xs)
- = (Fact x True) : getFacts xs
- getFactName :: String -> String
- getFactName [] = ""
- getFactName (x:xs)
- | isLetter x = x : getFactName xs
- | otherwise = ""
- t1 = "a."
- t2 = "b."
- t3 = "c :- a,b."
- t4 = "d :- c,b."
- t5 = "e :- a,f."
- pTest = parse [t1, t2, t3, t4, t5]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement