aboutsummaryrefslogtreecommitdiffstats
path: root/Solvers/Vampire-Solver/ca.mcgill.ecse.dslreasoner.vampire.reasoner/src/ca/mcgill/ecse/dslreasoner/vampire/reasoner/builder/Logic2VampireLanguageMapper_Support.xtend
blob: 44efd84eec72d463ee5a105d40a2c2da895d459e (plain) (blame)
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package ca.mcgill.ecse.dslreasoner.vampire.reasoner.builder

import ca.mcgill.ecse.dslreasoner.vampire.reasoner.BackendSolver
import ca.mcgill.ecse.dslreasoner.vampireLanguage.VLSConstant
import ca.mcgill.ecse.dslreasoner.vampireLanguage.VLSFunction
import ca.mcgill.ecse.dslreasoner.vampireLanguage.VLSFunctionAsTerm
import ca.mcgill.ecse.dslreasoner.vampireLanguage.VLSInequality
import ca.mcgill.ecse.dslreasoner.vampireLanguage.VLSTerm
import ca.mcgill.ecse.dslreasoner.vampireLanguage.VLSVariable
import ca.mcgill.ecse.dslreasoner.vampireLanguage.VampireLanguageFactory
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.ComplexTypeReference
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.QuantifiedExpression
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Term
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Type
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Variable
import java.util.ArrayList
import java.util.HashMap
import java.util.List
import java.util.Map
import java.util.concurrent.TimeUnit
import okhttp3.MediaType
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody
import okhttp3.Response
import org.eclipse.emf.common.util.EList

import static extension hu.bme.mit.inf.dslreasoner.util.CollectionsUtil.*

class Logic2VampireLanguageMapper_Support {
	private val extension VampireLanguageFactory factory = VampireLanguageFactory.eINSTANCE

// ID Handler
	def protected String toIDMultiple(String... ids) {
		ids.map[it.split("\\s+").join("_")].join("_")
	}

	def protected String toID(String ids) {
		ids.split("\\s+").join("_")
	}

// Term Handling
// TODO Make more general
	def protected VLSVariable duplicate(VLSVariable term) {
		return createVLSVariable => [it.name = term.name]
	}

	def protected VLSFunctionAsTerm duplicate(VLSFunctionAsTerm term) {
		return createVLSFunctionAsTerm => [it.functor = term.functor]
	}

	def protected VLSConstant duplicate(VLSConstant term) {
		return createVLSConstant => [it.name = term.name]
	}

	def protected VLSFunction duplicate(VLSFunction term) {
		return createVLSFunction => [
			it.constant = term.constant
			for (v : term.terms) {
				it.terms += duplicate(v as VLSVariable)
			}
		]
	}

	def protected VLSFunction duplicate(VLSFunction term, VLSVariable v) {
		return createVLSFunction => [
			it.constant = term.constant
			it.terms += duplicate(v)
		]
	}

	def protected VLSFunction duplicate(VLSFunction term, List<VLSVariable> vars) {
		return createVLSFunction => [
			it.constant = term.constant
			for (v : vars) {
				it.terms += duplicate(v)
			}
		]
	}

	def protected VLSFunction duplicate(VLSFunction term, VLSFunctionAsTerm v) {
		return createVLSFunction => [
			it.constant = term.constant
			it.terms += duplicate(v)
		]
	}

	def protected List<VLSVariable> duplicate(List<VLSVariable> vars) {
		var newList = newArrayList
		for (v : vars) {
			newList.add(duplicate(v))
		}
		return newList
	}

	def protected VLSConstant toConstant(VLSFunctionAsTerm term) {
		return createVLSConstant => [
			it.name = term.functor
		]
	}

	def protected VLSFunction topLevelTypeFunc() {
		return createVLSFunction => [
			it.constant = "object"
			it.terms += createVLSVariable => [
				it.name = "A"
			]
		]
	}

