forked from factor/factor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboolean-expr.factor
More file actions
95 lines (64 loc) · 2.23 KB
/
Copy pathboolean-expr.factor
File metadata and controls
95 lines (64 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
! Copyright (C) 2008 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
USING: accessors arrays classes kernel sequences sets
io prettyprint multi-methods ;
IN: boolean-expr
! Demonstrates the use of Unicode symbols in source files, and
! multi-method dispatch.
TUPLE: ⋀ x y ;
TUPLE: ⋁ x y ;
TUPLE: ¬ x ;
SINGLETONS: ⊤ ⊥ ;
SINGLETONS: P Q R S T U V W X Y Z ;
UNION: □ ⋀ ⋁ ¬ ⊤ ⊥ P Q R S T U V W X Y Z ;
GENERIC: ⋀ ( x y -- expr )
METHOD: ⋀ { ⊤ □ } nip ;
METHOD: ⋀ { □ ⊤ } drop ;
METHOD: ⋀ { ⊥ □ } drop ;
METHOD: ⋀ { □ ⊥ } nip ;
METHOD: ⋀ { ⋁ □ } [ [ x>> ] dip ⋀ ] [ [ y>> ] dip ⋀ ] 2bi ⋁ ;
METHOD: ⋀ { □ ⋁ } [ x>> ⋀ ] [ y>> ⋀ ] 2bi ⋁ ;
METHOD: ⋀ { □ □ } \ ⋀ boa ;
GENERIC: ⋁ ( x y -- expr )
METHOD: ⋁ { ⊤ □ } drop ;
METHOD: ⋁ { □ ⊤ } nip ;
METHOD: ⋁ { ⊥ □ } nip ;
METHOD: ⋁ { □ ⊥ } drop ;
METHOD: ⋁ { □ □ } \ ⋁ boa ;
GENERIC: ¬ ( x -- expr )
METHOD: ¬ { ⊤ } drop ⊥ ;
METHOD: ¬ { ⊥ } drop ⊤ ;
METHOD: ¬ { ⋀ } [ x>> ¬ ] [ y>> ¬ ] bi ⋁ ;
METHOD: ¬ { ⋁ } [ x>> ¬ ] [ y>> ¬ ] bi ⋀ ;
METHOD: ¬ { □ } \ ¬ boa ;
: → ( x y -- expr ) ¬ ⋀ ;
: ⊕ ( x y -- expr ) [ ⋁ ] [ ⋀ ¬ ] 2bi ⋀ ;
: ≣ ( x y -- expr ) [ ⋀ ] [ [ ¬ ] bi@ ⋀ ] 2bi ⋁ ;
GENERIC: (cnf) ( expr -- cnf )
METHOD: (cnf) { ⋀ } [ x>> (cnf) ] [ y>> (cnf) ] bi append ;
METHOD: (cnf) { □ } 1array ;
GENERIC: cnf ( expr -- cnf )
METHOD: cnf { ⋁ } [ x>> cnf ] [ y>> cnf ] bi append ;
METHOD: cnf { □ } (cnf) 1array ;
GENERIC: satisfiable? ( expr -- ? )
METHOD: satisfiable? { ⊤ } drop t ;
METHOD: satisfiable? { ⊥ } drop f ;
: partition ( seq quot -- left right )
[ [ not ] compose filter ] [ filter ] 2bi ; inline
: (satisfiable?) ( seq -- ? )
[ \ ¬ instance? ] partition [ x>> ] map intersect empty? ;
METHOD: satisfiable? { □ }
cnf [ (satisfiable?) ] any? ;
GENERIC: (expr.) ( expr -- )
METHOD: (expr.) { □ } pprint ;
: op. ( expr -- )
"(" write
[ x>> (expr.) ]
[ bl class pprint bl ]
[ y>> (expr.) ]
tri
")" write ;
METHOD: (expr.) { ⋀ } op. ;
METHOD: (expr.) { ⋁ } op. ;
METHOD: (expr.) { ¬ } [ class pprint ] [ x>> (expr.) ] bi ;
: expr. ( expr -- ) (expr.) nl ;