This repository was archived by the owner on Jul 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrueFOLFormulasOverR.hs
More file actions
296 lines (224 loc) · 11.5 KB
/
TrueFOLFormulasOverR.hs
File metadata and controls
296 lines (224 loc) · 11.5 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
-- Decision algorithm for real numbers, list of true sentences
-- Authors: Philip Lukert, Felix Mujkanovic
-- Date: 05 September 2018
import Data.List
import Data.Maybe
data Term = Zero | One | Var Integer | Neg Term | Inv Term | Add Term Term | Mul Term Term deriving (Eq)
data Proposition = Equals Term Term | Less Term Term deriving (Eq)
data Formula = Prop Proposition | Not Formula | Imp Formula Formula | Forall Integer Formula deriving (Eq)
instance Show Term where
show Zero = "0"
show One = "1"
show (Var 0) = "x"
show (Var 1) = "y"
show (Var 2) = "z"
show (Var n) = "x" ++ show n
show (Neg t) = "-" ++ show t
show (Inv t) = show t ++ "⁻¹"
show (Add t (Neg u)) = "⟨" ++ show t ++ "-" ++ show u ++ "⟩"
show (Add t u) = "⟨" ++ show t ++ "+" ++ show u ++ "⟩"
show (Mul t (Inv u)) = "⟨" ++ show t ++ "÷" ++ show u ++ "⟩"
show (Mul t u) = "⟨" ++ show t ++ "⋅" ++ show u ++ "⟩"
instance Show Proposition where
show (Equals p q) = show p ++ "=" ++ show q
show (Less p q) = show p ++ "<" ++ show q
instance Show Formula where
show (Prop p) = show p
show (Not f) = "¬" ++ show f
show (Imp f g) = "(" ++ show f ++ " → " ++ show g ++ ")"
show (Forall n f) = "∀" ++ show (Var n) ++ ". " ++ show f ++ ""
-- What follows is a definition of all the basic "static" axioms of ℝ.
orr x y = Imp (Not x) y
andd x y = Not $ Imp x (Not y)
axiomsAdd = [
Forall 0 $ Prop $ Equals (Add (Var 0) Zero) (Var 0),
Forall 0 $ Forall 1 $ Prop $ Equals (Add (Var 0) (Var 1))(Add (Var 1) (Var 0)),
Forall 0 $ Forall 1 $ Forall 2 $ Prop $ Equals (Add (Var 0) (Add (Var 1)(Var 2))) (Add (Add (Var 0) (Var 1)) (Var 2)),
Forall 0 $ Not $ Forall 1 $ Not $ Prop $ Equals (Add (Var 0) (Var 1)) Zero
]
axiomsMul = [
Forall 0 $ Prop $ Equals (Mul (Var 0) One) (Var 0),
Forall 0 $ Forall 1 $ Prop $ Equals (Mul (Var 0) (Var 1))(Mul (Var 1) (Var 0)),
Forall 0 $ Forall 1 $ Forall 2 $ Prop $ Equals (Mul (Var 0) (Mul (Var 1)(Var 2))) (Mul (Mul (Var 0) (Var 1)) (Var 2)),
Forall 0 $ Imp (Not (Prop (Equals (Var 0) Zero))) $ Not $ Forall 1 $ Not $ Prop $ Equals (Mul (Var 0) (Var 1)) One
]
axiomsSpecial = [Not $ Prop $ Equals One Zero,
Forall 0 $ Forall 1 $ Forall 2 $ Prop $ Equals (Mul (Var 0) (Add (Var 1)(Var 2))) (Add (Mul (Var 0)(Var 1))(Mul (Var 0)(Var 2)))
]
axiomsRel = [
Forall 0 $ orr (Prop (Less (Var 0) Zero)) $ orr (Prop (Equals (Var 0) Zero)) $ Prop (Less Zero (Var 0)),
Forall 0 $ Forall 1 $ Forall 2 $ Imp (andd (Prop (Less (Var 0) (Var 1))) (Prop (Less (Var 1) (Var 2)))) (Prop (Less (Var 0) (Var 2))),
Forall 0 $ Not $ Prop $ Less (Var 0) (Var 0),
Forall 0 $ Forall 1 $ Forall 2 $ Imp (Prop (Less (Var 0)(Var 1))) (Prop(Less (Add (Var 0)(Var 2))(Add (Var 1)(Var 2)))),
Forall 0 $ Forall 1 $ Forall 2 $ Imp (andd (Prop(Less (Var 0)(Var 1)))(Prop(Less Zero (Var 2)))) (Prop(Less(Mul (Var 0)(Var 2))(Mul(Var 1)(Var 2))))
]
staticAxioms =
axiomsAdd ++
axiomsMul ++
axiomsSpecial ++
axiomsRel
replaceT n (Zero) t = Zero
replaceT n (One) t = One
replaceT n (Var v) t = if v == n then t else Var v
replaceT n (Add p q) t = Add (replaceT n p t) (replaceT n q t)
replaceT n (Mul p q) t = Mul (replaceT n p t) (replaceT n q t)
replaceP n (Equals p q) t = Equals (replaceT n p t) (replaceT n q t)
replaceP n (Less p q) t = Less (replaceT n p t) (replaceT n q t)
replaceF n (Prop p) t = Prop (replaceP n p t)
replaceF n (Not f) t = Not (replaceF n f t)
replaceF n (Imp f g) t = Imp (replaceF n f t) (replaceF n g t)
replaceF n (Forall x f) t = Forall n (replaceF x f t)
canInsert :: Integer -> Formula -> Term -> Bool
canInsert n (Prop p) t = True
canInsert n (Not f) t = canInsert n f t
canInsert n (Imp f g) t = (canInsert n f t) && (canInsert n g t)
canInsert n (Forall x f) t = canInsert n f t && ((not (n `elem` (varsInFormula f))) || (not (x `elem` (varsInTerm t))))
-- An infinite list of all terms, grouped in sublists of increasing length.
terms :: [[Term]]
terms = growLengthwise [negGrower, invGrower] [addGrower, mulGrower] ([Zero, One, Var 0] : map (\x -> [Var x]) [1..])
where
negGrower :: Term -> [Term]
negGrower t = [Neg t]
invGrower :: Term -> [Term]
invGrower t = [Inv t]
addGrower :: Term -> Term -> [Term]
addGrower t u = [Add t u]
mulGrower :: Term -> Term -> [Term]
mulGrower t u = [Mul t u]
-- An infinite list of all propositions, grouped in sublists of increasing length.
-- This list contains both true and false propositions.
propositions :: [[Proposition]]
propositions = combineLengthwise [equalsCombinator, lessCombinator] [] terms
where
equalsCombinator :: Term -> Term -> [Proposition]
equalsCombinator t u = [Equals t u]
lessCombinator :: Term -> Term -> [Proposition]
lessCombinator t u = [Less t u]
-- An infinite list of all first order logic formulas, grouped in sublists of increasing length.
-- This list contains both true and false formulas.
formulas :: [[Formula]]
formulas = map (filter formulaValid) $ growLengthwise [notGrower, forallGrower] [impGrower] (map (map (\p -> Prop p)) propositions)
where
notGrower :: Formula -> [Formula]
notGrower f = [Not f]
forallGrower :: Formula -> [Formula]
forallGrower f = [Forall x f | x <- freeVarsInFormula f]
impGrower :: Formula -> Formula -> [Formula]
impGrower f g = [Imp f g]
formulaValid :: Formula -> Bool
formulaValid f = freeVarsInFormula f == []
-- An infinite list of all TRUE first order logic formulas you can obtain by putting
-- all possible formulas into the basic axioms of first order logic.
-- Note that the resulting list of formulas is no longer strictly ordered by length since
-- the axioms do more advanced stuff than adding just one operator.
-- However, the list is still kind of ordered.
laxiomInsts :: [Formula]
laxiomInsts = concat $ combineLengthwise [laxiom1, laxiom3, laxiom4, laxiom5] [laxiom2] formulas
where
laxiom1 :: Formula -> Formula -> [Formula]
laxiom1 a b = [Imp a (Imp b a)]
laxiom2 :: Formula -> Formula -> Formula -> [Formula]
laxiom2 a b c = [Imp (Imp a (Imp b c)) (Imp (Imp a b) (Imp a c))]
laxiom3 :: Formula -> Formula -> [Formula]
laxiom3 a b = [Imp (Imp (Not b) (Not a)) (Imp a b)]
laxiom4 :: Formula -> Formula -> [Formula]
laxiom4 a (Prop (Equals t1 t2)) = [replaceF x a t1 | x <- freeVarsInFormula a] ++ [replaceF x a t2 | x <- freeVarsInFormula a]
laxiom4 a (Prop (Less t1 t2)) = [replaceF x a t1 | x <- freeVarsInFormula a] ++ [replaceF x a t2 | x <- freeVarsInFormula a]
laxiom4 _ _ = []
laxiom5 :: Formula -> Formula -> [Formula]
laxiom5 a b = [Imp (Forall x (Imp a b)) $ Imp a $ Forall x b | x <- freeVarsInFormula b, x `notElem` freeVarsInFormula a]
varsInTerm :: Term -> [Integer]
varsInTerm (Var n) = [n]
varsInTerm (Add t u) = (varsInTerm t) ++ (varsInTerm u)
varsInTerm (Mul t u) = (varsInTerm t) ++ (varsInTerm u)
varsInTerm _ = []
varsInProp :: Proposition -> [Integer]
varsInProp (Equals p q) = (varsInTerm p) ++ (varsInTerm q)
varsInProp (Less p q) = (varsInTerm p) ++ (varsInTerm q)
varsInFormula :: Formula -> [Integer]
varsInFormula (Prop p) = varsInProp p
varsInFormula (Not f) = varsInFormula f
varsInFormula (Imp f g) = (varsInFormula f) ++ (varsInFormula g)
varsInFormula (Forall var f) = varsInFormula f
freeVarsInFormula :: Formula -> [Integer]
freeVarsInFormula (Prop p) = varsInProp p
freeVarsInFormula (Not f) = freeVarsInFormula f
freeVarsInFormula (Imp f g) = (freeVarsInFormula f) ++ (freeVarsInFormula g)
freeVarsInFormula (Forall var f) = deleteAll var (freeVarsInFormula f)
-- The final infinite list of all TRUE formulas.
-- It is generated by taking the axioms of ℝ, slowly adding laxiomInsts and repeatedly
-- applying modus ponens and generalization over time.
trueFormulas :: [Formula]
trueFormulas = staticAxioms ++ (iter staticAxioms laxiomInsts)
where
iter :: [Formula] -> [Formula] -> [Formula]
iter prevTrueFormulas (a:axioms) = let newTrueFormulas = (step prevTrueFormulas a)
in newTrueFormulas ++ (iter (prevTrueFormulas ++ newTrueFormulas) axioms)
step :: [Formula] -> Formula -> [Formula]
step old newAxiom = [newAxiom] ++ (nub $ applyMponens old [newAxiom]) ++ (applyGeneralization newAxiom)
applyMponens :: [Formula] -> [Formula] -> [Formula]
applyMponens [] _ = []
applyMponens _ [] = []
applyMponens fs gs = concat [(applyMponensSingle f g) ++
(applyMponensSingle g f) | f <- fs, g <- gs]
where
applyMponensSingle :: Formula -> Formula -> [Formula]
applyMponensSingle f (Imp p c) = if f == p
then [c]
else []
applyMponensSingle _ _ = []
applyGeneralization :: Formula -> [Formula]
applyGeneralization f = foldl (\list x -> ((Forall x f):list)) [] (freeVarsInFormula f)
---------------
-- Utilities --
---------------
deleteAll :: (Eq a) => a -> [a] -> [a]
deleteAll e = filter (/=e)
nthl :: [[a]] -> Int -> [a]
nthl [] _ = []
nthl (x:_) 1 = x
nthl (_:xs) n = nthl xs (n-1)
combineLengthwise :: [a -> a -> [b]] -> [a -> a -> a -> [b]] -> [[a]] -> [[b]]
combineLengthwise bCombinators tCombinators sourceLenGroups = doCL 1
where
-- doCL :: Int -> [[b]]
doCL currLen = currLenGroup : doCL (currLen+1)
where
-- currLenGroup :: [b]
currLenGroup =
concat [bCombinator a1 a2 | bCombinator <- bCombinators,
grp1 <- [1..(currLen-2)], let grp2 = currLen - grp1 - 1,
a1 <- sourceLenGroups `nthl` grp1,
a2 <- sourceLenGroups `nthl` grp2] ++
concat [tCombinator a1 a2 a3 | tCombinator <- tCombinators,
grp1 <- [1..(currLen-4)],
grp2 <- [1..(currLen-grp1-3)], let grp3 = currLen - grp1 - grp2 - 2,
a1 <- sourceLenGroups `nthl` grp1,
a2 <- sourceLenGroups `nthl` grp2,
a3 <- sourceLenGroups `nthl` grp3]
growLengthwise :: [a -> [a]] -> [a -> a -> [a]] -> [[a]] -> [[a]]
growLengthwise uGrowers bGrowers initLenGroups = doGL initLenGroups []
where
-- doGL :: [[a]] -> [[a]] -> [[a]]
doGL (currInitLenGroup:nextInitLenGroups) prevLenGroups = currLenGroup : (doGL nextInitLenGroups (prevLenGroups ++ [currLenGroup]))
where
currLen :: Int
currLen = length prevLenGroups + 1
-- currLenGroup :: [a]
currLenGroup =
currInitLenGroup ++
concat [uGrower a | uGrower <- uGrowers,
a <- (prevLenGroups `nthl` (currLen-1))] ++
concat [bGrower a1 a2 | bGrower <- bGrowers,
grp1 <- [1..(currLen-2)], let grp2 = currLen - grp1 - 1,
a1 <- prevLenGroups `nthl` grp1,
a2 <- prevLenGroups `nthl` grp2]
printListWithNewlines :: (Show a) => [a] -> IO ()
printListWithNewlines xs = putStr $ (concat . intersperse "\n" . map (\(n,s) -> show n ++ "\t" ++ show s) . zip [1..] $ xs) ++ "\n"
printTerms = printListWithNewlines $ concat terms
printPropositions = printListWithNewlines $ concat propositions
printFormulas = printListWithNewlines $ concat formulas
printLaxiomInsts = printListWithNewlines laxiomInsts
printStaticAxioms = printListWithNewlines staticAxioms
printTrueFormulas = printListWithNewlines trueFormulas
main = printTrueFormulas