class Matcher extends Logger
Matches a goal term against a template term.
- Input: terms template and goal
- Output: substitution solution such that
template ^ solution == goal
, where Term.^ is the method applying the substitution.
Full code example see below.
- Source
- Matcher.scala
In normed vector spaces you might have the template term
||x - y||
and would like to match the goal term||(a - b) - c||
with it.
The matcher will give you the substitutionsolution = [x := (a - b), y := c]
. This is a special case of (typed!) unification. In general, unification seeks a solution substitution such thattemplate solution == goal solution
, i.e. the substitution applied on *both* sides. Thus the template usually contains variables intended for substitution ("matching"), but the goal does not. Still, the goal term may contain free variables as in the example above, they will then be part of the solution substitution - but only in the RHS of the substitutions. No equality relation is taken into account except alpha-conversion of bound variables and solution rules. , scala // Construct the goal and template terms // val namespace = DPath(URI("http://example.com")) val module = namespace ? "myModule" val norm = OMID(module ? "norm") val minus = OMID(module ? "minus") // "||(x - y) - z||" val goal = OMA( norm, List( OMA( minus, List( OMA(minus, List(OMV("x"), OMV("y"))), OMV("z") ) ) ) ) // "||a - b||" val template = OMA( norm, List(OMA( minus, List(OMV("a"), OMV("b")) )) ) // Empty rule set because we don't use any logic foundation // specific typing rules (e.g. the rule in LF for function // application) val matcher = new Matcher(ctrl, new MutableRuleSet) val matchResult = matcher( Context(VarDecl(LocalName("x"), LocalName("y"), LocalName("z"))), goal, Context(VarDecl(LocalName("a"), LocalName("b"))), template ) // MatchSuccess(a:=(http://example.com?myModule?minus x y), b:=z,true)
- Note
The class can be reused for multiple matches using the same RuleSet but is not thread-safe.
,Beware of situations where the template and goal term share some variables. E.g. take template term = "a", goal term = "a - a". Intuitively, this should be a match with the substitution [a := a - a], but this class cannot handle this. The following code will lead to a MatchFail:
As a solution you might want to first make your template variables fresh:scala val matchResult = matcher( Context(VarDecl(LocalName("a"))), OMA(minus, List(OMV("a"), OMV("a"))), Context(VarDecl(LocalName("a"))), OMV("a") )
scala val matchResult = matcher( Context(VarDecl(LocalName("a"))), OMA(minus, List(OMV("a"), OMV("a"))), Context(VarDecl(LocalName("b"))), OMV("b") ) // MatchSuccess(b:=(http://example.com?myModule?minus a a),true) println(matchResult)
- Alphabetic
- By Inheritance
- Matcher
- Logger
- AnyRef
- Any
- Hide All
- Show All
- Public
- All
Instance Constructors
-
new
Matcher(controller: Controller, rules: RuleSet)
- controller
needed for lookups when type checking the matches
- rules
Simplification and other equality-related rules to take into account. Especially, you most probably want to specify the equality of your meta-theory (e.g. LF), e.g., by calling
scala val ctx = Context(mPathToATheory) new Matcher(ctrl, RuleSet.collectRules(ctrl, ctx))
Value Members
-
final
def
!=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
final
def
##(): Int
- Definition Classes
- AnyRef → Any
-
final
def
==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
def
apply(goalContext: Context, queryVars: Context)(doit: ((Term, Term) ⇒ Boolean) ⇒ Boolean): MatchResult
A more general matching function that allows for multiple calls to the equality predicate (in the same context), e.g., to handle \forall queryVars.
A more general matching function that allows for multiple calls to the equality predicate (in the same context), e.g., to handle \forall queryVars. q_1 = g_1 \wedge ... \wedge q_n = g_n.
- doit
a function that takes an equality predicate (for matching) and returns true if the match is possible e.g., basic matching is obtained as apply(queryVars){eq => eq(goal, query)}
-
def
apply(goalContext: Context, goal: Term, queryVars: Context, query: Term): MatchResult
The core matching function for API users.
The core matching function for API users.
- goalContext
the global context
- goal
the term to be matched, relative to goalContext
- queryVars
the variables to solve within the template
query
- query
the template term to match against, relative to goalContext ++ queryVars
- returns
MatchSuccess(subs, true) if
goal == query ^ subs
for
goalContext |- subs:queryVars -> .
-
final
def
asInstanceOf[T0]: T0
- Definition Classes
- Any
-
def
clone(): AnyRef
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( ... ) @native()
-
final
def
eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
def
equals(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
def
finalize(): Unit
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( classOf[java.lang.Throwable] )
-
final
def
getClass(): Class[_]
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
-
def
getSolution: Substitution
- returns
the solution if a match was found this does not provide maps for template variables that did not occur in the template
- def getUnsolvedVariables: List[LocalName]
-
def
hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
-
final
def
isInstanceOf[T0]: Boolean
- Definition Classes
- Any
-
def
log(e: Error): Unit
logs an error - always logged
logs an error - always logged
- Attributes
- protected
- Definition Classes
- Logger
-
def
log(s: ⇒ String, subgroup: Option[String] = None): Unit
logs a message with this logger's logprefix
logs a message with this logger's logprefix
- Attributes
- protected
- Definition Classes
- Logger
-
def
logError(s: ⇒ String): Unit
log as an error message
log as an error message
- Attributes
- protected
- Definition Classes
- Logger
-
def
logGroup[A](a: ⇒ A): A
wraps around a group to create nested logging
wraps around a group to create nested logging
- Attributes
- protected
- Definition Classes
- Logger
- def logPrefix: String
-
def
logTemp(s: ⇒ String): Unit
temporary logging - always logged
temporary logging - always logged
- Attributes
- protected
- Definition Classes
- Logger
-
final
def
ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
final
def
notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
-
final
def
notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
- def report: Report
-
final
def
synchronized[T0](arg0: ⇒ T0): T0
- Definition Classes
- AnyRef
-
def
toString(): String
- Definition Classes
- AnyRef → Any
-
final
def
wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... ) @native()