Paren datalog
This commit is contained in:
parent
8636c4ee36
commit
c1e7bf62f7
|
@ -19,11 +19,13 @@
|
||||||
(format-literals ls)))
|
(format-literals ls)))
|
||||||
|
|
||||||
(define (eval-program p)
|
(define (eval-program p)
|
||||||
(for-each (lambda (s)
|
(for-each eval-top-level-statement p))
|
||||||
(define v (eval-statement s))
|
|
||||||
(unless (void? v)
|
(define (eval-top-level-statement s)
|
||||||
(print-literals v)))
|
(define v (eval-statement s))
|
||||||
p))
|
(unless (void? v)
|
||||||
|
(print-literals v)))
|
||||||
|
|
||||||
(define (eval-statement s)
|
(define (eval-statement s)
|
||||||
(cond
|
(cond
|
||||||
[(assertion? s)
|
[(assertion? s)
|
||||||
|
@ -53,5 +55,6 @@
|
||||||
(provide/contract
|
(provide/contract
|
||||||
[current-theory (parameter/c mutable-theory/c)]
|
[current-theory (parameter/c mutable-theory/c)]
|
||||||
[eval-program (program/c . -> . void)]
|
[eval-program (program/c . -> . void)]
|
||||||
|
[eval-top-level-statement (statement/c . -> . void)]
|
||||||
[eval-statement (statement/c . -> . (or/c void (listof literal?)))]
|
[eval-statement (statement/c . -> . (or/c void (listof literal?)))]
|
||||||
[eval-program/fresh (program/c . -> . immutable-theory/c)])
|
[eval-program/fresh (program/c . -> . immutable-theory/c)])
|
|
@ -175,7 +175,7 @@ This library provides the structures that represent Datalog syntax. It can be re
|
||||||
}
|
}
|
||||||
|
|
||||||
@defstruct[query ([srcloc srcloc/c]
|
@defstruct[query ([srcloc srcloc/c]
|
||||||
[clause clause?])]{
|
[literal literal?])]{
|
||||||
A Datalog query.
|
A Datalog query.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
76
collects/datalog/sexp/lang.rkt
Normal file
76
collects/datalog/sexp/lang.rkt
Normal file
|
@ -0,0 +1,76 @@
|
||||||
|
#lang racket
|
||||||
|
(require (for-syntax syntax/parse)
|
||||||
|
"../eval.rkt"
|
||||||
|
"../ast.rkt")
|
||||||
|
|
||||||
|
(define-syntax (top stx)
|
||||||
|
(syntax-parse
|
||||||
|
stx
|
||||||
|
[(_ . sym:id)
|
||||||
|
(quasisyntax/loc stx
|
||||||
|
(constant #'#,stx 'sym))]))
|
||||||
|
|
||||||
|
(define-syntax (unquote stx)
|
||||||
|
(syntax-parse
|
||||||
|
stx
|
||||||
|
[(_ sym:id)
|
||||||
|
(quasisyntax/loc stx
|
||||||
|
(variable #'#,stx 'sym))]))
|
||||||
|
|
||||||
|
(define-syntax (datum stx)
|
||||||
|
(syntax-parse
|
||||||
|
stx
|
||||||
|
[(_ . sym:str)
|
||||||
|
(quasisyntax/loc stx
|
||||||
|
(constant #'#,stx 'sym))]))
|
||||||
|
|
||||||
|
(define-syntax (->literal stx)
|
||||||
|
(syntax-parse
|
||||||
|
stx
|
||||||
|
[(_ sym:id)
|
||||||
|
(quasisyntax/loc stx
|
||||||
|
(literal #'#,stx 'sym empty))]
|
||||||
|
[(_ (sym:id e ...))
|
||||||
|
(quasisyntax/loc stx
|
||||||
|
(literal #'#,stx 'sym (list e ...)))]))
|
||||||
|
|
||||||
|
(define-syntax (->simple-clause stx)
|
||||||
|
(syntax-case stx ()
|
||||||
|
[(_ e)
|
||||||
|
(quasisyntax/loc stx
|
||||||
|
(clause #'#,stx (->literal e) empty))]))
|
||||||
|
|
||||||
|
(define-syntax (:- stx)
|
||||||
|
(syntax-case stx ()
|
||||||
|
[(_ head body ...)
|
||||||
|
(quasisyntax/loc stx
|
||||||
|
(eval-top-level-statement
|
||||||
|
(assertion #'#,stx
|
||||||
|
(clause #'#,stx (->literal head)
|
||||||
|
(list (->literal body) ...)))))]))
|
||||||
|
|
||||||
|
(define-syntax-rule (define-paren-stx op struct)
|
||||||
|
(define-syntax (op stx)
|
||||||
|
(syntax-case stx ()
|
||||||
|
[(_ c)
|
||||||
|
(quasisyntax/loc stx
|
||||||
|
(eval-top-level-statement (struct #'#,stx (->simple-clause c))))])))
|
||||||
|
|
||||||
|
(define-paren-stx ! assertion)
|
||||||
|
(define-paren-stx ~ retraction)
|
||||||
|
|
||||||
|
(define-syntax (? stx)
|
||||||
|
(syntax-case stx ()
|
||||||
|
[(_ c)
|
||||||
|
(quasisyntax/loc stx
|
||||||
|
(eval-top-level-statement (query #'#,stx (->literal c))))]))
|
||||||
|
|
||||||
|
(provide (rename-out [top #%top]
|
||||||
|
[datum #%datum]
|
||||||
|
#;[module-begin #%module-begin]
|
||||||
|
#;[top-interaction #%top-interaction])
|
||||||
|
|
||||||
|
#%module-begin
|
||||||
|
! ~ ?
|
||||||
|
:-
|
||||||
|
unquote)
|
2
collects/datalog/sexp/lang/reader.rkt
Normal file
2
collects/datalog/sexp/lang/reader.rkt
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
(module reader syntax/module-reader
|
||||||
|
#:language 'datalog/sexp/lang)
|
|
@ -14,15 +14,16 @@
|
||||||
(define test-rkt (build-path examples-dir (format "~a.rkt" t)))
|
(define test-rkt (build-path examples-dir (format "~a.rkt" t)))
|
||||||
(define test-txt (build-path examples-dir (format "~a.txt" t)))
|
(define test-txt (build-path examples-dir (format "~a.txt" t)))
|
||||||
(test-equal? t
|
(test-equal? t
|
||||||
(filter (lambda (l)
|
|
||||||
(not (string=? l "")))
|
|
||||||
(file->lines test-txt))
|
|
||||||
(filter (lambda (l)
|
(filter (lambda (l)
|
||||||
(not (string=? l "")))
|
(not (string=? l "")))
|
||||||
(with-input-from-string
|
(with-input-from-string
|
||||||
(with-output-to-string
|
(with-output-to-string
|
||||||
(lambda () (dynamic-require test-rkt #f)))
|
(lambda () (dynamic-require test-rkt #f)))
|
||||||
port->lines))))
|
port->lines))
|
||||||
|
(filter (lambda (l)
|
||||||
|
(not (string=? l "")))
|
||||||
|
(file->lines test-txt))
|
||||||
|
))
|
||||||
|
|
||||||
(test-suite
|
(test-suite
|
||||||
(path->string examples-dir)
|
(path->string examples-dir)
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
|
ancestor(ebbon, douglas).
|
||||||
|
ancestor(ebbon, john).
|
||||||
|
ancestor(bob, douglas).
|
||||||
ancestor(ebbon, bob).
|
ancestor(ebbon, bob).
|
||||||
ancestor(bob, john).
|
ancestor(bob, john).
|
||||||
ancestor(john, douglas).
|
ancestor(john, douglas).
|
||||||
ancestor(bob, douglas).
|
|
||||||
ancestor(ebbon, john).
|
|
||||||
ancestor(ebbon, douglas).
|
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
; Laps Test
|
; Laps Test
|
||||||
(! (contains ca store rams_couch rams))
|
(! (contains ca store rams_couch rams))
|
||||||
(! (contains rams fetch rams_couch will))
|
(! (contains rams fetch rams_couch will))
|
||||||
(:- (contains ca ,Fetch ,Name ,Watcher)
|
(:- (contains ca fetch ,Name ,Watcher)
|
||||||
(contains ca store ,Name ,Owner)
|
(contains ca store ,Name ,Owner)
|
||||||
(contains ,Owner fetch ,Name ,Watcher))
|
(contains ,Owner fetch ,Name ,Watcher))
|
||||||
(! (trusted ca))
|
(! (trusted ca))
|
||||||
|
|
Loading…
Reference in New Issue
Block a user