orc.ast.extended.pattern
Class Pattern

java.lang.Object
  extended by orc.ast.extended.pattern.Pattern
All Implemented Interfaces:
ASTNode, Locatable, Located
Direct Known Subclasses:
AsPattern, CallPattern, ConsPattern, EqPattern, ListPattern, LiteralPattern, NilPattern, TuplePattern, TypedPattern, VariablePattern, WildcardPattern

public abstract class Pattern
extends java.lang.Object
implements ASTNode, Locatable

Base interface for the abstract syntax of patterns. Patterns exist only in the extended abstract syntax. They desugar into a series of operations which terminate in variable bindings.

Author:
dkitchin

Field Summary
protected static Argument EQUAL
           
static Argument ERROR
           
protected static Argument IF
           
protected static Argument NONE
           
protected static Argument SOME
           
protected static Argument TRYCONS
           
protected static Argument TRYNIL
           
static Argument TRYNONE
           
static Argument TRYSOME
           
 
Constructor Summary
Pattern()
           
 
Method Summary
static Expression caseof(Variable arg, Variable s, Expression succ, Expression fail)
          Constructs an optional case statement.
static Expression compare(Argument s, Argument t)
          Construct an expression comparing two arguments.
static Pattern condense(java.util.List<Pattern> ps)
          Condense a sequence of patterns into a single pattern using the following strategy: An empty sequence of patterns becomes a wildcard pattern _.
static Expression fail()
          Return a default expression to use in case a pattern match fails.
 SourceLocation getSourceLocation()
           
static Expression lift(Variable x)
          Lifts a partial function to a total function, using the ; combinator to detect a refusal to respond, and publishing optional values instead of values.
static Expression nth(Argument s, int i)
          Construct an expression which publishes the ith element of tuple s.
 PatternSimplifier process(Variable fragment)
          A different entry point for process, taking only a source variable.
abstract  void process(Variable fragment, PatternSimplifier visitor)
          Visit a pattern recursively, creating two products: An expression that will examine a value to determine whether it matches a pattern, building an output tuple of all value fragments which will be bound to variables.
 void setSourceLocation(SourceLocation location)
           
 boolean strict()
           
static Expression trycons(Argument s)
          Constructs an expression which will try to deconstruct an argument as if it were a list.
static Expression trynil(Argument s)
          Constructs an expression which tests whether the argument s can be treated as an empty list (nil).
static Expression trysize(Argument s, int n)
          Construct an expression to determine whether the argument s may be viewed as a tuple of size n.
static Expression unapply(Argument m, Argument s)
          Construct an expression which tries to find the inverse of the site m, and apply it to s.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface orc.ast.extended.ASTNode
accept
 

Field Detail

IF

protected static Argument IF

EQUAL

protected static Argument EQUAL

SOME

protected static Argument SOME

NONE

protected static Argument NONE

ERROR

public static Argument ERROR

TRYSOME

public static Argument TRYSOME

TRYNONE

public static Argument TRYNONE

TRYCONS

protected static Argument TRYCONS

TRYNIL

protected static Argument TRYNIL
Constructor Detail

Pattern

public Pattern()
Method Detail

strict

public boolean strict()

process

public abstract void process(Variable fragment,
                             PatternSimplifier visitor)
                      throws PatternException
Visit a pattern recursively, creating two products: An expression that will examine a value to determine whether it matches a pattern, building an output tuple of all value fragments which will be bound to variables. An expression transformer that will examine such an output tuple and bind its elements to variables in a given expression.

Parameters:
fragment - A variable holding the current fragment of the value to be matched
visitor - A visitor object which accumulates an expression and a transformer
Throws:
PatternException

process

public PatternSimplifier process(Variable fragment)
                          throws PatternException
A different entry point for process, taking only a source variable. Creates a new visitor, visits the pattern, and then returns that visitor.

Throws:
PatternException

condense

public static Pattern condense(java.util.List<Pattern> ps)
Condense a sequence of patterns into a single pattern using the following strategy: An empty sequence of patterns becomes a wildcard pattern _. A singleton sequence p becomes just that pattern p. A sequence of two or more patterns p1..pn becomes a tuple pattern (p1,...,pn)

Parameters:
ps -
Returns:
condensed Pattern

compare

public static Expression compare(Argument s,
                                 Argument t)
Construct an expression comparing two arguments. The result expression returns a signal if the arguments are equal, and remains silent otherwise.

Parameters:
s - An argument to compare
t - An argument to compare
Returns:
An expression publishing a signal if s=t, silent otherwise

nth

public static Expression nth(Argument s,
                             int i)
Construct an expression which publishes the ith element of tuple s.

Parameters:
s - An argument bound to a tuple
i - An index into a tuple (starting at 0)
Returns:
An expression publishing s(i)

trycons

public static Expression trycons(Argument s)
Constructs an expression which will try to deconstruct an argument as if it were a list. It publishes (h,t) if the argument s is viewable as a list h:t, and remains silent otherwise.

Parameters:
s -

trynil

public static Expression trynil(Argument s)
Constructs an expression which tests whether the argument s can be treated as an empty list (nil). If so, it publishes a signal; otherwise it remains silent.

Parameters:
s -

trysize

public static Expression trysize(Argument s,
                                 int n)
Construct an expression to determine whether the argument s may be viewed as a tuple of size n. If so, the expression publishes a signal; otherwise it remains silent.

Parameters:
s - Argument to test
n - Target arity

unapply

public static Expression unapply(Argument m,
                                 Argument s)
Construct an expression which tries to find the inverse of the site m, and apply it to s. While the result will depend on the site itself, as a guideline the inverse should obey the following specification: Let i = unapply(m,s). If s = m(x) for some x, then i(s) = x. Otherwise, i(s) remains silent. Currently we find the inverse via a special message.

Parameters:
m - The site to unapply
s - Argument to the inversion
See Also:
Pattern()

lift

public static Expression lift(Variable x)
Lifts a partial function to a total function, using the ; combinator to detect a refusal to respond, and publishing optional values instead of values. some(x) ; none

Parameters:
x -

caseof

public static Expression caseof(Variable arg,
                                Variable s,
                                Expression succ,
                                Expression fail)
Constructs an optional case statement. case arg of some(s) -> succ | none -> fail

Parameters:
arg -
s -
succ -
fail -

fail

public static Expression fail()
Return a default expression to use in case a pattern match fails. Instead of remaining silent, we halt with an error, to aid in debugging bad/non-total patterns.


setSourceLocation

public void setSourceLocation(SourceLocation location)
Specified by:
setSourceLocation in interface Locatable

getSourceLocation

public SourceLocation getSourceLocation()
Specified by:
getSourceLocation in interface ASTNode
Specified by:
getSourceLocation in interface Located