]> gitweb.factorcode.org Git - factor.git/commitdiff
boolean-expr: add some more expression simplification rules
authorAlexander Ilin <alex.ilin@protonmail.com>
Tue, 4 Jan 2022 00:11:58 +0000 (01:11 +0100)
committerJohn Benediktsson <mrjbq7@gmail.com>
Tue, 4 Jan 2022 17:09:15 +0000 (09:09 -0800)
Add some tests and comments.

extra/boolean-expr/boolean-expr-tests.factor [new file with mode: 0644]
extra/boolean-expr/boolean-expr.factor

diff --git a/extra/boolean-expr/boolean-expr-tests.factor b/extra/boolean-expr/boolean-expr-tests.factor
new file mode 100644 (file)
index 0000000..8f99a69
--- /dev/null
@@ -0,0 +1,16 @@
+! Copyright (C) 2022 Alexander Ilin.
+! See http://factorcode.org/license.txt for BSD license.
+USING: boolean-expr literals tools.test ;
+IN: boolean-expr.tests
+
+${ P ¬ Q ⋀ } [ P ¬ P Q ⋁ ⋀ ] unit-test
+${ P ¬ Q ⋀ } [ P ¬ Q P ⋁ ⋀ ] unit-test
+${ P Q ⋀ } [ P P ¬ Q ⋁ ⋀ ] unit-test
+${ P Q ⋀ } [ P Q P ¬ ⋁ ⋀ ] unit-test
+
+! The following tests can't pass because only the distributivity of ∧ over ∨ is
+! implemented, but not the other way around, to prevent infinite loops.
+! ${ P ¬ Q ⋀ } [ P ¬ P Q ⋀ ⋁ ] unit-test
+! ${ Q P ¬ ⋀ } [ P ¬ Q P ⋀ ⋁ ] unit-test
+! ${ P Q ⋀ } [ P P ¬ Q ⋀ ⋁ ] unit-test
+! ${ P Q ⋀ } [ P Q P ¬ ⋀ ⋁ ] unit-test
index 0f8c5b1091241657a0a90158126a79598ad1dbde..9959e93333f3913867077b327ee7b58889a39de1 100644 (file)
@@ -2,7 +2,7 @@
 ! See http://factorcode.org/license.txt for BSD license.
 USING: accessors arrays classes kernel sequences sets
 io prettyprint ;
-FROM: multi-methods => GENERIC: METHOD: ;
+FROM: multi-methods => GENERIC: METHOD: method ;
 IN: boolean-expr
 
 TUPLE: ⋀ x y ;
@@ -22,10 +22,15 @@ METHOD: ⋀ { □ ⊤ } drop ;
 METHOD: ⋀ { ⊥ □ } drop ;
 METHOD: ⋀ { □ ⊥ } nip ;
 
+! Implement only distributivity of ∧ over ∨, but not ∨ over ∧ to avoid
+! infinite looping. This makes sure that internally we always use DNF.
 METHOD: ⋀ { ⋁ □ } [ [ x>> ] dip ⋀ ] [ [ y>> ] dip ⋀ ] 2bi ⋁ ;
 METHOD: ⋀ { □ ⋁ } [ x>> ⋀ ] [ y>> ⋀ ] 2bi ⋁ ;
 
-METHOD: ⋀ { □ □ } \ ⋀ boa ;
+METHOD: ⋀ { ¬ ⋁ } { □ ⋁ } \ ⋀ method execute( x y -- expr ) ;
+METHOD: ⋀ { ¬ □ } 2dup swap x>> = [ 2drop ⊥ ] [ \ ⋀ boa ] if ;
+METHOD: ⋀ { □ ¬ } 2dup      x>> = [ 2drop ⊥ ] [ \ ⋀ boa ] if ;
+METHOD: ⋀ { □ □ } 2dup = [ drop ] [ \ ⋀ boa ] if ;
 
 GENERIC: ⋁ ( x y -- expr )
 
@@ -34,7 +39,9 @@ METHOD: ⋁ { □ ⊤ } nip ;
 METHOD: ⋁ { ⊥ □ } nip ;
 METHOD: ⋁ { □ ⊥ } drop ;
 
-METHOD: ⋁ { □ □ } \ ⋁ boa ;
+METHOD: ⋁ { ¬ □ } 2dup swap x>> = [ 2drop ⊤ ] [ \ ⋁ boa ] if ;
+METHOD: ⋁ { □ ¬ } 2dup      x>> = [ 2drop ⊤ ] [ \ ⋁ boa ] if ;
+METHOD: ⋁ { □ □ } 2dup = [ drop ] [ \ ⋁ boa ] if ;
 
 GENERIC: ¬ ( x -- expr )