Copyright | (c) 2014, Andrew G. Seniuk |
---|---|
License | BSD-style (see the file LICENSE) |
Maintainer | Andrew Seniuk <rasfar@gmail.com> |
Stability | provisional |
Portability | portable, except mkPat, mkPatN and growPat (which use SYB) |
Safe Haskell | None |
Language | Haskell2010 |
- unionPats :: [Pattern] -> Pattern
- intersectPats :: [Pattern] -> Pattern
- isSubPatOf :: Pattern -> Pattern -> Bool
- mkPat :: forall d. Data d => d -> Pattern
- mkPatN :: forall d. Data d => Int -> d -> Pattern
- growPat :: forall d. Data d => Pattern -> d -> Pattern
- truncatePat :: Int -> Pattern -> Pattern
- shrinkPat :: Pattern -> Pattern
- shrinkPat_old :: Pattern -> Pattern
- emptyPat :: Pattern
- liftPats :: [Pattern] -> Pattern
- splicePats :: Pattern -> [Int] -> [(Int, Pattern)] -> Pattern
- elidePats :: Pattern -> [Int] -> [Int] -> Pattern
- erodePat :: StdGen -> [Int] -> Pattern -> (Pattern, StdGen)
- module Control.DeepSeq.Bounded.Pattern
Basic operations on Patterns
intersectPats :: [Pattern] -> Pattern Source
isSubPatOf :: Pattern -> Pattern -> Bool Source
Return True
if the first pattern matches the second (and False
otherwise).
Note that matching does not imply spanning. Equality (==
) or
will work there, depending on your intentions.flip
isSubPatOf
XXX This doesn't yet handle type-constrained PatNode
s
(TI
, TR
, TN
or TW
), because intersectPats
doesn't.
Operations for obtaining and modifying Patterns based on a term
mkPat :: forall d. Data d => d -> Pattern Source
Obtain a lazy pattern, matching the shape of
an arbitrary term (value expression).
Interior nodes will be WR
, and leaves will be WS
.
Note this gives counter-intuitive results when used on
.
For example, a rose tree with a single node will have a 3-node /\ shape.)
Formally, Rose
amkPat
is not idempotent on Pattern
s, but
rather grows without bound when iterated. This shouldn't be
an issue in practise.
mkPatN :: forall d. Data d => Int -> d -> Pattern Source
Obtain a lazy pattern, matching the shape of
an arbitrary term, but only down to at most depth n
.
Interior nodes will be WR
.
Leaf nodes will be WS
if they were leaves in the host value;
otherwise (i.e. if they are at depth n
) they will be WR
.
Satisfies
.forcep
. mkPatN
n = forcen
n
See caveat in the comment to mkPat
.
growPat :: forall d. Data d => Pattern -> d -> Pattern Source
Grow all leaves by one level within the shape of the provided value.
Operations for obtaining subpatterns (in the isSubPatOf
sense)
truncatePat :: Int -> Pattern -> Pattern Source
Given an integer depth and a pattern, truncate the pattern to extend to at most this requested depth.
shrinkPat :: Pattern -> Pattern Source
Elide all leaves which have no non-leaf sibling. We want the pattern to still match the same value, only less of it. Merely eliding all leaves would, in most cases, cause match failure, so we have to be a bit more subtle. There are some arbitrary decisions about the relaxation route through the lattice. (Refer to the source for details.)
shrinkPat_old :: Pattern -> Pattern Source
Old version, for temporary compatibility of seqaid demo mode.
Operations for the direct construction and perturbation of Patterns
There is no Nil in the Pattern type, but a single WI
node as
empty pattern is a dependable way to assure the empty pattern
never forces anything.
liftPats :: [Pattern] -> Pattern Source
This creates a new WR
node, the common root. The argument patterns
become the children of the root (order is preserved).
splicePats :: Pattern -> [Int] -> [(Int, Pattern)] -> Pattern Source
Introduce siblings at a node (interior or leaf) of the target. The first argument is target, the second is a path, and the third is a list of subpatterns for insertion, along with the indices of the child before which to insert. If this index is negative, it counts from the right. Indices are always relative to the original target as it was received.
elidePats :: Pattern -> [Int] -> [Int] -> Pattern Source
Elide siblings at a node (interior or leaf) of the target. The first argument is target, the second is a path, and the third is a list of child indices for elision. If this index is negative, it counts from the right. Indices are always relative to the original target as it was received.
erodePat :: StdGen -> [Int] -> Pattern -> (Pattern, StdGen) Source
Select a leaf at random, and elide it. In order to achieve fairness, the node probabilities are weighted by nodes in branch. The path arg can "focus" the stochastic erosion to only consider leaves beneath a given node.