Skip to content
Snippets Groups Projects
Commit 9ea7cf9f authored by Mikaël Mayer's avatar Mikaël Mayer
Browse files

Corrected missing case

Prevented to loop via the top-level function.
Prevented the function escape to be considered as a standard string to string converter.
parent db92b555
No related branches found
No related tags found
No related merge requests found
...@@ -167,8 +167,10 @@ case object StringRender extends Rule("StringRender") { ...@@ -167,8 +167,10 @@ case object StringRender extends Rule("StringRender") {
rec(lhs, rhstail, accEqs, accLeft, accRight append s) rec(lhs, rhstail, accEqs, accLeft, accRight append s)
case (RegularStringFormToken(e)::lhstail, OtherStringChunk(s)::rhstail) => case (RegularStringFormToken(e)::lhstail, OtherStringChunk(s)::rhstail) =>
rec(lhstail, rhs, accEqs, accLeft += e, accRight) rec(lhstail, rhs, accEqs, accLeft += e, accRight)
case (RegularStringFormToken(e)::lhstail, RegularStringChunk(s)::rhstail) => case (RegularStringFormToken(e)::lhstail, rhs) =>
rec(lhstail, rhstail, accEqs, accLeft += e, accRight append s) rec(lhstail, rhs, accEqs, accLeft += e, accRight)
case (Nil, RegularStringChunk(s)::rhstail) =>
rec(Nil, rhstail, accEqs, accLeft, accRight append s)
} }
rec(lhs, rhs, ListBuffer[Equation](), ListBuffer[StringFormToken](), new StringBuffer) rec(lhs, rhs, ListBuffer[Equation](), ListBuffer[StringFormToken](), new StringBuffer)
} }
...@@ -327,10 +329,12 @@ case object StringRender extends Rule("StringRender") { ...@@ -327,10 +329,12 @@ case object StringRender extends Rule("StringRender") {
object StringSynthesisContext { object StringSynthesisContext {
def empty( def empty(
definedStringConverters: StringConverters, definedStringConverters: StringConverters,
abstractStringConverters: StringConverters)(implicit hctx: SearchContext) = abstractStringConverters: StringConverters,
originalInputs: Set[Identifier])(implicit hctx: SearchContext) =
new StringSynthesisContext(None, new StringSynthesisResult(Map(), Set()), new StringSynthesisContext(None, new StringSynthesisResult(Map(), Set()),
definedStringConverters, definedStringConverters,
abstractStringConverters) abstractStringConverters,
originalInputs)
} }
/** Context for the current synthesis process */ /** Context for the current synthesis process */
...@@ -338,17 +342,20 @@ case object StringRender extends Rule("StringRender") { ...@@ -338,17 +342,20 @@ case object StringRender extends Rule("StringRender") {
val currentCaseClassParent: Option[TypeTree], val currentCaseClassParent: Option[TypeTree],
val result: StringSynthesisResult, val result: StringSynthesisResult,
val definedStringConverters: StringConverters, val definedStringConverters: StringConverters,
val abstractStringConverters: StringConverters val abstractStringConverters: StringConverters,
val originalInputs: Set[Identifier]
)(implicit hctx: SearchContext) { )(implicit hctx: SearchContext) {
def add(d: DependentType, f: FunDef, s: Stream[WithIds[Expr]]): StringSynthesisContext = { def add(d: DependentType, f: FunDef, s: Stream[WithIds[Expr]]): StringSynthesisContext = {
new StringSynthesisContext(currentCaseClassParent, result.add(d, f, s), new StringSynthesisContext(currentCaseClassParent, result.add(d, f, s),
definedStringConverters, definedStringConverters,
abstractStringConverters) abstractStringConverters,
originalInputs)
} }
def copy(currentCaseClassParent: Option[TypeTree]=currentCaseClassParent, result: StringSynthesisResult = result): StringSynthesisContext = def copy(currentCaseClassParent: Option[TypeTree]=currentCaseClassParent, result: StringSynthesisResult = result): StringSynthesisContext =
new StringSynthesisContext(currentCaseClassParent, result, new StringSynthesisContext(currentCaseClassParent, result,
definedStringConverters, definedStringConverters,
abstractStringConverters) abstractStringConverters,
originalInputs)
def freshFunName(s: String) = result.freshFunName(s) def freshFunName(s: String) = result.freshFunName(s)
} }
...@@ -446,7 +453,11 @@ case object StringRender extends Rule("StringRender") { ...@@ -446,7 +453,11 @@ case object StringRender extends Rule("StringRender") {
case Some(fd) => case Some(fd) =>
gatherInputs(ctx, q, result += Stream((functionInvocation(fd._1, Seq(input)), Nil))) gatherInputs(ctx, q, result += Stream((functionInvocation(fd._1, Seq(input)), Nil)))
case None => // No function can render the current type. case None => // No function can render the current type.
val exprs1 = ctx.definedStringConverters.getOrElse(input.getType, Nil).map(f => (f(input), Nil)) val exprs1 = ctx.definedStringConverters.getOrElse(input.getType, Nil).flatMap(f =>
f(input) match {
case FunctionInvocation(fd, Seq(Variable(id))) if ctx.originalInputs(id) => None
case e => Some((e, Nil))
})
val exprs2 = ctx.abstractStringConverters.getOrElse(input.getType, Nil).map(f => (f(input), Nil)) val exprs2 = ctx.abstractStringConverters.getOrElse(input.getType, Nil).map(f => (f(input), Nil))
val converters = (exprs1 ++ exprs2).toStream val converters = (exprs1 ++ exprs2).toStream
def mergeResults(defaultconverters: =>Stream[WithIds[Expr]]): Stream[WithIds[Expr]] = { def mergeResults(defaultconverters: =>Stream[WithIds[Expr]]): Stream[WithIds[Expr]] = {
...@@ -556,11 +567,14 @@ case object StringRender extends Rule("StringRender") { ...@@ -556,11 +567,14 @@ case object StringRender extends Rule("StringRender") {
val examplesFinder = new ExamplesFinder(hctx.context, hctx.program) val examplesFinder = new ExamplesFinder(hctx.context, hctx.program)
val examples = examplesFinder.extractFromProblem(p) val examples = examplesFinder.extractFromProblem(p)
val stringConverters: StringConverters = val definedStringConverters: StringConverters =
hctx.program.definedFunctions.filter(fd => fd.returnType == StringType && fd.params.length == 1) hctx.program.definedFunctions.filter(fd =>
fd.returnType == StringType && fd.params.length == 1
&& fd != hctx.program.library.escape.get
)
.groupBy({ fd => fd.paramIds.head.getType }).mapValues(fds => .groupBy({ fd => fd.paramIds.head.getType }).mapValues(fds =>
fds.map((fd : FunDef) => ((x: Expr) => functionInvocation(fd, Seq(x))))) fds.map((fd : FunDef) => ((x: Expr) => functionInvocation(fd, Seq(x)))))
val internalStringConverters: StringConverters = val abstractStringConverters: StringConverters =
(p.as.flatMap { case x => x.getType match { (p.as.flatMap { case x => x.getType match {
case FunctionType(Seq(aType), StringType) => List((aType, (arg: Expr) => application(Variable(x), Seq(arg)))) case FunctionType(Seq(aType), StringType) => List((aType, (arg: Expr) => application(Variable(x), Seq(arg))))
case _ => Nil case _ => Nil
...@@ -569,7 +583,11 @@ case object StringRender extends Rule("StringRender") { ...@@ -569,7 +583,11 @@ case object StringRender extends Rule("StringRender") {
val ruleInstantiations = ListBuffer[RuleInstantiation]() val ruleInstantiations = ListBuffer[RuleInstantiation]()
ruleInstantiations += RuleInstantiation("String conversion") { ruleInstantiations += RuleInstantiation("String conversion") {
val (expr, synthesisResult) = createFunDefsTemplates( val (expr, synthesisResult) = createFunDefsTemplates(
StringSynthesisContext.empty(stringConverters, internalStringConverters), p.as.map(Variable)) StringSynthesisContext.empty(
definedStringConverters,
abstractStringConverters,
p.as.toSet
), p.as.map(Variable))
val funDefs = synthesisResult.adtToString val funDefs = synthesisResult.adtToString
/*val toDebug: String = (("\nInferred functions:" /: funDefs)( (t, s) => /*val toDebug: String = (("\nInferred functions:" /: funDefs)( (t, s) =>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment