Repository navigation
Expand file tree
/
Copy pathflags.py
More file actions
297 lines (222 loc) · 9.36 KB
/
Copy pathflags.py
File metadata and controls
297 lines (222 loc) · 9.36 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
290
291
292
293
294
295
296
297
from enum import Enum
import os
from pathlib import Path
from typing import TYPE_CHECKING, Callable, Optional
if TYPE_CHECKING:
from checker_proofs import StepRecord
from lsp.debugger import Debugger
class VerboseLevel(Enum):
NONE = 0
CURR_ONLY = 1
FULL = 2
# Python recursion limit used by every Deduce entry point (CLI, test
# harness, LSP/DAP/MCP servers). Deduce's numeric literals desugar to a
# *unary* term whose depth is proportional to the literal's value, so
# every tree-walking pass (uniquify, type-check, evaluation) recurses to
# that depth. This ceiling therefore bounds the largest literal Deduce
# can check before CPython's stack guard trips; keep it in one place so
# the bound is consistent across entry points (issue #1021). CPython
# 3.12 raises a clean ``RecursionError`` rather than crashing when the C
# stack is exhausted, so raising this is safe from segfaults.
RECURSION_LIMIT: int = 40000
# Largest magnitude Deduce accepts for an integer or ``Nat`` literal.
# Literals desugar to a unary term of depth == value, and CPython's C
# stack guard trips (a clean ``RecursionError``) once a tree-walking
# pass recurses a few thousand frames deep -- empirically around a
# literal value of ~5000 with ``RECURSION_LIMIT`` above. Rejecting
# larger literals at parse time with a located, beginner-friendly error
# is far better than an opaque mid-check recursion crash (issue #1021).
# Kept comfortably below the empirical ceiling so a literal near the
# bound still checks even inside a moderately deep proof.
MAX_LITERAL: int = 4000
# flag for displaying uniquified names
unique_names: bool = False
def set_unique_names(b: bool) -> None:
global unique_names
unique_names = b
def get_unique_names() -> bool:
global unique_names
return unique_names
# flag for verbose trace
#
# `verbose` carries two value shapes: the literal `False` sentinel
# (used as the off-state and as the falsy value returned by
# `get_verbose` when the level is `NONE`) and a `VerboseLevel`. Callers
# rely on both: `if get_verbose():` for truthiness, and
# `if get_verbose() == VerboseLevel.CURR_ONLY:` for level checks.
verbose: bool | VerboseLevel = False
def set_verbose(b: bool | VerboseLevel) -> None:
global verbose
verbose = b
def get_verbose() -> bool | VerboseLevel:
global verbose
if verbose == VerboseLevel.NONE:
return False
return verbose
def print_verbose(msg_thunk: Callable[[], str]) -> None:
if get_verbose():
print(msg_thunk())
# flag for expect fail
expect_fail_flag: bool = False
def expect_fail() -> bool:
return expect_fail_flag
def set_expect_fail(b: bool) -> None:
global expect_fail_flag
expect_fail_flag = b
# flag for expect static_fail
expect_static_fail_flag: bool = False
def expect_static_fail() -> bool:
return expect_static_fail_flag
def set_expect_static_fail(b: bool) -> None:
global expect_static_fail_flag
expect_static_fail_flag = b
# flag for import directories
# Searched in the order added. A directory already present under another
# spelling (e.g. `lib` and its absolute path) is not added again, so the
# file an import resolves to, and the path errors print, do not change
# between runs (#1246).
import_directories: list[str] = []
def init_import_directories() -> None:
add_import_directory(".")
lib_config_path = Path(os.path.expanduser("~/.config/deduce/libraries"))
if lib_config_path.exists() and lib_config_path.is_file():
with open(lib_config_path, 'r') as lib_config_file:
for line in lib_config_file:
add_import_directory(line.strip())
def get_import_directories() -> list[str]:
if (get_verbose()):
print("import directories: ", import_directories)
return import_directories
def add_import_directory(dir: str) -> None:
real = os.path.realpath(dir)
if all(os.path.realpath(d) != real for d in import_directories):
import_directories.append(dir)
# The LSP workspace folder that holds the file being checked, if any
# (#1225). Its imports resolve there first, ahead of the import
# directories, and never in another workspace's folder. Set by
# ``lsp.library.check_file`` for each check.
workspace_folder: Optional[str] = None
def get_workspace_folder() -> Optional[str]:
return workspace_folder
def set_workspace_folder(folder: Optional[str]) -> None:
global workspace_folder
workspace_folder = folder
# flag for recursive descent parser
recursive_descent: bool = True
def get_recursive_descent() -> bool:
global recursive_descent
return recursive_descent
def set_recursive_descent(b: bool) -> None:
global recursive_descent
recursive_descent = b
# flag for experimental imperative syntax
experimental_imperative: bool = False
def get_experimental_imperative() -> bool:
global experimental_imperative
return experimental_imperative
def set_experimental_imperative(b: bool) -> None:
global experimental_imperative
experimental_imperative = b
# flag for quiet mode (primarily for testing errors)
quiet_mode: bool = False
def get_quiet_mode() -> bool:
global quiet_mode
return quiet_mode
def set_quiet_mode(b: bool) -> None:
global quiet_mode
quiet_mode = b
# flag for checking to see if we need to re-deduce imported files
check_imports: bool = True
def get_check_imports() -> bool:
global check_imports
return check_imports
def set_check_imports(b: bool) -> None:
global check_imports
check_imports = b
# flag for LSP-targeted hole queries: when set to (line, column), the
# proof checker treats every `?` hole at a different location as a
# successful proof of its goal, and only raises IncompleteProof at the
# hole matching this location. None (the default) preserves the
# raise-on-first-hole behavior used by the CLI.
target_hole_location: Optional[tuple[int, int]] = None
def get_target_hole_location() -> Optional[tuple[int, int]]:
global target_hole_location
return target_hole_location
def set_target_hole_location(loc: Optional[tuple[int, int]]) -> None:
global target_hole_location
target_hole_location = loc
# active debugger session (Phase 5 / Step 21). ``None`` in non-debug
# runs; the hook callbacks in ``proof_checker.check_proofs`` and
# ``abstract_syntax.do_function_call`` / ``Call.reduce`` short-circuit
# on ``None`` so debugger-disabled runs pay only one attribute load
# per hook site. Set by ``lsp.library.check_file`` for the duration
# of one call (paired ``set_debugger(d)`` / ``set_debugger(None)`` in
# a try/finally).
#
# The Debugger import is type-only to avoid an import cycle: flags.py
# sits at the bottom of the import stack and lsp.debugger imports from it.
debugger: Optional["Debugger"] = None
def get_debugger() -> Optional["Debugger"]:
global debugger
return debugger
def set_debugger(d: Optional["Debugger"]) -> None:
global debugger
debugger = d
# Per-step proof annotations for ``lsp.query.proof_outline`` (#1214).
# ``None`` outside an outline run; otherwise ``check_proof_of`` and
# ``check_proof`` record each proof node they visit here, keyed by its
# source range (see ``checker_proofs.record_step``), and ``check_deduce``
# bypasses its per-statement cache so every proof is visited.
proof_outline: Optional[dict[tuple[object, ...], "StepRecord"]] = None
def get_proof_outline() -> Optional[dict[tuple[object, ...], "StepRecord"]]:
return proof_outline
def set_proof_outline(
steps: Optional[dict[tuple[object, ...], "StepRecord"]]) -> None:
global proof_outline
proof_outline = steps
# Display-only aliases mapping a view's source-type name to the view's
# public name. Populated when a `view` declaration is processed
# (`Env.declare_view`). Consulted by `name2str` so a private source
# type like `Binary` is shown as `UInt` in printed types and goals.
view_source_aliases: dict[str, str] = {}
def register_view_source_alias(source_name: str, view_name: str) -> None:
global view_source_aliases
view_source_aliases[source_name] = view_name
def lookup_view_source_alias(name: str) -> Optional[str]:
global view_source_aliases
return view_source_aliases.get(name)
# Nested counter that suppresses view-source aliasing in `name2str`.
# Bumped by `ViewDecl.pretty_print` so the view's own declaration
# still names its underlying source type instead of self-referentially
# rendering as the view name.
_suppress_view_alias_depth: int = 0
def push_suppress_view_alias() -> None:
global _suppress_view_alias_depth
_suppress_view_alias_depth += 1
def pop_suppress_view_alias() -> None:
global _suppress_view_alias_depth
_suppress_view_alias_depth -= 1
def view_aliasing_suppressed() -> bool:
return _suppress_view_alias_depth > 0
# Postulate report (`deduce.py --postulates`). When enabled, every
# imported module is re-checked even if its `.thm` file is up to date,
# and while each top-level statement is checked, `implicit_uses`
# collects the names of theorems it uses without naming them: auto
# rules that fire, the proofs behind `associative` declarations, and
# `inductive` principles.
postulate_report: bool = False
def get_postulate_report() -> bool:
return postulate_report
def set_postulate_report(b: bool) -> None:
global postulate_report
postulate_report = b
implicit_uses: set[str] | None = None
def record_implicit_use(name: str) -> None:
if implicit_uses is not None:
implicit_uses.add(name)
def swap_implicit_uses(uses: set[str] | None) -> set[str] | None:
"""Install `uses` as the recording set and return the previous one."""
global implicit_uses
old = implicit_uses
implicit_uses = uses
return old