Skip to content

Commit e51d5f8

Browse files
committed
Replace Maybe with Except (for error messages)
1 parent a4774b5 commit e51d5f8

File tree

2 files changed

+34
-56
lines changed

2 files changed

+34
-56
lines changed

minimal-logic.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ library
1616
exposed-modules: Minimal, Model
1717
build-depends: base >= 4.7 && < 5
1818
, containers
19+
, mtl
1920
default-language: Haskell2010
2021

2122
executable minimal-logic-exe

src/Minimal.hs

Lines changed: 33 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
module Minimal where
66

7-
import Text.Printf (printf)
7+
import Control.Monad.Except
88
import Data.Set (Set, (\\))
99
import qualified Data.Set as Set
10+
import Text.Printf (printf)
1011

1112
data Formula
1213
= Proposition String
@@ -34,39 +35,15 @@ instance Show Formula where
3435
show (Disjunction a b) = printf "(%s \x2228 %s)" (show a) (show b)
3536
show (Implication a b) = printf "(%s \x2192 %s)" (show a) (show b)
3637

37-
{-
38-
- NAME NUM ASSUMPS STACK
39-
- Assume +1 { - a }
40-
- ConjIntro 0 { a b - a^b }
41-
- LDisjIntro 0 { a - a^b }
42-
- RDisjIntro 0 { b - a^b }
43-
- LConjElim 0 { a^b - b }
44-
- RConjElim 0 { a^b - a }
45-
- DisjElim 2 { avb a>c b>c - c }
46-
-
47-
- Assume : takes F and gives a proof of F from {F}
48-
- ConjIntro : takes a proof of A from G and a proof of B from H and
49-
- gives a proof of A^B from G U H
50-
- LDisjIntro : takes a proof of B from G and gives a proof of AvB from G
51-
- LDisjIntro : takes a A and proof of B from G
52-
- and gives a proof of AvB from G
53-
- RDisjIntro : takes a proof of A from G and B
54-
- and gives a proof of AvB from G
55-
- LConjElim : takes a proof of A^B from G and gives a proof of B from G
56-
- RConjElim : takes a proof of A^B from G and gives a proof of A from G
57-
- DisjElim : takes a proof of AvB from G, a proof of C from H1 U {A}
58-
- and a proof of C from H2 U {B}, and gives a proof of
59-
- C from G U H1 U H2.
60-
- ImplElim : takes a proof of A>B from G and a proof of A from H
61-
- and gives a proof of B from G U H
62-
- ImplIntro : takes a proof of B from G U {A} and gives a proof of
63-
- A>B from G
64-
-}
65-
66-
data Deduction = Deduction { assumptions :: Set Formula,
67-
conclusion :: Formula
38+
data Deduction = Deduction { assumptions :: Set Formula
39+
, conclusion :: Formula
6840
} deriving (Show)
6941

42+
type Proof = Except String Deduction
43+
44+
runProof :: Proof -> Either String Deduction
45+
runProof = runExcept
46+
7047
assume' :: Formula -> Deduction
7148
assume' f = Deduction (Set.singleton f) f
7249

@@ -75,11 +52,11 @@ implIntro' f d = Deduction (Set.delete f a) (f #> c)
7552
where a = assumptions d
7653
c = conclusion d
7754

78-
implElim :: Deduction -> Deduction -> Maybe Deduction
55+
implElim :: Deduction -> Deduction -> Proof
7956
implElim (Deduction aA a') (Deduction aAtoB (Implication a b))
80-
| (a' == a) = Just $ (Deduction (Set.union aA aAtoB) b)
81-
| otherwise = Nothing
82-
implElim _ _ = Nothing
57+
| (a' == a) = return (Deduction (Set.union aA aAtoB) b)
58+
| otherwise = throwError "conclusion of first argument must be the antecedent of the conclusion of the second argument"
59+
implElim _ _ = throwError "second argument to implElim must be an implication"
8360

8461
conjIntro' :: Deduction -> Deduction -> Deduction
8562
conjIntro' l r = Deduction (Set.union al ar) (cl #& cr)
@@ -96,43 +73,43 @@ rightDisjIntro' :: Deduction -> Formula -> Deduction
9673
rightDisjIntro' d f = Deduction (assumptions d) (l #| f)
9774
where l = conclusion d
9875

99-
leftConjElim :: Deduction -> Maybe Deduction
76+
leftConjElim :: Deduction -> Proof
10077
leftConjElim d = case (conclusion d) of
101-
(Conjunction _ r) -> Just d { conclusion = r }
102-
_ -> Nothing
78+
(Conjunction _ r) -> return d { conclusion = r }
79+
_ -> throwError "argument must be a conjunction"
10380

104-
rightConjElim :: Deduction -> Maybe Deduction
81+
rightConjElim :: Deduction -> Proof
10582
rightConjElim d = case (conclusion d) of
106-
(Conjunction l _) -> Just d { conclusion = l }
107-
_ -> Nothing
83+
(Conjunction l _) -> return d { conclusion = l }
84+
_ -> throwError "argument must be a conjunction"
10885

109-
disjElim :: Deduction -> Deduction -> Deduction -> Maybe Deduction
86+
disjElim :: Deduction -> Deduction -> Deduction -> Proof
11087
disjElim avb atoc btoc
11188
| conclusion atoc == conclusion btoc = disjElimSame
112-
| otherwise = Nothing
89+
| otherwise = throwError "conclusion of second arg and conclusion of third arg must be equal"
11390
where disjElimSame = case (conclusion avb) of
114-
(Disjunction a b) -> Just (Deduction (Set.unions [g, hA, hB]) c)
91+
(Disjunction a b) -> return (Deduction (Set.unions [g, hA, hB]) c)
11592
where g = assumptions avb
11693
hA = assumptions atoc \\ Set.singleton a
11794
hB = assumptions btoc \\ Set.singleton b
11895
c = conclusion atoc
119-
_ -> Nothing
96+
_ -> throwError "conclusion of first arg must be a disjunction"
12097

12198
-- These allow for a more consistent syntax when doing proofs
122-
assume :: Formula -> Maybe Deduction
123-
assume f = Just $ assume' f
99+
assume :: Formula -> Proof
100+
assume f = return $ assume' f
124101

125-
conjIntro :: Deduction -> Deduction -> Maybe Deduction
126-
conjIntro l r = Just $ conjIntro' l r
102+
conjIntro :: Deduction -> Deduction -> Proof
103+
conjIntro l r = return $ conjIntro' l r
127104

128-
implIntro :: Formula -> Deduction -> Maybe Deduction
129-
implIntro f d = Just $ implIntro' f d
105+
implIntro :: Formula -> Deduction -> Proof
106+
implIntro f d = return $ implIntro' f d
130107

131-
leftDisjIntro :: Formula -> Deduction -> Maybe Deduction
132-
leftDisjIntro f d = Just $ leftDisjIntro' f d
108+
leftDisjIntro :: Formula -> Deduction -> Proof
109+
leftDisjIntro f d = return $ leftDisjIntro' f d
133110

134-
rightDisjIntro :: Deduction -> Formula -> Maybe Deduction
135-
rightDisjIntro d f = Just $ rightDisjIntro' d f
111+
rightDisjIntro :: Deduction -> Formula -> Proof
112+
rightDisjIntro d f = return $ rightDisjIntro' d f
136113

137114
exampleVacuousIntro = do
138115
let p = Proposition "P"

0 commit comments

Comments
 (0)