On this page:
redex-match
match?
match-bindings
bind
caching-enabled?
set-cache-size!
check-redudancy

1 Patterns

 (require redex/reduction-semantics)

This section covers Redex’s pattern language, used in many of Redex’s forms.

Note that pattern matching is caching (including caching the results of side-conditions). This means that once a pattern has matched a given term, Redex assumes that it will always match that term.

This is the grammar for the Redex pattern language. Non-terminal references are wrapped with angle brackets; otherwise identifiers in the grammar are terminals.

  pattern = any
  | number
  | natural
  | integer
  | real
  | string
  | variable
  | (variable-except <id> ...)
  | (variable-prefix <id>)
  | variable-not-otherwise-mentioned
  | hole
  | symbol
  | (name <id> <pattern>)
  | (in-hole <pattern> <pattern>)
  | (hide-hole <pattern>)
  | (side-condition <pattern> guard)
  | (cross <id>)
  | (<pattern-sequence> ...)
  | <racket-constant>
     
  pattern-sequence = <pattern>
  | ... ; literal ellipsis
  | ..._id

(redex-match lang pattern any)
(redex-match lang pattern)
If redex-match receives three arguments, it matches the pattern (in the language) against its third argument. If it matches, this returns a list of match structures describing the matches (see match? and match-bindings). If it fails, it returns #f.

If redex-match receives only two arguments, it builds a procedure for efficiently testing if expressions match the pattern, using the language lang. The procedures accepts a single expression and if the expresion matches, it returns a list of match structures describing the matches. If the match fails, the procedure returns #f.

(match? val)  boolean?
  val : any/c
Determines if a value is a match structure.

(match-bindings m)  (listof bind?)
  m : match?
This returns a list of bind structs that binds the pattern variables in this match.

(struct bind (name exp)
  #:extra-constructor-name make-bind)
  name : symbol?
  exp : any/c
Instances of this struct are returned by redex-match. Each bind associates a name with an s-expression from the language, or a list of such s-expressions, if the (name ...) clause is followed by an ellipsis. Nested ellipses produce nested lists.

When this parameter is #t (the default), Redex caches the results of pattern matching and metafunction evaluation. There is a separate cache for each pattern and metafunction; when one fills (see set-cache-size!), Redex evicts all of the entries in that cache.

Caching should be disabled when matching a pattern that depends on values other than the in-scope pattern variables or evaluating a metafunction that reads or writes mutable external state.

(set-cache-size! size)  void?
  size : positive-integer?
Changes the size of the per-pattern and per-metafunction caches.

The default size is 350.

(check-redudancy)  boolean?
(check-redudancy check?)  void?
  check? : boolean?
Ambiguous patterns can slow down Redex’s pattern matching implementation significantly. To help debug such performance issues, set the check-redundancy parameter to #t. This causes Redex to, at runtime, report any redundant matches that it encounters.