Skip to content

Commit

Permalink
[Preprocessing] Generalise negation normalisation + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TDacik committed Feb 10, 2025
1 parent 6880884 commit 1fb0446
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/preprocessing/NegationNormalisation.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

open SL

let is_negation phi = match SL.view phi with Not _ -> true | _ -> false
let get_negation phi = match SL.view phi with Not psi -> psi | _ -> assert false

let normalise =
SL.map_view (function
| And [f1; f2] -> begin match SL.view f1, SL.view f2 with
Expand All @@ -14,6 +17,14 @@ let normalise =
| _, Not g2 -> SL.mk_gneg f1 g2
| _, _ -> SL.mk_and @@ [f1; f2]
end
| And psis ->
let negated, others = List.partition is_negation psis in
begin match negated with
| [negation] ->
let phi = SL.mk_gneg (SL.mk_and others) (get_negation negation) in
SL.print phi;
phi
end
| Or [f1; f2] -> begin match SL.view f1, SL.view f2 with
| Not g1, Not g2 -> SL.mk_not @@ SL.mk_and [g1; g2]
| Not g1, _ -> SL.mk_not @@ SL.mk_gneg g1 f2
Expand Down
1 change: 1 addition & 0 deletions tests/dune
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
;; Preprocessing tests
IntroduceITE_tests
QuantifierElimination_tests
NegationNormalisation_tests

;Simplifier_tests
RemoveVariadic_tests
Expand Down
31 changes: 31 additions & 0 deletions tests/preprocessing/NegationNormalisation_tests.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
(* Tests for negation normalisation.
*
* Author: Tomas Dacik (idacik@fit.vut.cz), 2025 *)

module SL = SL_testable
open SL

let check_apply = SL.check_apply NegationNormalisation.apply

let test1 () =
let lhs = SL.mk_pto x x in
let rhs = SL.mk_pto y y in
let input = SL.mk_and [lhs; SL.mk_not rhs] in
let expected = SL.mk_gneg lhs rhs in
check_apply ~input ~expected

let test2 () =
let psi1 = SL.mk_pto x x in
let psi2 = SL.mk_pto y y in
let psi3 = SL.mk_pto z z in
let input = SL.mk_and [psi1; psi2; SL.mk_not psi3] in
let expected = SL.mk_gneg (SL.mk_and [psi1; psi2]) psi3 in
check_apply ~input ~expected

let () =
run "Negation normalisation" [
"apply", [
test_case "Binary" `Quick test1;
test_case "N-anary" `Quick test2;
];
]

0 comments on commit 1fb0446

Please sign in to comment.