/usr/share/racket/pkgs/datalog/stx.rkt is in racket-common 6.1-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | #lang racket/base
(require (for-syntax racket/base
syntax/parse
racket/local
racket/function
racket/list)
racket/list
racket/match
datalog/ast
datalog/eval)
(define-syntax (:- stx)
(raise-syntax-error ':- "only allowed inside ! and ~" stx))
(define-syntax (! stx)
(raise-syntax-error '! "only allowed inside datalog" stx))
(define-syntax (~ stx)
(raise-syntax-error '~ "only allowed inside datalog" stx))
(define-syntax (? stx)
(raise-syntax-error '? "only allowed inside datalog" stx))
(define (->substitutions sel ls)
(if (void? ls) empty
(map sel ls)))
(define literal->sexp
(match-lambda
[(external _ pred-sym _ args anss)
`(,pred-sym ,@(map term->datum args)
:-
,@(map term->datum anss))]
[(literal _ pred ts)
(list* (if '(predicate-sym? pred)
'(predicate-sym-sym pred)
pred)
(map term->datum ts))]))
(define term->datum
(match-lambda
[(constant _ v)
v]))
(define-syntax (datalog stx)
(syntax-case stx ()
[(_ thy-expr stmt ...)
(syntax/loc stx
(parameterize ([current-theory thy-expr])
(void)
(->substitutions
(datalog-stmt-var-selector stmt)
(eval-statement (datalog-stmt stmt)))
...))]))
(define-syntax (datalog! stx)
(syntax-case stx ()
[(_ thy-expr stmt ...)
(syntax/loc stx
(parameterize ([current-theory thy-expr])
(void)
(eval-top-level-statement (datalog-stmt stmt))
...))]))
(define-syntax (datalog-stmt stx)
(syntax-parse
stx
#:literals (! ~ ?)
[(_ (~and tstx (! c)))
(quasisyntax/loc #'tstx
(assertion #,(srcloc-list #'tstx) (datalog-clause c)))]
[(_ (~and tstx (~ c)))
(quasisyntax/loc #'tstx
(retraction #,(srcloc-list #'tstx) (datalog-clause c)))]
[(_ (~and tstx (? l)))
(quasisyntax/loc #'tstx
(query #,(srcloc-list #'tstx) (datalog-literal/ref l)))]))
(define-syntax (datalog-stmt-var-selector stx)
(syntax-parse
stx
#:literals (! ~ ?)
[(_ (~and tstx (! c)))
(quasisyntax/loc #'tstx (λ (l) (hasheq)))]
[(_ (~and tstx (~ c)))
(quasisyntax/loc #'tstx (λ (l) (hasheq)))]
[(_ (~and tstx (? l)))
(quasisyntax/loc #'tstx (datalog-literal-var-selector l))]))
(define-syntax (datalog-clause stx)
(syntax-parse
stx
#:literals (:-)
[(_ (~and tstx (:- head body ...)))
(local [(define (datalog-literal-variables stx)
(syntax-parse
stx
#:literals (:-)
[sym:id
empty]
[(~and tstx (sym:id arg ... :- ans ...))
(append-map datalog-term-variables
(syntax->list #'(arg ... ans ...)))]
[(~and tstx (sym:id e ...))
(append-map datalog-term-variables
(syntax->list #'(e ...)))]))
(define (datalog-term-variables stx)
(syntax-parse
stx
[sym:id
(cond
[(identifier-binding #'sym 0)
empty]
[(char-upper-case? (string-ref (symbol->string (syntax->datum #'sym)) 0))
(list #'sym)]
[else
empty])]
[sym:expr
empty]))
(define head-vars (datalog-literal-variables #'head))
(define body-vars
(append-map datalog-literal-variables (syntax->list #'(body ...))))
(define body-vars-in-head
(filter
(λ (bv)
(findf (curry bound-identifier=? bv)
head-vars))
body-vars))
(define fake-lam
(quasisyntax/loc #'tstx
(lambda #,head-vars
(void #,@body-vars-in-head))))]
(syntax-local-lift-expression
fake-lam))
(quasisyntax/loc #'tstx
(clause #,(srcloc-list #'tstx) (datalog-literal/bind head)
(list (datalog-literal/ref body) ...)))]
[(_ e)
(quasisyntax/loc #'e
(clause #,(srcloc-list #'e) (datalog-literal/bind e) empty))]))
(define-syntax (datalog-literal/bind stx) (datalog-literal/b stx #t))
(define-syntax (datalog-literal/ref stx) (datalog-literal/b stx #f))
(begin-for-syntax
(define-syntax-class table-id
#:literals (unsyntax)
(pattern sym:id
#:attr ref #''sym
#:attr val #'sym)
(pattern (unsyntax sym:expr)
#:attr ref #'sym
#:attr val #'sym))
(define (datalog-literal/b stx binding?)
(syntax-parse
stx
#:literals (:-)
[(_ sym:table-id)
(syntax-property
(quasisyntax/loc #'sym
(literal #,(srcloc-list #'sym) sym.ref empty))
(if binding? 'disappeared-binding 'disappeared-use)
(syntax-local-introduce #'sym))]
[(_ (~and tstx (sym:table-id arg ... :- ans ...)))
(quasisyntax/loc #'tstx
(external #,(srcloc-list #'tstx) 'sym sym.val
(list (datalog-term arg) ...)
(list (datalog-term ans) ...)))]
[(_ (~and tstx (sym:table-id e ...)))
(syntax-property
(quasisyntax/loc #'tstx
(literal #,(srcloc-list #'tstx) sym.ref
(list (datalog-term e)
...)))
(if binding? 'disappeared-binding 'disappeared-use)
(syntax-local-introduce #'sym))])))
(define-syntax (datalog-literal-var-selector stx)
(syntax-parse
stx
#:literals (:-)
[(_ sym:table-id)
(quasisyntax/loc #'sym (λ (l) (hasheq)))]
[(_ (~and tstx (sym:table-id arg ... :- ans ...)))
(quasisyntax/loc #'tstx
(match-lambda
[(external _srcloc _predsym _pred args anss)
(terms->hasheq (list (datalog-term arg) ...
(datalog-term ans) ...)
(append args anss))]))]
[(_ (~and tstx (sym:table-id e ...)))
(quasisyntax/loc #'tstx
(match-lambda
[(literal _srcloc _predsym ts)
(terms->hasheq (list (datalog-term e) ...)
ts)]))]))
(define (terms->hasheq src-ts res-ts)
(for/fold ([h (hasheq)])
([src (in-list src-ts)]
[res (in-list res-ts)])
(if (variable? src)
(hash-set h (variable-sym src) (constant-value res))
h)))
(define-syntax (datalog-term stx)
(syntax-parse
stx
#:literals (unsyntax)
[(_ sym:id)
(cond
[(identifier-binding #'sym 0)
(quasisyntax/loc #'sym
(constant #,(srcloc-list #'sym) sym))]
[(char-upper-case? (string-ref (symbol->string (syntax->datum #'sym)) 0))
(quasisyntax/loc #'sym
(variable #,(srcloc-list #'sym) 'sym))]
[else
(quasisyntax/loc #'sym
(constant #,(srcloc-list #'sym) 'sym))])]
[(_ (unsyntax sym:expr))
(quasisyntax/loc #'sym
(constant #,(srcloc-list #'sym) sym))]
[(_ sym:expr)
(quasisyntax/loc #'sym
(constant #,(srcloc-list #'sym) sym))]))
(define-for-syntax (srcloc-list stx)
(define src (syntax-source stx))
`(list ,(if (path? src)
`(bytes->path ,(path->bytes src))
`',src)
',(syntax-line stx)
',(syntax-column stx)
',(syntax-position stx)
',(syntax-span stx)))
(provide datalog datalog!
:- ! ~ ?)
|