packages feed

srtree 3.0.1.0 → 3.0.2.0

raw patch · 4 files changed

+61/−8 lines, 4 filesPVP ok

version bump matches the API change (PVP)

API changes (from Hackage documentation)

+ Algorithm.EqSat.DB: RestPat :: Char -> Pattern

Files

ChangeLog.md view
@@ -1,5 +1,22 @@ # Changelog for srtree +## 3.0.2.0++- **Pattern parser: reject trailing input** (`Text.ParseRule`): a pattern with+  trailing garbage (e.g. an unknown function like `negate(v1)` before this+  release) was silently truncated — `v0 * recip(exp(nope(v1)))` parsed as just+  `v0`, so a user rule could quietly become something completely different+  (and unsound). The parser now fails with `unexpected trailing input in+  pattern`.+- **Rest patterns (`...`)** (`Text.ParseRule`): `...` in a pattern matches the+  remaining factors of a product (or summands of a sum) of any arity, e.g.+  `exp(v1) * ... -> recip(exp(negate(v1))) * ...` rewrites the `exp` factor+  inside any product. Needed because n-ary matching is exact-multiset: a+  two-factor pattern never matches a three-factor product node. At most one+  rest per side; a source rest must also appear in the target.+- **Pattern vocabulary: `negate`/`neg`** (and capitalized aliases) encoding+  negation as `-1 * x`, matching the rule DSL's `negate`.+ ## 3.0.1.0  - **User-supplied equivalence rules** (new exposed module `Text.ParseRule`):
src/Algorithm/EqSat/DB.hs view
@@ -40,7 +40,7 @@ -- A Pattern is either a fixed-point of a tree, an index to a pattern variable -- (which matches anything), a hole (only used inside a 'MapP' target function), -- or an n-ary Add/Mul pattern whose children are matched as a multiset.-data Pattern = Fixed (SRTree Pattern) | VarPat Char | Hole | NAry NOp [NChild]+data Pattern = Fixed (SRTree Pattern) | VarPat Char | Hole | NAry NOp [NChild] | RestPat Char   deriving (Show, Eq, Ord)  -- | A child of an n-ary pattern: a single child pattern ('Ch'), a rest@@ -110,6 +110,7 @@ unFixPat (VarPat _) = error "unFixPat: VarPat is not a fixed pattern" unFixPat Hole       = error "unFixPat: Hole is not a fixed pattern" unFixPat (NAry _ _) = error "unFixPat: NAry is not a fixed pattern"+unFixPat (RestPat _) = error "unFixPat: RestPat is not a fixed pattern" {-# INLINE unFixPat #-}  
src/Text/ParseRule.hs view
@@ -71,7 +71,7 @@ -- Pattern variables can be written as @v<i>@ or as a single letter @[a-zA-Z]@ -- (e.g. @x + 0@). parsePat :: B.ByteString -> Either String Pattern-parsePat = eitherResult . (`feed` "") . parse parsePatExpr . putEOL . B.strip+parsePat = fmap normalizePat . eitherResult . (`feed` "") . parse parsePatExpr . putEOL . B.strip  putEOL :: B.ByteString -> B.ByteString putEOL bs | B.null bs = bs@@ -82,14 +82,14 @@ parsePatExpr = parsePatternExpr (prefixOps : binOps) [] var   where     prefixOps = Prelude.map (uncurry prefix)-                [ ("id", id), ("abs", abs)+                [ ("id", id), ("abs", abs), ("negate", negate), ("neg", negate)                   , ("sinh", sinh), ("cosh", cosh), ("tanh", tanh)                   , ("sin", sin), ("cos", cos), ("tan", tan)                   , ("asinh", asinh), ("acosh", acosh), ("atanh", atanh)                   , ("asin", asin), ("acos", acos), ("atan", atan)                   , ("sqrtabs", sqrtabs'), ("sqrt", sqrt), ("cbrt", cbrt'), ("square", (**2))                   , ("logabs", logabs'), ("log", log), ("exp", exp), ("cube", cube'), ("recip", recip')-                  , ("Id", id), ("Abs", abs)+                  , ("Id", id), ("Abs", abs), ("Negate", negate), ("Neg", negate)                   , ("Sinh", sinh), ("Cosh", cosh), ("Tanh", tanh)                   , ("Sin", sin), ("Cos", cos), ("Tan", tan)                   , ("ASinh", asinh), ("ACosh", acosh), ("ATanh", atanh)@@ -113,9 +113,12 @@     cube' t     = Fixed $ Uni Cube t     recip' t    = Fixed $ Uni Recip t -    var = do char 'x'-             ix <- decimal-             pure $ Fixed $ Var ix+    var = do string "..."+             c <- letter_ascii <|> pure 'r'+             pure $ RestPat c+          <|> do char 'x'+                 ix <- decimal+                 pure $ Fixed $ Var ix           <|> do char 't'                  ix <- decimal                  pure $ Fixed $ Param ix@@ -142,12 +145,32 @@ parsePatternExpr table binFuns var =     do e <- expr        many1' space+       -- refuse trailing garbage: without this, "x * f(nope)" silently parses+       -- as just "x" (the expression parser stops early and eitherResult+       -- ignores the unconsumed input)+       endOfInput <?> "unexpected trailing input in pattern"        pure e   where     term  = parens expr <|> choice (Prelude.map ($ expr) binFuns) <|> coef <|> var <?> "term"     expr  = buildExpressionParser table term     coef  = Fixed . Const <$> signed double <?> "const" +-- | Normalizes a parsed pattern: flattens nested same-op n-ary chains (the+-- @*@\/@+@ instances build left-nested @NAry@ nodes, e.g. @a * b * ...@) and+-- turns @Ch (RestPat c)@ children into proper @Rest c@ children so a written+-- @x * ...@ matches a product of any arity that contains @x@.+normalizePat :: Pattern -> Pattern+normalizePat = go+  where+    go (Fixed t)    = Fixed (fmap go t)+    go (NAry op cs) = NAry op (concatMap (nChild op) cs)+    go p            = p+    nChild _ (Ch (RestPat c)) = [Rest c]+    nChild op (Ch p) = case go p of+      NAry op' cs | op' == op -> concatMap (nChild op) cs+      p'                      -> [Ch p']+    nChild _ x = [x]+ -- * Rule parsing  -- | A rule direction: one-way ('Dir') or bidirectional ('Bi').@@ -256,6 +279,18 @@             VarPat c               -> Left ("condition variable '" <> [c] <> "' does not appear in the source pattern")             _ -> Left "condition arguments must be pattern variables (e.g. isNotZero(x))")         conds+  if length (restPats lhs) > 1 || length (restPats rhs) > 1+    then Left "at most one '...' (rest) is allowed per side of a rule"+    else if not (null (restPats lhs)) && null (restPats rhs)+      then Left "the source binds '...' but the target does not: the remaining factors would be dropped"+      else Right ()+  where+    restPats (NAry _ cs) = concatMap childRests cs+    restPats (Fixed t)   = concatMap restPats (getElems t)+    restPats _           = []+    childRests (Ch p)     = restPats p+    childRests (Rest c)   = [c]+    childRests (MapP p c) = c : restPats p  -- | The pattern variables occurring in a pattern. patVars :: Pattern -> [Char]
srtree.cabal view
@@ -5,7 +5,7 @@ -- see: https://github.com/sol/hpack  name:               srtree-version:            3.0.1.0+version:            3.0.2.0 synopsis:           A general library to work with Symbolic Regression expression trees. description:        A Symbolic Regression Tree data structure to work with mathematical expressions with support to first order derivative and simplification; license:            BSD3