	def protected VLSFunction topLevelTypeFunc(VLSVariable v) {
		return createVLSFunction => [
			it.constant = "object"
			it.terms += duplicate(v)
		]
	}

	def protected VLSFunction topLevelTypeFunc(VLSFunctionAsTerm v) {
		return createVLSFunction => [
			it.constant = "object"
			it.terms += duplicate(v)
		]
	}

// TODO Make more general
	def establishUniqueness(List<VLSConstant> terms, VLSConstant t2) {

//		OLD
//		val List<VLSInequality> eqs = newArrayList
//		for (t1 : terms.subList(1, terms.length)) {
//			for (t2 : terms.subList(0, terms.indexOf(t1))) {
//				val eq = createVLSInequality => [
//					// TEMP
//					it.left = createVLSConstant => [it.name = t2.name]
//					it.right = createVLSConstant => [it.name = t1.name]
//				// TEMP
//				]
//				eqs.add(eq)
//			}
//		}
//		return unfoldAnd(eqs)
//		END OLD
		val List<VLSInequality> eqs = newArrayList
		for (t1 : terms) {
			if (t1 != t2) {
				val eq = createVLSInequality => [
					it.left = createVLSConstant => [it.name = t2.name]
					it.right = createVLSConstant => [it.name = t1.name]
				]
				eqs.add(eq)
			}
		}
		return unfoldAnd(eqs)
	}

// Support Functions
// booleans
// AND and OR
	def protected VLSTerm unfoldAnd(List<? extends VLSTerm> operands) {
		if (operands.size == 1) {
			return operands.head
		} else if (operands.size > 1) {
			return createVLSAnd => [
				left = operands.head
				right = operands.subList(1, operands.size).unfoldAnd
			]
		} else
			throw new UnsupportedOperationException('''Logic operator with 0 operands!''')
	}

	def protected VLSTerm unfoldOr(List<? extends VLSTerm> operands) {
//		if(operands.size == 0) {basically return true}
		/*else*/ if (operands.size == 1) {
			return operands.head
		} else if (operands.size > 1) {
			return createVLSOr => [
				left = operands.head
				right = operands.subList(1, operands.size).unfoldOr
			]
		} else
			throw new UnsupportedOperationException('''Logic operator with 0 operands!''') // TEMP
	}

// can delete below
	def protected VLSTerm unfoldDistinctTerms(Logic2VampireLanguageMapper m, EList<Term> operands,
		Logic2VampireLanguageMapperTrace trace, Map<Variable, VLSVariable> variables) {
		if (operands.size == 1) {
			return m.transformTerm(operands.head, trace, variables)
		} else if (operands.size > 1) {
			val notEquals = new ArrayList<VLSTerm>(operands.size * operands.size / 2)
			for (i : 0 ..< operands.size) {
				for (j : i + 1 ..< operands.size) {
					notEquals += createVLSInequality => [
						it.left = m.transformTerm(operands.get(i), trace, variables)
						it.right = m.transformTerm(operands.get(j), trace, variables)
					]
				}
			}
			return notEquals.unfoldAnd
		} else
			throw new UnsupportedOperationException('''Logic operator with 0 operands!''')
	}

// Symbolic
// def postprocessResultOfSymbolicReference(TypeReference type, VLSTerm term, Logic2VampireLanguageMapperTrace trace) {
//		if(type instanceof BoolTypeReference) {
//			return booleanToLogicValue(term ,trace)
//		}
//		else return term
//	}
//	def booleanToLogicValue(VLSTerm term, Logic2VampireLanguageMapperTrace trace) {
//		throw new UnsupportedOperationException("TODO: auto-generated method stub")
//	}
	/*
	 * def protected  String toID(List<String> ids) {
	 * 	ids.map[it.split("\\s+").join("'")].join("'")
	 * }
	 */
// QUANTIFIERS + VARIABLES
	def protected VLSTerm createQuantifiedExpression(Logic2VampireLanguageMapper mapper,
		QuantifiedExpression expression, Logic2VampireLanguageMapperTrace trace, Map<Variable, VLSVariable> variables,
		boolean isUniversal) {
		val variableMap = expression.quantifiedVariables.toInvertedMap [ v |
			createVLSVariable => [it.name = toIDMultiple("V", v.name)]
		]

		val typedefs = new ArrayList<VLSTerm>
		for (variable : expression.quantifiedVariables) {
			val eq = duplicate((variable.range as ComplexTypeReference).referred.lookup(trace.type2Predicate),
				variable.lookup(variableMap))
			typedefs.add(eq)
		}
		if (isUniversal) {
			createVLSUniversalQuantifier => [
				it.variables += variableMap.values
				it.operand = createVLSImplies => [
					it.left = unfoldAnd(typedefs)
					it.right = mapper.transformTerm(expression.expression, trace, variables.withAddition(variableMap))
				]
			]
		} else {
			typedefs.add(mapper.transformTerm(expression.expression, trace, variables.withAddition(variableMap)))
			createVLSExistentialQuantifier => [
				it.variables += variableMap.values
				it.operand = unfoldAnd(typedefs)

			]
		}

	}

