-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpression_utils.py
More file actions
289 lines (230 loc) · 9.47 KB
/
expression_utils.py
File metadata and controls
289 lines (230 loc) · 9.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# Generative Logic: A deterministic reasoning and knowledge generation engine.
# Copyright (C) 2025 Generative Logic UG (haftungsbeschränkt)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
# ------------------------------------------------------------------------------
#
# This software is also available under a commercial license. For details,
# see: https://generative-logic.com/license
#
# Contributions to this project must be made under the terms of the
# Contributor License Agreement (CLA). See the project's CONTRIBUTING.md file.
"""
Lightweight utility functions extracted from create_expressions.py.
Conjecture generation is now handled by the C++ conjecturer (gl_quick.exe --conjecture).
This module retains only the string-parsing utilities needed by visualization,
proof graph processing, and pipeline orchestration.
"""
import re
from typing import Dict
from configuration_reader import configuration_reader
# ---------------------------------------------------------------------------
# Module-level state (replaces create_expressions._CONFIGURATION)
# ---------------------------------------------------------------------------
_CONFIGURATION = configuration_reader()
def set_configuration(config: configuration_reader):
global _CONFIGURATION
_CONFIGURATION = config
def get_configuration_data():
return _CONFIGURATION.data
def get_anchor_name(config: configuration_reader):
# 1. Explicit anchor_name field in config JSON
anchor_name = getattr(config, "anchor_name", None)
if anchor_name and anchor_name in config.data:
return anchor_name
# 2. Try "Anchor" + anchor_id
if getattr(config, "anchor_id", None):
candidate = "Anchor" + config.anchor_id
if candidate in config.data:
return candidate
assert False
# ---------------------------------------------------------------------------
# Expression parsing utilities
# ---------------------------------------------------------------------------
def extract_between_brackets(s, start_index=0):
start = s.find('[', start_index)
end = s.find(']', start)
if start != -1 and end != -1:
return s[start + 1:end]
return None
def get_args(expr):
sub_expr = extract_between_brackets(expr, 0)
if sub_expr == "":
return []
else:
return sub_expr.split(',')
def extract_expression(s: str) -> str:
index = s.find('[')
if index != -1:
if s[0] == "(":
return s[1:index]
else:
return s[0:index]
return ""
def extract_expression_from_negation(s: str) -> str:
start_index = s.find("!(")
end_index = s.find("[")
if start_index != -1 and end_index != -1 and start_index < end_index:
return s[start_index + 2:end_index]
return ""
# ---------------------------------------------------------------------------
# Tree / parse_expr / tree_to_expr
# ---------------------------------------------------------------------------
class TreeNode1:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
self.arguments = set()
def parse_expr(tree_str):
tree_str = tree_str.replace("\n", "")
tree_str = tree_str.replace(" ", "")
tree_str = tree_str.replace("\t", "")
index = 0
def parse_subtree(s):
nonlocal index
if not s:
raise RuntimeError("Input 's' cannot be empty. Execution terminated.")
node = TreeNode1("")
node_label = ""
if s[index] == '(':
index = index + 1
if s[index] == '>':
index = index + 1
node_label = node_label + '>'
args_to_remove = get_args(s[index:])
index = s[index:].find(']') + index + 1
node_label = node_label + "[" + ",".join(args_to_remove) + "]"
node.left = parse_subtree(s)
node.right = parse_subtree(s)
if node.left is not None:
node.arguments.update(node.left.arguments)
if node.right is not None:
node.arguments.update(node.right.arguments)
node.arguments.difference_update(get_args(node_label))
elif s[index] == '&':
index = index + 1
node_label = node_label + '&'
node.left = parse_subtree(s)
node.right = parse_subtree(s)
if node.left is not None:
node.arguments.update(node.left.arguments)
if node.right is not None:
node.arguments.update(node.right.arguments)
else:
end_index = s.find(')', index)
if end_index == -1:
raise RuntimeError("No closing ')'.")
node_label = s[index:end_index]
index = end_index
node.arguments.update(get_args(node_label))
elif s[index:index + 2] == "!(":
index = index + 2
if s[index] == '>':
index = index + 1
node_label = node_label + "!>"
args_to_remove = get_args(s[index:])
index = s[index:].find(']') + index + 1
node_label = node_label + "[" + ",".join(args_to_remove) + "]"
node.left = parse_subtree(s)
node.right = parse_subtree(s)
if node.left is not None:
node.arguments.update(node.left.arguments)
if node.right is not None:
node.arguments.update(node.right.arguments)
node.arguments.difference_update(get_args(node_label))
elif s[index] == '&':
index = index + 1
node_label = node_label + "!&"
node.left = parse_subtree(s)
node.right = parse_subtree(s)
if node.left is not None:
node.arguments.update(node.left.arguments)
if node.right is not None:
node.arguments.update(node.right.arguments)
else:
end_index = s.find(')', index)
if end_index == -1:
raise RuntimeError("No closing ')'.")
node_label = s[index:end_index]
node_label = "!(" + node_label + ")"
index = end_index
node.arguments.update(get_args(node_label))
elif s[index] == ")":
index -= 1
index = index + 1
node.value = node_label
if node.value == "":
node = None
return node
root = parse_subtree(tree_str)
return root
def tree_to_expr(root):
local_expr = ""
def node_to_str(node):
nonlocal local_expr
if node.value[0] == ">":
local_expr = local_expr + "(" + node.value
elif node.value == "&":
local_expr = local_expr + "(&"
elif node.value[0:2] == "!>":
local_expr = local_expr + "!(" + node.value[1:]
elif node.value == "!&":
local_expr = local_expr + "!(&"
elif node.value[0] == '!':
local_expr = local_expr + "!(" + node.value[2:-1]
else:
local_expr = local_expr + "(" + node.value
if node.left is not None:
node_to_str(node.left)
if node.right is not None:
node_to_str(node.right)
local_expr = local_expr + ")"
node_to_str(root)
return local_expr
# ---------------------------------------------------------------------------
# disintegrate_implication
# ---------------------------------------------------------------------------
def disintegrate_implication(expr_for_desintegration, chain):
head = ""
root = parse_expr(expr_for_desintegration)
node = root
while True:
if node is not None:
if node.value[0] == ">":
chain.append((tree_to_expr(node.left), get_args(node.value), node.left.arguments))
node = node.right
else:
head = tree_to_expr(node)
break
else:
break
return head
# ---------------------------------------------------------------------------
# replace_keys_in_string (regex-based)
# ---------------------------------------------------------------------------
_regex_cache = {}
def _get_compiled_regex(keys) -> re.Pattern:
key_tuple = tuple(sorted(keys))
if key_tuple not in _regex_cache:
escaped_keys = [re.escape(key) for key in key_tuple]
pattern = r'(?<=[\[,])(' + '|'.join(escaped_keys) + r')(?=[\],])'
_regex_cache[key_tuple] = re.compile(pattern)
return _regex_cache[key_tuple]
def replace_keys_in_string(big_string: str, replacement_map: Dict[str, str]) -> str:
if not replacement_map:
return big_string
regex = _get_compiled_regex(replacement_map.keys())
return regex.sub(lambda m: replacement_map.get(m.group(1), m.group(1)), big_string)