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

1 Patterns

 (require redex/reduction-semantics)

All of the exports in this section are provided both by redex/reduction-semantics (which includes all non-GUI portions of Redex) and also exported by redex (which includes all of Redex).

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> ...)
  | <scheme-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. 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 bindings structure (see below) that binds the pattern variables in this match.

(struct bind (name exp))
  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.

(set-cache-size! size)  void?
  size : positive-integer?
Changes the cache size; the default size is 350.

The cache is per-pattern (ie, each pattern has a cache of size at most 350 (by default)) and is a simple table that maps expressions to how they matched the pattern (ie, the bindings for the pattern variables). When the cache gets full, it is thrown away and a new cache is started.

This is a parameter that controls whether or not a cache is consulted (and updated) while matching and while evaluating metafunctions.

If it is #t, then side-conditions and the right-hand sides of metafunctions are assumed to only depend on the values of the pattern variables in scope (and thus not on any other external state).

Defaults to #t.