-
Notifications
You must be signed in to change notification settings - Fork 0
/
execution.rkt
143 lines (138 loc) · 6.52 KB
/
execution.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
; 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 at-exp racket
(require racket/set
scribble/srcdoc)
(require "abstract-multi-domain.rkt")
(require "abstract-domain-ordering.rkt")
(require "abstract-knowledge.rkt")
(require "preprior-graph.rkt")
(require "abstract-renaming.rkt")
(require (only-in racket-list-utils/utils findf-index))
(require (only-in "multi-unfolding.rkt" unfold-multi-bounded))
(require (only-in "abstraction-inspection-utils.rkt" assemble-var-indices))
(require graph)
(require (for-doc scribble/manual))
(module+ test (require rackunit "cclp-interpreter.rkt"))
(define (unique-atoms conjunction)
(reverse
(foldl
(λ (at acc)
(let ([renaming (findf (λ (x) (renames? x at)) acc)])
(if renaming acc (cons at acc))))
(list)
conjunction)))
(module+ test
(let* ([original (interpret-abstract-conjunction "foo(γ1,α1),bar(γ2,α2)")]
[filtered (unique-atoms original)])
(check-equal? filtered original))
(let* ([st "foo(γ1,α1),bar(γ2,α2),foo(γ1,α1),bar(γ2,α2)"]
[original (interpret-abstract-conjunction st)]
[filtered (unique-atoms original)])
(check-equal?
filtered
(interpret-abstract-conjunction "foo(γ1,α1),bar(γ2,α2)"))))
(provide
(proc-doc/names
unique-atoms
(-> (listof abstract-atom?) (listof abstract-atom?))
(conjunction)
@{Returns a list of all atoms in @racket[conjunction] which are either the first or only representative of their equivalence class.}))
(define (selected-index conjunction preprior full-ai-rules)
(define full-eval-index
(foldl (λ (c i acc)
(or acc
(and (memf (λ (r) (>=-extension (full-evaluation-input-pattern r) c)) full-ai-rules) i)))
#f
conjunction
(stream->list (in-range 0 (length conjunction)))))
(if full-eval-index
full-eval-index
(let* ([multis (filter multi? conjunction)]
[multi-conjuncts (apply append (map (λ (m) (let ([offset (apply max (cons 0 (assemble-var-indices (λ (_) #t) m)))]) (car (unfold-multi-bounded 1 m offset offset)))) multis))]
[unique (unique-atoms (map normalize-abstract-atom (append (filter abstract-atom? conjunction) multi-conjuncts)))]
; note: tc always has reachability of self, even when there are no self loops!
[tc (transitive-closure preprior)]
[first-choice
(findf
(λ (aa1)
(andmap
(λ (aa2)
(hash-ref tc (list aa1 aa2) #f))
unique))
unique)])
(begin
(if (not first-choice)
#f
(findf-index (λ (c) (if (abstract-atom? c) (renames? c first-choice) (ormap (λ (mc) (renames? mc first-choice)) (let ([offset (apply max (cons 0 (assemble-var-indices (λ (_) #t) c)))]) (car (unfold-multi-bounded 1 c offset offset)))))) conjunction))))))
(module+ test
(let* ([preprior (mk-preprior-graph)]
[conjunction (interpret-abstract-conjunction "foo(γ1,α1)")])
(begin
(add-vertex! preprior (first conjunction))
(check-equal? (selected-index conjunction preprior '()) 0)))
(let* ([preprior (mk-preprior-graph)]
[conjunction (interpret-abstract-conjunction "foo(γ1,α1),bar(γ2,α2)")])
(begin
(add-vertex! preprior (first conjunction))
(add-vertex! preprior (second conjunction))
(check-equal? (selected-index conjunction preprior '()) #f)))
(let* ([preprior (mk-preprior-graph)]
[conjunction1 (interpret-abstract-conjunction "foo(γ1,α1),bar(γ2,α2)")]
[conjunction2 (interpret-abstract-conjunction "foo(γ3,α3),bar(γ4,α4)")])
(begin
(add-vertex! preprior (first conjunction1))
(add-vertex! preprior (second conjunction1))
(add-directed-edge! preprior (first conjunction1) (second conjunction1))
(check-equal? (selected-index conjunction2 preprior '()) 0)))
(let* ([preprior (mk-preprior-graph)]
[conjunction (interpret-abstract-conjunction "foo(γ1,α1),bar(γ2,α2)")])
(begin
(add-vertex! preprior (first conjunction))
(add-vertex! preprior (second conjunction))
(add-directed-edge! preprior (second conjunction) (first conjunction))
(check-equal? (selected-index conjunction preprior '()) 1)))
(let* ([preprior (mk-preprior-graph)]
[conjunction (interpret-abstract-conjunction "foo(α1),foo(γ1),foo(nil)")])
(begin
(add-vertex! preprior (first conjunction))
(add-vertex! preprior (second conjunction))
(add-vertex! preprior (third conjunction))
(check-equal? (selected-index conjunction preprior '()) 2)))
(let ([conjunction (interpret-abstract-conjunction "foo(α1),bar(γ1)")]
[full-ai
(list
(full-evaluation
(interpret-abstract-atom "bar(γ1)")
(interpret-abstract-atom "bar(nil)")))])
(check-equal?
(selected-index conjunction (mk-preprior-graph) full-ai)
1)))
; contract could be more specific (range is from 0 to length of the list...), but can wait
(provide
(proc-doc/names
selected-index
(-> (listof abstract-conjunct?) preprior-graph? (listof full-evaluation?) (or/c #f natural-number/c))
(conjunction preprior full-ai-rules)
@{Selects an abstract conjunct from @racket[conjunction] based on the ordering generated by @racket[preprior],
or a fully evaluatable abstract atom based on @racket[full-ai-rules].
If neither source provides sufficient information to make a choice, this function returns @racket[#f].}))