Skip to content

Commit c2acc2f

Browse files
committed
Error is now a wrapper for either instead of its own ADT
1 parent d91516b commit c2acc2f

8 files changed

+55
-72
lines changed

GroupTheoryExample.txt

+1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ V x. E y. Eq[times(x, y), One]
1111
Inverse[x, y] <-> Eq[times(x, y), One] & Eq[times(y, x), One]
1212

1313
CONCLUSION:
14+
1415
Inverse[x, y] & Inverse[x, z] -> Eq[y, z]

src/Folly/Lexer.hs

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ tokEqual t1 t2 = name t1 == name t2
5757

5858
lexer :: String -> Error [Token]
5959
lexer str = case parse parseToks "LEXER" str of
60-
Left err -> Failed $ show err
61-
Right toks -> Succeeded $ toks
60+
Left err -> Left $ show err
61+
Right toks -> Right toks
6262

6363
parseToks = endBy parseTok spaces
6464

src/Folly/Parser.hs

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import Folly.Utils
1414

1515
parseTheorem :: [Token] -> Error Theorem
1616
parseTheorem toks = case parse parseTheoremToks "PARSER" toks of
17-
Left err -> Failed $ show err
18-
Right thm -> Succeeded thm
17+
Left err -> Left $ show err
18+
Right thm -> Right thm
1919

2020
parseTheoremToks = do
2121
axioms <- parseHypothesis
@@ -34,8 +34,8 @@ parseHypothesis = do
3434

3535
parseFormula :: [Token] -> Error (Formula)
3636
parseFormula toks = case parse parseForm "PARSER" toks of
37-
Left err -> Failed $ show err
38-
Right formula -> Succeeded formula
37+
Left err -> Left $ show err
38+
Right formula -> Right formula
3939

4040
parseForm = buildExpressionParser table parseFactor
4141

src/Folly/Resolution.hs

+10-7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module Folly.Resolution(isValid,
44
maxClause) where
55

66
import Data.List as L
7+
import Data.Maybe
78
import Data.Set as S
89

910
import Folly.Clause as C
@@ -40,13 +41,15 @@ isValid' s p t =
4041
resolve :: (Set Clause -> Clause) -> Set Clause -> Set Clause -> Maybe Clause
4142
resolve s axioms cls =
4243
case S.member C.empty cls of
43-
True -> lookupGE C.empty cls
44-
False -> case S.size cls == 0 of
45-
True -> Nothing
46-
False ->
47-
let c = s cls
48-
newClauses = genNewClauses c axioms in
49-
resolve s (S.insert c axioms) (S.delete c $ S.union newClauses cls)
44+
True -> S.lookupGE C.empty cls
45+
False -> case S.size cls == 0 of
46+
True -> Nothing
47+
False ->
48+
let c = s cls
49+
newClauses = genNewClauses c axioms
50+
nextAxioms = S.insert c axioms
51+
nextCls = S.delete c $ S.union newClauses cls in
52+
resolve s nextAxioms nextCls
5053

5154
genNewClauses c cls =
5255
S.fold S.union S.empty $ S.map (\x -> S.union (resolvedClauses c x) (resolvedClauses x c)) (S.delete c cls)

src/Folly/Utils.hs

+4-25
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,11 @@
11
module Folly.Utils(
22
Name,
3-
Error(..), extractValue) where
3+
Error, extractValue) where
44

55
type Name = String
66

7-
data Error a =
8-
Succeeded a |
9-
Failed String
10-
deriving (Show)
11-
12-
instance Applicative Error where
13-
pure = Succeeded
14-
(Succeeded f) <*> (Succeeded x) = Succeeded (f x)
15-
(Failed m) <*> _ = Failed m
16-
17-
instance Functor Error where
18-
fmap f (Succeeded a) = Succeeded (f a)
19-
fmap _ (Failed m) = Failed m
20-
21-
instance Monad Error where
22-
return a = Succeeded a
23-
(Succeeded a) >>= f = f a
24-
(Failed errMsg) >>= f = (Failed errMsg)
25-
26-
instance Eq a => Eq (Error a) where
27-
(==) (Succeeded v1) (Succeeded v2) = v1 == v2
28-
(==) _ _ = False
7+
type Error a = Either String a
298

