porky11 / minlang
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
(defpackage #:minlang
(:use #:cl #:alexandria #:split-sequence)
(:export #:text
#:text*
#:parse-text
#:expand-relations))
(defpackage #:minlang-primitives
(:nicknames #:mlp)
(:export #:imply
#:intersect
#:differ
#:unite
#:equal
#:without
#:relate
#:count
))
(in-package #:minlang)
(defmacro fl (stream control-string &rest arguments)
(let ((last (car (last arguments)))
(lastexp (make-gensym "lastexp")))
`(let ((,lastexp ,last))
(format ,stream ,control-string ,@(butlast arguments) ,lastexp)
,lastexp)))
(defmacro prl (&rest objects)
(let (skip)
(if objects
`(progn
(fl t "~@{~a~^ ~}~%"
,@(mapcar (lambda (obj)
(cond ((symbolp obj)
(prog1
(if skip
obj
`(format nil "~@{~a: ~S~}," ,(symbol-name obj) ,obj))
(setf skip nil)))
((consp obj)
(prog1
(if skip
obj
`(format nil "~@{~a: ~S~}," ,(symbol-name (car obj)) ,obj))
(setf skip nil)))
((stringp obj)
(setf skip t)
obj)
(t (error "Unsupported type"))))
objects)))
`(terpri))))
(defun split-string (string split)
(dotimes (i (length string))
(when (char= (char string i) split)
(return-from split-string
(values (subseq string 0 i) (subseq string (1+ i))))))
string)
(defmacro defadder (name adder)
(with-gensyms (fname fargs body)
`(defmacro ,name (,fname ,fargs &body ,body)
`(,',adder ',,fname (lambda ,,fargs ,@,body)))))
(defparameter *rel-sets*
(make-hash-table :test #'equal))
(defun add-rel-set (name fn)
(setf (gethash name *rel-sets*) fn))
(defadder defrel add-rel-set)
(defparameter *binary* (plist-hash-table '("is" mlp:imply
"or" mlp:differ
"en" mlp:unite
"ic" mlp:equal
"se" mlp:without)
:test #'equal))
(defparameter *sets* nil)
(defparameter *modifiers*
(make-hash-table :test #'equal))
(defun add-modifier (name fn)
(setf (gethash name *modifiers*) fn))
(defadder defmodifier add-modifier)
(defun apply-modifiers (tokens &aux sen)
(do ((token (car tokens) (car rest))
(rest (cdr tokens) (cdr rest)))
((not token) sen)
(if-let ((fn (gethash token *modifiers*)))
(if rest
(push (funcall fn token (pop rest)) rest)
(error "Modifier needs token"))
(push token sen))))
(defun parse-sentence (tokens &aux sen)
(do ((token (car tokens) (car rest))
(rest (cdr tokens) (cdr rest)))
((not token) sen)
(cond ((gethash token *rel-sets*)
(multiple-value-bind (sen0 rest0) (parse-sentence rest)
(push (cons token (reverse sen0)) sen)
(setf rest rest0)))
((gethash token *binary*)
(push token sen))
((or (member token *sets* :test #'equal) nil)
(return-from parse-sentence (values (cons token sen) rest)))
(t (error "Word ~a not allowed" token)))))
(defun parse-sentence-end (tokens &aux binary)
(or
(unless (cdr tokens)
(if (consp (car tokens))
(parse-sentence-end (car tokens))
(car tokens)))
(destructuring-bind (set rel . rest) tokens
(when (gethash set *binary*)
(error "Binary operations not allowed in this context"))
(let ((relation (or (when (gethash set *rel-sets*) 'mlp:relate)
(when-let ((bin (gethash rel *binary*)))
(setf binary t)
bin)
'mlp:intersect))
(set0 (if (listp set) (parse-sentence-end set) set))
(set1 (parse-sentence-end (if binary rest (cons rel rest)))))
(list relation set0 set1)))))
(defvar *whitespaces*
'(#\Space #\Newline #\Backspace #\Tab
#\Linefeed #\Page #\Return #\Rubout))
(defun whitespacep (char)
(member char *whitespaces* :test #'char=))
#+(or)
(defun parse-text (string)
(multiple-value-bind (first rest) (split-string string #\.)
(let ((sentence (parse-sentence (delete 0 (split-sequence-if #'whitespacep first) :key #'length))))
(if (and rest (not (string= rest "")))
`(mlp:intersect ,sentence ,(parse-text rest))
sentence))))
(eval-when (:compile-toplevel :load-toplevel :execute)
(defun parse-text (string)
(multiple-value-bind (first rest) (split-string string #\.)
(let* ((seq (reverse (apply-modifiers (delete 0 (split-sequence-if #'whitespacep first) :key #'length))))
sen)
(loop
(multiple-value-bind (sen0 seq0) (parse-sentence seq)
(setf sen (if sen (cons sen (reverse sen0)) (reverse sen0))
seq seq0)
(unless seq
(return))))
(setf sen (parse-sentence-end sen))
(if (and rest (not (string= rest "")))
`(mlp:intersect ,sen ,(parse-text rest))
sen)))))
(defmacro text (string)
(parse-text string))
(defun expand-relations (list &optional type)
(if (consp list)
(if (eq (car list) 'mlp:relate)
(destructuring-bind (rel set . rest) (cdr list)
(when rest
(error "Non-binary relations not possible"))
(let ((fn (gethash rel *rel-sets*)))
(if (and (functionp fn)
(or (not type) (eq type rel)))
(expand-relations (funcall fn set) type)
(list (car list) rel (expand-relations set type)))))
(cons (car list) (mapcar (lambda (form) (expand-relations form type)) (cdr list))))
list))
(defmacro text* (string)
(expand-relations (parse-text string)))
(set-dispatch-macro-character #\# #\~ (lambda (stream char s)
(declare (ignore char s))
`(text* ,(read-line stream))))
;;;language
(defun rel-set-modifier (ctok tok)
(let ((ntok (format nil "~a ~a" ctok tok)))
(cond ((gethash tok *rel-sets*)
(setf (gethash ntok *rel-sets*) t))
((gethash tok *modifiers*)
(setf (gethash ntok *modifiers*) #'rel-set-modifier))
(t (error "The modifier ~a before ~a only applies to relative sets" ctok tok)))
ntok))
(dolist (ctok (list "t" "c"))
(add-modifier ctok #'rel-set-modifier))
(defmodifier "n" (n name)
(let ((ntok (format nil "~a ~a" n name)))
(pushnew ntok *sets* :test #'equal)
ntok))
(defmodifier "x" (n name)
(let ((ntok (format nil "~a ~a" n name)))
(pushnew ntok *sets* :test #'equal)
ntok))
(defmodifier "ov" (n name)
(declare (ignore n))
(let ((ntok (parse-integer name :radix 8)))
(pushnew ntok *sets* :test #'equal)
ntok))
(defmodifier "lo" (n name)
(let ((ntok (format nil "~a ~a" n name)))
(pushnew ntok *sets* :test #'equal)
ntok))
(defmodifier "pi" (n name)
(let ((ntok (format nil "~a ~a" n name)))
(pushnew ntok *sets* :test #'equal)
ntok))
(defmodifier "ir" (n name)
(let ((ntok (format nil "~a ~a" n name)))
ntok))
(defmodifier "ar" (n name)
(let ((ntok (format nil "~a ~a" n name)))
ntok))
(defmodifier "uo" (n name)
(declare (ignore n))
(pushnew name *sets* :test #'equal)
name)
;; relative sets
(defrel "l" (set)
set)
(defrel "no" (set)
`(mlp:differ ,set "o"))
(defstruct set-element
(super-set)
(count))
(defrel "a" (set)
`(mlp:unite (make-set-element :super-set ,set :count 1)))
(dolist (rel-set (list "te"
"pa"
"ca" "ci" "co"
"van"
"zu"
"ni" "na"
"if" "pov" "et"
"nun" "sun" "nur" "nec" "inf"
))
(add-rel-set rel-set t))
;; simple sets
(macroexpand-1 '(text* "
no uo o
uo i uo u uo ju uo jo uo vo uo juo
uo tis uo nav
uo ri uo ro uo ra uo faj
uo sej uo go
uo vat uo tat
uo par
or o
"))
;;;printing
(defmethod print-object ((element set-element) stream)
(with-slots (super-set) element
(format stream "[~a]" super-set)))
(defparameter *binary-symbol* (plist-hash-table '(mlp:imply " => "
mlp:differ " >< "
mlp:unite " | "
mlp:equal " = "
mlp:without "\\"
mlp:relate ": "
mlp:intersect " & "
mlp:count "; "
)
:test #'eq))
(defun print-binary (symbol &rest args)
(format nil (format nil "~~{~~a~~^~a~~}" symbol) args))
(defun eval-binary (name rest)
(let ((args (mapcar (lambda (arg)
(if (consp arg)
`(format nil "(~a)" ,arg)
arg))
rest)))
`(print-binary ,(gethash name *binary-symbol*) ,@args)))
(defmacro define-print-macros (&aux result)
(dolist (name (list 'mlp:imply 'mlp:differ 'mlp:unite 'mlp:equal 'mlp:relate 'mlp:intersect 'mlp:without 'mlp:count))
(push
`(defmacro ,name (&rest args)
(eval-binary ',name args))
result))
`(progn ,@result))
(eval '(define-print-macros))