public override bool Matches(Expression E, MatchContext Matched) { return(target.CallMatches(arguments, E, Matched)); }
public override bool Matches(Expression E, MatchContext Matched) { // Move the constants in this pattern to E. IEnumerable <Expression> PTerms = Terms; IEnumerable <Expression> Constants = PTerms.OfType <Constant>(); if (Constants.Any()) { E = Binary.Subtract(E, New(Constants)).Evaluate(); PTerms = PTerms.Except(Constants, RefComparer); } IEnumerable <Expression> ETerms = TermsOf(E); // Try starting the match at each term of the pattern. foreach (Expression p in PTerms) { // Remaining terms of the pattern. Expression P = New(PTerms.ExceptUnique(p, RefComparer)); // If p is a variable, we have to handle the possibility that more than one term of E might match this term. if (p is Variable) { // Check if p has already been matched. If it has, treat it as a constant and match the rest of the terms. Expression matched; if (Matched.TryGetValue(p, out matched)) { // p has already been matched. Remove it out of E and match the remainder of the pattern. if (P.Matches(Binary.Subtract(E, matched).Evaluate(), Matched)) { return(true); } } else { // Try matching p to the various combinations of the terms of E. for (int i = 1; i <= ETerms.Count(); ++i) { foreach (IEnumerable <Expression> e in ETerms.Combinations(i)) { if (Matched.TryMatch(() => p.Matches(New(e), Matched) && P.Matches(New(ETerms.ExceptUnique(e, RefComparer)), Matched))) { return(true); } } } // Try matching p to identity. if (Matched.TryMatch(() => p.Matches(0, Matched) && P.Matches(E, Matched))) { return(true); } } } else { // If p is not a variable, try matching it to any of the terms of E. foreach (Expression e in ETerms) { if (Matched.TryMatch(() => p.Matches(e, Matched) && P.Matches(New(ETerms.ExceptUnique(e, RefComparer)), Matched))) { return(true); } } } } return(false); }
public override bool CallMatches(IEnumerable <Expression> Arguments, Expression E, MatchContext Matched) { return(base.CallMatches(Arguments, E, Matched) || PowHalf.Matches(E, Matched)); }
protected override Expression ApplyTransform(Expression x, MatchContext Matched) { return(result.Substitute(Matched, true).Evaluate()); }
/// <summary> /// Check if a call to this function with the given arguments matches an expression. /// </summary> /// <param name="Arguments"></param> /// <param name="E"></param> /// <param name="Matched"></param> /// <returns></returns> public virtual bool CallMatches(IEnumerable <Expression> Arguments, Expression E, MatchContext Matched) { Call EF = E as Call; if (!ReferenceEquals(EF, null)) { if (!Equals(EF.Target)) { return(false); } if (Arguments.Count() != EF.Arguments.Count()) { return(false); } return(Matched.TryMatch(() => Arguments.Reverse().Zip(EF.Arguments.Reverse(), (p, e) => p.Matches(e, Matched)).All())); } return(false); }
public override bool Matches(Expression Expr, MatchContext Matched) { throw new NotImplementedException(); }
public override bool Matches(Expression E, MatchContext Matched) { return((ReferenceEquals(Ring, null) || Ring.Contains(E)) && Matched.Matches(this, E)); }
protected abstract Expression ApplyTransform(Expression x, MatchContext Matched);
/// <summary> /// Try matching this pattern against an expression such that if Matches(Expr, Matched) is true, Substitute(Matched) is equal to Expr. /// </summary> /// <param name="Expr">Expression to match.</param> /// <param name="Matched">A context to store matched variables.</param> /// <returns>true if Equals(Expr.Substitute(Matched)) is true.</returns> public virtual bool Matches(Expression Expr, MatchContext Matched) { return(Equals(Expr)); }
public override bool Matches(Expression E, MatchContext Matched) { return((Ring is null || Ring.Contains(E)) && Matched.Matches(this, E)); }