Skip to content

Commit 7023c0a

Browse files
make structural validation optional, mark lemmas as imported
1 parent 5eeaf17 commit 7023c0a

File tree

4 files changed

+24
-15
lines changed

4 files changed

+24
-15
lines changed

scripts/preprocess.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55

66
if __name__ == '__main__':
7-
files = [tokenize_file(file) for file in parse_dir('../data/stdlib', False)]
7+
files = [tokenize_file(file) for file in parse_dir('../data/stdlib', strict=False, validate=True)]
88

99
print(f'Tokenized {len(files)} files with {sum(len(file.hole_asts) for file in files)} holes.')
1010
with open('../data/tokenized.p', 'wb') as f:

src/Name/data/agda/operations.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ def enum_references(file: File[Name]) -> tuple[File[int], dict[int, Name]]:
1414
name=name_to_index[entry.name],
1515
type=entry.type.substitute(name_to_index),
1616
definition=entry.definition.substitute(name_to_index),
17-
holes=[hole.substitute(name_to_index) for hole in entry.holes])
17+
holes=[hole.substitute(name_to_index) for hole in entry.holes],
18+
is_import=entry.is_import)
1819
for entry in file.scope])
1920
return file, index_to_name
2021

@@ -59,7 +60,7 @@ def top_sort_entries(file: File[Name]) -> dict[Name, int]:
5960
pre_resolved=dict())
6061

6162

62-
def merge_contexts(file: File[Name], merge_holes: bool, unique_only: bool) -> File[Name]:
63+
def merge_contexts(file: File[Name], merge_holes: bool, unique_only: bool, validate: bool = True) -> File[Name]:
6364
def ctx_to_pi(hole: Hole[Name]) -> Hole[Name]:
6465
return Hole(
6566
context=(),
@@ -84,8 +85,10 @@ def f(holes: list[Hole[Name]]) -> list[Hole[Name]]:
8485
name=entry.name,
8586
type=entry.type,
8687
definition=entry.definition,
87-
holes=f([ctx_to_pi(h) for h in entry.holes]))
88-
for entry in file.scope])
88+
holes=f([ctx_to_pi(h) for h in entry.holes]),
89+
is_import=entry.is_import)
90+
for entry in file.scope],
91+
validate=validate)
8992

9093

9194
def _merge_holes(holes: list[Hole[Name]]) -> list[Hole[Name]]:

src/Name/data/agda/reader.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -19,36 +19,38 @@ def wrapper(json: dict) -> T:
1919
return wrapper if DEBUG else wrapped
2020

2121

22-
def parse_dir(directory: str, strict: bool) -> Iterator[File[str]]:
22+
def parse_dir(directory: str, strict: bool, validate: bool = True) -> Iterator[File[str]]:
2323
for file in listdir(directory):
2424
print(f'Parsing {file}')
2525
try:
26-
yield parse_file(path.join(directory, file))
26+
yield parse_file(path.join(directory, file), validate)
2727
except AssertionError as e:
2828
if strict:
2929
raise e
3030
print(f'\tFailed: {e}.')
3131
continue
3232

3333

34-
def parse_file(filepath: str) -> File[str]:
34+
def parse_file(filepath: str, validate: bool) -> File[str]:
3535
with open(filepath, 'r') as f:
36-
return parse_data(load(f))
36+
return parse_data(load(f), validate=validate)
3737

3838

39-
def parse_data(json: dict) -> File[str]:
39+
def parse_data(json: dict, validate: bool) -> File[str]:
4040
return File(
4141
name=json['name'],
4242
scope=[parse_scope_entry(d, True) for d in json['scope-global']] +
43-
[parse_scope_entry(d, False) for d in json['scope-local']])
43+
[parse_scope_entry(d, False) for d in json['scope-local']],
44+
validate=validate)
4445

4546

4647
def parse_scope_entry(json: dict, is_import: bool) -> ScopeEntry[str]:
4748
return ScopeEntry(
4849
name=json['name'],
4950
type=parse_term(json['type']),
5051
definition=parse_definition(json['definition']),
51-
holes=[] if is_import else [parse_hole(hole) for hole in json['holes']])
52+
holes=[] if is_import else [parse_hole(hole) for hole in json['holes']],
53+
is_import=is_import)
5254

5355

5456
@debug

src/Name/data/agda/syntax.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ def substitute(self, names: dict[Name, Other]) -> Self: ...
2020
class File(_AgdaExpr[Name]):
2121
name: str
2222
scope: list[ScopeEntry[Name]]
23+
validate: bool = True
2324

2425
def __post_init__(self):
25-
assert self.valid_reference_structure(), 'Invalid reference structure.'
26-
assert self.unique_entry_names(), 'Duplicate entry names.'
26+
if self.validate:
27+
assert self.valid_reference_structure(), 'Invalid reference structure.'
28+
assert self.unique_entry_names(), 'Duplicate entry names.'
2729

2830
def valid_reference_structure(self) -> bool:
2931
names = [entry.name for entry in self.scope]
@@ -66,6 +68,7 @@ def substitute(self, names: dict[Name, Other]) -> NamedType[Other]:
6668
class ScopeEntry(NamedType[Name]):
6769
definition: AgdaDefinition[Name]
6870
holes: list[Hole[Name]]
71+
is_import: bool
6972

7073
def __repr__(self) -> str:
7174
return f'{super(ScopeEntry, self).__repr__()} ({len(self.holes)} holes)'
@@ -75,7 +78,8 @@ def substitute(self, names: dict[Name, Other]) -> ScopeEntry[Other]:
7578
name=names[self.name],
7679
type=self.type.substitute(names),
7780
definition=self.definition.substitute(names),
78-
holes=[hole.substitute(names) for hole in self.holes])
81+
holes=[hole.substitute(names) for hole in self.holes],
82+
is_import=self.is_import)
7983

8084

8185
@dataclass(frozen=True)

0 commit comments

Comments
 (0)