aboutsummaryrefslogtreecommitdiff
path: root/semestr-2/racket/cnf.rkt
blob: 67bd70f1e436bd1d46e780f203c6547030710300 (plain)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#lang racket

(define (var? t) (symbol? t))

(define (neg? t)
  (and (list? t)
       (= 2 (length t))
       (eq? 'neg (car t))))

(define (conj? t)
  (and (list? t)
       (= 3 (length t))
       (eq? 'conj (car t))))

(define (disj? t)
  (and (list? t)
       (= 3 (length t))
       (eq? 'disj (car t))))

(define (lit? t)
  (or (var? t)
      (and (neg? t)
           (var? (neg-subf t)))))

(define (prop? f)
  (or (var? f)
      (and (neg? f)
           (prop? (neg-subf f)))
      (and (disj? f)
           (prop? (disj-left f))
           (prop? (disj-right f)))
      (and (conj? f)
           (prop? (conj-left f))
           (prop? (conj-right f)))))

(define (make-conj left right)
  (list 'conj left right))

(define (make-disj left right)
  (list 'disj left right))

(define (make-neg f)
  (list 'neg f))

(define (conj-left f)
  (if (conj? f)
      (cadr f)
      (error "Złe dane ze znacznikiem -- CONJ-LEFT" f)))

(define (conj-right f)
  (if (conj? f)
      (caddr f)
      (error "Złe dane ze znacznikiem -- CONJ-RIGHT" f)))

(define (disj-left f)
  (if (disj? f)
      (cadr f)
      (error "Złe dane ze znacznikiem -- DISJ-LEFT" f)))

(define (disj-right f)
  (if (disj? f)
      (caddr f)
      (error "Złe dane ze znacznikiem -- DISJ-RIGHT" f)))

(define (neg-subf f)
  (if (neg? f)
      (cadr f)
      (error "Złe dane ze znacznikiem -- NEG-FORM" f)))

(define (lit-var f)
  (cond [(var? f) f]
        [(neg? f) (neg-subf f)]
        [else (error "Złe dane ze znacznikiem -- LIT-VAR" f)]))
      
(define (free-vars f)
  (cond [(null? f) null]
        [(var? f) (list f)]
        [(neg? f) (free-vars (neg-subf f))]
        [(conj? f) (append (free-vars (conj-left f))
                           (free-vars (conj-right f)))]
        [(disj? f) (append (free-vars (disj-left f))
                           (free-vars (disj-right f)))]
        [else (error "Zła formula -- FREE-VARS" f)]))

(define (gen-vals xs)
  (if (null? xs)
      (list null)
      (let*
          ((vss (gen-vals (cdr xs)))
           (x (car xs))
           (vst (map (λ (vs) (cons (list x true) vs)) vss))
           (vsf (map (λ (vs) (cons (list x false) vs)) vss)))
        (append vst vsf))))

(define (eval-formula f evaluation)
  (cond [(var? f)
         (let ((val (assoc f evaluation)))
           (if (not val)
               (error "Zmienna wolna nie wystepuje w wartościowaniu -- EVAL-FORMULA" f evaluation)
               (cadr val)))]
        [(neg? f) (not (eval-formula (neg-subf f) evaluation))]
        [(disj? f) (or (eval-formula (disj-left f) evaluation)
                       (eval-formula (disj-right f) evaluation))]
        [(conj? f) (and (eval-formula (conj-left f) evaluation)
                        (eval-formula (conj-right f) evaluation))]
        [else (error "Zła formuła -- EVAL-FORMULA" f evaluation)]))

(define (falsifable-eval? f)
  (let* ((evaluations (gen-vals (free-vars f)))
         (results (map (λ (evaluation) (eval-formula f evaluation)) evaluations)))
    (ormap false? results)))

(define (nff? f)
  (cond [(lit? f) true]
        [(neg? f) false]
        [(conj? f) (and (nff? (conj-left f))
                        (nff? (conj-right f)))]
        [(disj? f) (and (nff? (disj-left f))
                        (nff? (disj-right f)))]
        [else (error "Zła formuła -- NFF?" f)]))

(define (convert-to-nnf f)
  (cond [(lit? f) f]
        [(neg? f) (convert-negation (neg-subf f))]
        [(conj? f) (make-conj (convert-to-nnf (conj-left f))
                              (convert-to-nnf (conj-right f)))]
        [(disj? f) (make-disj (convert-to-nnf (disj-left f))
                              (convert-to-nnf (disj-right f)))]
        [else (error "Zła formuła -- CONVERT" f)]))

(define (convert-negation f)
  (cond [(lit? f)
         (if (var? f)
             (make-neg f)
             (neg-subf f))]
        [(neg? f) (convert-to-nnf (neg-subf f))]
        [(conj? f) (make-disj (convert-negation (conj-left f))
                              (convert-negation (conj-right f)))]
        [(disj? f) (make-conj (convert-negation (disj-left f))
                              (convert-negation (disj-right f)))]
        [else (error "Zła formuła -- CONVERT-NEGATION" f)]))

(define (clause? x)
  (and (list? x)
       (andmap lit? x)))

(define (clause-empty? x)
  (and (clause? x)
       (null? x)))

(define (cnf? x)
  (and (list? x)
       (andmap clause? x)))

(define (flatmap proc seq)
  (foldl append null (map proc seq)))

(define (convert-to-cnf f)
  (define (convert f)
    (cond [(lit? f) (list (list f))]
          [(conj? f) (append (convert-to-cnf (conj-left f))
                             (convert-to-cnf (conj-right f)))]
          [(disj? f)
           (let ((clause-left (convert-to-cnf (disj-left f)))
                 (clause-right (convert-to-cnf (disj-right f))))
             (flatmap (λ (clause)
                        (map (λ (clause2)
                               (append clause2 clause)) clause-left))
                      clause-right))]))
  (convert (convert-to-nnf f)))

(define (falsifable-clause? clause)
  (cond [(clause-empty? clause) true]
        [(lit? (findf (λ (l) (equal?
                              l
                              (convert-to-nnf (make-neg (car clause)))))
                              clause)) false]
        [else (falsifable-clause? (cdr clause))]))

(define (falsifable-cnf? f)
  (define (neg-value lit)
    (if (var? lit)
        (list lit false)
        (list (neg-subf lit) true)))
  (ormap (λ (clause) (if (falsifable-clause? clause)
                         (map neg-value clause)
                         false))
         (convert-to-cnf f)))