Skip to content
Snippets Groups Projects
Commit 4639239d authored by Ali Sinan Köksal's avatar Ali Sinan Köksal
Browse files

constraints have access to the L variables occurring in them

parent 2191f9e3
Branches
Tags
No related merge requests found
...@@ -70,7 +70,7 @@ trait CallTransformation ...@@ -70,7 +70,7 @@ trait CallTransformation
val extractedFunDefs : Map[Position,FunDef] = funDefMap(unit) val extractedFunDefs : Map[Position,FunDef] = funDefMap(unit)
private def transformHelper(tree : Tree, function : Function, codeGen : CodeGenerator) : Option[(Serialized, Serialized, Serialized, Tree, Int)] = { private def transformHelper(tree : Tree, function : Function, codeGen : CodeGenerator) : Option[(Serialized, Serialized, Serialized, Serialized, Tree, Tree, Tree, Int)] = {
val Function(funValDefs, funBody) = function val Function(funValDefs, funBody) = function
val fd = extractedFunDefs(function.pos) val fd = extractedFunDefs(function.pos)
...@@ -93,20 +93,24 @@ trait CallTransformation ...@@ -93,20 +93,24 @@ trait CallTransformation
purescalaReporter.info("Input variables : " + inputIdentifiers.mkString(", ")) purescalaReporter.info("Input variables : " + inputIdentifiers.mkString(", "))
purescalaReporter.info("Output variables : " + outputVars.mkString(", ")) purescalaReporter.info("Output variables : " + outputVars.mkString(", "))
// list of input "Variables" to concatenate with list of L variables // list of input "Variables" and list of L "Variables"
val inputVars = inputIdentifiers map (iv => Variable(iv)) val inputVars = inputIdentifiers map (iv => Variable(iv))
val lVars = lvarIdentifiers map (lv => Variable(lv)) val lVars = lvarIdentifiers map (lv => Variable(lv))
// serialize list of all non-output "Variable"s // serialize the above
val serializedInputVarList = serialize(inputVars ++ lVars) val serializedInputVarList = serialize(inputVars)
val serializedLVarList = serialize(lVars)
// serialize outputVars sequence // serialize outputVars sequence
val serializedOutputVars = serialize(outputVars) val serializedOutputVars = serialize(outputVars)
// sequence of input values // sequence of input values
val inputVarValues : Tree = codeGen.inputVarValues(serializedInputVarList, inputIdentifiers, lvarIdentifiers, scalaToExprSym) val inputVarValues : Tree = codeGen.inputVarValues(serializedInputVarList, inputIdentifiers, scalaToExprSym)
val lVarValues : Tree = codeGen.lVarValues(serializedLVarList, lvarIdentifiers, scalaToExprSym)
Some((serializedInputVarList, serializedOutputVars, serializedExpr, inputVarValues, outputVars.size)) val actualLVars : Tree = codeGen.lVars(lvarIdentifiers)
Some((serializedInputVarList, serializedLVarList, serializedOutputVars, serializedExpr, inputVarValues, lVarValues, actualLVars, outputVars.size))
} }
} }
...@@ -118,9 +122,9 @@ trait CallTransformation ...@@ -118,9 +122,9 @@ trait CallTransformation
val codeGen = new CodeGenerator(unit, currentOwner, tree.pos) val codeGen = new CodeGenerator(unit, currentOwner, tree.pos)
transformHelper(tree, function, codeGen) match { transformHelper(tree, function, codeGen) match {
case Some((serializedInputVarList, serializedOutputVars, serializedExpr, inputVarValues, arity)) => { case Some((serializedInputVarList, serializedLVarList, serializedOutputVars, serializedExpr, inputVarValues, lVarValues, actualLVars, arity)) => {
// create constraint instance // create constraint instance
val code = codeGen.newBaseTerm(exprToScalaSym, serializedProg, serializedInputVarList, serializedOutputVars, serializedExpr, inputVarValues, function, typeTreeList, arity) val code = codeGen.newBaseTerm(exprToScalaSym, serializedProg, serializedInputVarList, serializedLVarList, serializedOutputVars, serializedExpr, inputVarValues, lVarValues, actualLVars, function, typeTreeList, arity)
typer.typed(atOwner(currentOwner) { typer.typed(atOwner(currentOwner) {
code code
...@@ -141,9 +145,9 @@ trait CallTransformation ...@@ -141,9 +145,9 @@ trait CallTransformation
val typeTreeList = List(constraintParamType, TypeTree(definitions.BooleanClass.tpe)) val typeTreeList = List(constraintParamType, TypeTree(definitions.BooleanClass.tpe))
transformHelper(tree, predicate, codeGen) match { transformHelper(tree, predicate, codeGen) match {
case Some((serializedInputVarList, serializedOutputVars, serializedExpr, inputVarValues, arity)) => { case Some((serializedInputVarList, serializedLVarList, serializedOutputVars, serializedExpr, inputVarValues, lVarValues, actualLVars, arity)) => {
// create constraint instance // create constraint instance
val termCode = codeGen.newBaseTerm(exprToScalaSym, serializedProg, serializedInputVarList, serializedOutputVars, serializedExpr, inputVarValues, NULL, typeTreeList, arity) val termCode = codeGen.newBaseTerm(exprToScalaSym, serializedProg, serializedInputVarList, serializedLVarList, serializedOutputVars, serializedExpr, inputVarValues, lVarValues, actualLVars, NULL, typeTreeList, arity)
val code = (lhs DOT withFilter2Function) APPLY (Function(funValDefs,termCode) setSymbol predicate.symbol) val code = (lhs DOT withFilter2Function) APPLY (Function(funValDefs,termCode) setSymbol predicate.symbol)
typer.typed(atOwner(currentOwner) { typer.typed(atOwner(currentOwner) {
......
...@@ -267,21 +267,38 @@ trait CodeGeneration { ...@@ -267,21 +267,38 @@ trait CodeGeneration {
} }
/* Generates list of values that will replace the specified serialized input variables in a constraint */ /* Generates list of values that will replace the specified serialized input variables in a constraint */
def inputVarValues(serializedInputVarList : Serialized, inputVars : Seq[Identifier], lVars : Seq[Identifier], scalaToExprSym : Symbol) : Tree = { def inputVarValues(serializedInputVarList : Serialized, inputVars : Seq[Identifier], scalaToExprSym : Symbol) : Tree = {
val inputVarTrees = inputVars.map((iv: Identifier) => scalaToExprSym APPLY variablesToTrees(Variable(iv))).toList val inputVarTrees = inputVars.map((iv: Identifier) => scalaToExprSym APPLY variablesToTrees(Variable(iv)))
val lvarConversionTrees = lVars.map((lid: Identifier) => variableFromLFunction APPLY reverseLvarSubsts(Variable(lid))).toList listFromElements(inputVarTrees)
(scalaPackage DOT collectionModule DOT immutableModule DOT definitions.ListModule DOT listModuleApplyFunction) APPLY (inputVarTrees ::: lvarConversionTrees)
} }
def newBaseTerm(exprToScalaSym : Symbol, serializedProg : Serialized, serializedInputVarList : Serialized, serializedOutputVars : Serialized, serializedExpr : Serialized, inputVarValues : Tree, function : Tree, typeTreeList : List[Tree], arity : Int) : Tree = { /* Generates list of values that will replace the specified serialized L variables in a constraint */
def lVarValues(serializedLVarList : Serialized, lVars : Seq[Identifier], scalaToExprSym : Symbol) : Tree = {
val lvarConversionTrees = lVars.map((lid: Identifier) => variableFromLFunction APPLY reverseLvarSubsts(Variable(lid)))
listFromElements(lvarConversionTrees)
}
def lVars(ids: Seq[Identifier]) : Tree = {
val lvarTrees = ids.map(id => reverseLvarSubsts(Variable(id)))
listFromElements(lvarTrees)
}
def listFromElements(elems: Seq[Tree]): Tree = {
(scalaPackage DOT collectionModule DOT immutableModule DOT definitions.ListModule DOT listModuleApplyFunction) APPLY (elems.toList)
}
def newBaseTerm(exprToScalaSym : Symbol, serializedProg : Serialized, serializedInputVarList : Serialized, serializedLVarList : Serialized, serializedOutputVars : Serialized, serializedExpr : Serialized, inputVarValues : Tree, lVarValues : Tree, lVars : Tree, function : Tree, typeTreeList : List[Tree], arity : Int) : Tree = {
TypeApply( TypeApply(
Ident(termModules(arity)), typeTreeList) APPLY( Ident(termModules(arity)), typeTreeList) APPLY(
newConverter(exprToScalaSym), newConverter(exprToScalaSym),
newSerialized(serializedProg), newSerialized(serializedProg),
newSerialized(serializedInputVarList), newSerialized(serializedInputVarList),
newSerialized(serializedLVarList),
newSerialized(serializedOutputVars), newSerialized(serializedOutputVars),
newSerialized(serializedExpr), newSerialized(serializedExpr),
inputVarValues, inputVarValues,
lVarValues,
lVars,
function function
) )
} }
......
...@@ -21,7 +21,7 @@ object ConstraintSolving { ...@@ -21,7 +21,7 @@ object ConstraintSolving {
s s
} }
private val DEBUG = true private val DEBUG = false
private def printDebug(msg: String): Unit = if (DEBUG) println(msg) private def printDebug(msg: String): Unit = if (DEBUG) println(msg)
object GlobalContext { object GlobalContext {
......
...@@ -209,6 +209,7 @@ object LTrees { ...@@ -209,6 +209,7 @@ object LTrees {
val placeHolders = Seq(FreshIdentifier("placeholder", true).setType(BottomType)) val placeHolders = Seq(FreshIdentifier("placeholder", true).setType(BottomType))
val candidateL = new L[T](handler(), placeHolders) val candidateL = new L[T](handler(), placeHolders)
val instantiatedCnstr = constraint(candidateL) val instantiatedCnstr = constraint(candidateL)
// println("l vars in constraint: " + instantiatedCnstr.lVars)
// now that we have a Constraint, we can perform some actions such as: // now that we have a Constraint, we can perform some actions such as:
GlobalContext.initializeIfNeeded(instantiatedCnstr.program) GlobalContext.initializeIfNeeded(instantiatedCnstr.program)
......
This diff is collapsed.
...@@ -21,11 +21,11 @@ object Utils { ...@@ -21,11 +21,11 @@ object Utils {
val anonFunArgs = (0 until arity).map(i => "x_%d" format (i)) val anonFunArgs = (0 until arity).map(i => "x_%d" format (i))
val anonFunArgsString = anonFunArgs.mkString(",") val anonFunArgsString = anonFunArgs.mkString(",")
val orMethod = """def ||(other : %s)%s : %s = val orMethod = """def ||(other : %s)%s : %s =
Term%d(this.program, Or(this.expr, other.expr), if (this.scalaFunction == null || other.scalaFunction == null) null else (%s) => this.scalaFunction(%s) || other.scalaFunction(%s), this.types, this.converter)""" format (booleanTraitName, curriedImplicit2Boolean, booleanTraitName, arity, anonFunParamString, anonFunArgsString, anonFunArgsString) Term%d(this.program, Or(this.expr, other.expr), if (this.scalaFunction == null || other.scalaFunction == null) null else (%s) => this.scalaFunction(%s) || other.scalaFunction(%s), this.types, this.converter, this.lVars ++ other.lVars)""" format (booleanTraitName, curriedImplicit2Boolean, booleanTraitName, arity, anonFunParamString, anonFunArgsString, anonFunArgsString)
val andMethod = """def &&(other : %s)%s : %s = val andMethod = """def &&(other : %s)%s : %s =
Term%d(this.program, And(this.expr, other.expr), if (this.scalaFunction == null || other.scalaFunction == null) null else (%s) => this.scalaFunction(%s) && other.scalaFunction(%s), this.types, this.converter)""" format (booleanTraitName, curriedImplicit2Boolean, booleanTraitName, arity, anonFunParamString, anonFunArgsString, anonFunArgsString) Term%d(this.program, And(this.expr, other.expr), if (this.scalaFunction == null || other.scalaFunction == null) null else (%s) => this.scalaFunction(%s) && other.scalaFunction(%s), this.types, this.converter, this.lVars ++ other.lVars)""" format (booleanTraitName, curriedImplicit2Boolean, booleanTraitName, arity, anonFunParamString, anonFunArgsString, anonFunArgsString)
val notMethod = """def unary_!%s : %s = val notMethod = """def unary_!%s : %s =
Term%d(this.program, Not(this.expr), if (this.scalaFunction == null) null else (%s) => ! this.scalaFunction(%s), this.types, this.converter)""" format (curriedImplicit2Boolean, booleanTraitName, arity, anonFunParamString, anonFunArgsString) Term%d(this.program, Not(this.expr), if (this.scalaFunction == null) null else (%s) => ! this.scalaFunction(%s), this.types, this.converter, this.lVars)""" format (curriedImplicit2Boolean, booleanTraitName, arity, anonFunParamString, anonFunArgsString)
val intTraitName = "Term%d%s" format (arity, (traitArgParams ++ Seq("Int")).mkString("[", ",", "]")) val intTraitName = "Term%d%s" format (arity, (traitArgParams ++ Seq("Int")).mkString("[", ",", "]"))
val minimizingMethod = val minimizingMethod =
...@@ -61,7 +61,7 @@ object Utils { ...@@ -61,7 +61,7 @@ object Utils {
val s2 = val s2 =
"""def compose%d[%s](other : Term%d[%s]) : Term%d[%s] = { """def compose%d[%s](other : Term%d[%s]) : Term%d[%s] = {
val (newExpr, newTypes) = Terms.compose(other, this, %d, %d, %d) val (newExpr, newTypes) = Terms.compose(other, this, %d, %d, %d)
Term%d(this.program, newExpr, if (this.scalaFunction == null || other.scalaFunction == null) null else (%s) => this.scalaFunction(%s), newTypes, this.converter) Term%d(this.program, newExpr, if (this.scalaFunction == null || other.scalaFunction == null) null else (%s) => this.scalaFunction(%s), newTypes, this.converter, this.lVars ++ other.lVars)
}""" format (index, fParams.mkString(", "), arityF, fTermParams.mkString(", "), resultTermArity, resultTermParams.mkString(", "), index, arityF, arity, resultTermArity, scalaFunctionParams.mkString(", "), thisFunctionParams.mkString(", ")) }""" format (index, fParams.mkString(", "), arityF, fTermParams.mkString(", "), resultTermArity, resultTermParams.mkString(", "), index, arityF, arity, resultTermArity, scalaFunctionParams.mkString(", "), thisFunctionParams.mkString(", "))
s2 s2
...@@ -119,15 +119,15 @@ object Utils { ...@@ -119,15 +119,15 @@ object Utils {
val booleanTermTraitName = "Term%d%s" format (arity, (argParams ++ Seq("Boolean")).mkString("[", ",", "]")) val booleanTermTraitName = "Term%d%s" format (arity, (argParams ++ Seq("Boolean")).mkString("[", ",", "]"))
val objectString = val objectString =
"""object Term%d { """object Term%d {
def apply%s(conv : Converter, serializedProg : Serialized, serializedInputVars: Serialized, serializedOutputVars : Serialized, serializedExpr : Serialized, inputVarValues : Seq[Expr], scalaExpr : %s => %s) = { def apply%s(conv : Converter, serializedProg : Serialized, serializedInputVars: Serialized, serializedLVars: Serialized, serializedOutputVars : Serialized, serializedExpr : Serialized, inputVarValues : Seq[Expr], lVarValues : Seq[Expr], lVars : Seq[L[_]], scalaExpr : %s => %s) = {
val (converter, program, expr, types) = Term.processArgs(conv, serializedProg, serializedInputVars, serializedOutputVars, serializedExpr, inputVarValues) val (converter, program, expr, types) = Term.processArgs(conv, serializedProg, serializedInputVars, serializedLVars, serializedOutputVars, serializedExpr, inputVarValues, lVarValues)
new %s(program, expr, types, converter) with %s { new %s(program, expr, types, converter, lVars.toSet) with %s {
val scalaFunction = scalaExpr val scalaFunction = scalaExpr
} }
} }
def apply%s(program : Program, expr : Expr, scalaExpr : %s => %s, types : Seq[TypeTree], converter : Converter) = def apply%s(program : Program, expr : Expr, scalaExpr : %s => %s, types : Seq[TypeTree], converter : Converter, lVars: Set[L[_]]) =
new %s(program, expr, types, converter) with %s { new %s(program, expr, types, converter, lVars) with %s {
val scalaFunction = scalaExpr val scalaFunction = scalaExpr
} }
}""" format (arity, applyParamString, argParamTuple, "R", termClassName, termTraitName, applyParamString, argParamTuple, "R", termClassName, termTraitName) }""" format (arity, applyParamString, argParamTuple, "R", termClassName, termTraitName, applyParamString, argParamTuple, "R", termClassName, termTraitName)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment