Skip to content

Commit

Permalink
Better head matching.
Browse files Browse the repository at this point in the history
  • Loading branch information
corywalker committed Jul 16, 2017
1 parent 11ab1a6 commit a6f8238
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 21 deletions.
15 changes: 3 additions & 12 deletions expreduce/blank.go
Expand Up @@ -19,7 +19,7 @@ func IsBlankTypeOnly(e Ex) bool {
return false
}

func IsBlankTypeCapturing(e Ex, target Ex, t string, pm *PDManager, cl *CASLogger) (bool, *PDManager) {
func IsBlankTypeCapturing(e Ex, target Ex, head Ex, pm *PDManager, cl *CASLogger) (bool, *PDManager) {
// Similar to IsBlankType, but will capture target into es.patternDefined
// if there is a valid match.
asPattern, patternOk := HeadAssertion(e, "Pattern")
Expand All @@ -44,12 +44,7 @@ func IsBlankTypeCapturing(e Ex, target Ex, t string, pm *PDManager, cl *CASLogge
if len(parts) < 2 {
matchesHead = true
} else {
asSymbol, symbolOk := parts[1].(*Symbol)
if symbolOk {
if asSymbol.Name == t {
matchesHead = true
}
}
matchesHead = IsSameQ(head, parts[1], cl)
}
cl.Debugf("%v", matchesHead)
if matchesHead {
Expand Down Expand Up @@ -84,7 +79,6 @@ func IsBlankTypeCapturing(e Ex, target Ex, t string, pm *PDManager, cl *CASLogge
asBS, bsOk := HeadAssertion(e, "BlankSequence")
asBNS, bnsOk := HeadAssertion(e, "BlankNullSequence")
if blankOk || bsOk || bnsOk {
var asSymbol *Symbol
parts := []Ex{}
if blankOk {
parts = asBlank.Parts
Expand All @@ -96,10 +90,7 @@ func IsBlankTypeCapturing(e Ex, target Ex, t string, pm *PDManager, cl *CASLogge
if len(parts) < 2 {
return true, pm
}
asSymbol, symbolOk := parts[1].(*Symbol)
if symbolOk {
return asSymbol.Name == t, pm
}
return IsSameQ(head, parts[1], cl), pm
}
return false, pm
}
Expand Down
2 changes: 2 additions & 0 deletions expreduce/builtin_pattern.go
Expand Up @@ -91,6 +91,8 @@ func GetPatternDefinitions() (defs []Definition) {

// Test pinning in orderless
&SameTest{"{{b[[1]],b},{y,a},{y,c},{b[[1]],b},{x,a},{y,c},{b[[1]],b},{x,c},{y,a},{b[[1]],b},{x,a},{x,c}}", "pats"},

&SameTest{"True", "MatchQ[(a+b)[testsym],Blank[(a+b)]]"},
},
KnownFailures: []TestInstruction{
// Test order of pattern checking
Expand Down
17 changes: 8 additions & 9 deletions expreduce/matchq.go
Expand Up @@ -147,24 +147,23 @@ func NewMatchIter(a Ex, b Ex, pm *PDManager, es *EvalState) (matchIter, bool) {
}

// This initial value is just a randomly chosen placeholder
// TODO, convert headStr to symbol type, have Ex implement getHead() Symbol
headStr := "Unknown"
var headEx Ex
if aIsFlt {
headStr = "Real"
headEx = &Symbol{"Real"}
} else if aIsInteger {
headStr = "Integer"
headEx = &Symbol{"Integer"}
} else if aIsString {
headStr = "String"
headEx = &Symbol{"String"}
} else if aIsExpression {
headStr = aExpression.Parts[0].String()
headEx = aExpression.Parts[0]
} else if aIsSymbol {
headStr = "Symbol"
headEx = &Symbol{"Symbol"}
} else if aIsRational {
headStr = "Rational"
headEx = &Symbol{"Rational"}
}

if IsBlankTypeOnly(b) {
ibtc, ibtcNewPDs := IsBlankTypeCapturing(b, a, headStr, pm, &es.CASLogger)
ibtc, ibtcNewPDs := IsBlankTypeCapturing(b, a, headEx, pm, &es.CASLogger)
if ibtc {
return &dummyMatchIter{true, ibtcNewPDs, true}, true
}
Expand Down

0 comments on commit a6f8238

Please sign in to comment.