| 1 |
structure Wff : WFF = |
| 2 |
struct |
| 3 |
|
| 4 |
datatype wff = FALSE | TRUE | VAR of string |
| 5 |
| AND of wff * wff |
| 6 |
| OR of wff * wff |
| 7 |
| NOT of wff |
| 8 |
|
| 9 |
fun simplify e = |
| 10 |
Generic.rewrite( |
| 11 |
let include "wff.sig" |
| 12 |
fun wff (NOT FALSE) = TRUE |
| 13 |
| wff (NOT TRUE) = FALSE |
| 14 |
| wff (NOT(NOT x)) = x |
| 15 |
| wff (AND(TRUE,x)) = x |
| 16 |
| wff (AND(x,TRUE)) = x |
| 17 |
| wff (AND(FALSE,x)) = FALSE |
| 18 |
| wff (AND(x,FALSE)) = FALSE |
| 19 |
| wff (OR(TRUE,x)) = TRUE |
| 20 |
| wff (OR(x,TRUE)) = TRUE |
| 21 |
| wff (OR(FALSE,x)) = x |
| 22 |
| wff (OR(x,FALSE)) = x |
| 23 |
in rewrite'wff e |
| 24 |
end |
| 25 |
) |
| 26 |
|
| 27 |
fun countNots e = |
| 28 |
let val count = ref 0 |
| 29 |
in Generic.app( |
| 30 |
let include "wff.sig" |
| 31 |
fun wff (NOT _) = count := !count + 1 |
| 32 |
| wff _ = () |
| 33 |
in app'wff e; |
| 34 |
!count |
| 35 |
end |
| 36 |
) |
| 37 |
end |
| 38 |
|
| 39 |
fun countNots2 e = |
| 40 |
Generic.fold( |
| 41 |
let include "wff.sig" |
| 42 |
fun wff (NOT _, n) = n+1 |
| 43 |
| wff (_, n) = n |
| 44 |
in fold'wff(e, 0) |
| 45 |
end |
| 46 |
) |
| 47 |
|
| 48 |
fun allVars e = |
| 49 |
Generic.fold( |
| 50 |
let include "wff.sig" |
| 51 |
fun wff (VAR v, vs) = v::vs | wff (_, vs) = vs |
| 52 |
in fold'wff(e, []) |
| 53 |
end) |
| 54 |
|
| 55 |
end |