-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.lisp
206 lines (151 loc) · 8.19 KB
/
types.lisp
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
;; Copyright (c) 2022, "the Phoeron" Colin J.E. Lupton <[email protected]>
;; Released under the MIT License. See baphomet/LICENSE for more information.
(in-package :baphomet)
;;;; === [[ :: [function] SAFE-ENDP :: ]] ================================================================================================================= ;;;;
(declaim (inline safe-endp))
(defun safe-endp (x)
(declare (optimize safety))
(endp x))
;;;; === [[ :: [function] ENSURE-SYMBOL :: ]] ============================================================================================================ ;;;;
;; Liberated from Alexandria
(declaim (inline ensure-symbol))
(defun ensure-symbol (name &optional (package *package*))
"=> PACKAGE-QUALIFIED-SYMBOL, EXTERNAL-VISIBILITY-TYPE
Returns a symbol with name designated by NAME, accessible in package
designated by PACKAGE. If symbol is not already accessible in PACKAGE, it is
interned there. Returns a secondary value reflecting the status of the symbol
in the package, which matches the secondary return value of INTERN.
Example:
```lisp
(ensure-symbol :cons :cl) => cl:cons, :external
```
"
(intern (string name) package))
;;;; === [[ :: [function] ALIST-PLIST :: ]] =============================================================================================================== ;;;;
;; Liberated from Alexandria
(defun alist-plist (alist)
"Returns a property list containing the same keys and values as the
association list ALIST in the same order."
(let (plist)
(dolist (pair alist)
(push (car pair) plist)
(push (cdr pair) plist))
(nreverse plist)))
;;;; === [[ :: [function] PLIST-ALIST :: ]] =============================================================================================================== ;;;;
;; Liberated from Alexandria
(defun plist-alist (plist)
"Returns an association list containing the same keys and values as the
property list PLIST in the same order."
(let (alist)
(do ((tail plist (cddr tail)))
((safe-endp tail) (nreverse alist))
(push (cons (car tail) (cadr tail)) alist))))
;;;; === [[ :: [function] HASH-TABLE-ALIST :: ]] ========================================================================================================== ;;;;
;; Liberated from Alexandria
(defun hash-table-alist (table)
"Returns an association list containing the keys and values of hash table
TABLE."
(let ((alist nil))
(maphash (lambda (k v)
(push (cons k v) alist))
table)
alist))
;;;; === [[ :: [function] HASH-TABLE-PLIST :: ]] ========================================================================================================== ;;;;
;; Liberated from Alexandria
(defun hash-table-plist (table)
"Returns a property list containing the keys and values of hash table
TABLE."
(let ((plist nil))
(maphash (lambda (k v)
(setf plist (list* k v plist)))
table)
plist))
;;;; === [[ :: [derived-type] STRING-DESIGNATOR :: ]] ===================================================================================================== ;;;;
#+lispworks
(deftype type-designator ()
'(satisfies system::valid-type-specifier))
#-lispworks
(deftype type-designator ()
`(satisfies type-specifier-p))
;;;; === [[ :: [derived-type] STRING-DESIGNATOR :: ]] ===================================================================================================== ;;;;
(deftype string-designator ()
'(or string symbol character null))
(defun string-designator-p (thing)
(typep thing 'string-designator))
;;;; === [[ :: [derived-type] PROPER-LIST :: ]] =========================================================================================================== ;;;;
(deftype proper-list (&optional (element-type '*))
`(and list (cons ,element-type *)))
(defun proper-list-p (thing &optional (element-type '*))
(typep thing `(and list (proper-list ,element-type))))
;;;; === [[ :: [derived-type] PROPERTY-LIST :: ]] ========================================================================================================= ;;;;
(deftype property-list (&optional (element-type '*))
`(and list (cons keyword (cons ,element-type *))))
(defun property-list-p (thing)
(typep thing 'property-list))
;;;; === [[ :: [derived-type] ASSOCIATION-LIST :: ]] ====================================================================================================== ;;;;
(deftype association-list (&optional (key-type 'string-designator) (element-type '*))
`(and list (cons (cons ,key-type ,element-type) *)))
(defun association-list-p (thing &optional (key-type 'string-designator) (element-type '*))
(typep thing `(association-list ,@key-type ,@element-type)))
;;;; === [[ :: [parametric-type] EVAL-OP :: ]] ================================================================================================ ;;;;
;; PS: this is really a sequent. Shhh don't tell anyone!
(deftype eval-op (typed-lambda-list consequence)
`(function ,typed-lambda-list ,consequence))
(defmacro eval-op (op type-name typed-lambda-list consequence)
`(declaim (ftype (,op ,typed-lambda-list ,consequence) ,type-name)))
;; left-fold: reduce form, left-to-right
(deftype -> (typed-lambda-list reduction-value)
`(eval-op ,typed-lambda-list ,reduction-value))
(defmacro -> (type-name typed-lambda-list reduction-value)
`(eval-op -> ,type-name ,typed-lambda-list ,reduction-value))
;; right-fold: reduce form, right-to-left
(deftype <- (typed-lambda-list reduction-value)
`(eval-op ,typed-lambda-list ,reduction-value))
(defmacro <- (type-name typed-lambda-list reduction-value)
`(eval-op <- ,type-name ,typed-lambda-list ,reduction-value))
;; expand form in-place destructively
(deftype >> (typed-lambda-list expansion-value)
`(eval-op ,typed-lambda-list ,expansion-value))
(defmacro >> (type-name typed-lambda-list expansion-value)
`(eval-op >> ,type-name ,typed-lambda-list ,expansion-value))
;; evaluate in lexical environment, then return to caller
(deftype => (typed-lambda-list return-value)
`(eval-op ,typed-lambda-list ,return-value))
(defmacro => (type-name typed-lambda-list return-value)
`(eval-op => ,type-name ,typed-lambda-list ,return-value))
;; serialized evaluation in dynamic environment, typically due to structure or for side-effects
(deftype *> (typed-lambda-list implicit-return-value)
`(eval-op ,typed-lambda-list ,implicit-return-value))
(defmacro *> (type-name typed-lambda-list implicit-return-value)
`(eval-op *> ,type-name ,typed-lambda-list ,implicit-return-value))
;; dynamic closure
(deftype <*> (bindings captured-environment)
`(eval-op ,bindings ,captured-environment))
(defmacro <*> (type-name bindings captured-environment)
`(eval-op <*> ,type-name ,bindings ,captured-environment))
;;;; === [[ :: [struct] MODULE :: ]] ====================================================================================================================== ;;;;
(defstruct (module)
(unit nil :type function)
(symspace nil :type hash-table))
;;;; === [[ :: [struct] SIGNATURE :: ]] =================================================================================================================== ;;;;
(defstruct (signature (:type list) :named (:conc-name signature-) (:copier nil)
(:predicate signature-p)
(:constructor signature (eval-op name typed-lambda-list return-type)))
(eval-op nil :type symbol :read-only t)
(name nil :type string-designator :read-only t)
(typed-lambda-list nil :type proper-list :read-only t)
(return-type nil :type type-designator :read-only t))
(deftype signature ()
'(or list null))
;;;; === [[ :: [struct] INTERFACE :: ]] =================================================================================================================== ;;;;
(defstruct (interface (:type list) :named (:conc-name i-) (:copier nil)
(:constructor interface (name module signature)))
(name nil :type string)
(module nil :type module)
(signature nil :type signature)
(implementations nil :type (or hash-table null)))
;;;; === [[ :: [struct] IMPLEMENTATION :: ]] ============================================================================================================== ;;;;
(defstruct (implementation)
(assertions)
(interface)
(machine-code))