aboutsummaryrefslogtreecommitdiffstats
path: root/Solvers/Vampire-Solver/ca.mcgill.ecse.dslreasoner.vampire.reasoner/src/ca/mcgill/ecse/dslreasoner/vampire/reasoner/builder/Logic2VampireLanguageMapper.xtend
blob: b617912d41b8771b152ec58a3e7865b53606a025 (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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
package ca.mcgill.ecse.dslreasoner.vampire.reasoner.builder

import ca.mcgill.ecse.dslreasoner.vampire.reasoner.VampireSolverConfiguration
import ca.mcgill.ecse.dslreasoner.vampireLanguage.VLSTerm
import ca.mcgill.ecse.dslreasoner.vampireLanguage.VLSVariable
import ca.mcgill.ecse.dslreasoner.vampireLanguage.VampireLanguageFactory
import ca.mcgill.ecse.dslreasoner.vampireLanguage.VampireModel
import hu.bme.mit.inf.dslreasoner.logic.model.builder.TracedOutput
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.And
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Assertion
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.BoolLiteral
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.BoolTypeReference
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.ComplexTypeReference
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.ConstantDeclaration
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.ConstantDefinition
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.DefinedElement
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Distinct
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Equals
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Exists
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Forall
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.FunctionDeclaration
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.FunctionDefinition
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Iff
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Impl
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.InstanceOf
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.IntLiteral
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Not
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Or
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Relation
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.RelationDeclaration
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.RelationDefinition
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.SymbolicValue
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Term
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Variable
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.impl.RelationDefinitionImpl
import hu.bme.mit.inf.dslreasoner.logic.model.logicproblem.LogicProblem
import java.util.Collections
import java.util.HashMap
import java.util.List
import java.util.Map
import org.eclipse.xtend.lib.annotations.Accessors

import static extension hu.bme.mit.inf.dslreasoner.util.CollectionsUtil.*
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.impl.RelationDeclarationImpl

class Logic2VampireLanguageMapper {
	private val extension VampireLanguageFactory factory = VampireLanguageFactory.eINSTANCE
	private val Logic2VampireLanguageMapper_Support support = new Logic2VampireLanguageMapper_Support;
	@Accessors(PUBLIC_GETTER) private val Logic2VampireLanguageMapper_ConstantMapper constantMapper = new Logic2VampireLanguageMapper_ConstantMapper(
		this)
	@Accessors(PUBLIC_GETTER) private val Logic2VampireLanguageMapper_ContainmentMapper containmentMapper = new Logic2VampireLanguageMapper_ContainmentMapper(
		this)
	@Accessors(PUBLIC_GETTER) private val Logic2VampireLanguageMapper_RelationMapper relationMapper = new Logic2VampireLanguageMapper_RelationMapper(
		this)
	@Accessors(PUBLIC_GETTER) private val Logic2VampireLanguageMapper_ScopeMapper scopeMapper = new Logic2VampireLanguageMapper_ScopeMapper(
		this)
	@Accessors(PUBLIC_GETTER) private val Logic2VampireLanguageMapper_TypeMapper typeMapper = new Logic2VampireLanguageMapper_TypeMapper(
		this)

	public def TracedOutput<VampireModel, Logic2VampireLanguageMapperTrace> transformProblem(LogicProblem problem,
		VampireSolverConfiguration config) {
		// create model bases
		// TODO
		val initialComment = createVLSComment => [
			it.comment = "%This is an initial Test Comment"
		]

		val specification = createVampireModel => [
			it.comments += initialComment
		]

		val trace = new Logic2VampireLanguageMapperTrace => [
			it.specification = specification

//			it.incQueryEngine = viatraQueryEngine.on(new EMFScope(problem))
		]

		// TYPE MAPPER
		if (!problem.types.isEmpty) {
			typeMapper.transformTypes(problem.types, problem.elements, this, trace)
		}
		
		// RELATION MAPPER
		trace.relationDefinitions = problem.collectRelationDefinitions
//		println(problem.relations.filter[class == RelationDefinitionImpl])
		problem.relations.forEach[this.relationMapper.transformRelation(it, trace, new Logic2VampireLanguageMapper)]
		
		// CONTAINMENT MAPPER
		containmentMapper.transformContainment(config,problem.containmentHierarchies, trace)

		// SCOPE MAPPER
		scopeMapper.transformScope(problem.types, config, trace)

		// CONSTANT MAPPER
		// only transforms definitions
		trace.constantDefinitions = problem.collectConstantDefinitions
		// problem.constants.filter(ConstantDefinition).forEach[this.constantMapper.transformConstant(it, trace)]
		problem.constants.filter(ConstantDefinition).forEach [
			this.constantMapper.transformConstantDefinitionSpecification(it, trace)
		]

		// ASSERTION MAPPER
		for (assertion : problem.assertions) {
			transformAssertion(assertion, trace)
		}
		// OUTPUT
		return new TracedOutput(specification, trace)
	}

	// End of transformProblem
	// ////////////
	// Type References
	// ////////////
	def dispatch protected VLSTerm transformTypeReference(BoolTypeReference boolTypeReference,
		Logic2VampireLanguageMapperTrace trace) {
		// TODO, Not Now
		// return createALSReference => [ it.referred = support.getBooleanType(trace) ]
	}

	// ////////////
	// Collectors
	// ////////////
	// exact Same as for Alloy
	private def collectConstantDefinitions(LogicProblem problem) {
		val res = new HashMap
		problem.constants.filter(ConstantDefinition).filter[it.defines !== null].forEach [
			res.put(it.defines, it)
		]
		return res
	}

	private def collectRelationDefinitions(LogicProblem problem) {
		val res = new HashMap
		problem.relations.filter(RelationDefinition).filter[it.defines !== null].forEach [
			res.put(it.defines, it)
		]
		return res
	}

	// ////////////
	// Assertions + Terms
	// ////////////
	def protected transformAssertion(Assertion assertion, Logic2VampireLanguageMapperTrace trace) {
		val res = createVLSFofFormula => [
			it.name = support.toID("assertion_" + assertion.name)
			// below is temporary solution
			it.fofRole = "axiom"
			it.fofFormula = assertion.value.transformTerm(trace, Collections.EMPTY_MAP)
		// it.annotation = nothing			
		]
		trace.specification.formulas += res
	}

	def dispatch protected VLSTerm transformTerm(BoolLiteral literal, Logic2VampireLanguageMapperTrace trace,
		Map<Variable, VLSVariable> variables) {
		if (literal.value == true) {
			createVLSTrue
		} else {
			createVLSFalse
		}
	}

	def dispatch protected VLSTerm transformTerm(IntLiteral literal, Logic2VampireLanguageMapperTrace trace,
		Map<Variable, VLSVariable> variables) {
		createVLSInt => [it.value = literal.value.toString()]
	}

//	def dispatch protected VLSTerm transformTerm(RealLiteral literal, Logic2VampireLanguageMapperTrace trace,
//		Map<Variable, VLSVariable> variables) {
//		createVLSReal => [it.value = literal.value.toString()]
//	}

	def dispatch protected VLSTerm transformTerm(Not not, Logic2VampireLanguageMapperTrace trace,
		Map<Variable, VLSVariable> variables) {
		createVLSUnaryNegation => [operand = not.operand.transformTerm(trace, variables)]
	}

	def dispatch protected VLSTerm transformTerm(And and, Logic2VampireLanguageMapperTrace trace,
		Map<Variable, VLSVariable> variables) { support.unfoldAnd(and.operands.map[transformTerm(trace, variables)]) }

	def dispatch protected VLSTerm transformTerm(Or or, Logic2VampireLanguageMapperTrace trace,
		Map<Variable, VLSVariable> variables) { support.unfoldOr(or.operands.map[transformTerm(trace, variables)]) }

	def dispatch protected VLSTerm transformTerm(Impl impl, Logic2VampireLanguageMapperTrace trace,
		Map<Variable, VLSVariable> variables) {
		createVLSImplies => [
			left = impl.leftOperand.transformTerm(trace, variables)
			right = impl.rightOperand.transformTerm(trace, variables)
		]
	}

	def dispatch protected VLSTerm transformTerm(Iff iff, Logic2VampireLanguageMapperTrace trace,
		Map<Variable, VLSVariable> variables) {
		createVLSEquivalent => [
			left = iff.leftOperand.transformTerm(trace, variables)
			right = iff.rightOperand.transformTerm(trace, variables)
		]
	}

//	def dispatch protected VLSTerm transformTerm(MoreThan moreThan, Logic2VampireLanguageMapperTrace trace, Map<Variable, VLSVariable> variables) {
//		createALSMore => [leftOperand = moreThan.leftOperand.transformTerm(trace,variables) rightOperand = moreThan.rightOperand.transformTerm(trace,variables)] }
//	def dispatch protected VLSTerm transformTerm(LessThan lessThan, Logic2VampireLanguageMapperTrace trace, Map<Variable, VLSVariable> variables) {
//		createALSLess => [leftOperand = lessThan.leftOperand.transformTerm(trace,variables) rightOperand = lessThan.rightOperand.transformTerm(trace,variables)] }
//	def dispatch protected VLSTerm transformTerm(MoreOrEqualThan moreThan, Logic2VampireLanguageMapperTrace trace, Map<Variable, VLSVariable> variables) {
//		createALSMeq => [leftOperand = moreThan.leftOperand.transformTerm(trace,variables) rightOperand = moreThan.rightOperand.transformTerm(trace,variables)] }
//	def dispatch protected VLSTerm transformTerm(LessOrEqualThan lessThan, Logic2VampireLanguageMapperTrace trace, Map<Variable, VLSVariable> variables) {
//		createALSLeq => [leftOperand = lessThan.leftOperand.transformTerm(trace,variables) rightOperand = lessThan.rightOperand.transformTerm(trace,variables)] }
	def dispatch protected VLSTerm transformTerm(Equals equals, Logic2VampireLanguageMapperTrace trace,
		Map<Variable, VLSVariable> variables) {
		createVLSEquality => [
			left = equals.leftOperand.transformTerm(trace, variables)
			right = equals.rightOperand.transformTerm(trace, variables)
		]
	}

	def dispatch protected VLSTerm transformTerm(Distinct distinct, Logic2VampireLanguageMapperTrace trace,
		Map<Variable, VLSVariable> variables) { support.unfoldDistinctTerms(this, distinct.operands, trace, variables) }

//	
//		def dispatch protected ALSTerm transformTerm(Plus plus, Logic2AlloyLanguageMapperTrace trace, Map<Variable, ALSVariableDeclaration> variables) {
//		createALSFunctionCall => [it.params += plus.leftOperand.transformTerm(trace,variables) it.params += plus.rightOperand.transformTerm(trace,variables) it.referredNumericOperator = ALSNumericOperator.PLUS] }
//	def dispatch protected ALSTerm transformTerm(Minus minus, Logic2AlloyLanguageMapperTrace trace, Map<Variable, ALSVariableDeclaration> variables) {
//		createALSFunctionCall => [it.params += minus.leftOperand.transformTerm(trace,variables) it.params += minus.rightOperand.transformTerm(trace,variables) it.referredNumericOperator = ALSNumericOperator.SUB] }
//	def dispatch protected ALSTerm transformTerm(Multiply multiply, Logic2AlloyLanguageMapperTrace trace, Map<Variable, ALSVariableDeclaration> variables) {
//		createALSFunctionCall => [it.params += multiply.leftOperand.transformTerm(trace,variables) it.params += multiply.rightOperand.transformTerm(trace,variables) it.referredNumericOperator = ALSNumericOperator.MUL] }
//	def dispatch protected ALSTerm transformTerm(Divison div, Logic2AlloyLanguageMapperTrace trace, Map<Variable, ALSVariableDeclaration> variables) {
//		createALSFunctionCall => [it.params += div.leftOperand.transformTerm(trace,variables) it.params += div.rightOperand.transformTerm(trace,variables) it.referredNumericOperator = ALSNumericOperator.DIV] }
//	def dispatch protected ALSTerm transformTerm(Mod mod, Logic2AlloyLanguageMapperTrace trace, Map<Variable, ALSVariableDeclaration> variables) {
//		createALSFunctionCall => [it.params += mod.leftOperand.transformTerm(trace,variables) it.params += mod.rightOperand.transformTerm(trace,variables) it.referredNumericOperator = ALSNumericOperator.REM] }
//	
	def dispatch protected VLSTerm transformTerm(Forall forall, Logic2VampireLanguageMapperTrace trace,
		Map<Variable, VLSVariable> variables) {
		support.createQuantifiedExpression(this, forall, trace, variables, true)
	}

	def dispatch protected VLSTerm transformTerm(Exists exists, Logic2VampireLanguageMapperTrace trace,
		Map<Variable, VLSVariable> variables) {
		support.createQuantifiedExpression(this, exists, trace, variables, false)
	}

	def dispatch protected VLSTerm transformTerm(InstanceOf instanceOf, Logic2VampireLanguageMapperTrace trace,
		Map<Variable, VLSVariable> variables) {
		return createVLSFunction => [
			it.constant = (instanceOf.range as ComplexTypeReference).referred.lookup(trace.type2Predicate).constant
			it.terms += instanceOf.value.transformTerm(trace, variables)
		]
	}

//	
//	def dispatch protected ALSTerm transformTerm(TransitiveClosure tc, Logic2AlloyLanguageMapperTrace trace, Map<Variable, ALSVariableDeclaration> variables) {
//		return this.relationMapper.transformTransitiveRelationReference(
//			tc.relation,
//			tc.leftOperand.transformTerm(trace,variables),
//			tc.rightOperand.transformTerm(trace,variables),
//			trace
//		)
//	}
//	
	def dispatch protected VLSTerm transformTerm(SymbolicValue symbolicValue, Logic2VampireLanguageMapperTrace trace,
		Map<Variable, VLSVariable> variables) {
		symbolicValue.symbolicReference.transformSymbolicReference(symbolicValue.parameterSubstitutions, trace,
			variables)
	}

	def dispatch protected VLSTerm transformSymbolicReference(DefinedElement referred,
		List<Term> parameterSubstitutions, Logic2VampireLanguageMapperTrace trace,
		Map<Variable, VLSVariable> variables) {
			val name = referred.lookup(trace.definedElement2String)
			return createVLSConstant => [
				it.name = name
			]
//		typeMapper.transformReference(referred, trace)
	}

	def dispatch protected VLSTerm transformSymbolicReference(ConstantDeclaration constant,
		List<Term> parameterSubstitutions, Logic2VampireLanguageMapperTrace trace,
		Map<Variable, VLSVariable> variables) {
		// might need to make sure that only declared csts get transformed. see for Alloy
		val res = createVLSConstant => [
			// ask if necessary VLSConstantDeclaration and not just directly strng
			it.name = support.toID(constant.name)
		]
		// no postprocessing cuz booleans are accepted
		return res
	}

	// NOT NEEDED FOR NOW
	// TODO
	def dispatch protected VLSTerm transformSymbolicReference(ConstantDefinition constant,
		List<Term> parameterSubstitutions, Logic2VampireLanguageMapperTrace trace,
		Map<Variable, VLSVariable> variables) {
//		val res = createVLSFunctionCall => [
//			it.referredDefinition = constant.lookup(trace.constantDefinition2Function)
//		]
//		return support.postprocessResultOfSymbolicReference(constant.type,res,trace)
	}

	def dispatch protected VLSTerm transformSymbolicReference(Variable variable, List<Term> parameterSubstitutions,
		Logic2VampireLanguageMapperTrace trace, Map<Variable, VLSVariable> variables) {

		// cannot treat variable as function (constant) because of name ID not being the same
		// no need for potprocessing cuz booleans are supported
		return support.duplicate(variable.lookup(variables))
	}

	// TODO
	def dispatch protected VLSTerm transformSymbolicReference(FunctionDeclaration function,
		List<Term> parameterSubstitutions, Logic2VampireLanguageMapperTrace trace,
		Map<Variable, VLSVariable> variables) {
//		if(trace.functionDefinitions.containsKey(function)) {
//			return this.transformSymbolicReference(function.lookup(trace.functionDefinitions),parameterSubstitutions,trace,variables)
//		} else {
//			if(functionMapper.transformedToHostedField(function,trace)) {
//				val param = parameterSubstitutions.get(0).transformTerm(trace,variables)
//				val res = createVLSJoin => [
//					leftOperand = support.prepareParameterOfSymbolicReference(function.parameters.get(0),param,trace)
//					rightOperand = createVLSReference => [referred = function.lookup(trace.functionDeclaration2HostedField)]
//				]
//				return support.postprocessResultOfSymbolicReference(function.range,res,trace)
//			} else {
//				val functionExpression = createVLSJoin=>[
//						leftOperand = createVLSReference => [referred = trace.logicLanguage]
//						rightOperand = createVLSReference => [referred = function.lookup(trace.functionDeclaration2LanguageField)]
//					]
//				val res =  support.unfoldDotJoin(this,parameterSubstitutions,functionExpression,trace,variables)
//				return support.postprocessResultOfSymbolicReference(function.range,res,trace)
//			}
//		}
	}

	// TODO
	def dispatch protected VLSTerm transformSymbolicReference(FunctionDefinition function,
		List<Term> parameterSubstitutions, Logic2VampireLanguageMapperTrace trace,
		Map<Variable, VLSVariable> variables) {
//		val result = createVLSFunctionCall => [
//			it.referredDefinition = function.lookup(trace.functionDefinition2Function)
//			it.params += parameterSubstitutions.map[it.transformTerm(trace,variables)]
//		]
//		return support.postprocessResultOfSymbolicReference(function.range,result,trace)
	}

	// TODO
	/*
	 * def dispatch protected VLSTerm transformSymbolicReference(Relation relation,
	 * 	List<Term> parameterSubstitutions, Logic2VampireLanguageMapperTrace trace,
	 * 	Map<Variable, VLSVariable> variables) {
	 * 	if (trace.relationDefinitions.containsKey(relation)) {
	 * 		this.transformSymbolicReference(relation.lookup(trace.relationDefinitions),
	 * 			parameterSubstitutions, trace, variables)
	 * 	}
	 * 	else {
	 * //						if (relationMapper.transformToHostedField(relation, trace)) {
	 * //							val VLSRelation = relation.lookup(trace.relationDeclaration2Field)
	 * //							// R(a,b) =>
	 * //							// b in a.R
	 * //							return createVLSSubset => [
	 * //								it.leftOperand = parameterSubstitutions.get(1).transformTerm(trace, variables)
	 * //								it.rightOperand = createVLSJoin => [
	 * //									it.leftOperand = parameterSubstitutions.get(0).transformTerm(trace, variables)
	 * //									it.rightOperand = createVLSReference => [it.referred = VLSRelation]
	 * //								]
	 * //							]
	 * //						} else {
	 * //							val target = createVLSJoin => [
	 * //								leftOperand = createVLSReference => [referred = trace.logicLanguage]
	 * //								rightOperand = createVLSReference => [
	 * //									referred = relation.lookup(trace.relationDeclaration2Global)
	 * //								]
	 * //							]
	 * //							val source = support.unfoldTermDirectProduct(this, parameterSubstitutions, trace, variables)
	 * //
	 * //							return createVLSSubset => [
	 * //								leftOperand = source
	 * //								rightOperand = target
	 * //							]
	 * //						}
	 * 	}
	 * }
	 */
	// TODO
	def dispatch protected VLSTerm transformSymbolicReference(Relation relation, List<Term> parameterSubstitutions,
		Logic2VampireLanguageMapperTrace trace, Map<Variable, VLSVariable> variables) {
//					 createVLSFunction => [
//						it.referredDefinition = relation.lookup(trace.relationDefinition2Predicate)
//						it.params += parameterSubstitutions.map[p|p.transformTerm(trace, variables)]
//					]
//		println(relation.name)
//		if(relation.class == RelationDefinitionImpl) {
//			println("(" + (relation as RelationDefinition).getDefines + ")")
//		}
		return createVLSFunction => [
			if (relation.class == RelationDeclarationImpl) {
							it.constant = (relation as RelationDeclaration).lookup(trace.rel2Predicate).constant
			}
			else {
				it.constant = (relation as RelationDefinition).lookup(trace.relDef2Predicate).constant
			}
			
			it.terms += parameterSubstitutions.map[p|p.transformTerm(trace, variables)]
		]
	}

}