8.17.0.6
Logical Notations
This library provides a number of domain-specific DSLs, especially suited from writing domain-specific contracts.
1 Match
(require logic/match) | package: logic-lib |
syntax
(match e [pat e ...+] ...)
pat = id | (var id) | (and2 pat pat) | (? expr) | (app expr pat) | (struct-id pat ...) | (not pat)
A match pattern language, similar to Racket’s.
syntax
(define-match-expander id body)
Defines a match pattern macro, similar to Racket’s match expander.
2 Past-Time Linear Temporal Logic (PLTL)
(require logic/pltl) | package: logic-lib |
syntax
(define-pltl id ϕ)
ϕ = (neg ϕ) | (disj ϕ ϕ) | (conj ϕ ϕ) | (exists (id ...) ϕ) | (previous ϕ) | (since ϕ ϕ) | pat
Defines a PLTL formula that can be used in pltl.
PLTL formulae must be monitorable,
otherwise a compile-time error is raised.
Atomic propositions are match patterns.
Represents the negation of formula e. A negation can only appear inside a formula of the following shapes: (conj a (neg b)), (conj (neg a) b), (since (neg a) b).
Represents the disjunction of two formulae. The free variables of l must be equal to the free variables of r.
Represents the conjunction of two formulae. There is a restriction on conjunctions of the following shapes: (conj (neg a) b), (conj b (neg a)). Namely, the free variables of a must be a subset of the free variables of b.
Given the a sequence of variables, this formula is satisfied when there exists some mapping from the variables to values such that e holds. The free variables of the entire formula is the set of free variables of e minus the quantified xs.
Requires that e be satisfied at the last time step.
Requires that there is some time in the past such that r holds. Since the latest such time (inclusive) until the current time step (exclusive), l holds. The free variables of l must be a subset of the free variables of r.
syntax
(define-pltl-rule (name arg ...) body)
Defines a PLTL macro.
syntax
(pltl formula)
Compiles a PLTL formula into a machine.
> (machine-accepts? (pltl (previous (? number?))) '(1 a)) #t
> (machine-accepts? (pltl (previous (? number?))) '(1 a 2)) #f
3 Regular Expressions (RE)
(require logic/re) | package: logic-lib |
syntax
(define-re id r)
r = (complement r) | (seq r ...) | (union r ...) | (star r) | (epsilon) | (nullset) | pat
Defines a regular expression that can be used in re.
Returns the complement of r, i.e., everything accepted by r is no longer accepted and vice versa.
Returns the concatenation of the given regular expressions.
Returns the union of the given regular expressions.
Returns the Kleene star, i.e. zero or more repetitions, of the given regular expression.
A regular expression corresponding to the singleton language with the empty string.
A regular expression corresponding to the empty language.
syntax
(define-re-rule name body)
(define-re-rule (name arg ...) body)
Defines a regular expression macro.
syntax
(re r)
Compiles a regular expression into a machine.
> (machine-accepts? (re (plus 'a)) '(a a)) #t
> (machine-accepts? (re (plus 'a)) '(a b)) #f
4 Deterministic Finite Automata (DFA)
(require logic/dfa) | package: logic-lib |
syntax
(dfa start (accept ...) [state evt next-state] ...)
Compiles the given DFA to a machine.
> (define M (dfa s1 (s1) [s1 (equal? 0) s2] [s1 (? even?) s1] [s2 (equal? 0) s1] [s2 (? even?) s2])) > (machine-accepts? M (list 2 0 4 0 2)) #t
> (machine-accepts? M (list 0 4 0 2 0)) #f
5 Non-deterministic Finite Automata (NFA)
(require logic/nfa) | package: logic-lib |
syntax
(nfa start (accept ...) [state evt next-state] ...)
Compiles the given NFA to a machine.
6 Quantified Event Automata (QEA)
(require logic/qea) | package: logic-lib |
syntax
(qea opt ...)
opt = (∀ id) | (∃ id) | (start id) | (field fld-id expr) | [-> src-id pat tgt-id trans-opt ...] trans-opt = (when expr) | (set fld-id expr)
Compiles the given QEA to a machine.
> (define Q (qea (∀ i) (start start) (field curr-bid 0) (field min-bid 0) [-> start (list 'put-up-item i r) item-listed (set curr-bid 0) (set min-bid r)] [-> item-listed (list 'bid i a) item-listed (when (> a curr-bid)) (set curr-bid a)] [-> item-listed (list 'sell i) sold (when (> curr-bid min-bid))]))
> (machine-accepts? Q '((put-up-item a 10) (put-up-item b 50) (bid a 100) (sell a))) #t
> (machine-accepts? Q '((put-up-item a 10) (put-up-item b 50) (bid a 5) (sell a))) #f
syntax
[-> src-id pat tgt-id trans-opt ...]
Specifies a transition of the QEA.
syntax
(when expr)
The transition can only occur when expr is #t, where expr can reference fields.
syntax
(set fld-id expr)
Sets the given field when the transition it taken.
7 Stream Logic (SL)
(require logic/sl) | package: logic-lib |
syntax
(sl (id ...) [id expr] ...)
Compiles the given set of stream logic equations to a machine.
> (define S (sl (in) [out (and (out -1 #t) (set-first in))])) > (machine-accepts? S '((in . #t) (in . #t) (in . #t))) #t
> (machine-accepts? S '((in . #t) (in . #t) (in . #f))) #f