public
Created

  • Download Gist
bar.clj
Clojure
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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
(ns Archimedes.bar
(:refer-clojure :exclude [==])
(:require [clojure.core.logic :refer :all]))
 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; databaseo
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
(def types
{:long {:primitive true
:natural true
:fixed-width true}
:double {:primitive true
:floating-point true
:fixed-width true}
:Long {:boxed :long
:object true
:natural true
:fixed-width true}
:Double {:boxed :double
:object true
:floating-point true
:fixed-width true}
:float {:primitive true
:floating-point true
:fixed-width true}
:Float {:boxed :float
:object true
:floating-point true
:fixed-width true}
:int {:primitive true
:natural true
:fixed-width true}
:Integer {:boxed :int
:object true
:natural true
:fixed-width true}
:Ratio {:boxed false}
:Number {:boxed false}
:Object {:boxed false}
:BigDecimal {:boxed false
:floating-point true}
:BigInteger {:boxed false
:natural true}
:BigInt {:boxed false
:natural true}})
 
(def operations
{:add {:arity 2}
:subtract {:arity 2}
:zero? {:arity 1
:return :boolean}
:neg {:arity 1}
:divide {:arity 2}
:multiply {:arity 2}
})
 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; predicates
 
(defn binaryo [op]
(fn [s]
(let [op (get (.s s) op)
{:keys [arity]} (get operations op)]
(when (= 2 arity)
s))))
 
(defn unaryo [op]
(fn [s]
(let [op (get (.s s) op)
{:keys [arity]} (get operations op)]
(when (= 1 arity)
s))))
 
(defn returno [op]
(fn [s]
(let [op (get (.s s) op)
{:keys [return]} (get operations op)]
(when return
s))))
 
(defn not-boxedo [type]
(fn [s]
(let [type (get (.s s) type)
{:keys [boxed]} (get types type)]
(when-not boxed
s))))
 
(defn boxedo [type]
(fn [s]
(let [type (get (.s s) type)
{:keys [boxed]} (get types type)]
(when boxed
s))))
 
(defn floating-pointo [type]
(fn [s]
(let [type (get (.s s) type)
{:keys [floating-point]} (get types type)]
(when floating-point
s))))
 
(defn not-floating-pointo [type]
(fn [s]
(let [type (get (.s s) type)
{:keys [floating-point]} (get types type)]
(when-not floating-point
s))))
 
(defn naturalo [type]
(fn [s]
(let [type (get (.s s) type)
{:keys [natural]} (get types type)]
(when natural
s))))
 
(defn not-naturalo [type]
(fn [s]
(let [type (get (.s s) type)
{:keys [natural]} (get types type)]
(when-not natural
s))))
 
(defn fixed-widtho [type]
(fn [s]
(let [type (get (.s s) type)
{:keys [fixed-width]} (get types type)]
(when fixed-width
s))))
 
(defn not-fixed-widtho [type]
(fn [s]
(let [type (get (.s s) type)
{:keys [fixed-width]} (get types type)]
(when-not fixed-width
s))))
 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
(defn sameo [arg1 arg2 return]
(conde
[(== arg1 arg2) (== arg1 :int) (== return :long)]
[(== arg1 arg2) (== arg1 :float) (== return :double)]
[(== arg1 arg2) (boxedo arg1) (not-boxedo return)]
[(== arg1 arg2) (not-boxedo arg1) (== arg1 return)]
[(!= arg1 arg2)]))
 
(defn floating-point-contaminato [arg1 arg2 return]
(conde
[(floating-pointo arg1) (floating-pointo arg2) (floating-pointo return)]
[(floating-pointo arg1) (not-floating-pointo arg2) (floating-pointo return)]
[(not-floating-pointo arg1) (floating-pointo arg2) (floating-pointo return)]
[(not-floating-pointo arg1) (not-floating-pointo arg2) (not-floating-pointo return)]))
 
(defn ratio-contaminato [arg1 arg2 return]
(conde
[(== arg1 :Ratio) (== arg2 :Ratio) (== return :Ratio)]
[(floating-pointo arg1) (== arg2 :Ratio) (floating-pointo return)]
[(== arg1 :Ratio) (floating-pointo arg2) (floating-pointo return)]
[(not-floating-pointo arg1) (== arg2 :Ratio) (== return :Ratio)]
[(== arg1 :Ratio) (not-floating-pointo arg2) (== return :Ratio)]
[(!= arg1 :Ratio) (!= arg2 :Ratio)]))
 
