theory NNFInClass imports Main begin datatype formula = Var nat | And formula formula | Or formula formula | Neg formula primrec eval :: "formula \ (nat \ bool) \ bool" where "eval (Var p) env = env p" | "eval (And f1 f2) env = (eval f1 env \ eval f2 env)" | "eval (Or f1 f2) env = (eval f1 env \ eval f2 env)" | "eval (Neg f) env = (\ eval f env)" primrec nnf :: "formula \ bool" where "nnf (Var p) = True" | "nnf (And f1 f2) = (nnf f1 \ nnf f2)" | "nnf (Or f1 f2) = (nnf f1 \ nnf f2)" | "nnf (Neg f) = (case f of Var r \ True | _ \ False)" primrec to_nnf :: "formula \ bool \ formula" where "to_nnf (Var p) b = (if b then Var p else Neg (Var p))" | "to_nnf (And f1 f2) b = (if b then And (to_nnf f1 b) (to_nnf f2 b) else Or (to_nnf f1 b) (to_nnf f2 b) )" | "to_nnf (Or f1 f2) b = (if b then Or (to_nnf f1 b) (to_nnf f2 b) else And (to_nnf f1 b) (to_nnf f2 b) )" | "to_nnf (Neg f) b = to_nnf f (\ b)" theorem "nnf (to_nnf f b)" apply (induct f arbitrary: b) apply auto done -- "A unit test (was a counterexample generated by Isabelle" lemma "(to_nnf (Neg (Or (Var (Suc 0)) (Var (Suc 0)))) True) = And (Neg (Var (Suc 0))) (Neg (Var (Suc 0)))" by simp theorem "(eval f env = eval (to_nnf f True) env) \ (eval f env = (\ (eval (to_nnf f False) env)))" apply (induct f) apply auto done end