2014-05-01 04:30:18 +00:00
|
|
|
|
#lang racket/base
|
2014-06-10 17:54:10 +00:00
|
|
|
|
;; Implements a nested-word-like automaton mapping sets of messages to sets of other values.
|
|
|
|
|
;; A kind of "regular-expression"-keyed multimap.
|
2014-05-01 04:30:18 +00:00
|
|
|
|
|
2014-06-11 20:24:23 +00:00
|
|
|
|
;; TODO: More global purpose statement.
|
|
|
|
|
;; TODO: Some examples showing the idea(s).
|
2014-05-01 04:30:18 +00:00
|
|
|
|
|
2014-06-11 20:24:23 +00:00
|
|
|
|
;; TODO: rename to matcher.rkt or similar.
|
|
|
|
|
;; TODO: Ontology
|
2014-05-01 04:30:18 +00:00
|
|
|
|
|
2014-06-14 16:41:17 +00:00
|
|
|
|
;; TODO: (generally) interpretations for data definitions
|
|
|
|
|
|
2014-06-11 20:24:23 +00:00
|
|
|
|
(provide ;; Patterns and Projections
|
|
|
|
|
?
|
2014-05-10 23:26:51 +00:00
|
|
|
|
wildcard?
|
2014-05-13 23:35:17 +00:00
|
|
|
|
?!
|
2014-05-22 00:54:36 +00:00
|
|
|
|
(struct-out capture)
|
2014-06-11 20:24:23 +00:00
|
|
|
|
|
2014-05-07 23:23:11 +00:00
|
|
|
|
matcher? ;; expensive; see implementation
|
|
|
|
|
matcher-empty
|
|
|
|
|
matcher-empty?
|
2014-06-11 20:24:23 +00:00
|
|
|
|
pattern->matcher
|
|
|
|
|
pattern->matcher*
|
2014-05-07 23:23:11 +00:00
|
|
|
|
matcher-union
|
|
|
|
|
matcher-intersect
|
|
|
|
|
matcher-erase-path
|
2014-05-08 02:00:46 +00:00
|
|
|
|
matcher-match-value
|
2014-05-10 23:27:17 +00:00
|
|
|
|
matcher-match-matcher
|
2014-05-28 21:11:36 +00:00
|
|
|
|
matcher-append
|
2014-05-13 23:35:17 +00:00
|
|
|
|
matcher-relabel
|
2014-06-11 20:24:23 +00:00
|
|
|
|
|
|
|
|
|
;; Projections
|
2014-05-13 23:35:17 +00:00
|
|
|
|
compile-projection
|
2014-05-29 00:10:55 +00:00
|
|
|
|
compile-projection*
|
2014-05-22 01:02:38 +00:00
|
|
|
|
projection->pattern
|
2014-05-13 23:35:17 +00:00
|
|
|
|
matcher-project
|
2014-05-19 22:58:27 +00:00
|
|
|
|
matcher-key-set
|
2014-06-02 18:12:20 +00:00
|
|
|
|
matcher-key-set/single
|
2014-06-11 20:24:23 +00:00
|
|
|
|
|
|
|
|
|
;; Printing and Serialization
|
2014-05-22 03:20:49 +00:00
|
|
|
|
pretty-print-matcher
|
2014-06-17 21:15:34 +00:00
|
|
|
|
matcher->pretty-string
|
2014-05-28 19:48:37 +00:00
|
|
|
|
matcher->jsexpr
|
|
|
|
|
jsexpr->matcher
|
2014-05-22 03:20:49 +00:00
|
|
|
|
|
|
|
|
|
matcher-union-successes
|
|
|
|
|
matcher-intersect-successes
|
|
|
|
|
matcher-erase-path-successes
|
|
|
|
|
matcher-match-matcher-successes
|
|
|
|
|
matcher-match-matcher-unit
|
|
|
|
|
matcher-project-success)
|
|
|
|
|
|
2014-06-11 20:24:23 +00:00
|
|
|
|
(require racket/set)
|
|
|
|
|
(require racket/match)
|
2014-06-17 21:15:34 +00:00
|
|
|
|
(require (only-in racket/port call-with-output-string with-output-to-string))
|
2014-06-11 20:24:23 +00:00
|
|
|
|
(require (only-in racket/class object?))
|
2014-07-16 18:13:45 +00:00
|
|
|
|
(require "canonicalize.rkt")
|
2014-06-11 20:24:23 +00:00
|
|
|
|
|
|
|
|
|
(require rackunit)
|
|
|
|
|
|
2014-06-10 17:54:10 +00:00
|
|
|
|
;; TODO: perhaps avoid the parameters on the fast-path, if they are
|
|
|
|
|
;; causing measurable slowdown.
|
|
|
|
|
;; TODO: should these even be parameterizable?
|
|
|
|
|
|
2014-06-11 20:24:23 +00:00
|
|
|
|
(define matcher-union-successes
|
|
|
|
|
(make-parameter
|
|
|
|
|
(lambda (v1 v2)
|
|
|
|
|
(match* (v1 v2)
|
|
|
|
|
[(#t v) v]
|
|
|
|
|
[(v #t) v]
|
|
|
|
|
[(v1 v2) (set-union v1 v2)]))))
|
|
|
|
|
|
2014-05-22 03:20:49 +00:00
|
|
|
|
(define matcher-intersect-successes (make-parameter set-union))
|
2014-06-11 20:24:23 +00:00
|
|
|
|
|
|
|
|
|
(define matcher-erase-path-successes
|
|
|
|
|
(make-parameter
|
|
|
|
|
(lambda (s1 s2)
|
|
|
|
|
(define r (set-subtract s1 s2))
|
|
|
|
|
(if (set-empty? r) #f r))))
|
|
|
|
|
|
|
|
|
|
(define matcher-match-matcher-successes
|
|
|
|
|
(make-parameter
|
|
|
|
|
(lambda (v1 v2 a)
|
|
|
|
|
(cons (set-union (car a) v1)
|
|
|
|
|
(set-union (cdr a) v2)))))
|
|
|
|
|
|
2014-05-22 03:20:49 +00:00
|
|
|
|
(define matcher-match-matcher-unit (make-parameter (cons (set) (set))))
|
2014-06-11 20:24:23 +00:00
|
|
|
|
|
2014-06-14 16:41:17 +00:00
|
|
|
|
;; The project-success function should return #f to signal "no success values".
|
2014-05-22 03:20:49 +00:00
|
|
|
|
(define matcher-project-success (make-parameter values))
|
2014-05-21 20:28:38 +00:00
|
|
|
|
|
2014-06-10 17:54:10 +00:00
|
|
|
|
;; Constructs a structure type and a singleton instance of it.
|
2014-05-01 04:30:18 +00:00
|
|
|
|
(define-syntax-rule (define-singleton-struct singleton-name struct-name print-representation)
|
|
|
|
|
(begin
|
|
|
|
|
(struct struct-name ()
|
|
|
|
|
#:transparent
|
|
|
|
|
#:property prop:custom-write
|
|
|
|
|
(lambda (v port mode) (display print-representation port)))
|
|
|
|
|
(define singleton-name (struct-name))))
|
|
|
|
|
|
2014-06-11 20:24:23 +00:00
|
|
|
|
;; A Matcher is either
|
|
|
|
|
;; - #f, indicating no further matches possible
|
|
|
|
|
;; - (success Any), representing a successful match (if the end of
|
|
|
|
|
;; the input has been reached)
|
|
|
|
|
;; - (HashTable (U Sigma Wildcard) Matcher), {TODO}
|
2014-06-11 22:51:52 +00:00
|
|
|
|
;; TODO::: reimplement to use (ordinary-state (Option Matcher) (HashTable Sigma Matcher)), {TODO}
|
2014-06-11 20:24:23 +00:00
|
|
|
|
;; - (wildcard-sequence Matcher), {TODO}
|
|
|
|
|
;; If, in a hashtable matcher, a wild key is present, it is intended
|
|
|
|
|
;; to catch all and ONLY those keys not otherwise present in the
|
|
|
|
|
;; table.
|
2014-06-11 22:51:52 +00:00
|
|
|
|
;; INVARIANT: success only appears right at the end. Never in the middle. Never unbalanced parens. TODO
|
|
|
|
|
;; TODO as part of this: figure out whether we can get rid of the seemingly mandatory EOS-success
|
|
|
|
|
;; pattern that always shows up
|
2014-06-11 20:24:23 +00:00
|
|
|
|
(struct success (value) #:transparent)
|
|
|
|
|
(struct wildcard-sequence (matcher) #:transparent)
|
|
|
|
|
|
2014-05-01 04:30:18 +00:00
|
|
|
|
;; A Sigma is, roughly, a token in a value being matched. It is one of:
|
|
|
|
|
;; - a struct-type, signifying the start of a struct.
|
2014-06-10 17:54:10 +00:00
|
|
|
|
;; - SOL, signifying the start of a list.
|
|
|
|
|
;; - SOV, signifying the start of a vector.
|
|
|
|
|
;; - ILM, signifying the transition into the cdr position of a pair
|
|
|
|
|
;; - EOS, signifying the notional close-paren at the end of a compound.
|
2014-05-01 04:30:18 +00:00
|
|
|
|
;; - any other value, representing itself.
|
2014-06-10 17:54:10 +00:00
|
|
|
|
;; N.B. hash-tables cannot be Sigmas at present.
|
2014-05-28 19:24:26 +00:00
|
|
|
|
(define-singleton-struct SOL start-of-list "<")
|
2014-05-01 04:30:18 +00:00
|
|
|
|
(define-singleton-struct SOV start-of-vector "<vector")
|
2014-05-28 19:24:26 +00:00
|
|
|
|
(define-singleton-struct ILM improper-list-marker "|")
|
2014-05-01 04:30:18 +00:00
|
|
|
|
(define-singleton-struct EOS end-of-sequence ">")
|
|
|
|
|
|
2014-06-10 17:54:10 +00:00
|
|
|
|
;; A Pattern is an atom, the special wildcard value (?), an
|
|
|
|
|
;; (embedded-matcher Matcher), or a Racket compound (struct, pair, or
|
|
|
|
|
;; vector) containing Patterns.
|
2014-05-01 04:30:18 +00:00
|
|
|
|
(define-singleton-struct ? wildcard "★") ;; alternative printing: ¿
|
2014-05-28 21:11:36 +00:00
|
|
|
|
(struct embedded-matcher (matcher) #:transparent)
|
2014-05-01 04:30:18 +00:00
|
|
|
|
|
2014-06-10 17:54:10 +00:00
|
|
|
|
;; A Projection is an atom, the special wildcard value (?), a (capture
|
|
|
|
|
;; Pattern), or a Racket compound (struct, pair, or vector) containing
|
|
|
|
|
;; Projections. A Projection is much like a Pattern, but may include
|
|
|
|
|
;; captures, and may not include embedded matchers.
|
|
|
|
|
;;
|
2014-05-13 23:35:17 +00:00
|
|
|
|
;; When projecting a matcher, the capturing wildcard can be used.
|
2014-05-22 00:54:36 +00:00
|
|
|
|
(struct capture (pattern) #:transparent)
|
|
|
|
|
|
2014-06-10 17:54:10 +00:00
|
|
|
|
;; [Pattern] -> Projection
|
|
|
|
|
;; Construct a capture with default pattern of wildcard.
|
2014-05-22 00:54:36 +00:00
|
|
|
|
(define (?! [pattern ?]) (capture pattern))
|
|
|
|
|
|
2014-06-10 17:54:10 +00:00
|
|
|
|
;; A CompiledProjection is a (Listof (U Sigma ? SOC EOC)). Compiled
|
|
|
|
|
;; projections include start-of-capture and end-of-capture elements.
|
2014-05-22 00:54:36 +00:00
|
|
|
|
(define-singleton-struct SOC start-of-capture "{{")
|
|
|
|
|
(define-singleton-struct EOC end-of-capture "}}")
|
2014-05-13 23:35:17 +00:00
|
|
|
|
|
2014-06-10 17:54:10 +00:00
|
|
|
|
;; Any -> Boolean
|
|
|
|
|
;; Predicate recognising Matchers. Expensive!
|
2014-05-07 23:23:11 +00:00
|
|
|
|
(define (matcher? x)
|
|
|
|
|
(or (eq? x #f)
|
2014-05-19 22:41:59 +00:00
|
|
|
|
(success? x)
|
2014-05-07 23:23:11 +00:00
|
|
|
|
(wildcard-sequence? x)
|
|
|
|
|
(and (hash? x)
|
|
|
|
|
(for/and ([v (in-hash-values x)])
|
|
|
|
|
(matcher? v)))))
|
|
|
|
|
|
2014-06-10 17:54:10 +00:00
|
|
|
|
;; -> Matcher
|
|
|
|
|
;; The empty Matcher
|
2014-05-07 23:23:11 +00:00
|
|
|
|
(define (matcher-empty) #f)
|
2014-06-10 17:54:10 +00:00
|
|
|
|
|
|
|
|
|
;; Matcher -> Boolean
|
|
|
|
|
;; True iff the argument is the empty matcher
|
2014-05-07 23:23:11 +00:00
|
|
|
|
(define (matcher-empty? r) (not r))
|
2014-05-01 04:30:18 +00:00
|
|
|
|
|
2014-06-10 17:54:10 +00:00
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
;; Smart constructors & accessors
|
|
|
|
|
;;
|
|
|
|
|
;; Maintain this INVARIANT: A Matcher is non-empty iff it contains
|
|
|
|
|
;; some keys that map to some Values. Essentially, don't bother
|
|
|
|
|
;; prepending tokens to a Matcher unless there's some possibility it
|
|
|
|
|
;; can map to one or more Values.
|
|
|
|
|
|
2014-07-16 18:13:45 +00:00
|
|
|
|
;; Matcher Matcher -> Boolean
|
|
|
|
|
;; Exploits canonicalization to replace an expensive equal? check with eq?.
|
|
|
|
|
(define (requal? a b)
|
|
|
|
|
(eq? a b))
|
|
|
|
|
|
2014-06-11 22:51:52 +00:00
|
|
|
|
;; (Option Value) -> Matcher
|
2014-06-10 17:54:10 +00:00
|
|
|
|
;; If the argument is #f, returns the empty matcher; otherwise, a success Matcher.
|
2014-06-11 22:51:52 +00:00
|
|
|
|
(define (rsuccess v)
|
2014-07-16 18:13:45 +00:00
|
|
|
|
(and v (canonicalize (success v))))
|
2014-05-19 23:57:02 +00:00
|
|
|
|
|
2014-06-11 22:51:52 +00:00
|
|
|
|
;; (U Sigma Wildcard) Matcher -> Matcher
|
2014-06-10 17:54:10 +00:00
|
|
|
|
;; Prepends e to r, if r is non-empty.
|
2014-06-11 22:51:52 +00:00
|
|
|
|
(define (rseq e r)
|
2014-07-16 18:13:45 +00:00
|
|
|
|
(if (matcher-empty? r) r (canonicalize (hash e r))))
|
|
|
|
|
|
|
|
|
|
;; [ (U Sigma Wildcard) Matcher ] ... -> Matcher
|
|
|
|
|
(define (rseq-multi . ers)
|
|
|
|
|
(canonicalize (apply hash ers)))
|
2014-06-10 17:54:10 +00:00
|
|
|
|
|
|
|
|
|
;; Matcher -> Matcher
|
|
|
|
|
;; Prepends the wildcard pseudo-Sigma to r, if r is non-empty.
|
2014-06-11 22:51:52 +00:00
|
|
|
|
(define (rwild r)
|
|
|
|
|
(rseq ? r))
|
2014-06-10 17:54:10 +00:00
|
|
|
|
|
|
|
|
|
;; Matcher -> Matcher
|
|
|
|
|
;; If r is non-empty, returns a matcher that consumes input up to and
|
|
|
|
|
;; including EOS, then continuing with r.
|
2014-06-11 22:51:52 +00:00
|
|
|
|
(define (rwildseq r)
|
2014-07-16 18:13:45 +00:00
|
|
|
|
(if (matcher-empty? r) r (canonicalize (wildcard-sequence r))))
|
2014-05-01 04:30:18 +00:00
|
|
|
|
|
2014-06-11 22:51:52 +00:00
|
|
|
|
;; Matcher (U Sigma Wildcard) -> Matcher
|
2014-06-10 17:54:10 +00:00
|
|
|
|
;; r must be a hashtable matcher. Retrieves the continuation after
|
|
|
|
|
;; accepting key. If key is absent, returns the failing/empty matcher.
|
|
|
|
|
(define (rlookup r key)
|
|
|
|
|
(hash-ref r key (lambda () #f)))
|
|
|
|
|
|
2014-06-11 22:51:52 +00:00
|
|
|
|
;; (Option (HashTable (U Sigma Wildcard) Matcher)) (U Sigma Wildcard) Matcher -> Matcher
|
|
|
|
|
;; Updates (installs or removes) a continuation in the Matcher r. r must
|
2014-06-10 17:54:10 +00:00
|
|
|
|
;; be either #f or a hashtable matcher.
|
|
|
|
|
(define (rupdate r key k)
|
2014-06-11 22:51:52 +00:00
|
|
|
|
(empty-hash-guard
|
|
|
|
|
(if (matcher-empty? k)
|
|
|
|
|
(hash-remove (or r (hash)) key)
|
|
|
|
|
(hash-set (or r (hash)) key k))))
|
|
|
|
|
|
|
|
|
|
;; Hash -> Matcher
|
|
|
|
|
;; If the argument is empty, returns the canonical empty matcher;
|
|
|
|
|
;; otherwise, returns the argument.
|
|
|
|
|
(define (empty-hash-guard h)
|
2014-07-16 18:13:45 +00:00
|
|
|
|
(and (positive? (hash-count h)) (canonicalize h)))
|
2014-06-10 17:54:10 +00:00
|
|
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
;; Pattern compilation
|
|
|
|
|
|
|
|
|
|
;; Value (Listof Pattern) -> Matcher
|
|
|
|
|
;; Compiles a sequence of patterns into a matcher that accepts input
|
|
|
|
|
;; matching that sequence, yielding v.
|
2014-06-11 22:51:52 +00:00
|
|
|
|
(define (pattern->matcher* v ps0)
|
|
|
|
|
;; Pattern Matcher -> Matcher
|
|
|
|
|
;; acc is the continuation-matcher for the matcher created from ps.
|
|
|
|
|
(define (walk-pair-chain ps acc)
|
2014-05-29 00:10:55 +00:00
|
|
|
|
(match ps
|
|
|
|
|
['() (rseq EOS acc)]
|
2014-06-11 22:51:52 +00:00
|
|
|
|
[(cons p ps) (walk p (walk-pair-chain ps acc))]
|
2014-05-29 00:10:55 +00:00
|
|
|
|
[other (rseq ILM (walk other (rseq EOS acc)))]))
|
|
|
|
|
|
2014-06-11 22:51:52 +00:00
|
|
|
|
;; Pattern Matcher -> Matcher
|
|
|
|
|
;; acc is the continuation-matcher for the matcher created from p.
|
2014-05-29 00:10:55 +00:00
|
|
|
|
(define (walk p acc)
|
2014-05-01 04:30:18 +00:00
|
|
|
|
(match p
|
2014-06-19 00:18:23 +00:00
|
|
|
|
[(capture sub) (error 'pattern->matcher* "Embedded capture in one of the patterns ~v" ps0)]
|
2014-05-01 04:30:18 +00:00
|
|
|
|
[(== ?) (rwild acc)]
|
2014-06-11 22:51:52 +00:00
|
|
|
|
[(cons p1 p2) (rseq SOL (walk p1 (walk-pair-chain p2 acc)))]
|
2014-05-15 15:41:40 +00:00
|
|
|
|
[(? vector? v) (rseq SOV (vector-foldr walk (rseq EOS acc) v))]
|
2014-06-11 22:51:52 +00:00
|
|
|
|
[(embedded-matcher m) (matcher-append m (lambda (_mv) acc))]
|
2014-05-01 04:30:18 +00:00
|
|
|
|
[(? non-object-struct?)
|
2014-06-11 22:51:52 +00:00
|
|
|
|
(rseq (struct->struct-type p)
|
|
|
|
|
(walk-pair-chain (cdr (vector->list (struct->vector p)))
|
|
|
|
|
acc))]
|
2014-06-10 17:54:10 +00:00
|
|
|
|
;; TODO: consider options for treating hash tables as compounds
|
|
|
|
|
;; rather than (useless) atoms
|
2014-05-01 18:05:28 +00:00
|
|
|
|
[(? hash?) (error 'pattern->matcher "Cannot match on hash tables at present")]
|
2014-05-29 00:10:55 +00:00
|
|
|
|
[other (rseq other acc)]))
|
|
|
|
|
|
2014-06-11 22:51:52 +00:00
|
|
|
|
(walk-pair-chain ps0 (rsuccess v)))
|
2014-05-29 00:10:55 +00:00
|
|
|
|
|
2014-06-10 17:54:10 +00:00
|
|
|
|
;; Value Pattern* -> Matcher
|
|
|
|
|
;; Convenience form of pattern->matcher*.
|
2014-05-29 00:10:55 +00:00
|
|
|
|
(define (pattern->matcher v . ps)
|
|
|
|
|
(pattern->matcher* v ps))
|
2014-05-01 04:30:18 +00:00
|
|
|
|
|
2014-06-11 22:51:52 +00:00
|
|
|
|
;; Structure -> StructType
|
|
|
|
|
;; Errors when given any struct that isn't completely transparent/prefab.
|
|
|
|
|
(define (struct->struct-type p)
|
|
|
|
|
(define-values (t skipped?) (struct-info p))
|
|
|
|
|
(when skipped? (error 'struct->struct-type "Cannot reflect on struct instance ~v" p))
|
|
|
|
|
t)
|
2014-05-01 04:30:18 +00:00
|
|
|
|
|
2014-06-11 22:51:52 +00:00
|
|
|
|
;; Any -> Boolean
|
|
|
|
|
;; Racket objects are structures, so we reject them explicitly for
|
|
|
|
|
;; now, leaving them opaque to unification.
|
|
|
|
|
(define (non-object-struct? x)
|
|
|
|
|
(and (struct? x)
|
|
|
|
|
(not (object? x))))
|
2014-05-01 04:30:18 +00:00
|
|
|
|
|
2014-06-11 22:51:52 +00:00
|
|
|
|
;; (A B -> B) B (Vectorof A) -> B
|
|
|
|
|
(define (vector-foldr kons knil v)
|
|
|
|
|
(for/fold [(acc knil)] [(elem (in-vector v (- (vector-length v) 1) -1 -1))]
|
|
|
|
|
(kons elem acc)))
|
2014-05-01 04:30:18 +00:00
|
|
|
|
|
2014-06-11 22:51:52 +00:00
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
;; Matcher combinators
|
2014-05-01 10:46:22 +00:00
|
|
|
|
|
2014-06-10 17:54:10 +00:00
|
|
|
|
;; Matcher Matcher -> Matcher
|
|
|
|
|
;; Computes the union of the multimaps passed in.
|
2014-05-22 03:20:49 +00:00
|
|
|
|
(define matcher-union
|
|
|
|
|
(let ()
|
2014-07-15 20:55:53 +00:00
|
|
|
|
(define (walk re1 re2)
|
|
|
|
|
(match* (re1 re2)
|
2014-05-22 03:20:49 +00:00
|
|
|
|
[(#f #f) #f]
|
|
|
|
|
[(#f r) r]
|
|
|
|
|
[(r #f) r]
|
|
|
|
|
[((wildcard-sequence r1) (wildcard-sequence r2)) (rwildseq (walk r1 r2))]
|
|
|
|
|
[((wildcard-sequence r1) r2) (walk (expand-wildseq r1) r2)]
|
|
|
|
|
[(r1 (wildcard-sequence r2)) (walk r1 (expand-wildseq r2))]
|
|
|
|
|
[((success v1) (success v2)) (rsuccess ((matcher-union-successes) v1 v2))]
|
|
|
|
|
[((? hash? h1) (? hash? h2))
|
2014-07-15 20:55:53 +00:00
|
|
|
|
(define w (walk (rlookup h1 ?) (rlookup h2 ?)))
|
|
|
|
|
(if w (walk/wildcard w h1 h2) (walk/no-wildcard h1 h2))]))
|
|
|
|
|
(define (walk/wildcard w h1 h2)
|
2014-05-22 03:20:49 +00:00
|
|
|
|
(for/fold [(acc (rwild w))]
|
|
|
|
|
[(key (set-remove (set-union (hash-keys h1) (hash-keys h2)) ?))]
|
2014-07-15 20:55:53 +00:00
|
|
|
|
(define k (walk (rlookup h1 key) (rlookup h2 key)))
|
2014-05-22 03:20:49 +00:00
|
|
|
|
(rupdate acc
|
|
|
|
|
key
|
|
|
|
|
(cond
|
2014-07-15 20:55:53 +00:00
|
|
|
|
[(key-open? key) (walk (rwildseq w) k)]
|
2014-05-22 03:20:49 +00:00
|
|
|
|
[(key-close? key) (if (wildcard-sequence? w)
|
2014-07-15 20:55:53 +00:00
|
|
|
|
(walk (wildcard-sequence-matcher w) k)
|
2014-05-22 03:20:49 +00:00
|
|
|
|
k)]
|
2014-07-15 20:55:53 +00:00
|
|
|
|
[else (walk w k)]))))
|
|
|
|
|
(define (walk/no-wildcard h1 h2)
|
|
|
|
|
(define-values (walk-fn smaller-h larger-h)
|
2014-06-11 22:51:52 +00:00
|
|
|
|
(if (< (hash-count h1) (hash-count h2))
|
2014-07-15 20:55:53 +00:00
|
|
|
|
(values walk h1 h2)
|
|
|
|
|
(values (flip walk) h2 h1)))
|
2014-06-11 22:51:52 +00:00
|
|
|
|
(for/fold [(acc larger-h)] [((key k1) (in-hash smaller-h))]
|
2014-07-15 20:55:53 +00:00
|
|
|
|
(rupdate acc key (walk-fn k1 (rlookup larger-h key)))))
|
|
|
|
|
walk))
|
2014-05-01 10:46:22 +00:00
|
|
|
|
|
2014-06-11 22:51:52 +00:00
|
|
|
|
;; (A B -> C) -> A B -> B A -> C
|
|
|
|
|
(define ((flip f) a b) (f b a))
|
2014-05-14 20:29:02 +00:00
|
|
|
|
|
2014-06-10 17:54:10 +00:00
|
|
|
|
;; Matcher Matcher -> Matcher
|
|
|
|
|
;; Computes the intersection of the multimaps passed in.
|
2014-05-22 03:20:49 +00:00
|
|
|
|
(define matcher-intersect
|
2014-05-01 10:46:22 +00:00
|
|
|
|
(let ()
|
2014-05-08 02:00:46 +00:00
|
|
|
|
;; INVARIANT: re1 is a part of the original re1, and likewise for
|
|
|
|
|
;; re2. This is so that the first arg to combine-success-values
|
|
|
|
|
;; always comes from re1, and the second from re2.
|
2014-05-01 10:46:22 +00:00
|
|
|
|
(define (walk re1 re2)
|
|
|
|
|
(match* (re1 re2)
|
2014-05-01 17:37:28 +00:00
|
|
|
|
[((wildcard-sequence r1) (wildcard-sequence r2)) (rwildseq (walk r1 r2))]
|
2014-05-01 10:46:22 +00:00
|
|
|
|
[((wildcard-sequence r1) r2) (walk (expand-wildseq r1) r2)]
|
2014-05-08 02:00:46 +00:00
|
|
|
|
[(r1 (wildcard-sequence r2)) (walk r1 (expand-wildseq r2))]
|
2014-05-22 03:20:49 +00:00
|
|
|
|
[((success v1) (success v2)) (rsuccess ((matcher-intersect-successes) v1 v2))]
|
2014-05-01 04:30:18 +00:00
|
|
|
|
[((? hash? h1) (? hash? h2))
|
2014-05-01 10:46:22 +00:00
|
|
|
|
(define w1 (rlookup h1 ?))
|
|
|
|
|
(define w2 (rlookup h2 ?))
|
|
|
|
|
(define w (and w1 w2 (walk w1 w2)))
|
2014-05-14 20:29:02 +00:00
|
|
|
|
(define (examine-key acc key)
|
|
|
|
|
(rupdate acc
|
|
|
|
|
key
|
|
|
|
|
(match* ((rlookup h1 key) (rlookup h2 key))
|
|
|
|
|
[(#f #f) #f]
|
2014-06-11 22:51:52 +00:00
|
|
|
|
[(#f k2) (walk-wild walk w1 key k2)]
|
|
|
|
|
[(k1 #f) (walk-wild (flip walk) w2 key k1)]
|
2014-05-14 20:29:02 +00:00
|
|
|
|
[(k1 k2) (walk k1 k2)])))
|
|
|
|
|
;; If, say, w1 is #f, then we don't need to examine
|
2014-05-01 11:36:56 +00:00
|
|
|
|
;; every key in h2. So there are four cases:
|
|
|
|
|
;; - both false -> examine the intersection of the key sets
|
|
|
|
|
;; (done by enumerating keys in the smaller hash)
|
|
|
|
|
;; - one nonfalse -> examine only the keys in the other
|
|
|
|
|
;; - both nonfalse -> examine the union of the key sets
|
|
|
|
|
;; This is important for avoiding examination of the whole
|
|
|
|
|
;; structure when wildcards aren't being used.
|
2014-05-14 20:29:02 +00:00
|
|
|
|
(match* (w1 w2)
|
|
|
|
|
[(#f #f) (for/fold [(acc #f)] [(key (in-hash-keys (smaller-hash h1 h2)))]
|
|
|
|
|
(examine-key acc key))]
|
|
|
|
|
[(#f _) (for/fold [(acc #f)] [(key (in-hash-keys h1))] (examine-key acc key))]
|
|
|
|
|
[(_ #f) (for/fold [(acc #f)] [(key (in-hash-keys h2))] (examine-key acc key))]
|
|
|
|
|
[(_ _) (for/fold [(acc (rwild w))] [(key (set-remove (set-union (hash-keys h1)
|
|
|
|
|
(hash-keys h2))
|
|
|
|
|
?))]
|
|
|
|
|
(examine-key acc key))])]))
|
2014-05-08 02:00:46 +00:00
|
|
|
|
(define (walk-wild walk-fn w key k)
|
2014-05-01 10:46:22 +00:00
|
|
|
|
(and w (cond
|
2014-05-08 02:00:46 +00:00
|
|
|
|
[(key-open? key) (walk-fn (rwildseq w) k)]
|
2014-05-01 10:46:22 +00:00
|
|
|
|
[(key-close? key) (if (wildcard-sequence? w)
|
2014-05-08 02:00:46 +00:00
|
|
|
|
(walk-fn (wildcard-sequence-matcher w) k)
|
2014-05-01 10:46:22 +00:00
|
|
|
|
#f)]
|
2014-05-08 02:00:46 +00:00
|
|
|
|
[else (walk-fn w k)])))
|
2014-05-22 03:20:49 +00:00
|
|
|
|
(lambda (re1 re2)
|
|
|
|
|
(match* (re1 re2)
|
|
|
|
|
[(#f r) #f]
|
|
|
|
|
[(r #f) #f]
|
|
|
|
|
[(r1 r2) (walk r1 r2)]))))
|
2014-05-19 22:58:27 +00:00
|
|
|
|
|
2014-06-10 17:54:10 +00:00
|
|
|
|
;; Matcher Matcher -> Matcher
|
2014-05-19 22:41:59 +00:00
|
|
|
|
;; Removes re2's mappings from re1. Assumes re2 has previously been union'd into re1.
|
|
|
|
|
;; The combine-successes function should return #f to signal "no remaining success values".
|
2014-06-17 21:15:44 +00:00
|
|
|
|
(define (matcher-erase-path original1 original2)
|
2014-05-22 03:20:49 +00:00
|
|
|
|
(let ()
|
|
|
|
|
(define (cofinite-pattern)
|
2014-06-17 21:15:44 +00:00
|
|
|
|
(error 'matcher-erase-path "Cofinite pattern required subtracting:\n ~a\nfrom ~a"
|
|
|
|
|
(matcher->pretty-string original2 #:indent 5)
|
|
|
|
|
(matcher->pretty-string original1 #:indent 5)))
|
|
|
|
|
(define (erase-path r1 r2)
|
|
|
|
|
(match* (r1 r2)
|
|
|
|
|
[(#f #f) #f]
|
|
|
|
|
[(r #f) r]
|
|
|
|
|
[(#f r) (cofinite-pattern)]
|
|
|
|
|
[(r1 r2) (walk r1 r2)]))
|
2014-05-22 03:20:49 +00:00
|
|
|
|
(define (walk path aggregate)
|
|
|
|
|
(match* (path aggregate)
|
|
|
|
|
[((wildcard-sequence r1) (wildcard-sequence r2)) (rwildseq (walk r1 r2))]
|
|
|
|
|
[((wildcard-sequence r1) r2) (cofinite-pattern)]
|
|
|
|
|
[(r1 (wildcard-sequence r2)) (walk r1 (expand-wildseq r2))]
|
|
|
|
|
[((success v1) (success v2)) (rsuccess ((matcher-erase-path-successes) v1 v2))]
|
|
|
|
|
[((? hash? h1) (? hash? h2))
|
|
|
|
|
(define w1 (rlookup h1 ?))
|
|
|
|
|
(define w2 (rlookup h2 ?))
|
2014-06-17 21:15:44 +00:00
|
|
|
|
(define w (erase-path w1 w2))
|
2014-05-22 03:20:49 +00:00
|
|
|
|
(define (examine-key acc key)
|
|
|
|
|
(define updated-k (match* ((rlookup h1 key) (rlookup h2 key))
|
|
|
|
|
[(#f #f) #f]
|
|
|
|
|
[(#f k2) (cofinite-pattern)]
|
|
|
|
|
[(k1 #f) (walk-wild key k1 w2)]
|
|
|
|
|
[(k1 k2) (walk k1 k2)]))
|
|
|
|
|
;; Here we ensure a "minimal" remainder in cases where
|
|
|
|
|
;; after an erasure, a particular key's continuation is the
|
|
|
|
|
;; same as the wildcard's continuation.
|
2014-05-26 10:22:35 +00:00
|
|
|
|
(cond
|
|
|
|
|
[(key-open? key)
|
|
|
|
|
(if (and (wildcard-sequence? updated-k)
|
2014-07-16 18:13:45 +00:00
|
|
|
|
(requal? (wildcard-sequence-matcher updated-k) w))
|
2014-05-26 10:22:35 +00:00
|
|
|
|
(rupdate acc key #f)
|
|
|
|
|
(rupdate acc key updated-k))]
|
|
|
|
|
[(key-close? key)
|
|
|
|
|
;; We will check whether this can be removed later, in collapse-wildcard-sequences.
|
|
|
|
|
(rupdate acc key updated-k)]
|
|
|
|
|
[else
|
2014-07-16 18:13:45 +00:00
|
|
|
|
(rupdate acc key (if (requal? updated-k w) #f updated-k))]))
|
2014-05-22 03:20:49 +00:00
|
|
|
|
;; We only need to examine all keys of h1 if w2 nonfalse.
|
2014-05-26 10:22:35 +00:00
|
|
|
|
(collapse-wildcard-sequences
|
|
|
|
|
(if w2
|
|
|
|
|
(for/fold [(acc (rwild w))] [(key (set-remove (set-union (hash-keys h1)
|
|
|
|
|
(hash-keys h2))
|
|
|
|
|
?))]
|
|
|
|
|
(examine-key acc key))
|
|
|
|
|
(for/fold [(acc h1)] [(key (in-hash-keys h2))]
|
|
|
|
|
(examine-key acc key))))]))
|
2014-05-22 03:20:49 +00:00
|
|
|
|
(define (walk-wild key k w)
|
|
|
|
|
(if w
|
|
|
|
|
(cond
|
|
|
|
|
[(key-open? key) (walk k (rwildseq w))]
|
|
|
|
|
[(key-close? key) (if (wildcard-sequence? w)
|
|
|
|
|
(walk k (wildcard-sequence-matcher w))
|
|
|
|
|
k)]
|
|
|
|
|
[else (walk k w)])
|
|
|
|
|
k))
|
2014-06-17 21:15:44 +00:00
|
|
|
|
(erase-path original1 original2)))
|
2014-05-22 03:20:49 +00:00
|
|
|
|
|
2014-06-10 17:54:10 +00:00
|
|
|
|
;; Matcher -> Matcher
|
|
|
|
|
;; Checks for redundant branches in its argument: when a matcher
|
|
|
|
|
;; contains only entries for (EOS -> (wildcard-sequence m')) and
|
|
|
|
|
;; (★ -> (wildcard-sequence m')), it is equivalent to
|
|
|
|
|
;; (wildcard-sequence m') itself. This is in a way the inverse of
|
|
|
|
|
;; expand-wildseq.
|
2014-05-26 10:22:35 +00:00
|
|
|
|
(define (collapse-wildcard-sequences m)
|
|
|
|
|
(match m
|
|
|
|
|
[(? hash? h)
|
|
|
|
|
(define w (rlookup h ?))
|
|
|
|
|
(if (and (wildcard-sequence? w)
|
|
|
|
|
(= (hash-count h) 2))
|
|
|
|
|
(match (set->list (set-remove (hash-keys h) ?))
|
|
|
|
|
[(list (? key-close? other-key))
|
|
|
|
|
(define k (rlookup h other-key))
|
2014-07-16 18:13:45 +00:00
|
|
|
|
(if (requal? k (wildcard-sequence-matcher w))
|
2014-05-26 10:22:35 +00:00
|
|
|
|
w
|
|
|
|
|
h)]
|
|
|
|
|
[_ h])
|
|
|
|
|
h)]
|
|
|
|
|
[other other]))
|
|
|
|
|
|
2014-06-11 22:51:52 +00:00
|
|
|
|
;; Sigma -> Boolean
|
|
|
|
|
;; True iff k represents the start of a compound datum.
|
|
|
|
|
(define (key-open? k)
|
|
|
|
|
(or (eq? k SOL)
|
|
|
|
|
(eq? k SOV)
|
|
|
|
|
(struct-type? k)))
|
|
|
|
|
|
|
|
|
|
;; Sigma -> Boolean
|
|
|
|
|
;; True iff k represents the end of a compound datum.
|
|
|
|
|
(define (key-close? k)
|
|
|
|
|
(eq? k EOS))
|
|
|
|
|
|
|
|
|
|
;; Matcher -> Matcher
|
|
|
|
|
;; Unrolls the implicit recursion in a wildcard-sequence.
|
|
|
|
|
;; Exploits the fact that (rwildseq r) === (matcher-union (rwild (rwildseq r)) (rseq EOS r)).
|
|
|
|
|
(define (expand-wildseq r)
|
|
|
|
|
(matcher-union (rwild (rwildseq r))
|
|
|
|
|
(rseq EOS r)))
|
|
|
|
|
|
|
|
|
|
;; Hashtable Hashtable -> Hashtable
|
|
|
|
|
;; Returns the smaller of its arguments.
|
|
|
|
|
(define (smaller-hash h1 h2)
|
|
|
|
|
(if (< (hash-count h1) (hash-count h2))
|
|
|
|
|
h1
|
|
|
|
|
h2))
|
|
|
|
|
|
2014-06-10 17:54:10 +00:00
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
;; Matching single keys into a multimap
|
|
|
|
|
|
|
|
|
|
;; (Listof Sigma) -> (Listof Sigma)
|
|
|
|
|
;; Hackish support for improper lists. TODO: revisit
|
|
|
|
|
;; Converts an improper list into a proper one with ILM in the penultimate position.
|
2014-05-28 19:24:26 +00:00
|
|
|
|
(define (transform-list-value xs)
|
|
|
|
|
(match xs
|
|
|
|
|
['() '()]
|
|
|
|
|
[(cons x xs) (cons x (transform-list-value xs))]
|
|
|
|
|
[other (cons ILM (cons other '()))]))
|
|
|
|
|
|
2014-06-10 17:54:10 +00:00
|
|
|
|
;; Matcher InputValue [Value] -> Value
|
|
|
|
|
;; Converts the nested structure v on-the-fly into a sequence of
|
|
|
|
|
;; Sigmas and runs them through the Matcher r. If v leads to a success
|
|
|
|
|
;; Matcher, returns the values contained in the success Matcher;
|
|
|
|
|
;; otherwise, returns failure-result.
|
2014-05-22 03:20:49 +00:00
|
|
|
|
(define (matcher-match-value r v [failure-result (set)])
|
2014-05-14 03:11:50 +00:00
|
|
|
|
(if (matcher-empty? r)
|
2014-05-22 03:20:49 +00:00
|
|
|
|
failure-result
|
2014-05-14 03:11:50 +00:00
|
|
|
|
(let walk ((vs (list v)) (stack '(())) (r r))
|
|
|
|
|
(define (walk-wild vs stack)
|
|
|
|
|
(match (rlookup r ?)
|
2014-05-22 03:20:49 +00:00
|
|
|
|
[#f failure-result]
|
2014-05-14 03:11:50 +00:00
|
|
|
|
[k (walk vs stack k)]))
|
|
|
|
|
(match r
|
|
|
|
|
[(wildcard-sequence k)
|
|
|
|
|
(match stack
|
2014-05-22 03:20:49 +00:00
|
|
|
|
['() failure-result]
|
2014-05-14 03:11:50 +00:00
|
|
|
|
[(cons rest stack1) (walk rest stack1 k)])]
|
2014-05-19 22:41:59 +00:00
|
|
|
|
[(success result)
|
2014-05-14 03:11:50 +00:00
|
|
|
|
(if (and (null? vs)
|
|
|
|
|
(null? stack))
|
2014-05-19 22:41:59 +00:00
|
|
|
|
result
|
2014-05-22 03:20:49 +00:00
|
|
|
|
failure-result)]
|
2014-05-14 03:11:50 +00:00
|
|
|
|
[(? hash?)
|
|
|
|
|
(match vs
|
|
|
|
|
['()
|
|
|
|
|
(match stack
|
2014-05-22 03:20:49 +00:00
|
|
|
|
['() failure-result]
|
2014-05-14 03:11:50 +00:00
|
|
|
|
[(cons rest stack1)
|
|
|
|
|
(match (rlookup r EOS)
|
2014-05-22 03:20:49 +00:00
|
|
|
|
[#f failure-result]
|
2014-05-14 03:11:50 +00:00
|
|
|
|
[k (walk rest stack1 k)])])]
|
|
|
|
|
[(cons (== ?) rest)
|
|
|
|
|
(error 'matcher-match-value "Cannot match wildcard as a value")]
|
|
|
|
|
[(cons (cons v1 v2) rest)
|
2014-05-28 19:24:26 +00:00
|
|
|
|
(match (rlookup r SOL)
|
2014-05-14 03:11:50 +00:00
|
|
|
|
[#f (walk-wild rest stack)]
|
2014-05-28 19:24:26 +00:00
|
|
|
|
[k (walk (cons v1 (transform-list-value v2)) (cons rest stack) k)])]
|
2014-05-14 03:11:50 +00:00
|
|
|
|
[(cons (vector vv ...) rest)
|
|
|
|
|
(match (rlookup r SOV)
|
|
|
|
|
[#f (walk-wild rest stack)]
|
|
|
|
|
[k (walk vv (cons rest stack) k)])]
|
|
|
|
|
[(cons (? non-object-struct? s) rest)
|
2014-06-11 22:51:52 +00:00
|
|
|
|
(match (rlookup r (struct->struct-type s))
|
2014-05-14 03:11:50 +00:00
|
|
|
|
[#f (walk-wild rest stack)]
|
2014-06-11 22:51:52 +00:00
|
|
|
|
[k (walk (cdr (vector->list (struct->vector s))) (cons rest stack) k)])]
|
2014-05-14 03:11:50 +00:00
|
|
|
|
[(cons v rest)
|
|
|
|
|
(match (rlookup r v)
|
|
|
|
|
[#f (walk-wild rest stack)]
|
|
|
|
|
[k (walk rest stack k)])])]))))
|
2014-05-01 04:30:18 +00:00
|
|
|
|
|
2014-06-10 17:54:10 +00:00
|
|
|
|
;; Matcher Matcher -> Value
|
|
|
|
|
;;
|
|
|
|
|
;; Similar to matcher-match-value, but instead of a single key,
|
|
|
|
|
;; accepts a Matcher serving as *multiple* simultaneously-examined
|
|
|
|
|
;; keys. Returns the union of all successful values reached by the
|
|
|
|
|
;; probe.
|
2014-05-22 03:20:49 +00:00
|
|
|
|
(define matcher-match-matcher
|
2014-05-10 23:27:17 +00:00
|
|
|
|
(let ()
|
2014-05-21 01:50:19 +00:00
|
|
|
|
(define (walk re1 re2 acc)
|
2014-05-10 23:27:17 +00:00
|
|
|
|
(match* (re1 re2)
|
2014-05-21 01:50:19 +00:00
|
|
|
|
[((wildcard-sequence r1) (wildcard-sequence r2)) (walk r1 r2 acc)]
|
|
|
|
|
[((wildcard-sequence r1) r2) (walk (expand-wildseq r1) r2 acc)]
|
|
|
|
|
[(r1 (wildcard-sequence r2)) (walk r1 (expand-wildseq r2) acc)]
|
2014-05-22 03:20:49 +00:00
|
|
|
|
[((success v1) (success v2)) ((matcher-match-matcher-successes) v1 v2 acc)]
|
2014-05-10 23:27:17 +00:00
|
|
|
|
[((? hash? h1) (? hash? h2))
|
|
|
|
|
(define w1 (rlookup h1 ?))
|
|
|
|
|
(define w2 (rlookup h2 ?))
|
2014-05-21 01:50:19 +00:00
|
|
|
|
(define r (if (and w1 w2)
|
|
|
|
|
(walk w1 w2 acc)
|
|
|
|
|
acc))
|
|
|
|
|
(define (examine-key r key)
|
2014-05-10 23:27:17 +00:00
|
|
|
|
(match* ((rlookup h1 key) (rlookup h2 key))
|
2014-05-21 01:50:19 +00:00
|
|
|
|
[(#f #f) r]
|
|
|
|
|
[(#f k2) (walk-wild walk w1 key k2 r)]
|
|
|
|
|
[(k1 #f) (walk-wild (lambda (re2 re1 acc) (walk re1 re2 acc)) w2 key k1 r)]
|
|
|
|
|
[(k1 k2) (walk k1 k2 r)]))
|
2014-05-14 20:29:02 +00:00
|
|
|
|
;; We optimize as described in matcher-intersect.
|
|
|
|
|
(match* (w1 w2)
|
2014-05-21 01:50:19 +00:00
|
|
|
|
[(#f #f) (for/fold [(r r)] [(key (in-hash-keys (smaller-hash h1 h2)))] (examine-key r key))]
|
|
|
|
|
[(#f _) (for/fold [(r r)] [(key (in-hash-keys h1))] (examine-key r key))]
|
|
|
|
|
[(_ #f) (for/fold [(r r)] [(key (in-hash-keys h2))] (examine-key r key))]
|
|
|
|
|
[(_ _) (for/fold [(r r)] [(key (set-remove (set-union (hash-keys h1) (hash-keys h2)) ?))]
|
|
|
|
|
(examine-key r key))])]))
|
|
|
|
|
(define (walk-wild walker w key k acc)
|
2014-05-10 23:27:17 +00:00
|
|
|
|
(if w
|
|
|
|
|
(cond
|
2014-05-21 01:50:19 +00:00
|
|
|
|
[(key-open? key) (walker (rwildseq w) k acc)]
|
2014-05-10 23:27:17 +00:00
|
|
|
|
[(key-close? key) (if (wildcard-sequence? w)
|
2014-05-21 01:50:19 +00:00
|
|
|
|
(walker (wildcard-sequence-matcher w) k acc)
|
|
|
|
|
acc)]
|
|
|
|
|
[else (walker w k acc)])
|
|
|
|
|
acc))
|
2014-05-22 03:20:49 +00:00
|
|
|
|
(lambda (re1 re2)
|
|
|
|
|
(match* (re1 re2)
|
|
|
|
|
[(#f r) (matcher-match-matcher-unit)]
|
|
|
|
|
[(r #f) (matcher-match-matcher-unit)]
|
|
|
|
|
[(r1 r2) (walk r1 r2 (matcher-match-matcher-unit))]))))
|
2014-05-10 23:27:17 +00:00
|
|
|
|
|
2014-05-28 21:11:36 +00:00
|
|
|
|
;; Matcher × (Value → Matcher) → Matcher
|
2014-06-10 17:54:10 +00:00
|
|
|
|
;; Since Matchers accept *sequences* of input values, this appends two
|
|
|
|
|
;; matchers into a single matcher that accepts their concatenation.
|
|
|
|
|
;; Because matchers map inputs to values, the second matcher is
|
|
|
|
|
;; expressed as a function from success-values from the first matcher
|
|
|
|
|
;; to a second matcher.
|
2014-05-28 21:11:36 +00:00
|
|
|
|
(define (matcher-append m0 m-tail-fn)
|
|
|
|
|
(let walk ((m m0))
|
|
|
|
|
(match m
|
|
|
|
|
[#f #f]
|
|
|
|
|
[(success v) (error 'matcher-append "Ill-formed matcher: ~v" m0)]
|
|
|
|
|
[(wildcard-sequence m1) (rwildseq (walk m1))]
|
|
|
|
|
[(? hash?) (for/fold [(acc #f)] [((k v) (in-hash m))]
|
|
|
|
|
(if (and (key-close? k) (success? v))
|
|
|
|
|
(matcher-union acc (m-tail-fn (success-value v)))
|
|
|
|
|
(rupdate acc k (walk v))))])))
|
|
|
|
|
|
2014-06-10 17:54:10 +00:00
|
|
|
|
;; Matcher (Value -> (Option Value)) -> Matcher
|
|
|
|
|
;; Maps f over success values in m.
|
2014-05-08 02:00:46 +00:00
|
|
|
|
(define (matcher-relabel m f)
|
|
|
|
|
(let walk ((m m))
|
|
|
|
|
(match m
|
|
|
|
|
[#f #f]
|
2014-05-21 01:50:19 +00:00
|
|
|
|
[(success v) (rsuccess (f v))]
|
|
|
|
|
[(wildcard-sequence m1) (rwildseq (walk m1))]
|
|
|
|
|
[(? hash?) (for/fold [(acc #f)] [((k v) (in-hash m))] (rupdate acc k (walk v)))])))
|
2014-05-08 02:00:46 +00:00
|
|
|
|
|
2014-06-10 17:54:10 +00:00
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
;; Projection
|
|
|
|
|
|
|
|
|
|
;; (Listof Projection) -> CompiledProjection
|
|
|
|
|
;; Compiles a sequence of projections into a single CompiledProjection
|
|
|
|
|
;; for use with matcher-project.
|
2014-06-11 22:51:52 +00:00
|
|
|
|
(define (compile-projection* ps0)
|
|
|
|
|
(define (walk-pair-chain ps acc)
|
2014-05-29 00:10:55 +00:00
|
|
|
|
(match ps
|
|
|
|
|
['() (cons EOS acc)]
|
2014-06-11 22:51:52 +00:00
|
|
|
|
[(cons p ps) (walk p (walk-pair-chain ps acc))]
|
2014-05-29 00:10:55 +00:00
|
|
|
|
[other (cons ILM (walk other (cons EOS acc)))]))
|
|
|
|
|
|
|
|
|
|
(define (walk p acc)
|
2014-05-13 23:35:17 +00:00
|
|
|
|
(match p
|
2014-05-22 00:54:36 +00:00
|
|
|
|
[(capture sub) (cons SOC (walk sub (cons EOC acc)))] ;; TODO: enforce non-nesting here
|
2014-05-13 23:35:17 +00:00
|
|
|
|
[(== ?) (cons ? acc)]
|
2014-06-11 22:51:52 +00:00
|
|
|
|
[(cons p1 p2) (cons SOL (walk p1 (walk-pair-chain p2 acc)))]
|
2014-05-15 15:41:40 +00:00
|
|
|
|
[(? vector? v) (cons SOV (vector-foldr walk (cons EOS acc) v))]
|
2014-05-28 21:11:36 +00:00
|
|
|
|
[(embedded-matcher m) (error 'compile-projection "Cannot embed matcher in projection")]
|
2014-05-13 23:35:17 +00:00
|
|
|
|
[(? non-object-struct?)
|
2014-06-11 22:51:52 +00:00
|
|
|
|
(cons (struct->struct-type p)
|
|
|
|
|
(walk-pair-chain (cdr (vector->list (struct->vector p)))
|
|
|
|
|
acc))]
|
2014-05-13 23:35:17 +00:00
|
|
|
|
;; TODO: consider options for treating hash tables as compounds rather than (useless) atoms
|
2014-05-21 23:59:06 +00:00
|
|
|
|
[(? hash?) (error 'compile-projection "Cannot match on hash tables at present")]
|
2014-05-29 00:10:55 +00:00
|
|
|
|
[other (cons other acc)]))
|
|
|
|
|
|
2014-06-11 22:51:52 +00:00
|
|
|
|
(walk-pair-chain ps0 '()))
|
2014-05-29 00:10:55 +00:00
|
|
|
|
|
2014-06-10 17:54:10 +00:00
|
|
|
|
;; Projection* -> CompiledProjection
|
|
|
|
|
;; Convenience form of compile-projection*.
|
2014-05-29 00:10:55 +00:00
|
|
|
|
(define (compile-projection . ps)
|
|
|
|
|
(compile-projection* ps))
|
2014-05-13 23:35:17 +00:00
|
|
|
|
|
2014-06-10 17:54:10 +00:00
|
|
|
|
;; Projection -> Pattern
|
|
|
|
|
;; Strips captures from its argument, returning an equivalent non-capturing pattern.
|
2014-05-22 01:02:38 +00:00
|
|
|
|
(define (projection->pattern p)
|
|
|
|
|
(let walk ((p p))
|
|
|
|
|
(match p
|
2014-06-14 16:41:17 +00:00
|
|
|
|
[(capture sub) sub] ;; TODO: maybe enforce non-nesting here too?
|
2014-05-22 01:02:38 +00:00
|
|
|
|
[(cons p1 p2) (cons (walk p1) (walk p2))]
|
|
|
|
|
[(? vector? v) (for/vector [(e (in-vector v))] (walk e))]
|
|
|
|
|
[(? non-object-struct?)
|
2014-06-14 16:41:17 +00:00
|
|
|
|
(apply (struct-type-make-constructor (struct->struct-type p))
|
|
|
|
|
(map walk (cdr (vector->list (struct->vector p)))))]
|
2014-06-10 17:54:10 +00:00
|
|
|
|
;; TODO: consider options for treating hash tables as compounds
|
|
|
|
|
;; rather than (useless) atoms
|
2014-05-22 01:02:38 +00:00
|
|
|
|
[(? hash?) (error 'projection->pattern "Cannot match on hash tables at present")]
|
|
|
|
|
[other other])))
|
|
|
|
|
|
2014-06-14 16:41:17 +00:00
|
|
|
|
;; Matcher × CompiledProjection -> Matcher
|
2014-06-10 17:54:10 +00:00
|
|
|
|
;; The result matches a sequence of inputs of length equal to the number of captures.
|
2014-05-13 23:35:17 +00:00
|
|
|
|
(define matcher-project
|
|
|
|
|
(let ()
|
2014-06-14 17:10:27 +00:00
|
|
|
|
(define (drop-match m spec) (general-match values drop-edge drop-sigma drop-balanced m spec))
|
|
|
|
|
(define (take-match m spec) (general-match rwild rupdate rseq take-balanced m spec))
|
|
|
|
|
|
|
|
|
|
(define (drop-balanced m k) (general-balanced values values drop-edge m k))
|
|
|
|
|
(define (take-balanced m k) (general-balanced rwildseq rwild rupdate m k))
|
|
|
|
|
|
|
|
|
|
(define (drop-edge acc key k) (matcher-union acc k))
|
|
|
|
|
(define (drop-sigma sigma k) k)
|
|
|
|
|
|
|
|
|
|
(define (general-match add-wild add-edge add-sigma balanced m spec)
|
|
|
|
|
(let walk ((m m) (spec spec))
|
|
|
|
|
(match spec
|
|
|
|
|
['()
|
|
|
|
|
(match m
|
|
|
|
|
[(success v) (rseq EOS (rsuccess ((matcher-project-success) v)))]
|
|
|
|
|
[_ (matcher-empty)])]
|
|
|
|
|
|
|
|
|
|
[(cons (== EOC) k) (drop-match m k)]
|
|
|
|
|
[(cons (== SOC) k) (take-match m k)]
|
|
|
|
|
|
|
|
|
|
[(cons (== ?) k)
|
|
|
|
|
(match m
|
|
|
|
|
[(wildcard-sequence _) (add-wild (walk m k))]
|
|
|
|
|
[(? hash?)
|
|
|
|
|
(for/fold [(acc (add-wild (walk (rlookup m ?) k)))] [((key mk) (in-hash m))]
|
|
|
|
|
(if (eq? key ?)
|
|
|
|
|
acc
|
|
|
|
|
(add-edge acc key (cond
|
|
|
|
|
[(key-open? key) (balanced mk (lambda (mk) (walk mk k)))]
|
|
|
|
|
[(key-close? key) #f]
|
|
|
|
|
[else (walk mk k)]))))]
|
|
|
|
|
[_ (matcher-empty)])]
|
|
|
|
|
|
|
|
|
|
[(cons sigma k)
|
|
|
|
|
(add-sigma sigma
|
|
|
|
|
(match m
|
|
|
|
|
[(wildcard-sequence mk)
|
|
|
|
|
(cond
|
|
|
|
|
[(key-open? sigma) (walk (rwildseq m) k)]
|
|
|
|
|
[(key-close? sigma) (walk mk k)]
|
|
|
|
|
[else (walk m k)])]
|
|
|
|
|
[(? hash?)
|
|
|
|
|
(matcher-union (walk (rlookup m sigma) k)
|
|
|
|
|
(cond
|
|
|
|
|
[(key-open? sigma) (walk (rwildseq (rlookup m ?)) k)]
|
|
|
|
|
[(key-close? sigma) #f]
|
|
|
|
|
[else (walk (rlookup m ?) k)]))]
|
|
|
|
|
[_ (matcher-empty)]))])))
|
|
|
|
|
|
|
|
|
|
(define (general-balanced add-wildseq add-wild add-edge m k)
|
|
|
|
|
(let walk ((m m) (k k))
|
|
|
|
|
(match m
|
|
|
|
|
[(wildcard-sequence mk) (add-wildseq (k mk))]
|
|
|
|
|
[(? hash?)
|
|
|
|
|
(for/fold [(acc (add-wild (walk (rlookup m ?) k)))] [((key mk) (in-hash m))]
|
|
|
|
|
(if (eq? key ?)
|
|
|
|
|
acc
|
|
|
|
|
(add-edge acc key (cond
|
|
|
|
|
[(key-open? key) (walk mk (lambda (mk) (walk mk k)))]
|
|
|
|
|
[(key-close? key) (k mk)]
|
|
|
|
|
[else (walk mk k)]))))]
|
|
|
|
|
[_ (matcher-empty)])))
|
|
|
|
|
|
|
|
|
|
drop-match))
|
2014-05-13 23:35:17 +00:00
|
|
|
|
|
2014-06-10 17:54:10 +00:00
|
|
|
|
;; (Listof Sigma) -> (Listof Sigma)
|
|
|
|
|
;; Hackish support for improper lists. TODO: revisit
|
|
|
|
|
;; Undoes the transformation of transform-list-value, converting
|
|
|
|
|
;; ILM-marked proper lists back into improper ones.
|
2014-05-28 19:24:26 +00:00
|
|
|
|
(define (untransform-list-value vs)
|
|
|
|
|
(match vs
|
|
|
|
|
['() '()]
|
|
|
|
|
[(cons (== ILM) (cons v '())) v]
|
|
|
|
|
[(cons (== ILM) _) (error 'untransform-list-value "Illegal use of ILM" vs)]
|
|
|
|
|
[(cons v vs) (cons v (untransform-list-value vs))]))
|
|
|
|
|
|
2014-05-29 00:10:55 +00:00
|
|
|
|
;; Matcher → (Option (Setof (Listof Value)))
|
2014-06-10 17:54:10 +00:00
|
|
|
|
;; Extracts the "keys" in its argument multimap m, representing input
|
|
|
|
|
;; sequences as lists. Multiplies out unions. Returns #f if any
|
|
|
|
|
;; dimension of m is infinite.
|
2014-05-19 22:58:27 +00:00
|
|
|
|
(define matcher-key-set
|
2014-05-13 23:35:17 +00:00
|
|
|
|
(let ()
|
|
|
|
|
;; Matcher (Value Matcher -> (Setof Value)) -> (Option (Setof Value))
|
|
|
|
|
;; Calls k with each possible atomic value at this matcher
|
|
|
|
|
;; position, and accumulates the results.
|
|
|
|
|
(define (walk m k)
|
|
|
|
|
(match m
|
|
|
|
|
[(wildcard-sequence _) #f]
|
|
|
|
|
[(? hash?)
|
|
|
|
|
(and (not (hash-has-key? m ?))
|
|
|
|
|
(for/fold [(acc (set))] [((key mk) (in-hash m))]
|
|
|
|
|
(maybe-union
|
|
|
|
|
acc
|
|
|
|
|
(cond
|
|
|
|
|
[(key-open? key)
|
|
|
|
|
(walk-seq mk (lambda (vss vsk)
|
|
|
|
|
(for/fold [(acc (set))] [(vs (in-set vss))]
|
|
|
|
|
(maybe-union acc
|
|
|
|
|
(k (transform-seqs vs key) vsk)))))]
|
|
|
|
|
[(key-close? key)
|
2014-05-19 22:58:27 +00:00
|
|
|
|
(error 'matcher-key-set "Internal error: unexpected key-close")]
|
2014-05-13 23:35:17 +00:00
|
|
|
|
[else
|
|
|
|
|
(k key mk)]))))]
|
2014-05-14 03:12:57 +00:00
|
|
|
|
[_ (set)]))
|
2014-05-13 23:35:17 +00:00
|
|
|
|
|
|
|
|
|
;; Matcher (Value Matcher -> (Setof (Listof Value))) -> (Option (Setof (Listof Value)))
|
|
|
|
|
;; Calls k with each possible sequence of atomic values at this
|
|
|
|
|
;; matcher position, and accumulates the results.
|
|
|
|
|
(define (walk-seq m k)
|
|
|
|
|
(match m
|
|
|
|
|
[(wildcard-sequence _) #f]
|
|
|
|
|
[(? hash?)
|
|
|
|
|
(and (not (hash-has-key? m ?))
|
|
|
|
|
(for/fold [(acc (set))] [((key mk) (in-hash m))]
|
|
|
|
|
(maybe-union acc (cond
|
|
|
|
|
[(key-close? key) (k (set '()) mk)]
|
|
|
|
|
[else (walk (rseq key mk)
|
|
|
|
|
(lambda (v vk)
|
|
|
|
|
(walk-seq vk (lambda (vss vsk)
|
|
|
|
|
(k (for/set [(vs (in-set vss))]
|
|
|
|
|
(cons v vs))
|
|
|
|
|
vsk)))))]))))]
|
2014-05-29 00:10:55 +00:00
|
|
|
|
[_ (k (set) #f)]))
|
2014-05-13 23:35:17 +00:00
|
|
|
|
|
|
|
|
|
;; (Listof Value) Sigma -> Value
|
|
|
|
|
(define (transform-seqs vs opener)
|
|
|
|
|
(cond
|
2014-05-28 19:24:26 +00:00
|
|
|
|
[(eq? opener SOL) (untransform-list-value vs)]
|
2014-05-13 23:35:17 +00:00
|
|
|
|
[(eq? opener SOV) (list->vector vs)]
|
|
|
|
|
[(struct-type? opener) (apply (struct-type-make-constructor opener) vs)]))
|
|
|
|
|
|
|
|
|
|
;; (Option (Setof A)) (Option (Setof A)) -> (Option (Setof A))
|
|
|
|
|
(define (maybe-union s1 s2) (and s1 s2 (set-union s1 s2)))
|
|
|
|
|
|
|
|
|
|
(lambda (m)
|
2014-05-29 00:10:55 +00:00
|
|
|
|
(walk-seq m (lambda (vss vsk) vss)))))
|
2014-05-13 23:35:17 +00:00
|
|
|
|
|
2014-06-02 18:12:20 +00:00
|
|
|
|
;; Matcher → (Option (Setof Value))
|
|
|
|
|
;; As matcher-key-set, but extracts just the first captured subvalue.
|
|
|
|
|
(define (matcher-key-set/single m)
|
|
|
|
|
(define vss (matcher-key-set m))
|
|
|
|
|
(and vss (for/set [(vs (in-set vss))] (car vs))))
|
|
|
|
|
|
2014-06-10 17:54:10 +00:00
|
|
|
|
;; struct-type -> Symbol
|
|
|
|
|
;; Extract just the name of the given struct-type.
|
2014-05-28 19:48:37 +00:00
|
|
|
|
(define (struct-type-name st)
|
|
|
|
|
(define-values (name x2 x3 x4 x5 x6 x7 x8) (struct-type-info st))
|
|
|
|
|
name)
|
|
|
|
|
|
2014-06-10 17:54:10 +00:00
|
|
|
|
;; Matcher [OutputPort] [#:indent Nat] -> Void
|
|
|
|
|
;; Pretty-prints the given matcher on the given port, with
|
|
|
|
|
;; second-and-subsequent lines indented by the given amount.
|
2014-05-14 04:01:43 +00:00
|
|
|
|
(define (pretty-print-matcher m [port (current-output-port)] #:indent [initial-indent 0])
|
|
|
|
|
(define (d x) (display x port))
|
|
|
|
|
(define (walk i m)
|
|
|
|
|
(match m
|
|
|
|
|
[#f
|
|
|
|
|
(d "::: no further matches possible")]
|
|
|
|
|
[(wildcard-sequence k)
|
|
|
|
|
(d "...>")
|
|
|
|
|
(walk (+ i 4) k)]
|
2014-05-19 22:41:59 +00:00
|
|
|
|
[(success vs)
|
2014-05-14 04:01:43 +00:00
|
|
|
|
(d "{")
|
2014-05-19 22:41:59 +00:00
|
|
|
|
(d vs)
|
2014-05-14 04:01:43 +00:00
|
|
|
|
(d "}")]
|
|
|
|
|
[(? hash? h)
|
|
|
|
|
(if (zero? (hash-count h))
|
|
|
|
|
(d " ::: empty hash!")
|
|
|
|
|
(for/fold [(need-sep? #f)] [((key k) (in-hash h))]
|
|
|
|
|
(when need-sep?
|
|
|
|
|
(newline port)
|
|
|
|
|
(d (make-string i #\space)))
|
|
|
|
|
(d " ")
|
|
|
|
|
(define keystr (call-with-output-string
|
|
|
|
|
(lambda (p)
|
2014-06-18 01:53:09 +00:00
|
|
|
|
(cond
|
|
|
|
|
[(struct-type? key)
|
|
|
|
|
(display "<s:" p)
|
|
|
|
|
(display (struct-type-name key) p)]
|
|
|
|
|
[else
|
|
|
|
|
(write key p)]))))
|
2014-05-14 04:01:43 +00:00
|
|
|
|
(d keystr)
|
|
|
|
|
(walk (+ i 1 (string-length keystr)) k)
|
|
|
|
|
#t))]))
|
|
|
|
|
(walk initial-indent m)
|
|
|
|
|
(newline port)
|
|
|
|
|
m)
|
|
|
|
|
|
2014-06-17 21:15:34 +00:00
|
|
|
|
(define (matcher->pretty-string m #:indent [initial-indent 0])
|
|
|
|
|
(with-output-to-string (lambda () (pretty-print-matcher m #:indent initial-indent))))
|
|
|
|
|
|
2014-06-10 17:54:10 +00:00
|
|
|
|
;; Matcher (Value -> JSExpr) -> JSExpr
|
|
|
|
|
;; Serializes a matcher to a JSON expression.
|
2014-05-28 19:48:37 +00:00
|
|
|
|
(define (matcher->jsexpr m success->jsexpr)
|
|
|
|
|
(let walk ((m m))
|
|
|
|
|
(match m
|
|
|
|
|
[#f '()]
|
|
|
|
|
[(success v) (list "" (success->jsexpr v))]
|
|
|
|
|
[(wildcard-sequence m1) (list "...)" (walk m1))]
|
|
|
|
|
[(? hash?) (for/list [((k v) (in-hash m))]
|
|
|
|
|
(list (match k
|
|
|
|
|
[(== ?) (list "__")]
|
|
|
|
|
[(== SOL) (list "(")]
|
|
|
|
|
[(== SOV) (list "#(")]
|
|
|
|
|
[(== EOS) (list ")")]
|
|
|
|
|
[(? struct-type? t)
|
|
|
|
|
(list (string-append (symbol->string (struct-type-name t)) "("))]
|
|
|
|
|
[else k])
|
|
|
|
|
(walk v)))])))
|
|
|
|
|
|
2014-06-10 17:54:10 +00:00
|
|
|
|
;; String -> String
|
|
|
|
|
;; Undoes the encoding of struct-type names used in the JSON serialization of Matchers.
|
2014-05-28 19:48:37 +00:00
|
|
|
|
(define (deserialize-struct-type-name stn)
|
|
|
|
|
(define expected-paren-pos (- (string-length stn) 1))
|
|
|
|
|
(and (char=? (string-ref stn expected-paren-pos) #\()
|
|
|
|
|
(substring stn 0 expected-paren-pos)))
|
|
|
|
|
|
2014-06-10 17:54:10 +00:00
|
|
|
|
;; JSExpr (JSExpr -> Value) [String -> (Option struct-type)] -> Matcher
|
|
|
|
|
;; Deserializes a matcher from a JSON expression.
|
2014-05-28 19:48:37 +00:00
|
|
|
|
(define (jsexpr->matcher j jsexpr->success [struct-type-name->struct-type (lambda () #f)])
|
|
|
|
|
(let walk ((j j))
|
|
|
|
|
(match j
|
|
|
|
|
['() #f]
|
|
|
|
|
[(list "" vj) (rsuccess (jsexpr->success vj))]
|
|
|
|
|
[(list "...)" j1) (rwildseq (walk j1))]
|
2014-07-16 18:13:45 +00:00
|
|
|
|
[(list (list kjs vjs) ...)
|
|
|
|
|
(canonicalize
|
|
|
|
|
(for/hash [(kj kjs) (vj vjs)]
|
|
|
|
|
(values (match kj
|
|
|
|
|
[(list "__") ?]
|
|
|
|
|
[(list "(") SOL]
|
|
|
|
|
[(list "#(") SOV]
|
|
|
|
|
[(list ")") EOS]
|
|
|
|
|
[(list (? string? s))
|
|
|
|
|
(match (deserialize-struct-type-name s)
|
|
|
|
|
[#f (error 'jsexpr->matcher
|
|
|
|
|
"Illegal open-parenthesis mark ~v"
|
|
|
|
|
kj)]
|
|
|
|
|
[tn (match (struct-type-name->struct-type tn)
|
|
|
|
|
[#f (error 'jsexpr->matcher
|
|
|
|
|
"Unexpected struct type ~v"
|
|
|
|
|
tn)]
|
|
|
|
|
[t t])])]
|
|
|
|
|
[other other])
|
|
|
|
|
(walk vj))))])))
|
2014-05-28 19:48:37 +00:00
|
|
|
|
|
2014-05-13 23:35:03 +00:00
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
|
|
(module+ test
|
2014-05-23 02:34:24 +00:00
|
|
|
|
(require racket/pretty)
|
|
|
|
|
|
2014-05-19 22:41:59 +00:00
|
|
|
|
(define SA (set 'A))
|
|
|
|
|
(define SB (set 'B))
|
|
|
|
|
(define SC (set 'C))
|
|
|
|
|
(define SD (set 'D))
|
|
|
|
|
(define Sfoo (set 'foo))
|
|
|
|
|
(define S+ (set '+))
|
|
|
|
|
(define SX (set 'X))
|
2014-07-16 18:13:45 +00:00
|
|
|
|
(define (E v) (rseq EOS (rsuccess v)))
|
|
|
|
|
(check-equal? (pattern->matcher SA 123) (rseq 123 (E SA)))
|
2014-05-28 19:24:26 +00:00
|
|
|
|
(check-equal? (pattern->matcher SA (cons 1 2))
|
2014-07-16 18:13:45 +00:00
|
|
|
|
(rseq SOL (rseq 1 (rseq ILM (rseq 2 (rseq EOS (E SA)))))))
|
2014-05-28 19:24:26 +00:00
|
|
|
|
(check-equal? (pattern->matcher SA (cons ? 2))
|
2014-07-16 18:13:45 +00:00
|
|
|
|
(rseq SOL (rseq ? (rseq ILM (rseq 2 (rseq EOS (E SA)))))))
|
|
|
|
|
(check-equal? (pattern->matcher SA (list 1 2)) (rseq SOL (rseq 1 (rseq 2 (rseq EOS (E SA))))))
|
|
|
|
|
(check-equal? (pattern->matcher SA (list ? 2)) (rseq SOL (rseq ? (rseq 2 (rseq EOS (E SA))))))
|
|
|
|
|
(check-equal? (pattern->matcher SA SOL) (rseq struct:start-of-list (rseq EOS (E SA))))
|
|
|
|
|
(check-equal? (pattern->matcher SA ?) (rseq ? (E SA)))
|
2014-05-13 23:35:03 +00:00
|
|
|
|
)
|
|
|
|
|
|
2014-05-01 04:30:18 +00:00
|
|
|
|
(module+ test
|
|
|
|
|
(define (check-matches matcher . tests)
|
|
|
|
|
(let walk ((tests tests))
|
|
|
|
|
(match tests
|
|
|
|
|
['() (void)]
|
|
|
|
|
[(list* message expectedstr rest)
|
2014-05-07 23:23:11 +00:00
|
|
|
|
(define actualset (matcher-match-value matcher message))
|
2014-05-01 04:30:18 +00:00
|
|
|
|
(printf "~v ==> ~v\n" message actualset)
|
|
|
|
|
(check-equal? actualset
|
|
|
|
|
(apply set (map (lambda (c) (string->symbol (string c)))
|
|
|
|
|
(string->list expectedstr))))
|
|
|
|
|
(walk rest)])))
|
|
|
|
|
|
2014-05-14 03:11:50 +00:00
|
|
|
|
(check-matches
|
|
|
|
|
#f
|
|
|
|
|
(list 'z 'x) ""
|
|
|
|
|
'foo ""
|
|
|
|
|
(list (list 'z (list 'z))) "")
|
|
|
|
|
|
2014-05-14 04:01:43 +00:00
|
|
|
|
(define (pretty-print-matcher* m)
|
|
|
|
|
(newline)
|
|
|
|
|
(pretty-print-matcher m)
|
2014-05-14 20:28:47 +00:00
|
|
|
|
(flush-output)
|
|
|
|
|
m)
|
2014-05-14 04:01:43 +00:00
|
|
|
|
|
|
|
|
|
(void (pretty-print-matcher*
|
2014-05-19 22:41:59 +00:00
|
|
|
|
(matcher-union (pattern->matcher SA (list (list ?) 'x))
|
|
|
|
|
(pattern->matcher SB (list (list ?) 'y)))))
|
2014-05-01 04:30:18 +00:00
|
|
|
|
|
2014-05-14 04:01:43 +00:00
|
|
|
|
(void (pretty-print-matcher*
|
2014-05-19 22:41:59 +00:00
|
|
|
|
(matcher-union (pattern->matcher SA (list (list 'a 'b) 'x))
|
|
|
|
|
(pattern->matcher SB (list (list 'c 'd) 'y)))))
|
2014-05-01 04:30:18 +00:00
|
|
|
|
|
2014-05-14 04:01:43 +00:00
|
|
|
|
(void (pretty-print-matcher*
|
2014-05-19 22:41:59 +00:00
|
|
|
|
(matcher-union (pattern->matcher SA (list (list 'a 'b) 'x))
|
|
|
|
|
(pattern->matcher SB (list (list ? ?) 'y)))))
|
2014-05-01 04:30:18 +00:00
|
|
|
|
|
|
|
|
|
(check-matches
|
2014-05-14 04:01:43 +00:00
|
|
|
|
(pretty-print-matcher*
|
2014-05-19 22:41:59 +00:00
|
|
|
|
(matcher-union (pattern->matcher SA (list (list 'a 'b) 'x))
|
|
|
|
|
(pattern->matcher SB (list (list ? ?) 'x))))
|
2014-05-01 04:30:18 +00:00
|
|
|
|
(list 'z 'x) ""
|
|
|
|
|
(list (list 'z 'z) 'x) "B"
|
|
|
|
|
(list (list 'z (list 'z)) 'x) "B"
|
|
|
|
|
(list (list 'a 'b) 'x) "AB")
|
|
|
|
|
|
|
|
|
|
(check-matches
|
2014-05-14 04:01:43 +00:00
|
|
|
|
(pretty-print-matcher*
|
2014-05-19 22:41:59 +00:00
|
|
|
|
(matcher-union (pattern->matcher SA (list (list 'a 'b) 'x))
|
|
|
|
|
(pattern->matcher SB (list (list ?) 'y))))
|
2014-05-01 04:30:18 +00:00
|
|
|
|
(list 'z 'y) ""
|
|
|
|
|
(list (list 'z 'z) 'y) ""
|
|
|
|
|
(list (list 'z 'z) 'x) ""
|
|
|
|
|
(list (list 'a 'b) 'x) "A")
|
|
|
|
|
|
|
|
|
|
(check-matches
|
2014-05-14 04:01:43 +00:00
|
|
|
|
(pretty-print-matcher*
|
2014-05-19 22:41:59 +00:00
|
|
|
|
(matcher-union (pattern->matcher SA (list (list 'a 'b) 'x))
|
|
|
|
|
(pattern->matcher SB (list ? 'y))))
|
2014-05-01 04:30:18 +00:00
|
|
|
|
(list 'z 'y) "B"
|
|
|
|
|
(list (list 'z 'z) 'y) "B"
|
|
|
|
|
(list (list 'a 'b) 'x) "A")
|
|
|
|
|
|
|
|
|
|
(check-matches
|
2014-05-14 04:01:43 +00:00
|
|
|
|
(pretty-print-matcher*
|
2014-05-19 22:41:59 +00:00
|
|
|
|
(matcher-union (pattern->matcher SA (list 'a 'b))
|
|
|
|
|
(pattern->matcher SB (list 'c 'd))))
|
2014-05-01 04:30:18 +00:00
|
|
|
|
(list 'a 'b) "A"
|
|
|
|
|
(list 'c 'd) "B"
|
|
|
|
|
(list 'a 'd) ""
|
|
|
|
|
(list 'c 'b) "")
|
|
|
|
|
|
2014-05-19 22:41:59 +00:00
|
|
|
|
(void (pretty-print-matcher* (matcher-union (pattern->matcher SA (list (list 'a 'b) 'x))
|
2014-05-14 04:01:43 +00:00
|
|
|
|
;; Note: this is a largely nonsense matcher,
|
|
|
|
|
;; since it expects no input at all
|
2014-05-19 23:57:02 +00:00
|
|
|
|
(rseq EOS (rsuccess (set 'B))))))
|
2014-05-01 04:30:18 +00:00
|
|
|
|
|
|
|
|
|
(check-matches
|
2014-05-14 04:01:43 +00:00
|
|
|
|
(pretty-print-matcher*
|
2014-05-19 22:41:59 +00:00
|
|
|
|
(matcher-union (pattern->matcher SA (list (list 'a 'b) 'x))
|
|
|
|
|
(pattern->matcher SB ?)))
|
2014-05-01 04:30:18 +00:00
|
|
|
|
(list (list 'a 'b) 'x) "AB"
|
|
|
|
|
'p "B"
|
|
|
|
|
(list 'p) "B")
|
|
|
|
|
|
|
|
|
|
(check-matches
|
2014-05-14 04:01:43 +00:00
|
|
|
|
(pretty-print-matcher*
|
2014-05-19 22:41:59 +00:00
|
|
|
|
(matcher-union (pattern->matcher SA (list 'a ?))
|
|
|
|
|
(pattern->matcher SB (list 'a (list 'b)))))
|
2014-05-01 04:30:18 +00:00
|
|
|
|
|
|
|
|
|
(list 'a (list 'b)) "AB"
|
|
|
|
|
(list 'a (list 'b 'b)) "A"
|
|
|
|
|
(list 'a (list 'c 'c)) "A"
|
|
|
|
|
(list 'a (list 'c)) "A"
|
|
|
|
|
(list 'a (list (list))) "A"
|
|
|
|
|
(list 'a (list)) "A"
|
|
|
|
|
(list 'a 'x) "A")
|
|
|
|
|
|
|
|
|
|
(check-matches
|
2014-05-14 04:01:43 +00:00
|
|
|
|
(pretty-print-matcher*
|
2014-05-19 22:41:59 +00:00
|
|
|
|
(matcher-union (matcher-union (pattern->matcher SA (list 'a ?))
|
|
|
|
|
(pattern->matcher SA (list 'q ?)))
|
|
|
|
|
(pattern->matcher SB (list 'a (list 'b)))))
|
2014-05-01 04:30:18 +00:00
|
|
|
|
(list 'a (list 'b)) "AB"
|
|
|
|
|
(list 'q (list 'b)) "A"
|
|
|
|
|
(list 'a 'x) "A"
|
|
|
|
|
(list 'q 'x) "A"
|
|
|
|
|
(list 'a (list)) "A"
|
|
|
|
|
(list 'q (list)) "A"
|
|
|
|
|
(list 'z (list)) "")
|
|
|
|
|
|
|
|
|
|
(define (bigdemo)
|
|
|
|
|
(define ps
|
|
|
|
|
(for/list ((c (in-string "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")))
|
|
|
|
|
(define csym (string->symbol (string c)))
|
2014-05-19 22:41:59 +00:00
|
|
|
|
(pattern->matcher (set csym) (list csym ?))))
|
2014-05-07 23:23:11 +00:00
|
|
|
|
(matcher-union (foldr matcher-union (matcher-empty) ps)
|
2014-05-19 22:41:59 +00:00
|
|
|
|
(pattern->matcher S+ (list 'Z (list ? '- ?)))))
|
2014-05-01 04:30:18 +00:00
|
|
|
|
|
2014-05-14 04:01:43 +00:00
|
|
|
|
(void (pretty-print-matcher* (bigdemo)))
|
2014-05-01 04:30:18 +00:00
|
|
|
|
(check-matches
|
|
|
|
|
(bigdemo)
|
|
|
|
|
(list 'a '-) "a"
|
|
|
|
|
(list 'Z '-) "Z"
|
|
|
|
|
(list '? '-) ""
|
|
|
|
|
(list 'a (list '- '- '-)) "a"
|
|
|
|
|
(list 'a (list '- '- '- '- '- '- '- '- '- '- '- '- '- '- '- '- '- '- '- '- '- '-)) "a"
|
|
|
|
|
(list 'Z) ""
|
|
|
|
|
(list 'Z 'x) "Z"
|
|
|
|
|
(list 'Z (list)) "Z"
|
|
|
|
|
(list 'Z (list '-)) "Z"
|
|
|
|
|
(list 'Z (list '-)) "Z"
|
|
|
|
|
(list 'Z (list '- '-)) "Z"
|
|
|
|
|
(list 'Z (list '- '- '-)) "Z+"
|
|
|
|
|
(list 'Z (list '- '- '- '-)) "Z"
|
|
|
|
|
(list 'Z (list '- '- '- '- '- '- '- '- '- '- '- '- '- '- '- '- '- '- '- '- '- '-)) "Z"
|
|
|
|
|
(list 'Z '((()) - -)) "Z+"
|
|
|
|
|
(list '? (list '- '- '-)) "")
|
|
|
|
|
|
2014-05-28 19:24:26 +00:00
|
|
|
|
;; ;; Having switched from pair-based matching to list-based matching,
|
|
|
|
|
;; ;; it's no longer supported to match with a wildcard in the cdr of a
|
|
|
|
|
;; ;; pair. Or rather, it is, but it won't work reliably: when the
|
|
|
|
|
;; ;; value to be matched is a proper list, it will fail to match.
|
|
|
|
|
;; ;; Consequently: Don't Do That.
|
|
|
|
|
;; (check-matches (pretty-print-matcher* (pattern->matcher SA (list* 'a 'b ?)))
|
|
|
|
|
;; (list 'a 'b 'c 'd 'e 'f) "A"
|
|
|
|
|
;; (list 'b 'c 'd 'e 'f 'a) ""
|
|
|
|
|
;; 3 "")
|
2014-05-01 04:30:18 +00:00
|
|
|
|
|
2014-05-19 22:41:59 +00:00
|
|
|
|
(void (pretty-print-matcher* (matcher-intersect (pattern->matcher SA (list 'a))
|
|
|
|
|
(pattern->matcher SB (list 'b)))))
|
2014-05-07 23:23:11 +00:00
|
|
|
|
|
2014-05-19 22:41:59 +00:00
|
|
|
|
(let ((r1 (matcher-union (pattern->matcher SA (list ? 'b))
|
|
|
|
|
(pattern->matcher SA (list ? 'c))))
|
|
|
|
|
(r2 (matcher-union (pattern->matcher SB (list 'a ?))
|
|
|
|
|
(pattern->matcher SB (list 'b ?)))))
|
2014-05-14 04:01:43 +00:00
|
|
|
|
(pretty-print-matcher* (matcher-union r1 r2))
|
|
|
|
|
(pretty-print-matcher* (matcher-union r1 r1))
|
|
|
|
|
(pretty-print-matcher* (matcher-union r2 r2))
|
|
|
|
|
(pretty-print-matcher* (matcher-intersect r1 r2))
|
|
|
|
|
(pretty-print-matcher* (matcher-intersect r1 r1))
|
|
|
|
|
(pretty-print-matcher* (matcher-intersect r2 r2))
|
2014-05-01 10:46:22 +00:00
|
|
|
|
(void))
|
|
|
|
|
|
2014-05-19 22:41:59 +00:00
|
|
|
|
(void (pretty-print-matcher* (matcher-intersect (bigdemo) (pattern->matcher SX (list 'm 'n)))))
|
2014-05-01 10:46:22 +00:00
|
|
|
|
|
|
|
|
|
(check-matches
|
2014-05-19 22:41:59 +00:00
|
|
|
|
(pretty-print-matcher* (matcher-intersect (bigdemo) (pattern->matcher SX (list 'Z ?))))
|
2014-05-01 10:46:22 +00:00
|
|
|
|
(list 'a '-) ""
|
|
|
|
|
(list 'Z '-) "XZ"
|
|
|
|
|
(list '? '-) ""
|
|
|
|
|
(list 'a (list '- '- '-)) ""
|
|
|
|
|
(list 'a (list '- '- '- '- '- '- '- '- '- '- '- '- '- '- '- '- '- '- '- '- '- '-)) ""
|
|
|
|
|
(list 'Z) ""
|
|
|
|
|
(list 'Z 'x) "XZ"
|
|
|
|
|
(list 'Z (list)) "XZ"
|
|
|
|
|
(list 'Z (list '-)) "XZ"
|
|
|
|
|
(list 'Z (list '-)) "XZ"
|
|
|
|
|
(list 'Z (list '- '-)) "XZ"
|
|
|
|
|
(list 'Z (list '- '- '-)) "XZ+"
|
|
|
|
|
(list 'Z (list '- '- '- '-)) "XZ"
|
|
|
|
|
(list 'Z (list '- '- '- '- '- '- '- '- '- '- '- '- '- '- '- '- '- '- '- '- '- '-)) "XZ"
|
|
|
|
|
(list 'Z '((()) - -)) "XZ+"
|
|
|
|
|
(list '? (list '- '- '-)) "")
|
|
|
|
|
|
2014-05-08 02:00:46 +00:00
|
|
|
|
(check-matches
|
2014-05-22 03:20:49 +00:00
|
|
|
|
(pretty-print-matcher* (parameterize ((matcher-intersect-successes (lambda (a b) b)))
|
|
|
|
|
(matcher-intersect (bigdemo) (pattern->matcher SX (list 'Z ?)))))
|
2014-05-08 02:00:46 +00:00
|
|
|
|
(list 'a '-) ""
|
|
|
|
|
(list 'Z '-) "X"
|
|
|
|
|
(list '? '-) ""
|
|
|
|
|
(list 'a (list '- '- '-)) ""
|
|
|
|
|
(list 'a (list '- '- '- '- '- '- '- '- '- '- '- '- '- '- '- '- '- '- '- '- '- '-)) ""
|
|
|
|
|
(list 'Z) ""
|
|
|
|
|
(list 'Z 'x) "X"
|
|
|
|
|
(list 'Z (list)) "X"
|
|
|
|
|
(list 'Z (list '-)) "X"
|
|
|
|
|
(list 'Z (list '-)) "X"
|
|
|
|
|
(list 'Z (list '- '-)) "X"
|
|
|
|
|
(list 'Z (list '- '- '-)) "X"
|
|
|
|
|
(list 'Z (list '- '- '- '-)) "X"
|
|
|
|
|
(list 'Z (list '- '- '- '- '- '- '- '- '- '- '- '- '- '- '- '- '- '- '- '- '- '-)) "X"
|
|
|
|
|
(list 'Z '((()) - -)) "X"
|
|
|
|
|
(list '? (list '- '- '-)) "")
|
|
|
|
|
|
|
|
|
|
(check-matches
|
2014-05-22 03:20:49 +00:00
|
|
|
|
(pretty-print-matcher* (parameterize ((matcher-intersect-successes (lambda (a b) b)))
|
|
|
|
|
(matcher-intersect (bigdemo) (pattern->matcher SX ?))))
|
2014-05-08 02:00:46 +00:00
|
|
|
|
(list 'a '-) "X"
|
|
|
|
|
(list 'Z '-) "X"
|
|
|
|
|
(list '? '-) ""
|
|
|
|
|
(list 'a (list '- '- '-)) "X"
|
|
|
|
|
(list 'a (list '- '- '- '- '- '- '- '- '- '- '- '- '- '- '- '- '- '- '- '- '- '-)) "X"
|
|
|
|
|
(list 'Z) ""
|
|
|
|
|
(list 'Z 'x) "X"
|
|
|
|
|
(list 'Z (list)) "X"
|
|
|
|
|
(list 'Z (list '-)) "X"
|
|
|
|
|
(list 'Z (list '-)) "X"
|
|
|
|
|
(list 'Z (list '- '-)) "X"
|
|
|
|
|
(list 'Z (list '- '- '-)) "X"
|
|
|
|
|
(list 'Z (list '- '- '- '-)) "X"
|
|
|
|
|
(list 'Z (list '- '- '- '- '- '- '- '- '- '- '- '- '- '- '- '- '- '- '- '- '- '-)) "X"
|
|
|
|
|
(list 'Z '((()) - -)) "X"
|
|
|
|
|
(list '? (list '- '- '-)) "")
|
|
|
|
|
|
2014-05-19 22:41:59 +00:00
|
|
|
|
(let* ((r1 (pattern->matcher SA (list ? 'b)))
|
|
|
|
|
(r2 (pattern->matcher SB (list 'a ?)))
|
2014-05-07 23:23:11 +00:00
|
|
|
|
(r12 (matcher-union r1 r2)))
|
2014-05-01 11:36:56 +00:00
|
|
|
|
(printf "\n-=-=-=-=-=-=-=-=- erase1\n")
|
2014-05-14 04:01:43 +00:00
|
|
|
|
(pretty-print-matcher* r1)
|
|
|
|
|
(pretty-print-matcher* r2)
|
|
|
|
|
(pretty-print-matcher* r12)
|
2014-05-19 22:41:59 +00:00
|
|
|
|
(pretty-print-matcher* (matcher-erase-path r12 r1))
|
|
|
|
|
(pretty-print-matcher* (matcher-erase-path r12 r2))
|
2014-05-01 11:36:56 +00:00
|
|
|
|
(void))
|
|
|
|
|
|
2014-05-19 22:41:59 +00:00
|
|
|
|
(let* ((r1 (matcher-union (pattern->matcher SA (list 'a ?))
|
|
|
|
|
(pattern->matcher SA (list 'b ?))))
|
|
|
|
|
(r2 (pattern->matcher SB (list 'b ?)))
|
2014-05-07 23:23:11 +00:00
|
|
|
|
(r12 (matcher-union r1 r2)))
|
2014-05-01 11:36:56 +00:00
|
|
|
|
(printf "\n-=-=-=-=-=-=-=-=- erase2\n")
|
2014-05-14 04:01:43 +00:00
|
|
|
|
(pretty-print-matcher* r12)
|
2014-05-19 22:41:59 +00:00
|
|
|
|
(pretty-print-matcher* (matcher-erase-path r12 r1))
|
|
|
|
|
(pretty-print-matcher* (matcher-erase-path r12 r2))
|
2014-05-01 11:36:56 +00:00
|
|
|
|
(void))
|
|
|
|
|
|
2014-05-01 18:05:28 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
(module+ test
|
|
|
|
|
(struct a (x) #:prefab)
|
|
|
|
|
(struct b (x) #:transparent)
|
|
|
|
|
|
|
|
|
|
(define (intersect a b)
|
2014-05-19 22:41:59 +00:00
|
|
|
|
(matcher-intersect (pattern->matcher SA a)
|
|
|
|
|
(pattern->matcher SB b)))
|
2014-05-01 18:05:28 +00:00
|
|
|
|
|
2014-05-19 22:41:59 +00:00
|
|
|
|
(define EAB (E (set 'A 'B)))
|
2014-05-01 18:05:28 +00:00
|
|
|
|
|
2014-05-22 23:13:02 +00:00
|
|
|
|
(define (rseq* x . xs)
|
|
|
|
|
(let walk ((xs (cons x xs)))
|
|
|
|
|
(match xs
|
|
|
|
|
[(list r) r]
|
|
|
|
|
[(cons e xs1) (rseq e (walk xs1))])))
|
|
|
|
|
|
2014-07-16 18:13:45 +00:00
|
|
|
|
(define (check-requal? actual expected)
|
|
|
|
|
(check-eq? actual expected))
|
|
|
|
|
|
|
|
|
|
(check-requal? (intersect ? ?) (rwild EAB))
|
|
|
|
|
(check-requal? (intersect 'a ?) (rseq 'a EAB))
|
|
|
|
|
(check-requal? (intersect 123 ?) (rseq 123 EAB))
|
|
|
|
|
(check-requal? (intersect (cons ? 2) (cons 1 ?)) (rseq* SOL 1 ILM 2 EOS EAB))
|
|
|
|
|
(check-requal? (intersect (list ? 2) (list 1 ?)) (rseq* SOL 1 2 EOS EAB))
|
|
|
|
|
(check-requal? (intersect (cons 1 2) ?) (rseq* SOL 1 ILM 2 EOS EAB))
|
|
|
|
|
(check-requal? (intersect (list 1 2) ?) (rseq* SOL 1 2 EOS EAB))
|
|
|
|
|
(check-requal? (intersect 1 2) #f)
|
|
|
|
|
(check-requal? (intersect (cons 1 2) (cons ? 2)) (rseq* SOL 1 ILM 2 EOS EAB))
|
|
|
|
|
(check-requal? (intersect (list 1 2) (list ? 2)) (rseq* SOL 1 2 EOS EAB))
|
|
|
|
|
(check-requal? (intersect (cons 1 2) (cons 3 2)) #f)
|
|
|
|
|
(check-requal? (intersect (cons 1 2) (cons 1 3)) #f)
|
|
|
|
|
(check-requal? (intersect (vector 1 2) (vector 1 2)) (rseq* SOV 1 2 EOS EAB))
|
|
|
|
|
(check-requal? (intersect (vector 1 2) (vector 1 2 3)) #f)
|
|
|
|
|
|
|
|
|
|
(check-requal? (intersect (a 'a) (a 'b)) #f)
|
|
|
|
|
(check-requal? (intersect (a 'a) (a 'a)) (rseq* struct:a 'a EOS EAB))
|
|
|
|
|
(check-requal? (intersect (a 'a) (a ?)) (rseq* struct:a 'a EOS EAB))
|
|
|
|
|
(check-requal? (intersect (a 'a) ?) (rseq* struct:a 'a EOS EAB))
|
|
|
|
|
(check-requal? (intersect (b 'a) (b 'b)) #f)
|
|
|
|
|
(check-requal? (intersect (b 'a) (b 'a)) (rseq* struct:b 'a EOS EAB))
|
|
|
|
|
(check-requal? (intersect (b 'a) (b ?)) (rseq* struct:b 'a EOS EAB))
|
|
|
|
|
(check-requal? (intersect (b 'a) ?) (rseq* struct:b 'a EOS EAB))
|
|
|
|
|
|
|
|
|
|
(check-requal? (intersect (a 'a) (b 'a)) #f)
|
2014-05-01 18:06:25 +00:00
|
|
|
|
|
2014-05-01 18:05:28 +00:00
|
|
|
|
(check-exn #px"Cannot match on hash tables at present"
|
|
|
|
|
(lambda ()
|
2014-07-16 18:13:45 +00:00
|
|
|
|
(intersect (canonicalize (hash 'a 1 'b ?))
|
|
|
|
|
(canonicalize (hash 'a ? 'b 2)))))
|
|
|
|
|
;; (check-requal? (intersect (rseq 'a 1 'b ?) (rseq 'a ? 'b 2)) (rseq 'a 1 'b 2))
|
|
|
|
|
;; (check-requal? (intersect (rseq 'a 1 'b ?) (rseq 'a ?)) (void))
|
|
|
|
|
;; (check-requal? (intersect (rseq 'a 1 'b ?) (rseq 'a 1 'b ?)) (rseq 'a 1 'b ?))
|
|
|
|
|
;; (check-requal? (intersect (rseq 'a 1 'b ?) (rseq 'a ? 'c ?)) (void))
|
|
|
|
|
|
|
|
|
|
;; (check-requal? (intersect (rseq 'a 1 'b ?) (rseq 'a 1 'b (list 2 ?)))
|
|
|
|
|
;; (rseq 'a 1 'b (list 2 ?)))
|
|
|
|
|
;; (check-requal? (intersect (rseq 'a 1 'b (list ? 3)) (rseq 'a 1 'b (list 2 ?)))
|
|
|
|
|
;; (rseq 'a 1 'b (list 2 3)))
|
|
|
|
|
|
|
|
|
|
(let ((H rseq-multi))
|
|
|
|
|
(newline)
|
|
|
|
|
(printf "Checking that intersection with wildcard is identity-like\n")
|
|
|
|
|
(define m1 (pretty-print-matcher*
|
|
|
|
|
(foldr matcher-union (matcher-empty)
|
|
|
|
|
(list (pattern->matcher SA (list 'a ?))
|
|
|
|
|
(pattern->matcher SB (list 'b ?))
|
|
|
|
|
(pattern->matcher SC (list 'b 'c))))))
|
|
|
|
|
(define m2 (pretty-print-matcher* (pattern->matcher SD ?)))
|
|
|
|
|
(define mi (pretty-print-matcher* (matcher-intersect m1 m2)))
|
|
|
|
|
(check-requal? mi
|
|
|
|
|
(H SOL (H 'a (H ? (H EOS (E (set 'A 'D))))
|
|
|
|
|
'b (H ? (H EOS (E (set 'B 'D)))
|
|
|
|
|
'c (H EOS (E (set 'B 'C 'D)))))))
|
|
|
|
|
(check-requal? (pretty-print-matcher*
|
|
|
|
|
(parameterize ((matcher-intersect-successes (lambda (v1 v2) v1)))
|
|
|
|
|
(matcher-intersect m1 m2)))
|
|
|
|
|
m1))
|
|
|
|
|
)
|
2014-05-10 23:27:17 +00:00
|
|
|
|
|
|
|
|
|
(module+ test
|
2014-05-14 20:29:02 +00:00
|
|
|
|
(define (matcher-match-matcher-list m1 m2)
|
2014-05-21 01:50:19 +00:00
|
|
|
|
(match-define (cons s1 s2) (matcher-match-matcher m1 m2))
|
2014-05-14 20:29:02 +00:00
|
|
|
|
(list s1 s2))
|
2014-05-10 23:27:17 +00:00
|
|
|
|
(let ((abc (foldr matcher-union (matcher-empty)
|
2014-05-19 22:41:59 +00:00
|
|
|
|
(list (pattern->matcher SA (list 'a ?))
|
|
|
|
|
(pattern->matcher SB (list 'b ?))
|
|
|
|
|
(pattern->matcher SC (list 'c ?)))))
|
2014-05-10 23:27:17 +00:00
|
|
|
|
(bcd (foldr matcher-union (matcher-empty)
|
2014-05-19 22:41:59 +00:00
|
|
|
|
(list (pattern->matcher SB (list 'b ?))
|
|
|
|
|
(pattern->matcher SC (list 'c ?))
|
|
|
|
|
(pattern->matcher SD (list 'd ?))))))
|
2014-05-14 20:29:02 +00:00
|
|
|
|
(check-equal? (matcher-match-matcher-list abc abc)
|
|
|
|
|
(list (set 'A 'B 'C) (set 'A 'B 'C)))
|
2014-05-22 03:20:49 +00:00
|
|
|
|
(check-equal? (parameterize ((matcher-match-matcher-successes (lambda (v1 v2 a)
|
|
|
|
|
(set-union v2 a)))
|
|
|
|
|
(matcher-match-matcher-unit (set)))
|
|
|
|
|
(matcher-match-matcher abc abc))
|
2014-05-21 01:50:19 +00:00
|
|
|
|
(set 'A 'B 'C))
|
2014-05-14 20:29:02 +00:00
|
|
|
|
(check-equal? (matcher-match-matcher-list abc (matcher-relabel bcd (lambda (old) (set #t))))
|
|
|
|
|
(list (set 'B 'C) (set #t)))
|
2014-05-19 22:41:59 +00:00
|
|
|
|
(check-equal? (matcher-match-matcher-list abc (pattern->matcher Sfoo ?))
|
2014-05-14 20:29:02 +00:00
|
|
|
|
(list (set 'A 'B 'C) (set 'foo)))
|
2014-05-19 22:41:59 +00:00
|
|
|
|
(check-equal? (matcher-match-matcher-list abc (pattern->matcher Sfoo (list ? ?)))
|
2014-05-14 20:29:02 +00:00
|
|
|
|
(list (set 'A 'B 'C) (set 'foo)))
|
2014-05-19 22:41:59 +00:00
|
|
|
|
(check-equal? (matcher-match-matcher-list abc (pattern->matcher Sfoo (list ? 'x)))
|
2014-05-14 20:29:02 +00:00
|
|
|
|
(list (set 'A 'B 'C) (set 'foo)))
|
2014-05-19 22:41:59 +00:00
|
|
|
|
(check-equal? (matcher-match-matcher-list abc (pattern->matcher Sfoo (list ? 'x ?)))
|
2014-05-14 20:29:02 +00:00
|
|
|
|
(list (set) (set)))))
|
2014-05-13 23:35:17 +00:00
|
|
|
|
|
|
|
|
|
(module+ test
|
2014-05-28 19:24:26 +00:00
|
|
|
|
(check-equal? (compile-projection (cons 'a 'b))
|
|
|
|
|
(list SOL 'a ILM 'b EOS EOS))
|
|
|
|
|
(check-equal? (compile-projection (cons 'a (?!)))
|
|
|
|
|
(list SOL 'a ILM SOC ? EOC EOS EOS))
|
2014-05-13 23:35:17 +00:00
|
|
|
|
(check-equal? (compile-projection (list 'a 'b))
|
2014-05-28 19:24:26 +00:00
|
|
|
|
(list SOL 'a 'b EOS EOS))
|
2014-05-22 00:54:36 +00:00
|
|
|
|
(check-equal? (compile-projection (list 'a (?!)))
|
2014-05-28 19:24:26 +00:00
|
|
|
|
(list SOL 'a SOC ? EOC EOS EOS))
|
2014-05-13 23:35:17 +00:00
|
|
|
|
|
2014-05-22 03:20:49 +00:00
|
|
|
|
(parameterize ((matcher-project-success (lambda (v) #t)))
|
2014-07-16 18:13:45 +00:00
|
|
|
|
(check-requal? (matcher-project (matcher-union (pattern->matcher SA (list 'a 'a))
|
|
|
|
|
(pattern->matcher SB (list 'a 'b)))
|
|
|
|
|
(compile-projection (list 'a (?!))))
|
|
|
|
|
(matcher-union (pattern->matcher #t 'a)
|
|
|
|
|
(pattern->matcher #t 'b)))
|
|
|
|
|
|
|
|
|
|
(check-requal? (matcher-project (matcher-union (pattern->matcher SA (list 'a 'a))
|
|
|
|
|
(pattern->matcher SB (list 'a (vector 'b 'c 'd))))
|
|
|
|
|
(compile-projection (list 'a (?!))))
|
|
|
|
|
(matcher-union (pattern->matcher #t 'a)
|
|
|
|
|
(pattern->matcher #t (vector 'b 'c 'd))))
|
|
|
|
|
|
|
|
|
|
(check-requal? (matcher-project (matcher-union (pattern->matcher SA (list 'a 'a))
|
|
|
|
|
(pattern->matcher SB (list 'a (vector 'b ? 'd))))
|
|
|
|
|
(compile-projection (list 'a (?!))))
|
|
|
|
|
(matcher-union (pattern->matcher #t 'a)
|
|
|
|
|
(pattern->matcher #t (vector 'b ? 'd))))
|
2014-05-22 03:20:49 +00:00
|
|
|
|
|
|
|
|
|
(check-equal? (matcher-key-set
|
|
|
|
|
(matcher-project (matcher-union (pattern->matcher SA (list 'a 'a))
|
|
|
|
|
(pattern->matcher SB (list 'a 'b)))
|
|
|
|
|
(compile-projection (list 'a (?!)))))
|
2014-05-29 00:10:55 +00:00
|
|
|
|
(set '(a) '(b)))
|
2014-05-22 03:20:49 +00:00
|
|
|
|
|
|
|
|
|
(check-equal? (matcher-key-set
|
|
|
|
|
(matcher-project (matcher-union (pattern->matcher SA (list 'a 'a))
|
|
|
|
|
(pattern->matcher SB (list 'a (vector 'b 'c 'd))))
|
|
|
|
|
(compile-projection (list 'a (?!)))))
|
2014-05-29 00:10:55 +00:00
|
|
|
|
(set '(a) '(#(b c d))))
|
2014-05-22 03:20:49 +00:00
|
|
|
|
|
|
|
|
|
(check-equal? (matcher-key-set
|
|
|
|
|
(matcher-project (matcher-union (pattern->matcher SA (list 'a 'a))
|
|
|
|
|
(pattern->matcher SB (list 'a (vector 'b ? 'd))))
|
|
|
|
|
(compile-projection (list 'a (?!)))))
|
|
|
|
|
#f)
|
|
|
|
|
|
|
|
|
|
(check-equal? (matcher-key-set
|
|
|
|
|
(matcher-project (matcher-union (pattern->matcher SA (list 'a 'a))
|
|
|
|
|
(pattern->matcher SB (list 'a (vector 'b ? 'd))))
|
|
|
|
|
(compile-projection (list 'a (?! 'a)))))
|
2014-05-29 00:10:55 +00:00
|
|
|
|
(set '(a)))
|
2014-05-22 03:20:49 +00:00
|
|
|
|
|
2014-07-16 18:13:45 +00:00
|
|
|
|
(check-requal? (matcher-project (matcher-union (pattern->matcher SA (cons 1 2))
|
|
|
|
|
(pattern->matcher SB (cons 3 4)))
|
|
|
|
|
(compile-projection (cons (?!) (?!))))
|
|
|
|
|
(matcher-union (pattern->matcher #t 1 2)
|
|
|
|
|
(pattern->matcher #t 3 4)))
|
|
|
|
|
|
|
|
|
|
(check-requal? (matcher-project (foldr matcher-union (matcher-empty)
|
|
|
|
|
(list (pattern->matcher SA (cons 1 2))
|
|
|
|
|
(pattern->matcher SB (cons 1 4))
|
|
|
|
|
(pattern->matcher SC (cons 3 4))))
|
|
|
|
|
(compile-projection (cons (?!) (?!))))
|
|
|
|
|
(foldr matcher-union (matcher-empty)
|
|
|
|
|
(list (pattern->matcher #t 1 2)
|
|
|
|
|
(pattern->matcher #t 1 4)
|
|
|
|
|
(pattern->matcher #t 3 4))))
|
|
|
|
|
|
|
|
|
|
(check-requal? (matcher-project (foldr matcher-union (matcher-empty)
|
|
|
|
|
(list (pattern->matcher SA (cons 1 2))
|
|
|
|
|
(pattern->matcher SB (cons 1 4))
|
|
|
|
|
(pattern->matcher SC (cons 3 4))))
|
|
|
|
|
(compile-projection (?! (cons ? ?))))
|
|
|
|
|
(foldr matcher-union (matcher-empty)
|
|
|
|
|
(list (pattern->matcher #t (cons 1 2))
|
|
|
|
|
(pattern->matcher #t (cons 1 4))
|
|
|
|
|
(pattern->matcher #t (cons 3 4)))))
|
|
|
|
|
|
|
|
|
|
(check-requal? (matcher-project (foldr matcher-union (matcher-empty)
|
|
|
|
|
(list (pattern->matcher SA (cons 1 2))
|
|
|
|
|
(pattern->matcher SB (cons 1 4))
|
|
|
|
|
(pattern->matcher SC (cons 3 4))))
|
|
|
|
|
(compile-projection (?! (cons 1 ?))))
|
|
|
|
|
(foldr matcher-union (matcher-empty)
|
|
|
|
|
(list (pattern->matcher #t (cons 1 2))
|
|
|
|
|
(pattern->matcher #t (cons 1 4)))))
|
|
|
|
|
|
|
|
|
|
(check-requal? (matcher-project (foldr matcher-union (matcher-empty)
|
|
|
|
|
(list (pattern->matcher SA (cons 1 2))
|
|
|
|
|
(pattern->matcher SB (cons 1 4))
|
|
|
|
|
(pattern->matcher SC (cons 3 4))))
|
|
|
|
|
(compile-projection (cons (?! 1) (?!))))
|
|
|
|
|
(foldr matcher-union (matcher-empty)
|
|
|
|
|
(list (pattern->matcher #t 1 2)
|
|
|
|
|
(pattern->matcher #t 1 4))))
|
|
|
|
|
|
|
|
|
|
(check-requal? (matcher-project (foldr matcher-union (matcher-empty)
|
|
|
|
|
(list (pattern->matcher SA (cons 1 2))
|
|
|
|
|
(pattern->matcher SB (cons 1 4))
|
|
|
|
|
(pattern->matcher SC (cons 3 4))))
|
|
|
|
|
(compile-projection (cons (?!) (?! 4))))
|
|
|
|
|
(foldr matcher-union (matcher-empty)
|
|
|
|
|
(list (pattern->matcher #t 1 4)
|
|
|
|
|
(pattern->matcher #t 3 4))))
|
2014-05-22 03:20:49 +00:00
|
|
|
|
|
2014-05-25 17:22:49 +00:00
|
|
|
|
(check-equal? (matcher-key-set
|
|
|
|
|
(matcher-project (foldr matcher-union (matcher-empty)
|
|
|
|
|
(list (pattern->matcher SA (cons 1 2))
|
|
|
|
|
(pattern->matcher SC (cons ? 3))
|
|
|
|
|
(pattern->matcher SB (cons 3 4))))
|
|
|
|
|
(compile-projection (cons (?!) (?!)))))
|
|
|
|
|
#f)
|
|
|
|
|
|
|
|
|
|
(check-equal? (matcher-key-set
|
|
|
|
|
(matcher-project (foldr matcher-union (matcher-empty)
|
|
|
|
|
(list (pattern->matcher SA (cons ? 2))
|
|
|
|
|
(pattern->matcher SC (cons 1 3))
|
|
|
|
|
(pattern->matcher SB (cons 3 4))))
|
|
|
|
|
(compile-projection (cons ? (?!)))))
|
2014-05-29 00:10:55 +00:00
|
|
|
|
(set '(2) '(3) '(4)))
|
2014-05-25 17:22:49 +00:00
|
|
|
|
|
2014-05-22 03:20:49 +00:00
|
|
|
|
(check-equal? (matcher-key-set
|
|
|
|
|
(matcher-project (matcher-union (pattern->matcher SA (cons 1 2))
|
|
|
|
|
(pattern->matcher SB (cons 3 4)))
|
|
|
|
|
(compile-projection (cons (?!) (?!)))))
|
2014-05-29 00:10:55 +00:00
|
|
|
|
(set '(1 2) '(3 4))))
|
2014-05-22 01:02:38 +00:00
|
|
|
|
|
2014-07-16 18:13:45 +00:00
|
|
|
|
(check-requal? (matcher-project (matcher-union (pattern->matcher SA ?)
|
2014-05-23 02:34:24 +00:00
|
|
|
|
(pattern->matcher SB (list 'a)))
|
2014-07-16 18:13:45 +00:00
|
|
|
|
(compile-projection (?! (list (list ?)))))
|
|
|
|
|
(pattern->matcher SA (list (list ?))))
|
2014-05-23 02:34:24 +00:00
|
|
|
|
|
2014-05-22 01:02:38 +00:00
|
|
|
|
(check-equal? (projection->pattern (list 'a 'b)) (list 'a 'b))
|
|
|
|
|
(check-equal? (projection->pattern (list 'a ?)) (list 'a ?))
|
|
|
|
|
(check-equal? (projection->pattern (list 'a (?!))) (list 'a ?))
|
|
|
|
|
(check-equal? (projection->pattern (list 'a (?! 'b))) (list 'a 'b))
|
|
|
|
|
(check-equal? (projection->pattern (list 'a (?! (vector 'b)))) (list 'a (vector 'b)))
|
|
|
|
|
(check-equal? (projection->pattern (list 'a (?! (vector ? ?)))) (list 'a (vector ? ?)))
|
2014-05-25 17:22:49 +00:00
|
|
|
|
)
|
2014-05-26 10:22:35 +00:00
|
|
|
|
|
|
|
|
|
(module+ test
|
|
|
|
|
(let ((A (pattern->matcher SA ?))
|
|
|
|
|
(B (pattern->matcher SB (list (list (list (list 'foo)))))))
|
2014-07-16 18:13:45 +00:00
|
|
|
|
(check-requal? (pretty-print-matcher* (matcher-erase-path (matcher-union A B) B))
|
|
|
|
|
A))
|
2014-05-26 10:22:35 +00:00
|
|
|
|
(let ((A (pattern->matcher SA ?))
|
|
|
|
|
(B (matcher-union (pattern->matcher SB (list (list (list (list 'foo)))))
|
|
|
|
|
(pattern->matcher SB (list (list (list (list 'bar))))))))
|
2014-07-16 18:13:45 +00:00
|
|
|
|
(check-requal? (pretty-print-matcher* (matcher-erase-path (matcher-union A B) B))
|
|
|
|
|
A))
|
2014-05-26 10:29:06 +00:00
|
|
|
|
(let ((A (pattern->matcher SA ?))
|
|
|
|
|
(B (matcher-union (pattern->matcher SB (list (list (list (list 'foo)))))
|
|
|
|
|
(pattern->matcher SB (list (list (list (list 'bar))))))))
|
2014-07-16 18:13:45 +00:00
|
|
|
|
(check-requal? (pretty-print-matcher* (matcher-erase-path (matcher-union A B) A))
|
|
|
|
|
B)))
|
2014-05-28 19:48:37 +00:00
|
|
|
|
|
|
|
|
|
(module+ test
|
|
|
|
|
(let ((M (foldr matcher-union (matcher-empty)
|
|
|
|
|
(list (pattern->matcher SA (list ? 2))
|
|
|
|
|
(pattern->matcher SC (list 1 3))
|
|
|
|
|
(pattern->matcher SD (list ? 3))
|
|
|
|
|
(pattern->matcher SB (list 3 4)))))
|
|
|
|
|
(S '((("(")
|
|
|
|
|
((("__") ((2 (((")") (((")") ("" ("A")))))))
|
|
|
|
|
(3 (((")") (((")") ("" ("D")))))))))
|
|
|
|
|
(1 ((2 (((")") (((")") ("" ("A")))))))
|
|
|
|
|
(3 (((")") (((")") ("" ("D" "C")))))))))
|
|
|
|
|
(3 ((2 (((")") (((")") ("" ("A")))))))
|
|
|
|
|
(3 (((")") (((")") ("" ("D")))))))
|
|
|
|
|
(4 (((")") (((")") ("" ("B"))))))))))))))
|
|
|
|
|
(check-equal? (matcher->jsexpr M (lambda (v) (map symbol->string (set->list v)))) S)
|
2014-07-16 18:13:45 +00:00
|
|
|
|
(check-requal? (jsexpr->matcher S (lambda (v) (list->set (map string->symbol v)))) M)))
|
2014-05-28 21:11:36 +00:00
|
|
|
|
|
|
|
|
|
(module+ test
|
2014-07-16 18:13:45 +00:00
|
|
|
|
(check-requal? (pretty-print-matcher*
|
|
|
|
|
(pattern->matcher SA (list 1
|
|
|
|
|
(embedded-matcher
|
|
|
|
|
(pattern->matcher SB (list 2 3)))
|
|
|
|
|
4)))
|
|
|
|
|
(pattern->matcher SA (list 1 (list 2 3) 4)))
|
|
|
|
|
|
|
|
|
|
(check-requal? (pretty-print-matcher*
|
|
|
|
|
(pattern->matcher SA
|
|
|
|
|
(list (embedded-matcher (pattern->matcher SB (list 1 2)))
|
|
|
|
|
(embedded-matcher (pattern->matcher SC (list 3 4))))))
|
|
|
|
|
(pattern->matcher SA (list (list 1 2) (list 3 4)))))
|