forked from VitorPinheiro/SequentProver
-
Notifications
You must be signed in to change notification settings - Fork 3
/
ParseInput.lua
350 lines (279 loc) · 9.62 KB
/
ParseInput.lua
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
------------------------------------------------------------------------------
-- Parse Input Module
--
-- Utilitary module to parse formula input.
--
-- @authors: Vitor, Hermann, Jefferson, Bernardo
--
-------------------------------------------------------------------------------
require "Logic/Set"
require "logging"
require "logging.file"
local lpeg = require "lpeg"
local atom_count = 0
local mimp_t = Set:new()
local logger = logging.file("aux/prover%s.log", "%Y-%m-%d")
logger:setLevel(logging.INFO)
-- Parsing functions
local function table_atom(x)
return (string.format("\"%s\"", x))
end
local function table_formulas(t)
if #t > 0 then
local s = ""
for i=1,#t do
p = p.." ( "..i..table_formula(t[i]).." ) "
end
else
return(t.tag.."empty")
end
end
local function print_Goal(t)
io.write("[ Goal ")
if #t > 0 then
print_formulas(t[1])
-- TODO modificar aqui, ver como remover o SEQ
io.write(" SEQ ")
print_formulas(t[2])
end
io.write("]")
end
local function print_ast(t)
if t.tag == "Goal" then
print_Goal(t)
end
print("nothing to print")
end
local function table_formula(t)
if type(t) == "number" then
return(t)
elseif type(t) == "string" then
return(string.format("%s", t))
elseif type(t) == "table" then
local s = "{ "
for k,v in pairs(t) do
s = s.."[ "..table_formula(k).."="..table_formula(v).." ]"
end
s = s.." }"
return(s)
else
print("cannot convert table object")
end
end
-- Lexical Elements
local Space = lpeg.S(" \n\t")
local skip = Space^0
local upper = lpeg.R("AZ")
local lower = lpeg.R("az")
local letter = upper + lower
local dig = lpeg.R("09")^0
local Atom = lpeg.C(letter * dig) * skip
local function getcontents(filename)
file = assert(io.open(filename, "r"))
contents = file:read("*a")
file:close()
return contents
end
local function token(pat)
return pat * skip
end
local function kw(str)
return token (lpeg.P(str))
end
local function symb(str)
return token (lpeg.P(str))
end
local function taggedCap(tag, pat)
return lpeg.Ct(lpeg.Cg(lpeg.Cc(tag), "tag") * pat)
end
function parse_input(contents)
-- Grammar
local formula = lpeg.V("formula")
local form, factor, term = lpeg.V("form"), lpeg.V("factor"), lpeg.V("term")
local term_imp, term_and, term_or, term_bot, term_not = lpeg.V("term_imp"), lpeg.V("term_and"), lpeg.V("term_or"), lpeg.V("term_bot"), lpeg.V("term_not")
local Atomo = taggedCap("Atom", token(Atom))
G = lpeg.P{
formula,
formula = skip * form * skip * -1;
form = term + factor;
factor = taggedCap("Atom", token(Atom)) + symb("(") * form * symb(")");
term = term_imp + term_and + term_or + term_bot + term_not;
term_imp = taggedCap("imp", factor * kw("imp") * symb("(") * form * symb(")"));
term_and = taggedCap("and", factor * kw("and") * symb("(") * form * symb(")"));
term_or = taggedCap("or", factor * kw("or") * symb("(") * form * symb(")"));
term_bot = taggedCap("bottom", kw("bottom"));
term_not = taggedCap("not", kw("not") * symb("(") * form * symb(")"));
}
local t = lpeg.match(G, contents)
assert(t, "falha no reconhecimento de "..contents)
ast = table_formula(t)
return(ast)
end
-- MIMP translation functions
local function fresh_atom()
local ret = "p"..atom_count
atom_count = atom_count + 1
return ret
end
-- Recursivelly replace formulas by propositional letters
--
-- @param formula a formula, in table format which will have its
-- non-MIMP subformulas translated in new propostional
-- letters.
--
-- @return a propositonal representation of non-MIMP (sub)formulas as string
local function mimp(formula)
local s_formula = convert_formula_tostring(formula)
if mimp_t[s_formula] then
return mimp_t[s_formula]
else
if formula["tag"] == "Atom" then
return formula["1"]
elseif formula["tag"] == "imp" then
return "("..mimp(formula["1"]).." imp ("..mimp(formula["2"]).."))"
elseif formula["tag"] == "and" then
return fresh_atom()
elseif formula["tag"] == "or" then
return fresh_atom()
elseif formula["tag"] == "not" then
return "("..mimp(formula["1"]).." imp (f))"
end
end
end
-- Recursivelly builds a set of axioms to make the translation to MIMP Logic
--
-- @param alpha the initial formula to be translated, in table format.
-- @param formula subformulas of the initial formula to be examined recursivelly.
-- @return a set of axioms of MIMP Logic in table format.
local function axioms(alpha, formula)
local set1
local set2
if formula["tag"] == "Atom" then
return Set:new()
elseif formula["tag"] == "imp" then
set1 = axioms(alpha, formula["1"])
set2 = axioms(alpha, formula["2"])
return set1:union(set2)
elseif formula["tag"] == "and" then
local s_formula = convert_formula_tostring(formula)
local formula_esq_s = convert_formula_tostring(formula["1"])
local formula_dir_s = convert_formula_tostring(formula["2"])
local p = mimp_t[s_formula]
local axiom1 = parse_input(mimp_t[formula_esq_s].." imp ("..mimp_t[formula_dir_s].." imp ("..p.."))")
local axiom2 = parse_input(p.." imp ("..mimp_t[formula_esq_s]..")")
local axiom3 = parse_input(p.." imp ("..mimp_t[formula_dir_s]..")")
local axiom1_t = convert_formula_totable(axiom1)
local axiom2_t = convert_formula_totable(axiom2)
local axiom3_t = convert_formula_totable(axiom3)
local axioms_set = Set:new({axiom1_t, axiom2_t, axiom3_t})
set1 = axioms_set:union(axioms(alpha, formula["1"]))
set2 = set1:union(axioms(alpha, formula["2"]))
return set2
elseif formula["tag"] == "or" then
local s_formula = convert_formula_tostring(formula)
local formula_esq_s = convert_formula_tostring(formula["1"])
local formula_dir_s = convert_formula_tostring(formula["2"])
local q = mimp_t[s_formula]
local axiom1 = parse_input(mimp_t[formula_esq_s].." imp ("..q..")")
local axiom2 = parse_input(mimp_t[formula_dir_s].." imp ("..q..")")
local axiom1_t = convert_formula_totable(axiom1)
local axiom2_t = convert_formula_totable(axiom2)
local axioms_set = Set:new({axiom1_t, axiom2_t})
local axioms_sub = nil
local axioms_sub_t = nil
local axioms_sub_set = Set:new()
for k,_ in pairs(mimp_t) do
axioms_sub = parse_input("("..mimp_t[formula_esq_s].." imp (".. mimp_t[k]..")) imp (("..mimp_t[formula_dir_s].." imp ("..mimp_t[k]..")) imp (("..
q.." imp ("..mimp_t[k].."))))")
axioms_sub_t = convert_formula_totable(axioms_sub)
axioms_sub_set:add(axioms_sub_t)
end
local set_or = axioms_set:union(axioms_sub_set)
set1 = set_or:union(axioms(alpha, formula["1"]))
set2 = set1:union(axioms(alpha, formula["2"]))
return set2
elseif formula["tag"] == "not" then
set1 = axioms(alpha, formula["1"])
return set1
end
end
local function subformulas(formula)
local subformulas_set
if formula["tag"] == "Atom" then
return Set:new({convert_formula_tostring(formula)})
else
local sub1_set = subformulas(formula["1"])
subformulas_set = sub1_set
if formula["tag"] ~= "not" then
local sub2_set = subformulas(formula["2"])
subformulas_set = subformulas_set:union(sub2_set)
end
local actual_formula = Set:new({convert_formula_tostring(formula)})
subformulas_set = subformulas_set:union(actual_formula)
return subformulas_set
end
end
local function compare_formulas_size(a, b)
return (string.len(a) < string.len(b))
end
-- Translates formulas from Intuitionistic Logic to Minimal Implicational Logic
--
-- @param t_formula a formula parsed into table format
-- @return a translated formula in table format
function implicational(t_formula)
local s_formula = convert_formula_tostring(t_formula)
local subformulas_set = subformulas(t_formula)
local l = {}
for k,_ in pairs(subformulas_set) do
table.insert(l, k)
end
table.sort(l, compare_formulas_size)
for _,v in ipairs(l) do
local new_v = mimp(convert_formula_totable(parse_input(v)))
mimp_t[v] = new_v
end
local axiom_set = axioms(t_formula, t_formula)
local alpha = convert_formula_totable(parse_input(mimp_t[s_formula]))
for k,_ in pairs(axiom_set) do
local s_k = convert_formula_tostring(k)
local s_alpha = convert_formula_tostring(alpha)
alpha = {["1"] = k, ["2"] = alpha, ["tag"] = "imp"}
end
return alpha
end
-- String to Table/Table to String convertion functions
function convert_formula_totable(s)
local t = {}
if s == nil then
return
end
for k in string.gmatch(s, "(%b[])") do
v1=string.match(k,"%[%s*([^=]+)=.+")
if v1 =="tag" then
v1,v2= string.match(k,"%[%s*([^=]+)=(%S+)%s")
t[v1]=v2
else
if v1 then
v2=string.match(k,"(%b{})")
if v2 then
t[v1] = convert_formula_totable(v2,client)
else
v2=string.match(k,"[^=]+=%s*(%S+)%s*%]")
t[v1]= v2
end
end
end
end
return(t)
end
function convert_formula_tostring(t)
local s = ""
if t["tag"] == "Atom" then
s = t["1"]
elseif t["tag"] == "not" then
s = t["tag"].."("..convert_formula_tostring(t["1"])..") "
else
s = "("..convert_formula_tostring(t["1"])..") "..t["tag"].." ("..convert_formula_tostring(t["2"])..")"
end
return s
end