-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
elsa-types.el
1216 lines (960 loc) · 43.7 KB
/
elsa-types.el
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; elsa-types.el --- Elsa types -*- lexical-binding: t -*-
;; Copyright (C) 2017 Matúš Goljer
;; Author: Matúš Goljer <matus.goljer@gmail.com>
;; Maintainer: Matúš Goljer <matus.goljer@gmail.com>
;; Created: 23rd March 2017
;; Keywords: languages, lisp
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License
;; as published by the Free Software Foundation; either version 3
;; of the License, or (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;; Code:
(require 'eieio)
(eval-and-compile (setq eieio-backward-compatibility nil))
(require 'cl-macs)
(require 'trinary)
(require 'dash)
(require 'elsa-type)
(require 'elsa-types-simple)
(require 'elsa-methods)
(require 'elsa-explainer)
(require 'elsa-structure-slot)
(defclass elsa-type--structured nil
(
;; (slots :: (hash-table symbol (class elsa-structure-slot)))
(slots
:type hash-table
:initarg :slots
:initform (make-hash-table))
(extends
:type (list-of symbol)
:initarg :extends
:initform nil))
:documentation "Structured Elsa type with named slots.
This class abstracts the management of slots on an object-like type.
These include structured types like classes or structures as well as
ad-hoc types like plists.")
(cl-defmethod elsa-get-slots ((this elsa-type--structured))
(append
(hash-table-values (oref this slots))
(->> (oref this extends)
(-keep (lambda (iface) (get iface 'elsa-interface)))
(-mapcat #'elsa-get-slots))))
(cl-defmethod elsa-get-slot ((this elsa-type--structured) (name symbol))
(or (gethash name (oref this slots))
(-some
(lambda (iface)
(when-let ((iface-type (get iface 'elsa-interface)))
(elsa-get-slot iface-type name)))
(oref this extends))))
(defun elsa-type--check-slots (this other relation explainer)
"Check that slots on THIS are compatible with slots on OTHER using RELATION.
RELATION can be any relation which compares two types and returns
bool. Most common relations are `elsa-type-accept' and
`elsa-type-assignable-p'"
(catch 'done
(mapc
(lambda (slot)
(let ((name (oref slot name)))
(unless (-if-let* ((other-slot (elsa-get-slot other name)))
(elsa-with-explainer explainer
(elsa--fmt-explain-types-of-slot-0-do-not-match name)
(funcall relation
(oref slot type)
(oref other-slot type)
explainer))
(elsa-explain explainer
elsa--fmt-explain-slot-0-not-found-on-1
name (elsa-tostring other))
nil)
(throw 'done nil))))
(elsa-get-slots this))
t))
(cl-defmethod elsa-tostring ((this elsa-type))
(elsa-type-describe this))
(cl-defmethod cl-print-object ((this elsa-type) stream)
(princ (concat "#<elsa-type " (elsa-tostring this) ">") stream))
(defclass elsa-composite-type nil ()
:abstract t
:documentation "Composite type holding other types.
This type is a wrapper around one or more other types and
modifies their behaviour.")
;; (elsa-type-accept :: (function (mixed mixed mixed) bool))
(cl-defgeneric elsa-type-accept (_this _other &optional _explainer)
"Check if THIS accepts OTHER.
This means OTHER is assignable to THIS.
If EXPLAINER is non-nil, return instead a recursive explanation why
one type can not accept another.
This method only performs soundness analysis. To account for the
special handling of `elsa-type-mixed', use
`elsa-type-assignable-p'.")
(cl-defgeneric elsa-type-is-accepted-by (this other &optional explainer)
"Check if THIS is accepted by OTHER.
This means THIS is assignable to OTHER.
This method is used to unpack composite types and perform a
\"double dispatch\" when testing one composite type accepting
another, because there is a many-to-many relationship."
(elsa-type-accept other this explainer))
;; TODO: what is the relationship of `a' and `a?'
;; (elsa-instance-of :: (function ((class elsa-type) (class elsa-type)) bool))
(defun elsa-instance-of (this other)
"Non-nil if THIS is instance of OTHER."
(object-of-class-p this (eieio-object-class other)))
;; Mixed type is special in that it is always created nullable. Mixed
;; can also serve as bool type in Emacs Lisp.
(defun elsa-type-assignable-p (this other &optional explainer)
"Check if THIS accepts OTHER.
Uses special rules for `elsa-type-mixed'.
- Mixed accepts anything except unbound.
- Mixed is accepted by anything."
(or (and (elsa-type-mixed-p this)
(not (elsa-type-unbound-p other)))
(elsa-type-mixed-p other)
(elsa-type-accept this other explainer)))
(cl-defmethod elsa-get-type ((_this null))
(elsa-type-unbound))
(cl-defmethod elsa-get-type ((this elsa-type))
this)
(cl-defmethod elsa-type-describe ((this elsa-type))
"Describe THIS type."
(symbol-name (eieio-object-class this)))
;; (elsa-type-get-args :: (function (mixed) (list mixed)))
(cl-defgeneric elsa-type-get-args (_thing)
"Get argument types of THING."
nil)
;; (elsa-type-get-return :: (and (function (nil) nil) (function ((class elsa-type)) (class elsa-type))))
(cl-defgeneric elsa-type-get-return (_this)
"Get return type of THIS type."
nil)
(cl-defmethod elsa-type-get-return ((this elsa-type))
this)
(cl-defmethod elsa-type-accept ((this elsa-type) (other elsa-type) &optional explainer)
"Test if THIS type accepts OTHER.
Accepting in this context means that OTHER can be assigned to
THIS."
(elsa-with-explainer explainer
(elsa--fmt-explain-type-0-does-not-accept-type-1
(elsa-tostring this) (elsa-tostring other))
(cond
((elsa-instance-of other this))
((elsa-const-type-p other)
(elsa-type-accept this (oref other type) explainer))
((and (elsa-type-list-p this)
(elsa-type-nil-p other)))
((elsa-type-composite-p other)
(elsa-type-is-accepted-by other this explainer)))))
;; (elsa-type-composite-p :: (function (mixed) bool))
(cl-defgeneric elsa-type-composite-p (_this)
"Determine if the type is a composite type.
Composite types have to be wrapped in parens when passed as
arguments to other constructors."
nil)
(cl-defmethod elsa-type-composite-p ((_this elsa-composite-type)) t)
;; (elsa-type-callable-p :: (function ((class elsa-type)) bool))
(cl-defgeneric elsa-type-callable-p (_this)
"Check if the type is callable.
A callable type can be called either directly as a list form or
with `funcall' or `apply' (or with other similar functions)."
nil)
;; (elsa-function-type-nth-arg :: (function (mixed (or int symbol)) mixed))
(cl-defgeneric elsa-function-type-nth-arg (_this _n)
"Return type of Nth argument or named argument.
For non-callable functions, return nil.
If N is more or equal to the arity of the function and the last
argument is variadic, return that type, otherwise return nil.
If N is not a number, it is assumed to be a slot name which we
look up in the last keys argument type."
nil)
(defclass elsa-type-empty (elsa-type elsa-simple-type eieio-singleton) ()
:documentation "Empty type. Has no domain.
This is accepted by any type and does not accept any type except
empty.")
(cl-defmethod elsa-type-describe ((_this elsa-type-empty))
"empty")
(cl-defmethod elsa-type-accept ((this elsa-type-empty) (other elsa-type) &optional explainer)
(if (elsa-type-composite-p other)
(elsa-type-is-accepted-by other this explainer)
(elsa-with-explainer explainer
(elsa--fmt-explain-type-0-only-accepts-type-1-was-2
"empty" "empty" (elsa-tostring other))
(elsa-type-empty-p other))))
(cl-defmethod elsa-type-accept ((_this elsa-type) (_this2 elsa-type-empty) &optional _explainer)
t)
(defun elsa-type-sum-all (types)
"Sum of all the TYPES.
This will sum the empty type with the first type, then the result
of this with the second type and so on until all types are
summed.
Type sum is similar to set union in that it is commutative and
associative."
(-reduce-from #'elsa-type-sum (elsa-type-empty) types))
(defun elsa-type-intersect-all (types)
"Intersect of all the TYPES.
This will intersect the mixed type with the first type, then the
result of this with the second type and so on until all types are
intersected.
Type intersect is similar to set intersection in that it is
commutative and associative."
(-reduce-from #'elsa-type-intersect (elsa-type-mixed) types))
(defclass elsa-intersection-type (elsa-type elsa-composite-type)
((types :initform nil :initarg :types))
:documentation "Intersection type.
This type is an intersection of multiple types.
It can accept any type that is all the types of the intersection.
It is accepted by any of the intersected types because it is all
of them.")
(cl-defmethod clone ((this elsa-intersection-type))
"Make a deep copy of a intersection type."
(let ((types (-map 'clone (oref this types)))
(new (cl-call-next-method)))
(oset new types types)
new))
(cl-defmethod elsa-type-accept ((this elsa-intersection-type) (other elsa-type) &optional explainer)
(elsa-with-explainer explainer
(elsa--fmt-explain-type-0-does-not-accept-type-1
(elsa-tostring this) (elsa-tostring other))
(-all? (lambda (type) (elsa-type-accept type other explainer)) (oref this types))))
(cl-defmethod elsa-type-is-accepted-by ((this elsa-intersection-type) other &optional explainer)
(-any? (lambda (type) (elsa-type-accept other type explainer)) (oref this types)))
(cl-defmethod elsa-type-describe ((this elsa-intersection-type))
(format "(and %s)" (mapconcat 'elsa-type-describe (oref this types) " ")))
(cl-defmethod elsa-type-callable-p ((this elsa-intersection-type))
"An intersection type is callable if at least one intersected type is callable."
(let ((types (oref this types)))
(cond
((= 0 (length types)) nil)
(t (-any? #'elsa-type-callable-p types)))))
(cl-defmethod elsa-type-get-return ((this elsa-intersection-type))
"Return type of an intersection is the sum of return types of intersected types.
In case of primitive types the return type is the type it self.
In case of functions, it is the return type of the function."
(-reduce 'elsa-type-sum (-map 'elsa-type-get-return (oref this types))))
(cl-defmethod elsa-function-type-nth-arg ((this elsa-intersection-type) n)
(let ((types (oref this types)))
(cond
((= 0 (length types)) nil)
(t (-when-let (summands (--keep (elsa-function-type-nth-arg it n) types))
(elsa-type-sum-all summands))))))
(defclass elsa-sum-type (elsa-type elsa-composite-type)
((types :type list
:initarg :types
:initform nil))
:documentation "Sum type.
This type is a combination of other types.
It can accept any type that is accepted by at least one of its
summands.
It is accepted by any type that is all of the summed types
because the actual type can be any of them.")
(cl-defmethod elsa-type-describe ((this elsa-sum-type))
(format "(or %s)" (mapconcat 'elsa-type-describe (oref this types) " ")))
(cl-defmethod clone ((this elsa-sum-type))
"Make a deep copy of a sum type."
(let ((types (-map 'clone (oref this types)))
(new (cl-call-next-method)))
(oset new types types)
new))
(cl-defmethod elsa-type-accept ((this elsa-sum-type) (other elsa-type) &optional explainer)
(cond
((elsa-type-composite-p other)
(elsa-type-is-accepted-by other this explainer))
((and (null (oref this types))
(elsa-type-empty-p other)))
(t
(elsa-with-explainer explainer
(elsa--fmt-explain-type-0-does-not-accept-type-1
(elsa-tostring this) (elsa-tostring other))
(-any? (lambda (ot) (elsa-type-accept ot other explainer)) (oref this types))))))
(cl-defmethod elsa-type-is-accepted-by ((this elsa-sum-type) other &optional explainer)
(-all? (lambda (type) (elsa-type-accept other type explainer)) (oref this types)))
(cl-defmethod elsa-type-callable-p ((this elsa-sum-type))
"A sum type is callable only if all the summands are callable."
(let ((types (oref this types)))
(cond
((= 0 (length types)) nil)
(t (-all? #'elsa-type-callable-p types)))))
(cl-defmethod elsa-function-type-nth-arg ((this elsa-sum-type) n)
(let ((types (oref this types)))
(cond
((= 0 (length types)) nil)
(t (-when-let (summands (--keep (elsa-function-type-nth-arg it n) types))
(elsa-type-intersect-all summands))))))
;; TODO: is this true? It seems that return type of intersection of
;; functions is also a sum of their return types... and this kind of
;; breaks the expected symmetry.
(cl-defmethod elsa-type-get-return ((this elsa-sum-type))
"Return type of a sum is the sum of return types of its types.
In case of primitive types the return type is the type it self.
In case of functions, it is the return type of the function."
(-reduce 'elsa-type-sum (-map 'elsa-type-get-return (oref this types))))
(defclass elsa-diff-type (elsa-type elsa-composite-type)
((positive :initform (progn (elsa-type-mixed)) :initarg :positive)
(negative :initform (progn (elsa-type-empty)) :initarg :negative))
:documentation "Diff type.
This type is a combination of positive and negative types. It
can accept any type that is accepted by at least one positive
type and none of the negative types.")
(cl-defmethod clone ((this elsa-diff-type))
"Make a deep copy of a diff type."
(let ((positive (clone (oref this positive)))
(negative (clone (oref this negative)))
(new (elsa-diff-type)))
(oset new positive positive)
(oset new negative negative)
new))
(cl-defmethod elsa-type-accept ((this elsa-diff-type) (other elsa-type) &optional explainer)
(elsa-with-explainer explainer
(elsa--fmt-explain-type-0-does-not-accept-type-1
(elsa-tostring this) (elsa-tostring other))
(and (elsa-type-accept (oref this positive) other explainer)
(elsa-with-explainer explainer
("Intersection of subtracted type `%s' and tested type `%s' is not empty"
(elsa-tostring (oref this negative)) (elsa-tostring other))
(elsa-type-empty-p
(elsa-type-intersect (oref this negative) other))))))
(cl-defmethod elsa-type-is-accepted-by ((this elsa-diff-type) other &optional explainer)
(elsa-type-accept other (oref this positive) explainer))
(cl-defmethod elsa-type-describe ((this elsa-diff-type))
(if (oref this negative)
(format "(diff %s %s)"
(elsa-type-describe (oref this positive))
(elsa-type-describe (oref this negative)))
(elsa-type-describe (oref this positive))))
(defclass elsa-type-symbol (elsa-type elsa-simple-type eieio-singleton) ()
:documentation "Quoted symbol")
(cl-defmethod elsa-type-describe ((_this elsa-type-symbol))
"symbol")
(defclass elsa-type-bool (elsa-type elsa-type-symbol)
((predicates
:initarg :predicates
:documentation "Type to which the function's argument will be narrowed.
Any time a function is called with some variable, Elsa will narrow
that variable to this specific type if the original type is
compatible.")))
(defclass elsa-type-t (elsa-type-bool) ())
(cl-defmethod elsa-type-describe ((_this elsa-type-t))
"t")
(defclass elsa-type-nil (elsa-type-bool) ())
(cl-defmethod elsa-type-describe ((_this elsa-type-nil))
"nil")
(cl-defmethod elsa-type-describe ((this elsa-type-bool))
(if (slot-boundp this 'predicates)
(format "(is %s)" (elsa-type-describe (oref this predicates)))
"bool"))
(defun elsa-type-is-type-predicate-p (this)
"Return non-nil if THIS is a type predicate.
A type predicate is always of type bool and additionally narrows
the type of its argument to the predicated type."
(and (elsa-type-bool-p this)
(slot-boundp this 'predicates)))
(defclass elsa-type-sequence (elsa-type) ())
(cl-defmethod elsa-type-describe ((_this elsa-type-sequence))
"sequence")
(cl-defmethod elsa-type-get-item-type ((_this elsa-type))
"Get the type of items of a sequence type."
nil)
(defclass elsa-type-string (elsa-type-sequence elsa-simple-type eieio-singleton) ())
(cl-defmethod elsa-type-get-item-type ((_this elsa-type-string))
"Get the type of items of a sequence type."
(elsa-type-int))
(defclass elsa-type-short-string (elsa-type-string) ())
(cl-defmethod elsa-type-describe ((_this elsa-type-string))
"string")
(defclass elsa-type-buffer (elsa-type elsa-simple-type eieio-singleton) ())
(cl-defmethod elsa-type-describe ((_this elsa-type-buffer))
"buffer")
(defclass elsa-type-frame (elsa-type elsa-simple-type eieio-singleton) ())
(cl-defmethod elsa-type-describe ((_this elsa-type-frame))
"frame")
(defclass elsa-type-number (elsa-type elsa-simple-type eieio-singleton)
()
:documentation "Type of any number.
Numbers can be integers or floats. Even though mathematically
integers are a subset of floats, Elsa considers them two separate
non-overlapping sets.
The type number is then a set-theoretic union of these.
For convenience, float type accepts int type because that is how Emacs
operates. This breaks the assumption that acceptance corresponds to
the set-theoretic inclusion of values, because int values are not part
of the float type domain.
The accept rules are as follows:
number float int
number ✓ ✓ ✓
float x ✓ ✓
int x x ✓
The type sum (row + column) resolution is as follows:
number float int
number number number number
float number float number
int number number int
The type intersection (and row column) resolution is as follows:
number float int
number number float int
float float float empty
int int empty int
The type diff (row - column) resolution is as follows:
number float int
number empty int float
float empty empty float
int empty int empty
")
(cl-defmethod elsa-type-describe ((_this elsa-type-number))
"number")
(defclass elsa-type-float (elsa-type-number)
()
:documentation "Floating point number.
See `elsa-type-number' for explanation of relationship of types int,
float and number.")
(cl-defmethod elsa-type-describe ((_this elsa-type-float))
"float")
(defclass elsa-type-int (elsa-type-float)
()
:documentation "Integer number.
See `elsa-type-number' for explanation of relationship of types int,
float and number.")
(cl-defmethod elsa-type-describe ((_this elsa-type-int))
"int")
(defclass elsa-type-marker (elsa-type elsa-simple-type eieio-singleton) ())
(cl-defmethod elsa-type-describe ((_this elsa-type-marker))
"marker")
(defclass elsa-type-keyword (elsa-type-symbol) ())
(cl-defmethod elsa-type-describe ((_this elsa-type-keyword))
"keyword")
(defclass elsa-type-cons (elsa-type)
((car-type :type elsa-type :initarg :car-type
:initform (progn (elsa-type-mixed)))
(cdr-type :type elsa-type :initarg :cdr-type
:initform (progn (elsa-type-mixed)))))
(cl-defmethod clone ((this elsa-type-cons))
"Make a deep copy of a cons type."
(let ((car-type (clone (oref this car-type)))
(cdr-type (clone (oref this cdr-type)))
(new (cl-call-next-method)))
(oset new car-type car-type)
(oset new cdr-type cdr-type)
new))
(cl-defmethod elsa-type-accept ((this elsa-type-cons) (other elsa-type-cons) &optional explainer)
"A cons type accepts another cons type covariantly.
That means that iff both arguments of this are supertypes of
other, then this is a supertype of other."
(elsa-with-explainer explainer
(elsa--fmt-explain-type-0-does-not-accept-type-1
(elsa-tostring this) (elsa-tostring other))
(and (elsa-with-explainer explainer
("Car of `%s' does not accept `%s'"
(elsa-tostring this) (elsa-tostring other))
(elsa-type-accept (oref this car-type) (oref other car-type) explainer))
(elsa-with-explainer explainer
("Cdr of `%s' does not accept `%s'"
(elsa-tostring this) (elsa-tostring other))
(elsa-type-accept (oref this cdr-type) (oref other cdr-type) explainer)))))
(cl-defmethod elsa-type-is-accepted-by ((_this elsa-type-cons) (_other elsa-type-empty) &optional _explainer)
"A cons type is not accepted by empty."
nil)
(cl-defmethod elsa-type-describe ((this elsa-type-cons))
(format "(cons %s %s)"
(elsa-type-describe (oref this car-type))
(elsa-type-describe (oref this cdr-type))))
(defclass elsa-type-tuple (elsa-type)
((types :type list :initarg :types)))
(cl-defmethod clone ((this elsa-type-tuple))
"Make a deep copy of a cons type."
(let ((types (-map 'clone (oref this types)))
(new (cl-call-next-method)))
(oset new types types)
new))
(cl-defmethod elsa-type-accept ((this elsa-type-tuple) (other elsa-type-tuple) &optional explainer)
(elsa-with-explainer explainer
(elsa--fmt-explain-type-0-does-not-accept-type-1
(elsa-tostring this) (elsa-tostring other))
(catch 'ok
;; types are accepted co-variantly
(let ((this-types (oref this types))
(other-types (oref other types))
(i 0))
(unless (= (length this-types) (length other-types))
(elsa-explain explainer
"Different number of elements in tuple, this has %d, other has %d"
(length this-types) (length other-types))
(throw 'ok nil))
(cl-mapc
(lambda (this-type other-type)
(cl-incf i)
(unless (elsa-with-explainer explainer
("Elements %d. is not assignable" i)
(elsa-type-accept this-type other-type explainer))
(throw 'ok nil)))
this-types other-types))
t)))
(cl-defmethod elsa-type-describe ((this elsa-type-tuple))
(format "(%s)" (mapconcat 'elsa-type-describe (oref this types) " ")))
(defclass elsa-type-list (elsa-type-cons elsa-type-sequence)
((item-type :type elsa-type
:initarg :item-type
:initform (progn (elsa-type-mixed)))))
(cl-defmethod clone ((this elsa-type-list))
"Make a deep copy of a list type."
(let ((item-type (clone (oref this item-type)))
(new (cl-call-next-method)))
(oset new item-type item-type)
new))
(cl-defmethod elsa-type-accept ((this elsa-type-list) (other elsa-type) &optional explainer)
"A list type accepts another list type covariantly.
That means that iff the item type of this is supertype of other,
then this is a supertype of other.
Note that a list here is a homogeneous list, meaning a list of
unknown length where elements are all of the same type.
For a list of fixed length with heterogeneous types, see
`elsa-type-tuple'."
(elsa-with-explainer explainer
(elsa--fmt-explain-type-0-does-not-accept-type-1
(elsa-tostring this) (elsa-tostring other))
(cond
((elsa-type-nil-p other))
((elsa-type-tuple-p other)
;; we can accept a tuple if all of its types are accepted by this
(elsa-with-explainer explainer
("Some element of tuple does not match this list's item type")
(-all?
(lambda (ot) (elsa-type-accept (oref this item-type) ot explainer))
(oref other types))))
((elsa-type-plist-p other)
(let ((plist-type (elsa-type-empty)))
(maphash
(lambda (_key slot)
(setq plist-type
(-> plist-type
(elsa-type-sum (elsa--make-type `(const ,(oref slot name))))
(elsa-type-sum (oref slot type)))))
(oref other slots))
(elsa-with-explainer explainer
(elsa--fmt-explain-sequence-item-types-of-these-0-do-not-match "lists")
(elsa-type-accept (oref this item-type) plist-type explainer))))
((elsa-type-list-p other)
(elsa-with-explainer explainer
(elsa--fmt-explain-sequence-item-types-of-these-0-do-not-match "lists")
(elsa-type-accept (oref this item-type) (oref other item-type) explainer)))
((elsa-type-composite-p other)
(elsa-type-is-accepted-by other this explainer)))))
(cl-defmethod elsa-type-describe ((this elsa-type-list))
(format "(list %s)" (elsa-type-describe (oref this item-type))))
(cl-defmethod elsa-type-get-item-type ((this elsa-type-list))
"Get the type of items of a sequence type."
(oref this item-type))
(defclass elsa-interface (elsa-type elsa-type--structured)
((name :type symbol :initarg :name))
:documentation "Abstract description of an object-like type.
Each interface is uniquely determined by its name.
Interfaces hold slots. Each slot must have a name and a type and
other optional attributes (see `elsa-structure-slot').
Interfaces can not be instantiated by themselves, but can be
*implemented* by other structured types.
For example, you can construct a plist by either supplying the list of
names and types or by only supplying the interface name, at which
point the plist will gain all the slots defined by the interface.
The same interface can be used to define plists or classes or any
other structured type.
Structured types all inherit from `elsa-type--structured'.")
(cl-defmethod elsa-type-describe ((this elsa-interface))
(let* ((slots (oref this slots))
(keys (-sort #'string< (hash-table-keys slots))))
(format "(interface %s %s)"
(oref this name)
(mapconcat
(lambda (key)
(let ((slot (gethash key slots)))
(format "%s %s"
(symbol-name (oref slot name))
(elsa-tostring (oref slot type)))))
keys
" "))))
(defclass elsa-type-plist (elsa-type-list elsa-type--structured)
()
:documentation "Ad-hoc interface-like structure.
The plist data structure is actually a simple list with alternating
keys and values.
One key and value pair is called a slot.
An example of plist is (:one 1 :two 2). Elsa currently supports
symbols as keys.")
(defun elsa-type-make-plist-hashtable (pairs)
"Make hashtable of slots from PAIRS of keywords and types.
Each item of PAIRS is a list (KEYWORD ELSA-TYPE)"
(let ((hashtable (make-hash-table)))
(dolist (pair pairs)
(let ((keyword (car pair))
(type (cadr pair)))
(puthash keyword (elsa-structure-slot :name keyword :type type) hashtable)))
hashtable))
(defclass elsa-type-keys (elsa-type-plist) ()
"This is a type of keyword arguments and available slots.
It is the same as plist except it is only used to denote that
this plist type is supposed to be expanded into key-value pairs
rather than consumed as a single value in the argument list.")
(cl-defmethod elsa-type-accept ((this elsa-type-plist) (other elsa-type-plist) &optional explainer)
(elsa-with-explainer explainer
(elsa--fmt-explain-type-0-does-not-accept-type-1
(elsa-tostring this) (elsa-tostring other))
(elsa-type--check-slots this other #'elsa-type-accept explainer)))
(cl-defmethod elsa-type-describe ((this elsa-type-plist))
(let* ((slots (oref this slots))
(keys (-sort #'string< (hash-table-keys slots)))
(extends (-sort #'string< (oref this extends))))
(format "(%s %s%s)"
(if (elsa-type-plist-p this) "plist" "keys")
(mapconcat
(lambda (key)
(let ((slot (gethash key slots)))
(format "%s %s"
(symbol-name (oref slot name))
(elsa-tostring (oref slot type)))))
keys
" ")
(if extends
(format " &extends %s"
(mapconcat #'symbol-name extends " "))
""))))
(defclass elsa-type-vector (elsa-type-sequence)
((item-type :type elsa-type
:initarg :item-type
:initform (progn (elsa-type-mixed)))))
(cl-defmethod clone ((this elsa-type-vector))
"Make a deep copy of a vector type."
(let ((item-type (clone (oref this item-type)))
(new (cl-call-next-method)))
(oset new item-type item-type)
new))
(cl-defmethod elsa-type-accept ((this elsa-type-vector) (other elsa-type-vector) &optional explainer)
"A vector type accepts another vector type covariantly.
That means that iff the item type of this is supertype of other,
then this is a supertype of other."
(elsa-with-explainer explainer
(elsa--fmt-explain-sequence-item-types-of-these-0-do-not-match "vectors")
(elsa-type-accept (oref this item-type) (oref other item-type) explainer)))
(cl-defmethod elsa-type-describe ((this elsa-type-vector))
(format "(vector %s)" (elsa-type-describe (oref this item-type))))
(cl-defmethod elsa-type-get-item-type ((this elsa-type-vector))
"Get the type of items of a sequence type."
(oref this item-type))
(defclass elsa-variadic-type (elsa-type-list) nil)
(cl-defmethod elsa-type-describe ((this elsa-variadic-type))
(format "&rest %s" (elsa-type-describe (oref this item-type))))
(defclass elsa-function-type (elsa-type)
((args :type list :initarg :args)
(return :type elsa-type :initarg :return)))
(cl-defmethod clone ((this elsa-function-type))
"Make a deep copy of a function type."
(let ((args (-map 'clone (oref this args)))
(return (clone (oref this return)))
(new (cl-call-next-method)))
(oset new args args)
(oset new return return)
new))
(cl-defmethod elsa-type-describe ((this elsa-function-type))
(format "(function (%s) %s)"
(mapconcat 'elsa-type-describe (oref this args) " ")
(elsa-type-describe (oref this return))))
(cl-defmethod elsa-type-accept ((this elsa-function-type) (other elsa-type) &optional explainer)
(if (elsa-type-composite-p other)
(elsa-type-is-accepted-by other this explainer)
(if (elsa-function-type-p other)
;; Argument types must be contra-variant, return types must be
;; co-variant.
(elsa-with-explainer explainer
(elsa--fmt-explain-type-0-does-not-accept-type-1
(elsa-tostring this) (elsa-tostring other))
(catch 'ok
(let ((this-args (oref this args))
(other-args (oref other args))
(i 0))
(unless (= (length this-args) (length other-args))
(elsa-explain explainer "Different number of arguments")
(throw 'ok nil))
(cl-mapc
(lambda (this-arg other-arg)
(cl-incf i)
(unless (elsa-with-explainer explainer
("Argument %d not compatible" i)
(elsa-type-accept other-arg this-arg explainer))
(throw 'ok nil)))
this-args other-args)
(unless (elsa-with-explainer explainer
("Return types not compatible")
(elsa-type-accept
(elsa-type-get-return this)
(elsa-type-get-return other)
explainer))
(throw 'ok nil)))
t))
(elsa-with-explainer explainer
("Function type `%s' can only accept function types, was `%s'"
(elsa-tostring this) (elsa-tostring other))
nil))))
(cl-defmethod elsa-type-callable-p ((_this elsa-function-type)) t)
(cl-defmethod elsa-function-type-nth-arg ((this elsa-function-type) n)
(let* ((args (oref this args)))
(cond
((symbolp n)
(let ((last-type (-last-item args)))
(when (elsa-type-keys-p last-type)
(when-let ((slot (elsa-get-slot last-type n)))
(elsa-get-type slot)))))
((<= (1- (length args)) n)
(let ((last-type (-last-item args)))
(if (elsa-variadic-type-p last-type)
(oref last-type item-type)
last-type)))
(t (nth n args)))))
(cl-defmethod elsa-type-get-args ((this elsa-function-type))
"Get argument types of THIS function type."
(oref this args))
(cl-defmethod elsa-type-get-return ((this elsa-function-type))
(oref this return))
(defclass elsa-generic-type (elsa-type)
((label :type symbol :initarg :label)))
(cl-defmethod clone ((this elsa-generic-type))
"Make a deep copy of a generic type."
(let ((label (oref this label))
(new (cl-call-next-method)))
(oset new label label)
new))
(cl-defmethod elsa-type-describe ((this elsa-generic-type))
(symbol-name (oref this label)))
;; One-dimensional sparse arrays indexed by characters
(defclass elsa-type-char-table (elsa-type elsa-simple-type eieio-singleton) ())
(cl-defmethod elsa-type-describe ((_this elsa-type-char-table))
"char-table")
;; One-dimensional arrays of t or nil.
(defclass elsa-type-bool-vector (elsa-type elsa-simple-type eieio-singleton) ())
(cl-defmethod elsa-type-describe ((_this elsa-type-bool-vector))
"bool-vector")
;; Super-fast lookup tables
;; TODO: add key and value types
(defclass elsa-type-hash-table (elsa-type) ())
(cl-defmethod elsa-type-describe ((_this elsa-type-hash-table))
"hash-table")
;; Compound objects with programmer-defined types
(defclass elsa-type-record (elsa-type) ())
(cl-defmethod elsa-type-describe ((_this elsa-type-record))
"record")
;; Buffers are displayed in windows
(defclass elsa-type-window (elsa-type elsa-simple-type eieio-singleton) ())
(cl-defmethod elsa-type-describe ((_this elsa-type-window))
"window")
;; A terminal device displays frames
(defclass elsa-type-terminal (elsa-type elsa-simple-type eieio-singleton) ())
(cl-defmethod elsa-type-describe ((_this elsa-type-terminal))
"terminal")
;; Recording the way a frame is subdivided
(defclass elsa-type-window-configuration (elsa-type elsa-simple-type eieio-singleton) ())
(cl-defmethod elsa-type-describe ((_this elsa-type-window-configuration))
"window-configuration")
;; Recording the status of all frames
(defclass elsa-type-frame-configuration (elsa-type elsa-simple-type eieio-singleton) ())
(cl-defmethod elsa-type-describe ((_this elsa-type-frame-configuration))
"frame-configuration")
;; A subprocess of Emacs running on the underlying OS
(defclass elsa-type-process (elsa-type elsa-simple-type eieio-singleton) ())
(cl-defmethod elsa-type-describe ((_this elsa-type-process))
"process")
;; A thread of Emacs Lisp execution
(defclass elsa-type-thread (elsa-type elsa-simple-type eieio-singleton) ())
(cl-defmethod elsa-type-describe ((_this elsa-type-thread))
"thread")
;; An exclusive lock for thread synchronization
(defclass elsa-type-mutex (elsa-type elsa-simple-type eieio-singleton) ())
(cl-defmethod elsa-type-describe ((_this elsa-type-mutex))
"mutex")
;; Condition variable for thread synchronization
(defclass elsa-type-condition-variable (elsa-type elsa-simple-type eieio-singleton) ())
(cl-defmethod elsa-type-describe ((_this elsa-type-condition-variable))
"condition-variable")
;; Receive or send characters
(defclass elsa-type-stream (elsa-type elsa-simple-type eieio-singleton) ())
(cl-defmethod elsa-type-describe ((_this elsa-type-stream))
"stream")