...
The file Parser.java
includes
two Parser constructors parse(File file)
and parse(String form)
for building parsers to parse the boolean expression (in external
form) in the specified File
or String
, respectively. To construct a Parser
for the formula
in a file {{
HTML |
---|
<fileName> |
}} you must invoke
Code Block |
---|
new Parser(new File("<fileName>")); |
If you omit the new File(...)
construction in the argument to Parser
and use {{"
...
<}}
...
fileName
...
{{>
...
" }} instead,
you will create a Parser
for the String {{"
HTML |
---|
<}} {{fileName}} {{> |
<
fileName
>
{}
" "}} which is interpreted
as a simple boolean variable.
The File
input format is important because it enables us to
conveniently teat your simplifier on formulas thousands of symbols
long).
As a result, you only have to translate the Scheme code in
boolsimp.ss
into corresponding cleanly-written OO Java code by filling in the gaps in our Java
stub file boolSimp.dj1
. You are expected to
appropriately use the composite, interpreter, singleton, and visitor
patterns in the code that you write. Since the only stub files that
you have to modify are boolSimp.dj1
and boolSimpTest.java
, simply submit expanded
versions of these files
via OwlNet 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 on
the course wiki.
...
Code Block |
---|
(check-expect (head-normalize 'x 'y 'z) (make-If 'x 'y 'z))
(check-expect (head-normalize true 'y 'z) (make-If true 'y 'z))
(check-expect (head-normalize false 'y 'z) (make-If false 'y 'z))
(check-expect (head-normalize (make-If 'x 'y 'z) 'u 'v) (make-If 'x (make-If 'y 'u 'v) (make-If 'z 'u 'v)))
(check-expect (head-normalize (make-If 'x (make-If 'yt 'yc 'ya) (make-If 'zt 'zc 'za)) 'u 'v)
(make-If 'x (make-If 'yt (make-If 'yc 'u 'v) (make-If 'ya 'u 'v)) (make-If 'zt (make-If 'zc 'u 'v) (make-If 'za 'u 'v))))
(check-expect (normalize true) true)
(check-expect (normalize false) false)
(check-expect (normalize 'x) 'x)
(check-expect (normalize (make-If 'x 'y 'z)) (make-If 'x 'y 'z))
(check-expect (normalize (make-If (make-If 'x 'y 'z) 'u 'v)) (make-If 'x (make-If 'y 'u 'v) (make-If 'z 'u 'v)))
|
...
Wiki Markup |
---|
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-if-form}} data. To avoid this computational expense, the Scheme simplifier maintains a list of variable 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
}.
...
The final phase converts an expression in (not necessarily normalized or reduced) 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
Code Block |
---|
(make-If X Y Z)
|
where one of the arguments \ {X
, Y
, =Z=} is a constant {
true
, =false=}.
...
Code Block |
---|
(make-If X false true) => (make-Not X) (make-If X Y false) => (make-And X Y) (make-If X true Y) => (make-Or X Y) (make-If X Y true) => (make-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.
...