Write a Scala function reduce in a Scala object Reducer in package comp402 that reduces boolean expressions (represented in the input and output streams in Scheme-like notation) to simplified form. Store your Scala function in a file BoolSimp.scala. For the purposes of this assignment, boolean expressions are Scheme expressions constructed from:
x T (& x y) (| x (! y)) (? x y F) (> x F) (> (& F y) x) (> z (| y z)) (> (| (! a) b) (> a b))
The shorter names T, F, !, &, |, >, and ? are used instead of true, false, not, and, or, implies, and if for notational brevity which matters in very large inputs.
The course staff is providing:
The stub file BoolExp.scala also includes comments showing you exactly what code you have to write to complete writing your simplifier. Of course, you also need to write corresponding tests and add them to the file BoolExpTest.java.
The code in Parser.scala enables you to test your solution on large inputs stored in files. Parser.scala includes two Parser constructors Parser(File file) and Parser(String form) for building parsers to parse the boolean expression (in external text form) in the specified File or String, respectively. Since the library class File is defined in the package java.io, you need to insert either
import java.io._
or more specifically
import java.io._
at the head of a test file that uses the Parser class on the contents of a file.
To construct a Parser for the formula in a file <fileName> you must invoke
Parser(File(<fileName>)).
If you omit the File(...) construction in the argument to Parser and use "<fileName>" instead, you will create a Parser for the String "<fileName>". which is interpreted as a simple boolean variable. The File input format is important because it enables us to conveniently apply your simplifier to formulas that are thousands of symbols long. You are expected to appropriately use case classes and pattern matching to write your code Since the only stub files that you have to modify are BoolSimp.scala and BoolSimpTest.java, simply submit expanded versions of these files via OwlSpace to submit your assignment. Warning: we will run your program on large inputs to determine if you wrote the code correctly. Try using the large test files provided in ??.
Given a parsed input of type boolExp, the simplification process consists of following four phases:
A description of each of these phases follows. The reduce function has type String -> String.
A boolean expression (boolExp) can be converted to if form by repeatedly applying the following rewrite rules in any order until no rule is applicable.
(! X) => (if X false true) (& X Y) => (if X Y false) (| X Y) => (if X true Y) (> X Y) => (if X Y true) // recall that ">" denotes "implies"
In these rules, we use the external textual notation for formulas rather than the internal Scala represe1ntation. Note that the "if" constructor on the right hand side of these rules should be implemented in your program by IIf objects. The symbols X and Y denote arbitrary boolExps}. The conversion process always terminates (since each rewrite strictly reduces the number of logical connectives excluding {{make-If) and yields a unique answer independent of the order in which the rewrites are performed. This property is called the Church-Rosser property, after the logicians (Alonzo Church and Barkley Rosser) who invented the concept.
Since the reduction rules for this phase are Church-Rosser, you can write the function convertToIf using simple structural recursion. For each of the boolean operators &, |, !, >, and if, reduce the component expressions first and then applying the matching reduction (except for if for which there is no top-level reduction).
The following examples (written in Scala notation) illustrate the conversion process:
convertToIf(Or(And('x, 'y)), 'z) => IIf(IIf('x, 'y, false), true, 'z)
convertToIf(Implies('x, Not('y))) => IIf('x, IIf('y, false, true), true)
We suggest simply traversing the tree using the structural recursion template for type BoolExp and converting all Form structures to the corresponding IIf structures.
The provided function parse: input -> boolExp takes a Scheme expression and returns the corresponding boolExp.
An IfExp is normalized iff every sub-expression in test position is either a variable (symbol) or a constant (true or false). We call the set of normalized IfExps norm-IfExp; it cannot be defined as a Scala type.
For example, the IfExp (make-If (make-If X Y Z) U V)) is not a norm-IfExp because it has an IIf construction in test position. In contrast, the equivalent IfExp (make-If X (make-If Y U V) (make-If Z U V)) is normalized and hence is an norm-IfExp.
The normalization process, implemented by the function normalize: IfExp -> norm-IfExp eliminates all if constructions that appear in test positions inside if constructions. We perform this transformation by repeatedly applying the following rewrite rule (to any portion of the expression) until it is inapplicable:
IIf(IIf(X, Y, Z), U, V) => IIf(X, IIf(Y, U, V), IIf(Z, U, V)).
This transformation always terminates and yields a unique answer independent of the order in which rewrites are performed. The proof of this fact is left as an optional exercise.
In the normalize function, it is critically important not to duplicate any work, so the order in which reductions are made really matters. Do NOT apply the normalization rule above unless U and V are already normalized, because the rule duplicates both U and V. If you reduce the consequent and the alternative (U and V in the left hand side of the rule above) before reducing the test, normalize runs in linear time (in the number of nodes in the input); if done in the wrong order it runs in exponential time in the worst case. (And some of our test cases will exhibit this worst case behavior.)
Hint: define an auxiliary function headNormalize that takes three norm-If Exps X, Y, and Z and constructs a norm-ifExp equivalent to IIf(X, Y, Z). This help function processes X because the test position must be a variable or a constant, yet X can be an arbitrary norm-ifExp. In contrast, (headNormalize X Y Z) never even inspects Y and Z because they are already normalized and the normalizing transformations performed in headNormalize never place these expressions in test position.
The following examples illustrate how the normalize and headNormalize functions behave (expressed in Scala notation):
headNormalize('x, 'y, 'z) =>: IIf('x, 'y, 'z)
headNormalize(True, 'y, 'z) =>: IIf(True, 'y, 'z)
headNormalize(False, 'y, 'z) =>: IIf(False, 'y, 'z)
headNormalize(IIf('x, 'y, 'z), 'u, 'v) =>: IIf('x, IIf('y, 'u, 'v), IIf('z, 'u, 'v))
headNormalize(IIf('x, IIf('yt, 'yc, 'ya), IIf('zt, 'zc, 'za)), 'u, 'v) =>:
IIf('x, IIf('yt, IIf('yc, 'u, 'v)), IIf('ya, 'u, 'v), IIf('zt, IIf('zc, 'u, 'v)), IIf('za, 'u, 'v))
normalize(True) =>: True
normalize(False) =>: False
normalize('x) =>: 'x
normalize(IIf('x, 'y, 'z)) =>: IIf('x, 'y, 'z)
normalize(IIf(IIf('x, 'y, 'z), 'u, 'v)) =>: IIf('x, IIf('y, 'u, 'v), IIf('z, 'u, 'v))
Once a large formula has been normalized, do not try to print it unless you know that the formula is small! The printed form can be exponentially larger than the internal representation (because the internal representation can share subtrees).
Before you start writing normalize, write the template corresponding to the inductive data definition of norm-ifExp.
The symbolic evaluation process, implemented by the function eval: norm-if-form environment -> norm-if-form, reduces a norm-if-form to simple form. In particular, it reduces all tautologies (expressions that are always true) to true and all contradictions (expressions that are always false) to false.
Symbolic evaluation applies the following rewrite rules to an expression until none is applicable (with one exception discussed below):
IIf(T, X, Y) => X IIf(F, X, Y) => Y IIf(X, T, F) => X IIf(X, Y, Y) => Y IIf(X, Y, Z) => IIf(X, Y\[X <\- true\], Z\[X <\- false\])
The notation M[X <\- N] means M with all occurrences of the symbol X replaced by the expression N. It is very costly to actually perform these subtitutions on norm-IfExp. To avoid this computational expense, we simply maintain a list of bindings which are pairs consisting of symbols (variable names) and boolean values {true, false. The following data definition definition formally defines the binding type.
A binding is a pair (make-binding s v) where s is a symbol (a variable) and v is a boolean value (an element of { true, false }.
An environment is a (list-of binding).
When the eval function encounters a variable (symbol), it looks up the symbol in the environment and replaces the symbol it's boolean value if it exists.
These rewrite rules do not have the Church-Rosser property. The last two rewrite rules are the spoilers; the relative order in which they are applied can affect the result in some cases. However, the rewrite rules do have the Church-Rosser property on expressions which are tautologies or contradictions.
If the final rule is applied only when X actually occurs in either Y or Z, then the symbolic evaluation process is guaranteed to terminate. In this case, every rule either reduces the size of the expression or the number of variable occurrences in it.
We recommend applying the rules in the order shown from the top down until no more reductions are possible (using the constraint on the final rule). Note that the last rule should only be applied once to a given sub-expression.
The final phase converts an expression in (not necessarily reduced or normalized) If form to an equivalent expression constructed from variables and { true, false, And, Or, Not, Implies, If. This process eliminates every expression of the form
IIf(X, Y, Z)
where one of the arguments {X, Y, Z is a constant { true, false }.
Use the following set of reduction rules to perform this conversion
IIf(X, false, true) => Not(X) IIf(X, Y, false) => And(X, Y) IIf(X, true, Y) => Or(X, Y) IIf(X, Y, T) => Implies(X, Y)
where X , Y , and Z are arbitrary If forms. This set of rules is Church-Rosser, so the rules can safely be applied using simple structural recursion.
The abstract syntax classes in BoolSimp.java include a separate composite (case class) hierarchy called IfForm for representing boolean expressions exclusively in terms of conditionals. This representation includes only case classes, making it much easier to write the functions perform normalization, evaluation, and clean-up. This representation also improves program performance.
In Scala, you can write pattern matching functions explicitly instead of encoding them as visitor objects. (The Scala compiler constructs the appropriate visitors classes for execution on the JVM.)
Here are the links for the files:
The following files contain large formulas that can be reduced by your simplifier. Only the files named bigData x require a larger thread stack size than the JVM default on most platforms. NOTE: to handle the bigData x files, you must set JVM argument -Xss64M for the Interactions JVM using the DrJava Preferences command on the Edit menu. The JVM argument setting can be found on the last panel (called JVMs) in the Preferences categories tree.