309
extractValue :: Error a -> a
31-
extractValue (Succeeded val) = val
32-
extractValue (Failed errMsg) = error $ "Computation Failed: " ++ errMsg
10+
extractValue (Right val) = val
11+
extractValue (Left errMsg) = error $ "Computation Failed: " ++ errMsg

src/Main.hs

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ main = do
1717
thmString <- hGetContents fHandle
1818
let thm = processTheoremFile thmString
1919
case thm of
20-
Failed errMsg -> putStrLn errMsg
21-
Succeeded t -> do
20+
Left errMsg -> putStrLn errMsg
21+
Right t -> do
2222
putStr $ show t
2323
case isValid t of
2424
True -> putStrLn "\n\nIs Valid"

test/Folly/LexerTests.hs

+20-20
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,23 @@ allLexerTests = do
88
testFunction lexer lexerCases
99

1010
lexerCases =
11-
[("nooo", Succeeded $ [testVar "nooo"]),
12-
("(", Succeeded $ [testSep "("]),
13-
(")", Succeeded $ [testSep ")"]),
14-
("[", Succeeded $ [testSep "["]),
15-
("]", Succeeded $ [testSep "]"]),
16-
(",", Succeeded $ [testSep ","]),
17-
(".", Succeeded $ [testSep "."]),
18-
("|", Succeeded $ [testOp "|"]),
19-
("&", Succeeded $ [testOp "&"]),
20-
("~", Succeeded $ [testOp "~"]),
21-
("->", Succeeded $ [testOp "->"]),
22-
("<->", Succeeded $ [testOp "<->"]),
23-
("E", Succeeded $ [testQuant "E"]),
24-
("Q", Succeeded $ [testQuant "Q"]),
25-
("=", Succeeded $ [testPred "="]),
26-
("Ever", Succeeded $ [testPred "Ever"]),
27-
("Quacks", Succeeded $ [testPred "Quacks"]),
28-
("N#%_Man", Succeeded $ [testPred "N#%_Man"]),
29-
("n#2@", Succeeded $ [testVar "n#2@"]),
30-
("V z . E k. F[z] -> G[k]", Succeeded $ [testPred "V", testVar "z", testSep ".", testPred "E", testVar "k", testSep ".", testPred "F", testSep "[", testVar "z", testSep "]", testOp "->", testPred "G", testSep "[", testVar "k", testSep "]"])]
11+
[("nooo", Right $ [testVar "nooo"]),
12+
("(", Right $ [testSep "("]),
13+
(")", Right $ [testSep ")"]),
14+
("[", Right $ [testSep "["]),
15+
("]", Right $ [testSep "]"]),
16+
(",", Right $ [testSep ","]),
17+
(".", Right $ [testSep "."]),
18+
("|", Right $ [testOp "|"]),
19+
("&", Right $ [testOp "&"]),
20+
("~", Right $ [testOp "~"]),
21+
("->", Right $ [testOp "->"]),
22+
("<->", Right $ [testOp "<->"]),
23+
("E", Right $ [testQuant "E"]),
24+
("Q", Right $ [testQuant "Q"]),
25+
("=", Right $ [testPred "="]),
26+
("Ever", Right $ [testPred "Ever"]),
27+
("Quacks", Right $ [testPred "Quacks"]),
28+
("N#%_Man", Right $ [testPred "N#%_Man"]),
29+
("n#2@", Right $ [testVar "n#2@"]),
30+
("V z . E k. F[z] -> G[k]", Right $ [testPred "V", testVar "z", testSep ".", testPred "E", testVar "k", testSep ".", testPred "F", testSep "[", testVar "z", testSep "]", testOp "->", testPred "G", testSep "[", testVar "k", testSep "]"])]

test/Folly/ParserTests.hs

+12-12
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ allParserTests = do
1111

