Advertisement
DoromaAnim

saddsa

Mar 27th, 2019
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.09 KB | None | 0 0
  1. #lang racket
  2.  
  3. (define (var? t)
  4. (symbol? t))
  5.  
  6. (define (neg? t)
  7. (and (list? t)
  8. (= 2 (length t))
  9. (eq? 'neg (car t))))
  10.  
  11. (define (lit? t)
  12. (and (list? t)
  13. (= 3 (length t))
  14. (eq? `lit (car t))))
  15.  
  16. (define (lit t)
  17. (list `lit #f #t))
  18.  
  19. (define (conj? t)
  20. (and (list? t)
  21. (= 3 (length t))
  22. (eq? 'conj (car t))))
  23.  
  24. (define (disj? t)
  25. (and (list? t)
  26. (= 3 (length t))
  27. (eq? 'disj (car t))))
  28.  
  29. (define (prop? f)
  30. (or (var? f)
  31. (and (neg? f)
  32. (prop? (neg-subf f)))
  33. (and (disj? f)
  34. (prop? (disj-left f))
  35. (prop? (disj-rght f)))
  36. (and (conj? f)
  37. (prop? (conj-left f))
  38. (prop? (conj-rght f)))))
  39.  
  40. (define (neg-subf f)
  41. (second f))
  42.  
  43. (define (disj-left f)
  44. (second f))
  45.  
  46. (define (disj-rght f)
  47. (third f))
  48.  
  49. (define (conj-left f)
  50. (second f))
  51.  
  52. (define (lit-neg? f)
  53. (second f))
  54.  
  55. (define (lit-var f)
  56. (third f))
  57.  
  58. (define (conj-rght f)
  59. (third f))
  60.  
  61. (define (neg f)
  62. (list 'neg f))
  63.  
  64. (define (conj l r)
  65. (list 'conj l r))
  66.  
  67. (define (disj l r)
  68. (list 'disj l r))
  69.  
  70. (define (free-vars f)
  71. (define (find-list el xs)
  72. (if (null? xs)
  73. #false
  74. (if (eq? (car xs) el)
  75. #true
  76. (find-list el (cdr xs)))))
  77. (define (polacz xs ys)
  78. (if (null? ys)
  79. xs
  80. (if (find-list (car ys) xs)
  81. (polacz xs (cdr ys))
  82. (polacz (cons (car ys) xs) (cdr ys)))))
  83. (define (find-free f)
  84. (cond [(neg? f) (find-free (neg-subf f))]
  85. [(lit? f) (lit-var f)]
  86. [(disj? f)
  87. (polacz (find-free (disj-left f)) (find-free (disj-rght f)))]
  88. [(conj? f)
  89. (polacz (find-free (conj-left f)) (find-free (conj-rght f)))]
  90. [else (list f)]))
  91. (find-free f))
  92.  
  93. (define (gen-vals xs)
  94. (if (null? xs)
  95. (list null)
  96. (let*
  97. ((vss (gen-vals (cdr xs)))
  98. (x (car xs))
  99. (vst (map (lambda (vs) (cons (list x true) vs)) vss))
  100. (vsf (map (lambda (vs) (cons (list x false) vs)) vss)))
  101. (append vst vsf))))
  102.  
  103. (define (eval-formula f ev)
  104. (define (find-list el xs)
  105. (if (null? xs)
  106. 0
  107. (if (eq? (caar xs) el)
  108. (second (car xs))
  109. (find-list el (cdr xs)))))
  110. (define (evaluate f)
  111. (cond [(var? f) (let ((res (find-list f ev)))
  112. (if (number? res)
  113. (error "NIE MA")
  114. res))]
  115. [(neg? f) (if (lit? (neg-subf f))
  116. (xor (lit-neg? (neg-subf f)
  117. (lit-var (neg-subf))))
  118. (not (evaluate (neg-subf f))))]
  119. [(disj? f) (or
  120. (evaluate (disj-left f))
  121. (evaluate (disj-rght f)))]
  122. [(conj? f) (and
  123. (evaluate (conj-left f))
  124. (evaluate (conj-rght f)))]))
  125. (evaluate f))
  126.  
  127. (define (falsifable-eval? f)
  128. (define (iter vals)
  129. (if (null? vals)
  130. false
  131. (if (eval-formula f (car vals))
  132. (iter (cdr vals))
  133. (car vals))))
  134. (iter (gen-vals (free-vars f))))
  135.  
  136. ;;(free-vars (neg (disj (conj `a `b) `c)))
  137. ;;(gen-vals (free-vars (neg (disj (conj `a `b) `c))))
  138. ;;(eval-formula (neg (disj (conj `a `b) `c)) `((c #f) (b #f) (a #t)))
  139. ;;(falsifable-eval? (neg (disj (conj `a `b) `c)))
  140. ;;(falsifable-eval? (conj `p (neg `p)))
  141. ;;(falsifable-eval? (disj `p (neg `p)))
  142.  
  143. (define (nnf? f)
  144. (cond [(lit? f) #true]
  145. [(neg? f) (if (lit? (neg-subf f))
  146. #true
  147. #false)]
  148. [(disj? f) (and
  149. (nnf? (disj-left f))
  150. (nnf? (disj-rght f)))]
  151. [(conj? f) (and
  152. (nnf? (conj-left f))
  153. (nnf? (conj-rght f)))]))
  154.  
  155. ;;(nnf? (disj (conj (lit `a) (lit `b)) (lit `c)))
  156.  
  157. (free-vars (neg (disj (conj (list `lit #t `a) `b) `c)))
  158. ;;(falsifable-eval? (neg (disj (conj (list `lit #t `a) `b) `c)))
  159.  
  160.  
  161. (define (convert-to-cnf f)
  162. ())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement