-
Notifications
You must be signed in to change notification settings - Fork 0
/
cclp-expander.rkt
425 lines (382 loc) · 15.3 KB
/
cclp-expander.rkt
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
; MIT License
;
; Copyright (c) 2016 Vincent Nys
;
; Permission is hereby granted, free of charge, to any person obtaining a copy
; of this software and associated documentation files (the "Software"), to deal
; in the Software without restriction, including without limitation the rights
; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
; copies of the Software, and to permit persons to whom the Software is
; furnished to do so, subject to the following conditions:
;
; The above copyright notice and this permission notice shall be included in all
; copies or substantial portions of the Software.
;
; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
; SOFTWARE.
#lang br
; for abstract variables, functions, atoms,...
(require (prefix-in ad: "abstract-multi-domain.rkt"))
; because output patterns can be obtained by applying a subsitution
(require (prefix-in as: "abstract-substitution.rkt")
(only-in cclp/abstract-analysis full-ai-rule->full-evaluation))
; for rules on how to fully evaluate
(require (prefix-in fai: "fullai-domain.rkt"))
(require (only-in "syntax-utils.rkt" odd-elems-as-list))
(require (prefix-in cd: "concrete-domain.rkt"))
(require (prefix-in ck: "concrete-knowledge.rkt"))
(require (for-syntax syntax/parse))
(require "interaction.rkt")
(require racket/contract)
(require "abstract-domain-ordering.rkt")
(require "preprior-graph.rkt" (only-in graph add-directed-edge!))
(require (only-in sugar/coerce ->symbol))
(require (for-syntax (only-in racket-list-utils/utils odd-elems)))
; PUTTING THE THREE PARTS TOGETHER
; can I make this more modular?
(define-syntax (cclp-program stx)
(syntax-parse stx
; no full ai rules, no concrete constants
[(_ "{PROGRAM}" _PROGRAM-SECTION
"{QUERY}" _QUERY-SECTION)
(syntax/loc stx (cclp _PROGRAM-SECTION (list) (list) (mk-preprior-graph) _QUERY-SECTION "dummy"))]
[(_ "{PROGRAM}" _PROGRAM-SECTION
"{FULL EVALUATION}" _FULL-EVALUATION-SECTION
"{QUERY}" _QUERY-SECTION)
(syntax/loc stx (cclp _PROGRAM-SECTION _FULL-EVALUATION-SECTION (list) (mk-preprior-graph) _QUERY-SECTION "dummy"))]
[(_ "{PROGRAM}" _PROGRAM-SECTION
"{CONCRETE CONSTANTS}" _CONCRETE-CONSTANTS-SECTION
"{QUERY}" _QUERY-SECTION)
(syntax/loc stx (cclp _PROGRAM-SECTION (list) _CONCRETE-CONSTANTS-SECTION (mk-preprior-graph) _QUERY-SECTION "dummy"))]
[(_ "{PROGRAM}" _PROGRAM-SECTION
"{FULL EVALUATION}" _FULL-EVALUATION-SECTION
"{CONCRETE CONSTANTS}" _CONCRETE-CONSTANTS-SECTION
"{QUERY}" _QUERY-SECTION)
(syntax/loc stx (cclp _PROGRAM-SECTION _FULL-EVALUATION-SECTION _CONCRETE-CONSTANTS-SECTION (mk-preprior-graph) _QUERY-SECTION "dummy"))]
[(_ "{PROGRAM}" _PROGRAM-SECTION
"{FULL EVALUATION}" _FULL-EVALUATION-SECTION
"{CONCRETE CONSTANTS}" _CONCRETE-CONSTANTS-SECTION
"{PARTIAL ORDER}" _PARTIAL-ORDER-SECTION
"{QUERY}" _QUERY-SECTION)
(syntax/loc stx (cclp _PROGRAM-SECTION _FULL-EVALUATION-SECTION _CONCRETE-CONSTANTS-SECTION _PARTIAL-ORDER-SECTION _QUERY-SECTION "dummy"))]))
(provide cclp-program)
; AND THE GLUE TO GO TO TOP-LEVEL INTERACTION
(define-syntax (cclp-module-begin stx)
(syntax-parse stx
[(_ _PARSE-TREE ...)
(syntax/loc stx
(#%module-begin
(define fn (current-contract-region)) ; inside submodule this is not a string but a list!
(define initial-program-analysis
(let ([initial _PARSE-TREE ...])
(cclp->initial-analysis
(struct-copy
cclp
initial
[filename fn]
[full-ai-rules
(map
full-ai-rule->full-evaluation
(cclp-full-ai-rules initial))]))))
(provide initial-program-analysis)))]))
(provide
(rename-out
[cclp-module-begin #%module-begin]))
; PART FOR THE LOGIC PROGRAM ITSELF
(define-for-syntax (inject-rule-id-stx rule-stx id)
(syntax-parse rule-stx #:literals (rule)
[(rule arg ...)
(cons
(quasisyntax/loc rule-stx
(rule arg ... #,id))
(add1 id))]))
(require (for-syntax (only-in racket-list-utils/utils map-accumulatel)))
(define-syntax (program-section stx)
(with-syntax ([(RULE-STX ...)
(car
(map-accumulatel
inject-rule-id-stx
1
(odd-elems (cdr (syntax->list stx)))))])
(syntax/loc stx
(list RULE-STX ...))))
(provide program-section)
(define-syntax (atom stx)
(syntax-parse stx
[(_ symbol)
(syntax/loc stx (cd:atom (string->symbol (quote symbol)) '()))]
[(_ symbol "(" arg ... ")")
(syntax/loc stx (cd:atom (string->symbol (quote symbol)) (odd-elems-as-list arg ...)))]))
(provide atom)
(define-syntax (term stx)
(syntax-parse stx
[(_ VAR-OR-LIST-OR-MISC-FUNCTION)
(syntax/loc stx VAR-OR-LIST-OR-MISC-FUNCTION)]))
(provide term)
(define-syntax-rule (variable VARIABLE-NAME) (cd:variable (string->symbol (quote VARIABLE-NAME))))
(provide variable)
(define-syntax (function-term stx)
(syntax-parse stx
[(_ symbol:str)
(syntax/loc stx (cd:function (string->symbol (quote symbol)) '()))]
[(_ num-term) (syntax/loc stx num-term)] ; these are just plain numbers
[(_ symbol "(" arg ... ")")
(syntax/loc stx (cd:function (string->symbol (quote symbol)) (odd-elems-as-list arg ...)))]))
(provide function-term)
(define-syntax (lplist stx)
(syntax-parse stx
[(_ "[" "]")
(syntax/loc stx (cd:function 'nil '()))]
[(_ "[" term0 "]")
(syntax/loc stx (cd:function 'cons (list term0 (cd:function 'nil '()))))]
[(_ "[" term0 "," rest ... "]")
(syntax/loc stx (cd:function 'cons (list term0 (lplist "[" rest ... "]"))))]
[(_ "[" term0 "|" rest ... "]")
(syntax/loc stx (cd:function 'cons (list term0 rest ...)))]))
(provide lplist)
(define-syntax (rule stx)
(syntax-parse stx
[(_ atom id) (syntax/loc stx (ck:rule atom '() id))]
[(_ atom ":-" conjunction id) (syntax/loc stx (ck:rule atom conjunction id))]))
(provide rule)
(define-syntax (conjunction stx)
(syntax-parse stx
[(_ conjunct ...) (syntax/loc stx (odd-elems-as-list conjunct ...))]))
(provide conjunction)
(define-syntax (abstract-conjunction stx)
(syntax-parse stx
[(_ conjunct ...) (syntax/loc stx (odd-elems-as-list conjunct ...))]))
(provide abstract-conjunction)
; PART RELATED TO FULL EVALUATION
(define-syntax (fail stx)
(syntax-parse stx
[(_ "fail")
(syntax/loc stx #f)]))
(provide fail)
(define-for-syntax (inject-full-ai-rule air-stx id)
(syntax-parse
air-stx
[(type args ...)
(cons #`(type args ... #,id) (add1 id))]))
(define-syntax (full-evaluation-section stx)
(with-syntax
([(INJECTED-RULE ...)
(car
(map-accumulatel
inject-full-ai-rule
1
(cdr (syntax->list stx))))])
(syntax/loc stx (list INJECTED-RULE ...))))
(provide full-evaluation-section)
(define-syntax-rule (fullai-rule-with-body atom "->" subst "." idx)
(fai:full-ai-rule atom subst idx))
(provide fullai-rule-with-body)
(define-syntax-rule (fullai-rule-without-body atom "." idx)
(fai:full-ai-rule atom (list) idx))
(provide fullai-rule-without-body)
(define-syntax-rule (abstract-atom-with-args symbol "(" arg ... ")")
(ad:abstract-atom (string->symbol (quote symbol)) (odd-elems-as-list arg ...)))
(provide abstract-atom-with-args)
(define-syntax-rule (abstract-atom-without-args symbol)
(ad:abstract-atom (string->symbol (quote symbol)) (list)))
(provide abstract-atom-without-args)
(define-syntax (abstract-atom stx)
(syntax-parse stx [(_ args-or-nothing) (syntax/loc stx args-or-nothing)]))
(provide abstract-atom)
(define-syntax-rule (abstract-term specific-term) specific-term)
(provide abstract-term)
(define-syntax-rule (abstract-variable specific-var) specific-var)
(provide abstract-variable)
; M.O.: string is hier overbodig...
(define-syntax-rule (abstract-variable-a "α" index) (ad:a (quote index)))
(provide abstract-variable-a)
(define-syntax-rule (abstract-variable-g "γ" index) (ad:g (quote index)))
(provide abstract-variable-g)
(define-syntax (abstract-function-term stx)
(syntax-parse stx
[(_ symbol:str) (syntax/loc stx (ad:abstract-function (string->symbol (quote symbol)) '()))]
[(_ num-term) (syntax/loc stx num-term)]
[(_ symbol "(" arg ... ")")
(syntax/loc stx (ad:abstract-function (string->symbol (quote symbol)) (odd-elems-as-list arg ...)))]))
(provide abstract-function-term)
(define-syntax-rule (abstract-number NUMBER)
(ad:abstract-function (->symbol (quote NUMBER)) '()))
(provide abstract-number)
(define-syntax-rule (abstract-number-term TERM) TERM)
(provide abstract-number-term)
(define-syntax-rule (number-term TERM)
(cd:function (->symbol (quote TERM)) '()))
(provide number-term)
(define-syntax (abstract-lplist stx)
(syntax-parse stx
[(_ "[" "]")
(syntax/loc stx (ad:abstract-function 'nil '()))]
[(_ "[" term0 "]")
(syntax/loc stx (ad:abstract-function 'cons (list term0 (ad:abstract-function 'nil '()))))]
[(_ "[" term0 "," rest ... "]")
(syntax/loc stx (ad:abstract-function 'cons (list term0 (abstract-lplist "[" rest ... "]"))))]
[(_ "[" term0 "|" rest "]")
(syntax/loc stx (ad:abstract-function 'cons (list term0 rest)))]))
(provide abstract-lplist)
; empty substitutions make sense if we can just scratch the abstract atom
; e.g. lte(g1,g2) just disappears and does not need a substitution
(define-syntax (abstract-substitution stx)
(syntax-parse stx
[(_) (list)]
[(_ lhs0 lhs1 ...)
(syntax/loc stx (odd-elems-as-list lhs0 lhs1 ...))]))
(provide abstract-substitution)
(define-syntax-rule (abstract-substitution-pair lhs "/" rhs)
(as:abstract-equality lhs rhs))
(provide abstract-substitution-pair)
(define-syntax (concrete-constant stx)
(syntax-parse stx
[(_ _CONSTANT-SYMBOL)
(with-syntax
([CON-SYM (datum->syntax #'_CONSTANT-SYMBOL (string->symbol (syntax->datum #'_CONSTANT-SYMBOL)))])
(syntax/loc stx (cd:function (quote CON-SYM) (list))))]))
(provide concrete-constant)
(define-syntax (concrete-constants-section stx)
(syntax-parse stx
[(_ _CONCRETE-CONSTANT ...) (syntax/loc stx (list _CONCRETE-CONSTANT ...))]))
(provide concrete-constants-section)
(define-syntax (partial-order-section stx)
(syntax-parse stx
[(_ PAIR ...)
(syntax/loc stx
(let ([g (mk-preprior-graph)])
(for ([p (list PAIR ...)])
(add-directed-edge! g (car p) (cdr p)))
g))]))
(provide partial-order-section)
(define-syntax (partial-ordering-pair stx)
(syntax-parse stx
[(_ A _ B)
(syntax/loc stx
(cons A B))]))
(provide partial-ordering-pair)
(module+ test
(require rackunit)
(check-equal? (number-term 4) (cd:function (->symbol 4) '()))
(check-equal? (lplist "[" "]") (cd:function 'nil '()))
(check-equal? (abstract-variable-a "α" 1) (ad:a 1))
(check-equal? (abstract-variable-g "γ" 2) (ad:g 2))
(check-equal? (abstract-number 3) (ad:abstract-function (->symbol 3) '()))
(check-equal? (abstract-variable (abstract-variable-a "α" 1)) (ad:a 1))
(check-equal? (abstract-variable (abstract-variable-g "γ" 2)) (ad:g 2))
(check-equal? (abstract-number-term (abstract-number 3)) (ad:abstract-function (->symbol 3) '()))
(check-equal? (abstract-function-term "my-func") (ad:abstract-function 'my-func '()))
(check-equal? (abstract-atom-without-args "my-atom") (ad:abstract-atom 'my-atom '()))
(check-equal? (abstract-atom (abstract-atom-without-args "my-atom"))
(ad:abstract-atom 'my-atom '()))
(check-equal? (abstract-lplist "[" "]") (ad:abstract-function 'nil '()))
(check-equal? (abstract-lplist
"["
(abstract-variable (abstract-variable-g "γ" 2))
","
(abstract-variable (abstract-variable-a "α" 1))
"]")
(ad:abstract-function
'cons
(list (ad:g 2)
(ad:abstract-function
'cons
(list (ad:a 1)
(ad:abstract-function 'nil '()))))))
(check-equal? (abstract-lplist
"["
(abstract-variable (abstract-variable-g "γ" 2))
","
(abstract-variable (abstract-variable-a "α" 1))
"|"
(abstract-variable (abstract-variable-a "α" 2))
"]")
(ad:abstract-function
'cons
(list (ad:g 2) (ad:abstract-function 'cons (list (ad:a 1) (ad:a 2))))))
(check-equal? (conjunction
(abstract-atom (abstract-atom-without-args "my-atom1"))
"," (abstract-atom (abstract-atom-without-args "my-atom2"))
"," (abstract-atom (abstract-atom-without-args "my-atom3")))
(list (ad:abstract-atom 'my-atom1 '())
(ad:abstract-atom 'my-atom2 '())
(ad:abstract-atom 'my-atom3 '())))
(check-equal? (abstract-function-term
"my-func"
"("
(abstract-term (abstract-variable (abstract-variable-g "γ" 1)))
","
(abstract-term (abstract-variable (abstract-variable-g "γ" 2))) ")")
(ad:abstract-function 'my-func (list (ad:g 1) (ad:g 2))))
(check-equal? (abstract-atom
(abstract-atom-with-args
"my-atom"
"("
(abstract-variable (abstract-variable-g "γ" 1))
","
(abstract-variable (abstract-variable-g "γ" 2))
")"))
(ad:abstract-atom 'my-atom (list (ad:g 1) (ad:g 2))))
; concrete program section
(check-equal?
(variable "MyPrologVar")
(cd:variable 'MyPrologVar))
; full eval section
(check-equal?
(fullai-rule-without-body
(abstract-atom-with-args
"myatom"
"("
(abstract-variable
(abstract-variable-g "γ" 1))
","
(abstract-variable
(abstract-variable-g "γ" 2))
")")
"."
1)
(fai:full-ai-rule
(ad:abstract-atom 'myatom (list (ad:g 1) (ad:g 2)))
(list)
1))
(check-equal?
(fullai-rule-with-body
(abstract-atom-with-args
"del"
"("
(abstract-variable
(abstract-variable-a "α" 1))
","
(abstract-variable
(abstract-variable-g "γ" 1))
","
(abstract-variable
(abstract-variable-a "α" 2))
")")
"->"
(abstract-substitution
(abstract-substitution-pair
(abstract-variable
(abstract-variable-a "α" 1))
"/"
(abstract-variable
(abstract-variable-g "γ" 2)))
","
(abstract-substitution-pair
(abstract-variable
(abstract-variable-a "α" 2))
"/"
(abstract-variable
(abstract-variable-g "γ" 3))))
"."
1)
(fai:full-ai-rule
(ad:abstract-atom 'del (list (ad:a 1) (ad:g 1) (ad:a 2)))
(list (as:abstract-equality (ad:a 1) (ad:g 2))
(as:abstract-equality (ad:a 2) (ad:g 3)))
1)))