- Test modular integration of variables + ctx
- Removing boilerplate acceptors and visitors
- More abstract datatypes ~> Monoid[_] == Sum
- Adding mutable of mutable like operations
Created
June 29, 2017 08:28
-
-
Save manuelleduc/6f06aaa103368ea369d6479db78774e3 to your computer and use it in GitHub Desktop.
modularexp with mult extension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package modularexp.acceptor | |
/** | |
* Created by mleduc on 29/06/17. | |
*/ | |
trait Acceptor[A] { | |
def accept(): A | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package modularexp.acceptor | |
import modularexp.model.Const | |
import modularexp.visitors.VisitorConst | |
/** | |
* Created by mleduc on 29/06/17. | |
*/ | |
object AcceptorConst { | |
def initBuildConst[A](ppv: VisitorConst[A]): (Int) => Const[Acceptor[A]] with Acceptor[A] = (nbr) => { | |
lazy val self: Const[Acceptor[A]] with Acceptor[A] = new Const[Acceptor[A]] with Acceptor[A] { | |
override val value: Int = nbr | |
override def accept(): A = ppv.visitConst(self) | |
} | |
self | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package modularexp.acceptor | |
import modularexp.model.{Exp, Mult} | |
import modularexp.visitors.VisitorMult | |
/** | |
* Created by mleduc on 29/06/17. | |
*/ | |
object AcceptorMult { | |
def initBuildMult[A](ppv: VisitorMult[A]): (Exp[Acceptor[A]] with Acceptor[A], Exp[Acceptor[A]] with Acceptor[A]) => Mult[Acceptor[A]] with Acceptor[A] = (lhs_, rhs_) => { | |
lazy val self: Mult[Acceptor[A]] with Acceptor[A] = new Mult[Acceptor[A]] with Acceptor[A] { | |
override val lhs: Exp[Acceptor[A]] with Acceptor[A] = lhs_ | |
override val rhs: Exp[Acceptor[A]] with Acceptor[A] = rhs_ | |
override def accept(): A = ppv.visitMult(self) | |
} | |
self | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package modularexp.acceptor | |
import modularexp.model.{Exp, Sum} | |
import modularexp.visitors.VisitorSum | |
/** | |
* Created by mleduc on 29/06/17. | |
*/ | |
object AcceptorSum { | |
def initBuildSum[A](ppv: VisitorSum[A]): (Exp[Acceptor[A]] with Acceptor[A], Exp[Acceptor[A]] with Acceptor[A]) => Sum[Acceptor[A]] with Acceptor[A] = (lhs_, rhs_) => { | |
lazy val self: Sum[Acceptor[A]] with Acceptor[A] = new Sum[Acceptor[A]] with Acceptor[A] { | |
override val lhs: Exp[Acceptor[A]] with Acceptor[A] = lhs_ | |
override val rhs: Exp[Acceptor[A]] with Acceptor[A] = rhs_ | |
override def accept(): A = ppv.visitSum(self) | |
} | |
self | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package modularexp.model | |
/** | |
* Created by mleduc on 29/06/17. | |
*/ | |
trait Const[A] extends Exp[A] { | |
val value: Int | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package modularexp.model | |
import scala.language.higherKinds | |
/** | |
* Created by mleduc on 28/06/17. | |
*/ | |
trait Exp[A] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package modularexp.model | |
/** | |
* Created by mleduc on 29/06/17. | |
*/ | |
// TODO: really close to Sum except the name (alias ?) | |
trait Mult[A] extends Exp[A] { | |
val lhs: Exp[A] with A | |
val rhs: Exp[A] with A | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package modularexp.model | |
/** | |
* Created by mleduc on 29/06/17. | |
*/ | |
trait Sum[A] extends Exp[A] { | |
val lhs: Exp[A] with A | |
val rhs: Exp[A] with A | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package modularexp.semantics.calculate | |
import modularexp.acceptor.Acceptor | |
import modularexp.model.Const | |
import modularexp.visitors.VisitorConst | |
/** | |
* Created by mleduc on 29/06/17. | |
*/ | |
trait CalculateVisitorConst extends VisitorConst[Int] { | |
override def visitConst(e: Const[Acceptor[Int]]): Int = e.value | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package modularexp.semantics.calculate | |
import modularexp.acceptor.Acceptor | |
import modularexp.model.Mult | |
import modularexp.visitors.VisitorMult | |
/** | |
* Created by mleduc on 29/06/17. | |
*/ | |
trait CalculateVisitorMult extends VisitorMult[Int] { | |
override def visitMult(e: Mult[Acceptor[Int]]): Int = e.lhs.accept() * e.rhs.accept() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package modularexp.semantics.calculate | |
import modularexp.acceptor.Acceptor | |
import modularexp.model.Sum | |
import modularexp.visitors.VisitorSum | |
/** | |
* Created by mleduc on 29/06/17. | |
*/ | |
trait CalculateVisitorSum extends VisitorSum[Int] { | |
override def visitSum(e: Sum[Acceptor[Int]]): Int = e.lhs.accept() + e.rhs.accept() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package modularexp.semantics.prettyprint | |
import modularexp.acceptor.Acceptor | |
import modularexp.model.{Const, Sum} | |
import modularexp.visitors.{VisitorConst, VisitorSum} | |
/** | |
* Created by mleduc on 29/06/17. | |
*/ | |
trait PrettyPrintVisitor extends VisitorConst[String] with VisitorSum[String] { | |
override def visitConst(e: Const[Acceptor[String]]): String = e.value.toString | |
override def visitSum(e: Sum[Acceptor[String]]): String = s"(${e.lhs.accept()} + ${e.rhs.accept()})" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package modularexp.semantics.prettyprint | |
import modularexp.acceptor.Acceptor | |
import modularexp.model.Mult | |
import modularexp.visitors.VisitorMult | |
/** | |
* Created by mleduc on 29/06/17. | |
*/ | |
trait PretyPrintMultVisitor extends VisitorMult[String] { | |
override def visitMult(e: Mult[Acceptor[String]]): String = s"(${e.lhs.accept()} * ${e.rhs.accept()})" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package modularexp | |
import modularexp.acceptor.Acceptor | |
import modularexp.model.Sum | |
import modularexp.semantics.calculate.{CalculateVisitorConst, CalculateVisitorSum} | |
import modularexp.semantics.prettyprint.PrettyPrintVisitor | |
import modularexp.visitors.{VisitorConst, VisitorSum} | |
/** | |
* Created by mleduc on 29/06/17. | |
*/ | |
object TestConstSum extends App { | |
val ppv = new PrettyPrintVisitor {} | |
def program[A](ppv: VisitorSum[A] with VisitorConst[A]): Sum[Acceptor[A]] with Acceptor[A] = { | |
import modularexp.acceptor.AcceptorConst._ | |
import modularexp.acceptor.AcceptorSum._ | |
val buildSum = initBuildSum(ppv) | |
val buildConst = initBuildConst(ppv) | |
buildSum(buildConst(1), buildConst(2)) | |
} | |
println(program(ppv).accept()) | |
println(program(new CalculateVisitorConst with CalculateVisitorSum {}).accept()) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package modularexp | |
import modularexp.acceptor.Acceptor | |
import modularexp.model.Exp | |
import modularexp.semantics.calculate.{CalculateVisitorConst, CalculateVisitorMult, CalculateVisitorSum} | |
import modularexp.semantics.prettyprint.{PrettyPrintVisitor, PretyPrintMultVisitor} | |
import modularexp.visitors.{VisitorConst, VisitorMult, VisitorSum} | |
/** | |
* Created by mleduc on 29/06/17. | |
*/ | |
object TestConstSumMult extends App { | |
val ppv = new PrettyPrintVisitor with PretyPrintMultVisitor | |
def program[A](ppv: VisitorSum[A] with VisitorConst[A] with VisitorMult[A]): Exp[Acceptor[A]] with Acceptor[A] = { | |
import modularexp.acceptor.AcceptorConst._ | |
import modularexp.acceptor.AcceptorMult._ | |
import modularexp.acceptor.AcceptorSum._ | |
val buildSum = initBuildSum(ppv) | |
val buildConst = initBuildConst(ppv) | |
val buildMult = initBuildMult(ppv) | |
buildMult(buildSum(buildConst(1), buildConst(2)), buildConst(3)) | |
} | |
println(program(ppv).accept()) | |
println(program(new CalculateVisitorConst with CalculateVisitorSum with CalculateVisitorMult {}).accept()) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package modularexp.visitors | |
import modularexp.acceptor.Acceptor | |
import modularexp.model.Const | |
/** | |
* Created by mleduc on 29/06/17. | |
*/ | |
trait VisitorConst[A] { | |
def visitConst(e: Const[Acceptor[A]]): A | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package modularexp.visitors | |
import modularexp.acceptor.Acceptor | |
import modularexp.model.Mult | |
/** | |
* Created by mleduc on 29/06/17. | |
*/ | |
trait VisitorMult[A] { | |
def visitMult(e: Mult[Acceptor[A]]): A | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package modularexp.visitors | |
import modularexp.acceptor.Acceptor | |
import modularexp.model.Sum | |
/** | |
* Created by mleduc on 29/06/17. | |
*/ | |
trait VisitorSum[A] { | |
def visitSum(e: Sum[Acceptor[A]]): A | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment