1 ! Copyright (C) 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays classes kernel sequences sets
4 io prettyprint multi-methods ;
7 ! Demonstrates the use of Unicode symbols in source files, and
8 ! multi-method dispatch.
16 SINGLETONS: P Q R S T U V W X Y Z ;
18 UNION: □ ⋀ ⋁ ¬ ⊤ ⊥ P Q R S T U V W X Y Z ;
20 GENERIC: ⋀ ( x y -- expr )
22 METHOD: ⋀ { ⊤ □ } nip ;
23 METHOD: ⋀ { □ ⊤ } drop ;
24 METHOD: ⋀ { ⊥ □ } drop ;
25 METHOD: ⋀ { □ ⊥ } nip ;
27 METHOD: ⋀ { ⋁ □ } [ [ x>> ] dip ⋀ ] [ [ y>> ] dip ⋀ ] 2bi ⋁ ;
28 METHOD: ⋀ { □ ⋁ } [ x>> ⋀ ] [ y>> ⋀ ] 2bi ⋁ ;
30 METHOD: ⋀ { □ □ } \ ⋀ boa ;
32 GENERIC: ⋁ ( x y -- expr )
34 METHOD: ⋁ { ⊤ □ } drop ;
35 METHOD: ⋁ { □ ⊤ } nip ;
36 METHOD: ⋁ { ⊥ □ } nip ;
37 METHOD: ⋁ { □ ⊥ } drop ;
39 METHOD: ⋁ { □ □ } \ ⋁ boa ;
41 GENERIC: ¬ ( x -- expr )
43 METHOD: ¬ { ⊤ } drop ⊥ ;
44 METHOD: ¬ { ⊥ } drop ⊤ ;
46 METHOD: ¬ { ⋀ } [ x>> ¬ ] [ y>> ¬ ] bi ⋁ ;
47 METHOD: ¬ { ⋁ } [ x>> ¬ ] [ y>> ¬ ] bi ⋀ ;
49 METHOD: ¬ { □ } \ ¬ boa ;
51 : → ( x y -- expr ) ¬ ⋀ ;
52 : ⊕ ( x y -- expr ) [ ⋁ ] [ ⋀ ¬ ] 2bi ⋀ ;
53 : ≣ ( x y -- expr ) [ ⋀ ] [ [ ¬ ] bi@ ⋀ ] 2bi ⋁ ;
55 GENERIC: (cnf) ( expr -- cnf )
57 METHOD: (cnf) { ⋀ } [ x>> (cnf) ] [ y>> (cnf) ] bi append ;
58 METHOD: (cnf) { □ } 1array ;
60 GENERIC: cnf ( expr -- cnf )
62 METHOD: cnf { ⋁ } [ x>> cnf ] [ y>> cnf ] bi append ;
63 METHOD: cnf { □ } (cnf) 1array ;
65 GENERIC: satisfiable? ( expr -- ? )
67 METHOD: satisfiable? { ⊤ } drop t ;
68 METHOD: satisfiable? { ⊥ } drop f ;
70 : partition ( seq quot -- left right )
71 [ [ not ] compose filter ] [ filter ] 2bi ; inline
73 : (satisfiable?) ( seq -- ? )
74 [ \ ¬ instance? ] partition [ x>> ] map intersect empty? ;
76 METHOD: satisfiable? { □ }
77 cnf [ (satisfiable?) ] any? ;
79 GENERIC: (expr.) ( expr -- )
81 METHOD: (expr.) { □ } pprint ;
86 [ bl class pprint bl ]
91 METHOD: (expr.) { ⋀ } op. ;
92 METHOD: (expr.) { ⋁ } op. ;
93 METHOD: (expr.) { ¬ } [ class pprint ] [ x>> (expr.) ] bi ;
95 : expr. ( expr -- ) (expr.) nl ;