1212
parseFormulaCases :: [(String, Error Formula)]
1313
parseFormulaCases =
14-
[("Dog[ a ]", Succeeded $ pr "Dog" [var "a"]),
15-
("~Super[ k, kill(a, b)]", Succeeded $ neg (pr "Super" [var "k", func "kill" [var "a", var "b"]])),
16-
("Dog[x] & Owns[John, x]", Succeeded $ con (pr "Dog" [var "x"]) (pr "Owns" [constant "John", var "x"])),
17-
("Dog [x] & (Owns[John, x] | Not[Cat, John])", Succeeded $ con (pr "Dog" [var "x"]) (dis (pr "Owns" [constant "John", var "x"]) (pr "Not" [constant "Cat", constant "John"]))),
18-
("Ex[p] | ~Ex[x]", Succeeded $ dis (pr "Ex" [var "p"]) (neg (pr "Ex" [var "x"]))),
19-
("Ex[p] -> ~Ex[x]", Succeeded $ imp (pr "Ex" [var "p"]) (neg (pr "Ex" [var "x"]))),
20-
("Ex[p] <-> ~Vx[x]", Succeeded $ bic (pr "Ex" [var "p"]) (neg (pr "Vx" [var "x"]))),
21-
("V x . U#12[x]", Succeeded $ fa (var "x") (pr "U#12" [var "x"])),
22-
("E y . K[f(l, y, No)]", Succeeded $ te (var "y") (pr "K" [func "f" [var "l", var "y", constant "No"]])),
23-
("V z . E k. F[z] -> G[k]", Succeeded $ fa (var "z") (te (var "k") (imp (pr "F" [var "z"]) (pr "G" [var "k"])))),
24-
("E y. K[y] -> V x. Z[x, y]", Succeeded $ te (var "y") (imp (pr "K" [var "y"]) (fa (var "x") (pr "Z" [var "x", var "y"])))),
25-
("E y. K[y] -> Z[x, y]", Succeeded $ te (var "y") (imp (pr "K" [var "y"]) (pr "Z" [var "x", var "y"])))]
14+
[("Dog[ a ]", Right $ pr "Dog" [var "a"]),
15+
("~Super[ k, kill(a, b)]", Right $ neg (pr "Super" [var "k", func "kill" [var "a", var "b"]])),
16+
("Dog[x] & Owns[John, x]", Right $ con (pr "Dog" [var "x"]) (pr "Owns" [constant "John", var "x"])),
17+
("Dog [x] & (Owns[John, x] | Not[Cat, John])", Right $ con (pr "Dog" [var "x"]) (dis (pr "Owns" [constant "John", var "x"]) (pr "Not" [constant "Cat", constant "John"]))),
18+
("Ex[p] | ~Ex[x]", Right $ dis (pr "Ex" [var "p"]) (neg (pr "Ex" [var "x"]))),
19+
("Ex[p] -> ~Ex[x]", Right $ imp (pr "Ex" [var "p"]) (neg (pr "Ex" [var "x"]))),
20+
("Ex[p] <-> ~Vx[x]", Right $ bic (pr "Ex" [var "p"]) (neg (pr "Vx" [var "x"]))),
21+
("V x . U#12[x]", Right $ fa (var "x") (pr "U#12" [var "x"])),
22+
("E y . K[f(l, y, No)]", Right $ te (var "y") (pr "K" [func "f" [var "l", var "y", constant "No"]])),
23+
("V z . E k. F[z] -> G[k]", Right $ fa (var "z") (te (var "k") (imp (pr "F" [var "z"]) (pr "G" [var "k"])))),
24+
("E y. K[y] -> V x. Z[x, y]", Right $ te (var "y") (imp (pr "K" [var "y"]) (fa (var "x") (pr "Z" [var "x", var "y"])))),
25+
("E y. K[y] -> Z[x, y]", Right $ te (var "y") (imp (pr "K" [var "y"]) (pr "Z" [var "x", var "y"])))]

0 commit comments

Comments
 (0)