rerum 0.2.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- rerum/__init__.py +137 -0
- rerum/cli.py +722 -0
- rerum/engine.py +1622 -0
- rerum/rewriter.py +957 -0
- rerum/tests/__init__.py +1 -0
- rerum/tests/test_bindings.py +216 -0
- rerum/tests/test_cli.py +368 -0
- rerum/tests/test_engine_methods.py +449 -0
- rerum/tests/test_expr_builder.py +120 -0
- rerum/tests/test_groups.py +420 -0
- rerum/tests/test_guards.py +321 -0
- rerum/tests/test_includes.py +252 -0
- rerum/tests/test_priorities.py +228 -0
- rerum/tests/test_rewriter.py +303 -0
- rerum/tests/test_sequencing.py +181 -0
- rerum/tests/test_strategies.py +260 -0
- rerum/tests/test_trace.py +285 -0
- rerum-0.2.2.dist-info/METADATA +567 -0
- rerum-0.2.2.dist-info/RECORD +21 -0
- rerum-0.2.2.dist-info/WHEEL +4 -0
- rerum-0.2.2.dist-info/entry_points.txt +2 -0
rerum/__init__.py
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
"""
|
|
2
|
+
RERUM - Rewriting Expressions via Rules Using Morphisms
|
|
3
|
+
|
|
4
|
+
A pattern matching and term rewriting library for symbolic computation.
|
|
5
|
+
|
|
6
|
+
Quick Start:
|
|
7
|
+
from rerum import RuleEngine
|
|
8
|
+
|
|
9
|
+
engine = RuleEngine.from_dsl('''
|
|
10
|
+
@add-zero: (+ ?x 0) => :x
|
|
11
|
+
@mul-one: (* ?x 1) => :x
|
|
12
|
+
''')
|
|
13
|
+
|
|
14
|
+
result = engine(["+", "y", 0]) # => "y"
|
|
15
|
+
|
|
16
|
+
DSL Syntax:
|
|
17
|
+
# Comments start with #
|
|
18
|
+
@rule-name: (pattern) => (skeleton)
|
|
19
|
+
@rule-name "Description": (pattern) => (skeleton)
|
|
20
|
+
|
|
21
|
+
Pattern Syntax:
|
|
22
|
+
?x or ?x:expr - match any expression, bind to x
|
|
23
|
+
?x:const - match constant only
|
|
24
|
+
?x:var - match variable only
|
|
25
|
+
?x:free(v) - match expression not containing v
|
|
26
|
+
:x - substitute bound value
|
|
27
|
+
|
|
28
|
+
Example Rules File (algebra.rules):
|
|
29
|
+
# Basic algebra
|
|
30
|
+
@add-zero "x + 0 = x": (+ ?x 0) => :x
|
|
31
|
+
@mul-one: (* ?x 1) => :x
|
|
32
|
+
@mul-zero: (* ?x 0) => 0
|
|
33
|
+
|
|
34
|
+
# Derivatives
|
|
35
|
+
@dd-const: (dd ?c:const ?v:var) => 0
|
|
36
|
+
@dd-var: (dd ?x:var ?x) => 1
|
|
37
|
+
"""
|
|
38
|
+
|
|
39
|
+
__version__ = "0.1.0"
|
|
40
|
+
__author__ = "spinoza"
|
|
41
|
+
|
|
42
|
+
# Core rewriter components
|
|
43
|
+
from .rewriter import (
|
|
44
|
+
rewriter,
|
|
45
|
+
simplifier,
|
|
46
|
+
match,
|
|
47
|
+
instantiate,
|
|
48
|
+
ExprType,
|
|
49
|
+
BindingsType,
|
|
50
|
+
RuleType,
|
|
51
|
+
NumericType,
|
|
52
|
+
FoldHandler,
|
|
53
|
+
FoldFuncsType,
|
|
54
|
+
# Bindings classes
|
|
55
|
+
Bindings,
|
|
56
|
+
NoMatch,
|
|
57
|
+
wrap_bindings,
|
|
58
|
+
# Fold operation builders
|
|
59
|
+
nary_fold,
|
|
60
|
+
unary_only,
|
|
61
|
+
binary_only,
|
|
62
|
+
special_minus,
|
|
63
|
+
safe_div,
|
|
64
|
+
# Standard preludes
|
|
65
|
+
ARITHMETIC_PRELUDE,
|
|
66
|
+
MATH_PRELUDE,
|
|
67
|
+
MINIMAL_PRELUDE,
|
|
68
|
+
PREDICATE_PRELUDE,
|
|
69
|
+
FULL_PRELUDE,
|
|
70
|
+
NO_PRELUDE,
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
# Engine and DSL
|
|
74
|
+
from .engine import (
|
|
75
|
+
RuleEngine,
|
|
76
|
+
SequencedEngine,
|
|
77
|
+
RuleMetadata,
|
|
78
|
+
RewriteStep,
|
|
79
|
+
RewriteTrace,
|
|
80
|
+
E,
|
|
81
|
+
parse_sexpr,
|
|
82
|
+
format_sexpr,
|
|
83
|
+
parse_rule_line,
|
|
84
|
+
load_rules_from_dsl,
|
|
85
|
+
load_rules_from_file,
|
|
86
|
+
load_rules_from_json,
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
# Public API
|
|
90
|
+
__all__ = [
|
|
91
|
+
# Version
|
|
92
|
+
"__version__",
|
|
93
|
+
# Core
|
|
94
|
+
"rewriter",
|
|
95
|
+
"simplifier",
|
|
96
|
+
"match",
|
|
97
|
+
"instantiate",
|
|
98
|
+
# Types
|
|
99
|
+
"ExprType",
|
|
100
|
+
"BindingsType",
|
|
101
|
+
"RuleType",
|
|
102
|
+
"NumericType",
|
|
103
|
+
"FoldHandler",
|
|
104
|
+
"FoldFuncsType",
|
|
105
|
+
# Bindings
|
|
106
|
+
"Bindings",
|
|
107
|
+
"NoMatch",
|
|
108
|
+
"wrap_bindings",
|
|
109
|
+
# Fold operation builders
|
|
110
|
+
"nary_fold",
|
|
111
|
+
"unary_only",
|
|
112
|
+
"binary_only",
|
|
113
|
+
"special_minus",
|
|
114
|
+
"safe_div",
|
|
115
|
+
# Standard preludes
|
|
116
|
+
"ARITHMETIC_PRELUDE",
|
|
117
|
+
"MATH_PRELUDE",
|
|
118
|
+
"MINIMAL_PRELUDE",
|
|
119
|
+
"PREDICATE_PRELUDE",
|
|
120
|
+
"FULL_PRELUDE",
|
|
121
|
+
"NO_PRELUDE",
|
|
122
|
+
# Engine
|
|
123
|
+
"RuleEngine",
|
|
124
|
+
"SequencedEngine",
|
|
125
|
+
"RuleMetadata",
|
|
126
|
+
"RewriteStep",
|
|
127
|
+
"RewriteTrace",
|
|
128
|
+
# Expression builder
|
|
129
|
+
"E",
|
|
130
|
+
# DSL utilities
|
|
131
|
+
"parse_sexpr",
|
|
132
|
+
"format_sexpr",
|
|
133
|
+
"parse_rule_line",
|
|
134
|
+
"load_rules_from_dsl",
|
|
135
|
+
"load_rules_from_file",
|
|
136
|
+
"load_rules_from_json",
|
|
137
|
+
]
|