/
transformers.clj
136 lines (114 loc) · 3.92 KB
/
transformers.clj
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
(ns clojure.core.contracts.impl.transformers
(:require [clojure.core.contracts.impl.funcify :as funcification])
(:require [clojure.core.unify :as unify]
[clojure.core.contracts.impl.utils :as utils]))
(defn- divide-pre-post
"'[odd? pos? => int?]
=>
{:pre (odd? pos?) :post (int?)}
"
[cnstr]
(if (vector? cnstr)
(let [[L M R] (partition-by #{'=>} cnstr)]
{:pre (when (not= L '(=>)) L)
:post (if (= L '(=>)) M R)})
cnstr))
;; HoC support
(declare build-constraints-description
build-contract-body)
(defrecord Hoc [field desc])
(defmethod funcification/funcify* Hoc [e args] e)
(def hoc? #(= Hoc (type %)))
(defn- tag-hocs
[cnstr]
(let [ret
(mapcat (fn [form]
(if (and (seq? form) (= '_ (first form)))
[(list 'fn? (second form))
(Hoc. (second form)
(apply build-constraints-description (-> form nnext vec (conj "foo"))))]
[form]))
cnstr)]
ret))
(defn- build-constraints-description
"'[n] '[odd? pos? => int?] \"foo\"
=>
[[n] {:pre [(pos? n) (int? n)], :post [(neg? %)]} \"foo\"]"
[args cnstr docstring]
(let [cnstr (vec (tag-hocs cnstr))]
[args
(->> (divide-pre-post cnstr)
(utils/manip-map (partial funcification/funcify '[%]) [:post])
(utils/manip-map (partial funcification/funcify args) [:pre]))
docstring]))
(defn- build-condition-body
[constraint-map body prefix-msg]
(unify/subst
'(try
((fn []
?CNSTR
?BODY))
(catch AssertionError ae
(throw (AssertionError. (str ?PREFIX ?MSG \newline (.getMessage ae))))))
{'?CNSTR constraint-map
'?PREFIX prefix-msg
'?BODY body}))
(comment
(build-contract-body
(build-constraints-description '[f n] '[number? (_ f [n] [odd?]) => pos?] "foo"))
(build-contract-body
(build-constraints-description '[n] '[number? => odd?] "foo"))
(prepare-args '[f b c] '{f {:desc [[n] {:pre [(odd? n)], :post []} "bar"]}})
)
(defn prepare-args [args hocs]
(let [vargs? #{'&}
has-vargs (boolean (some vargs? args))]
(with-meta
(vec
(map (fn [arg]
(if-let [hoc (get hocs arg)]
(list `partial (list* `fn (build-contract-body (:desc hoc))) arg)
arg))
(->> args (remove vargs?))))
{::vargs has-vargs})))
(defn- build-contract-body
[[args cnstr descr :as V]]
(let [vargs? #{'&}
fun-name (gensym "fun")
hocs (apply merge (map #(hash-map (:field %) %)
(filter hoc? (concat (:pre cnstr) (:post cnstr)))))
cnstr {:pre (vec (filter (complement hoc?) (:pre cnstr)))
:post (vec (filter (complement hoc?) (:post cnstr)))}
prep-args (prepare-args args hocs)
callsite (if (::vargs (meta prep-args))
(list* `apply '?F prep-args)
'(apply ?F ?ARGS))]
(unify/subst
'(?PARMS
(let [ret ?PRE-CHECK]
?POST-CHECK))
{'?ARGS prep-args
'?F fun-name
'?PARMS (vec (list* fun-name args))
'?MSG descr
'?PRE-CHECK (build-condition-body
{:pre (:pre cnstr)}
callsite
"Pre-condition failure: ")
'?POST-CHECK (build-condition-body
{:post (:post cnstr)}
'ret
"Post-condition failure: ")})))
(defn- build-contract-bodies
[constraint-descriptions]
(for [cnstr constraint-descriptions]
(build-contract-body cnstr)))
;; # Public API
(defn build-contract-fn-body
[name docstring raw-constraints]
(let [raw-cnstr (partition 2 raw-constraints)
cnstr-descrs (for [[a c] raw-cnstr]
(build-constraints-description a c docstring))] ;; needs work
(->> cnstr-descrs
build-contract-bodies
(list* `fn name))))