(defn object-contaminato [arg1 arg2 return]
(conde
[(== arg1 :Object) (== arg2 :Object) (== return :Number)]
[(!= arg1 :Object) (== arg2 :Object) (== return :Number)]
[(== arg1 :Object) (!= arg2 :Object) (== return :Number)]
[(!= arg1 :Object) (!= arg2 :Object)]))
 
(defn number-contaminato [arg1 arg2 return]
(conde
[(== arg1 :Number) (== arg2 :Number) (== return :Number)]
[(!= arg1 :Number) (== arg2 :Number) (== return :Number)]
[(== arg1 :Number) (!= arg2 :Number) (== return :Number)]
[(!= arg1 :Number) (!= arg2 :Number)]))
 
(defn bigdecimal-contaminato [arg1 arg2 return]
(conde
[(== arg1 :BigDecimal) (== arg2 :BigDecimal) (== return :BigDecimal)]
[(!= arg1 :BigDecimal) (== arg2 :BigDecimal) (== return :BigDecimal)]
[(== arg1 :BigDecimal) (!= arg2 :BigDecimal) (== return :BigDecimal)]
[(!= arg1 :BigDecimal) (!= arg2 :BigDecimal)]))
 
(defn special-diviso [op arg1 arg2 return]
(conde
[(== op :divide) (floating-pointo arg1) (floating-pointo arg2) (floating-pointo return)]
[(== op :divide) (floating-pointo arg1) (not-floating-pointo arg2) (floating-pointo return)]
[(== op :divide) (not-floating-pointo arg1) (floating-pointo arg2) (floating-pointo return)]
[(== op :divide) (not-floating-pointo arg1) (not-floating-pointo arg2) (== return :Ratio)]
[(!= op :divide)]))
 
(defn constrain-returno [return]
(fresh
[]
(!= return :float)
(!= return :int)
(!= return :Object)
(!= return :BigInteger)
(not-boxedo return)))
 
(defn setupo [q op arg1 arg2 return]
(fresh
[]
(== q [op arg1 arg2 return])
(membero op (keys operations))
(membero arg1 (keys types))
(membero arg2 (keys types))
(membero return (keys types))))
 
(defn fixed-width-cantaminato [op arg1 arg2 return]
(conde
[(!= :divide op) (fixed-widtho arg1) (fixed-widtho arg2) (fixed-widtho return)]
[(!= :divide op) (not-fixed-widtho arg1) (fixed-widtho arg2) (not-fixed-widtho return)]
[(!= :divide op) (fixed-widtho arg1) (not-fixed-widtho arg2) (not-fixed-widtho return)]
[(!= :divide op) (not-fixed-widtho arg1) (not-fixed-widtho arg2) (not-fixed-widtho return)]
[(== :divide op) (fixed-widtho arg1) (fixed-widtho arg2)
(conde
[(floating-pointo arg1) (floating-pointo arg2) (fixed-widtho return)]
[(floating-pointo arg1) (not-floating-pointo arg2) (fixed-widtho return)])]))
 
(defn f []
(run*
[q]
(fresh
[op arg1 arg2 return]
(setupo q op arg1 arg2 return)
(binaryo op)
(constrain-returno return)
(sameo arg1 arg2 return)
(floating-point-contaminato arg1 arg2 return)
(ratio-contaminato arg1 arg2 return)
(object-contaminato arg1 arg2 return)
(number-contaminato arg1 arg2 return)
(bigdecimal-contaminato arg1 arg2 return)
(special-diviso op arg1 arg2 return)
(fixed-width-cantaminato op arg1 arg2 return)
(conde
[(!= :divide op) (naturalo arg1) (naturalo arg2) (naturalo return)]
[(!= :divide op) (not-naturalo arg1) (naturalo arg2) (not-naturalo return)]
[(!= :divide op) (naturalo arg1) (not-naturalo arg2) (not-naturalo return)]
[(== :divide op)])
)))

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.