Created
August 4, 2015 15:33
-
-
Save mullr/d655db18ebb7aa77a62e to your computer and use it in GitHub Desktop.
simplifier-clojure
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns clj-automated-reasoning.core-match | |
(:require [clojure.core.match :as cm])) | |
(defn simplify1 [e] | |
(cm/match [e] | |
[(['+ 0 x] :seq)] x | |
[(['+ x 0] :seq)] x | |
[(['* x 1] :seq)] x | |
[(['* 1 x] :seq)] x | |
[(['* x 0] :seq)] 0 | |
[(['* 0 x] :seq)] 0 | |
[(['+ (x :guard number?) (y :guard number?)] :seq)] (+ x y) | |
[(['* (x :guard number?) (y :guard number?)] :seq)] (* x y) | |
:else e)) | |
(defn simplify [e] | |
(simplify1 | |
(cm/match [e] | |
[(['+ x y] :seq)] (list '+ (simplify x) (simplify y)) | |
[(['* x y] :seq)] (list '* (simplify x) (simplify y)) | |
:else e))) | |
(println (simplify '(+ (* (+ 1 (* 0 x)) 3) 12))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns clj-automated-reasoning.plain) | |
(defn simplify1 [e] | |
(if-not (sequential? e) | |
e | |
(let [[operator & [op1 op2]] e] | |
(case operator | |
+ (cond | |
(= 0 op1) op2 | |
(= 0 op2) op1 | |
(and (number? op1) (number? op2)) (+ op1 op2) | |
:else e) | |
* (cond | |
(= 0 op1) 0 | |
(= 0 op2) 0 | |
(= 1 op1) op2 | |
(= 1 op2) op1 | |
(and (number? op1) (number? op2)) (+ op1 op2) | |
:else e) | |
e)))) | |
(defn simplify [e] | |
(simplify1 | |
(if-not (sequential? e) | |
e | |
(let [[operator & [op1 op2]] e] | |
(case operator | |
+ (list '+ (simplify op1) (simplify op2)) | |
* (list '* (simplify op1) (simplify op2)) | |
e))))) | |
(println (simplify '(+ (* (+ 1 (* 0 x)) 3) 12))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment