-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCoreTypes.hs
172 lines (144 loc) · 4.24 KB
/
CoreTypes.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
{-# LANGUAGE OverloadedStrings , DeriveDataTypeable #-}
module CoreTypes where
import GHC.Exts( IsString(..) )
import Data.Function
import Data.Set (Set)
import qualified Data.Set as S
import Data.Typeable
instance IsString Var where
fromString = UVar
instance IsString Term where
fromString = Var . fromString
type Ident = String
data Ctx = Ctx
{ predicates :: Set (Ident, [Term])
, good :: Set Formula
, goodSplit :: Set Formula
, bad :: Set Formula
}
empty :: Ctx
empty = Ctx
{ predicates = S.empty
, good = S.empty
, goodSplit = S.empty
, bad = S.empty
}
ctxToSet :: Ctx -> Set Formula
ctxToSet ctx = S.unions
[ S.map (uncurry Rel) (predicates ctx)
, good ctx
, goodSplit ctx
, bad ctx
]
instance Show Ctx where
show = show . ctxToSet
getPredicates :: Ctx -> Set (Ident, [Term])
getPredicates = predicates
getGood :: Ctx -> Maybe (Formula , Ctx)
getGood ctx | S.null (good ctx) = Nothing
| otherwise = Just (x , ctx { good = xs })
where (x , xs) = S.deleteFindMin (good ctx)
getGoodSplit :: Ctx -> Maybe (Formula , Ctx)
getGoodSplit ctx | S.null (goodSplit ctx) = Nothing
| otherwise = Just (x , ctx { goodSplit = xs })
where (x , xs) = S.deleteFindMin (goodSplit ctx)
getBad :: Ctx -> Set Formula
getBad = bad
infixr 4 +=>
infixr 4 =>+
infixr 4 -:
(+=>) :: Formula -> Ctx -> Ctx
(+=>) = insertLeft
(=>+) :: Formula -> Ctx -> Ctx
(=>+) = insertRight
(-:) :: Formula -> Ctx -> Ctx
(-:) (Rel i ts) ctx = ctx {predicates = S.delete (i,ts) (predicates ctx)}
(-:) form ctx = ctx { good = S.delete form (good ctx)
, goodSplit = S.delete form (goodSplit ctx)
, bad = S.delete form (bad ctx)
}
(+:+) :: Ctx -> Ctx -> Ctx
c1 +:+ c2 = Ctx
{ predicates = f predicates
, good = f good
, goodSplit = f goodSplit
, bad = f bad
}
where f c = on S.union c c1 c2
insertLeft :: Formula -> Ctx -> Ctx
insertLeft form gamma = case form of
Top -> inGood
Bot -> inGood
Imp _ _ -> inGoodSplit
Equiv _ _ -> inGoodSplit
Rel i ts -> gamma { predicates = S.insert (i , ts) (predicates gamma) }
Neg _ -> inGood
And _ _ -> inGood
Or _ _ -> inGoodSplit
ForAll _ _ -> inBad
Exists _ _ -> inGood
where
inGood = gamma { good = S.insert form (good gamma) }
inGoodSplit = gamma { goodSplit = S.insert form (goodSplit gamma) }
inBad = gamma {bad = S.insert form (bad gamma) }
insertRight :: Formula -> Ctx -> Ctx
insertRight form gamma = case form of
Top -> inGood
Bot -> inGood
Imp _ _ -> inGood
Equiv _ _ -> inGoodSplit
Rel i ts -> gamma { predicates = S.insert (i , ts) (predicates gamma) }
Neg _ -> inGood
And _ _ -> inGoodSplit
Or _ _ -> inGood
ForAll _ _ -> inGood
Exists _ _ -> inBad
where
inGood = gamma { good = S.insert form (good gamma) }
inGoodSplit = gamma { goodSplit = S.insert form (goodSplit gamma) }
inBad = gamma {bad = S.insert form (bad gamma) }
type MVar = Int
data Var
= GVar Int [MVar] -- ^ generated
| UVar String -- ^ from proof
| MVar MVar -- ^ meta variable
deriving (Eq, Ord, Show, Typeable)
data Term
= Var Var
| App Ident [Term]
| Const Ident
deriving (Eq,Ord,Show, Typeable)
data Formula
= Top | Bot
| Imp Formula Formula
| Equiv Formula Formula
-- ^ A->B /\ B -> A, this is not the = relation (since it takes formulas)
| Rel Ident [Term]
| Neg Formula
| And Formula Formula
| Or Formula Formula
| ForAll Var Formula
| Exists Var Formula
deriving (Eq,Ord, Show, Typeable)
infixr 2 ~>
(~>) = Imp
(<~>) a b = Equiv a b -- (a ~> b) `And` (b ~> a)
data Deduction
= Intro Ctx Ctx
| LBot Ctx Ctx
| RTop Ctx Ctx
| RImp Formula Formula Deduction
| LImp Formula Formula Deduction Deduction
| REqu Formula Formula Deduction Deduction
| LEqu Formula Formula Deduction Deduction
| RNeg Formula Deduction
| LNeg Formula Deduction
| RAnd Formula Formula Deduction Deduction
| ROr Formula Formula Deduction
| LAnd Formula Formula Deduction
| LOr Formula Formula Deduction Deduction
| LForAll Formula Formula Deduction
| LExists Formula Formula Deduction
| RForAll Formula Formula Deduction
| RExists Formula Formula Deduction
deriving Show