This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-04-06
Channels
- # announcements (33)
- # babashka (13)
- # babashka-sci-dev (23)
- # beginners (94)
- # calva (105)
- # cider (37)
- # circleci (1)
- # clj-kondo (45)
- # cljs-dev (59)
- # cljsrn (2)
- # clojure (145)
- # clojure-czech (1)
- # clojure-europe (19)
- # clojure-nl (18)
- # clojure-norway (13)
- # clojure-portugal (1)
- # clojure-uk (5)
- # clojurescript (19)
- # community-development (2)
- # conjure (3)
- # copenhagen-clojurians (1)
- # core-async (34)
- # data-science (6)
- # datomic (25)
- # emacs (43)
- # fulcro (19)
- # graalvm (7)
- # graalvm-mobile (12)
- # graphql (10)
- # honeysql (3)
- # hyperfiddle (3)
- # improve-getting-started (2)
- # interop (20)
- # kaocha (3)
- # lsp (16)
- # meander (7)
- # off-topic (22)
- # other-languages (14)
- # portal (15)
- # releases (1)
- # rewrite-clj (2)
- # ring (1)
- # shadow-cljs (119)
- # spacemacs (19)
- # sql (65)
- # testing (4)
- # tools-deps (11)
- # xtdb (29)
Looking through the logs for an elegant to pull up (simplify) nested boolean logic. For example, a program like this:
(and aa (and bb cc) dd (or ee (and ff))
would simplify to:
(and aa bb cc dd (or ee ff))
(m/rewrite
program
;; single arg and/or unnest
((m/or and or) ?single-arg)
?single-arg
;; pull up nested ands, recursively
(and . (m/or (m/cata (and . !arg ...)) (m/cata !arg)) ...)
(and . !arg ...)
;; recurse
((m/cata !x) ...)
(!x ...)
;; stop
?x ?x)
Maybe something like this? ^Something like that could probably work. When I’ve made logic simplifiers in the past, I always followed a structure like this. Made it nice and explicit what the rules for simplification were imo.
(defn reduce-logic [prop]
(m/rewrite prop
(or true ?x) true
(or ?x true) true
(or false ?x) ?x
(or ?x false) ?x
?x ?x))
(defn recursive-reduce [prop]
(m/match prop
(?op ?x ?y) (reduce-logic (list ?op (recursive-reduce ?x) (recursive-reduce ?y)))
(?op ?x) (reduce-logic (list ?op (recursive-reduce ?x)))
?x ?x))
(recursive-reduce '(or true true))
(recursive-reduce '(or true x))
(recursive-reduce '(or false x))
(recursive-reduce '(or (or (or false false) x) false))
(recursive-reduce '(or (or (or true false) x) false))
I have some haskell code that uses this exact structure, just with more rules https://github.com/jimmyhmiller/PlayGround/blob/master/logic.hs
Yeah, that is always the tricky bit is not infinite looping. This structure prevents you from having to think about that. In, fact I actually have an unnecessary cata. Let me remove that
Yeah, so everything in reduce-logic does not recurse at all. It is just the basic rules for simplification.