What is wrong? I want to run it all in just one file: I got an error: Unable to resolve symbol: bool_and in this context
(ns user_task4
(:require [clojure.string :as str])
(:use clojure.test))
(defn print-expr [expr]
(let [s0 (str expr)
s1 (str/replace s0 #":task_4/" "")
s2 (str/replace s1 #"(const (\d+))" "1")
s3 (str/replace s2 #"\(var :([a-zA-Z]+)\)" "1")]
(println s3)))
(defn in?
"true if coll contains elm"
[coll elm]
(some #(= elm %) coll))
(defn constant [num]
{:pre [(number? num)]}
(list ::const num))
(defn constant? [expr]
(= (first expr) ::const))
(defn constant-value [v]
(second v))
(defn same-constants? [v1 v2]
(and
(constant? v1)
(constant? v2)
(= (constant-value v1)
(constant-value v2))))
(defn variable [name]
{:pre [(keyword? name)]}
(list ::var name))
(defn variable? [expr]
(= (first expr) ::var))
(defn variable-name [v]
(second v))
(defn same-variables? [v1 v2]
(and
(variable? v1)
(variable? v2)
(= (variable-name v1)
(variable-name v2))))
;; порождение дизъюнкции
(defn bool_or [expr & rest]
(cons ::or (cons expr rest)))
;; проверка типа для дизъюнкции
(defn bool_or? [expr]
(= ::or (first expr)))
;; порождение конъюнкции
(defn bool_and
([rest]
(cons ::and rest))
([expr & rest]
(cons ::and (cons expr rest)))
)
;; проверка типа для конъюнкции
(defn bool_and? [expr]
(= ::and (first expr)))
;; список аргументов выражения
(defn args [expr]
(rest expr))
;; порождение обратного выражения
(defn bool_not [expr]
(list ::not expr))
(defn bool_not? [expr]
(= ::not (first expr)))
;; проверка типа для обратного выражения
(defn single_not? [expr]
(let [token2 (first (second expr))]
(and
(bool_not? expr)
(not (bool_and? (second expr)))
(not (bool_or? (second expr)))
;; порождение импликации
(defn bool_impl [v1 & v2]
(cons ::impl (cons v1 v2)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; провеки правил:
; 1. есть ли импликация
(defn bool_impl? [expr]
(= ::impl (first expr)))
; 2. есть ли отрицание скобки
(defn not-expr? [expr]
(and
(bool_not? expr)
(or
(bool_and? (second expr)) ;; (A * B) (bool_or? (second expr)) ;; (A v B)
)))
; 3. есть ли двойное отрицание
(defn not-not? [expr]
(and
(bool_not? expr)
(bool_not? (second expr))
))
; 4. дистрибутивность
(defn or-in-and? [expr]
(and
(bool_and? expr)
(> (count
(filter
#(= ::or (first %))
(args expr)
))
0
)
))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; служебные функции
(defn my-concat-and [expr & rest]
(let [exprs2 (cons expr rest)]
(if (= 1 (count exprs2))
(first exprs2)
(cons ::and exprs2))))
(defn my-concat-or [expr & rest]
(let [exprs2 (cons expr rest)]
(if (= 1 (count exprs2))
(first exprs2)
(cons ::or exprs2))))
(defn append-to-and-or [expr or_list]
(let [
t (first expr)
expr_args
(case t
::not (list expr)
::impl expr
::or (args expr)
::and (args expr)
(list expr)
)
or_list_args
(if (variable? or_list)
or_list
(args or_list)
)
result
(map
#(apply bool_and (concat expr_args (list %)))
or_list_args)
]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; список правил вывода
(declare dnf)
(def dnf-rules
(list
;; [pred_i transform_i]
; https://en.wikipedia.org/wiki/Disjunctive_normal_form
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; тестирование
(ns task-4-test
(:require [clojure.test :refer :all]))
(deftest test_private_functions
(testing "Метод нужный при реализации дистрибутивности")
(is (=
(list
(bool_and
(variable :A)
(variable :B)
(variable :C))
(bool_and
(variable :A)
(variable :B)
(variable :D)))
(append-to-and-or
(bool_and
(variable :A)
(variable :B))
(bool_or
(variable :C)
(variable :D))))))
(deftest test_no_recurion
(testing "Замена импликации на +,*,`")
(is (=
(bool_or
(bool_not (variable :A))
(variable :B))
(my-dnf
(bool_impl
(variable :A)
(variable :B)))))
(is (=
(bool_or
(variable :A)
(variable :B))
(my-dnf
(bool_impl
(bool_not (variable :A))
(variable :B)))))
;; Additional test cases go here...
(testing "Закон противоречия")
(is (=
(constant 0)
(my-dnf
(bool_and
(variable :X)
(bool_not (variable :X)))))))
(deftest test_complex_example
(testing "Составной пример с википедии")
(is (=
(bool_or
(bool_and
(variable :X)
(bool_not (variable :Y)))
(bool_and
(variable :X)
(bool_not (variable :Y))
(variable :Z)))
(my-dnf
(bool_not
(bool_or
(bool_impl
(variable :X)
(variable :Y))
(bool_not
(bool_impl
(variable :Y)
(variable :Z)))))))))
(deftest test_const
(testing "проверяем, что (1 or ... = 1)")
(is (=
(constant 1)
(my-dnf
(bool_or
(constant 1)
(bool_not
(bool_or
(bool_impl
(variable :X)
(variable :Y))
(bool_not
(bool_impl
(variable :Y)
(variable :Z)))))))))
(testing "проверяем, что (0 and ... = 0)")
(is (=
(constant 0)
(my-dnf
(bool_and
(constant 0)
(bool_not
(bool_or
(bool_impl
(variable :X)
(variable :Y))
(bool_not
(bool_impl
(variable :Y)
(variable :Z))))))))))
;; Define the run-all-tests function
(defn run-all-tests []
(run-tests 'task-4-test))
;; You can call (run-all-tests) to run all your tests.