	def protected boolean dfsSupertypeCheck(Type type, Type type2) {
		// There is surely a better way to do this
		if (type.supertypes.isEmpty)
			return false
		else {
			if (type.supertypes.contains(type2))
				return true
			else {
				for (supertype : type.supertypes) {
					if(dfsSupertypeCheck(supertype, type2)) return true
				}
			}
		}
	}

	// TODO rewrite such that it uses "listSubTypes"
	def protected boolean dfsSubtypeCheck(Type type, Type type2) {
		// There is surely a better way to do this
		if (type.subtypes.isEmpty)
			return false
		else {
			if (type.subtypes.contains(type2))
				return true
			else {
				for (subtype : type.subtypes) {
					if(dfsSubtypeCheck(subtype, type2)) return true
				}
			}
		}
	}

	def protected void listSubtypes(Type t, List<Type> allSubtypes) {
		for (subt : t.subtypes) {
			allSubtypes.add(subt)
			listSubtypes(subt, allSubtypes)
		}
	}

	def protected withAddition(Map<Variable, VLSVariable> map1, Map<Variable, VLSVariable> map2) {
		new HashMap(map1) => [putAll(map2)]
	}

	// SERVERS
	def makeForm(String formula, BackendSolver solver, int time) {
		return header + formula + addOptions + addSolver(solver, time) + addEnd
	}

	def getSolverSpecs(BackendSolver solver) {
		switch (solver) {
			case BackendSolver::CVC4:
				return newArrayList("CVC4---SAT-1.7", "do_CVC4 %s %d SAT")
			case BackendSolver::DARWINFM:
				return newArrayList("DarwinFM---1.4.5", "darwin -fd true -ppp true -pl 0 -to %d -pmtptp true %s")
			case BackendSolver::EDARWIN:
				return newArrayList("E-Darwin---1.5",
					"e-darwin -pev \"TPTP\" -pmd true -if tptp -pl 2 -pc false -ps false %s")
			case BackendSolver::GEOIII:
				return newArrayList("Geo-III---2018C",
					"geo -tptp_input -nonempty -include /home/tptp/TPTP -inputfile %s")
			case BackendSolver::IPROVER:
				return newArrayList("iProver---SAT-3.0", "iproveropt_run_sat.sh %d %s")
			case BackendSolver::PARADOX:
				return newArrayList("Paradox---4.0", "paradox --no-progress --time %d --tstp --model %s")
			case BackendSolver::VAMPIRE:
				return newArrayList("Vampire---SAT-4.4", "vampire --mode casc_sat -t %d %s")
			case BackendSolver::Z3:
				return newArrayList("Z3---4.4.1", "run_z3_tptp -proof -model -t:%d -file:%s")
		}
	}

	def getHeader() {
		return "------WebKitFormBoundaryBdFiQ5zEvTbBl4DA\r\nContent-Disposition: form-data; name=\"ProblemSource\"\r\n\r\nFORMULAE\r\n------WebKitFormBoundaryBdFiQ5zEvTbBl4DA\r\nContent-Disposition: form-data; name=\"FORMULAEProblem\"\r\n\r\n\r\n"
	}

	def addSpec(String spec) {
		return spec.replace("\n", "\\r\\n")
	}

	def addOptions() {
		return "\r\n------WebKitFormBoundaryBdFiQ5zEvTbBl4DA\r\nContent-Disposition: form-data; name=\"QuietFlag\"\r\n\r\n-q3\r\n------WebKitFormBoundaryBdFiQ5zEvTbBl4DA\r\nContent-Disposition: form-data; name=\"SubmitButton\"\r\n\r\nRunSelectedSystems\r\n"
	}

	def addSolver(BackendSolver solver, int time) {
		val solverSpecs = getSolverSpecs(solver)
		val ID = solverSpecs.get(0)
		val cmd = solverSpecs.get(1)

		return "------WebKitFormBoundaryBdFiQ5zEvTbBl4DA\r\nContent-Disposition: form-data; name=\"TimeLimit___" + ID +
			"\"\r\n\r\n" + time +
			"\r\n------WebKitFormBoundaryBdFiQ5zEvTbBl4DA\r\nContent-Disposition: form-data; name=\"System___" + ID +
			"\"\r\n\r\n" + ID +
			"\r\n------WebKitFormBoundaryBdFiQ5zEvTbBl4DA\r\nContent-Disposition: form-data; name=\"Command___" + ID +
			"\"\r\n\r\n" + cmd + "\r\n"
	}

	def addEnd() {
		return "------WebKitFormBoundaryBdFiQ5zEvTbBl4DA--"
	}

	def sendPost(String formData) throws Exception {

		val OkHttpClient client = new OkHttpClient.Builder().connectTimeout(600, TimeUnit.SECONDS).readTimeout(350,
			TimeUnit.SECONDS).build()

		val MediaType mediaType = MediaType.parse("multipart/form-data boundary=----WebKitFormBoundaryBdFiQ5zEvTbBl4DA")
		val RequestBody body = RequestBody.create(mediaType, formData)
		val Request request = new Request.Builder().url("http://www.tptp.org/cgi-bin/SystemOnTPTPFormReply").post(body).
			addHeader("Connection", "keep-alive").addHeader("Cache-Control", "max-age=0").addHeader("Origin",
				"http://tptp.cs.miami.edu").addHeader("Upgrade-Insecure-Requests", "1").addHeader("Content-Type",
				"multipart/form-data boundary=----WebKitFormBoundaryBdFiQ5zEvTbBl4DA").addHeader("User-Agent",
				"Mozilla/5.0 (Windows NT 10.0 Win64 x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36").
			addHeader("Accept",
				"text/html,application/xhtml+xml,application/xmlq=0.9,image/webp,image/apng,*/*q=0.8,application/signed-exchangev=b3").
			addHeader("Referer", "http://tptp.cs.miami.edu/cgi-bin/SystemOnTPTP").addHeader("Accept-Encoding",
				"gzip, deflate").addHeader("Accept-Language", "en-US,enq=0.9").addHeader("Postman-Token",
				"639ff59f-ab5c-4d9f-9da5-ac8bb64be466,ecb71882-f4d8-4126-8a97-4edb07d4055c").addHeader("Host",
				"www.tptp.org").addHeader("Content-Length", "44667").addHeader("cache-control", "no-cache").build()

		val Response response = client.newCall(request).execute()
//		TimeUnit.SECONDS.sleep(5)
		return newArrayList(response.body.string.split("\n"))
//		return response.body.string
	// case 1: 
	}

}