blob: bb22d343f53c863f973e70f9d9b5776e5e533770 (
plain)
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
|
(ns lux.type
(:refer-clojure :exclude [deref apply merge])
(:require [clojure.core.match :refer [match]]
[lux.util :as &util :refer [exec return* return fail fail*
repeat-m try-m try-all-m map-m
sequence-m
apply-m assert!]]))
;; [Util]
(def ^:private success (return nil))
(defn ^:private deref [id]
(fn [state]
(if-let [top+bottom (get-in state [::&util/types ::mappings id])]
[::&util/ok [state top+bottom]]
[::&util/failure (str "Unknown type-var: " id)])))
(defn ^:private update [id top bottom]
(fn [state]
(if-let [top+bottom (get-in state [::&util/types ::mappings id])]
[::&util/ok [(assoc-in state [::&util/types ::mappings id] [top bottom]) nil]]
[::&util/failure (str "Unknown type-var: " id)])))
;; [Interface]
(def fresh-var
(fn [state]
(let [id (::counter state)]
[::&util/ok [(-> state
(update-in [::counter] inc)
(assoc-in [::mappings id] [::any ::nothing]))
[::var id]]])))
(def fresh-function
(exec [=arg fresh-var
=return fresh-var]
(return [::function =arg =return])))
;; (defn solve [expected actual]
;; ;; (prn 'solve expected actual)
;; (match [expected actual]
;; [::any _]
;; success
;; [_ ::nothing]
;; success
;; [_ [::var ?id]]
;; (exec [[=top =bottom] (deref ?id)]
;; (try-all-m [(exec [_ (solve expected =top)]
;; success)
;; (exec [_ (solve =top expected)
;; _ (solve expected =bottom)
;; _ (update ?id expected =bottom)]
;; success)]))
;; [[::var ?id] _]
;; (exec [[=top =bottom] (deref ?id)]
;; (try-all-m [(exec [_ (solve =bottom actual)]
;; success)
;; (exec [_ (solve actual =bottom)
;; _ (solve =top actual)
;; _ (update ?id =top actual)]
;; success)]))
;; ;; [[::primitive ?prim] _]
;; ;; (let [as-obj (case ?prim
;; ;; "boolean" [:lang.type/object "java.lang.Boolean" []]
;; ;; "int" [:lang.type/object "java.lang.Integer" []]
;; ;; "long" [:lang.type/object "java.lang.Long" []]
;; ;; "char" [:lang.type/object "java.lang.Character" []]
;; ;; "float" [:lang.type/object "java.lang.Float" []]
;; ;; "double" [:lang.type/object "java.lang.Double" []])]
;; ;; (solve as-obj actual))
;; [[::primitive ?e-prim] [::primitive ?a-prim]]
;; (if (= ?e-prim ?a-prim)
;; success
;; (fail (str "Can't solve types: " (pr-str expected actual))))
;; [[::object ?eclass []] [::object ?aclass []]]
;; (if (.isAssignableFrom (Class/forName ?eclass) (Class/forName ?aclass))
;; success
;; (fail (str "Can't solve types: " (pr-str expected actual))))
;; [_ _]
;; (fail (str "Can't solve types: " (pr-str expected actual)))
;; ))
;; (defn pick-matches [methods args]
;; (if (empty? methods)
;; (fail "No matches.")
;; (try-all-m [(match (-> methods first second)
;; [::function ?args ?return]
;; (exec [_ (assert! (= (count ?args) (count args)) "Args-size doesn't match.")
;; _ (map-m (fn [[e a]] (solve e a)) (map vector ?args args))]
;; (return (first methods))))
;; (pick-matches (rest methods) args)])))
(defn clean [type]
(match type
[::var ?id]
(exec [[=top =bottom] (deref ?id)]
(clean =top))
[::function ?args ?return]
(exec [=args (map-m clean ?args)
=return (clean ?return)]
(return [::function =args =return]))
;; ::any
;; (return [::object "java.lang.Object" []])
;; _
;; (return type)
))
;; Java Reflection
(def success (return nil))
(defn solve [needed given]
(match [needed given]
[[::Any] _]
success
[_ [::Nothing]]
success
[[::Data n!name] [::Data g!name]]
(cond (or (= n!name g!name)
(.isAssignableFrom (Class/forName n!name) (Class/forName g!name)))
success
:else
(fail (str "not (" given " <= " needed ")")))
[[::Tuple n!elems] [::Tuple g!elems]]
(exec [_ (assert! (= (count n!elems) (count g!elems))
"Tuples must have matching element sizes.")
_ (map-m (fn [n g] (solve n g))
(map vector n!elems g!elems))]
success)
[[::Variant n!cases] [::Variant g!cases]]
(exec [_ (assert! (every? (partial contains? n!cases) (keys g!cases))
"The given variant contains unhandled cases.")
_ (map-m (fn [label]
(solve (get n!cases label) (get g!cases label)))
(keys g!cases))]
success)
[[::Record n!fields] [::Record g!fields]]
(exec [_ (assert! (every? (partial contains? g!fields) (keys n!fields))
"The given record lacks necessary fields.")
_ (map-m (fn [label]
(solve (get n!fields label) (get g!fields label)))
(keys n!fields))]
success)
[[::Lambda n!input n!output] [::Lambda g!input g!output]]
(exec [_ (solve g!input n!input)]
(solve n!output g!output))
))
(let [&& #(and %1 %2)]
(defn merge [x y]
(match [x y]
[_ [::Nothing]]
(return x)
[[::Nothing] _]
(return y)
[[::Variant x!cases] [::Variant y!cases]]
(if (and (reduce && true
(for [[xslot xtype] (keys x!cases)]
(if-let [ytype (get y!cases xslot)]
(= xtype ytype)
true)))
(reduce && true
(for [[yslot ytype] (keys y!cases)]
(if-let [xtype (get x!cases yslot)]
(= xtype ytype)
true))))
(return [::Variant (clojure.core/merge x!cases y!cases)])
(fail (str "Incompatible variants: " (pr-str x) " and " (pr-str y))))
[[::Record x!fields] [::Record y!fields]]
(if (and (= (keys x!fields) (keys y!fields))
(->> (keys x!fields)
(map #(= (get x!fields %) (get y!fields %)))
(reduce && true)))
(return x)
(fail (str "Incompatible records: " (pr-str x) " and " (pr-str y))))
:else
(fail (str "Can't merge types: " (pr-str x) " and " (pr-str y))))))
(def +dont-care-type+ [::Any])
(comment
;; Types
[::Any]
[::Nothing]
[::Tuple (list)]
[::Lambda input output]
[::Variant {}]
[::Record {}]
[::Data name]
[::All self {} arg body]
[::Exists evar body]
[::Bound name]
;; ???
[::Alias name args type]
[::Var id]
;; (deftype #rec Type
;; (| #Any
;; #Nothing
;; (#Tuple (List Type))
;; (#Lambda Type Type)
;; (#Variant (List [Text Type]))
;; (#Record (List [Text Type]))
;; (#Data Text)))
;; (deftype #rec Kind
;; (| (#Type Type)
;; (#All Text (List [Text Kind]) Text Kind)))
;; (deftype (Higher lower)
;; (| (#Lower lower)
;; (#Apply (Higher lower) (Higher lower))
;; (#All Text (List [Text lower]) Text (Higher lower))
;; (#Exists (List [Text lower]) Text (Higher lower))))
;; (deftype Kind (Higher Type))
;; (deftype Sort (Higher Kind))
;; (deftype HList (| (#Cons (Exists x x) HList)
;; #Nil))
;; (def success (return nil))
;; (defn apply [type-lambda input]
;; (match type-lambda
;; [::All ?self ?env ?arg ?body]
;; (let [env* (-> ?env
;; (assoc ?arg input)
;; (assoc ?self type-lambda))]
;; (match ?body
;; [::All ?sub-self _ ?sub-arg ?sub-body]
;; [::All ?sub-self env* ?sub-arg ?sub-body]
;; _
;; (beta-reduce env* ?body)))))
;; (defn solve [needed given]
;; (match [needed given]
;; [[::Any] _]
;; success
;; [_ [::Nothing]]
;; success
;; [[::Tuple n!elems] [::Tuple g!elems]]
;; (exec [_ (assert! (= (count n!elems) (count g!elems))
;; "Tuples must have matching element sizes.")
;; _ (map-m (fn [[n g]] (solve n g))
;; (map vector n!elems g!elems))]
;; success)
;; [[::Variant n!cases] [::Variant g!cases]]
;; (exec [_ (assert! (every? (partial contains? n!cases) (keys g!cases))
;; "The given variant contains unhandled cases.")
;; _ (map-m (fn [label]
;; (solve (get n!cases label) (get g!cases label)))
;; (keys g!cases))]
;; success)
;; [[::Record n!fields] [::Record g!fields]]
;; (exec [_ (assert! (every? (partial contains? g!fields) (keys n!fields))
;; "The given record lacks necessary fields.")
;; _ (map-m (fn [label]
;; (solve (get n!fields label) (get g!fields label)))
;; (keys n!fields))]
;; success)
;; [[::Lambda n!input n!output] [::Lambda g!input g!output]]
;; (exec [_ (solve g!input n!input)
;; _ (solve n!output g!output)]
;; success)
;; ))
;; (deftype (List x)
;; (| (#Cons x (List x))
;; #Nil))
;; (deftype List
;; (All List [x]
;; (| (#Cons x (List x))
;; #Nil)))
;; (def List
;; [::All "List" {} x
;; [::Variant {"Cons" [::Tuple (list [::Local x] [::Apply {} [::Local "List"] [::Local x]])]
;; "Nil" [::Tuple (list)]}]])
;; (deftype User
;; {#name Text
;; #email Text
;; #password Text
;; #joined Time
;; #last-login Time})
;; (deftype (Pair x y)
;; [x y])
;; (deftype (State s a)
;; (-> s [a s]))
;; (: + (-> Int Int Int))
;; (def (+ x y)
;; (jvm:ladd x y))
)
|