/** COMP 411 Homework #0.5 * Symbolic Boolean Expressions */ /** Symbolic boolean expression representation */ sealed trait BoolExp sealed trait Atomic extends BoolExp with IfExp with NormExp /** BoolExp case classes */ case class Val(v: Boolean) extends Atomic { override def toString = if (v) "T" else "F" } case class Var(id: String) extends Atomic { override def toString = id } case class Not(exp: BoolExp) extends BoolExp { override def toString = "(! " + exp + ")" } case class And(lhs: BoolExp, rhs: BoolExp) extends BoolExp { override def toString = "(& " + lhs + " " + rhs + ")" } case class Or(lhs: BoolExp, rhs: BoolExp) extends BoolExp { override def toString = "(| " + lhs + " " + rhs + ")" } case class Implies(lhs: BoolExp, rhs: BoolExp) extends BoolExp { override def toString = "(> " + lhs + " " + rhs + ")" } case class If(test: BoolExp, conseq: BoolExp, alt: BoolExp) extends BoolExp { override def toString = "(? " + test + " " + conseq + " " + alt + ")" } /** Internal representation for if expressions */ sealed trait IfExp /** New case classes */ case class IIf(test: IfExp, conseq: IfExp, alt: IfExp) extends IfExp sealed trait NormExp case class NIf(test: Atomic, conseq: NormExp, alt: NormExp) extends NormExp object BoolExp { /* Convenience declarations for boolean constants * - Scala convention is to capitalize the first letter for a constant * - Such (capitalized) identifiers are treated as constants in match patterns */ val True = Val(true) val False = Val(false) /** Convert boolean expressions to internal if-representation for further processing */ def convertToIf(exp: BoolExp): IfExp = { /* YOUR CODE GOES HERE */ throw new UnsupportedOperationException("method not implemented") } /** Normalize the if-representation form of the given expression by making all test expressions slots atomic. * Returns a NormExp, which forces test expressions to be atomic.*/ def normalize(exp: IfExp): NormExp = { /* YOUR CODE GOES HERE */ throw new UnsupportedOperationException("method not implemented") } /** Perform head normalization (a help function for normalize) */ def headNormalize(test: NormExp, conseq: NormExp, alt: NormExp): NormExp = { /* YOUR CODE GOES HERE */ throw new UnsupportedOperationException("method not implemented") } /** Perform symbolic evaluation on the given expression * (i.e. simplify out tautologies, contradictions, etc.) */ def eval(exp: NormExp, env: Map[String,Boolean]): NormExp = { /* YOUR CODE GOES HERE */ throw new UnsupportedOperationException("method not implemented") } /** Convert a normalized if-representation (NormExp) back to a conventional boolean expression. * This function tries to map conditional expressions to more concise boolean expressions. */ def convertToBool(exp: NormExp): BoolExp = { /* YOUR CODE GOES HERE */ throw new UnsupportedOperationException("method not implemented") } }