Skip to content

Commit

Permalink
Support removing implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
WeetHet committed Oct 10, 2024
1 parent 4e26e59 commit 7542085
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 15 deletions.
8 changes: 8 additions & 0 deletions verified_cogen/args.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class ProgramArgs:
log_tries: Optional[str]
output_logging: bool
remove_conditions: bool
remove_implementations: bool

@no_type_check
def __init__(self, args):
Expand All @@ -45,6 +46,7 @@ def __init__(self, args):
self.log_tries = args.log_tries
self.output_logging = args.output_logging
self.remove_conditions = args.remove_conditions
self.remove_implementations = args.remove_implementations


def get_default_parser():
Expand Down Expand Up @@ -105,6 +107,12 @@ def get_default_parser():
default=False,
action="store_true",
)
parser.add_argument(
"--remove-implementations",
help="Remove implementations from the program",
default=False,
action="store_true",
)
return parser


Expand Down
3 changes: 3 additions & 0 deletions verified_cogen/experiments/incremental_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ def main():
all_removed = [AnnotationType.INVARIANTS, AnnotationType.ASSERTS]
if args.remove_conditions:
all_removed += [AnnotationType.PRE_CONDITIONS, AnnotationType.POST_CONDITIONS]
if args.remove_implementations:
all_removed += [AnnotationType.IMPLS]

register_basic_languages(with_removed=all_removed)

mode = Mode(args.insert_conditions_mode)
Expand Down
2 changes: 2 additions & 0 deletions verified_cogen/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ def main():
all_removed = [AnnotationType.INVARIANTS, AnnotationType.ASSERTS]
if args.remove_conditions:
all_removed += [AnnotationType.PRE_CONDITIONS, AnnotationType.POST_CONDITIONS]
if args.remove_implementations:
all_removed += [AnnotationType.IMPLS]
register_basic_languages(with_removed=all_removed)
mode = Mode(args.insert_conditions_mode)
if mode == Mode.REGEX:
Expand Down
16 changes: 16 additions & 0 deletions verified_cogen/runners/languages/dafny.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def __init__(self, remove_annotations: list[AnnotationType]): # type: ignore
AnnotationType.ASSERTS: r" *// assert-start.*?// assert-end\n",
AnnotationType.PRE_CONDITIONS: r" *// pre-conditions-start.*?// pre-conditions-end\n",
AnnotationType.POST_CONDITIONS: r" *// post-conditions-start.*?// post-conditions-end\n",
AnnotationType.IMPLS: r" *// impl-start.*?// impl-end\n",
}
super().__init__(
re.compile(
Expand All @@ -41,3 +42,18 @@ def _validators_from(
", ".join(f"ret{i}" for i in range(len(returns.split(",")))),
)
return result

def separate_validator_errors(self, errors: str) -> tuple[str, str]:
lines = errors.split("\n")
lines = [
line for line in lines if "Dafny program verifier finished" not in line
]
line_with_ret0 = next(
(i for i, line in enumerate(lines) if "ret0" in line), None
)
if line_with_ret0 is None:
return "\n".join(lines), ""
else:
non_verifier_errors = "\n".join(lines[: line_with_ret0 - 2]).strip()
verifier_errors = "\n".join(lines[line_with_ret0 - 2 :]).strip()
return non_verifier_errors, verifier_errors
4 changes: 4 additions & 0 deletions verified_cogen/runners/languages/language.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class AnnotationType(Enum):
ASSERTS = "asserts"
PRE_CONDITIONS = "pre-conditions"
POST_CONDITIONS = "post-conditions"
IMPLS = "impls"


class Language:
Expand All @@ -22,6 +23,9 @@ def generate_validators(self, code: str) -> str: ...
@abstractmethod
def remove_conditions(self, code: str) -> str: ...

@abstractmethod
def separate_validator_errors(self, errors: str) -> tuple[str, str]: ...


class GenericLanguage(Language):
method_regex: Pattern[str]
Expand Down
5 changes: 5 additions & 0 deletions verified_cogen/runners/languages/nagini.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,8 @@ def __init__(self, remove_annotations: list[AnnotationType]): # type: ignore
"# assert-line",
"#",
)

def separate_validator_errors(self, errors: str) -> tuple[str, str]:
raise NotImplementedError(
"Separating validator errors is not implemented for Verus yet"
)
5 changes: 5 additions & 0 deletions verified_cogen/runners/languages/verus.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,8 @@ def __init__(self, remove_annotations: list[AnnotationType]): # type: ignore
def generate_validators(self, code: str) -> str:
result = super().generate_validators(code)
return "verus!{{\n{}}}".format(result)

def separate_validator_errors(self, errors: str) -> tuple[str, str]:
raise NotImplementedError(
"Separating validator errors is not implemented for Verus yet"
)
3 changes: 1 addition & 2 deletions verified_cogen/runners/validating.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from verified_cogen.runners import Runner
from verified_cogen.runners.languages.language import Language
from verified_cogen.tools.modes import Mode
from verified_cogen.tools.dafny_separate import dafny_separate


class ValidatingRunner(Runner):
Expand Down Expand Up @@ -64,7 +63,7 @@ def ask_for_timeout(self) -> str:
def ask_for_fixed(self, err: str) -> str:
assert self.starting_prg is not None

result, validator = dafny_separate(err)
result, validator = self.language.separate_validator_errors(err)
if validator:
self.summarizer_llm.add_user_prompt(
"Here are the errors you need to summarize:\n" + validator,
Expand Down
13 changes: 0 additions & 13 deletions verified_cogen/tools/dafny_separate.py

This file was deleted.

0 comments on commit 7542085

Please sign in to comment.