Skip to content
Snippets Groups Projects
Commit 1ec8524a authored by Philippe Suter's avatar Philippe Suter
Browse files

No commit message

No commit message
parent a39bf16f
No related branches found
No related tags found
No related merge requests found
...@@ -93,19 +93,10 @@ class Analysis(val program: Program) { ...@@ -93,19 +93,10 @@ class Analysis(val program: Program) {
} }
} }
def flatten(expr: Expr) : (Expr,List[(Variable,Expr)]) = {
// Recursively flattens the expression. The head in the end
// should be the top-level original expression.
def fl(expr: Expr, lets: List[(Variable,Expr)]) : List[(Variable,Expr)] = expr match {
case _ => throw new Exception("ah ha !")
}
(expr, Nil)
}
def replaceInExpr(substs: Map[Expr,Expr], expr: Expr) : Expr = { def replaceInExpr(substs: Map[Expr,Expr], expr: Expr) : Expr = {
def rec(ex: Expr) : Expr = ex match { def rec(ex: Expr) : Expr = ex match {
case _ if (substs.get(ex).isDefined) => substs(ex) case _ if (substs.get(ex).isDefined) => substs(ex)
case Let(i,e,b) => Let(i, rec(e), rec(b))
case FunctionInvocation(fd, args) => FunctionInvocation(fd, args.map(rec(_))) case FunctionInvocation(fd, args) => FunctionInvocation(fd, args.map(rec(_)))
case IfExpr(t1,t2,t3) => IfExpr(rec(t1),rec(t2),rec(t3)) case IfExpr(t1,t2,t3) => IfExpr(rec(t1),rec(t2),rec(t3))
case MatchExpr(_,_) => ex case MatchExpr(_,_) => ex
......
...@@ -69,6 +69,40 @@ object TypeTrees { ...@@ -69,6 +69,40 @@ object TypeTrees {
case _ => scala.Predef.error("Asking for lub of unrelated types: " + t1 + " and " + t2) case _ => scala.Predef.error("Asking for lub of unrelated types: " + t1 + " and " + t2)
} }
// returns the number of distinct values that inhabit a type
sealed abstract class TypeSize
case class FiniteSize(size: Int) extends TypeSize
case object InfiniteSize extends TypeSize
def domainSize(typeTree: TypeTree) : TypeSize = typeTree match {
case NoType => FiniteSize(0)
case AnyType => InfiniteSize
case BooleanType => FiniteSize(2)
case Int32Type => InfiniteSize
case ListType(_) => InfiniteSize
case TupleType(bases) => {
val baseSizes = bases.map(domainSize(_))
baseSizes.find(_ == InfiniteSize) match {
case Some(_) => InfiniteSize
case None => FiniteSize(baseSizes.map(_.asInstanceOf[FiniteSize].size).reduceLeft(_ * _))
}
}
case SetType(base) => domainSize(base) match {
case InfiniteSize => InfiniteSize
case FiniteSize(n) => FiniteSize(scala.math.pow(2, n).toInt)
}
case MapType(from,to) => (domainSize(from),domainSize(to)) match {
case (InfiniteSize,_) => InfiniteSize
case (_,InfiniteSize) => InfiniteSize
case (FiniteSize(n),FiniteSize(m)) => FiniteSize(scala.math.pow(m+1, n).toInt)
}
case OptionType(base) => domainSize(base) match {
case InfiniteSize => InfiniteSize
case FiniteSize(n) => FiniteSize(n+1)
}
case c: ClassType => InfiniteSize
}
case object NoType extends TypeTree case object NoType extends TypeTree
case object AnyType extends TypeTree case object AnyType extends TypeTree
...@@ -77,7 +111,6 @@ object TypeTrees { ...@@ -77,7 +111,6 @@ object TypeTrees {
case class ListType(base: TypeTree) extends TypeTree case class ListType(base: TypeTree) extends TypeTree
case class TupleType(bases: Seq[TypeTree]) extends TypeTree { lazy val dimension: Int = bases.length } case class TupleType(bases: Seq[TypeTree]) extends TypeTree { lazy val dimension: Int = bases.length }
case class FunctionType(arg: TypeTree, res: TypeTree) extends TypeTree
case class SetType(base: TypeTree) extends TypeTree case class SetType(base: TypeTree) extends TypeTree
// case class MultisetType(base: TypeTree) extends TypeTree // case class MultisetType(base: TypeTree) extends TypeTree
case class MapType(from: TypeTree, to: TypeTree) extends TypeTree case class MapType(from: TypeTree, to: TypeTree) extends TypeTree
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment