A pattern matching library.
Project description
A pattern matching libary for python.
Work in progress
Overview
This package implements pattern matching in python. It is similar to the implementation in Mathematica or Haskell.
Expressions
Expressions and patterns both have a tree structure. Expressions consist of symbols (leafs) and operations (internal nodes):
>>> from matchpy.expressions import Operation, Symbol, Arity
>>> f = Operation.new('f', Arity.binary)
>>> a = Symbol('a')
>>> print(f(a, a))
f(a, a)
Patterns are expressions which can additionally contain variables and wildcards. Variables can give a name to a node of the pattern so that it can be accessed later. Wildcards are placeholders that stand for any expression. Usually, the two are used in combination:
>>> from matchpy.expressions import Variable
>>> x = Variable.dot('x')
>>> print(f(a, x))
f(a, x_)
However, unnamed wildcards can also be used:
>>> from matchpy.expressions import Wildcard >>> w = Wildcard.dot() >>> print(f(w, w)) f(_, _)
Or a more complex expression can be named with a variable:
>>> print(Variable('y', f(w, a)))
y: f(_, a)
In addition, it supports sequence wildcards that stand for multiple expressions:
>>> z = Variable.plus('z')
>>> print(f(z))
f(z__)
Substitutions
Matches are given in the form of substitutions, which are a mapping from variable names to expressions:
>>> from matchpy.matching.one_to_one import match
>>> y = Variable.dot('y')
>>> b = Symbol('b')
>>> expression = f(a, b)
>>> pattern = f(x, y)
>>> substitution = next(match(expression, pattern))
>>> substitution
{'x': FrozenSymbol('a'), 'y': FrozenSymbol('b')}
Replacing the variables in the pattern according to the substitution will yield the original subject expression:
>>> from matchpy.functions import substitute >>> original, _ = substitute(pattern, substitution) >>> print(original) f(a, b)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file matchpy-0.1.1-py3-none-any.whl.
File metadata
- Download URL: matchpy-0.1.1-py3-none-any.whl
- Upload date:
- Size: 55.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1e3d6e4afa37c51fa512a6c99a50a9dbccc98525716ba22a588ce07ae07b2a8c
|
|
| MD5 |
0cf1c82ec75cbff4f7cc0679a13d53b0
|
|
| BLAKE2b-256 |
4103f513080c2b52356b1202a72849ae5b23a1e5a3972e61762d491c98b3fcb2
|